SurfaceFlinger service of Surface mechanism in Android system Brief Introduction and learning plan

Source: Internet
Author: User

We started from the relationship between the Android Application and the SurfaceFlinger service and learned the SurfaceFlinger service from the perspective. With these preparations, we can analyze the implementation principle of SurfaceFlinger service from the front. SurfaceFlinger is responsible for managing the frame buffer devices of the system and rendering the UI of the system, that is, the UI of each application. In this article, we will briefly introduce the SurfaceFlinger service and develop a learning plan.

As mentioned in a series of articles about the relationship between Android applications and SurfaceFlinger services and the learning plan, SurfaceFlinger services run in the System process to centrally manage System frame buffer devices. Because the SurfaceFlinger service runs in the System process, Android applications need to request it through the Binder inter-process communication mechanism to render their UI. The Android app requests the SurfaceFlinger service to render its UI in three steps: first, create a connection to the SurfaceFlinger service, and then create a Surface through the connection, finally, the SurfaceFlinger service is requested to render the Surface.

Because the SurfaceFlinger service needs to communicate with the Android Application to execute the Binder process, it is itself a local Binder object, as shown in 1:

 

Figure 1 class relationship diagram of SurfaceFlinger Service

To understand this figure, you need to learn about the Binder inter-process communication mechanism. For details, refer to the Android inter-process communication (IPC) mechanism Binder introduction and learning plan series of articles.

The SurfaceFlinger Service implements the ISurfaceComposer interface, which is defined in the file frameworks/base/include/surfaceflinger/ISurfaceComposer. h, as shown below:

[Cpp]
Class ISurfaceComposer: public IInterface
{
Public:
DECLARE_META_INTERFACE (SurfaceComposer );
......
 
/* Create connection with surface flinger, requires
* ACCESS_SURFACE_FLINGER permission
*/
Virtual sp <ISurfaceComposerClient> createConnection () = 0;
 
/* Create a client connection with surface flinger
*/
Virtual sp <ISurfaceComposerClient> createClientConnection () = 0;
 
/* Retrieve the control block */
Virtual sp <IMemoryHeap> getCblk () const = 0;
 
/* Open/close transactions. requires ACCESS_SURFACE_FLINGER permission */
Virtual void openGlobalTransaction () = 0;
Virtual void closeGlobalTransaction () = 0;
 
/* [Un] freeze display. requires ACCESS_SURFACE_FLINGER permission */
Virtual status_t freezeDisplay (DisplayID dpy, uint32_t flags) = 0;
Virtual status_t unfreezeDisplay (DisplayID dpy, uint32_t flags) = 0;
 
/* Set display orientation. requires ACCESS_SURFACE_FLINGER permission */
Virtual int setOrientation (DisplayID dpy, int orientation, uint32_t flags) = 0;
 
/* Signal that we're re done booting.
* Requires ACCESS_SURFACE_FLINGER permission
*/
Virtual void bootFinished () = 0;
 
/* Capture the specified screen. requires READ_FRAME_BUFFER permission
* This function will fail if there is a secure window on screen.
*/
Virtual status_t captureScreen (DisplayID dpy,
Sp <IMemoryHeap> * heap,
Uint32_t * width, uint32_t * height, PixelFormat * format,
Uint32_t reqWidth, uint32_t reqHeight) = 0;
 
Virtual status_t turnElectronBeamOff (int32_t mode) = 0;
Virtual status_t turnElectronBeamOn (int32_t mode) = 0;
 
/* Signal surfaceflinger that there might be some work to do
* This is an ASYNCHRONOUS call.
*/
Virtual void signal () const = 0;
};
The ISurfaceComposer interface has 13 member functions. The following is a brief introduction:
-- CreateConnection: the Android Application requests a connection from the SurfaceFlinger service. For details, refer to the connection process analysis document between the Android Application and the SurfaceFlinger service.

-- CreateClientConnection: the Android Application requests the SurfaceFlinger service to create a shared UI metadata buffer. For details, refer to the shared UI metadata (SharedClient) between the Android Application and SurfaceFlinger service) analysis of the creation process.

-- GetCblk: the Android Application requests the SurfaceFlinger service to return an anonymous shared memory. The returned anonymous shared memory contains information about the device display, such as width and height.

-- OpenGlobalTransaction: the Android Application requests the SurfaceFlinger service to add a global Transaction count to modify the UI attribute information in batches. Note that the modified UI property information will be cached and will not take effect immediately. To make these modified UI attributes take effect, you need to call another member function closeGlobalTransaction, as described below.

