Why your Android apps suck

Source: Internet
Author: User
Tags skia
Why I wrote this article?

When I learn more about Android's graphics system, and do more work about how to use CPU/GPU in more paralleled Way to Improve the graphics performance in Android, I start to think that there are actually some big design mistakes in Android graphics system,
Especially the rendering architecture in the client side.

Some mistakes have been solved after 3.x, especially above 4.1, but others can never be solved due to the compatible reason. as developers, we need to know how the android graphics system work, how to utilize the new features Android 3.x and 4.x provided, and
How to do The optimazation and overcome the age of Android.

Direct Rendering

In the very beginning, Android's client/server architecture of its graphics system choose the direct rendering mode, Android team develop a lightweight window compositor-surfaceflinger by themself, it response to create Native Windows, allocate graphics Buffers
And composite the window surface to display (via framebuffer ). in client side, apps (through Android runtime) can use CPU to access the graphics buffer of native window directly (through skia), or use GPU to manipulate it indirectly (through gles ). this direct
Rendeing mode is more appropriate than the traditional X in Desktop Linux.

Window composite in server side

Surfaceflinger use gles1 to do the window composition (via texture operation) in the very beginning, this cause two issues:

  • GPU may have more efficient way to do the window composition than use gles;
  • When the client side also use gles to render the window surface, it may competes with the server about the GPU resources;

Just mention, when GPU do not support gles1, Android has a built-in SW version gles1 stack, and it can use copybit Hal module provided by chip vendor to accelerated the texture operation.

So, after 3.0, Android introduce hwcomposer Hal module to solve these issues, also abandon the old copybit module. chip vendor can through the implementation of hwcomposer module to declare they can do all of or part of Windows 'composition by themselves, usually
With dedicated hardware and always more efficient than use gles. Also, after 4.1, hwcomposer can provide the vsync signal of the display, so that android can sync three things together:

  • The Windows composition in server side
  • The rendering of window surface in Client Side
  • The refresh of Display

Rendering architecture in Client Side

The most mistake Android team make is the rendering architecture of its GUI framework:

  1. It do not has a layer rendering architecture (or called scene graph in some gui fw );
  2. It do not has a dedicated rendering thread to render the window surface;
  3. It's rendering only use the CPU until 3.0;

The first one is partially support after 3.0, the third is support after 3.0, but the second problem can never be solved...

Compare to IOS

In iOS, every view has a layer as its backing store, app can create more layers for better performance. view's content will be drew into its layer, as long as the content do not changed, the view do not need to draw itself again. IOS do a lot of things to avoid
Change the content of a view, Please modify properties of a view can be changed without affect to its content, such as background, border, opacity, position, transformation, even the geometry !!!

The composition of layers done by another dedicated rendering thread, it always use GPU to draw each layer to the window surface. the main thread only reponse to handle touch event, relayout the views, draw view's content into its layer when necessary, Etc...
So the main thread only use CPU and the rendering thread use GPU mostly, and I think there will be just a few synchronization between these two threads, and they can run concurrently in most of time.

But in Android, the main thread need to do everything, such as handle touch events, relayout views, dequeue/enqueue graphics buffer, draw views 'content on window surface, and other things need to be done by app... and it only use CPU before 3.0 !!! Even
Position of a view just change one pixel, android need to redraw its content along with the contents of other views overlapped, and the content drawing is very expensive for CPU !!!

The improvements

A lot improvements have been made after 3.0 to overcome the lifecycle age of previous versions. android 3.0 introduce a new hwui module, and it can use GPU to accelerated the drawing of view's content, it create a HW accelerated canvas to replace the old SW canvas,
The new canvas use OpenGL ES as its backend instead of use skcanvas from skia.

Android 3.0 also introduce the displaylist mechanic, displaylist can be considered as a 2D drawing Commands Buffer, every view has its own displaylist, and when its ondraw method called, all drawing commands issue through the input canvas actually store
Its own displaylist. when every displaylist are ready, Android will draw all the displaylists, it actually turn the 2D drawing commands into gles commands to use GPU to render the window surface. so the rendering of view hierarchy actually separated into two
Steps, generate view's displaylist, and then draw the displaylists, and the second one use GPU mostly.

