CPU and GPU chip difference

CPU: Full name Centrel Processing Unit, it is the computing core and control core of the modern computer system; GPU: full name for Graphics Processing Unit. It is a microprocessor that performs drawing operations and connects a computer to a display terminal.

Principles of computer rendering

Random scan display

What does the graph look like, just scan and show those pixels, for example

When the green square is displayed on the screen using a random scanning display, the beam of light scans around the edge of the square, displaying the corresponding pixel point on the screen. The more complex the graph, the longer the random scan display takes.

Raster scan display

Instead of managing what graphics look like, scan the entire screen every time you scan, for example

Using a raster scan to display the circle in the image, the beam will scan the screen from top to bottom and from left to right to display the circle on the screen. The time displayed by raster scanning is independent of the complexity of the graph.

Random scan display used to be used, now raster scan display.

Raster scanning display principle

  • 1. Remove the picture from the main memory of the system;
  • 2. The CPU decodes the image;
  • 3. Copy the decoded picture to the frame cache;
  • 4. The GPU will display the decoded picture on the display.

The reason why the screen stuck

  • 1.GPU reads images from frame cache;
  • 2. Display the frame cache information that the controller will read, that is, our bitmap for digital-to-analog conversion (digital-to-analog conversion is to convert digital signals into analog information);
  • 3. Raster scan display displays images on the display through progressive scanning.

The above process will continue and proceed rapidly. When raster scan is displayed on the display, if the image A is read at the beginning, when the scan is displayed in the middle, the GPU updates the data in the frame cache, and the data in the frame cache becomes the IMAGE B. Then continue to scan the time when the images in the cache are displayed on the screen, and the B images are read, resulting in the phenomenon of two images on one screen, which is the screen tearing phenomenon shown in the above picture.

How to solve the screen stutter problem

Apple has implemented two strategies to solve the screen lag problem

1. Vsync

Vsync actually means that a lock is added to the frame cache when raster scan displays the image, and no data is written to the frame cache when the data is read from the frame cache. This ensures that every image displayed on the screen is the same image.

2. Double cache

Using vertical synchronization to the frame buffer and the lock, the image on the screen while the tear won’t happen, but if in the process of vertical with the raster scan display pictures, read the GPU to new image data, due to the vertical synchronization frame buffer for the lock, the new image data will not be able to save to the frame buffer, will lead to loss of data, So what to do? So we have A double cache, let’s say we have two caches called A and B. When raster scan shows the image in cache A, the newly read image of GPU is stored in B, when B is displayed, the newly read image of GPU is stored in A… “, which solves the problem of screen stutter.

Three buffer

With vSYNC and dual cache, we solved the problem of screen tearing and ensured that data was not lost. So let’s think about what other problems might occur? The answer is yes. When the display finishes displaying picture A, if picture B has not been loaded into the frame cache by THE GPU, it will not receive the signal of vertical synchronization to load picture B. At this time, the same picture will be displayed twice, which is called frame drop. Frame drop is inevitable due to the processing speed of CPU and GPU, but we can optimize it to reduce the rate of frame drop. So how do you optimize? The answer is three caches, then create A cache C, at this time we have three caches A, B, C, when displaying A, the GPU will load the image to B, C; After B is displayed, GPU loads the new image to A. When C is displayed, GPU will load A new picture to A, and so on. In this way, the screen can always have A new picture to display, thus reducing the probability of dropping frames.

conclusion

When will the screen freeze occur?

  • 1. The CPU/GPU processing speed is too slow, and it takes too long to load images into the frame cache, resulting in frame loss and stuttering.
  • 2. Use vSYNC and double cache strategy to solve the screen tear problem, at the cost of dropping frames, resulting in lag;
  • 3. Use CPU/GPU reasonably to reduce the number of frames dropped, but frames may still drop, resulting in lag.