android display 笔记(八)WMS与SurfaceFlinger交互

在Android系统中由WindowManagerService也就是WMS承担窗口位置的管理。

WMS

WMS是一个系统服务,运行在system server进程,系统启动便开始启动该服务

java 复制代码
/frameworks/base/services/java/com/android/server/SystemServer.java


  // Start services.
938          try {
939              t.traceBegin("StartServices");
940              startBootstrapServices(t);
941              startCoreServices(t);
942              startOtherServices(t);
943              startApexServices(t);
944              // Only update the timeout after starting all the services so that we use
945              // the default timeout to start system server.
946              updateWatchdogTimeout(t);


在这里面启动相关服务, 在startOtherServices(t)启动 WMS服务。
  private void startOtherServices(@NonNull TimingsTraceAndSlog t) {
1425          t.traceBegin("startOtherServices");
1426          mSystemServiceManager.updateOtherServicesStartIndex();

       t.traceBegin("StartWindowManagerService");
1604              // WMS needs sensor service ready
1605              mSystemServiceManager.startBootPhase(t, SystemService.PHASE_WAIT_FOR_SENSOR_SERVICE);
1606              wm = WindowManagerService.main(context, inputManager, !mFirstBoot,
1607                      new PhoneWindowManager(), mActivityManagerService.mActivityTaskManager);
1608              ServiceManager.addService(Context.WINDOW_SERVICE, wm, /* allowIsolated= */ false,
1609                      DUMP_FLAG_PRIORITY_CRITICAL | DUMP_FLAG_PROTO);
1610              ServiceManager.addService(Context.INPUT_SERVICE, inputManager,
1611                      /* allowIsolated= */ false, DUMP_FLAG_PRIORITY_CRITICAL);
1612              t.traceEnd();

上述代码可以看到,创建了wms对象并添加到服务管理进程中。

与wms交互

应用进程通过WindowManagerGlobal与WMS建立通信连接。

java 复制代码
7      @UnsupportedAppUsage
168      public static IWindowManager getWindowManagerService() {
169          synchronized (WindowManagerGlobal.class) {
170              if (sWindowManagerService == null) {
171                  sWindowManagerService = IWindowManager.Stub.asInterface(
172                          ServiceManager.getService("window"));
173                  try {
174                      if (sWindowManagerService != null) {
175                          ValueAnimator.setDurationScale(
176                                  sWindowManagerService.getCurrentAnimatorScale());
177                          sUseBLASTAdapter = sWindowManagerService.useBLAST();
178                      }
179                  } catch (RemoteException e) {
180                      throw e.rethrowFromSystemServer();
181                  }
182              }
183              return sWindowManagerService;
184          }
185      }

      @UnsupportedAppUsage
188      public static IWindowSession getWindowSession() {
189          synchronized (WindowManagerGlobal.class) {
190              if (sWindowSession == null) {
191                  try {
192                      // Emulate the legacy behavior.  The global instance of InputMethodManager
193                      // was instantiated here.
194                      // TODO(b/116157766): Remove this hack after cleaning up @UnsupportedAppUsage
195                      InputMethodManager.ensureDefaultInstanceForDefaultDisplayIfNecessary();
196                      IWindowManager windowManager = getWindowManagerService();
197                      sWindowSession = windowManager.openSession(
198                              new IWindowSessionCallback.Stub() {
199                                  @Override
200                                  public void onAnimatorScaleChanged(float scale) {
201                                      ValueAnimator.setDurationScale(scale);
202                                  }
203                              });
204                  } catch (RemoteException e) {
205                      throw e.rethrowFromSystemServer();
206                  }
207              }
208              return sWindowSession;
209          }
210      }

通过getWindowManagerService,openSession方法获取到IWindowManager对象。应用进程通常用通过IWindowManager与WMS交互。

WMS与SurfaceFlinger交互

再上图可以看到,在system server中,通过WMS中的DisplayContent成员,创建DisplayContent对象时,也会闯将SurfaceSession对象。

然后SurfaceSeeion会通过JNI 调用nativeCreate创建SurfaceComposerClient对象,并把该对象的地址保存到mNativeClient中。

每创建一个SurfaceComposerClient对象,SurfaceFlinger进程都会创建一个Client对象与它对应。

总的来说,当WMS需要与Surfaceflinger进行交互的时候,会调用DisplayContent中的SurfaceSession取出SurfaceComposerClient对象的地址,然后在向Surfaceflinger发送请求。

相关推荐
柳岸风10 小时前
Android Studio Meerkat | 2024.3.1 Gradle Tasks不展示
android·ide·android studio
编程乐学10 小时前
安卓原创--基于 Android 开发的菜单管理系统
android
easy202010 小时前
Kaggle项目实践——Titanic: Machine Learning from Disaster
笔记·学习·机器学习
The_Second_Coming11 小时前
容器应用学习笔记:containerd 篇
运维·笔记·学习·容器·containerd
HainesFreeman12 小时前
书籍成长书籍文字#创业付费杂志《财新周刊》2025最新合集 更33期
笔记
whatever who cares12 小时前
android中ViewModel 和 onSaveInstanceState 的最佳使用方法
android
毕设源码-钟学长12 小时前
【开题答辩全过程】以 Android的传统中医诊断管理系统为例,包含答辩的问题和答案
android
脚踏实地,坚持不懈!12 小时前
Android,Jetpack Compose,坦克大战游戏案例Demo
android·游戏
yzpyzp13 小时前
kotlin的函数前面增加suspend关键字的作用
android·开发语言·kotlin
jiet_h13 小时前
Android Kotlin ObjectAnimator 和 ValueAnimator 全面解析
android·开发语言·kotlin