Search code examples
androiddrawingandroid-canvasandroid-viewhardware-acceleration

Hardware layers and hardware acceleration on Android


I just read about Hardware Acceleration and watched this video. Now I feel a little confused.

Hardware acceleration means that drawing operations are performed by the GPU, and it's enabled on Android 3.0+ by default. If hardware acceleration is enabled, why does a View's layer type is LAYER_TYPE_NONE by default? I thought it should be LAYER_TYPE_HARDWARE.

Hardware layers have to do something with keeping the View in an off-screen buffer.

So it looks like the constants View.LAYER_TYPE_NONE, View.LAYER_TYPE_HARDWARE, and View.LAYER_TYPE_SOFTWARE are not about hardware acceleration. They are about keeping the View in the off-screen buffer to avoid drawing every time View's transparency and some other properties change.


Solution

  • View.LAYER_TYPE_HARDWARE means that the view will be backed by a hardware layer e.g. a texture, FBO... Thus it is easy/almost free to apply color effects, translucency and other GPU effects without the need to redraw the view. It is good to enable the hardware layer when the view has such effects or before it will be animated. Hardware layers takes a lot of RAM though, so be careful.

    Documentation here: LAYER_TYPE_HARDWARE and a quotation from it:

    Indicates that the view has a hardware layer. A hardware layer is backed by a hardware specific texture (generally Frame Buffer Objects or FBO on OpenGL hardware) and causes the view to be rendered using Android's hardware rendering pipeline, but only if hardware acceleration is turned on for the view hierarchy. When hardware acceleration is turned off, hardware layers behave exactly as software layers.

    A hardware layer is useful to apply a specific color filter and/or blending mode and/or translucency to a view and all its children.

    A hardware layer can be used to cache a complex view tree into a texture and reduce the complexity of drawing operations. For instance, when animating a complex view tree with a translation, a hardware layer can be used to render the view tree only once.

    A hardware layer can also be used to increase the rendering quality when rotation transformations are applied on a view. It can also be used to prevent potential clipping issues when applying 3D transforms on a view.

    EDIT: To answer your question, the layer is by default NONE so it does not take RAM. HW accelerated drawing and a hardware layer are two different things. HW accelerated drawing means that the draw commands, e.g. when drawing into a Canvas, are done by a GPU instead of CPU. HW layer means that the result of drawing is saved into the GPU's memory so the result (e.g. a texture) can be later displayed instead of re-drawing the view again.

    EDIT 2: The first edit is my own but I was signed out of SO while editing :D