Skip to content

Commit b9e6e59

Browse files
author
Arpit Singh
committed
[14/n Dispatcher refactor] Move TouchState methods
In this CL we move or create following TouchState related methods to the TouchState class: 1. clear 2. isPointerInWindow 3. hasTouchingOrHoveringPointers 4. removeAllPointersForDevice 5. dump This CL also updates InputDispatcherUserActivityPokeTests to use current time to inject events, to avoid them being dropped as stale. Bug: 367661487 Bug: 245989146 Test: atest inputflinger_tests Flag: EXEMPT refactor Change-Id: I624700a78471a85a1e97c06b170137ee4e6ddf82
1 parent 403a3d7 commit b9e6e59

3 files changed

Lines changed: 105 additions & 68 deletions

File tree

services/inputflinger/dispatcher/InputDispatcher.cpp

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1267,14 +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 =
1271-
mTouchStates.mTouchStatesByDisplay.find(motionEntry->displayId);
1272-
if (touchStateIt != mTouchStates.mTouchStatesByDisplay.end()) {
1273-
const TouchState& touchState = touchStateIt->second;
1274-
if (!touchState.hasTouchingPointers(motionEntry->deviceId) &&
1275-
!touchState.hasHoveringPointers(motionEntry->deviceId)) {
1276-
dropReason = DropReason::STALE;
1277-
}
1270+
if (!mTouchStates.hasTouchingOrHoveringPointers(motionEntry->displayId,
1271+
motionEntry->deviceId)) {
1272+
dropReason = DropReason::STALE;
12781273
}
12791274
}
12801275
if (dropReason == DropReason::NOT_DROPPED && mNextUnblockedEvent) {
@@ -1716,9 +1711,7 @@ bool InputDispatcher::dispatchDeviceResetLocked(nsecs_t currentTime,
17161711
synthesizeCancelationEventsForAllConnectionsLocked(options);
17171712

17181713
// Remove all active pointers from this device
1719-
for (auto& [_, touchState] : mTouchStates.mTouchStatesByDisplay) {
1720-
touchState.removeAllPointersForDevice(entry.deviceId);
1721-
}
1714+
mTouchStates.removeAllPointersForDevice(entry.deviceId);
17221715
return true;
17231716
}
17241717

@@ -4581,13 +4574,8 @@ void InputDispatcher::notifyMotion(const NotifyMotionArgs& args) {
45814574
if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) {
45824575
// Set the flag anyway if we already have an ongoing gesture. That would allow us to
45834576
// complete the processing of the current stroke.
4584-
const auto touchStateIt = mTouchStates.mTouchStatesByDisplay.find(args.displayId);
4585-
if (touchStateIt != mTouchStates.mTouchStatesByDisplay.end()) {
4586-
const TouchState& touchState = touchStateIt->second;
4587-
if (touchState.hasTouchingPointers(args.deviceId) ||
4588-
touchState.hasHoveringPointers(args.deviceId)) {
4589-
policyFlags |= POLICY_FLAG_PASS_TO_USER;
4590-
}
4577+
if (mTouchStates.hasTouchingOrHoveringPointers(args.displayId, args.deviceId)) {
4578+
policyFlags |= POLICY_FLAG_PASS_TO_USER;
45914579
}
45924580
}
45934581

@@ -4893,13 +4881,8 @@ InputEventInjectionResult InputDispatcher::injectInputEvent(const InputEvent* ev
48934881
if (!(policyFlags & POLICY_FLAG_PASS_TO_USER)) {
48944882
// Set the flag anyway if we already have an ongoing motion gesture. That
48954883
// would allow us to complete the processing of the current stroke.
4896-
const auto touchStateIt = mTouchStates.mTouchStatesByDisplay.find(displayId);
4897-
if (touchStateIt != mTouchStates.mTouchStatesByDisplay.end()) {
4898-
const TouchState& touchState = touchStateIt->second;
4899-
if (touchState.hasTouchingPointers(resolvedDeviceId) ||
4900-
touchState.hasHoveringPointers(resolvedDeviceId)) {
4901-
policyFlags |= POLICY_FLAG_PASS_TO_USER;
4902-
}
4884+
if (mTouchStates.hasTouchingOrHoveringPointers(displayId, resolvedDeviceId)) {
4885+
policyFlags |= POLICY_FLAG_PASS_TO_USER;
49034886
}
49044887
}
49054888

@@ -5978,7 +5961,7 @@ void InputDispatcher::resetAndDropEverythingLocked(const char* reason) {
59785961
resetNoFocusedWindowTimeoutLocked();
59795962

59805963
mAnrTracker.clear();
5981-
mTouchStates.mTouchStatesByDisplay.clear();
5964+
mTouchStates.clear();
59825965
}
59835966

59845967
void InputDispatcher::logDispatchStateLocked() const {
@@ -6036,15 +6019,7 @@ void InputDispatcher::dumpDispatchStateLocked(std::string& dump) const {
60366019
dump += mFocusResolver.dump();
60376020
dump += dumpPointerCaptureStateLocked();
60386021

6039-
if (!mTouchStates.mTouchStatesByDisplay.empty()) {
6040-
dump += StringPrintf(INDENT "TouchStatesByDisplay:\n");
6041-
for (const auto& [displayId, state] : mTouchStates.mTouchStatesByDisplay) {
6042-
std::string touchStateDump = addLinePrefix(state.dump(), INDENT2);
6043-
dump += INDENT2 + displayId.toString() + " : " + touchStateDump;
6044-
}
6045-
} else {
6046-
dump += INDENT "TouchStates: <no displays touched>\n";
6047-
}
6022+
dump += addLinePrefix(mTouchStates.dump(), INDENT);
60486023

60496024
if (mDragState) {
60506025
dump += StringPrintf(INDENT "DragState:\n");
@@ -7093,7 +7068,7 @@ void InputDispatcher::cancelCurrentTouch() {
70937068
"cancel current touch", traceContext.getTracker());
70947069
synthesizeCancelationEventsForAllConnectionsLocked(options);
70957070

7096-
mTouchStates.mTouchStatesByDisplay.clear();
7071+
mTouchStates.clear();
70977072
}
70987073
// Wake up poll loop since there might be work to do.
70997074
mLooper->wake();
@@ -7228,18 +7203,7 @@ bool InputDispatcher::isPointerInWindow(const sp<android::IBinder>& token,
72287203
ui::LogicalDisplayId displayId, DeviceId deviceId,
72297204
int32_t pointerId) {
72307205
std::scoped_lock _l(mLock);
7231-
auto touchStateIt = mTouchStates.mTouchStatesByDisplay.find(displayId);
7232-
if (touchStateIt == mTouchStates.mTouchStatesByDisplay.end()) {
7233-
return false;
7234-
}
7235-
for (const TouchedWindow& window : touchStateIt->second.windows) {
7236-
if (window.windowHandle->getToken() == token &&
7237-
(window.hasTouchingPointer(deviceId, pointerId) ||
7238-
window.hasHoveringPointer(deviceId, pointerId))) {
7239-
return true;
7240-
}
7241-
}
7242-
return false;
7206+
return mTouchStates.isPointerInWindow(token, displayId, deviceId, pointerId);
72437207
}
72447208

72457209
void InputDispatcher::setInputMethodConnectionIsActive(bool isActive) {
@@ -7403,4 +7367,56 @@ ftl::Flags<InputTarget::Flags> InputDispatcher::DispatcherTouchState::getTargetF
74037367
return targetFlags;
74047368
}
74057369

7370+
bool InputDispatcher::DispatcherTouchState::hasTouchingOrHoveringPointers(
7371+
ui::LogicalDisplayId displayId, int32_t deviceId) const {
7372+
const auto touchStateIt = mTouchStatesByDisplay.find(displayId);
7373+
if (touchStateIt == mTouchStatesByDisplay.end()) {
7374+
return false;
7375+
}
7376+
return touchStateIt->second.hasTouchingPointers(deviceId) ||
7377+
touchStateIt->second.hasHoveringPointers(deviceId);
7378+
}
7379+
7380+
bool InputDispatcher::DispatcherTouchState::isPointerInWindow(const sp<android::IBinder>& token,
7381+
ui::LogicalDisplayId displayId,
7382+
android::DeviceId deviceId,
7383+
int32_t pointerId) const {
7384+
const auto touchStateIt = mTouchStatesByDisplay.find(displayId);
7385+
if (touchStateIt == mTouchStatesByDisplay.end()) {
7386+
return false;
7387+
}
7388+
for (const TouchedWindow& window : touchStateIt->second.windows) {
7389+
if (window.windowHandle->getToken() == token &&
7390+
(window.hasTouchingPointer(deviceId, pointerId) ||
7391+
window.hasHoveringPointer(deviceId, pointerId))) {
7392+
return true;
7393+
}
7394+
}
7395+
return false;
7396+
}
7397+
7398+
std::string InputDispatcher::DispatcherTouchState::dump() const {
7399+
std::string dump;
7400+
if (!mTouchStatesByDisplay.empty()) {
7401+
dump += StringPrintf("TouchStatesByDisplay:\n");
7402+
for (const auto& [displayId, state] : mTouchStatesByDisplay) {
7403+
std::string touchStateDump = addLinePrefix(state.dump(), INDENT);
7404+
dump += INDENT + displayId.toString() + " : " + touchStateDump;
7405+
}
7406+
} else {
7407+
dump += "TouchStates: <no displays touched>\n";
7408+
}
7409+
return dump;
7410+
}
7411+
7412+
void InputDispatcher::DispatcherTouchState::removeAllPointersForDevice(android::DeviceId deviceId) {
7413+
for (auto& [_, touchState] : mTouchStatesByDisplay) {
7414+
touchState.removeAllPointersForDevice(deviceId);
7415+
}
7416+
}
7417+
7418+
void InputDispatcher::DispatcherTouchState::clear() {
7419+
mTouchStatesByDisplay.clear();
7420+
}
7421+
74067422
} // namespace android::inputdispatcher

services/inputflinger/dispatcher/InputDispatcher.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,17 @@ class InputDispatcher : public android::InputDispatcherInterface {
373373
sp<android::gui::WindowInfoHandle> findTouchedForegroundWindow(
374374
ui::LogicalDisplayId displayId) const;
375375

376+
bool hasTouchingOrHoveringPointers(ui::LogicalDisplayId displayId, int32_t deviceId) const;
377+
378+
bool isPointerInWindow(const sp<android::IBinder>& token, ui::LogicalDisplayId displayId,
379+
DeviceId deviceId, int32_t pointerId) const;
380+
381+
std::string dump() const;
382+
383+
void removeAllPointersForDevice(DeviceId deviceId);
384+
385+
void clear();
386+
376387
std::unordered_map<ui::LogicalDisplayId, TouchState> mTouchStatesByDisplay;
377388

378389
private:

services/inputflinger/tests/InputDispatcher_test.cpp

Lines changed: 30 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9959,57 +9959,63 @@ TEST_F_WITH_FLAGS(
99599959
InputDispatcherUserActivityPokeTests, MinPokeTimeObserved,
99609960
REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
99619961
rate_limit_user_activity_poke_in_dispatcher))) {
9962+
// Use current time otherwise events may be dropped due to being stale.
9963+
const nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
9964+
99629965
mDispatcher->setMinTimeBetweenUserActivityPokes(50ms);
99639966

99649967
// First event of type TOUCH. Should poke.
99659968
notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
9966-
milliseconds_to_nanoseconds(50));
9969+
currentTime + milliseconds_to_nanoseconds(50));
99679970
mFakePolicy->assertUserActivityPoked(
9968-
{{milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH,
9971+
{{currentTime + milliseconds_to_nanoseconds(50), USER_ACTIVITY_EVENT_TOUCH,
99699972
ui::LogicalDisplayId::DEFAULT}});
99709973

99719974
// 80ns > 50ns has passed since previous TOUCH event. Should poke.
99729975
notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
9973-
milliseconds_to_nanoseconds(130));
9976+
currentTime + milliseconds_to_nanoseconds(130));
99749977
mFakePolicy->assertUserActivityPoked(
9975-
{{milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH,
9978+
{{currentTime + milliseconds_to_nanoseconds(130), USER_ACTIVITY_EVENT_TOUCH,
99769979
ui::LogicalDisplayId::DEFAULT}});
99779980

99789981
// First event of type OTHER. Should poke (despite being within 50ns of previous TOUCH event).
99799982
notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER,
9980-
ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(135));
9983+
ui::LogicalDisplayId::DEFAULT,
9984+
currentTime + milliseconds_to_nanoseconds(135));
99819985
mFakePolicy->assertUserActivityPoked(
9982-
{{milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER,
9986+
{{currentTime + milliseconds_to_nanoseconds(135), USER_ACTIVITY_EVENT_OTHER,
99839987
ui::LogicalDisplayId::DEFAULT}});
99849988

99859989
// Within 50ns of previous TOUCH event. Should NOT poke.
99869990
notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
9987-
milliseconds_to_nanoseconds(140));
9991+
currentTime + milliseconds_to_nanoseconds(140));
99889992
mFakePolicy->assertUserActivityNotPoked();
99899993

99909994
// Within 50ns of previous OTHER event. Should NOT poke.
99919995
notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER,
9992-
ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(150));
9996+
ui::LogicalDisplayId::DEFAULT,
9997+
currentTime + milliseconds_to_nanoseconds(150));
99939998
mFakePolicy->assertUserActivityNotPoked();
99949999

999510000
// Within 50ns of previous TOUCH event (which was at time 130). Should NOT poke.
999610001
// Note that STYLUS is mapped to TOUCH user activity, since it's a pointer-type source.
999710002
notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT,
9998-
milliseconds_to_nanoseconds(160));
10003+
currentTime + milliseconds_to_nanoseconds(160));
999910004
mFakePolicy->assertUserActivityNotPoked();
1000010005

