Android system display principle

Application layer

Summary of the Android display process: The Android application caches the data after measurement, layout, and drawing, renders the data to the display screen through SurfaceFlinger, and refreshes the data through the Android refresh mechanism. In other words, the application layer is responsible for drawing, and the system layer is responsible for rendering. The data to be drawn by the application layer is transferred to the system layer service through inter-process communication. The system layer service updates the data to the screen through the refresh mechanism.

In the three steps of drawing each View in the drawing principle, Measure and Layout are recursive to obtain the size and position of the View. The deeper the level, the more elements, and the longer it takes.

1.Measure
用深度优先规则递归得到所有试图(View)的宽、高;获取当前View的宽度childWidthMeasureSpec和高度childHeightMeasureSpec之后,
可以调用它的成员函数Measure来设置它的大小。如果当前正在测量的子视图child是一个视图容器,那么它又会重复执行操作,
直到它的所有子孙视图的大小都测量完成为止

2.Layout
用深度优先原则递归得到所有View的位置;当一个子View在应用程序窗口左上角的位置确定之后,再结合它在前边测量过程中确定的宽度和高度,就可以完全确定它在应用程序窗口中的布局。

3.Draw
目前Android有两种绘制方式,软件绘制CPU与硬件加速GPU,GPU的显示和绘制效率远高于CPU,但也有缺点:耗电,兼容问题,内存大(使用openGL的接口至少需要8MB内存)

System layer

Really render the data that needs to be displayed on the screen, a
simple process realized through SufaceFlinger service

1. In response to client events, create a Layer to establish a connection with the client's Surface

2. Accept client data and attributes, modify Layer attributes, such as size, color, transparency, etc.

3. Refresh the created Layer content to the screen

4. Maintain the Layer sequence and make crop calculations for the final Layer output

In Android ’s display system, Android ’s anonymous shared memory is used: SharedClient. A SharedClient is created between each application and SurfaceFlinger. Each SharedClient can create up to 31 SharedBufferStacks. Each Surface corresponds to a SharedBufferStack, which is a window . At the same time, each SharedBufferStack contains two or three buffers, and surfaceFlinger renders the data in the buffer to the screen at the driver layer.

FPS

This means the number of frames transferred per second. Old people often talk about sending a VSYNC signal at 16ms to trigger UI rendering. This is 60FPS. If you exceed it, you will lose frames and feel stuck. Many drawing units, it may be that the animation is executed too many times

The root cause of Caton

There are two main aspects

1.绘制任务太重,绘制一帧的内容耗时太长
2.主线程太忙,导致VSYNC信号来的时候还没准备好数据导致丢帧

Main thread's main job

1.UI生命周期控制
2.系统事件处理
3.消息处理
4.界面布局
5.界面绘制
6.界面刷新
除了这些之外,其他活尽量避免让主线程去干

Guess you like

Origin www.cnblogs.com/yinyulong/p/12702272.html