1. Basic probability
-
The refresh rate
The number of frames displayed on the screen within 1s (unit: Hz)
-
Frame rate
Number of frames generated by a CPU or GPU within 1s (unit: FPS
Two, double cache
-
Won’t tear (tearing)
CPU –> GPU –> (buffer) –> display. The data processed by CPU/GPU is put into buffer, and the display takes the data from buffer to display. The refresh rate of the display is fixed. When the CPU/GPU processing speed is slow, only the part of the buffer filled with the current frame will be displayed by the display
-
Double cache
Two buffers, Back Buffer and Frame Buffer, are prepared for CPU/GPU and display respectively. After the data of Back Buffer is ready, they are exchanged with Frame Buffer for display (exchanging memory address), so that the picture will not be torn due to data inconsistency
-
VSync
If the CPU/GPU processing speed is faster than the display speed, when the Back Buffer data is ready, The data in the Frame Buffer is still being used by the display, so there is a problem with the exchange. The exchange can only be done after the screen processes the Frame data
-
drawing VSync
After VSync signal, the CPU/GPU can immediately process the next frame of data
Three, three cache
The GPU does not finish processing between two VSync intervals, and after the processor finishes processing, it will wait for the next VSync to exchange the cache. During this period, drawing cannot be carried out. A Graphic Buffer can be introduced, and the CPU can continue processing the next frame of data during this period
Choreographer
Control VSync signal after the CPU/GPU immediately execute the next frame drawing work
-
postCallback
Register a VSync signal monitoring, in the corresponding action after the next VSync signal coming VSync signal to trigger FrameDisplayEventReceiver onVsync doFrame execution method, The process of rendering the View from the corresponding Callback array is performed at the end: addView is called to WindowManager to add the View, and scheduleTraversals is called to the requestLayout method of ViewRootImpl
-
ViewRootImpl#scheduleTraversals
void scheduleTraversals() { if (! mTraversalScheduled) { mTraversalScheduled = true; MTraversalBarrier = mhandler.getLooper ().getQueue().postsyncbarrier (); // Invoke Choreographer's postCallback, Immediately after the next Vsync signal to hold mTraversalRunnable mChoreographer. PostCallback (Choreographer. CALLBACK_TRAVERSAL mTraversalRunnable, null); notifyRendererOfFramePending(); pokeDrawLockIfNeeded(); }} // mTraversalRunnable final class TraversalRunnable final class traversal implements Runnable { @Override public void run() { doTraversal(); } } void doTraversal() { if (mTraversalScheduled) { mTraversalScheduled = false; // Remove the synchronization barrier mhandler.getLooper ().getQueue().removesyncBarrier (mTraversalBarrier); if (mProfile) { Debug.startMethodTracing("ViewAncestor"); } // Execute performTraversals(); if (mProfile) { Debug.stopMethodTracing(); mProfile = false; }}}Copy the code
-
postFrameCallback
It is almost the same as postCallback, with different parameters and different callBackTypes
reference
-
- “Finally Understood” Series: Android Screen Refresh Mechanisms – VSync, Choreographer Understand!
-
- Android-Choreographer Working principles