-- CloseGlobalTransaction: the Android Application requests the SurfaceFlinger service to reduce the global Transaction count. When the global Transaction count is reduced to 0, the previously modified UI attribute information requested by openGlobalTransaction takes effect immediately.

-- FreezeDisplay: the Android Application uses it to request the SurfaceFlinger service to freeze the screen. When the screen is frozen, all UI rendering operations are cached and waiting for execution.

-- UnfreezeDisplay: the Android Application uses it to request the SurfaceFlinger service to restore the screen. After the screen is restored, the SurfaceFlinger service can perform the UI rendering operation.

-- SetOrientation: the Android Application uses this method to request the orientation of the SurfaceFlinger service device screen.

-- BootFinished: WindowManagerService tells the SurfaceFlinger service that the system has been started. At this time, the SurfaceFlinger service will stop executing the boot animation. For details, refer to the analysis of the boot screen display process of the Android system.

-- CaptureScreen: the Android app uses it to request SurfaceFlinger to intercept screen images.

-- TurnElectronBeamOff: the Android Application uses it to request the SurfaceFlinger service to close the screen.

-- TurnElectronBeamOn: the Android Application uses it to request the SurfaceFlinger service to light up the screen.

-- Signal: the Android app uses it to request the SurfaceFlinger service to render the UI. For details, refer to the analysis article on how the Android app requests the SurfaceFlinger service to render the Surface.

After understanding the ISurfaceComposer interface definition, let's take a look at the implementation of the Binder proxy object BpSurfaceComposer of SurfaceFlinger service, as shown in Figure 2:

 

Figure 2 class relationship diagram of the Binder proxy object of SurfaceFlinger Service

To understand this figure, you also need to learn about the Binder inter-process communication mechanism. For details, refer to the Android inter-process communication (IPC) mechanism Binder introduction and learning plan series of articles.

The Android Application obtains a Binder proxy object of the SurfaceFlinger service. After a BpSurfaceComposer object is obtained, it can request the SurfaceFlinger service to create and render its own UI.

Next, let's briefly introduce the definition of SurfaceFlinger class, as shown in 3:

 

Figure 3 SurfaceFlinger class definition

The SurfaceFlinger class has two member variables: mCurrentState and mDrawingState. Among them, the member variable mCurrentState is used to describe the state of the system's next UI to be rendered, while the mDrawingState is used to describe the state of the UI to be rendered.

The State class is used to describe a UI State. It has four important member variables: layersSortedByZ, orientation, orientationType, and freezeDisplay. Among them, the member variable layersSortedByZ is a vector of the LayerVector type, which stores the Surface contained by the system. Each Surface is described using a LayerBase object and arranged in the z-axis order; the member variables orientation and orientationType are all uint8_t, which are used to describe the screen direction. The member variable freezeDisplay is also uint8_t, which describes whether the screen is frozen.

The SurfaceFlinger class member variable mVisibleLayerSortedByZ is a Vector of the sp type <LayerBase>. It is used to save the visible Surface to be rendered next time by SurfaceFlinger, they are the member variable layersSortedByZ of a State object described by the mDrawingState member variable of the SurfaceFlinger class.

The SurfaceFlinger class member variable mGraphicPlanes is an array of GraphicPlane types, which is used to describe all the display devices of the system. The size of this array is equal to 1. Currently, Android only supports one display device.

The GraphicPlane class has four important member variables: mHw, mOrientation, mWidth, and mHeight. The member variable mHw points to a DisplayHardware object to describe a hardware display device. The member variables mOrientation, mWidth, and mHeight are of the int type, it is used to describe the orientation, width, and height of a hardware display device. You can call the member functions setDisplayHardware and displayHardware of the GraphicPlane class to obtain a hardware display device contained in a GraphicPlane object.

The DisplayHardware class has an important member variable mNativeWindow, which is a strong pointer of the type FramebufferNativeWindow. The FramebufferNativeWindow class is used to describe an Android system local window, which represents the system's hardware frame buffer. The member function flip of the DisplayHardware class is used to render the system UI. The graphic buffer at the backend is flipped to the graphic buffer at the front end and rendered in the hardware frame buffer.

The FramebufferNativeWindow class serves the same purpose as the Surface class described in the previous analysis of the process when the Android Application requested the SurfaceFlinger service to create a Surface, that is, it is a bridge between the OpenGL library and the Android UI system. The OpenGL library uses its member function dequeueBuffer to obtain a graphic buffer for filling UI data, the queueBuffer function is used to render a graphic buffer that has filled the UI data to the frame buffer of the system.

