GPU hardware acceleration control in Android and its limitations on 2D graphics rendering

Source: Internet
Author: User

Graphics can be rendered in two categories: software rendering and hardware rendering. Software rendering relies on the CPU to calculate various coordinates and draw, mainly to occupy memory, hardware rendering is GPU-based, mainly video memory, the general 3D graphics programs (OpenGL, DirectX) are GPU-accelerated.

Before Android3.0, the 2D drawing API only supported software rendering mode, starting with Android3.0, 2D drawing API began to support GPU hardware rendering, that is, canvas drawing operations in view will use the GPU, so from Android 3.0 (API Level 11), there are some hardware-related methods in view. If the app's Androidmanifest.xml file defines a targetsdkversion greater than or equal to (Android 4.0), then Android will default to enable GPU-rendered 2D graphics for the app, We can also decide for ourselves whether to use the GPU, see below. If GPU hardware acceleration is turned on, Android will cache drawing commands in OpenGL ES using the Display list technology common in the OPENGGL drawing to improve drawing efficiency and speed. The display list drawing mechanism for GPU hardware acceleration in Android will be elaborated later in this article, this article does not introduce too much.

Control whether to use the GPU

We can also explicitly enable or disable GPU rendering, and can control it from multiple application, Activity, Window, and view levels.

  • Application

    Add the following attributes to the Androidmenifest.xml to enable GPU hardware accelerated rendering of 2D graphics across all activity's view of the app:

    <application android:hardwareAccelerated="true" ...>

  • Activity
    You can control whether the GPU is enabled at the application level or you can control it at the activity level. For example, you have multiple activity in your app, and you want most of your activity to enable GPU hardware acceleration, but there's an activity you don't want to enable hardware acceleration, you can do it with the following configuration:

    < Code class= "Hljs r" ><application android:hardwareaccelerated= "true"  > < Activity /> <activity android:hardwareaccelerated=" false "/></application>  
  • Window
    If you want to control the use of the GPU more granular, you can enable GPU hardware acceleration for the specified Window through code, as shown in the following code:

    getwindow () .setflags  (Windowmanager  _hardware_accelerated, Windowmanager  _hardware_accelerated) ;   

    It is important to note that hardware acceleration for a window cannot be disabled by code at run time.

  • View
    You can also disable GPU hardware acceleration for a given view at run time by using the following code:

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB){    11才加入setLayerType方法    myView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);}

    View joins the Setlayertype method from API level 11, so you need to judge the current version of the system before using it. It is important to note that GPU hardware acceleration cannot be enabled for a view at run time by code.

Determine if you are currently in hardware acceleration

Starting with Android 3.0 (API level 11), both the view and canvas classes have joined the ishardwareaccelerated () method, which can be used to determine whether the current view and canvas are in hardware acceleration.

    • View.ishardwareaccelerated ()
      If the view's ishardwareaccelerated () method returns True, it simply means that the view is added to a hardware-accelerated window, and that it is possible to still use a non-hardware-accelerated canvas for actual rendering. Therefore, the ishardwareaccelerated () method of the view is generally not very useful.

    • Canvas.ishardwareaccelerated ()
      We can get the canvas object in the view's OnDraw callback method, if the canvas's ishardwareaccelerated () method returns True, it means that the current canvas is being rendered with GPU hardware acceleration, If False is returned, it is rendered with software. In general, it is important to determine whether the current canvas is in GPU hardware acceleration for drawing a custom view, as explained below.

Limitations of 2D graphics drawing when hardware acceleration

Turning on GPU hardware acceleration can improve the efficiency of your program's drawing, but there are some limitations.

  1. Enabling GPU hardware acceleration increases the use of memory.

  2. Some 2D drawing APIs in Android are not available for GPU hardware acceleration or to a specific version.

    • Canvas
      The following features are limited by the GPU hardware acceleration in canvas:
      The first column is the restricted method, the second column is the API level to start supporting, and the Red Fork is not currently supported.

    • Paint
      The following features are limited by the GPU hardware acceleration in Paint:

    • Xfermode
      The following features are limited by Xfermode in GPU hardware acceleration:

    • Shader
      The following features are limited by shader in GPU hardware acceleration:

    • Canvas Zoom
      Hardware-accelerated 2D rendering pipelines in Android initially support only non-scaled drawings, which results in significantly lower drawing quality when the scale is set to large. Initially, the 2D drawing operation under GPU acceleration is rendered into a texture with a scale of 1.0, and the GPU zooms to the specified scale. When the API level is less than 17, the quality of the drawing becomes more difficult to guarantee as the scale scales scale. The following table shows what version of Android can correctly handle 2D graphics in GPU hardware calculations with large scale scaling issues:

    • Now we develop the app generally write targetsdkversion as the latest version, certainly greater than the API level 14, and the market is the majority of mobile phones are Android 4.0 or more, So the app we're currently developing by default on most phones is basically the GPU hardware acceleration enabled by default. If we're going to customize a view ourselves, we're going to rewrite its OnDraw method to implement complex effects by invoking various drawing methods, but if we call an API that doesn't support GPU hardware acceleration, we can't draw the results we want, for example, For example, if we want to draw an ellipse with a blur effect in the custom view, we need to call the Setmaskfilter () method of the brush paint, but we can see from the list of restricted APIs above that, with GPU hardware acceleration, pait's Setmaskfilter () method is not supported, although the call does not error, but does not have any effect. To draw the effect we want, we can disable GPU hardware acceleration by using the view's Setlayertype (View.layer_type_software, null) method to separate our view. This allows all 2D drawing APIs to work properly in software rendering mode.

    • Finally, there is a need to explain that Android in the GPU hardware accelerated 2D graphics Drawing API limitations are based on the current latest API level 23, as soon as the release of the Android version later, it is possible that the above-mentioned limited API will be gradually better supported under the GPU.

Hopefully this article will help you get an initial look at GPU hardware rendering 2D graphics in Android, and later write an article that explores Android's display list mechanism for drawing 2D graphics under GPU hardware rendering.

Related reading:
My Android Blog Finishing summary
The basics of canvas drawing in Android (with source download)

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced. If you think the article is good, remember the top! If there is doubt that there are errors in the article, you are welcome to give me a message in the comments, see will be timely reply, a lot of communication

GPU hardware acceleration control in Android and its limitations on 2D graphics rendering

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.