InputDispatcher分发时间需要知道所有的窗口的信息, 而这些信息是由WMS更新到InputManager中的, 下面我们分析窗口信息更新的过程
InputWindow类描述了窗口的布局信息, 包括创建口的位置窗口的大小, 是否有焦点等,是窗口存在在输入系统中的形式
每个WindowState中都有一个InputWindowHandleWrapper 类
WindowState.java
616 final InputWindowHandleWrapper mInputWindowHandle;
而InputWindowHandleWrapper又有一个InputWindowHandle 对象, 除此之外,它还有一个变量为mChanged ,记录了InputWindowHandle是否发生了改变
39 class InputWindowHandleWrapper {
40
41 /** The wrapped handle should not be directly exposed to avoid untracked changes. */
42 private final @NonNull InputWindowHandle mHandle;
private boolean mChanged = true;
由于一个窗口有一个InputWindowHandleWrapper, 因此在WindowState初始化时就生成了一个InputWindowHandleWrapper的对象, 并且初始化了InputWindowHandleWrapper和InputWindowHandle的成员变量
1083 WindowState(WindowManagerService service, Session s, IWindow c, WindowToken token,
1084 WindowState parentWindow, int appOp, WindowManager.LayoutParams a, int viewVisibility,
1085 int ownerId, int showUserId, boolean ownerCanAddInternalSystemWindow,
1086 PowerManagerWrapper powerManagerWrapper) {
1087 super(service);
1106 mInputWindowHandle = new InputWindowHandleWrapper(new InputWindowHandle(
1107 mActivityRecord != null
1108 ? mActivityRecord.getInputApplicationHandle(false /* update */) : null,
1109 getDisplayId()));
1110 mInputWindowHandle.setFocusable(false);
1111 mInputWindowHandle.setOwnerPid(s.mPid);
1112 mInputWindowHandle.setOwnerUid(s.mUid);
1113 mInputWindowHandle.setName(getName());
1114 mInputWindowHandle.setPackageName(mAttrs.packageName);
1115 mInputWindowHandle.setLayoutParamsType(mAttrs.type);
1116 mInputWindowHandle.setTrustedOverlay(shouldWindowHandleBeTrusted(s));
47 InputWindowHandleWrapper(@NonNull InputWindowHandle handle) {
48 mHandle = handle;
49 }
50
传入的参数为新生成的InputWindowHandle
/frameworks/base/core/java/android/view/InputWindowHandle.java
171 public InputWindowHandle(InputApplicationHandle inputApplicationHandle, int displayId) {
172 this.inputApplicationHandle = inputApplicationHandle;
173 this.displayId = displayId;
174 }
通过调用updateInputWindowsLw更新
在WMS的addWinodw的最后会调用哪个此函数
1431 public int addWindow(Session session, IWindow client, LayoutParams attrs, int viewVisibility,
1432 int displayId, int requestUserId, @InsetsType int requestedVisibleTypes,
1433 InputChannel outInputChannel, InsetsState outInsetsState,
1434 InsetsSourceControl.Array outActiveControls, Rect outAttachedFrame,
1435 float[] outSizeCompatScale) {
1825 if (focusChanged) {
1826 displayContent.getInputMonitor().setInputFocusLw(displayContent.mCurrentFocus,
1827 false /*updateInputWindows*/);
1828 }
1829 displayContent.getInputMonitor().updateInputWindowsLw(false /*force*/);
InputMonitor.java
329 void updateInputWindowsLw(boolean force) {
330 if (!force && !mUpdateInputWindowsNeeded) {
331 return;
332 }
333 scheduleUpdateInputWindows();
334 }
336 private void scheduleUpdateInputWindows() {
337 if (mDisplayRemoved) {
338 return;
339 }
340
341 if (!mUpdateInputWindowsPending) {
342 mUpdateInputWindowsPending = true;
343 mHandler.post(mUpdateInputWindows);
344 }
345 }
118 private class UpdateInputWindows implements Runnable {
119 @Override
120 public void run() {
121 synchronized (mService.mGlobalLock) {
122 mUpdateInputWindowsPending = false;
123 mUpdateInputWindowsNeeded = false;
124
125 if (mDisplayRemoved) {
126 return;
127 }
128
129 // Populate the input window list with information about all of the windows that
130 // could potentially receive input.
131 // As an optimization, we could try to prune the list of windows but this turns
132 // out to be difficult because only the native code knows for sure which window
133 // currently has touch focus.
134
135 // If there's a drag in flight, provide a pseudo-window to catch drag input
136 final boolean inDrag = mService.mDragDropController.dragDropActiveLocked();
137
138 // Add all windows on the default display.
139 mUpdateInputForAllWindowsConsumer.updateInputWindows(inDrag);
140 }
141 }
142 }
143
144 private final UpdateInputWindows mUpdateInputWindows = new UpdateInputWindows();
调用139 mUpdateInputForAllWindowsConsumer.updateInputWindows(inDrag)更新
InputMonitor.java
565 private void updateInputWindows(boolean inDrag) {
566 Trace.traceBegin(TRACE_TAG_WINDOW_MANAGER, "updateInputWindows");
567
596 mDisplayContent.forAllWindows(this, true /* traverseTopToBottom */);
597 updateInputFocusRequest(mRecentsAnimationInputConsumer);
598
599 if (!mUpdateInputWindowsImmediately) {
600 mDisplayContent.getPendingTransaction().merge(mInputTransaction);
601 mDisplayContent.scheduleAnimation();
602 }
603
604 Trace.traceEnd(TRACE_TAG_WINDOW_MANAGER);
605 }
遍历每个窗口执行accept函数, 如果窗口hasSurface
拿到窗口的InputWindowHandleWrapper, 调用setInputWindowInfoIfNeeded
607 @Override
608 public void accept(WindowState w) {
609 final InputWindowHandleWrapper inputWindowHandle = w.mInputWindowHandle;
610 if (w.mInputChannelToken == null || w.mRemoved || !w.canReceiveTouchInput()) {
611 if (w.mWinAnimator.hasSurface()) {
612 // Make sure the input info can't receive input event. It may be omitted from
613 // occlusion detection depending on the type or if it's a trusted overlay.
614 populateOverlayInputInfo(inputWindowHandle, w);
615 setInputWindowInfoIfNeeded(mInputTransaction,
616 w.mWinAnimator.mSurfaceController.mSurfaceControl, inputWindowHandle);
617 return;
618 }
619 // Skip this window because it cannot possibly receive input.
620 return;
621 }
622
677 if (w.mWinAnimator.hasSurface()) {
678 populateInputWindowHandle(inputWindowHandle, w);
679 setInputWindowInfoIfNeeded(mInputTransaction,
680 w.mWinAnimator.mSurfaceController.mSurfaceControl, inputWindowHandle);
681 }
682 }
683 }
685 @VisibleForTesting
686 static void setInputWindowInfoIfNeeded(SurfaceControl.Transaction t, SurfaceControl sc,
687 InputWindowHandleWrapper inputWindowHandle) {
688 if (DEBUG_INPUT) {
689 Slog.d(TAG_WM, "Update InputWindowHandle: " + inputWindowHandle);
690 }
691 if (inputWindowHandle.isChanged()) {
692 inputWindowHandle.applyChangesToSurface(t, sc);
693 }
694 }
frameworks/base/services/core/java/com/android/server/wm/InputWindowHandleWrapper.java
63 void applyChangesToSurface(@NonNull SurfaceControl.Transaction t, @NonNull SurfaceControl sc) {
64 t.setInputWindowInfo(sc, mHandle);
65 mChanged = false;
66 }
将窗口信息设置到SurfaceControl
SurfaceControl.java
3015
3016 /**
3017 * @hide
3018 */
3019 public Transaction setInputWindowInfo(SurfaceControl sc, InputWindowHandle handle) {
3020 checkPreconditions(sc);
3021 nativeSetInputWindowInfo(mNativeObject, sc.mNativeObject, handle);
3022 return this;
3023 }
/frameworks/base/core/jni/android_view_SurfaceControl.cpp
804 static void nativeSetInputWindowInfo(JNIEnv* env, jclass clazz, jlong transactionObj,
805 jlong nativeObject, jobject inputWindow) {
806 auto transaction = reinterpret_cast(transactionObj);
807
808 sp handle = android_view_InputWindowHandle_getHandle(
809 env, inputWindow);
810 handle->updateInfo();
811
812 auto ctrl = reinterpret_cast(nativeObject);
813 transaction->setInputWindowInfo(ctrl, *handle->getInfo());
814 }
nativeSetInputWindowInfo分为两步,
将信息设置到handle的info中
将*handle->getInfo()的信息设置到layer_state_t
可以看到通过调用native的updateInfo把数据设置到mInfo的变量中
/frameworks/base/core/jni/android_hardware_input_InputWindowHandle.cpp
108
109 bool NativeInputWindowHandle::updateInfo() {
110 JNIEnv* env = AndroidRuntime::getJNIEnv();
111 jobject obj = env->NewLocalRef(mObjWeak);
112 if (!obj) {
113 releaseChannel();
114 return false;
115 }
116
117 mInfo.touchableRegion.clear();
118
119 jobject tokenObj = env->GetObjectField(obj, gInputWindowHandleClassInfo.token);
120 if (tokenObj) {
121 mInfo.token = ibinderForJavaObject(env, tokenObj);
122 env->DeleteLocalRef(tokenObj);
123 } else {
124 mInfo.token.clear();
125 }
126
127 mInfo.name = getStringField(env, obj, gInputWindowHandleClassInfo.name, "");
128
129 mInfo.dispatchingTimeout = std::chrono::milliseconds(
130 env->GetLongField(obj, gInputWindowHandleClassInfo.dispatchingTimeoutMillis));
131 mInfo.frameLeft = env->GetIntField(obj,
132 gInputWindowHandleClassInfo.frameLeft);
133 mInfo.frameTop = env->GetIntField(obj,
134 gInputWindowHandleClassInfo.frameTop);
135 mInfo.frameRight = env->GetIntField(obj,
136 gInputWindowHandleClassInfo.frameRight);
137 mInfo.frameBottom = env->GetIntField(obj,
138 gInputWindowHandleClassInfo.frameBottom);
139 mInfo.surfaceInset = env->GetIntField(obj,
140 gInputWindowHandleClassInfo.surfaceInset);
141 mInfo.globalScaleFactor = env->GetFloatField(obj,
142 gInputWindowHandleClassInfo.scaleFactor);
143
144 jobject regionObj = env->GetObjectField(obj,
145 gInputWindowHandleClassInfo.touchableRegion);
146 if (regionObj) {
147 for (graphics::RegionIterator it(env, regionObj); !it.isDone(); it.next()) {
148 ARect rect = it.getRect();
149 mInfo.addTouchableRegion(Rect(rect.left, rect.top, rect.right, rect.bottom));
150 }
151 env->DeleteLocalRef(regionObj);
152 }
153
154 const auto flags = ftl::Flags(
155 env->GetIntField(obj, gInputWindowHandleClassInfo.layoutParamsFlags));
156 const auto type = static_cast(
157 env->GetIntField(obj, gInputWindowHandleClassInfo.layoutParamsType));
158 mInfo.layoutParamsFlags = flags;
159 mInfo.layoutParamsType = type;
160
161 mInfo.inputConfig = static_cast(
162 env->GetIntField(obj, gInputWindowHandleClassInfo.inputConfig));
163
164 mInfo.touchOcclusionMode = static_cast(
165 env->GetIntField(obj, gInputWindowHandleClassInfo.touchOcclusionMode));
166 mInfo.ownerPid = env->GetIntField(obj,
167 gInputWindowHandleClassInfo.ownerPid);
168 mInfo.ownerUid = env->GetIntField(obj,
169 gInputWindowHandleClassInfo.ownerUid);
170 mInfo.packageName = getStringField(env, obj, gInputWindowHandleClassInfo.packageName, "");
171 mInfo.displayId = env->GetIntField(obj,
172 gInputWindowHandleClassInfo.displayId);
173
174 jobject inputApplicationHandleObj = env->GetObjectField(obj,
175 gInputWindowHandleClassInfo.inputApplicationHandle);
176 if (inputApplicationHandleObj) {
177 std::shared_ptr inputApplicationHandle =
178 android_view_InputApplicationHandle_getHandle(env, inputApplicationHandleObj);
179 if (inputApplicationHandle != nullptr) {
180 inputApplicationHandle->updateInfo();
181 mInfo.applicationInfo = *(inputApplicationHandle->getInfo());
182 }
183 env->DeleteLocalRef(inputApplicationHandleObj);
184 }
185
186 mInfo.replaceTouchableRegionWithCrop = env->GetBooleanField(obj,
187 gInputWindowHandleClassInfo.replaceTouchableRegionWithCrop);
188
189 jobject weakSurfaceCtrl = env->GetObjectField(obj,
190 gInputWindowHandleClassInfo.touchableRegionSurfaceControl.ctrl);
191 bool touchableRegionCropHandleSet = false;
192 if (weakSurfaceCtrl) {
193 // Promote java weak reference.
194 jobject strongSurfaceCtrl = env->CallObjectMethod(weakSurfaceCtrl,
195 gInputWindowHandleClassInfo.touchableRegionSurfaceControl.get);
196 if (strongSurfaceCtrl) {
197 jlong mNativeObject = env->GetLongField(strongSurfaceCtrl,
198 gInputWindowHandleClassInfo.touchableRegionSurfaceControl.mNativeObject);
199 if (mNativeObject) {
200 auto ctrl = reinterpret_cast(mNativeObject);
201 mInfo.touchableRegionCropHandle = ctrl->getHandle();
202 touchableRegionCropHandleSet = true;
203 }
204 env->DeleteLocalRef(strongSurfaceCtrl);
205 }
206 env->DeleteLocalRef(weakSurfaceCtrl);
207 }
208 if (!touchableRegionCropHandleSet) {
209 mInfo.touchableRegionCropHandle.clear();
210 }
211
212 jobject windowTokenObj = env->GetObjectField(obj, gInputWindowHandleClassInfo.windowToken);
213 if (windowTokenObj) {
214 mInfo.windowToken = ibinderForJavaObject(env, windowTokenObj);
215 env->DeleteLocalRef(windowTokenObj);
216 } else {
217 mInfo.windowToken.clear();
218 }
219
220 ScopedLocalRef
221 focusTransferTargetObj(env,
222 env->GetObjectField(obj,
223 gInputWindowHandleClassInfo
224 .focusTransferTarget));
225 if (focusTransferTargetObj.get()) {
226 mInfo.focusTransferTarget = ibinderForJavaObject(env, focusTransferTargetObj.get());
227 } else {
228 mInfo.focusTransferTarget.clear();
229 }
230
231 env->DeleteLocalRef(obj);
232 return true;
233 }
234
/frameworks/native/libs/gui/SurfaceComposerClient.cpp
1941 SurfaceComposerClient::Transaction& SurfaceComposerClient::Transaction::setInputWindowInfo(
1942 const sp& sc, const WindowInfo& info) {
1943 layer_state_t* s = getLayerState(sc);
1944 if (!s) {
1945 mStatus = BAD_INDEX;
1946 return *this;
1947 }
1948 s->windowInfoHandle = new WindowInfoHandle(info);
1949 s->what |= layer_state_t::eInputInfoChanged;
1950 return *this;
1951 }
SurfaceControl.java
2752 public void apply() {
2753 apply(false);
2754 }
2784 public void apply(boolean sync) {
2785 applyResizedSurfaces();
2786 notifyReparentedSurfaces();
2787 nativeApplyTransaction(mNativeObject, sync);
2788 }
/frameworks/base/core/jni/android_view_SurfaceControl.cpp
464 static void nativeApplyTransaction(JNIEnv* env, jclass clazz, jlong transactionObj, jboolean sync) {
465 auto transaction = reinterpret_cast(transactionObj);
466 transaction->apply(sync);
467 }
/frameworks/native/libs/gui/SurfaceComposerClient.cpp
1151 status_t SurfaceComposerClient::Transaction::apply(bool synchronous, bool oneWay) {
;
1224
1225 sp sf(ComposerService::getComposerService());
1226 sf->setTransactionState(mFrameTimelineInfo, composerStates, displayStates, flags, applyToken,
1227 mInputWindowCommands, mDesiredPresentTime, mIsAutoTimestamp,
1228 mUncacheBuffers, hasListenerCallbacks, listenerCallbacks, mId,
1229 mMergedTransactionIds);
1230 mId = generateId();
1231
1232 // Clear the current states and flags
1233 clear();
1234
1235 if (synchronous) {
1236 syncCallback->wait();
1237 }
1238
1239 mStatus = NO_ERROR;
1240 return NO_ERROR;
1241 }
通过调用ComposerService::getComposerService() 获取SurfaceFlinger服务
/frameworks/native/libs/gui/SurfaceComposerClient.cpp
96 bool ComposerService::connectLocked() {
97 const String16 name("SurfaceFlinger");
98 mComposerService = waitForService(name);
99 if (mComposerService == nullptr) {
100 return false; // fatal error or permission problem
101 }
102
103 // Create the death listener.
104 class DeathObserver : public IBinder::DeathRecipient {
105 ComposerService& mComposerService;
106 virtual void binderDied(const wp& who) {
107 ALOGW("ComposerService remote (surfaceflinger) died [%p]",
108 who.unsafe_get());
109 mComposerService.composerServiceDied();
110 }
111 public:
112 explicit DeathObserver(ComposerService& mgr) : mComposerService(mgr) { }
113 };
114
115 mDeathObserver = new DeathObserver(*const_cast(this));
116 IInterface::asBinder(mComposerService)->linkToDeath(mDeathObserver);
117 return true;
118 }
然后来到SurfaceFlinger中调用sf->setTransactionState
会向SurfaceFlinger提交一个事务并放入mTransactionQueue; mTransactionHandler.queueTransaction(std::move(state));
/frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
4503 status_t SurfaceFlinger::setTransactionState(
4504 const FrameTimelineInfo& frameTimelineInfo, Vector& states,
4505 const Vector& displays, uint32_t flags, const sp& applyToken,
4506 InputWindowCommands inputWindowCommands, int64_t desiredPresentTime, bool isAutoTimestamp,
4507 const std::vector& uncacheBuffers, bool hasListenerCallbacks,
4508 const std::vector& listenerCallbacks, uint64_t transactionId,
4509 const std::vector& mergedTransactionIds) {
4605 const auto schedule = [](uint32_t flags) {
4606 if (flags & eEarlyWakeupEnd) return TransactionSchedule::EarlyEnd;
4607 if (flags & eEarlyWakeupStart) return TransactionSchedule::EarlyStart;
4608 return TransactionSchedule::Late;
4609 }(state.flags);
4610
4611 const auto frameHint = state.isFrameActive() ? FrameHint::kActive : FrameHint::kNone;
4612 mTransactionHandler.queueTransaction(std::move(state));
4613 setTransactionFlags(eTransactionFlushNeeded, schedule, applyToken, frameHint);
4614 return NO_ERROR;
4615 }
/frameworks/native/services/surfaceflinger/FrontEnd/TransactionHandler.cpp
30 void TransactionHandler::queueTransaction(TransactionState&& state) {
31 mLocklessTransactionQueue.push(std::move(state));
32 mPendingTransactionCount.fetch_add(1);
33 ATRACE_INT("TransactionQueue", static_cast(mPendingTransactionCount.load()));
34 }
4257
4258 void SurfaceFlinger::setTransactionFlags(uint32_t mask, TransactionSchedule schedule,
4259 const sp& applyToken, FrameHint frameHint) {
4260 mScheduler->modulateVsync({}, &VsyncModulator::setTransactionSchedule, schedule, applyToken);
4261 uint32_t transactionFlags = mTransactionFlags.fetch_or(mask);
4262 ATRACE_INT("mTransactionFlags", transactionFlags);
4263
4264 if (const bool scheduled = transactionFlags & mask; !scheduled) {
4265 scheduleCommit(frameHint);
4266 } else if (frameHint == FrameHint::kActive) {
4267 // Even if the next frame is already scheduled, we should reset the idle timer
4268 // as a new activity just happened.
4269 mScheduler->resetIdleTimer();
4270 }
4271 }
2049 void SurfaceFlinger::scheduleComposite(FrameHint hint) {
2050 mMustComposite = true;
2051 scheduleCommit(hint);
2052 }
2041 void SurfaceFlinger::scheduleCommit(FrameHint hint) {
2042 if (hint == FrameHint::kActive) {
2043 mScheduler->resetIdleTimer();
2044 }
2045 mPowerAdvisor->notifyDisplayUpdateImminentAndCpuReset();
2046 mScheduler->scheduleFrame();
2047 }
4404 // For tests only
4405 bool SurfaceFlinger::flushTransactionQueues(VsyncId vsyncId) {
4406 std::vector transactions = mTransactionHandler.flushTransactions();
4407 return applyTransactions(transactions, vsyncId);
4408 }
4410 bool SurfaceFlinger::applyTransactions(std::vector& transactions,
4411 VsyncId vsyncId) {
4412 Mutex::Autolock lock(mStateLock);
4413 return applyTransactionsLocked(transactions, vsyncId);
4414 }
4415
4416 bool SurfaceFlinger::applyTransactionsLocked(std::vector& transactions,
4417 VsyncId vsyncId) {
4418 bool needsTraversal = false;
4419 // Now apply all transactions.
4420 for (auto& transaction : transactions) {
4421 needsTraversal |=
4422 applyTransactionState(transaction.frameTimelineInfo, transaction.states,
4423 transaction.displays, transaction.flags,
4424 transaction.inputWindowCommands,
4425 transaction.desiredPresentTime, transaction.isAutoTimestamp,
4426 std::move(transaction.uncacheBufferIds), transaction.postTime,
4427 transaction.hasListenerCallbacks,
4428 transaction.listenerCallbacks, transaction.originPid,
4429 transaction.originUid, transaction.id);
4430 }
4431 return needsTraversal;
4432 }
4617 bool SurfaceFlinger::applyTransactionState(const FrameTimelineInfo& frameTimelineInfo,
4618 std::vector& states,
4619 Vector& displays, uint32_t flags,
4620 const InputWindowCommands& inputWindowCommands,
4621 const int64_t desiredPresentTime, bool isAutoTimestamp,
4622 const std::vector& uncacheBufferIds,
4623 const int64_t postTime, bool hasListenerCallbacks,
4624 const std::vector& listenerCallbacks,
4625 int originPid, int originUid, uint64_t transactionId) {
4626 uint32_t transactionFlags = 0;
4627 if (!mLayerLifecycleManagerEnabled) {
4641 for (auto& resolvedState : states) {
4642 if (mLegacyFrontEndEnabled) {
4643 clientStateFlags |=
4644 setClientStateLocked(frameTimelineInfo, resolvedState, desiredPresentTime,
4645 isAutoTimestamp, postTime, transactionId);
4646
4799 uint32_t SurfaceFlinger::setClientStateLocked(const FrameTimelineInfo& frameTimelineInfo,
4800 ResolvedComposerState& composerState,
4801 int64_t desiredPresentTime, bool isAutoTimestamp,
4802 int64_t postTime, uint64_t transactionId) {
调用updateInputFlinger
首先填充windowInfos, 通过调用buildWindowInfos填充
然后调用windowInfosChanged, 将信息通知inputDispatcher
然后调用inputFlinger->setFocusedWindow(focusReques 跟心焦点信息
/frameworks/native/services/surfaceflinger/SurfaceFlinger.cpp
2374 bool SurfaceFlinger::commit(TimePoint frameTime, VsyncId vsyncId, TimePoint expectedVsyncTime)
2375 FTL_FAKE_GUARD(kMainThreadContext) {
updateInputFlinger(vsyncId, frameTime);
}
3746 void SurfaceFlinger::updateInputFlinger(VsyncId vsyncId, TimePoint frameTime) {
3747 if (!mInputFlinger || (!mUpdateInputInfo && mInputWindowCommands.empty())) {
3748 return;
3749 }
3750 ATRACE_CALL();
3751
3752 std::vector windowInfos;
3753 std::vector displayInfos;
3754 bool updateWindowInfo = false;
3755 if (mUpdateInputInfo) {
3756 mUpdateInputInfo = false;
3757 updateWindowInfo = true;
3758 buildWindowInfos(windowInfos, displayInfos);
3759 }
3760
3772
3773 BackgroundExecutor::getInstance().sendCallbacks({[updateWindowInfo,
3774 windowInfos = std::move(windowInfos),
3775 displayInfos = std::move(displayInfos),
3776 inputWindowCommands =
3777 std::move(mInputWindowCommands),
3778 inputFlinger = mInputFlinger, this,
3779 visibleWindowsChanged, vsyncId, frameTime]() {
3780 ATRACE_NAME("BackgroundExecutor::updateInputFlinger");
3781 if (updateWindowInfo) {
3782 mWindowInfosListenerInvoker
3783 ->windowInfosChanged(gui::WindowInfosUpdate{std::move(windowInfos),
3784 std::move(displayInfos),
3785 vsyncId.value, frameTime.ns()},
3786 std::move(
3787 inputWindowCommands.windowInfosReportedListeners),
3788 /* forceImmediateCall= */ visibleWindowsChanged ||
3789 !inputWindowCommands.focusRequests.empty());
3790 } else {
3791 // If there are listeners but no changes to input windows, call the listeners
3792 // immediately.
3793 for (const auto& listener : inputWindowCommands.windowInfosReportedListeners) {
3794 if (IInterface::asBinder(listener)->isBinderAlive()) {
3795 listener->onWindowInfosReported();
3796 }
3797 }
3798 }
3799 for (const auto& focusRequest : inputWindowCommands.focusRequests) {
3800 inputFlinger->setFocusedWindow(focusRequest);
3801 }
3802 }});
3803
3804 mInputWindowCommands.clear();
3805 }
outWindowInfos.push_back(layer->fillInputInfo(opt.value_or(Layer::InputDisplayArgs{})));
根据layer填满outWindowInfos
3836 void SurfaceFlinger::buildWindowInfos(std::vector& outWindowInfos,
3837 std::vector& outDisplayInfos) {
3838 static size_t sNumWindowInfos = 0;
3839 outWindowInfos.reserve(sNumWindowInfos);
3840 sNumWindowInfos = 0;
3841
3842 if (mLayerLifecycleManagerEnabled) {
3843 mLayerSnapshotBuilder.forEachInputSnapshot(
3844 [&outWindowInfos](const frontend::LayerSnapshot& snapshot) {
3845 outWindowInfos.push_back(snapshot.inputInfo);
3846 });
3847 } else {
3848 mDrawingState.traverseInReverseZOrder([&](Layer* layer) {
3849 if (!layer->needsInputInfo()) return;
3850 const auto opt =
3851 mFrontEndDisplayInfos.get(layer->getLayerStack())
3852 .transform([](const frontend::DisplayInfo& info) {
3853 return Layer::InputDisplayArgs{&info.transform, info.isSecure};
3854 });
3855
3856 outWindowInfos.push_back(layer->fillInputInfo(opt.value_or(Layer::InputDisplayArgs{})));
3857 });
3858 }
3859
3860 sNumWindowInfos = outWindowInfos.size();
3861
3862 outDisplayInfos.reserve(mFrontEndDisplayInfos.size());
3863 for (const auto& [_, info] : mFrontEndDisplayInfos) {
3864 outDisplayInfos.push_back(info.info);
3865 }
3866 }
通知信息变化
WindowInfosListenerInvoker.cpp
84 void WindowInfosListenerInvoker::windowInfosChanged(
85 gui::WindowInfosUpdate update, WindowInfosReportedListenerSet reportedListeners,
86 bool forceImmediateCall) {
87 if (!mDelayInfo) {
88 mDelayInfo = DelayInfo{
89 .vsyncId = update.vsyncId,
90 .frameTime = update.timestamp,
91 };
92 }
93
94 // If there are unacked messages and this isn't a forced call, then return immediately.
95 // If a forced window infos change doesn't happen first, the update will be sent after
96 // the WindowInfosReportedListeners are called. If a forced window infos change happens or
97 // if there are subsequent delayed messages before this update is sent, then this message
98 // will be dropped and the listeners will only be called with the latest info. This is done
99 // to reduce the amount of binder memory used.
115
116 reportedListeners.merge(mReportedListeners);
117 mReportedListeners.clear();
118
119 // Update mUnackedState to include the message we're about to send
120 auto [it, _] = mUnackedState.try_emplace(update.vsyncId,
121 UnackedState{.reportedListeners =
122 std::move(reportedListeners)});
123 auto& unackedState = it->second;
124 for (auto& pair : mWindowInfosListeners) {
125 int64_t listenerId = pair.second.first;
126 unackedState.unackedListenerIds.push_back(listenerId);
127 }
128
129 mDelayInfo.reset();
130 updateMaxSendDelay();
131
132 // Call the listeners
133 for (auto& pair : mWindowInfosListeners) {
134 auto& [listenerId, listener] = pair.second;
135 auto status = listener->onWindowInfosChanged(update);
136 if (!status.isOk()) {
137 ackWindowInfosReceived(update.vsyncId, listenerId);
138 }
139 }
140 }
这里的listener就是InputDispacher
WindowInfosListenerReporter.cpp
91 binder::Status WindowInfosListenerReporter::onWindowInfosChanged(
92 const gui::WindowInfosUpdate& update) {
93 std::unordered_set, gui::SpHash>
94 windowInfosListeners;
95
96 {
97 std::scoped_lock lock(mListenersMutex);
98 for (auto listener : mWindowInfosListeners) {
99 windowInfosListeners.insert(listener);
100 }
101
102 mLastWindowInfos = update.windowInfos;
103 mLastDisplayInfos = update.displayInfos;
104 }
105
106 for (auto listener : windowInfosListeners) {
107 listener->onWindowInfosChanged(update);
108 }
109
110 mWindowInfosPublisher->ackWindowInfosReceived(update.vsyncId, mListenerId);
111
112 return binder::Status::ok();
113 }
InputDispatcher在初始化时就会去向SurfaceComposerClient注册WindowInfosListener,便于后面在窗口信息发生变化时InputDispatcher能够同步接收到更新后的窗口列表信息。注册listener的相关代码流程为:
SurfaceComposerClient.addwindowInfosListener->WindowInfosListenerReporter.addwindowInfosListener
->SurfaceFlinger.addWindowInfosListener(向SurfaceFlinger注册WindowInfosListenerReporter)
->WindowInfosListenerInvoker.addWindowInfosListener。
可以看到,InputDispatcher其实是通过SurfaceComposerClient最终向WindowInfosListenerReporter去注册WindowInfosListener类型的listener,而WindowInfosListenerReporter会向SurfaceFlinger注册自己。
当窗口信息变化时,Surfaceflinger会回调前面所注册listener的onWindowInfosChanged方法并最终通知到InputDispatcher,相关代码流程如下:SurfaceFlinger.updateInputFlinger-> WindowInfosListenerInvoker.windowInfosChanged
->WindowInfosListenerReporter.onWindowInfosChanged
->WindowInfosListener.onWindowInfosChanged->InputDispatcher.DispatcherwindowListener.onWindowInfosChanged
->InputDispatcher.onWindowInfosChanged->setInputwindowsLocked
InputDispatcher.cpp
6739 void InputDispatcher::DispatcherWindowListener::onWindowInfosChanged(
6740 const gui::WindowInfosUpdate& update) {
6741 mDispatcher.onWindowInfosChanged(update);
6742 }
6685 void InputDispatcher::onWindowInfosChanged(const gui::WindowInfosUpdate& update) {
6686 // The listener sends the windows as a flattened array. Separate the windows by display for
6687 // more convenient parsing.
6688 std::unordered_map>> handlesPerDisplay;
6689 for (const auto& info : update.windowInfos) {
6690 handlesPerDisplay.emplace(info.displayId, std::vector>());
6691 handlesPerDisplay[info.displayId].push_back(sp::make(info));
6692 }
6693
6694 { // acquire lock
6695 std::scoped_lock _l(mLock);
6696
6697 // Ensure that we have an entry created for all existing displays so that if a displayId has
6698 // no windows, we can tell that the windows were removed from the display.
6699 for (const auto& [displayId, _] : mWindowHandlesByDisplay) {
6700 handlesPerDisplay[displayId];
6701 }
6702
6703 mDisplayInfos.clear();
6704 for (const auto& displayInfo : update.displayInfos) {
6705 mDisplayInfos.emplace(displayInfo.displayId, displayInfo);
6706 }
6707
6708 for (const auto& [displayId, handles] : handlesPerDisplay) {
6709 setInputWindowsLocked(handles, displayId);
6710 }
6711
6712 if (update.vsyncId < mWindowInfosVsyncId) {
6713 ALOGE("Received out of order window infos update. Last update vsync id: %" PRId64
6714 ", current update vsync id: %" PRId64,
6715 mWindowInfosVsyncId, update.vsyncId);
6716 }
6717 mWindowInfosVsyncId = update.vsyncId;
6718 }
6719 // Wake up poll loop since it may need to make new input dispatching choices.
6720 mLooper->wake();
6721 }
到此为止, inputwindow的信息设置到了InputManagerService中了