A summary,
How the Android Application displays: The Android application calls the SurfaceFlinger service to render the measured, laid out, and drawn Surface onto the display screen.
SurfaceFlinger: Android system service that manages the Frame buffer of the Android system, i.e. the display screen. Surface: Each window of an Android application corresponds to a Canvas, or Surface, which can be understood as a window of an Android application.
The display process of Android application consists of two parts (application side rendering, system side rendering) and two mechanisms (interprocess communication mechanism, display refresh mechanism).
Two, application side drawing
An Android application window contains many UI elements, which are organized in a tree structure, i.e. they have a parent-child relationship, with child UI elements inside the parent UI element.
Therefore, before drawing the UI of an Android application window, first determine the size and position of each child UI element within the parent UI element. The process of determining the size and position of each child UI element within the parent UI element is called the measurement process and the layout process. Therefore, the UI rendering process of An Android application window can be divided into three stages: measurement, layout, and drawing.
Measure: Recursively (depth-first) determine the size (width, height) of all views Layout: recursively (depth-first) determine the position (upper-left coordinate) of all views Draw: Draw all views of the application window on the canvas
3. System-side rendering
After the Android application draws the View hierarchy in the graphics buffer, the graphics buffer is handed to the SurfaceFlinger service, which then uses the OpenGL graphics library API to render the graphics buffer into the hardware frame buffer.
The underlying principle of Android image display:
GPU: render the color of the image Display: Display the image on the screen Vsync: When the Android image is drawn, the CPU first calculates the shape of the image, and the CPU will give the image to the GPU to render the color. If all this can be completed in 16ms, then when the next VSync appears, You can display the frame you just rendered. However, if the CPU and GPU process an image for more than 16ms, then the image can only be flashed out of the screen and presented to the user when the second VSync appears, which means that the user sees the same image in 32ms, which is called frame drop, also known as stuck.
4. Interprocess communication mechanism
In order for Android applications to draw their UI onto the system’s frame buffer, they must communicate with the Surface service. Android applications and SurfaceFlinger services run in different processes, so they use some kind of interprocess communication mechanism to communicate. When an Android application notifying the SurfaceFlinger service to draw its OWN UI, it needs to pass the UI data to the SurfaceFlinger service, such as the area and location of the UI to draw. An Android application can have many Windows, and each window has its own UI data, so the android system’s anonymous shared memory mechanism comes in handy.
Each Android application passes UI data between it and the SurfaceFlinger service via an anonymous shared memory. But pure anonymous shared memory lacks effective management when passing multiple window data, so anonymous shared memory is abstracted into a more upper-tier data structure, SharedClient.
In each SharedClient, there are up to 31 SharedBufferStacks, and each SharedBufferStack corresponds to a Surface, that is, a window. A SharedClient corresponds to an Android application, and an Android application may contain multiple Windows, but can contain up to 31 Windows. Each SharedBufferStack contains N buffers (below android4.1, N = 2, above 4.1, N = 3), which is the double and triple buffering technique that will be mentioned in the display refresh mechanism.
5. Display refresh mechanism
We use a technique called “double buffering” when drawing uIs. Double-buffering means that you use two buffers, one called the Front Buffer and the other called the Back Buffer. The UI is always drawn in the Back Buffer, then swapped with the Front Buffer and rendered to the display device. VSync was introduced in android4.1, which is similar to a clock interrupt, and as soon as a VSync interrupt is received, the CPU starts processing each frame of data.
Pay attention to me, share knowledge every day, you want, I have ~~~