Android display Architecture Analysis 7-surfaceflinger Process Analysis

Source: Internet
Author: User

According to the previous introduction, surfaceflinger serves as a server process, and upper-layer applications (as clients) communicate with it through the binder method. Surfaceflinger, as a thread, is divided into three parts:

1. The processing part of the thread itself, including initialization and thread loop.

2. The binder part is responsible for receiving various settings and commands of upper-layer applications, and giving feedback to the upper-layer applications.

3. Interaction with the underlying layer, responsible for calling the underlying interface (HAL ).

The structure is as follows:

Note:

A. Binder receives application commands (such as creating surface and setting parameters) and passes them to flinger.

B. After flinger completes the corresponding command, the relevant result status is fed back to the upper layer.

C. When processing upper-layer commands, set the event as needed (mainly related to display) and notify thread loop for processing.

D. flinger notifies the underlying layer for Processing Based on upper-layer commands (mainly set some parameters, such as layer and position)

E. Merge the surface in the thread loop and notify the underlying layer to display the surface (POST buffer ).

F. The displayhardware layer calls Hal to perform HW operations based on the flinger command.

The following describes some important surfaceflinger processing functions and the properties of surface and layer.

1) readtorun

Surfaceflinger thread initialization function. The main task is to allocate memory and set the underlying interface (EGL & Hal ).

Status_t surfaceflinger: readytorun (){... ... Mserverheap = new memorydealer (4096, memorydealer: read_only); // allocate shared memory for IPC... Msurfaceheapmanager = new surfaceheapmanager (this, 8 <20); // allocate heap for flinger. The size is 8 Mb, store specific display data {// initialize the main display graphicplane & plane (graphicplane (dpy); displayhardware * const hW = new displayhardware (this, dpy); plane. setdisplayhardware (HW); // Save the display interface} // obtain the display parameters const graphicplane & plane (graphicplane (dpy); const displayhardware & hW = plane. displayhardware (); const uint32_t W = HW. ge Twidth (); const uint32_t H = HW. getheight (); const uint32_t F = HW. getformat ();... ... // Initialize OpenGL | es glactivetexture (gl_texture0); glbindtexture (latency, 0); gltexparameterx (latency, latency, latency); gltexparameterx (latency, latency, latency); gltexparameterx (latency, latency, gl_texture_mag_filter, gl_nearest );... ... }

2) threadloop

Surfaceflinger's loop function mainly waits for the event sent by other interfaces to synthesize and display the display data.

Bool surfaceflinger: threadloop () {waitforevent (); // wait for the signal event of other interfaces... ... // Post surfaces (if needed) handlepageflip (); // The const displayhardware & HW (graphicplane (0 ). displayhardware (); If (likely (HW. candraw () {// repaint the framebuffer (if needed) handlerepaint (); // merge all layers and fill them in the buffer... ... Postframebuffer (); // swap front buffer and back buffer, call the EGL interface for display }... ... }

3) createsurface

The main interface provided to the application. This interface creates a surface. The underlying layer creates a layer and allocates memory based on the parameters. The surface parameters are fed back to the upper layer.

Sp <isurface> surfaceflinger: createsurface (clientid, int PID, priority: parameter * Params, displayid D, uint32_t W, uint32_t H, pixelformat format, uint32_t flags ){... ... Int32_t id = C-> generateid (PID); If (uint32_t (ID)> = num_layers_max) // num_layers_max = 31 {LogE ("createsurface () failed, generateid = % d ", ID); return }... Layer = createnormalsurfacelocked (c, d, ID, W, H, format, flags); // create a layer and allocate memory (two buffers in total) based on the parameter (width and height format: front/back buffer) if (layer) {settransactionflags (etransactionneeded); surfacehandle = Layer-> getsurface (); // create surface if (surfacehandle! = 0) surfacehandle-> getsurfacedata (Params); // The created surface parameter is fed back to the application layer }}

4) setclientstate

Process the upper-layer commands and set Event Notification threadloop Based on flag for processing.

Status_t surfaceflinger: setclientstate (clientid CID, int32_t count, const layer_state_t * States) {mutex: autolock _ L (mstatelock); uint32_t flags = 0; CID <= 16; for (INT I = 0; I <count; I ++) // checks the status signs of all layers {const layer_state_t & s = States [I]; layerbaseclient * layer = getlayeruser_l (S. surface | CID); If (layer) {const uint32_t what = S. what; // check whether each flag is set at the application layer. If yes, notify the underlying layer to complete corresponding operations and notify threadloop to perform corresponding processing if (What & edestroyed) // Delete the layer {If (removelayer_l (layer) = no_error) {flags | = etransactionneeded; Continue ;}} if (What & epositionchanged) // display location changes {If (layer-> setposition (S. x, S. y) flags | = etraversalneeded;} If (What & elayerchanged) // layer changes {If (layer-> setlayer (S. z) {mcurrentstate. layerssortedbyz. reorder (layer, & layer: comparecurrentstatez); flags | = etransactionneeded | etraversalneeded ;}} if (What & esizechanged) {If (layer-> setsize (S. w, S. h) // set the width and height change flags | = etraversalneeded;} If (What & ealphachanged) {// set the Alpha effect if (layer-> setalpha (uint8_t (255.0f * s. alpha + 0.5f) flags | = etraversalneeded;} If (What & ematrixchanged) {// matrix parameter change if (layer-> setmatrix (S. matrix) flags | = etraversalneeded;} If (What & etransparentregionchanged) {// display region changes if (layer-> settransparentregionhint (S. transparentregion) flags | = etraversalneeded;} If (What & evisibilitychanged) {// whether to display if (layer-> setflags (S. flags, S. mask) flags | = etraversalneeded ;}}if (flags) {settransactionflags (flags); // notify threadloop through signal} return no_error ;}

5) composesurfaces

This interface is called in threadloop to merge all existing surfaces. The OpenGL module is responsible for this part.

6) postframebuffer

This interface is called in threadloop to push the merged data (stored in back buffer) into the front buffer, and then call the Hal interface command to display the underlying layer.

7) from 3, we can see that each time a surface is created on the upper layer, a layer is created at the bottom layer. Let's take a look at the properties of the surface and layer.

Note: The structures in the Code are too large to be listed.

A. Surface Properties (For details, refer to the file surface. h)

A1: surfaceid: maps the surface and layer based on this ID.

A2: surfaceinfo

Including information such as width and height

A3: two buffer pointers, buffer indexes, and other information.

B. layer-related attributes (For details, refer to the file layer. h/layerbase. h/layerbitmap. h)

Including layer ID, width and height, location, layer, Alpha, buffer address and index, layer status information (such as efliprequested, ebusy, and elocked)

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.