Surfaceflinger inherits the Thread class, and naturally inherits the threadloop method of the thread class. The main code of the surfaceflinger working thread is in the threadloop () method. After a worker thread starts, the basic Thread class will call the threadloop method cyclically. The threadloop () of surfaceflinger mainly aims to complete the mixing (compose) of each layer (surface) in the system ), then, the image data of a frame of mixed frame is continuously transmitted to the display device.
Threadloop Process
Figure 1 threadloop Process
1. handleconsoleevents
Handleconsoleevent is not well understood at present. It seems that it only changes the sufaceflinger status when the device enters sleep state or wakes up from sleep, then, the subsequent process of threadloop determines whether to send the display data to the display device based on the corresponding status.
2. handletransaction
Because layer mixing is performed in the thread, during the mixing process, the application or system may change the layer state, for example, to rotate the screen, add or delete a layer, or make a layer visible or invisible, surfaceflinger maintains the list of two layers so that these changes do not disrupt the current hybrid action:
- Mcurrentstate. layerssortedbyz ---- the latest layer list of the current system
- Mdrawingstate. layerssortedbyz ---- list of layers used in this hybrid operation
Handletransaction is to calculate whether an update is required in a visible area based on the state changes in the layer list, set the state variable mvisibleregionsdirty, and assign the mcurrentstate to mdrawingstate, finally, the layer that has been discarded (ditch) is released.
- In the last hybrid process, a layer may be released by the application, but the mdrawingstate is in use and cannot be destroyed immediately. Therefore, the destroy action can be performed only before the Hybrid Operation.
- If the layer size changes and is visible, the handletransaction of the layer will re-allocate the buffer and freeze the surfaceflinger's subsequent mixed operations, that is, the screen content will not be refreshed this time, the freeze will not be lifted until the handlepageflip phase of the next loop.
3. handlepageflip
In this phase, the layers are traversed, And the frontbuffer of the layer is obtained and locked in each layer. Then, the 2D texture (texture) of the layer is generated using the image data in the frontbuffer ), the update area is calculated to prepare for subsequent hybrid operations.
Figure 2 handlepageflip processing process
The lockpageflip () of the layer first uses the member variable lcblk of the sharedbufferserver class to call retireandlock to obtain the currently available frontbuffer of the layer, and then generates the texture map of OpenGL ES through the reloadtexture method, finally, unlockpageflip is used to update the layer coordinates of the region to the screen coordinates.
Handlerepaint
Handlerepaint is the processing process of handlerepaint to implement Layer Mixing:
Figure 3 handlerepaint Process
Handlerepaint first resets the OpenGL observation matrix and then traverses mdrawingstate. the layer list in layerssortedbyz calls the ondraw method of each layer. In the ondraw method, the drawwithopengl () method is called to mix the textures generated in the handlepageflip stage to the main surface of OpenGL, finally, handlerepaint clears the area to be refreshed.
Unlockclients
Unlockclients only traverses each layer and calls the finishpageflip method of each layer. Finishpageflip will further call the unlock () method of sharedbufferserver: (for details about sharedbuffersever, refer to the sharedclient and sharedbufferstack sections in my blog below)
Void layer: finishpageflip () <br/>{< br/> status_t err = lcblk-> unlock (mfrontbufferindex); <br/> loge_if (Err! = No_error, <br/> "layer % P, buffer = % d wasn't locked! ", <Br/> This, mfrontbufferindex); <br/>}< br/>
Lcblk-> unlock (mfrontbufferindex) will unlock the front buffer of the layer.
Postframebuffer
Entering the postframebuffer stage, the main surface of OpenGL has prepared mixed image data, and postframebuffer simply calls HW. flip (), HW. flip () Further calls eglswapbuffers to complete the Master surface switching, so that the images on the screen will be updated to new data.