Key words: Rendernode,threadedrenderer,displaylist,uvmapper,fontrenderer
What is hardware acceleration (what)
The traditional software UI drawing is done by CPU, and hardware acceleration is the execution of the drawing task on the GPU. GPUs are more suitable for time-consuming tasks such as rasterization and animation than CPUs, and the GPU is more power-saving and user-friendly than using CPUs to accomplish these tasks on mobile devices.
Why hardware acceleration (why)
The underlying implementation of Android's hardware acceleration is to complete the drawing by submitting instructions to the GPU based on the OpenGL ES interface. The hardware acceleration frame rate is higher than the CPU and is more efficient than the CPU-implemented soft drawing. The larger the screen resolution (especially for HDTV), the more obvious the hardware acceleration benefits.
Is the Hwui drawing execution process for Android 5.0:
Source code is located in directory Android/platform/framework/base/libs/hwui
Public class View implements drawable. Callback, keyevent. Callback, accessibilityeventsource {...... Public void Buildlayer() { ......FinalAttachinfo attachinfo = Mattachinfo; ......Switch(Mlayertype) { CaseLayer_type_hardware:updatedisplaylistifdirty ();if(Attachinfo.mhardwarerenderer! =NULL&& Mrendernode.isvalid ()) {AttachInfo.mHardwareRenderer.buildLayer (Mrendernode); } Break; CaseLayer_type_software:builddrawingcache (true); Break; } } ......}
When the view's layertype is set to hardware, the view binds to an OpenGL ES framebufferobject, if the view is to be animation (alpha,rotation,etc.) , the framebufferobject is rendered to texture (2D) and then animated, which has the disadvantage of increasing memory consumption.
Hardware acceleration implementation (how) stand-alone render thread
On Android 5, the advent of threadedrenderer reduces the burden on the main thread and can respond more quickly to user actions.
Deferred Display List
Displaylist records the drawing operation and state, the main thread submits them to the OpenGL renderer, and the renderer executes the final drawcall.
After Android 4.4, the Hwui module introduced the deferred display list, which made some optimizations based on the display list, such as culling areas that were drawn, batching or merging the drawcall, to some extent improving the performance of the drawing.
Create a texture Set
Assetatlasservice to the resources of the load in the special management, it in order to reduce the image resources uploaded to the GPU operation, the resources are merged, packaged into a single texture to upload, through uvmapper remapping texture coordinates , So that the UI can still be drawn correctly.
If the device supports OpenGL ES 3.0,android uses pixelbufferobject bindings to implement an asynchronous upload of textures, the benefit is that the mapping method (Glmapbuffer) is more efficient for uploading texture data to the GPU. The following code is from the implementation of the desktop side, the implementation of the mobile side is not very different.
//Bind texture unit and PBOGlbindtexture (gl_texture_2d, Textureid); Glbindbufferarb (Gl_pixel_unpack_buffer_arb, pboIds[Index]);//Copy the PBO pixels into the textureGltexsubimage2d (gl_texture_2d,0,0,0, WIDTH, HEIGHT, Gl_bgra, Gl_unsigned_byte,0);//Prepare to upload next copy of textureGlbindbufferarb (Gl_pixel_unpack_buffer_arb, Pboids[nextindex]);//Here if the direct call to Glmapbuffer will cause a synchronous check of the OpenGL state machine (overhead), but using bufferdata will not, he can directly allocate the data backGlbufferdataarb (Gl_pixel_unpack_buffer_arb, Data_size,0, Gl_stream_draw_arb);//mapping GPU data to memoryglubyte* ptr = (glubyte*) glmapbufferarb (Gl_pixel_unpack_buffer_arb, Gl_write_only_arb);if(PTR) {Updatepixels (PTR, data_size);//Update texture data directlyGlunmapbufferarb (Gl_pixel_unpack_buffer_arb);//Release the mapped buffer}glbindbufferarb (Gl_pixel_unpack_buffer_arb,0);
The use of Atlas reduces GPU memory consumption and the overhead of the texture unit (Texture unit) Binding call (Resource binding).
Merging Drawcall
In the process of Android hardware acceleration, depth testing (depthtest) is not enabled, and all UI drawing is displayed on the screen in the order in which they are drawn. Because of the complexity of the driver implementation of OpenGL ES, the binding invocation of each GL and the switching of the GL state are expensive, so the deferred Display list is introduced to Drawcall merge as well as batch, related classes, such as.
Font drawing
Text rendering is also a headache, the text rendering of the app is not as simple as in the game, usually when the game is released, you can pre-convert the fixed text into a single-sheet texture, rendering the text directly mapped texture coordinates. The android underlying caches the text glyph texture data. Android uses Skia and third-party open source font libraries (FreeType) to rasterize fonts.
Improvements in hardware acceleration
iOS UI drawing uses multiple GL Context, followed by the introduction of the metal Framework after iOS 8, native support for multi-threaded UI rendering, Android due to GL driver and GPU vendor differences do not work well to dry the GPU function, However, after the next generation of graphics API (Vulkan, driver thinning, multi-threading support) popularization, there is still a large space for optimization, UI fluency can be further improved.
Reference
- Efficient text rendering with OpenGL ES
- Rendering Text in Metal with signed-distance fields
- Lao Luo's Android tour
- How about some Android graphics true facts?
- Introduction to the Android Graphics Pipeline
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
An analysis of Android Hwui hardware acceleration Module