1000110006
// 65ns > 50ns has passed since previous OTHER event. Should poke.
1000210007
notifyAndConsumeMotion(ACTION_SCROLL, AINPUT_SOURCE_ROTARY_ENCODER,
10003-
ui::LogicalDisplayId::DEFAULT, milliseconds_to_nanoseconds(200));
10008+
ui::LogicalDisplayId::DEFAULT,
10009+
currentTime + milliseconds_to_nanoseconds(200));
1000410010
mFakePolicy->assertUserActivityPoked(
10005-
{{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER,
10011+
{{currentTime + milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_OTHER,
1000610012
ui::LogicalDisplayId::DEFAULT}});
1000710013

1000810014
// 170ns > 50ns has passed since previous TOUCH event. Should poke.
1000910015
notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_STYLUS, ui::LogicalDisplayId::DEFAULT,
10010-
milliseconds_to_nanoseconds(300));
10016+
currentTime + milliseconds_to_nanoseconds(300));
1001110017
mFakePolicy->assertUserActivityPoked(
10012-
{{milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH,
10018+
{{currentTime + milliseconds_to_nanoseconds(300), USER_ACTIVITY_EVENT_TOUCH,
1001310019
ui::LogicalDisplayId::DEFAULT}});
1001410020

1001510021
// Assert that there's no more user activity poke event.
@@ -10020,35 +10026,39 @@ TEST_F_WITH_FLAGS(
1002010026
InputDispatcherUserActivityPokeTests, DefaultMinPokeTimeOf100MsUsed,
1002110027
REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
1002210028
rate_limit_user_activity_poke_in_dispatcher))) {
10029+
// Use current time otherwise events may be dropped due to being stale.
10030+
const nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1002310031
notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
10024-
milliseconds_to_nanoseconds(200));
10032+
currentTime + milliseconds_to_nanoseconds(200));
1002510033
mFakePolicy->assertUserActivityPoked(
10026-
{{milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH,
10034+
{{currentTime + milliseconds_to_nanoseconds(200), USER_ACTIVITY_EVENT_TOUCH,
1002710035
ui::LogicalDisplayId::DEFAULT}});
1002810036

1002910037
notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
10030-
milliseconds_to_nanoseconds(280));
10038+
currentTime + milliseconds_to_nanoseconds(280));
1003110039
mFakePolicy->assertUserActivityNotPoked();
1003210040

