Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 209
Maksim Dmitriev Avatar asked Sep 09 '17 13:09

Maksim Dmitriev


2 Answers

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

like image 128
shelll Avatar answered Oct 14 '22 15:10

shelll


In simple language, take it like: Hardware acceleration is a feature available by default on API 14+, but isn't supported for all the 2D drawing operations. Which means some custom views might not support it, but because it's enabled by default, it might cause some of the custom views rendering issues.

Check below link for one if the issues: http://postimg.org/image/6wzssbdbr/

Coming to Layer types, In all versions of Android, views have had the ability to render into off-screen buffers, either by using a view's drawing cache, or by using Canvas.saveLayer(). Off-screen buffers, or layers, have several uses. You can use them to get better performance when animating complex views or to apply composition effects.

LAYER_TYPE_NONE: The view is rendered normally and is not backed by an off-screen buffer. This is the default behavior for all APIs from 1 to 13.

LAYER_TYPE_HARDWARE: The view is rendered in hardware into a hardware texture if the application is hardware accelerated. If the application is not hardware accelerated, this layer type behaves the same as LAYER_TYPE_SOFTWARE. This is the default behavior for all APIs 14+.

LAYER_TYPE_SOFTWARE: The view is rendered in software into a bitmap.

If required for any view, we can disable it by setting yourViewObject.setLayerType(View.LAYER_TYPE_SOFTWARE, null);.

Hope this explains.

like image 21
Surendra Kumar Avatar answered Oct 14 '22 17:10

Surendra Kumar