Android Hardware acceleration

Source: Internet
Author: User

Overview

Starting with Android 3.0 (API L11), Android is starting to use hardware acceleration for 2D rendering, hardware acceleration means that graphics images drawn on the view in Android are drawn using the GPU, using hardware acceleration and, most of the time, making the drawing smoother, But the cost of paying is to consume more memory resources.

Hardware acceleration is on by default on the API L14, and for basic view drawing, hardware acceleration can increase the flow of the drawing, but note that not all 2D graphics APIs support hardware acceleration.

By "Force GPU rendering" in the developer options, users can turn on hardware acceleration globally. In more than 3.0 of Android, the system has been hardware-accelerated for most animations.


Control hardware Acceleration

Because hardware acceleration is not supported for some 2D drawing APIs, the Android system provides four levels of control.


Application level

In the app's Android manifest file, add the following attributes to the <application> element to enable hardware acceleration for the entire application, as shown in the following code:

Android:hardwareaccelerated= "true"


Activity level

Slightly smaller than the entire app, we can control it within the scope of the activity, as shown in the code below:

Android:hardwareaccelerated= "true"


Window levelfor a single window,android, you can also control hardware acceleration, as shown in the following code:
GetWindow (). SetFlags (        WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED,        WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);

But for Windows, Android can't disable hardware acceleration.



View level

The view level is the most controlled level of hardware acceleration, and we can suppress hardware acceleration by code such as the following:

Setlayertype (view.layer_type_software, NULL);

Contrary to the window level, we cannot turn on hardware acceleration at the view level.


Determine if the view has hardware acceleration turned on
There are two ways to determine whether a view supports hardware acceleration, as follows:


View.ishardwareaccelerated () canvas.ishardwareaccelerated ()

try to use the method of judging the canvas object.

Android Drawing Mode
compared to whether to turn on hardware acceleration mode, Android's drawing mode is divided into two types--software-based drawing mode and hardware-based drawing mode.

Software-based drawing mode

The software-based drawing mode requires the following two procedures when redrawing a view:

    • Invalidate the Hierarchy
    • Draw the Hierarchy

when repainting is required, the system emits a invalidate () signal and is passed in the view tree to calculate the area that needs to be redrawn, but there are two disadvantages to this drawing:

    1. When we only need to redraw a view in the views tree, the view in the views tree is redrawn, and traversing the view tree is a waste of time. For example, a Viewa on another viewb, even if B does not change, redraw a, B will redraw.
    2. This way hides the bug in the drawing, such as the example above, because Viewa, viewb overlap each other, there is a need to redraw the dirty region, then if b forgot to redraw the logic, then a to redraw, will be B redraw, That means using the wrong behavior to get the right phenomenon. It is for this reason that developers need to ensure that the correct invalidate () method is called when the view needs to be redrawn.

Hardware-based drawing mode
Hardware-based plotting is also redrawn using the invalidate () signal, but it is drawn and rendered in a different way. Android Internal maintenance A display list is used to record the state of the view tree. When the invalidate () signal is received, the system only needs to change the display list of the view that needs to be redrawn, while the other unchanged views only need to use the original display list, and the whole process is divided into three parts:

    • Invalidate the Hierarchy
    • Record and update display lists
    • Draw the display lists

This way, you can avoid the 2nd bug in the software drawing.

For example, if you have a linearlayout layout for a ListView object that contains a button object, the display list for the LinearLayout layout is as follows:

1. Drawdisplaylist (ListView);

2.DrawDisplayList (Button)

Assuming that you want to change the opacity of a ListView object now, when you call the Setalpha (0.5f) method of the ListView object, the display list contains the following processing:

1.SaveLayerAlpha (0.5);

2.DrawDisplayList (ListView);

3. Restore;

4.DrawDisplayList (Button)


Drawing methods that do not support hardware acceleration

If your application is affected by the functionality or limitations of these errors, you can turn off hardware acceleration by calling the Setlayertype (View.layer_type_software, null) method to the parts of the application that are affected.

