Skip to content

Commit 403a3d7

Browse files
author
Arpit Singh
committed
[13/n Dispatcher refactor] Move TouchState to DispatcherTouchState
This CL moves mTouchStatesByDisplay to DispatcherTouchState subclass as a public attribute. Upcoming CLs will remove direct access to it and eventually make it private. Bug: 367661487 Bug: 245989146 Test: atest inputflinger_tests Flag: EXEMPT refactor Change-Id: I4db90c626abb909e685eb06c8a8ec82d22a572b9
1 parent 4f2ba50 commit 403a3d7

2 files changed

Lines changed: 62 additions & 57 deletions

File tree

services/inputflinger/dispatcher/InputDispatcher.cpp

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,8 +1267,9 @@ void InputDispatcher::dispatchOnceInnerLocked(nsecs_t& nextWakeupTime) {
12671267
if (dropReason == DropReason::NOT_DROPPED && isStaleEvent(currentTime, *motionEntry)) {
12681268
// The event is stale. However, only drop stale events if there isn't an ongoing
12691269
// gesture. That would allow us to complete the processing of the current stroke.
1270-
const auto touchStateIt = mTouchStatesByDisplay.find(motionEntry->displayId);
1271-
if (touchStateIt != mTouchStatesByDisplay.end()) {
1270+
const auto touchStateIt =
1271+
mTouchStates.mTouchStatesByDisplay.find(motionEntry->displayId);
1272+
if (touchStateIt != mTouchStates.mTouchStatesByDisplay.end()) {
12721273
const TouchState& touchState = touchStateIt->second;
12731274
if (!touchState.hasTouchingPointers(motionEntry->deviceId) &&
12741275
!touchState.hasHoveringPointers(motionEntry->deviceId)) {
@@ -1355,7 +1356,8 @@ bool InputDispatcher::shouldPruneInboundQueueLocked(const MotionEntry& motionEnt
13551356
// Alternatively, maybe there's a spy window that could handle this event.
13561357
const std::vector<sp<WindowInfoHandle>> touchedSpies =
13571358
mWindowInfos.findTouchedSpyWindowsAt(displayId, x, y, isStylus,
1358-
motionEntry.deviceId, mTouchStatesByDisplay);
1359+
motionEntry.deviceId,
1360+
mTouchStates.mTouchStatesByDisplay);
13591361
for (const auto& windowHandle : touchedSpies) {
13601362
const std::shared_ptr<Connection> connection =
13611363
mConnectionManager.getConnection(windowHandle->getToken());
@@ -1714,7 +1716,7 @@ bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime,
17141716
synthesizeCancelationEventsForAllConnectionsLocked(options);
17151717

17161718
// Remove all active pointers from this device
1717-
for (auto& [_, touchState] : mTouchStatesByDisplay) {
1719+
for (auto& [_, touchState] : mTouchStates.mTouchStatesByDisplay) {
17181720
touchState.removeAllPointersForDevice(entry.deviceId);
17191721
}
17201722
return true;
@@ -2077,14 +2079,17 @@ bool InputDispatcher::dispatchMotionLocked(nsecs_t currentTime,
20772079
pilferPointersLocked(mDragState->dragWindow->getToken());
20782080
}
20792081

2080-
Result<std::vector<InputTarget>, InputEventInjectionResult> result = DispatcherTouchState::
2081-
findTouchedWindowTargets(currentTime, *entry, mConnectionManager, mWindowInfos,
2082-
mTouchStatesByDisplay,
2083-
mDragState ? mDragState->dragWindow : nullptr,
2084-
std::bind_front(&InputDispatcher::addDragEventLocked,
2085-
this),
2086-
std::bind_front(&InputDispatcher::logDispatchStateLocked,
2087-
this));
2082+
Result<std::vector<InputTarget>, InputEventInjectionResult> result =
2083+
mTouchStates
2084+
.findTouchedWindowTargets(currentTime, *entry, mConnectionManager,
2085+
mWindowInfos,
2086+
mDragState ? mDragState->dragWindow : nullptr,
2087+
std::bind_front(&InputDispatcher::
2088+
addDragEventLocked,
2089+
this),
2090+
std::bind_front(&InputDispatcher::
2091+
logDispatchStateLocked,
2092+
this));
20882093

20892094
if (result.ok()) {
20902095
inputTargets = std::move(*result);
@@ -2399,11 +2404,10 @@ InputDispatcher::findFocusedWindowTargetLocked(nsecs_t currentTime, const EventE
23992404
return focusedWindowHandle;
24002405
}
24012406

2402-
base::Result<std::vector<InputTarget>, android::os::InputEventInjectionResult>
2407+
base::Result<std::vector<InputTarget>, os::InputEventInjectionResult>
24032408
InputDispatcher::DispatcherTouchState::findTouchedWindowTargets(
24042409
nsecs_t currentTime, const MotionEntry& entry, const ConnectionManager& connections,
24052410
const DispatcherWindowInfo& windowInfos,
2406-
std::unordered_map<ui::LogicalDisplayId, TouchState>& touchStates,
24072411
const sp<android::gui::WindowInfoHandle> dragWindow,
24082412
std::function<void(const MotionEntry&)> addDragEvent, std::function<void()> dump) {
24092413
ATRACE_CALL();
@@ -2420,7 +2424,7 @@ InputDispatcher::DispatcherTouchState::findTouchedWindowTargets(
24202424
// If no state for the specified display exists, then our initial state will be empty.
24212425
const TouchState* oldState = nullptr;
24222426
TouchState tempTouchState;
2423-
if (const auto it = touchStates.find(displayId); it != touchStates.end()) {
2427+
if (const auto it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) {
24242428
oldState = &(it->second);
24252429
tempTouchState = *oldState;
24262430
}
@@ -2484,7 +2488,7 @@ InputDispatcher::DispatcherTouchState::findTouchedWindowTargets(
24842488

24852489
std::vector<sp<WindowInfoHandle>> newTouchedWindows =
24862490
windowInfos.findTouchedSpyWindowsAt(displayId, x, y, isStylus, entry.deviceId,
2487-
touchStates);
2491+
mTouchStatesByDisplay);
24882492
if (newTouchedWindowHandle != nullptr) {
24892493
// Process the foreground window first so that it is the first to receive the event.
24902494
newTouchedWindows.insert(newTouchedWindows.begin(), newTouchedWindowHandle);
@@ -2498,7 +2502,7 @@ InputDispatcher::DispatcherTouchState::findTouchedWindowTargets(
24982502

24992503
for (const sp<WindowInfoHandle>& windowHandle : newTouchedWindows) {
25002504
if (!canWindowReceiveMotion(windowHandle, entry, connections, windowInfos,
2501-
touchStates)) {
2505+
mTouchStatesByDisplay)) {
25022506
continue;
25032507
}
25042508

@@ -2622,7 +2626,7 @@ InputDispatcher::DispatcherTouchState::findTouchedWindowTargets(
26222626
// Do not slide events to the window which can not receive motion event
26232627
if (newTouchedWindowHandle != nullptr &&
26242628
!canWindowReceiveMotion(newTouchedWindowHandle, entry, connections, windowInfos,
2625-
touchStates)) {
2629+
mTouchStatesByDisplay)) {
26262630
newTouchedWindowHandle = nullptr;
26272631
}
26282632

@@ -2812,14 +2816,14 @@ InputDispatcher::DispatcherTouchState::findTouchedWindowTargets(
28122816
if (maskedAction != AMOTION_EVENT_ACTION_SCROLL) {
28132817
if (displayId >= ui::LogicalDisplayId::DEFAULT) {
28142818
tempTouchState.clearWindowsWithoutPointers();
2815-
touchStates[displayId] = tempTouchState;
2819+
mTouchStatesByDisplay[displayId] = tempTouchState;
28162820
} else {
2817-
touchStates.erase(displayId);
2821+
mTouchStatesByDisplay.erase(displayId);
28182822
}
28192823
}
28202824

28212825
if (tempTouchState.windows.empty()) {
2822-
touchStates.erase(displayId);
2826+
mTouchStatesByDisplay.erase(displayId);
28232827
}
28242828

28252829
return targets;
@@ -4075,7 +4079,7 @@ void InputDispatcher::synthesizeCancelationEventsForAllConnectionsLocked(
40754079
// Generate cancellations for touched windows first. This is to avoid generating cancellations
40764080
// through a non-touched window if there are more than one window for an input channel.
40774081
if (cancelPointers) {
4078-
for (const auto& [displayId, touchState] : mTouchStatesByDisplay) {
4082+
for (const auto& [displayId, touchState] : mTouchStates.mTouchStatesByDisplay) {
40794083
if (options.displayId.has_value() && options.displayId != displayId) {
40804084
continue;
40814085
}
@@ -4280,7 +4284,8 @@ void InputDispatcher::synthesizePointerDownEventsForConnectionLocked(
42804284
}
42814285

42824286
const auto [_, touchedWindowState, displayId] =
4283-
findTouchStateWindowAndDisplay(connection->getToken(), mTouchStatesByDisplay);
4287+
findTouchStateWindowAndDisplay(connection->getToken(),
4288+
mTouchStates.mTouchStatesByDisplay);
42844289
if (touchedWindowState == nullptr) {
42854290
LOG(FATAL) << __func__ << ": Touch state is out of sync: No touched window for token";
42864291
}
@@ -4576,8 +4581,8 @@ void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) {
45764581
if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) {
45774582
// Set the flag anyway if we already have an ongoing gesture. That would allow us to
45784583
// complete the processing of the current stroke.
4579-
const auto touchStateIt = mTouchStatesByDisplay.find(args.displayId);
4580-
if (touchStateIt != mTouchStatesByDisplay.end()) {
4584+
const auto touchStateIt = mTouchStates.mTouchStatesByDisplay.find(args.displayId);
4585+
if (touchStateIt != mTouchStates.mTouchStatesByDisplay.end()) {
45814586
const TouchState& touchState = touchStateIt->second;
45824587
if (touchState.hasTouchingPointers(args.deviceId) ||
45834588
touchState.hasHoveringPointers(args.deviceId)) {
@@ -4888,8 +4893,8 @@ InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* ev
48884893
if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) {
48894894
// Set the flag anyway if we already have an ongoing motion gesture. That
48904895
// would allow us to complete the processing of the current stroke.
4891-
const auto touchStateIt = mTouchStatesByDisplay.find(displayId);
4892-
if (touchStateIt != mTouchStatesByDisplay.end()) {
4896+
const auto touchStateIt = mTouchStates.mTouchStatesByDisplay.find(displayId);
4897+
if (touchStateIt != mTouchStates.mTouchStatesByDisplay.end()) {
48934898
const TouchState& touchState = touchStateIt->second;
48944899
if (touchState.hasTouchingPointers(resolvedDeviceId) ||
48954900
touchState.hasHoveringPointers(resolvedDeviceId)) {
@@ -5465,7 +5470,8 @@ void InputDispatcher::setInputWindowsLocked(
54655470
onFocusChangedLocked(*changes, traceContext.getTracker(), removedFocusedWindowHandle);
54665471
}
54675472

5468-
if (const auto& it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) {
5473+
if (const auto& it = mTouchStates.mTouchStatesByDisplay.find(displayId);
5474+
it != mTouchStates.mTouchStatesByDisplay.end()) {
54695475
TouchState& state = it->second;
54705476
for (size_t i = 0; i < state.windows.size();) {
54715477
TouchedWindow& touchedWindow = state.windows[i];
@@ -5506,7 +5512,8 @@ void InputDispatcher::setInputWindowsLocked(
55065512

55075513
// Check if the hovering should stop because the window is no longer eligible to receive it
55085514
// (for example, if the touchable region changed)
5509-
if (const auto& it = mTouchStatesByDisplay.find(displayId); it != mTouchStatesByDisplay.end()) {
5515+
if (const auto& it = mTouchStates.mTouchStatesByDisplay.find(displayId);
5516+
it != mTouchStates.mTouchStatesByDisplay.end()) {
55105517
TouchState& state = it->second;
55115518
for (TouchedWindow& touchedWindow : state.windows) {
55125519
std::vector<DeviceId> erasedDevices = touchedWindow.eraseHoveringPointersIf(
@@ -5812,7 +5819,7 @@ bool InputDispatcher::transferTouchGesture(const sp<IBinder>& fromToken, const s
58125819

58135820
// Find the target touch state and touched window by fromToken.
58145821
auto [state, touchedWindow, displayId] =
5815-
findTouchStateWindowAndDisplay(fromToken, mTouchStatesByDisplay);
5822+
findTouchStateWindowAndDisplay(fromToken, mTouchStates.mTouchStatesByDisplay);
58165823

58175824
if (state == nullptr || touchedWindow == nullptr) {
58185825
ALOGD("Touch transfer failed because from window is not being touched.");
@@ -5905,10 +5912,9 @@ bool InputDispatcher::transferTouchGesture(const sp<IBinder>& fromToken, const s
59055912
* window is being touched.
59065913
*/
59075914
sp<WindowInfoHandle> InputDispatcher::DispatcherTouchState::findTouchedForegroundWindow(
5908-
const std::unordered_map<ui::LogicalDisplayId, TouchState>& touchStatesByDisplay,
5909-
ui::LogicalDisplayId displayId) {
5910-
const auto stateIt = touchStatesByDisplay.find(displayId);
5911-
if (stateIt == touchStatesByDisplay.end()) {
5915+
ui::LogicalDisplayId displayId) const {
5916+
const auto stateIt = mTouchStatesByDisplay.find(displayId);
5917+
if (stateIt == mTouchStatesByDisplay.end()) {
59125918
ALOGI("No touch state on display %s", displayId.toString().c_str());
59135919
return nullptr;
59145920
}
@@ -5944,8 +5950,7 @@ bool InputDispatcher::transferTouchOnDisplay(const sp<IBinder>& destChannelToken
59445950
return false;
59455951
}
59465952

5947-
sp<WindowInfoHandle> from =
5948-
DispatcherTouchState::findTouchedForegroundWindow(mTouchStatesByDisplay, displayId);
5953+
sp<WindowInfoHandle> from = mTouchStates.findTouchedForegroundWindow(displayId);
59495954
if (from == nullptr) {
59505955
ALOGE("Could not find a source window in %s for %p", __func__, destChannelToken.get());
59515956
return false;
@@ -5973,7 +5978,7 @@ void InputDispatcher::resetAndDropEverythingLocked(const char* reason) {
59735978
resetNoFocusedWindowTimeoutLocked();
59745979

59755980
mAnrTracker.clear();
5976-
mTouchStatesByDisplay.clear();
5981+
mTouchStates.mTouchStatesByDisplay.clear();
59775982
}
59785983

59795984
void InputDispatcher::logDispatchStateLocked() const {
@@ -6031,9 +6036,9 @@ void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const {
60316036
dump += mFocusResolver.dump();
60326037
dump += dumpPointerCaptureStateLocked();
60336038

6034-
if (!mTouchStatesByDisplay.empty()) {
6039+
if (!mTouchStates.mTouchStatesByDisplay.empty()) {
60356040
dump += StringPrintf(INDENT "TouchStatesByDisplay:\n");
6036-
for (const auto& [displayId, state] : mTouchStatesByDisplay) {
6041+
for (const auto& [displayId, state] : mTouchStates.mTouchStatesByDisplay) {
60376042
std::string touchStateDump = addLinePrefix(state.dump(), INDENT2);
60386043
dump += INDENT2 + displayId.toString() + " : " + touchStateDump;
60396044
}
@@ -6242,7 +6247,7 @@ status_t InputDispatcher::pilferPointersLocked(const sp<IBinder>& token) {
62426247
}
62436248

62446249
auto [statePtr, windowPtr, displayId] =
6245-
findTouchStateWindowAndDisplay(token, mTouchStatesByDisplay);
6250+
findTouchStateWindowAndDisplay(token, mTouchStates.mTouchStatesByDisplay);
62466251
if (statePtr == nullptr || windowPtr == nullptr) {
62476252
LOG(WARNING)
62486253
<< "Attempted to pilfer points from a channel without any on-going pointer streams."
@@ -7088,7 +7093,7 @@ void InputDispatcher::cancelCurrentTouch() {
70887093
"cancel current touch", traceContext.getTracker());
70897094
synthesizeCancelationEventsForAllConnectionsLocked(options);
70907095

7091-
mTouchStatesByDisplay.clear();
7096+
mTouchStates.mTouchStatesByDisplay.clear();
70927097
}
70937098
// Wake up poll loop since there might be work to do.
70947099
mLooper->wake();
@@ -7223,8 +7228,8 @@ bool InputDispatcher::isPointerInWindow(const sp<android::IBinder>& token,
72237228
ui::LogicalDisplayId displayId, DeviceId deviceId,
72247229
int32_t pointerId) {
72257230
std::scoped_lock _l(mLock);
7226-
auto touchStateIt = mTouchStatesByDisplay.find(displayId);
7227-
if (touchStateIt == mTouchStatesByDisplay.end()) {
7231+
auto touchStateIt = mTouchStates.mTouchStatesByDisplay.find(displayId);
7232+
if (touchStateIt == mTouchStates.mTouchStatesByDisplay.end()) {
72287233
return false;
72297234
}
72307235
for (const TouchedWindow& window : touchStateIt->second.windows) {

services/inputflinger/dispatcher/InputDispatcher.h

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -352,15 +352,6 @@ class InputDispatcher : public android::InputDispatcherInterface {
352352

353353
class DispatcherTouchState {
354354
public:
355-
static base::Result<std::vector<InputTarget>, android::os::InputEventInjectionResult>
356-
findTouchedWindowTargets(nsecs_t currentTime, const MotionEntry& entry,
357-
const ConnectionManager& connections,
358-
const DispatcherWindowInfo& windowInfos,
359-
std::unordered_map<ui::LogicalDisplayId, TouchState>& touchStates,
360-
const sp<android::gui::WindowInfoHandle> dragWindow,
361-
std::function<void(const MotionEntry&)> addDragEvent,
362-
std::function<void()> dump);
363-
364355
static void addPointerWindowTarget(const sp<android::gui::WindowInfoHandle>& windowHandle,
365356
InputTarget::DispatchMode dispatchMode,
366357
ftl::Flags<InputTarget::Flags> targetFlags,
@@ -371,9 +362,18 @@ class InputDispatcher : public android::InputDispatcherInterface {
371362
std::function<void()> dump,
372363
std::vector<InputTarget>& inputTargets);
373364

374-
static sp<android::gui::WindowInfoHandle> findTouchedForegroundWindow(
375-
const std::unordered_map<ui::LogicalDisplayId, TouchState>& touchStatesByDisplay,
376-
ui::LogicalDisplayId displayId);
365+
base::Result<std::vector<InputTarget>, android::os::InputEventInjectionResult>
366+
findTouchedWindowTargets(nsecs_t currentTime, const MotionEntry& entry,
367+
const ConnectionManager& connections,
368+
const DispatcherWindowInfo& windowInfos,
369+
const sp<android::gui::WindowInfoHandle> dragWindow,
370+
std::function<void(const MotionEntry&)> addDragEvent,
371+
std::function<void()> dump);
372+
373+
sp<android::gui::WindowInfoHandle> findTouchedForegroundWindow(
374+
ui::LogicalDisplayId displayId) const;
375+
376+
std::unordered_map<ui::LogicalDisplayId, TouchState> mTouchStatesByDisplay;
377377

378378
private:
379379
static std::vector<InputTarget> findOutsideTargets(
@@ -409,6 +409,8 @@ class InputDispatcher : public android::InputDispatcherInterface {
409409
bool isSplit, const DispatcherWindowInfo& windowInfos);
410410
};
411411

412+
DispatcherTouchState mTouchStates GUARDED_BY(mLock);
413+
412414
// With each iteration, InputDispatcher nominally processes one queued event,
413415
// a timeout, or a response from an input consumer.
414416
// This method should only be called on the input dispatcher's own thread.
@@ -540,8 +542,6 @@ class InputDispatcher : public android::InputDispatcherInterface {
540542
const std::vector<sp<android::gui::WindowInfoHandle>>& inputWindowHandles,
541543
ui::LogicalDisplayId displayId) REQUIRES(mLock);
542544

543-
std::unordered_map<ui::LogicalDisplayId /*displayId*/, TouchState> mTouchStatesByDisplay
544-
GUARDED_BY(mLock);
545545
std::unique_ptr<DragState> mDragState GUARDED_BY(mLock);
546546

547547
void setFocusedApplicationLocked(

0 commit comments

Comments
 (0)