Detailed explanation of vertical synchronization of iOS screen

http://www.cocoachina.com/ios/20151130/14477.html

How the screen displays images

QQ screenshot 20151127145420.png

Let's start with the past CRT display principle. The electron gun of CRT scans line by line from top to bottom according to the above method. After the scanning is completed, the display presents a frame of picture, and then the electron gun returns to the initial position to continue the next scan. In order to synchronize the display process of the display with the system's video controller, the display (or other hardware) uses the hardware clock to generate a series of timing signals. When the electron gun is changed to a new row and is ready to scan, the monitor will send out a horizontal synchronization signal (HSync for short); when a frame is drawn, the electron gun returns to its original position and is ready to draw the next frame. The monitor will send out a vertical synchronization signal (vertical synchronization), referred to as VSync. Displays are usually refreshed at a fixed frequency, which is the frequency at which the VSync signal is generated. Although most of today's devices are LCD screens, the principle remains the same.

ios_screen_display.png

Generally speaking, the CPU, GPU, and display in a computer system work together in the above manner. The CPU calculates the display content and submits it to the GPU. After the GPU rendering is completed, the rendering result is put into the frame buffer. Then the video controller will read the data in the frame buffer line by line according to the VSync signal, and pass it to the display after possible digital-to-analog conversion. .

In the simplest case, there is only one frame buffer. At this time, the reading and refreshing of the frame buffer will have a relatively large efficiency problem. In order to solve the problem of efficiency, the display system usually introduces two buffers, that is, double buffering mechanism. In this case, the GPU will pre-render a frame into a buffer for the video controller to read, and when the next frame is rendered, the GPU will directly point the video controller's pointer to the second buffer. In this way, the efficiency will be greatly improved.

Although double buffering can solve the efficiency problem, it will introduce a new problem. When the video controller has not finished reading, that is, when the screen content is just half displayed, the GPU submits a new frame of content to the frame buffer and exchanges the two buffers, the video controller will put the new frame. The lower half of the frame data is displayed on the screen, causing the screen to tear, as shown in the following figure:

ios_vsync_off.jpg

In order to solve this problem, the GPU usually has a mechanism called vertical synchronization (also called V-Sync for short). When vertical synchronization is turned on, the GPU will wait for the VSync signal of the display to be sent before rendering a new frame and updating the buffer. This can solve the phenomenon of screen tearing and increase the smoothness of the screen, but it needs to consume more computing resources and also brings some delays.

So what is the current mainstream mobile device situation? From the information found on the Internet, we can know that iOS devices will always use double buffering and turn on vertical synchronization. For Android devices, it was not until version 4.1 that Google began to introduce this mechanism. At present, the Android system is three caches + vertical synchronization.

Causes and Solutions of Caton

ios_frame_drop.png

After the VSync signal arrives, the system graphics service will notify the App through CADisplayLink and other mechanisms, and the main thread of the App will start to calculate the display content in the CPU, such as view creation, layout calculation, picture decoding, text drawing, etc. Then the CPU will submit the calculated content to the GPU, and the GPU will transform, synthesize, and render it. Then the GPU will submit the rendering result to the frame buffer, waiting for the next VSync signal to be displayed on the screen. Due to the vertical synchronization mechanism, if the CPU or GPU does not complete the content submission within a VSync time, the frame will be discarded and displayed again after the next opportunity, and the display screen will keep the previous content unchanged. This is why the interface is stuck.

As can be seen from the above figure, no matter which of the CPU and GPU blocks the display process, it will cause frame drops. Therefore, during development, it is also necessary to evaluate and optimize the CPU and GPU pressures separately.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325453959&siteId=291194637