Graphical android-android GUI System (1)-Introduction

Source: Internet
Author: User
Tags skia

Http://www.cnblogs.com/samchen2009/p/3364327.html

Android's GUI system is one of the most important and complex Android systems. It includes the following sections:

    1. Windows and Graphics Systems-window and View Manager system.
    2. Display crafting system-Surface Flinger
    3. User input Systems-InputManager system
    4. Application Framework System-Activity Manager system.

The relationships between them are as shown

Only by having a basic understanding of how these systems function and how they work can we answer some of the questions that often linger in our minds, such as:

    1. What is the activity initiation process? What does the activity's onxxx () work in the background? How does the activity show () and hide () control?
    2. When was surface created by whom?
    3. How does Android put a control on surface? And then show it to the phone screen?
    4. How does the application window get focus? How does the user's key (touch screen or keyboard) pass to the current window?
    5. Is Android a multi-window system?
    6. How does Android support multi-screen interaction? (Wifi Display)
    7. Which process does the IME window belong to? Why is it possible for any application to pop up whenever there is an input box?

This article will try to make a brief but comprehensive introduction to the Android GUI system from the framework and process point of view, hoping to help you find the answers to the above questions.

All the content can be condensed in the picture below, there are a lot of names and concepts we need to explain in advance:

1. Window, Phonewindow and Activity
  • activity is one of the four components of Android applications (activity, Service, Content Provider, broadcast Receiver) and the only component that interacts directly with the user.
  • window  has different meanings in different places. In activity, Window is an abstract class that represents a rectangular invisible container with several visible areas (View). Each activity will have a window class member variable, Mwindow. In Windowmanagerservice, window refers to the WindowState object, as you can see, windowstate corresponds to a Mwindow object in a Viewrootimpl. So, the window that Windowmanagerservice manages is actually the viewroot of acitivity. The window we mentioned below, if not specified, refers to the concept of ' Window ' in Windowmanagerservice, which is a specific display area. From the user's point of view, Android is a multi-window operating system, the different size of the window area according to the size, location, z-order and whether transparent and other parameters superimposed together and ultimately presented to the user. These windows can be either from an app or from multiple apps, which can be displayed either in a flat or a different plane. In summary, the window is a hierarchical display area, each window at the bottom of the final embodiment as a rectangular buffer, these buffer is calculated to become a new buffer, the final delivery display system to show. To assist in the final window management, Android defines a number of different window types: The
    • application window (application window): includes the Windows that all applications create themselves, and the windows that the system is responsible for displaying before applying them.
    • subwindow (sub window): For example, if you apply a custom dialog box, or an Input method window, the child window must be attached to an application window (setting the same token).
    • System window: System-designed, not attached to any application window, for example, status bar, navigation bar (Navigation bar), wallpaper (wallpaper), Caller ID window (phone ), Lock screen window (keyguard), Information tip window (Toast), Volume adjustment window, mouse cursor, and so on.
  • Phonewindow is an extension of Activity window, a window layout scheme specifically designed for mobile phones or tablet devices, as you can see on your phone, and a Phonewindow layout is as follows:

2. View, Decorview, ViewGroup, Viewroot

View is the visible area of a rectangle.

ViewGroup is a special view that can contain other view and layout in a certain way. Android supported layouts are framelayout, linearlayout, relativelayout, etc.

Decorview is framelayout sub-class, Framelayout also called single frame layout, is the simplest kind of layout, all sub-view in the vertical direction in accordance with the order in succession, if there are overlapping parts, The view in the back will block the previous view. We often see pop-up boxes that block the back of the window part, which is used by the framelayout layout. The android window basically uses the framelayout layout, so Decorview is a top view of the Activity window, and all the view displayed in the window is its child view.

viewroot . We can define all the view that is called by AddView () is viewroot, because the interface will generate a Viewrootimpl object and save it in the mroots[] array of Windowmanagerglobal. A program can have a lot of viewroot (as long as you call AddView ()), and at the Windowmanagerservice end, it is multiple windows. In the activity's default implementation, however, only Mdecorview is added to the Windowmanagerservice via AddView (see code below)

Frameworks/base/core/java/android/app/activity.java void Makevisible () {        if (!mwindowadded) {            ViewManager WM = Getwindowmanager ();            Wm.addview (Mdecor, GetWindow (). GetAttributes ());            mwindowadded = true;        }        Mdecor.setvisibility (view.visible);  }

So in general, we can say that an application can have multiple activity, each activity a window (Phonewindow), each window has a decorview, a viewrootimpl, Corresponds to a window (WindowState) in the Windowmanagerservice.

3. Viewrootimple, Windowmanagerimpl, windowmanagerglobals

Windowmanagerimpl: The interface of WindowManager and Viewmanager is implemented, but most of them are implemented by invoking the Windowmanagerglobals interface.

windowmanagerglobals: A singleton object with three arrays maintained in the object:

    • Mroots[]: Store all the Viewrootimpl
    • Mviews[]: Store all the Viewroot
    • Mparams[]: Store all the layoutparams.