When app invalidate a view, android need to regenerate its displaylist, but the overlapped view's displaylist can keep untouched. also, Android 4.1 introduce displaylist properties, displaylist now can has same properties such as opacity, transformation, Etc ...,
And the changed of some properties of view will just cause changed of corresponding properties of its displaylist and need not to regenerate it. These two improvements can save some times by avoid regenerate displaylists unnecessary.

Although android can never has a layer rendering architecture, it actually introduce some layer support after 3.0, a view can has a layer as its backing store. the so called HW layer actually back by a FBO, if the content of view is too complicated and unlikely
To change in the future, use layer may help. also, when a view is animating (but content do not change), cache itself and its parent with layers may also help. but use layer with caution, because it increase the memory usage, if your want to use layers
Animation, your may need to release them when the animation is finish, Android 4.2 provide new animation API to help you about that. also, because Android use gles to draw the content of view, so most views 'Drawing will be fast enough, and the use of Layer
May be unnecessary.

Android 4.0 also introduce a new type of native window-surfacetextureclient (back by a surfacetexture) and its Java wrapper textureview, app can create and own this kind of native window and response to its composition. if the content of view is too complicated
And continue to change, textureview will be very helpful, app can use another thread to generate the content of textureview and has y the main thread to update, and main thread can use the textureview as a normal texture and draw it directly on the window
Of current activity. (textureview can also replace the usage of original surfaceview and glsurfaceview)

Android 4.1 make the touch event handling and UI drawing sync with the vsync signal of display, it also use triple buffers to avoid block the main thread too often because it need to wait the surfaceflinger to do the page flip and release the previous front
Buffer, and surfaceflinger will always sync with vsync signal of display.

The OpenGL Renderer for HW accelerated canvas is continue be improved and become faster, especially for complicated shape drawing.

But...

But android can never has a dedicated rendering thread... although the drawing is much faster than before, and keep the changed of everything as little as possible during animating, it still need to share the 16 ms interval with other jobs in main thread
Achieve 60 FPS.

So...

So, as developer, We need to utilize the improvements in higher version android as much as possible:

  1. Turn on the GPU acceleration switch above Android 3.0;
  2. Use the new animation framework for your animation;
  3. Use layer and textureview when necessary;
  4. Etc...

And avoid to block the main thread as much as possible, that means:

  1. If your handle the touch events too long, do it in another thread;
  2. If your need to load a file from sdcard or read data from database, do it in another thread;
  3. If your need to decode a bitmap, do it in another thread;
  4. If your view's content is too complicated, use layer, if it continue to change, use textureview and generate its content in another thread;
  5. Even your can use another standalone window (such as surfaceview) as a overlay and render it in another thread;
  6. Etc...
Golden rules for butter graphics
  • Whatever you can do in another thread, then do it in another thread;
  • Whatever you must do in main thread, then do it fast;
  • Always profiling, it is your most dear friend;

All you need to do is keep the loop of main thread under 16 ms interval, and every frame will be perfect!

The last word

When I finish this article, what make me think most is, when you make a big design mistake in the very beginning, and you can not change it due to some reasons, no matter how hard you try to patch it in future, it will never get perfect again.

Android team make huge effects to provide features like strict mode, asynctask, concurrent & background GC, HW accelerated canvas, HW layer, textureview, vsync, triple buffers, Etc... all they do just about two things, do everything you can in another thread,
And make the must thing in main thread faster, and these actually help a lot. but no matter how hard you try to use these features to improve your app, it is nearly impossible to get every frame perfect, because it is so hard to forecast everything the main
Thread will do in every loop, it may suddenly jump into something totally uncontrollable by you app and make you break the 16 Ms curse.

And the worse is, If you has a good design, you can continue improve it (like utilize more advance hardware ), but the developer need not to know anything about this and do not need to change even one line of their code, if the app run on higher version OS
Faster device, it just got the performance boost. if you has a bad design, although you do a lot of things to patch it, but the developer need to know why, they need to rewrite their code to use these new features, they need to know how to avoid the trap,
They need to know how to make compatible with older version system, and too many other things...

This is too hard for a developer just want to build a useful and beauty app...

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.