[Android] Android UI display principle

One, window related

Window can be said to be the most basic UI component management class provided by the Android Framework layer, and PhoneWindow is its only implementation class. It shields developers from interacting with WindowManagerService, unifies UI design, and uniformly receives user interaction events, such as background, title, and key events.

The UI display of Activity/Dialog/Toast depends on Window. For UI writing, developers only need to use View related. View will eventually be set to Window in the form of ContentView:

PhoneWindow.java

  public void setContentView(int layoutResID) {
    
    
  }

DecorView is the root ViewGroup of PhoneWindow. Window provides a series of configuration items, and the UI composition of different configuration items DecorView will be different.

、 、 WindowManager

A Window will have a WindowManager. When it comes to WindowManager, it is necessary to mention WindowManagerGlobal. The difference between them is:

WindowManager: It is responsible for managing a Window and provides a series of flags for configuring the Window.
WindowManagerGlobal: It is a singleton class, responsible for managing all the windows of the application (in fact, it is not very rigorous, it should manage all ViewRootImpl). And it contains Binder that communicates with WindowManagerService.
The API provided by WindowManager is actually used to operate ViewRootImpl in WindowManagerGlobal. For example, WindowManager.addView(contentView) actually creates a ViewRootImpl corresponding to contentView in WindowManagerGlobal.

Three, VIewRootImpl

It is responsible for managing a specific View Tree, such as DecorView and all its child Views. Specifically have the following responsibilities:

By communicating with WindowManagerService, create a Surface to display the View Tree it
manages to manage the measurement, layout, and drawing of the entire View Tree. The specific method is that performTraversals
uses Choreographer to synchronize the UI refresh (measurement, layout, drawing) of the entire ViewTree with the system.

四、Choreographer

Choreographer is used to control the three UI operations of input (Input), animation (Animation) and drawing (Draw) synchronously (there are only three things to be completed in each frame when the UI is displayed). It maintains a Queue internally, and users can put a series of UI operations to be run into the Queue through postXXX. These events will perform these operations after Choreographer receives the time pulse (vertical synchronization signal-VSync signal) of the display system. For example, ViewRootImpl's update event for View Tree:

ViewRootImpl.java

void scheduleTraversals() {
    
    
    ...
     mChoreographer.postCallback(Choreographer.CALLBACK_TRAVERSAL, mTraversalRunnable, null);
}

Five, Surface

A ViewRootImpl contains a Surface (a Surface contains a Canvas). It can be understood as a canvas, and you can paint on it through Canvas. The entire ViewTree of ViewRootImpl is drawn on the Surface.

It actually corresponds to the Layer in SurfaceFlinger, and the content drawn on Surface will eventually be rendered by SurfaceFlinger.

六、WindowManagerService

It manages the Window of all applications:

Manage the state of all Window (WindowState)
communicate with SurfaceFlinger to complete the rendering of Window.
You can add a Window to WindowManagerService through ViewRootImpl:

ViewRootImpl.java

public void setView(View view, WindowManager.LayoutParams attrs, View panelParentView) {
    
    
    ...
    //mWindow是一个`Binder`
    res = mWindowSession.addToDisplay(mWindow, mSeq, mWindowAttributes,
                        getHostVisibility(), mDisplay.getDisplayId(), mWinFrame,
                        mAttachInfo.mContentInsets, mAttachInfo.mStableInsets,
                        mAttachInfo.mOutsets, mAttachInfo.mDisplayCutout, mInputChannel);
    ...
}

mWindowSession is an instance of IWindowSession, a Binder that communicates with WindowManagerService. Created and maintained by WindowManagerGlobal, there will only be one application.

Seven, WindowState and WindowToken

WindowState is used to represent a Window in WindowManagerService, it contains all the attributes of a window, and it corresponds to ViewRootImpl. It is stored in the mWindowMap collection of WindowManagerService. mWindowMap is a complete collection of all windows in the entire system.

WindowToken organizes windows belonging to the same application component together. In the process of window management by WindowManagerService, WindowToken represents an application component. For example, when performing window Z-Order sorting, windows belonging to the same WindowToken will be arranged together, which can also be understood as the Z-axis order of the Surface of the rendering Window. There can be multiple WindowState (Window) under a token:

WindowManagerService.addWindow()

win.mToken.addWindow(win);//一个token下会有多个win state

八、SurfaceFlinger

SurfaceFlinger is one of Android's most important system services. It is mainly responsible for UI rendering, which can be said to be Layer composition and rendering. The following objects basically exist in WindowManagerService. It is the key object for the application to interact with SurfaceFlinger.

九、SurfaceControl

It can be simply understood as the manager of Surface. It has a one-to-one relationship with Surface. When the SurfaceControl is constructed, the Surface will be constructed. The Surface of ViewRootImpl is actually the same object it points to. It can communicate with SurfaceFlinger through SurfaceComposerClient. For example, request SurfaceFlinger to create Surface(Layer)

十、SurfaceComposerClient

This object is also unique to the process, and there is only one for an application. You can use it to establish a connection with SurfaceFlinger to communicate with SurfaceFlinger. The specific communication function is completed by the Client object.

11. Client

It is a Binder through which SurfaceComposerClient can communicate with SurfaceFlinger. For example, it can make SurfaceFlinger create a Layer. It also maintains all layers of an application.

12. Layer

Managed by SurfaceFlinger, it is divided into many different types. It is a unit that can be rendered by SurfaceFlinger. It has a BufferQueueProducer, which maintains a lot of GraphicBuffers that can be rendered. This buffer may be rendered, or it may be in a state to be rendered.

Guess you like

Origin blog.csdn.net/sinat_36955332/article/details/108535819