Android 4.0 hardware acceleration

Source: Internet
Author: User

1. Use of Android 4.0 hardware acceleration

1.1 control level of hardware acceleration

The simplest way to enable hardware acceleration is to enable the global settings of hardware acceleration for the entire system. If your program is a standard view or drawable, the global setting of hardware acceleration will not cause adverse effects. However, hardware acceleration does not support all 2D painting operations, so enabling hardware acceleration may affect applications that use custom components, the problem is often manifested in invisible element exceptions and incorrect pixel rendering. To solve this problem, Android allows you to choose to start or disable the following levels of hardware acceleration: application activity window and view.

1.1.1 application level

Add property tags to your android manifest file to enable hardware acceleration for the entire application.

1.1.2 activity level

If your application cannot perform well at the application level, you can use the activity for separate control. To enable or disable hardware acceleration for an activity, you can use the Android: hardwareaccelerated attribute of the activity. The following column enables hardware acceleration for the entire application, but hardware acceleration is not allowed for an activity.

1.1.3 window level

If you need more fine-grained control, you can use the following code to accelerate the window.

 
 
  1. getWindow().setFlags( 
  2. WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED, WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); 

Note: currently, you cannot disable hardware acceleration for Windows.

1.1.4 view level

We can disable hardware acceleration for a separate view during runtime. We can use the following code:

 
 
  1. myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 

Note: currently, hardware acceleration cannot be performed at the view level.

1.2 determine whether a view has enabled hardware acceleration

Sometimes we need to know whether an application has enabled hardware acceleration, especially for some custom controls. Because your application does a lot of custom "painting" operations, but not all processes support the new "painting" rendering process.

There are two different ways to check whether the application has enabled hardware acceleration:

1.2.1 use view. ishardwareaccelerated (). If true is returned, hardware acceleration is enabled in the View window.

1.2.2 canvas. ishardwareaccelerated () if true is returned, hardware acceleration is enabled for the canvas.

If you have to check whether your painting code has been accelerated, use canvas. ishardwareaccelerated () instead of view. ishardwareaccelerated () if possible (). When a view exists on an accelerated Windows server, you can use a canvas without hardware acceleration to paint it. For example, when we draw a view to bitmap and use it as a cache.

2. Draw models for Android 4.0

When hardware acceleration is enabled, the android framework uses a new rendering model that displays your application on the screen using the display list. To fully understand the display list and how they affect your applications, it is necessary to understand how Android 4.0 draws views without hardware acceleration, the following describes Software Acceleration and hardware acceleration respectively.

2.1 software-based drawing Model

In a software-based drawing model, view is drawn in the following two steps: 1. Make the entire control level invalid. 2. Draw layers.

When an application needs to update a part of its UI, it will call the invalidate () method (or the variant of invalidate) of the view whose content is changed ). The invalidate message is passed up based on the hierarchical relationship of the view to calculate the part to be repainted (that is, the dirty area ). The Android system then draws all views that have an intersection with the dirty area. Unfortunately, this model has two disadvantages:

2.1.1 when drawing on different layers in this model, a lot of code will be executed. For example, a button is located on another view. When invalidate () is called for a button, Android re-draws the view even if the view has not changed.

2.1.2 The second problem is that this rendering model will hide bugs in your application. Because the Android system will re-paint the view that has an intersection with the dirty area. In this case, if the content of a view changes, even if the invalidate () of the View () the method is not called, and it may be re-painted. You will rely on other controls that call invalidate () to obtain the correct behavior. Therefore, this behavior will change whenever your application changes. It is also based on the secondary cause. In your custom control, you must constantly call the invalidate () method. When your data or status affects the View Drawing code.

Note:Android view automatically calls invalidate () when their attributes change (). For example, you can change the background or text of A textview.

2.2 hardware-based acceleration Model

The Android system still requests screen update and re-rendering through invalidate () and draw (), but the actual processing method is different. Instead of executing the painting command immediately, Android records all the painting commands in a display list, which contains the output view-Level Drawing code. Another optimization is that android only records and updates the display level in the display list and marks the view as "dirty" by calling the invalidate () function. A view that has not been refreshed can be re-painted by re-requesting the previous display list. The new model has three steps: 1. Disable the entire view level. 2. record and update the display list. 3. Draw a display list.

Using this model, you cannot rely on a view or dirty area to execute the draw () method. To ensure that the Android system records a view display list, you must call the invalidate () method. If you forget to call the refresh method, the view will look the same even after a change, this is a relatively easy way to discover bugs.

The display list method is also very good for the animation performance, because you do not need to request to refresh the target view (this will be automatically executed) when setting the specified attribute value, such as transparency or rotation ). This optimization is also applied to views with a display list (view with hardware acceleration enabled). For example, a linearlayout contains a listview and a button, and the listview is on the button. The display list of linearlayout is as follows:

◆ Drawdisplaylist (listview );

◆ Drawdisplaylist (button );

If you want to update the opacity of the listview, after setting the setalpha (0.5f) attribute of the listview, the display list of linearlayout should include the following:

◆ Savelayeralpha (0.5)

◆ Drawdisplaylist (listview)

◆ Restore

◆ Drawdisplaylist (button)

In this case, the complex process of creating a listview is omitted. Instead, the display list of linearlayout is updated. If an application does not enable hardware acceleration, the code drawn by listview and its parent view will be re-executed.

3. Android 4.0 view layer

Classification at Layer 1