It also maintains two global ibinder objects to access the two sets of interfaces provided by Windowmanagerservice:

    • Iwindowmanager: The primary interface is Opensession (), which is used to create and initialize the session inside the Windowmanagerservice and return the IBinder object.
    • ISession: is the main interface for the activity window to talk to Windowmanagerservice.

Viewrootimpl:

Viewrootimpl occupies a very important place in the entire Android GUI system, and if you think of activity and view as V in ' MVC ', the various backend services as Modal,viewrootimpl are ' C ' in ' MVC '- Controller. Controller plays a connecting role in the MVC architecture, in general its logic is the most complex. From can see, viewrootimpl with user input system (receiving user key, touch screen input), Window System (complex window layout, refresh, animation), display synthesis system (including Timer choreograph, Surfaceflinger), And even audio systems (sound output) are closely related. Research Viewrootimpl is the core and entry point of the whole Android window system, and we will discuss the implementation and function of Viewrootimpl in detail later.

All three (Viewrootimpl, Windowmanagerimpl, Windowmanagerglobal) exist in the process space of the application (with activity), an activity corresponds to a windowmanagerimpl, A Decorview (Viewroot), and a Viewrootimpl (as stated above, the actual activity has only one decorview), and Windowmanagerglobals is a global object, an application is always only one.

Note that in some cases an app may have several Viewrootimpl objects, such as the ANR Popup dialog, or a video window (Surfaceview) in the Web page, which, in Windowmanagerservice's view, is also a window. At the same time, Systemserver's process space also has its own windowmanagerglobals and several viewroot, because Windowmanagerservice also manages some system windows, such as the StatusBar at the top of the phone, The Navigationbar at the bottom of the phone, and the lock screen (keyguard) window, are not part of a particular activity.

4. WindowManager, Windowmanagerservice and Windowmanagerpolicyservice

WindowManager: is an interface class that defines a number of interfaces to manage windows in the acitivity. WindowManager is an object in the Android app process space and does not provide IPC services.

Windowmanagerservice: is a service in the Systemserver process, its main function is

    • The display of the window refreshes. The ' window ' in this case is actually viewroot, and it is not the same as the ' windows ' above WindowManager management, the former is actually to be displayed in the ' windowed ', and the latter is just a view container and will not be displayed. In most cases, Android has only one activity at the same time, but it doesn't mean that only one window is displayed, and Android may display multiple windows from the same or different applications at the same time, for example, there is a status bar above the screen, and a navigation bar at the bottom , sometimes pop up some dialog boxes, the background may display wallpaper, in the application launch process, there will be animated, this time the two activity window will be deformed and displayed at the same time, all this requires WindowManager to control when, where, The way in which all windows are combined to display.
    • Preprocess user input time (globalkey? Systemkey) and distributed to the appropriate window for processing.
    • Output display management. including Wifidisplay.

Windowmanagerservice is one of the largest and most complex modules in the Android framework, and we'll analyze it in every way possible.

5. Token, Windowtoken, Appwindowtoken, Applicationtoken, Apptoken

Tokens in English denote tokens, meaning of tokens, in code, somewhat like Handle,cookie, ID, used to identify a particular object. There are a lot of ' tokens ' in the android window System, and they represent different meanings.

Windowtoken: is a base class defined in Windowmanagerservice, as the name implies, which is used to identify a window. Compared to the following appwindowtoken, it does not belong to a specific activity, such as the Input Method window, the Status bar window, and so on.

Appwindowtoken: As the name implies, it is used to identify the app, with an accurate statement, to identify a specific activity.

Applicationtoken: Refers to the token subclass in the Activityrecord class. The Apptoken in Appwindowtoken is it.

Apptoken: And Applicationtoken is a meaning.

The following figure depicts the relationship between tokens. A token is placed under a windowlist queue with all the Windows attached to the token. When a window joins Windowmanagerservice management, it must specify his token value, Windowmanagerservice maintains a token and WindowState key-value hash table.

Through ' dumpsys window tokens ' We can list all current tokens and windows of Windowmanagerservice. Like what

Window MANAGER TOKENS (dumpsys window TOKENS) all  TOKENS:  windowtoken{4ea639c4 null}://token = null    windows =[window{4ea7670c u0 application Not Responding:jackpal.androidterm}, window{4ea63a08 u0 Keyguard}]    Windowtype=-1 Hidden=false hasvisible=true  appwindowtoken{4eb29760 token=token{4eb289d4 activityrecord{4ea87a20 u0 Com.android.launcher/com.android.launcher2.launcher}}://launcher2    windows=[window{4ea837c8 u0 Com.android.launcher/com.android.launcher2.launcher}]    windowtype=2 hidden=true hasvisible=true    ...  windowtoken{4eb1fd48 [email protected]}:    windows=[window{4ea92b78 u0 popupwindow:4ea0240c}]  //dialog box    Windowtype=-1 hidden=false hasvisible=false  appwindowtoken{4eb5d6c0 token=token{4ea35074 ActivityRecord{ 4ea68590 u0 jackpal.androidterm/. Term}}}:    windows=[window{4eb314e4 u0 jackpal.androidterm/jackpal.androidterm.term}]    windowtype=2 hidden =false hasvisible=true    App=true

