赞
踩
DisplayManagerService是Android Framework中管理显示屏幕相关的模块,各种Display的连接、配置等,都是通过DMS和来管理控制。
在DMS模块中,对显示屏幕的描述分为物理显示屏(physical display)和逻辑显示屏(logical display),物理屏主要从SurfaceFlinger中读取参数创建,代表实实在在的物理屏,描述其物理特性,不同物理屏有不同的属性。逻辑屏则是相对于对物理屏,侧重于同一个物理屏的不同逻辑设置,受应用和WMS模块影响,如显示区域、显示位置坐标、显示方向等。每一个物理屏幕都对应一个逻辑屏幕,可以修改逻辑屏幕参数,做到同一个物理屏的不同显示方式。
由于不同的物理显示屏有不同的属性和处理方式,因此DMS中通过一系列的屏幕适配器(DisplayAdapter)来和分别各种物理显示屏进行连接。
包名 | 目录 | 描述 | 例子 |
---|---|---|---|
android.hardware.display | frameworks/base/core/java/android/hardware/display | DMS模块管理 | DisplayManager, DisplayMangaerGlobal, … |
android.view | frameworks/base/core/java/android/view | 逻辑屏配置项相关 | DisplayInfo, DisplayViewport, … |
com.android.server.display | frameworks/base/services/core/java/com/android/server/display | DMS核心 | DMS,DisplayDevice, … |
系统启动后会启动JVM虚拟机,SystemServer 是虚拟机的第一个进程,由init 进程fork 产生,主要用来启动frameworks层中的服务,DMS服务也是在这里被启动的,下面我们简单列出SystemServer中和DMS启动相关的源码。
frameworks/base/service/java/com/android/server/SystemServer.java
public final class SystemServer { private DisplayManagerService mDisplayManagerService; public static void main(String[] args) { new SystemServer().run(); } private void run() { ...代码省略... try { t.traceBegin("StartServices"); startBootstrapServices(t);//引导服务 startCoreServices(t);//核心服务 startOtherServices(t);//其他服务 } catch (Throwable ex) { Slog.e("System", "******************************************"); Slog.e("System", "************ Failure starting system services", ex); throw ex; } finally { t.traceEnd(); // StartServices } ...代码省略... } private void startBootstrapServices(@NonNull TimingsTraceAndSlog t) { ...代码省略... // Display manager is needed to provide display metrics before package manager // starts up. t.traceBegin("StartDisplayManager"); mDisplayManagerService = mSystemServiceManager.startService(DisplayManagerService.class);//通过反射构建对象实例 t.traceEnd(); // We need the default display before we can initialize the package manager. t.traceBegin("WaitForDisplay"); mSystemServiceManager.startBootPhase(t, SystemService.PHASE_WAIT_FOR_DEFAULT_DISPLAY);//调用DMS的onBootPhase方法 t.traceEnd(); ...代码省略... // DisplayManagerService needs to setup android.display scheduling related policies // since setSystemProcess() would have overridden policies due to setProcessGroup mDisplayManagerService.setupSchedulerPolicies();//调用DMS的setupSchedulerPolicies方法 ...代码省略... } private void startOtherServices(@NonNull TimingsTraceAndSlog t) { ...代码省略... t.traceBegin("DisplayManagerWindowManagerAndInputReady"); mDisplayManagerService.windowManagerAndInputReady();//调用DMS的windowManagerAndInputReady方法 t.traceEnd(); ...代码省略... t.traceBegin("MakeDisplayManagerServiceReady"); try { mDisplayManagerService.systemReady(safeMode, mOnlyCore);//调用DMS的systemReady方法 } catch (Throwable e) { reportWtf("making Display Manager Service ready", e); } t.traceEnd(); } }
DisplayManagerService实例对象是通过SystemServiceManager的startService方法获得的,该方法内部先是通过反射构建DisplayManagerService对象实例的,然后还会调用该对象的onStart方法,先来看下构造方法。
frameworks/base/services/core/java/com/android/server/display/DisplayManagerService.java
public final class DisplayManagerService extends SystemService { private final Context mContext; //android.display线程的Handler private final DisplayManagerHandler mHandler; //android.ui线程Handler private final Handler mUiHandler; //主要负责管理和协调设备的显示模式,包括分辨率、刷新率等。 //它优化了系统的显示设置,以平衡性能和电池使用,并确保在多显示器配置下的显示效果一致。 private final DisplayModeDirector mDisplayModeDirector; //屏幕设备管理者, 此类内容维护了一个 DisplayDevice 对象的集合,这些对象代表了系统中所有的显示设备。 //可以通过这个类跟踪各个显示设备的状态、属性和配置。 private final DisplayDeviceRepository mDisplayDeviceRepo; //主要负责逻辑显示设备的映射和管理,确保物理显示设备的状态和配置能够正确映射到逻辑显示设备上。 //这个类对于支持多显示器和虚拟显示设备的系统功能至关重要。 private final LogicalDisplayMapper mLogicalDisplayMapper; private final Injector mInjector; //负责同步和管理屏幕亮度设置,以确保多个显示设备或显示区域的亮度一致,同时优化用户体验和系统协调。 private final BrightnessSynchronizer mBrightnessSynchronizer; public DisplayManagerService(Context context) { this(context, new Injector()); } @VisibleForTesting DisplayManagerService(Context context, Injector injector) { super(context); mInjector = injector; mContext = context; //android.display线程handler mHandler = new DisplayManagerHandler(DisplayThread.get().getLooper()); //android.ui线程Handler mUiHandler = UiThread.getHandler(); //管理所有的屏幕设备 mDisplayDeviceRepo = new DisplayDeviceRepository(mSyncRoot, mPersistentDataStore); //负责逻辑显示设备的映射和管理 mLogicalDisplayMapper = new LogicalDisplayMapper(mContext, mDisplayDeviceRepo, new LogicalDisplayListener(), mSyncRoot, mHandler); //负责管理和协调设备的显示模式 mDisplayModeDirector = new DisplayModeDirector(context, mHandler); //负责同步和管理屏幕亮度设置 mBrightnessSynchronizer = new BrightnessSynchronizer(mContext); Resources resources = mContext.getResources(); mDefaultDisplayDefaultColorMode = mContext.getResources().getInteger( com.android.internal.R.integer.config_defaultDisplayDefaultColorMode); mDefaultDisplayTopInset = SystemProperties.getInt(PROP_DEFAULT_DISPLAY_TOP_INSET, -1); float[] lux = getFloatArray(resources.obtainTypedArray( com.android.internal.R.array.config_minimumBrightnessCurveLux)); float[] nits = getFloatArray(resources.obtainTypedArray( com.android.internal.R.array.config_minimumBrightnessCurveNits)); mMinimumBrightnessCurve = new Curve(lux, nits); mMinimumBrightnessSpline = Spline.createSpline(lux, nits); mCurrentUserId = UserHandle.USER_SYSTEM; ColorSpace[] colorSpaces = SurfaceControl.getCompositionColorSpaces(); mWideColorSpace = colorSpaces[1]; mAllowNonNativeRefreshRateOverride = mInjector.getAllowNonNativeRefreshRateOverride(); mSystemReady = false; } }
public final class DisplayManagerService extends SystemService { private final PersistentDataStore mPersistentDataStore = new PersistentDataStore(); @Override public void onStart() { // 加载数据 synchronized (mSyncRoot) { mPersistentDataStore.loadIfNeeded();//加载display中的固定数据 loadStableDisplayValuesLocked(); } // 在android.display线程中创建默认DisplayAdapter,并进行注册 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS); // If there was a runtime restart then we may have stale caches left around, so we need to // make sure to invalidate them upon every start. DisplayManagerGlobal.invalidateLocalDisplayInfoCaches(); //向ServiceManager注册BinderService,以便其他模块可以跨进程调用 publishBinderService(Context.DISPLAY_SERVICE, new BinderService(), true /*allowIsolated*/); //注册Local Service publishLocalService(DisplayManagerInternal.class, new LocalService()); } }
frameworks/base/services/core/java/com/android/server/display/PersistentDataStore.java
final class PersistentDataStore { private Injector mInjector; public PersistentDataStore() { this(new Injector()); } @VisibleForTesting PersistentDataStore(Injector injector) { mInjector = injector; } public void loadIfNeeded() { if (!mLoaded) { load(); mLoaded = true; } } private void load() { clearState(); final InputStream is; try { is = mInjector.openRead(); } catch (FileNotFoundException ex) { return; } TypedXmlPullParser parser; try { parser = Xml.resolvePullParser(is); loadFromXml(parser); } catch (IOException ex) { Slog.w(TAG, "Failed to load display manager persistent store data.", ex); clearState(); } catch (XmlPullParserException ex) { Slog.w(TAG, "Failed to load display manager persistent store data.", ex); clearState(); } finally { IoUtils.closeQuietly(is); } } static class Injector { private final AtomicFile mAtomicFile; public Injector() { //display配置的文件路径 mAtomicFile = new AtomicFile(new File("/data/system/display-manager-state.xml"), "display-state"); } public InputStream openRead() throws FileNotFoundException { return mAtomicFile.openRead(); } public OutputStream startWrite() throws IOException { return mAtomicFile.startWrite(); } public void finishWrite(OutputStream os, boolean success) { if (!(os instanceof FileOutputStream)) { throw new IllegalArgumentException("Unexpected OutputStream as argument: " + os); } FileOutputStream fos = (FileOutputStream) os; if (success) { mAtomicFile.finishWrite(fos); } else { mAtomicFile.failWrite(fos); } } } }
利用PersistentDataStore加载display中的固定数据,这些数据都是以xml文件的形式,保存在/data/system/display-manager-state.xml中;
public final class DisplayManagerService extends SystemService { private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1; private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2; @Override public void onStart() { ...代码省略... // android.display线程中创建默认DisplayAdapter,并进行注册 mHandler.sendEmptyMessage(MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS); ...代码省略... } private final class DisplayManagerHandler extends Handler { @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS: //注册默认的屏幕适配器 registerDefaultDisplayAdapters(); break; case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS: //注册额外的屏幕设备适配器 registerAdditionalDisplayAdapters(); break; ...代码省略... } } } private void registerDefaultDisplayAdapters() { synchronized (mSyncRoot) { //注册默认的屏幕适配器 registerDisplayAdapterLocked(new LocalDisplayAdapter( mSyncRoot, mContext, mHandler, mDisplayDeviceRepo)); //注册虚拟的屏幕适配器 mVirtualDisplayAdapter = mInjector.getVirtualDisplayAdapter(mSyncRoot, mContext, mHandler, mDisplayDeviceRepo); if (mVirtualDisplayAdapter != null) { registerDisplayAdapterLocked(mVirtualDisplayAdapter); } } } }
DisplayManagerService的onStart()方法被执行完毕后,onBootPhase()方法将会被执行。
public final class DisplayManagerService extends SystemService { @Override public void onBootPhase(int phase) { if (phase == PHASE_WAIT_FOR_DEFAULT_DISPLAY) { synchronized (mSyncRoot) { //Injector的getDefaultDisplayDelayTimeout默认返回10秒 long timeout = SystemClock.uptimeMillis() + mInjector.getDefaultDisplayDelayTimeout(); // 超过timeout之后,如果默认屏幕的逻辑显示屏为空,则抛出异常 while (mLogicalDisplayMapper.getDisplayLocked(Display.DEFAULT_DISPLAY) == null || mVirtualDisplayAdapter == null) { long delay = timeout - SystemClock.uptimeMillis(); if (delay <= 0) { throw new RuntimeException("Timeout waiting for default display " + "to be initialized. DefaultDisplay=" + mLogicalDisplayMapper.getDisplayLocked(Display.DEFAULT_DISPLAY) + ", mVirtualDisplayAdapter=" + mVirtualDisplayAdapter); } if (DEBUG) { Slog.d(TAG, "waitForDefaultDisplay: waiting, timeout=" + delay); } try { mSyncRoot.wait(delay); } catch (InterruptedException ex) { } } } } else if (phase == PHASE_BOOT_COMPLETED) { mDisplayModeDirector.onBootCompleted(); } } }
PHASE_WAIT_FOR_DEFAULT_DISPLAY阶段是最早的一个启动阶段,当一些引导服务启动完毕后,启动后续服务时,检查DMS是否已经创建默认逻辑屏,如果在10s内没有完成默认LogicalDisplay的创建,则抛出异常。
public final class DisplayManagerService extends SystemService {
public void setupSchedulerPolicies() {
// 给android.display、android.anim、android.anim.lf三个线程设置组和CPU集
Process.setThreadGroupAndCpuset(DisplayThread.get().getThreadId(),
Process.THREAD_GROUP_TOP_APP);
Process.setThreadGroupAndCpuset(AnimationThread.get().getThreadId(),
Process.THREAD_GROUP_TOP_APP);
Process.setThreadGroupAndCpuset(SurfaceAnimationThread.get().getThreadId(),
Process.THREAD_GROUP_TOP_APP);
}
}
给以上三个线程设置线程组和调度集,将线程组设为Process.THREAD_GROUP_TOP_APP,以确保更加高效的执行。
当SystemServer中启动WindowManagerService和InputManagerService后,将调用此方法通知DMS。
public final class DisplayManagerService extends SystemService { private static final int MSG_REQUEST_TRAVERSAL = 4; //WMS和输入法启动完毕 public void windowManagerAndInputReady() { synchronized (mSyncRoot) { mWindowManagerInternal = LocalServices.getService(WindowManagerInternal.class); mInputManagerInternal = LocalServices.getService(InputManagerInternal.class); //获取设备状态服务并设置回调对象 DeviceStateManager deviceStateManager = mContext.getSystemService(DeviceStateManager.class); deviceStateManager.registerCallback(new HandlerExecutor(mHandler), new DeviceStateListener()); //通知WMS进行窗口更新流程 scheduleTraversalLocked(false); } } private void scheduleTraversalLocked(boolean inTraversal) { if (!mPendingTraversal && mWindowManagerInternal != null) { mPendingTraversal = true; if (!inTraversal) { mHandler.sendEmptyMessage(MSG_REQUEST_TRAVERSAL); } } } private final class DisplayManagerHandler extends Handler { @Override public void handleMessage(Message msg) { switch (msg.what) { ...代码省略... case MSG_REQUEST_TRAVERSAL: //通知WMS进行窗口更新流程 mWindowManagerInternal.requestTraversalFromDisplayManager(); break; ...代码省略... } } } }
先是获得WMS和IMS各自的LocalService对象,然后将发起显示内容更新流程,请求WMS模块进行窗口更新。DMS和WMS联系非常紧密,通常当DMS中相关属性发生变化后,会通知WMS进行窗口更新,WMS在更新过程中也会通知DMS,应用来自窗口的Display相关属性。
此方法DMS在SystemServer中被调用的最后一个方法,通知DMS启动完成:
public final class DisplayManagerService extends SystemService { private static final String PROP_DEFAULT_DISPLAY_TOP_INSET = "persist.sys.displayinset.top"; private static final int MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS = 1;//注册物理屏幕适配器、虚拟屏幕适配器 private static final int MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS = 2;//注册其他屏幕适配器 public void systemReady(boolean safeMode, boolean onlyCore) { synchronized (mSyncRoot) { mSafeMode = safeMode; mOnlyCore = onlyCore; mSystemReady = true;//启动完成 //将默认Display的top inset存储到persist.sys.displayinset.top中 recordTopInsetLocked(mLogicalDisplayMapper.getDisplayLocked(Display.DEFAULT_DISPLAY)); updateSettingsLocked(); updateUserDisabledHdrTypesFromSettingsLocked(); } mDisplayModeDirector.setDesiredDisplayModeSpecsListener(new DesiredDisplayModeSpecsObserver()); mDisplayModeDirector.start(mSensorManager); //注册除了物理屏幕适配器、虚拟屏幕适配器以外的其他屏幕适配器 mHandler.sendEmptyMessage(MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS); mSettingsObserver = new SettingsObserver(); mBrightnessSynchronizer.startSynchronizing(); } private void recordTopInsetLocked(@Nullable LogicalDisplay d) { if (!mSystemReady || d == null) { return; } int topInset = d.getInsets().top; if (topInset == mDefaultDisplayTopInset) { return; } mDefaultDisplayTopInset = topInset; //将默认Display的top inset存储到persist.sys.displayinset.top中 SystemProperties.set(PROP_DEFAULT_DISPLAY_TOP_INSET, Integer.toString(topInset)); } private final class DisplayManagerHandler extends Handler { @Override public void handleMessage(Message msg) { switch (msg.what) { case MSG_REGISTER_DEFAULT_DISPLAY_ADAPTERS: //注册默认的屏幕适配器 registerDefaultDisplayAdapters(); break; case MSG_REGISTER_ADDITIONAL_DISPLAY_ADAPTERS: //注册额外的屏幕设备适配器 registerAdditionalDisplayAdapters(); break; ...代码省略... } } } //注册额外的屏幕适配器对象 private void registerAdditionalDisplayAdapters() { synchronized (mSyncRoot) { if (shouldRegisterNonEssentialDisplayAdaptersLocked()) { registerOverlayDisplayAdapterLocked();//注册模拟辅助设备屏幕适配器 registerWifiDisplayAdapterLocked();//注册WIFI屏幕适配器 } } } private void registerOverlayDisplayAdapterLocked() { registerDisplayAdapterLocked(new OverlayDisplayAdapter( mSyncRoot, mContext, mHandler, mDisplayDeviceRepo, mUiHandler)); } private void registerWifiDisplayAdapterLocked() { if (mContext.getResources().getBoolean( com.android.internal.R.bool.config_enableWifiDisplay) || SystemProperties.getInt(FORCE_WIFI_DISPLAY_ENABLE, -1) == 1) { mWifiDisplayAdapter = new WifiDisplayAdapter( mSyncRoot, mContext, mHandler, mDisplayDeviceRepo, mPersistentDataStore); registerDisplayAdapterLocked(mWifiDisplayAdapter); } } private void registerDisplayAdapterLocked(DisplayAdapter adapter) { //将适配器对面添加到适配器集合中 mDisplayAdapters.add(adapter); adapter.registerLocked(); } }
此方法该会通过mHandler发起注册其他DeviceAdapter的操作,最终完成四种DisplayAdapter的注册。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。