From bd0fbcd1f37f4e7e2e505cf5e5ede0fe52a3767c Mon Sep 17 00:00:00 2001 From: Garfield Tan Date: Fri, 30 Nov 2018 12:45:03 -0800 Subject: Keep instance of BinderInputWindow for the same token. Some code in InputDispatcher uses pointer value comparison to track if a BinderInputWindow is the same. Therefore just keep the initial instance and reuse that for every update. This is better than changing all pointer comparisons to token comparisons because it would be hard to make sure every comparison is changed. Later we could simply drop InputWindowHandle and use InputWindowInfo value instead. It would also be better if it could implement move constructor so that we can use move semantics. Bug: 120289807 Bug: 120463595 Bug: 120481017 Test: Verified that input focus stops constantly changing by inpecting logcat after enabling DEBUG_FOCUS in InputDispatcher.cpp. Freeform drag-resize works with a workaround to another race condition. Change-Id: I34bbf8c076373ef23494f858536c5188eff95abd --- include/input/InputWindow.h | 5 ++++ libs/input/InputWindow.cpp | 4 ++++ services/inputflinger/InputDispatcher.cpp | 38 ++++++++++++++++++++++++------- 3 files changed, 39 insertions(+), 8 deletions(-) diff --git a/include/input/InputWindow.h b/include/input/InputWindow.h index 8dd95cfb9a..2b8cc57c05 100644 --- a/include/input/InputWindow.h +++ b/include/input/InputWindow.h @@ -224,6 +224,11 @@ public: */ virtual bool updateInfo() = 0; + /** + * Updates from another input window handle. + */ + void updateFrom(const sp handle); + /** * Releases the channel used by the associated information when it is * no longer needed. diff --git a/libs/input/InputWindow.cpp b/libs/input/InputWindow.cpp index 556a005580..aa1371fdc9 100644 --- a/libs/input/InputWindow.cpp +++ b/libs/input/InputWindow.cpp @@ -162,4 +162,8 @@ sp InputWindowHandle::getToken() const { return mInfo.token; } +void InputWindowHandle::updateFrom(sp handle) { + mInfo = handle->mInfo; +} + } // namespace android diff --git a/services/inputflinger/InputDispatcher.cpp b/services/inputflinger/InputDispatcher.cpp index 12d91bbd68..a498fa154c 100644 --- a/services/inputflinger/InputDispatcher.cpp +++ b/services/inputflinger/InputDispatcher.cpp @@ -3077,22 +3077,44 @@ void InputDispatcher::setInputWindows(const Vector>& input // Remove all handles on a display if there are no windows left. mWindowHandlesByDisplay.erase(displayId); } else { - size_t numWindows = inputWindowHandles.size(); + // Since we compare the pointer of input window handles across window updates, we need + // to make sure the handle object for the same window stays unchanged across updates. + const Vector>& oldHandles = mWindowHandlesByDisplay[displayId]; + std::unordered_map, sp, IBinderHash> oldHandlesByTokens; + for (size_t i = 0; i < oldHandles.size(); i++) { + const sp& handle = oldHandles.itemAt(i); + oldHandlesByTokens[handle->getToken()] = handle; + } + + const size_t numWindows = inputWindowHandles.size(); + Vector> newHandles; for (size_t i = 0; i < numWindows; i++) { - const sp& windowHandle = inputWindowHandles.itemAt(i); - if (!windowHandle->updateInfo() || getInputChannelLocked(windowHandle->getToken()) == nullptr) { + const sp& handle = inputWindowHandles.itemAt(i); + if (!handle->updateInfo() || getInputChannelLocked(handle->getToken()) == nullptr) { ALOGE("Window handle %s has no registered input channel", - windowHandle->getName().c_str()); + handle->getName().c_str()); continue; } - if (windowHandle->getInfo()->displayId != displayId) { + if (handle->getInfo()->displayId != displayId) { ALOGE("Window %s updated by wrong display %d, should belong to display %d", - windowHandle->getName().c_str(), displayId, - windowHandle->getInfo()->displayId); + handle->getName().c_str(), displayId, + handle->getInfo()->displayId); continue; } + if (oldHandlesByTokens.find(handle->getToken()) != oldHandlesByTokens.end()) { + const sp oldHandle = + oldHandlesByTokens.at(handle->getToken()); + oldHandle->updateFrom(handle); + newHandles.push_back(oldHandle); + } else { + newHandles.push_back(handle); + } + } + + for (size_t i = 0; i < newHandles.size(); i++) { + const sp& windowHandle = newHandles.itemAt(i); if (windowHandle->getInfo()->hasFocus && windowHandle->getInfo()->visible) { newFocusedWindowHandle = windowHandle; } @@ -3102,7 +3124,7 @@ void InputDispatcher::setInputWindows(const Vector>& input } // Insert or replace - mWindowHandlesByDisplay[displayId] = inputWindowHandles; + mWindowHandlesByDisplay[displayId] = newHandles; } if (!foundHoveredWindow) { -- cgit v1.2.3-59-g8ed1b