Android P WMS(2) -- wms初始化

Android P WMS(1) -- wms简介

Android P WMS(2) -- wms初始化

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 问题种类和debug技巧

Android P WMS(8) --View SYstem 简介

Android P WMS(9) --Surface

 

WMS的初始化

@/frameworks/base/services/java/com/android/server/SystemServer.java
private void startOtherServices() {
       WindowManagerService wm = null;
       InputManagerService inputManager = null;

           traceBeginAndSlog("InitWatchdog");
           final Watchdog watchdog = Watchdog.getInstance();
           watchdog.init(context, mActivityManagerService);
           traceEnd();

           traceBeginAndSlog("StartInputManagerService");
           inputManager = new InputManagerService(context);  //4  需要input,因此才有事件输入
           traceEnd();

           traceBeginAndSlog("StartWindowManagerService");
           // 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,
                   DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO);
           ServiceManager.addService(Context.INPUT_SERVICE, inputManager,
                   /* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL);
           traceEnd();

           traceBeginAndSlog("SetWindowManagerService");
           mActivityManagerService.setWindowManager(wm);
           traceEnd();

           traceBeginAndSlog("WindowManagerServiceOnInitReady");
           wm.onInitReady();  //android o没有
           traceEnd();

   
           traceBeginAndSlog("StartInputManager");
           inputManager.setWindowManagerCallbacks(wm.getInputMonitor());
           inputManager.start();
           traceEnd();

           // TODO: Use service dependencies instead.
           traceBeginAndSlog("DisplayManagerWindowManagerAndInputReady");
           mDisplayManagerService.windowManagerAndInputReady();
           traceEnd();


        traceBeginAndSlog("MakeWindowManagerServiceReady");
        try {
            wm.systemReady();
        } catch (Throwable e) {
            reportWtf("making Window Manager Service ready", e);
        }
        traceEnd();

 
        // 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);
        w.getDefaultDisplay().getMetrics(metrics);
        context.getResources().updateConfiguration(config, metrics);

} 

startOtherServices方法用于启动其他服务,其他服务大概有70多个,上面的代码只列出了WMS以及和它相关的IMS的启动逻辑,剩余的其他服务的启动逻辑也都大同小异。 
在注释1、2处分别得到Watchdog实例并对它进行初始化,Watchdog用来监控系统的一些关键服务的运行状况,后文会再次提到它。在注释3处创建了IMS,并赋值给IMS类型的inputManager对象。注释4处执行了WMS的main方法,其内部会创建WMS,需要注意的是main方法其中一个传入的参数就是注释1处创建的IMS,WMS是输入事件的中转站,其内部包含了IMS引用并不意外。结合上文,我们可以得知WMS的main方法是运行在SystemServer的run方法中,换句话说就是运行在”system_server”线程”中,后面会再次提到”system_server”线程。 
注释5和注释6处分别将WMS和IMS注册到ServiceManager中,这样如果某个客户端想要使用WMS,就需要先去ServiceManager中查询信息,然后根据信息与WMS所在的进程建立通信通路,客户端就可以使用WMS了。注释7处用来初始化显示信息,注释8处则用来通知WMS,系统的初始化工作已经完成,其内部调用了WindowManagerPolicy的systemReady方法。 
 

@http://androidxref.com/9.0.0_r3/xref/frameworks/base/services/java/com/android/server/SystemServer.java#875    
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();
        mDisplaySettings.readSettingsLocked();

        mPolicy = policy;
        mAnimator = new WindowAnimator(this);  //创建了WindowAnimator,它用于管理所有的窗口动画
        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);

        ...
 
        //得到AMS实例,并赋值给mActivityManager ,这样WMS就持有了AMS的引用            
        mActivityManager = ActivityManager.getService();
        mAmInternal = LocalServices.getService(ActivityManagerInternal.class);
      
        IntentFilter filter = new IntentFilter();
        // Track changes to DevicePolicyManager state so we can enable/disable keyguard.
        filter.addAction(ACTION_DEVICE_POLICY_MANAGER_STATE_CHANGED);
        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);
        mHoldingScreenWakeLock.setReferenceCounted(false);

        mSurfaceAnimationRunner = new SurfaceAnimationRunner();



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

public void onInitReady() {
    initPolicy(); //初始化了窗口管理策略的接口类WindowManagerPolicy(WMP)

    // Add ourself to the Watchdog monitors.
    Watchdog.getInstance().addMonitor(this);  //加入到watchdog

    openSurfaceTransaction();
    try {
        createWatermarkInTransaction();
    } finally {
        closeSurfaceTransaction("createWatermarkInTransaction");
    }

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

 

运行线程状态

 

initPolicy方法和此前讲的WMS的main方法的实现类似,注释1处执行了WMP的init方法,WMP是一个接口,init方法的具体实现在PhoneWindowManager(PWM)中。PWM的init方法运行在”android.ui”线程中,它的优先级要高于initPolicy方法所在的”android.display”线程,因此”android.display”线程要等PWM的init方法执行完毕后,处于等待状态的”android.display”线程才会被唤醒从而继续执行下面的代码。

在本文中共提到了3个线程,分别是”system_server”、”android.display”和”android.ui”,为了便于理解,下面给出这三个线程之间的关系。
 

 

main运行在DisplayThread(android.display),initPolicy运行在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() {
        @Override
        public void run() {
            WindowManagerPolicyThread.set(Thread.currentThread(), Looper.myLooper());
            mPolicy.init(mContext, WindowManagerService.this, WindowManagerService.this);
        }
    }, 0);
}
//DisplayThread  android.display
/frameworks/base/services/core/java/com/android/server/
H A D	DisplayThread.java	36 super("android.display", Process.THREAD_PRIORITY_DISPLAY + 1, false /*allowIo*/);

//UiThread android.ui
/frameworks/base/services/core/java/com/android/server/
H A D	UiThread.java	36 super("android.ui", Process.THREAD_PRIORITY_FOREGROUND, false /*allowIo*/);

 

“system_server”线程中会调用WMS的main方法,main方法中会创建WMS,创建WMS的过程运行在”android.display”线程中,它的优先级更高一些,因此要等创建WMS完毕后才会唤醒处于等待状态的”system_server”线程。 
WMS初始化时会执行initPolicy方法,initPolicy方法会调用PWM的init方法,这个init方法运行在”android.ui”线程,并且优先级更高,因此要先执行完PWM的init方法后,才会唤醒处于等待状态的”android.display”线程。 
PWM的init方法执行完毕后会接着执行运行在”system_server”线程的代码,比如本文前部分提到WMS的 
systemReady方法。
 

总结

wms初始化和前面的android版本变化不是很大,可以进行对比分析

 

源码路径: http://androidxref.com/9.0.0_r3/

参考书籍: Android解析WindowManager系列

                 Android解析WindowManagerService(一)WMS的诞生

              《深入理解Android:卷III》 

 

 

 

 

 

你可能感兴趣的:(WMS)