The FramebufferNativeWindow class has three important member variables: fbDev, grDev, and buffers. The member variables fbDev and grDev point to a framebuffer_device_t device and an alloc_device_t device respectively. According to the previous analysis of the implementation principle of the hardware abstraction layer (HAL) module Gralloc of the Android Frame Buffer, the framebuffer_device_t device and the alloc_device_t device are provided by the HAL module Gralloc, they are used to allocate graphic buffers and render graphic buffers respectively. The member variable buffers is an array of NativeBuffer types. This array is used to describe a graphic buffer stack with the stack size NUM_FRAME_BUFFERS, these graphic buffers are directly allocated in the hardware frame buffer, which is different from the graphic buffer used by the Surface class because the graphics buffer used by the latter is allocated in the anonymous shared memory.

After learning about the important member variables of the SurfaceFlinger class, let's take a look at several important member functions: threadLoop, waitForEvent, signalEvent, handleConsoleEvents, handleTransaction, handlePageFlip, handleRepaint, and postFramebuffer.

Although the SurfaceFlinger service is started in the System process, it creates a thread at startup to render the UI. To facilitate the description, we call this thread the UI rendering thread. The execution function of the UI rendering thread is threadLoop, a member function of the SurfaceFlinger class, and a message queue. When the UI rendering thread does not need to render the UI, it will wait in the waitForEvent function of the SurfaceFlinger class until the SurfaceFlinger service needs to perform a new UI rendering operation.

When will the SurfaceFlinger service execute new UI rendering operations? When the system display properties change or the application window changes, it needs to re-render the system UI. At this time, the SurfaceFlinger service will wake up from the SurfaceFlinger class member function waitEvent and execute the SurfaceFlinger class member functions handleConsoleEvents, handleTransaction, handlePageFlip, handleRepaint and postFramebuffer to perform the rendering UI operation. The member function handleConsoleEvents is used to handle console events, and the member function handleTransaction is used to handle system display attribute changes and application window attribute changes; the member function handlePageFlip is used to obtain the graphic buffer to be rendered next time in the application window, that is, to set the active graphic buffer in the application window. The member function handleRepaint is used to re-paint the application window; the postFramebuffer function is used to render the system UI to the hardware frame buffer.

We know that an application runs in a different process than the SurfaceFlinger service, and we can understand the process analysis from the previous Android application requesting the SurfaceFlinger service to render the Surface, each time an application needs to update its own UI, it notifies the SurfaceFlinger service through the inter-process communication mechanism of the Binder. After the SurfaceFlinger Service receives this notification, it will call the signalEvent function of the SurfaceFlinger class to wake up the UI rendering thread so that it can perform rendering UI operations. Note that the SurfaceFlinger Service obtains application requests through the Binder thread. Therefore, the SurfaceFlinger service UI rendering thread is actually awakened by the Binder thread. The signalEvent function of the SurfaceFlinger class is actually used to wake up the UI rendering thread by sending a message of the INVALIDATE type to the Message Queue of the UI rendering thread.

As mentioned above, the SurfaceFlinger service needs to call the SurfaceFlinger class member function handleConsoleEvents to handle console events when performing UI rendering operations. How can this be understood? It turns out that when the SurfaceFlinger service is started, another thread will be created to monitor the frame buffer hardware events sent by the kernel. To facilitate the description, we call this thread a console event monitoring thread. When the frame buffer enters the sleep state, the kernel will issue a sleep event. At this time, the SurfaceFlinger service will execute an operation to release the screen. When the frame buffer is awakened from the sleep state, the kernel sends a wake-up event. At this time, the SurfaceFlinger Service performs an operation to obtain the screen.

In this way, we will briefly introduce the SurfaceFlinger class definition. You can learn from these introductions:

1. The SurfaceFlinger service uses a GraphicPlane object to manage system display devices;

2. The SurfaceFlinger service has three types of threads: The Binder thread, the console event monitoring thread, and the UI rendering thread;

3. The SurfaceFlinger Service performs rendering system UI operations in the UI rendering thread.

Based on the above three content, combined with the SurfaceFlinger Service Startup Process, we will then systematically learn the implementation principle of SurfaceFlinger service through the following four articles:

1. How is the SurfaceFlinger service started?

2. How does the SurfaceFlinger service Manage System display devices through GraphicPlane, DisplayHardware, and FramebufferNativeWindow?

3. What is the collaboration model of the three threads of the SurfaceFlinger service?

4. How does the SurfaceFlinger Service perform UI rendering operations in the UI rendering thread?

Based on the previous articles on the relationship between Android applications and SurfaceFlinger services, a series of learning plans, and the above four articles, I believe we can have a clear understanding of SurfaceFlinger services, stay tuned!

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.