Android P WMS -- wms initialization

Android P WMS (1) -- Introduction to WMS

Android P WMS(2) -- wms initialization

Android O WMS(3) -- addwindow

Android P WMS(4) -- removewindow

Android P WMS(5) -- relayoutWindow

Android P WMS(6) -- windowanimator

Android P WMS(7) --wms Problem Types and Deug Skills

Android P WMS (8) -- Introduction to View SYstem

Android P WMS(9) --Surface


Initialization of WMS

private void startOtherServices() {
       WindowManagerService wm = null;
       InputManagerService inputManager = null;

           final Watchdog watchdog = Watchdog.getInstance();
           watchdog.init(context, mActivityManagerService);

           inputManager = new InputManagerService(context);  //4 requires input, so event input is required

           // WMS needs sensor service ready
           ConcurrentUtils.waitForFutureNoInterrupt(mSensorServiceStart, START_SENSOR_SERVICE);
           mSensorServiceStart = null;
           wm = WindowManagerService.main(context, inputManager,
                   mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL,
                   !mFirstBoot, mOnlyCore, new PhoneWindowManager());
           ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false,
           ServiceManager.addService(Context.INPUT_SERVICE, inputManager,
                   /* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL);


           wm.onInitReady();  //android o does not


           // TODO: Use service dependencies instead.

        try {
        } catch (Throwable e) {
            reportWtf("making Window Manager Service ready", e);

        // Update the configuration for this context by hand, because we're going
        // to start using it before the config change done in wm.systemReady() will
        // propagate to it.
        final Configuration config = wm.computeNewConfiguration(DEFAULT_DISPLAY);
        DisplayMetrics metrics = new DisplayMetrics();
        WindowManager w = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        context.getResources().updateConfiguration(config, metrics);


The startOther Services method is used to start other services. There are about 70 other services. The above code only lists the startup logic of WMS and its related IMS. The startup logic of the remaining services is similar.  
Watchdog instances are obtained and initialized at Notes 1 and 2 respectively. Watchdog is used to monitor the operation of some key services, which will be mentioned again later. IMS is created at Note 3 and assigned to an inputManager object of type IMS. Note 4 implements the main method of WMS, which creates WMS internally. It should be noted that one of the incoming parameters of the main method is the IMS created at Note 1. WMS is a transit station for input events, and it is not surprising that it contains an IMS reference. Combined with the above, we can know that the main method of WMS is to run in the run method of System Server. In other words, it runs in the "system_server" thread, which will be mentioned again later.  
Note 5 and Note 6 register WMS and IMS into Service Manager respectively, so that if a client wants to use WMS, it needs to go to Service Manager to query information first, and then establish communication paths with the WMS process according to the information, and the client can use WMS. Annotation 7 is used to initialize display information, and annotation 8 is used to notify WMS that the initialization of the system has been completed. The system Ready method of Windows Manager Policy is called internally.  

private WindowManagerService(Context context, InputManagerService inputManager,
            boolean haveInputMethods, boolean showBootMsgs, boolean onlyCore,
            WindowManagerPolicy policy) {
        mInputManager = inputManager; // Must be before createDisplayContentLocked.
        mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
        mDisplaySettings = new DisplaySettings();

        mPolicy = policy;
        mAnimator = new WindowAnimator(this);  //Windows Animator was created to manage all window animations
        mRoot = new RootWindowContainer(this);

        mWindowPlacerLocked = new WindowSurfacePlacer(this);
        mTaskSnapshotController = new TaskSnapshotController(this);

        mWindowTracing = WindowTracing.createDefaultAndStartLooper(context);

        LocalServices.addService(WindowManagerPolicy.class, mPolicy);

        if(mInputManager != null) {
            final InputChannel inputChannel = mInputManager.monitorInput(TAG_WM);
            mPointerEventDispatcher = inputChannel != null
                    ? new PointerEventDispatcher(inputChannel) : null;
        } else {
            mPointerEventDispatcher = null;

        mDisplayManager = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);

        mKeyguardDisableHandler = new KeyguardDisableHandler(mContext, mPolicy);

        //Get the AMS instance and assign it to mActivityManager so that WMS holds the reference to AMS            
        mActivityManager = ActivityManager.getService();
        mAmInternal = LocalServices.getService(ActivityManagerInternal.class);
        IntentFilter filter = new IntentFilter();
        // Track changes to DevicePolicyManager state so we can enable/disable keyguard.
        mContext.registerReceiver(mBroadcastReceiver, filter);

        mLatencyTracker = LatencyTracker.getInstance(context);

        mSettingsObserver = new SettingsObserver();

        mHoldingScreenWakeLock = mPowerManager.newWakeLock(
                PowerManager.SCREEN_BRIGHT_WAKE_LOCK | PowerManager.ON_AFTER_RELEASE, TAG_WM);

        mSurfaceAnimationRunner = new SurfaceAnimationRunner();

        LocalServices.addService(WindowManagerInternal.class, new LocalService());

public void onInitReady() {
    initPolicy(); //Windows Manager Policy (WMP), an interface class that initializes window management policies

    // Add ourself to the Watchdog monitors.
    Watchdog.getInstance().addMonitor(this);  //Join watchdog

    try {
    } finally {

    private void initPolicy() {
        UiThread.getHandler().runWithScissors(new Runnable() {
            public void run() {
                WindowManagerPolicyThread.set(Thread.currentThread(), Looper.myLooper());
                mPolicy.init(mContext, WindowManagerService.this, WindowManagerService.this);
        }, 0);


Running thread status


The initPolicy method is similar to the main method of WMS mentioned earlier. The init method of WMP is executed in Note 1. WMP is an interface. The implementation of init method is in Phone Windows Manager (PWM). The init method of PWM runs in the "android.ui" thread, and its priority is higher than the "android.display" thread where the initPolicy method is located. Therefore, the "android.display" thread will not be awakened until the init method of PWM has been executed and the "android.display" thread in the waiting state will continue to execute the following generation. Code.

In this paper, three threads are mentioned, namely "system_server", "android.display" and "android.ui". For ease of understanding, the relationship between the three threads is given below.


main runs in DisplayThread(android.display),initPolicy runs in UiThread(android.ui)

//android.display thread
public static WindowManagerService main(final Context context, final InputManagerService im,
        final boolean haveInputMethods, final boolean showBootMsgs, final boolean onlyCore,
        WindowManagerPolicy policy) {
    DisplayThread.getHandler().runWithScissors(() ->
            sInstance = new WindowManagerService(context, im, haveInputMethods, showBootMsgs,
                    onlyCore, policy), 0);
    return sInstance;

//android.ui thread
private void initPolicy() {
    UiThread.getHandler().runWithScissors(new Runnable() {
        public void run() {
            WindowManagerPolicyThread.set(Thread.currentThread(), Looper.myLooper());
            mPolicy.init(mContext, WindowManagerService.this, WindowManagerService.this);
    }, 0);
//DisplayThread  android.display
H A D	36 super("android.display", Process.THREAD_PRIORITY_DISPLAY + 1, false /*allowIo*/);

//UiThread android.ui
H A D	36 super("android.ui", Process.THREAD_PRIORITY_FOREGROUND, false /*allowIo*/);


The main method of WMS is called in the "system_server" thread. The main method creates WMS. The process of creating WMS runs in the "android.display" thread. It has a higher priority. Therefore, the "system_server" thread in the waiting state will not be awakened until the creation of WMS is completed.  
The initPolicy method is executed when WMS is initialized. The initPolicy method calls the init method of PWM. This init method runs on the "android.ui" thread and has a higher priority. Therefore, the "android.display" thread will not be awakened until the init method of PWM has been executed.  
After the init method of PWM is executed, the code running in the "system_server" thread will be executed, as mentioned in the previous section of this article.
systemReady method.


wms initialization and the previous version of android do not change very much and can be compared and analyzed


Source path:

Reference books: Android Parsing Windows Manager Series

                 Android Parsing Windows Manager Service (I) The Birth of WMS

Deep Understanding of Android: Volume III






Tags: Android Java Windows

Posted on Tue, 27 Aug 2019 20:20:30 -0700 by ronverdonk