> 文章列表 > 图形系统:简简单单学习WindowManagerService的启动流程

图形系统:简简单单学习WindowManagerService的启动流程

图形系统:简简单单学习WindowManagerService的启动流程

作者:新小梦

在系统启动流程中,Zygote进程通过fork自己来创建SystemServer进程。SystemServer进程的主要职责之一就是创建SystemServiceManger,使自己成为服务的注册中心,并启动三种不同类型的服务:引导服务、核心服务、其他服务。

WMS被划分到其他服务。我们以SystemServer的main函数为入口来分析WMS的启动流程。

1、SystemServer.main

public static void main(String[] args) {new SystemServer().run();
}

2、SystemServer.run

如下所见,SystemServer先加载android_servers动态库,创建SystemServiceManager对象,将服务划分为三种类型的服务引导服务、核心服务、其他服务。

private void run() {// 初始化本地服务System.loadLibrary("android_servers");...//创建系统服务管理mSystemServiceManager = new SystemServiceManager(mSystemContext);...//启动三种类型的服务try {startBootstrapServices(t);startCoreServices(t);startOtherServices(t);} catch (Throwable ex) {...} finally {...}
}

3、SystemServer.startOtherServices

startOtherServices函数会创建非常多的服务,这里重点关注WMS的创建,先创建了InputManagerService,再调用WindowManagerService的main函数。

...     
inputManager = new InputManagerService(context);
... 
wm = WindowManagerService.main(context, inputManager, !mFirstBoot, mOnlyCore,new PhoneWindowManager(), mActivityManagerService.mActivityTaskManager);
... 
ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false,DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO);
ServiceManager.addService(Context.INPUT_SERVICE, inputManager,
... 
//初始化PhoneWindowManager和添加WatchDog监听
wm.onInitReady();
...wm.displayReady();
...
wm.systemReady();

4、WindowManagerService.main

    public static WindowManagerService main(final Context context, final InputManagerService im,final boolean showBootMsgs, final boolean onlyCore, WindowManagerPolicy policy,ActivityTaskManagerService atm, Supplier<SurfaceControl.Transaction> transactionFactory,Supplier<Surface> surfaceFactory,Function<SurfaceSession, SurfaceControl.Builder> surfaceControlFactory) {DisplayThread.getHandler().runWithScissors(() ->sInstance = new WindowManagerService(context, im, showBootMsgs, onlyCore, policy,atm, transactionFactory, surfaceFactory, surfaceControlFactory), 0);return sInstance;}

DisplayThread继承自ServiceThread类,ServiceThread又继承自HandlerThread类。是一个前台系统共享的单例线程,主要服务于WindowManager、DisplayManager、InputManager快速实时执行显示相关的操作。

5、Handler.runWithScissors

public final boolean runWithScissors(@NonNull Runnable r, long timeout) {if (r == null) {throw new IllegalArgumentException("runnable must not be null");}if (timeout < 0) {throw new IllegalArgumentException("timeout must be non-negative");}if (Looper.myLooper() == mLooper) {r.run();return true;}BlockingRunnable br = new BlockingRunnable(r);return br.postAndWait(this, timeout);
}

当前代码是运行在SystemServer的主线程,runWithScissors函数使得Runable对象的run函数切换到DisplayThread代码执行。创建了BlockingRunnable类型的对象r,并调用其他postAndWait函数。此时参数timeout是0,this是自身Handler对象。

6、BlockingRunnable.postAndWait

//BlockingRunnable的postAndWait函数
public boolean postAndWait(Handler handler, long timeout) {if (!handler.post(this)) {//1return false;}synchronized (this) {if (timeout > 0) {while (!mDone) {...try {wait(delay);} catch (InterruptedException ex) {}}} else {while (!mDone) { //2try {wait(); //3} catch (InterruptedException ex) {}}}}return true;
}
//BlockingRunnable的run函数,mTask是构造函数传递进来的Runable对象        
public void run() {try {mTask.run(); //4} finally {synchronized (this) {mDone = true; //5notifyAll();}}
}

注释1,由于handler对象来自DisplayThread线程,所以BlockingRunnable的run函数会在DisplayThread线程中执行。DisplayThread线程在执行注释4后,会执行注释5,将mDone设置为true,然后通知其他等待的线程继续执行。

注意到此时还运行在SystemServer线程中,当注释1的Runable对象添加到DisplayThread线程的Handler的消息队列会立即返回true,继续执行注释1后续代码。而不是等待DisplayThread线程执行完BlockingRunnable对象的run函数再返回。mDone变量默认false,执行到注释3代码时,如果mDone为true,SystemServer线程则不进入等待状态,代表DisplayThread已经执行完run函数,否则进入等待状态,直到DisplayThread线程执行完run函数通知SystemThread线程醒来继续执行。

总的来说就是,SystemServer线程会等待DisplayThread线程创建好WMS之后,再继续执行后续内容。到这里WindowMangerService对象也创建完毕。


WMS 是学习了解Framework底层原理的核心知识点之一,在Framework底层原理中除了WMS要学习以外,还有Binder、Handler、AMS、PMS……等知识点都要学习,这样才能全面的了解Framework底层原理,为了帮助大家可以更快速的掌握理解这个版块的知识点,这边整理了 《Framework 学习手册》,里面将其Framework中所有的知识点记录在册了,希望能给大家有效的帮助。

《Android Framework学习手册》:https://qr18.cn/AQpN4J

  1. 开机Init 进程
  2. 开机启动 Zygote 进程
  3. 开机启动 SystemServer 进程
  4. Binder 驱动
  5. AMS 的启动过程
  6. PMS 的启动过程
  7. Launcher 的启动过程
  8. Android 四大组件
  9. Android 系统服务 - Input 事件的分发过程
  10. Android 底层渲染 - 屏幕刷新机制源码分析
  11. Android 源码分析实战

图形系统:简简单单学习WindowManagerService的启动流程