WMS也是由SystemServer启动的
Q:\MTK_U\t-alps-mp-u0.mp1.rc-phone-Vx.xx\frameworks\base\services\java\com\android\server\SystemServer.java
private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
t.traceBegin("startOtherServices");
.......
WindowManagerService wm = null;
......
t.traceBegin("StartWindowManagerService");
//在启动WMS之前需要先启动Sensor service
mSystemServiceManager.startBootPhase(t, SystemService.PHASE_WAIT_FOR_SENSOR_SERVICE);
//传入inputmanager ATMS 对象
//创建一个PhoneWindowManager 对象
//这里的PhoneWindowManager对象实际上是WindowManagerPolicy
wm = WindowManagerService.main(context, inputManager, !mFirstBoot,
new PhoneWindowManager(), mActivityManagerService.mActivityTaskManager);
//把WMS对象加入到ServiceManager的监控队列之中
ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false,
DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO);
t.traceBegin("SetWindowManagerService");
//把WMS 对象给到AMS
mActivityManagerService.setWindowManager(wm);
t.traceEnd();
//调用WMS的onInitReady 初始化方法
t.traceBegin("WindowManagerServiceOnInitReady");
wm.onInitReady();
t.traceEnd();
.......
t.traceBegin("MakeDisplayReady");
try {
wm.displayReady();
} catch (Throwable e) {
reportWtf("making display ready", e);
}
t.traceEnd();
.......
t.traceBegin("MakeWindowManagerServiceReady");
try {
wm.systemReady();
} catch (Throwable e) {
reportWtf("making Window Manager Service ready", e);
}
t.traceEnd();
.......
//更新下Configuration
final Configuration config = wm.computeNewConfiguration(DEFAULT_DISPLAY);
DisplayMetrics metrics = new DisplayMetrics();
context.getDisplay().getMetrics(metrics);
context.getResources().updateConfiguration(config, metrics);
systemserver 中执行了WMS的以下方法:
main
onInitReady
displayReady
systemReady
computeNewConfiguration
main
Q:\MTK_U\t-alps-mp-u0.mp1.rc-phone-Vx.xx\frameworks\base\services\core\java\com\android\server\wm\WindowManagerService.java
public static WindowManagerService main(final Context context, final InputManagerService im,
final boolean showBootMsgs, WindowManagerPolicy policy,
ActivityTaskManagerService atm) {
return main(context, im, showBootMsgs, policy, atm, new DisplayWindowSettingsProvider(),
SurfaceControl.Transaction::new, SurfaceControl.Builder::new);
}
public static WindowManagerService main(final Context context, final InputManagerService im,
final boolean showBootMsgs, WindowManagerPolicy policy, ActivityTaskManagerService atm,
DisplayWindowSettingsProvider displayWindowSettingsProvider,
Supplier<SurfaceControl.Transaction> transactionFactory,
Function<SurfaceSession, SurfaceControl.Builder> surfaceControlFactory) {
final WindowManagerService[] wms = new WindowManagerService[1];
DisplayThread.getHandler().runWithScissors(() ->
wms[0] = new WindowManagerService(context, im, showBootMsgs, policy, atm,
displayWindowSettingsProvider, transactionFactory,
surfaceControlFactory), 0);
return wms[0];
}
main方法主要做两件事情:
1.调用WMS的构造方法,创建WMS对象并返回;
2.调用DisplayThread的getHandler对象,并将WMS对象传递给DisplayThread;
WMS构造方法
private WindowManagerService(Context context, InputManagerService inputManager,
boolean showBootMsgs, WindowManagerPolicy policy, ActivityTaskManagerService atm,
DisplayWindowSettingsProvider displayWindowSettingsProvider,
Supplier<SurfaceControl.Transaction> transactionFactory,
Function<SurfaceSession, SurfaceControl.Builder> surfaceControlFactory) {
installLock(this, INDEX_WINDOW);
mGlobalLock = atm.getGlobalLock();
//拿到AMS context进行赋值
mAtmService = atm;
mContext = context;
........
//拿到InputManager对象
mInputManager = inputManager;
........
//surface相关
mSurfaceControlFactory = surfaceControlFactory;
mTransactionFactory = transactionFactory;
mTransaction = mTransactionFactory.get();
.......
//创建 WindowAnimator 对象 窗口动画
mAnimator = new WindowAnimator(this);
//创建RootWindowContainer 对象 根窗口容器
mRoot = new RootWindowContainer(this);
........
//拿到DisplayerManagerservice
mDisplayManager = (DisplayManager)context.getSystemService(Context.DISPLAY_SERVICE);
// AMS相关
mActivityManager = ActivityManager.getService();
mAmInternal = LocalServices.getService(ActivityManagerInternal.class);
mUmInternal = LocalServices.getService(UserManagerInternal.class);
........
}
onInitReady
public void onInitReady() {
//PhoneWindowManager相关
initPolicy();
//把WMS加入到WatchDog的监控中
Watchdog.getInstance().addMonitor(this);
//创建系统水印
createWatermark();
showEmulatorDisplayOverlayIfNeeded();
displayReady
public void displayReady() {
synchronized (mGlobalLock) {
if (mMaxUiWidth > 0) {
mRoot.forAllDisplays(displayContent -> displayContent.setMaxUiWidth(mMaxUiWidth));
}
applyForcedPropertiesForDefaultDisplay();
mAnimator.ready();
mDisplayReady = true;
//根据DisplayContent重新配置显示器的大小
mRoot.forAllDisplays(DisplayContent::reconfigureDisplayLocked);
//判断是否是触摸屏
mIsTouchDevice = mContext.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_TOUCHSCREEN);
mIsFakeTouchDevice = mContext.getPackageManager().hasSystemFeature(
PackageManager.FEATURE_FAKETOUCH);
}
//AMS更新
mAtmService.updateConfiguration(null /* request to compute config */);
}
systemReady
public void systemReady() {
mSystemReady = true;
//调用PhoneWindowManager的systemReady
mPolicy.systemReady();
mRoot.forAllDisplayPolicies(DisplayPolicy::systemReady);
mSnapshotController.systemReady();
mHasWideColorGamutSupport = queryWideColorGamutSupport();
mHasHdrSupport = queryHdrSupport();
//加载系统设置信息
UiThread.getHandler().post(mSettingsObserver::loadSettings);
IVrManager vrManager = IVrManager.Stub.asInterface(
ServiceManager.getService(Context.VR_SERVICE));
//VR相关
if (vrManager != null) {
try {
final boolean vrModeEnabled = vrManager.getVrModeState();
synchronized (mGlobalLock) {
vrManager.registerListener(mVrStateCallbacks);
if (vrModeEnabled) {
mVrModeEnabled = vrModeEnabled;
mVrStateCallbacks.onVrStateChanged(vrModeEnabled);
}
}
} catch (RemoteException e) {
// Ignore, we cannot do anything if we failed to register VR mode listener
}
}
DisplayThread
Q:\MTK_U\t-alps-mp-u0.mp1.rc-phone-Vx.xx\frameworks\base\services\core\java\com\android\server\DisplayThread.java
public final class DisplayThread extends ServiceThread {
private static DisplayThread sInstance;
private static Handler sHandler;
private DisplayThread() {
// DisplayThread runs important stuff, but these are not as important as things running in
// AnimationThread. Thus, set the priority to one lower.
super("android.display", Process.THREAD_PRIORITY_DISPLAY + 1, false /*allowIo*/);
}
DisplayThread继承自ServiceThread
trace中看到的android.display 线程就是DisplayThread,该线程的优先级是Process.THREAD_PRIORITY_DISPLAY + 1(-3)
Q:\MTK_U\t-alps-mp-u0.mp1.rc-phone-Vx.xx\frameworks\base\services\core\java\com\android\server\UiThread.java
public final class UiThread extends ServiceThread {
private static final long SLOW_DISPATCH_THRESHOLD_MS = 100;
private static final long SLOW_DELIVERY_THRESHOLD_MS = 200;
private static UiThread sInstance;
private static Handler sHandler;
private UiThread() {
super("android.ui", Process.THREAD_PRIORITY_FOREGROUND, false /*allowIo*/);
}
@Override
public void run() {
// Make sure UiThread is in the fg stune boost group
Process.setThreadGroup(Process.myTid(), Process.THREAD_GROUP_TOP_APP);
super.run();
}