Skip to content

Commit febbea0

Browse files
Arpit SinghAndroid (Google) Code Review
authored andcommitted
Merge changes I9f2f1821,I74840ff6,Ie41c0381 into main
* changes: [11/n Dispatcher refactor] Move isTouchTrusted to WindowInfo [10/n Dispatcher refactor] Move obscuring related methods to WindowInfo [9/n Dispatcher refactor] Move computeTouchOcclusionInfo to WindowInfos
2 parents 0fb93b8 + 60448cf commit febbea0

2 files changed

Lines changed: 184 additions & 179 deletions

File tree

services/inputflinger/dispatcher/InputDispatcher.cpp

Lines changed: 57 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -918,6 +918,21 @@ InputTarget createInputTarget(const std::shared_ptr<Connection>& connection,
918918
return inputTarget;
919919
}
920920

921+
std::string dumpWindowForTouchOcclusion(const WindowInfo& info, bool isTouchedWindow) {
922+
return StringPrintf(INDENT2 "* %spackage=%s/%s, id=%" PRId32 ", mode=%s, alpha=%.2f, "
923+
"frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32
924+
"], touchableRegion=%s, window={%s}, inputConfig={%s}, "
925+
"hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n",
926+
isTouchedWindow ? "[TOUCHED] " : "", info.packageName.c_str(),
927+
info.ownerUid.toString().c_str(), info.id,
928+
toString(info.touchOcclusionMode).c_str(), info.alpha, info.frame.left,
929+
info.frame.top, info.frame.right, info.frame.bottom,
930+
dumpRegion(info.touchableRegion).c_str(), info.name.c_str(),
931+
info.inputConfig.string().c_str(), toString(info.token != nullptr),
932+
info.applicationInfo.name.c_str(),
933+
binderToString(info.applicationInfo.token).c_str());
934+
}
935+
921936
} // namespace
922937

