Friday, July 21, 2017

Little planets effect (小行星特效)



I got a task to implement this effect recently.
Before it's assigned to me, there was a fragment shader in the codebase to handle this effect.
But the view angle can not be rotated.
So I try to "google" whether there is any information about it.
Then I found this page .
It's a way to project a sphere to a plane.
It's called "stereographic projection";

Φ = asin( cos(c) sin(Φ0) + (y sin(c) cos(Φ0) / )
λ = λ0 + atan( x sin(c) / (r cos(Φ0) cos(c) - y sin(Φ0) sin(c) )
I can use the above equation to calculate the position correspond to the x,y coordinate on the plane. 
After I adjust the fragment shader by the equation, the user can watch this planet at any angle.
It's quite interesting to try the OpenGL shader, I can generate such amazing results by a few lines of code.

By the way, if you would like to try such kind of effect by your self, you can download  Insta360Player.
If I didn't find a way to implement the little planets effect, I will try to decompile it's apk.


Saturday, July 15, 2017

Android OpenGL Texture memory occupied

I try to solve an interesting memory issue recently.

There is a viewer page in a project I owned recently.
The whole app will be closed without showing the "force close dialog" if we keep entering and leaving that page.
It seems like a memory leak , so I try to use LeakCanary to check it but found nothing related.

Then I try to use the  Memory Monitor  in Android Studio to monitor the memory usage.
But all the memory is released after the GC.

So I can only record the logs during that period and check it line by line.
Then I notice that many process is killed by Activity Manager during that period.
Even a process with OomAdj 0 (contain a foreground activity) is killed too.
It's quite abnormal that Activity Manager will kill so many process when I can't found any app spend lots of memory.
It pops into my mind there is a lesson learned I got in HTC .
In the early version of Android , the memory of a Bitmap can be allocated in the native layer.
Is it the reason it's not watched by the Memory Monitor?

So I try to survey whether there is any command I can use to check the memory usage in native layer.
Then I found the command below.


adb shell dumpsys meminfo <package_name|pid> [-d]

After I execute it, I LOL.
EGL mtrack is fucking high!!



It seems like the memory related to the OpenGL in this page is not released correctly.
After I found the hint, it's much easier to solve this issue.