Android screen refresh mechanism

basic knowledge

CPU、GPU

  • CPU: The central processing unit is mainly responsible for calculating data. In Android, it is mainly used for the calculation process of Surface in the three major drawing processes.
  • GPU: The image processor is mainly responsible for rendering graphics data. In Android, it is mainly used to synthesize the Surface data calculated by the CPU and put it into the buffer, so that the display can read and present it on the screen.

line-by-line scan

When the screen refreshes the buffer, it does not complete a one-time scan, but a reading process from left to right, from top to bottom, and displays each pixel of a screen in sequence, calculated according to the screen refresh rate of 60HZ. This process is only 16.66666... ​​ms.

  • Start scanning from the initial position (the upper left corner of the first line), from left to right, and perform horizontal scanning (Horizontal Scanning)
  • After each line is scanned, the scan line will switch to the starting point of the next line. This switching process is called horizontal blanking, referred to as hblank (horizontal blank interval), and a horizontal synchronization signal (horizontal synchronization, also known as line synchronization) is sent.
  • By analogy, after the entire screen (one vertical cycle) is scanned, the display can present a frame of picture
  • After the last line (one vertical period) of the screen is scanned, it needs to return to the initial position in the upper left corner. This process is called vertical blanking, referred to as vblank (vertical blank interval)
  • After the scanning line returns to the initial position, it is ready to scan the next frame, and at the same time, a vertical synchronization signal (vertical synchronization, also known as field synchronization) is sent out.

image.png

Graphics card frame rate

Indicates how many frames the GPU can render into the buffer within 1 second, and the unit is fps. What we need to understand here is that the frame rate is dynamic. For example, we usually say 60fps, but it can render up to 60 frames within 1 second. If our screen is If it is still, the GPU does not operate at this time, and the frame rate is 0.

screen refresh rate

Screen refresh rate: the number of times the screen fetches data from the buffer within 1 second, the unit is HZ, and the common screen refresh rate is 60HZ. The screen refresh rate is a fixed value related to hardware parameters. That is, a vertical synchronization signal is sent at this frequency to tell the GPU to write data into the buffer, that is, to render the next frame.

The Evolution of the Screen Refresh Mechanism

single buffer

GPU and display share a buffer

screen tearing screen tearing, screen tearing

When there is only one buffer, the GPU writes data into the buffer, and the screen fetches the image data from the buffer and refreshes it for display. Ideally, the frame rate of the graphics card is equal to the refresh rate of the screen. Every time a frame is drawn, a frame is displayed on the screen. The actual situation is that there is no necessary size relationship between the two. If there is no synchronization mechanism, problems will easily occur.
When the frame rate of the graphics card is greater than the screen refresh rate and the screen is ready to refresh the second frame, the GPU is already generating the third frame, and part of the data in the second frame will be overwritten.
When the screen starts to refresh the second frame, part of the data in the buffer is the data of the third frame, and part is the data of the second frame, and the displayed image will have obvious deviations, which is called screen tearing, and its essence is The frame rate of the graphics card is inconsistent with the refresh rate of the screen.

double buffer

Before Android 4.1,
the basic principle was to use two buffers.
The cache written by the GPU is: Back Buffer
The cache used for screen refresh is: Frame Buffer
Because double buffers are used, the frame buffer will not change when the screen is refreshed, and frame data switching is realized by exchanging buffers.
When will the buffer exchange be performed? When the device screen is refreshed and before the next frame is refreshed, because there is no screen refresh, this period is the best time for buffer exchange.
At this time, the hardware screen will send a pulse signal to inform the GPU and CPU that it can be exchanged. This is the Vsync signal, the vertical synchronization signal.
It is undeniable that double buffering can greatly reduce screen tearing errors, but there are still some other problems.

Jank dropped frames

If the back buffer is not ready when Vsync arrives, there will be no buffer exchange, and the screen will still display the previous frame, that is, the same frame data will be displayed in two refresh cycles, which is called Jank frame drop.

image.png
The reason for jank is that it was too late when the CPU processed the data in the second frame, and the GPU did not write the data into the buffer in time, resulting in jank.
The timing of CPU processing data and GPU writing buffer is relatively random.

Project Butter Butter Project

After the Android 4.1
system receives the VSync signal, it immediately performs CPU drawing and GPU buffer writing. Minimize the occurrence of jank.

image.png
If the frame rate of the graphics card is greater than the screen refresh rate, that is, the screen refreshes one frame, the CPU and GPU can make full use of the refresh time to process the data and write it into the buffer, then this solution is perfect, and the display effect will be very good.

image.png
Because the main thread has done some relatively complex and time-consuming logic, the processing time of CPU and GPU exceeds the time of refreshing one frame of the screen. Since the back buffer writes B frame data at this time, it cannot be overwritten before the buffer is exchanged, and the frame The buffer is used by Display for refreshing, so both buffers are occupied before the B frame is written into the back buffer until the next VSync signal arrives, and the CPU cannot continue to draw. It will be empty during this period, so it appears again three caches.

Three buffers

image.png
Minimize the situation where the CPU is idle.

Choreographer

After the system receives the VSync signal, it will immediately perform CPU drawing and GPU buffer writing. Implemented by Choreographer in Android.

  • In the constructor of Choreographer, a FrameDisplayEventReceiver class object will be created. This object implements the onVSync method for VSync signal callback.
  • The parent class construction method of the FrameDisplayEventReceiver object will call the nativeInit method to pass the current FrameDisplayEventReceiver object to the native layer, and the native layer returns an address mReceiverPtr to the upper layer.
  • The main thread calls nativeScheduleVsync in the scheduleVsync method, and passes in the mReceiverPtr returned in 2, so that a FrameDisplayEventReceiver object is officially registered in the native layer.
  • Driven by the GPU, the native layer will periodically call back the onVSync method of FrameDisplayEventReceiver, thus realizing: when the VSync signal arrives, the doFrame method is executed immediately.
  • The doFrame method executes input events, animation events, layout/measure/draw processes and submits data to the GPU.

References:
https://juejin.cn/post/7163858831309537294
https://blog.csdn.net/litefish/article/details/53939882
https://www.jianshu.com/p/996bca12eb1d

Guess you like

Origin blog.csdn.net/yuantian_shenhai/article/details/131009232