6. Surface, Layer and Canvas, Surfaceflinger, Region, Layerstack

In Android, window corresponds to surface one by one . If window is concerned with hierarchies and layouts, it is a class defined from the designer's point of view, and surface is the kind of engineer relationship and consideration from the perspective of implementation. The contents of the window change, and surface needs space to record the contents of the window at each moment. In Android's Surfaceflinger implementation, usually a surface has two buffer, a piece for painting, a piece for display, two Buffer to the fixed frequency to exchange, so that the dynamic Refresh window.

Layer is the basic operating unit for Surfaceflinger synthesis. Layer is created inside Surfaceflinger when the app requests to create surface, so one surface corresponds to a Layer, but note that surface doesn't necessarily correspond to window, Some of the surface in Android is not related to a window, but it has programs created directly, such as Strictmode, a red background, hints for some exceptions in Java code, and Surfaceview for displaying video content with hardware output.

When multiple layers are synthesized, not the entire layer will be fully displayed, depending on the layer's final display, a layer can be divided into a lot of region, Android Surfaceflinger Some of the following region types are defined:

    • Transparantregion: Completely transparent area, where the area below it is to be displayed.
    • Opaqueregion: A completely opaque area, whether the display depends on whether it is obscured or transparent.
    • Visibleregion: Visible areas, including completely opaque or translucent areas. Visibleregion = Region-above opaqueregion.
    • Coveredregion: Obscured area, above it, with opaque or translucent areas.
    • DirtyRegion: The visible part changes the area, including the new occluded area, and the new exposed area.

The Android system supports a variety of display devices, such as outputting to a phone screen or projecting to a TV screen via WiFi. Android uses the display class to represent such devices. Not all layers will be output to all display, for example, we can only project the video layer onto the TV, not the entire screen. Layerstack is designed for this purpose, Layerstack is a value of a display object, and the class layer has member variable mlayerstack, only the mlayerstack values of the two are the same, Layer will be output to the display device. So layerstack determines the number of layers that can be displayed on each display device.

Surfaceflinger's job is to periodically check all layer parameter updates (layerstack, etc.), calculate the new dirtyregion, and then push the results to the underlying display driver for display. There's a lot of detail in this, and we'll be studying it in another chapter.

Several of the concepts described above are aimed at showing this level, more involved in the middle and lower modules, and the application layer is not involved and does not need to be concerned. For the application, it is concerned with how to draw the content. Canvas is a class defined by the Java layer that corresponds to an area on your surface and provides a lot of 2D drawing functions (with the help of the underlying Skia or OpenGL). The app simply gets a canvas object through Lockcanvas (), invokes its painting method, and then Unlockcanvasandpost () to notify the underlying to display the updated content. Of course, not all applications need to operate directly canva, in fact, only a small number of applications need to directly manipulate the canvas, Android provides a lot of packaged control widgets, the application only need to provide material, such as text, pictures, attributes and so on, These controls invoke the interface provided by canvas to help the user finish the drawing work.

7. Surfaceflinger, Hwcomposer, OpenGL and Display

Surfaceflinger is a standalone service that takes all of the window's surface as input and calculates the position of each surface in the final composite image, based on parameters such as ZOrder, transparency, size, position, It is then sent to Hwcomposer or OpenGL to generate the final display buffer, which is then displayed on a specific display device.

Hwcomposer is a new feature introduced after Andrid 4.0, which defines a set of HAL layer interfaces, which are then implemented by each chip manufacturer based on a variety of hardware features. Its main task is to synthesize the display parameters of the Surfaceflinger computed layer to a display buffer. Note that surface Flinger is not the only input for hwcomposer, and some surface is not managed by Android WindowManager, such as the camera's preview input buffer, which can be directly written by hardware, Then as one of the inputs of the hwcomposer and the output of the Surfaceflinger to do the final synthesis.

OpenGL is a 2d/3d graphics library that requires the support of the underlying hardware (GPU) and drivers. After Android 4.0, it replaced Skia as the Android 2D drawing graphics library, most of the controls are used to implement it, the application can also call the OpenGL function directly to implement a complex graphical interface.

Display is an abstraction of Android to the output display device, the traditional display device is the LCD screen on the phone, after Andrid 4.1, Android has made a lot of changes to the Surfaceflinger to support other external input devices, such as HDMI, Wifi Display and so on. The input of display is the surface of all windows that is filtered according to the Layerstack value above, and the output is the same buffer as the display device size, which is eventually sent to the hardware FB device, or the HDMI device, or the remote WiFi Displays the display sink device. Input to output this path has Surfaceflinger, OpenGL, and Hwcomposer.

With the above concept of interpretation, the Android GUI system should have some vague understanding, next we will be in the following order to step into the details.

Graphical android-android GUI System (1)-Introduction

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.