1003310041
notifyAndConsumeMotion(ACTION_UP, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
10034-
milliseconds_to_nanoseconds(340));
10042+
currentTime + milliseconds_to_nanoseconds(340));
1003510043
mFakePolicy->assertUserActivityPoked(
10036-
{{milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH,
10044+
{{currentTime + milliseconds_to_nanoseconds(340), USER_ACTIVITY_EVENT_TOUCH,
1003710045
ui::LogicalDisplayId::DEFAULT}});
1003810046
}
1003910047

1004010048
TEST_F_WITH_FLAGS(
1004110049
InputDispatcherUserActivityPokeTests, ZeroMinPokeTimeDisablesRateLimiting,
1004210050
REQUIRES_FLAGS_ENABLED(ACONFIG_FLAG(com::android::input::flags,
1004310051
rate_limit_user_activity_poke_in_dispatcher))) {
10052+
// Use current time otherwise events may be dropped due to being stale.
10053+
const nsecs_t currentTime = systemTime(SYSTEM_TIME_MONOTONIC);
1004410054
mDispatcher->setMinTimeBetweenUserActivityPokes(0ms);
1004510055

1004610056
notifyAndConsumeMotion(ACTION_DOWN, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
10047-
20);
10057+
currentTime + 20);
1004810058
mFakePolicy->assertUserActivityPoked();
1004910059

1005010060
notifyAndConsumeMotion(ACTION_MOVE, AINPUT_SOURCE_TOUCHSCREEN, ui::LogicalDisplayId::DEFAULT,
10051-
30);
10061+
currentTime + 30);
1005210062
mFakePolicy->assertUserActivityPoked();
1005310063
}
1005410064

0 commit comments

Comments
 (0)