Which methods do not support hardware acceleration can refer to this document on the Google developer website http://developer.android.com/guide/topics/graphics/hardware-accel.html


View Layers


In all versions of Android, view is able to get off-screen buffering with the Canvas.savelayer () method, which can be used in a variety of ways, from the screen buffer or layer, especially when rendering complex animations or using composite effects for better performance. For example, you can use Canvas.savelayer () to achieve a fade-out effect by temporarily rendering a view object in a layer and then compositing it and the opacity factor onto the screen.

Starting with Android3.0 (API level 11), use the View.setlayertype () method to take more control of the way the layer is used and when the opportunity is available. This API requires two parameters: one is the type of the layer and the other is optional, which describes the paint object to which the layer should be synthesized. The ability to use this paint object for color filtering, special blending modes, or layer transparency. The view object is capable of using the following three layer types:


    • The Layer_type_none:view object is rendered in a normal manner and is not returned by an off-screen cache. This type is the default behavior;
    • Layer_type_hardware: If the application is hardware-accelerated, then the view object is rendered in a hardware texture on the hardware. This layer type behaves the same as Layer_type_software if it is not accelerated by hardware.
    • The Layer_type_software:view object is rendered in a bitmap in the software.

The type of layer to use depends on the following targets:

    • Performance: Use the hardware layer type to render the view into a hardware texture. Once the view object is rendered to a layer, its drawing code is not executed until the invalidate () method of the View object is called. For some animations, such as alpha animations, you can use the layer directly, which is very efficient for the GPU.
    • Visual effects: Use a hardware or software layer type and a paint object to apply some special visual processing to a View object. For example, use the Colormatrixcolorfilter object to draw a black-and-white view object.
    • Compatibility: Using the Software layer type forces a view object to be rendered in the software. If the view object is being rendered with hardware acceleration (for example, if the entire application is hardware accelerated), it is an easy way to use the software layer type to address the hardware rendering pipeline limitations.


View layers and animations

When the application layer uses hardware acceleration, the phone display hardware can make the animation, the display effect smoother. If your animations aren't smooth enough, then you need to consider using hardware acceleration on the view plane for optimization. In Android, some view operations that use hardware acceleration are very efficient because these operations do not require objects to be redrawn after they are invalidated, for example:

1. Alpha: Change the transparency of the layer

2. X,y,translation,translation: Change the position of the layer

3. Scalex,scaley: Changing to zoom

4. Rotation,rotation,rotationy: Change the orientation of the view in 3D space

5. Pivotx,pivoty: Changing the control point of a layer



Animations for the above properties are very efficient to draw when hardware acceleration is turned on, and the following code shows how to efficiently rotate the view object around the y axis in 3D space, as shown in the code below:

View.setlayertype (View.layer_type_hardware,null); Objectanimator.offloat (View, "RotationY", (). Start ();

However, because hardware acceleration consumes system memory, it is strongly recommended that hardware acceleration be canceled after the animation ends, as shown in the following code:

View.setlayertype (view.layer_type_hardware, null); Objectanimator animator = Objectanimator.offloat (View, "RotationY Animator.addlistener (New Animatorlisteneradapter () {    @Override public    void Onanimationend (animator Animation) {        view.setlayertype (view.layer_type_none, null);    }}); Animator.start ();


Disadvantages of hardware acceleration

When we use hardware acceleration, we consume more memory (nearly 4 times times), and if there is not so much memory allocated to the application, it is divided from the memory left in the system, so hardware acceleration does not apply to the core process of the system. Good application, you should find a balance between hardware acceleration (GPU) and software drawing (CPU).

In addition, it is important to note that UI rendering in the app is not concurrent, and multi-core does not bring a performance boost on the display. At the same time, resolution is also a very important aspect of the smoothness, in order to be able to smooth drawing effect at higher resolution, then need to use a more powerful GPU for rendering.















Android Hardware acceleration

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.