All Android versions have the ability to render the off-screen buffer, use the view Rendering Buffer, or use the canvas. savelayer () function. The off-screen buffer or layer can have many applications, for example, it can make the animation effects of complex view processing or some compositing effects better. For example, you can use canvas. savelayer () to perform a gradient effect on the view and render it to the layer, and then combine the opacity effect to display it to the screen.

Starting from Android 3.0, you can use view. the setlayertype () method has more control over when and how to use the layer. This API has two parameters: one is the layer type you want to use, another optional parameter, paint, indicates how the layer is superimposed. You can apply the paint parameter to Color Filtering, especially in the mixed mode or opacity of a layer. One view can use one of the following three layer types:

◆ Layer_type_none: This view will be rendered in normal mode, but will not return an off-screen buffer. This is the default action.

◆ Layer_type_hardware: if the application is accelerated by hardware, the view will be rendered as hardware texture in the hardware. If the application is not accelerated by hardware, the effect is the same as that of layer_type_software.

◆ Layer_type_software: this view is rendered as a bitmap through software.

Use of Layer 3

The type of the Use layer depends on your purpose:

3.2.1 performance: Use the hardware layer to render a view into a hardware texture. Once a view is rendered as a layer, its drawing code will not be executed until you call the invalidate () function. For some animations, such as transparent animations, which can be directly applied to a layer, this is the most efficient way to use GPU.

3.2.2 display effect: Use the hardware or software layer and paint to perform special visual processing on a view. For example, you can use colormatrixcolorfilter to implement the black/white effect on a view.

3.2.3 compatibility: using the software layer type forces a view to be rendered in the software. If a view is accelerated by hardware (for example, if you set the entire application as accelerated by hardware) and rendering problems occur, this is a very simple way to restrict the hardware rendering process.

3.3 relationship between view layers and animations

When your application uses hardware acceleration, the hardware layer can provide faster and smoother animation effects. When performing animation operations on a complex view, it is not always possible to reach 60 frames per second because many painting operations are required. In this case, you can use the hardware layer to render the hardware texture to improve performance. Hardware texture operations can be used to animation a view. When animations are performed, frequent repainting of the view itself can be reduced. Unless you change the attribute of this view (call the invalidate () method) or manually call invalidate (). If you run an animation in your application but it does not get the smooth effect you want, you can consider opening the hardware layer for the view to be animated.

When a view is returned through the hardware layer, some properties of the view will be processed at the same time when the final screen is displayed after all layers are superimposed. Setting these attributes is very efficient because they do not need to deploy invalidate and redraw the view. The following attributes will affect the overlay of the layer. setting these attributes will automatically refresh the view, and you do not need to repaint the view.

◆ ALPHA: Change the transparency of the layer.

◆ X, Y, translationx, and translationy: Change the layer position.

◆ Scalex and scaley: Change the layer size

◆ Rotation, rotationx, and rotationy: Change the layer direction in 3D space

◆ 127tx, 0000ty: Specifies the starting point for deformation.

These attributes are used for animation operations on a view through the objectanimator object. If you want to access these attributes, you can directly call the setter or getter methods of these attributes, for example, if you want to change the view's Alpha, call setalpha () directly (). The following code snippet shows a view that uses the Y axis for 3D Rotation.

 
 
  1. view.setLayerType(View.LAYER_TYPE_HARDWARE, null); 
  2. ObjectAnimator.ofFloat(view, "rotationY", 180).start(); 

Because the hardware layer consumes video memory, it is strongly recommended that you enable them during animation. When the animation is completed, you can disable them through animation listening. The Code is as follows:

 
 
  1. View.setLayerType(View.LAYER_TYPE_HARDWARE, null); 
  2. ObjectAnimator animator= ObjectAnimator.ofFloat(view, "rotationY", 180); 
  3. animator.addListener(new AnimatorListenerAdapter() { 
  4. @Override 
  5. public void onAnimationEnd(Animator animation) { 
  6. view.setLayerType(View.LAYER_TYPE_NONE, null); 
  7. }); 
  8. animator.start();

4 Android 4.0 tips and tips

Switching to a hardware-accelerated 2D image can increase performance immediately, but you still need to design your applications to use the GPU more efficiently with the following suggestions.

4.1 Reduce the number of views used in your program

The more views you draw in your system, the slower your program will be. The same is true for the process of drawing in software, reducing the number of views is the easiest way to optimize your UI.

4.2 avoid drawing too much

Do not overwrite the stack layer. When a view is completely blocked by other views, it is best to remove the blocked view. If you need to draw different layers for a superposition effect, consider merging these layers into a layer. From the current hardware perspective, there is a good experience that each frame of an animation should not draw 2.5 times the number of pixels of excess screen pixels (the transparent pixels in bimap are also included ).

4.3 do not create a drawing object in the painting Method

A common error is that when the painting method is called, a new paint or path must be created each time. This will force the Garbage Collector to run too frequently, which will affect the buffer and hardware rendering.

4.4 do not modify the shape too frequently

Taking complex shapes, path, and rotation as an example, texture masks are used for rendering. Each time you create or modify a path, the hardware rendering process creates a new mask, which is costly.

4.5 do not modify bitmap too frequently

Each time you modify the content of a bitmap, it will be uploaded as a GPU texture when you draw it again.

4.6 Be careful when using the alpha channel

When you use setalpha, alphaanimation, or objectanimator to set the transparency of a view. It will require two times of off-screen Rendering Buffer filling rate. When an Alpha is applied to a large view, consider setting the view layer type to layer_type_hardware.

The author of this article Weibo for http://weibo.com/zuiniuwang welcome to contact him.

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.