923938
// --- InputDispatcher ---
@@ -933,13 +948,12 @@ InputDispatcher::InputDispatcher(InputDispatcherPolicyInterface& policy,
933948
mLastDropReason(DropReason::NOT_DROPPED),
934949
mIdGenerator(IdGenerator::Source::INPUT_DISPATCHER),
935950
mMinTimeBetweenUserActivityPokes(DEFAULT_USER_ACTIVITY_POKE_INTERVAL),
951+
mConnectionManager(mLooper),
936952
mNextUnblockedEvent(nullptr),
937953
mMonitorDispatchingTimeout(DEFAULT_INPUT_DISPATCHING_TIMEOUT),
938954
mDispatchEnabled(false),
939955
mDispatchFrozen(false),
940956
mInputFilterEnabled(false),
941-
mMaximumObscuringOpacityForTouch(1.0f),
942-
mConnectionManager(mLooper),
943957
mFocusedDisplayId(ui::LogicalDisplayId::DEFAULT),
944958
mWindowTokenWithPointerCapture(nullptr),
945959
mAwaitedApplicationDisplayId(ui::LogicalDisplayId::INVALID),
@@ -2491,9 +2505,9 @@ InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, const Motio
24912505
if (isSplit) {
24922506
targetFlags |= InputTarget::Flags::SPLIT;
24932507
}
2494-
if (isWindowObscuredAtPointLocked(windowHandle, x, y)) {
2508+
if (mWindowInfos.isWindowObscuredAtPoint(windowHandle, x, y)) {
24952509
targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED;
2496-
} else if (isWindowObscuredLocked(windowHandle)) {
2510+
} else if (mWindowInfos.isWindowObscured(windowHandle)) {
24972511
targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
24982512
}
24992513

@@ -2524,7 +2538,8 @@ InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, const Motio
25242538
if (isDownOrPointerDown && targetFlags.test(InputTarget::Flags::FOREGROUND) &&
25252539
windowHandle->getInfo()->inputConfig.test(
25262540
gui::WindowInfo::InputConfig::DUPLICATE_TOUCH_TO_WALLPAPER)) {
2527-
sp<WindowInfoHandle> wallpaper = findWallpaperWindowBelow(windowHandle);
2541+
sp<WindowInfoHandle> wallpaper =
2542+
mWindowInfos.findWallpaperWindowBelow(windowHandle);
25282543
if (wallpaper != nullptr) {
25292544
ftl::Flags<InputTarget::Flags> wallpaperFlags =
25302545
InputTarget::Flags::WINDOW_IS_OBSCURED |
@@ -2637,9 +2652,9 @@ InputDispatcher::findTouchedWindowTargetsLocked(nsecs_t currentTime, const Motio
26372652
if (isSplit) {
26382653
targetFlags |= InputTarget::Flags::SPLIT;
26392654
}
2640-
if (isWindowObscuredAtPointLocked(newTouchedWindowHandle, x, y)) {
2655+
if (mWindowInfos.isWindowObscuredAtPoint(newTouchedWindowHandle, x, y)) {
26412656
targetFlags |= InputTarget::Flags::WINDOW_IS_OBSCURED;
2642-
} else if (isWindowObscuredLocked(newTouchedWindowHandle)) {
2657+
} else if (mWindowInfos.isWindowObscured(newTouchedWindowHandle)) {
26432658
targetFlags |= InputTarget::Flags::WINDOW_IS_PARTIALLY_OBSCURED;
26442659
}
26452660

@@ -3093,12 +3108,12 @@ static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle,
30933108
*
30943109
* If neither of those is true, then it means the touch can be allowed.
30953110
*/
3096-
InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked(
3111+
InputDispatcher::DispatcherWindowInfo::TouchOcclusionInfo
3112+
InputDispatcher::DispatcherWindowInfo::computeTouchOcclusionInfo(
30973113
const sp<WindowInfoHandle>& windowHandle, float x, float y) const {
30983114
const WindowInfo* windowInfo = windowHandle->getInfo();
30993115
ui::LogicalDisplayId displayId = windowInfo->displayId;
3100-
const std::vector<sp<WindowInfoHandle>>& windowHandles =
3101-
mWindowInfos.getWindowHandlesForDisplay(displayId);
3116+
const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesForDisplay(displayId);
31023117
TouchOcclusionInfo info;
31033118
info.hasBlockingOcclusion = false;
31043119
info.obscuringOpacity = 0;
@@ -3110,12 +3125,11 @@ InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLo
31103125
}
31113126
const WindowInfo* otherInfo = otherHandle->getInfo();
31123127
if (canBeObscuredBy(windowHandle, otherHandle) &&
3113-
windowOccludesTouchAt(*otherInfo, displayId, x, y,
3114-
mWindowInfos.getDisplayTransform(displayId)) &&
3128+
windowOccludesTouchAt(*otherInfo, displayId, x, y, getDisplayTransform(displayId)) &&
31153129
!haveSameApplicationToken(windowInfo, otherInfo)) {
31163130
if (DEBUG_TOUCH_OCCLUSION) {
31173131
info.debugInfo.push_back(
3118-
dumpWindowForTouchOcclusion(otherInfo, /*isTouchedWindow=*/false));
3132+
dumpWindowForTouchOcclusion(*otherInfo, /*isTouchedWindow=*/false));
31193133
}
31203134
// canBeObscuredBy() has returned true above, which means this window is untrusted, so
31213135
// we perform the checks below to see if the touch can be propagated or not based on the
@@ -3143,28 +3157,14 @@ InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLo
31433157
}
31443158
}
31453159
if (DEBUG_TOUCH_OCCLUSION) {
3146-
info.debugInfo.push_back(dumpWindowForTouchOcclusion(windowInfo, /*isTouchedWindow=*/true));
3160+
info.debugInfo.push_back(
3161+
dumpWindowForTouchOcclusion(*windowInfo, /*isTouchedWindow=*/true));
31473162
}
31483163
return info;
31493164
}
31503165

3151-
std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info,
3152-
bool isTouchedWindow) const {
3153-
return StringPrintf(INDENT2 "* %spackage=%s/%s, id=%" PRId32 ", mode=%s, alpha=%.2f, "
3154-
"frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32
3155-
"], touchableRegion=%s, window={%s}, inputConfig={%s}, "
3156-
"hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n",
3157-
isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(),
3158-
info->ownerUid.toString().c_str(), info->id,
3159-
toString(info->touchOcclusionMode).c_str(), info->alpha, info->frame.left,
3160-
info->frame.top, info->frame.right, info->frame.bottom,
3161-
dumpRegion(info->touchableRegion).c_str(), info->name.c_str(),
3162-
info->inputConfig.string().c_str(), toString(info->token != nullptr),
3163-
info->applicationInfo.name.c_str(),
3164-
binderToString(info->applicationInfo.token).c_str());
3165-
}
3166-
3167-
bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const {
3166+
bool InputDispatcher::DispatcherWindowInfo::isTouchTrusted(
3167+
const TouchOcclusionInfo& occlusionInfo) const {
31683168
if (occlusionInfo.hasBlockingOcclusion) {
31693169
ALOGW("Untrusted touch due to occlusion by %s/%s", occlusionInfo.obscuringPackage.c_str(),
31703170
occlusionInfo.obscuringUid.toString().c_str());
@@ -3180,29 +3180,27 @@ bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionIn
31803180
return true;
31813181
}
31823182

3183-
bool InputDispatcher::isWindowObscuredAtPointLocked(const sp<WindowInfoHandle>& windowHandle,
3184-
float x, float y) const {
3183+
bool InputDispatcher::DispatcherWindowInfo::isWindowObscuredAtPoint(
3184+
const sp<WindowInfoHandle>& windowHandle, float x, float y) const {
31853185
ui::LogicalDisplayId displayId = windowHandle->getInfo()->displayId;
3186-
const std::vector<sp<WindowInfoHandle>>& windowHandles =
3187-
mWindowInfos.getWindowHandlesForDisplay(displayId);
3186+
const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesForDisplay(displayId);
31883187
for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
31893188
if (windowHandle == otherHandle) {
31903189
break; // All future windows are below us. Exit early.
31913190
}
31923191
const WindowInfo* otherInfo = otherHandle->getInfo();
31933192
if (canBeObscuredBy(windowHandle, otherHandle) &&
3194-
windowOccludesTouchAt(*otherInfo, displayId, x, y,
3195-
mWindowInfos.getDisplayTransform(displayId))) {
3193+
windowOccludesTouchAt(*otherInfo, displayId, x, y, getDisplayTransform(displayId))) {
31963194
return true;
31973195
}
31983196
}
31993197
return false;
32003198
}
32013199

3202-
bool InputDispatcher::isWindowObscuredLocked(const sp<WindowInfoHandle>& windowHandle) const {
3200+
bool InputDispatcher::DispatcherWindowInfo::isWindowObscured(
3201+
const sp<WindowInfoHandle>& windowHandle) const {
32033202
ui::LogicalDisplayId displayId = windowHandle->getInfo()->displayId;
3204-
const std::vector<sp<WindowInfoHandle>>& windowHandles =
3205-
mWindowInfos.getWindowHandlesForDisplay(displayId);
3203+
const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesForDisplay(displayId);
32063204
const WindowInfo* windowInfo = windowHandle->getInfo();
32073205
for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
32083206
if (windowHandle == otherHandle) {
@@ -5272,8 +5270,9 @@ std::string InputDispatcher::DispatcherWindowInfo::dumpDisplayAndWindowInfo() co
52725270
return dump;
52735271
}
52745272

5275-
bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& window,
5276-
const MotionEntry& motionEntry) const {
5273+
bool InputDispatcher::canWindowReceiveMotionLocked(
5274+
const sp<android::gui::WindowInfoHandle>& window,
5275+
const android::inputdispatcher::MotionEntry& motionEntry) const {
52775276
const WindowInfo& info = *window->getInfo();
52785277

52795278
// Skip spy window targets that are not valid for targeted injection.
@@ -5306,8 +5305,9 @@ bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& w
53065305

53075306
// Drop events that can't be trusted due to occlusion
53085307
const auto [x, y] = resolveTouchedPosition(motionEntry);
5309-
TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y);
5310-
if (!isTouchTrustedLocked(occlusionInfo)) {
5308+
DispatcherWindowInfo::TouchOcclusionInfo occlusionInfo =
5309+
mWindowInfos.computeTouchOcclusionInfo(window, x, y);
5310+
if (!mWindowInfos.isTouchTrusted(occlusionInfo)) {
53115311
if (DEBUG_TOUCH_OCCLUSION) {
53125312
ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y);
53135313
for (const auto& log : occlusionInfo.debugInfo) {
@@ -5751,13 +5751,8 @@ bool InputDispatcher::recentWindowsAreOwnedByLocked(gui::Pid pid, gui::Uid uid)
57515751
}
57525752

57535753
void InputDispatcher::setMaximumObscuringOpacityForTouch(float opacity) {
5754-
if (opacity < 0 || opacity > 1) {
5755-
LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1");
5756-
return;
5757-
}
5758-
57595754
std::scoped_lock lock(mLock);
5760-
mMaximumObscuringOpacityForTouch = opacity;
5755+
mWindowInfos.setMaximumObscuringOpacityForTouch(opacity);
57615756
}
57625757

57635758
std::tuple<const TouchState*, const TouchedWindow*, ui::LogicalDisplayId>
@@ -7065,7 +7060,7 @@ bool InputDispatcher::shouldDropInput(
70657060
if (windowHandle->getInfo()->inputConfig.test(WindowInfo::InputConfig::DROP_INPUT) ||
70667061
(windowHandle->getInfo()->inputConfig.test(
70677062
WindowInfo::InputConfig::DROP_INPUT_IF_OBSCURED) &&
7068-
isWindowObscuredLocked(windowHandle))) {
7063+
mWindowInfos.isWindowObscured(windowHandle))) {
70697064
ALOGW("Dropping %s event targeting %s as requested by the input configuration {%s} on "
70707065
"display %s.",
70717066
ftl::enum_string(entry.type).c_str(), windowHandle->getName().c_str(),
@@ -7118,7 +7113,7 @@ void InputDispatcher::slipWallpaperTouch(ftl::Flags<InputTarget::Flags> targetFl
71187113
const sp<WindowInfoHandle> oldWallpaper =
71197114
oldHasWallpaper ? state.getWallpaperWindow(deviceId) : nullptr;
71207115
const sp<WindowInfoHandle> newWallpaper =
7121-
newHasWallpaper ? findWallpaperWindowBelow(newWindowHandle) : nullptr;
7116+
newHasWallpaper ? mWindowInfos.findWallpaperWindowBelow(newWindowHandle) : nullptr;
71227117
if (oldWallpaper == newWallpaper) {
71237118
return;
71247119
}
@@ -7155,7 +7150,7 @@ void InputDispatcher::transferWallpaperTouch(
71557150
const sp<WindowInfoHandle> oldWallpaper =
71567151
oldHasWallpaper ? state.getWallpaperWindow(deviceId) : nullptr;
71577152
const sp<WindowInfoHandle> newWallpaper =
7158-
newHasWallpaper ? findWallpaperWindowBelow(toWindowHandle) : nullptr;
7153+
newHasWallpaper ? mWindowInfos.findWallpaperWindowBelow(toWindowHandle) : nullptr;
71597154
if (oldWallpaper == newWallpaper) {
71607155
return;
71617156
}
@@ -7187,10 +7182,10 @@ void InputDispatcher::transferWallpaperTouch(
71877182
}
71887183
}
71897184

7190-
sp<WindowInfoHandle> InputDispatcher::findWallpaperWindowBelow(
7185+
sp<WindowInfoHandle> InputDispatcher::DispatcherWindowInfo::findWallpaperWindowBelow(
71917186
const sp<WindowInfoHandle>& windowHandle) const {
71927187
const std::vector<sp<WindowInfoHandle>>& windowHandles =
7193-
mWindowInfos.getWindowHandlesForDisplay(windowHandle->getInfo()->displayId);
7188+
getWindowHandlesForDisplay(windowHandle->getInfo()->displayId);
71947189
bool foundWindow = false;
71957190
for (const sp<WindowInfoHandle>& otherHandle : windowHandles) {
71967191
if (!foundWindow && otherHandle != windowHandle) {
@@ -7362,4 +7357,11 @@ std::string InputDispatcher::ConnectionManager::dump(nsecs_t currentTime) const
73627357
return dump;
73637358
}
73647359

7360+
void InputDispatcher::DispatcherWindowInfo::setMaximumObscuringOpacityForTouch(float opacity) {
7361+
if (opacity < 0 || opacity > 1) {
7362+
LOG_ALWAYS_FATAL("Maximum obscuring opacity for touch should be >= 0 and <= 1");
7363+
}
7364+
mMaximumObscuringOpacityForTouch = opacity;
7365+
}
7366+
73657367
} // namespace android::inputdispatcher

0 commit comments

Comments
 (0)