WindowManagerService 分析

一 概述

WindowManagerService(WMS)根据字面意思理解就可知道是窗口管理服务,与AMS一样是framewrok层的核心服务;负责窗口的启动,添加,删除等;了解WMS首先来了解下Window的体系结构

1.1 Window体系图

这里写图片描述

二 从WMS的启动分析

2.1 SystemService.startOtherServices

[—>SystemService.java]

private void startOtherServices() {
    ...
    // [见2.2]
    WindowManagerService wm = WindowManagerService.main(context, inputManager,
    mFactoryTestMode != FactoryTest.FACTORY_TEST_LOW_LEVEL,
    !mFirstBoot, mOnlyCore);

    wm.displayReady(); 
    ...
    wm.systemReady(); 
}

与ams一样,在system_service进程创建的时候会启动这些服务

2.2 WMS.main

[—>WindowManagerService.java]

public static WindowManagerService main(final Context context,
            final InputManagerService im,
            final boolean haveInputMethods, final boolean showBootMsgs,
            final boolean onlyCore) {
        final WindowManagerService[] holder = new WindowManagerService[1];
        DisplayThread.getHandler().runWithScissors(new Runnable() {
            @Override
            public void run() {
                //真正的创建wms,运行在"android.display"线程[见2.3]
                holder[0] = new WindowManagerService(context, im,
                        haveInputMethods, showBootMsgs, onlyCore);
            }
        }, 0);
        return holder[0];
    }

可见wms是运行在android.display线程中的

2.3 WindowManagerService

private WindowManagerService(Context context, InputManagerService inputManager, boolean haveInputMethods, boolean showBootMsgs, boolean onlyCore) {
    mContext = context;
    mHaveInputMethods = haveInputMethods;
    mAllowBootMessages = showBootMsgs;
    mOnlyCore = onlyCore;
    ...
    mInputManager = inputManager; 
    mDisplayManagerInternal = LocalServices.getService(DisplayManagerInternal.class);
    mDisplaySettings = new DisplaySettings();
    mDisplaySettings.readSettingsLocked();

    LocalServices.addService(WindowManagerPolicy.class, mPolicy);
    mPointerEventDispatcher = new PointerEventDispatcher(mInputManager.monitorInput(TAG));

    mFxSession = new SurfaceSession();
    mDisplayManager = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);
    mDisplays = mDisplayManager.getDisplays();

    for (Display display : mDisplays) {
        createDisplayContentLocked(display);
    }

    mKeyguardDisableHandler = new KeyguardDisableHandler(mContext, mPolicy);
    ...

    mAppTransition = new AppTransition(context, mH);
    mAppTransition.registerListenerLocked(mActivityManagerAppTransitionNotifier);
    mActivityManager = ActivityManagerNative.getDefault();
    ...
    mAnimator = new WindowAnimator(this);

    LocalServices.addService(WindowManagerInternal.class, new LocalService());
    //初始化策略[见2.4]
    initPolicy();

    Watchdog.getInstance().addMonitor(this);

    SurfaceControl.openTransaction();
    try {
        createWatermarkInTransaction();
        mFocusedStackFrame = new FocusedStackFrame(
                getDefaultDisplayContentLocked().getDisplay(), mFxSession);
    } finally {
        SurfaceControl.closeTransaction();
    }

    updateCircularDisplayMaskIfNeeded();
    showEmulatorDisplayOverlayIfNeeded();
}

2.4 WMS.initPolicy

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);
}

在android ui线程初始化完成后进行策略初始化

2.5 总结

在启动过程中一共有三个线程:system_server主线程,android.d

  • 3
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值