Skip to content

Commit 6cd42cb

Browse files
Arpit SinghAndroid (Google) Code Review
authored andcommitted
Merge changes I65b2b48d,I0d41ba77 into main
* changes: [20/n Dispatcher refactor] Allow access to all TouchedWindowHandles [19/n Dispatcher refactor] Remove findTouchStateWindowAndDisplay
2 parents 2766306 + 071afa0 commit 6cd42cb

2 files changed

Lines changed: 60 additions & 51 deletions

File tree

services/inputflinger/dispatcher/InputDispatcher.cpp

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4072,13 +4072,17 @@ void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked(
40724072
// Generate cancellations for touched windows first. This is to avoid generating cancellations
40734073
// through a non-touched window if there are more than one window for an input channel.
40744074
if (cancelPointers) {
4075-
for (const auto& [displayId, touchState] : mTouchStates.mTouchStatesByDisplay) {
4076-
if (options.displayId.has_value() && options.displayId != displayId) {
4077-
continue;
4078-
}
4079-
for (const auto& touchedWindow : touchState.windows) {
4080-
synthesizeCancelationEventsForWindowLocked(touchedWindow.windowHandle, options);
4081-
}
4075+
if (options.displayId.has_value()) {
4076+
mTouchStates.forAllTouchedWindowsOnDisplay(
4077+
options.displayId.value(), [&](const sp<gui::WindowInfoHandle>& windowHandle) {
4078+
base::ScopedLockAssertion assumeLocked(mLock);
4079+
synthesizeCancelationEventsForWindowLocked(windowHandle, options);
4080+
});
4081+
} else {
4082+
mTouchStates.forAllTouchedWindows([&](const sp<gui::WindowInfoHandle>& windowHandle) {
4083+
base::ScopedLockAssertion assumeLocked(mLock);
4084+
synthesizeCancelationEventsForWindowLocked(windowHandle, options);
4085+
});
40824086
}
40834087
}
40844088
// Follow up by generating cancellations for all windows, because we don't explicitly track
@@ -4276,13 +4280,13 @@ void InputDispatcher::synthesizePointerDownEventsForConnectionLocked(
42764280
connection->getInputChannelName().c_str(), downEvents.size());
42774281
}
42784282

4279-
const auto [_, touchedWindowState, displayId] =
4280-
findTouchStateWindowAndDisplay(connection->getToken(),
4281-
mTouchStates.mTouchStatesByDisplay);
4282-
if (touchedWindowState == nullptr) {
4283+
auto touchedWindowHandleAndDisplay =
4284+
mTouchStates.findTouchedWindowHandleAndDisplay(connection->getToken());
4285+
if (!touchedWindowHandleAndDisplay.has_value()) {
42834286
LOG(FATAL) << __func__ << ": Touch state is out of sync: No touched window for token";
42844287
}
4285-
const auto& windowHandle = touchedWindowState->windowHandle;
4288+
4289+
const auto [windowHandle, displayId] = touchedWindowHandleAndDisplay.value();
42864290

42874291
const bool wasEmpty = connection->outboundQueue.empty();
42884292
for (std::unique_ptr<EventEntry>& downEventEntry : downEvents) {
@@ -5798,34 +5802,6 @@ void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) {
57985802
mWindowInfos.setMaximumObscuringOpacityForTouch(opacity);
57995803
}
58005804

5801-
std::tuple<const TouchState*, const TouchedWindow*, ui::LogicalDisplayId>
5802-
InputDispatcher::findTouchStateWindowAndDisplay(
5803-
const sp<IBinder>& token,
5804-
const std::unordered_map<ui::LogicalDisplayId, TouchState>& touchStatesByDisplay) {
5805-
for (auto& [displayId, state] : touchStatesByDisplay) {
5806-
for (const TouchedWindow& w : state.windows) {
5807-
if (w.windowHandle->getToken() == token) {
5808-
return std::make_tuple(&state, &w, displayId);
5809-
}
5810-
}
5811-
}
5812-
return std::make_tuple(nullptr, nullptr, ui::LogicalDisplayId::DEFAULT);
5813-
}
5814-
5815-
std::tuple<TouchState*, TouchedWindow*, ui::LogicalDisplayId>
5816-
InputDispatcher::findTouchStateWindowAndDisplay(
5817-
const sp<IBinder>& token,
5818-
std::unordered_map<ui::LogicalDisplayId, TouchState>& touchStatesByDisplay) {
5819-
auto [constTouchState, constTouchedWindow, displayId] = InputDispatcher::
5820-
findTouchStateWindowAndDisplay(token,
5821-
const_cast<const std::unordered_map<ui::LogicalDisplayId,
5822-
TouchState>&>(
5823-
touchStatesByDisplay));
5824-
5825-
return std::make_tuple(const_cast<TouchState*>(constTouchState),
5826-
const_cast<TouchedWindow*>(constTouchedWindow), displayId);
5827-
}
5828-
58295805
bool InputDispatcher::transferTouchGesture(const sp<IBinder>& fromToken, const sp<IBinder>& toToken,
58305806
bool isDragDrop) {
58315807
if (fromToken == toToken) {
@@ -7492,6 +7468,40 @@ bool InputDispatcher::DispatcherTouchState::isPointerInWindow(const sp<android::
74927468
return false;
74937469
}
74947470

7471+
std::optional<std::tuple<const sp<gui::WindowInfoHandle>&, ui::LogicalDisplayId>>
7472+
InputDispatcher::DispatcherTouchState::findTouchedWindowHandleAndDisplay(
7473+
const sp<android::IBinder>& token) const {
7474+
for (const auto& [displayId, state] : mTouchStatesByDisplay) {
7475+
for (const TouchedWindow& w : state.windows) {
7476+
if (w.windowHandle->getToken() == token) {
7477+
return std::make_tuple(std::ref(w.windowHandle), displayId);
7478+
}
7479+
}
7480+
}
7481+
return std::nullopt;
7482+
}
7483+
7484+
void InputDispatcher::DispatcherTouchState::forAllTouchedWindows(
7485+
std::function<void(const sp<gui::WindowInfoHandle>&)> f) const {
7486+
for (const auto& [_, state] : mTouchStatesByDisplay) {
7487+
for (const TouchedWindow& window : state.windows) {
7488+
f(window.windowHandle);
7489+
}
7490+
}
7491+
}
7492+
7493+
void InputDispatcher::DispatcherTouchState::forAllTouchedWindowsOnDisplay(
7494+
ui::LogicalDisplayId displayId,
7495+
std::function<void(const sp<gui::WindowInfoHandle>&)> f) const {
7496+
const auto touchStateIt = mTouchStatesByDisplay.find(displayId);
7497+
if (touchStateIt == mTouchStatesByDisplay.end()) {
7498+
return;
7499+
}
7500+
for (const TouchedWindow& window : touchStateIt->second.windows) {
7501+
f(window.windowHandle);
7502+
}
7503+
}
7504+
74957505
std::string InputDispatcher::DispatcherTouchState::dump() const {
74967506
std::string dump;
74977507
if (!mTouchStatesByDisplay.empty()) {

services/inputflinger/dispatcher/InputDispatcher.h

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,16 @@ class InputDispatcher : public android::InputDispatcherInterface {
392392
bool isPointerInWindow(const sp<android::IBinder>& token, ui::LogicalDisplayId displayId,
393393
DeviceId deviceId, int32_t pointerId) const;
394394

395+
// Find touched windowHandle and display by token.
396+
std::optional<std::tuple<const sp<gui::WindowInfoHandle>&, ui::LogicalDisplayId>>
397+
findTouchedWindowHandleAndDisplay(const sp<IBinder>& token) const;
398+
399+
void forAllTouchedWindows(std::function<void(const sp<gui::WindowInfoHandle>&)> f) const;
400+
401+
void forAllTouchedWindowsOnDisplay(
402+
ui::LogicalDisplayId displayId,
403+
std::function<void(const sp<gui::WindowInfoHandle>&)> f) const;
404+
395405
std::string dump() const;
396406

397407
// Updates the touchState for display from WindowInfo,
@@ -855,17 +865,6 @@ class InputDispatcher : public android::InputDispatcherInterface {
855865
const std::shared_ptr<Connection>& connection, DispatchEntry* dispatchEntry,
856866
bool handled) REQUIRES(mLock);
857867

858-
// Find touched state and touched window by token.
859-
static std::tuple<TouchState*, TouchedWindow*, ui::LogicalDisplayId>
860-
findTouchStateWindowAndDisplay(
861-
const sp<IBinder>& token,
862-
std::unordered_map<ui::LogicalDisplayId, TouchState>& touchStatesByDisplay);
863-
864-
static std::tuple<const TouchState*, const TouchedWindow*, ui::LogicalDisplayId>
865-
findTouchStateWindowAndDisplay(
866-
const sp<IBinder>& token,
867-
const std::unordered_map<ui::LogicalDisplayId, TouchState>& touchStatesByDisplay);
868-
869868
// Statistics gathering.
870869
nsecs_t mLastStatisticPushTime = 0;
871870
std::unique_ptr<InputEventTimelineProcessor> mInputEventTimelineProcessor GUARDED_BY(mLock);

0 commit comments

Comments
 (0)