Optimizing Hardware Layers

Android applications running with hardware acceleration turned on can greatly benefit from using hardware layers. The point of this article is not to discuss hardware layers in details but to explain how you can quickly ensure you are not using them in a way that negatively affects performance. If you want to know more about layers, please refer to the Google I/O 2011 talk I gave with Chet Haase.

A view backed by a hardware layer can be rendered in a very efficient manner by the GPU. Updating a hardware layer adds quite a bit of overhead though. Every time a view backed by a hardware layer invalidates, the layer is cleared, the view is redrawn into the layer and the layer is then drawn on screen. This means the amount of work, in terms of fill-rate, is effectively doubled.

Layer updates are perfectly acceptable but should be kept to a minimum, particularly during animations. If a large layer updates multiple times during an animation you may see a drop in frame-rate. Android offers a developer option to help you track down this potential issue: Settings → Developer options → Show hardware layers updates.

Optimizing hardware layers

When this option is enabled, every update to a hardware layer will result in that layer to be highlighted in green for the duration of the frame during which the update happens. If your application is behaving properly you should only see brief green highlights. The default Android launcher is a good example of how layers should behave. Open the list of applications and swipe between pages to observe the proper behavior.

A layer that remains green across multiple frame indicates that this layer is updating to often, which can lead to a lower frame-rate. The video below shows what such a scenario looks like. You can see that the layers remain green for the entire duration of the animations, indicating that a view inside the layer gets invalidated on every frame.

Even though the impact on performance is negligible in this example due to the small size of the layers the widget should be optimized to avoid performing unnecessary work. If you are using hardware layers – or if you’re not sure you are –, turn this option on and make sure you are not doing extra work!

2 Responses to “Optimizing Hardware Layers”

  1. Puru says:

    Nice one. I wander how many of such are coming in the future since you’ve decided to leave android.
    We, all fellow developers will miss such fine tricks and tips blogs from you regarding android.