window对应用层来说来,是view跟display解耦,对framework来说,是用来display跟layer(即surfaceflinger)解耦,简单的理解:display ->layer/view<-window。
从显示的角度来看,并没有windows这个概念,只有layer图层,display显示屏的概念,window提供了一个桥梁来连接display和layer,因为安卓系统只允许应用处理自己的view关系,而且display并不属于应用可以管理的范围,于是就在activity的生命周期加了入window来桥接displayt和view,window决定了view的显示,具体window和display创建的流程如下:
一、window创建流程:
如上图,应用进程activity的生命周期函数里面按步骤创建window,view,关联view和window,会binder调到WMS的addwindow。
注:此处的WindowSession.addToDisplay 应为**com.android.server.wm**.Session
WMS, WindowToken 里的displaycontent即是Display。
addToDisplay 里会执行 WindowManagerService的addwindow。
@Override public int addToDisplay(IWindow window, WindowManager.LayoutParams attrs, int viewVisibility, int displayId, InsetsVisibilities requestedVisibilities, InputChannel outInputChannel, InsetsState outInsetsState, InsetsSourceControl[] outActiveControls) { return mService.addWindow(this, window, attrs, viewVisibility, displayId, UserHandle.getUserId(mUid), requestedVisibilities, outInputChannel, outInsetsState, outActiveControls); } @Override public int addToDisplayAsUser(IWindow window, WindowManager.LayoutParams attrs, int viewVisibility, int displayId, int userId, InsetsVisibilities requestedVisibilities, InputChannel outInputChannel, InsetsState outInsetsState, InsetsSourceControl[] outActiveControls) { return mService.addWindow(this, window, attrs, viewVisibility, displayId, userId, requestedVisibilities, outInputChannel, outInsetsState, outActiveControls); } @Override public int addToDisplayWithoutInputChannel(IWindow window, WindowManager.LayoutParams attrs, int viewVisibility, int displayId, InsetsState outInsetsState) { return mService.addWindow(this, window, attrs, viewVisibility, displayId, UserHandle.getUserId(mUid), mDummyRequestedVisibilities, null /* outInputChannel */, outInsetsState, mDummyControls); }
在addwindow中对应不同场景。
final boolean openInputChannels = (outInputChannel != null && (attrs.inputFeatures & INPUT_FEATURE_NO_INPUT_CHANNEL) == 0); if (openInputChannels) { win.openInputChannel(outInputChannel); } int userId = UserHandle.getUserId(session.mUid); if (requestUserId != userId) { try { mAmInternal.handleIncomingUser(callingPid, callingUid, requestUserId, false /*allowAll*/, ALLOW_NON_FULL, null, null); } catch (Exception exp) { ProtoLog.w(WM_ERROR, "Trying to add window with invalid user=%d", requestUserId); return WindowManagerGlobal.ADD_INVALID_USER; } // It's fine to use this userId userId = requestUserId; }
ViewRootImpl中requestLayout函数调用mWindowSession.relayout,最终调用WindowManagerservice 的relayoutWindow,这个函数会配置Display信息; WindowStateAnimator创建Surfacecontrol,通过SurfaceControl,设置Layerstack。即在触发view更新绘制的时候才会更新图层数据。
com.android.server.wm.WindowManagerService#relayoutWindow
if (shouldRelayout) { try { result = createSurfaceControl(outSurfaceControl, result, win, winAnimator); } catch (Exception e) { displayContent.getInputMonitor().updateInputWindowsLw(true /*force*/); ProtoLog.w(WM_ERROR, "Exception thrown when creating surface for client %s (%s). %s", client, win.mAttrs.getTitle(), e); Binder.restoreCallingIdentity(origId); return 0; } }
至此,一个Windows对应一个ViewRootImpl,用来管理应用进程的view的绘制生命周期,事件机制等等,后续的流程是layer会在surfaceflinger里面处理后,经由GPU或者硬件合成渲染。
添加评论