The relationship between Vsync signal and View drawing process

Get into the habit of writing together! This is the 11th day of my participation in the "Nuggets Daily New Plan · April Update Challenge", click to view the details of the event .

VSync signal

vsync has two signals,

One is the data that vsync-app uses to generate the current frame; (CPU computing and GPU rendering)

One for consuming data (compositing image to Display, vsync-surface).

Three buffer mechanisms:

CPU cache In order to prevent GPU computing from timing out, generate data in advance GPU: put data in the buffer pool to prevent screen rendering from timing out

First, the source of the vsync signal

vsync can be provided by the underlying HardWare and sent via Display, and the vsync signal will also be sent to Display when the underlying Hardware cannot provide it. vsync shields the underlying Hal so that hardware without Vsync can also be used.

Second, the sending process

After HardWare arrives at Display, Display splits the vsync signal into two to generate one vsync signal for consumption .

One is that vsync-app wakes up Chrographer to do the drawing operation of the App (generates the current frame data)

One is that vsync-sf is used by SurfaceFliger. When the vsync signal comes, the synthesis operation is performed temporarily (under the condition that the previous frame of data is consumed)

Third, the offset

vsync sends one every 16ms. vsync will be divided into two signals sent. This means that as long as the two signals process the data within 16ms, it is OK. That is to say, we can mess up the order whether to synthesize the consumption frame data and draw it to the screen first or generate the frame data first .

比如先发送vsync-app在0-13ms做完处理,接着13-16ms在发送vsync-surface合成数据 或者颠倒,但是事件一定保证只要在16ms之内处理完这两个信号即可

四,整个处理过程:

1.vsync-app:UI Thread准备好绘制指令,提交给Render Thread渲染线程去调用OpenGl的函数去生成buffer并放到BufferQuene中

2.vsync-surface:SurfaceFliger进程去BufferQuene中去取出buffer合成图像显示到屏幕Display中。

五,vsync-app 解释

唤醒Chorgrapher去做处理生成当前这一帧的数据。 注意:有两个线程共同合作完成绘制动作:UIThread生成指令和RenderThread调用OpenGl库生成Buffer放入到BufferQuene缓冲队列中。 UIThread:Choreographer.doFrame()RenderThread:DrawFrame

首先来讲UIThread的Choreographer.doFrame方法:

1.按顺序发送INPUT,ANIMATION,TRASVEL并处理他们各自的doFrame方法 先处理输入事件在处理动画,最后的TRASVEL会进行调用到ViewRootImpl中的doTrasvel回调,这个回调里面会进行measure,layout和draw。

Here is the draw method. When the performDraw method is called, the lockCanvas method of the global Surface (that is, the activity) will be called. This method will lock a memory area in the Surface object of the native layer and the return value is canvas, which is the Surface memory space in the native layer. Next, call the draw method to pass the canvas into the parameters, that is, the modifications we make to the canvas in the draw method are essentially modifications to this memory area. After the final draw method call is completed, this memory area will be released and handed over to RenderThread to process rendering data. (The corresponding processing of the release operation in the native layer is to turn this memory area into a Bitmap and hand it over to RenderThread for rendering)

The draw method does not actually draw, but puts the drawn content into the DisplayList and then synchronizes it to the RenderThread.

Drawing will eventually call the View.invalidate method

2. When the RenderThread is executed, the UIThread can be released for other processing, and then the RenerThread will take out the data in the DisplayList for processing to generate a frameBuffer and give it to the Surface for synthesis processing. Specific process: RenderThread will execute a Task of DrawFrameTask, and the core method is DrawFrame. Notify the rendering data to SurefaceFliger for layer composition through OpenGl and some libraries. Put rendering data into the blocking queue

Six, vsync-sf:

The FrameBuffer data generated by RenderThread on the App side will be consumed in SurfaceFliger. That is, take out the rendering data in the blocking queue. SurfaceFliger is synthesized and processed on Display

Guess you like

Origin juejin.im/post/7085612354716565517