Skip to content

Commit ae8afff

Browse files
author
Arpit Singh
committed
[9/n Dispatcher refactor] Move computeTouchOcclusionInfo to WindowInfos
In this CL we 1. Moves computeTouchOcclusionInfo to WindowInfo subclass. 2. Moves dumpWindowForTouchOcclusion to anonymous namespace. 3. Move subclass definitions before all private functions. Bug: 367661487 Bug: 245989146 Test: atest inputflinger_tests Flag: EXEMPT refactor Change-Id: Iccf5b53d1754990a29891ca2172132c7dbab2642
1 parent 5f04e1f commit ae8afff

2 files changed

Lines changed: 141 additions & 141 deletions

File tree

services/inputflinger/dispatcher/InputDispatcher.cpp

Lines changed: 27 additions & 26 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,13 @@ 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),
941957
mMaximumObscuringOpacityForTouch(1.0f),
942-
mConnectionManager(mLooper),
943958
mFocusedDisplayId(ui::LogicalDisplayId::DEFAULT),
944959
mWindowTokenWithPointerCapture(nullptr),
945960
mAwaitedApplicationDisplayId(ui::LogicalDisplayId::INVALID),
@@ -3090,12 +3105,12 @@ static bool canBeObscuredBy(const sp<WindowInfoHandle>& windowHandle,
30903105
*
30913106
* If neither of those is true, then it means the touch can be allowed.
30923107
*/
3093-
InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLocked(
3108+
InputDispatcher::DispatcherWindowInfo::TouchOcclusionInfo
3109+
InputDispatcher::DispatcherWindowInfo::computeTouchOcclusionInfo(
30943110
const sp<WindowInfoHandle>& windowHandle, float x, float y) const {
30953111
const WindowInfo* windowInfo = windowHandle->getInfo();
30963112
ui::LogicalDisplayId displayId = windowInfo->displayId;
3097-
const std::vector<sp<WindowInfoHandle>>& windowHandles =
3098-
mWindowInfos.getWindowHandlesForDisplay(displayId);
3113+
const std::vector<sp<WindowInfoHandle>>& windowHandles = getWindowHandlesForDisplay(displayId);
30993114
TouchOcclusionInfo info;
31003115
info.hasBlockingOcclusion = false;
31013116
info.obscuringOpacity = 0;
@@ -3107,12 +3122,11 @@ InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLo
31073122
}
31083123
const WindowInfo* otherInfo = otherHandle->getInfo();
31093124
if (canBeObscuredBy(windowHandle, otherHandle) &&
3110-
windowOccludesTouchAt(*otherInfo, displayId, x, y,
3111-
mWindowInfos.getDisplayTransform(displayId)) &&
3125+
windowOccludesTouchAt(*otherInfo, displayId, x, y, getDisplayTransform(displayId)) &&
31123126
!haveSameApplicationToken(windowInfo, otherInfo)) {
31133127
if (DEBUG_TOUCH_OCCLUSION) {
31143128
info.debugInfo.push_back(
3115-
dumpWindowForTouchOcclusion(otherInfo, /*isTouchedWindow=*/false));
3129+
dumpWindowForTouchOcclusion(*otherInfo, /*isTouchedWindow=*/false));
31163130
}
31173131
// canBeObscuredBy() has returned true above, which means this window is untrusted, so
31183132
// we perform the checks below to see if the touch can be propagated or not based on the
@@ -3140,28 +3154,14 @@ InputDispatcher::TouchOcclusionInfo InputDispatcher::computeTouchOcclusionInfoLo
31403154
}
31413155
}
31423156
if (DEBUG_TOUCH_OCCLUSION) {
3143-
info.debugInfo.push_back(dumpWindowForTouchOcclusion(windowInfo, /*isTouchedWindow=*/true));
3157+
info.debugInfo.push_back(
3158+
dumpWindowForTouchOcclusion(*windowInfo, /*isTouchedWindow=*/true));
31443159
}
31453160
return info;
31463161
}
31473162

3148-
std::string InputDispatcher::dumpWindowForTouchOcclusion(const WindowInfo* info,
3149-
bool isTouchedWindow) const {
3150-
return StringPrintf(INDENT2 "* %spackage=%s/%s, id=%" PRId32 ", mode=%s, alpha=%.2f, "
3151-
"frame=[%" PRId32 ",%" PRId32 "][%" PRId32 ",%" PRId32
3152-
"], touchableRegion=%s, window={%s}, inputConfig={%s}, "
3153-
"hasToken=%s, applicationInfo.name=%s, applicationInfo.token=%s\n",
3154-
isTouchedWindow ? "[TOUCHED] " : "", info->packageName.c_str(),
3155-
info->ownerUid.toString().c_str(), info->id,
3156-
toString(info->touchOcclusionMode).c_str(), info->alpha, info->frame.left,
3157-
info->frame.top, info->frame.right, info->frame.bottom,
3158-
dumpRegion(info->touchableRegion).c_str(), info->name.c_str(),
3159-
info->inputConfig.string().c_str(), toString(info->token != nullptr),
3160-
info->applicationInfo.name.c_str(),
3161-
binderToString(info->applicationInfo.token).c_str());
3162-
}
3163-
3164-
bool InputDispatcher::isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const {
3163+
bool InputDispatcher::isTouchTrustedLocked(
3164+
const DispatcherWindowInfo::TouchOcclusionInfo& occlusionInfo) const {
31653165
if (occlusionInfo.hasBlockingOcclusion) {
31663166
ALOGW("Untrusted touch due to occlusion by %s/%s", occlusionInfo.obscuringPackage.c_str(),
31673167
occlusionInfo.obscuringUid.toString().c_str());
@@ -5303,7 +5303,8 @@ bool InputDispatcher::canWindowReceiveMotionLocked(const sp<WindowInfoHandle>& w
53035303

53045304
// Drop events that can't be trusted due to occlusion
53055305
const auto [x, y] = resolveTouchedPosition(motionEntry);
5306-
TouchOcclusionInfo occlusionInfo = computeTouchOcclusionInfoLocked(window, x, y);
5306+
DispatcherWindowInfo::TouchOcclusionInfo occlusionInfo =
5307+
mWindowInfos.computeTouchOcclusionInfo(window, x, y);
53075308
if (!isTouchTrustedLocked(occlusionInfo)) {
53085309
if (DEBUG_TOUCH_OCCLUSION) {
53095310
ALOGD("Stack of obscuring windows during untrusted touch (%.1f, %.1f):", x, y);

services/inputflinger/dispatcher/InputDispatcher.h

Lines changed: 114 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,119 @@ class InputDispatcher : public android::InputDispatcherInterface {
224224
/** Stores the latest user-activity poke event times per user activity types. */
225225
std::array<nsecs_t, USER_ACTIVITY_EVENT_LAST + 1> mLastUserActivityTimes GUARDED_BY(mLock);
226226

227+
template <typename T>
228+
struct StrongPointerHash {
229+
std::size_t operator()(const sp<T>& b) const { return std::hash<T*>{}(b.get()); }
230+
};
231+
232+
class ConnectionManager {
233+
public:
234+
ConnectionManager(const sp<Looper>& lopper);
235+
~ConnectionManager();
236+
237+
std::shared_ptr<Connection> getConnection(const sp<IBinder>& inputConnectionToken) const;
238+
239+
// Find a monitor pid by the provided token.
240+
std::optional<gui::Pid> findMonitorPidByToken(const sp<IBinder>& token) const;
241+
void forEachGlobalMonitorConnection(
242+
std::function<void(const std::shared_ptr<Connection>&)> f) const;
243+
void forEachGlobalMonitorConnection(
244+
ui::LogicalDisplayId displayId,
245+
std::function<void(const std::shared_ptr<Connection>&)> f) const;
246+
247+
void createGlobalInputMonitor(ui::LogicalDisplayId displayId,
248+
std::unique_ptr<InputChannel>&& inputChannel,
249+
const IdGenerator& idGenerator, gui::Pid pid,
250+
std::function<int(int)> callback);
251+
252+
status_t removeConnection(const std::shared_ptr<Connection>& connection);
253+
254+
void createConnection(std::unique_ptr<InputChannel>&& inputChannel,
255+
const IdGenerator& idGenerator, std::function<int(int)> callback);
256+
257+
std::string dump(nsecs_t currentTime) const;
258+
259+
private:
260+
const sp<Looper> mLooper;
261+
262+
// All registered connections mapped by input channel token.
263+
std::unordered_map<sp<IBinder>, std::shared_ptr<Connection>, StrongPointerHash<IBinder>>
264+
mConnectionsByToken;
265+
266+
// Input channels that will receive a copy of all input events sent to the provided display.
267+
std::unordered_map<ui::LogicalDisplayId, std::vector<Monitor>> mGlobalMonitorsByDisplay;
268+
269+
void removeMonitorChannel(const sp<IBinder>& connectionToken);
270+
};
271+
272+
ConnectionManager mConnectionManager GUARDED_BY(mLock);
273+
274+
class DispatcherWindowInfo {
275+
public:
276+
struct TouchOcclusionInfo {
277+
bool hasBlockingOcclusion;
278+
float obscuringOpacity;
279+
std::string obscuringPackage;
280+
gui::Uid obscuringUid = gui::Uid::INVALID;
281+
std::vector<std::string> debugInfo;
282+
};
283+
284+
void setWindowHandlesForDisplay(
285+
ui::LogicalDisplayId displayId,
286+
std::vector<sp<android::gui::WindowInfoHandle>>&& windowHandles);
287+
288+
void setDisplayInfos(const std::vector<android::gui::DisplayInfo>& displayInfos);
289+
290+
void removeDisplay(ui::LogicalDisplayId displayId);
291+
292+
// Get a reference to window handles by display, return an empty vector if not found.
293+
const std::vector<sp<android::gui::WindowInfoHandle>>& getWindowHandlesForDisplay(
294+
ui::LogicalDisplayId displayId) const;
295+
296+
void forEachWindowHandle(
297+
std::function<void(const sp<android::gui::WindowInfoHandle>&)> f) const;
298+
299+
void forEachDisplayId(std::function<void(ui::LogicalDisplayId)> f) const;
300+
301+
// Get the transform for display, returns Identity-transform if display is missing.
302+
ui::Transform getDisplayTransform(ui::LogicalDisplayId displayId) const;
303+
304+
// Get the raw transform to use for motion events going to the given window.
305+
ui::Transform getRawTransform(const android::gui::WindowInfo&) const;
306+
307+
// Lookup for WindowInfoHandle from token and optionally a display-id. In cases where
308+
// display-id is not provided lookup is done for all displays.
309+
sp<android::gui::WindowInfoHandle> findWindowHandle(
310+
const sp<IBinder>& windowHandleToken,
311+
std::optional<ui::LogicalDisplayId> displayId = {}) const;
312+
313+
bool isWindowPresent(const sp<android::gui::WindowInfoHandle>& windowHandle) const;
314+
315+
// Returns the touched window at the given location, excluding the ignoreWindow if provided.
316+
sp<android::gui::WindowInfoHandle> findTouchedWindowAt(
317+
ui::LogicalDisplayId displayId, float x, float y, bool isStylus = false,
318+
const sp<android::gui::WindowInfoHandle> ignoreWindow = nullptr) const;
319+
320+
std::vector<sp<android::gui::WindowInfoHandle>> findTouchedSpyWindowsAt(
321+
ui::LogicalDisplayId displayId, float x, float y, bool isStylus, DeviceId deviceId,
322+
const std::unordered_map<ui::LogicalDisplayId, TouchState>& touchStatesByDisplay)
323+
const;
324+
325+
TouchOcclusionInfo computeTouchOcclusionInfo(
326+
const sp<android::gui::WindowInfoHandle>& windowHandle, float x, float y) const;
327+
328+
std::string dumpDisplayAndWindowInfo() const;
329+
330+
private:
331+
std::unordered_map<ui::LogicalDisplayId /*displayId*/,
332+
std::vector<sp<android::gui::WindowInfoHandle>>>
333+
mWindowHandlesByDisplay;
334+
std::unordered_map<ui::LogicalDisplayId /*displayId*/, android::gui::DisplayInfo>
335+
mDisplayInfos;
336+
};
337+
338+
DispatcherWindowInfo mWindowInfos GUARDED_BY(mLock);
339+
227340
// With each iteration, InputDispatcher nominally processes one queued event,
228341
// a timeout, or a response from an input consumer.
229342
// This method should only be called on the input dispatcher's own thread.
@@ -262,11 +375,6 @@ class InputDispatcher : public android::InputDispatcherInterface {
262375

263376
status_t pilferPointersLocked(const sp<IBinder>& token) REQUIRES(mLock);
264377

265-
template <typename T>
266-
struct StrongPointerHash {
267-
std::size_t operator()(const sp<T>& b) const { return std::hash<T*>{}(b.get()); }
268-
};
269-
270378
const HmacKeyManager mHmacKeyManager;
271379
const std::array<uint8_t, 32> getSignature(const MotionEntry& motionEntry,
272380
const DispatchEntry& dispatchEntry) const;
@@ -344,103 +452,6 @@ class InputDispatcher : public android::InputDispatcherInterface {
344452
};
345453
sp<gui::WindowInfosListener> mWindowInfoListener;
346454

347-
class DispatcherWindowInfo {
348-
public:
349-
void setWindowHandlesForDisplay(
350-
ui::LogicalDisplayId displayId,
351-
std::vector<sp<android::gui::WindowInfoHandle>>&& windowHandles);
352-
353-
void setDisplayInfos(const std::vector<android::gui::DisplayInfo>& displayInfos);
354-
355-
void removeDisplay(ui::LogicalDisplayId displayId);
356-
357-
// Get a reference to window handles by display, return an empty vector if not found.
358-
const std::vector<sp<android::gui::WindowInfoHandle>>& getWindowHandlesForDisplay(
359-
ui::LogicalDisplayId displayId) const;
360-
361-
void forEachWindowHandle(
362-
std::function<void(const sp<android::gui::WindowInfoHandle>&)> f) const;
363-
364-
void forEachDisplayId(std::function<void(ui::LogicalDisplayId)> f) const;
365-
366-
// Get the transform for display, returns Identity-transform if display is missing.
367-
ui::Transform getDisplayTransform(ui::LogicalDisplayId displayId) const;
368-
369-
// Get the raw transform to use for motion events going to the given window.
370-
ui::Transform getRawTransform(const android::gui::WindowInfo&) const;
371-
372-
// Lookup for WindowInfoHandle from token and optionally a display-id. In cases where
373-
// display-id is not provided lookup is done for all displays.
374-
sp<android::gui::WindowInfoHandle> findWindowHandle(
375-
const sp<IBinder>& windowHandleToken,
376-
std::optional<ui::LogicalDisplayId> displayId = {}) const;
377-
378-
bool isWindowPresent(const sp<android::gui::WindowInfoHandle>& windowHandle) const;
379-
380-
// Returns the touched window at the given location, excluding the ignoreWindow if provided.
381-
sp<android::gui::WindowInfoHandle> findTouchedWindowAt(
382-
ui::LogicalDisplayId displayId, float x, float y, bool isStylus = false,
383-
const sp<android::gui::WindowInfoHandle> ignoreWindow = nullptr) const;
384-
385-
std::vector<sp<android::gui::WindowInfoHandle>> findTouchedSpyWindowsAt(
386-
ui::LogicalDisplayId displayId, float x, float y, bool isStylus, DeviceId deviceId,
387-
const std::unordered_map<ui::LogicalDisplayId, TouchState>& touchStatesByDisplay)
388-
const;
389-
390-
std::string dumpDisplayAndWindowInfo() const;
391-
392-
private:
393-
std::unordered_map<ui::LogicalDisplayId /*displayId*/,
394-
std::vector<sp<android::gui::WindowInfoHandle>>>
395-
mWindowHandlesByDisplay;
396-
std::unordered_map<ui::LogicalDisplayId /*displayId*/, android::gui::DisplayInfo>
397-
mDisplayInfos;
398-
};
399-
400-
DispatcherWindowInfo mWindowInfos GUARDED_BY(mLock);
401-
402-
class ConnectionManager {
403-
public:
404-
ConnectionManager(const sp<Looper>& lopper);
405-
~ConnectionManager();
406-
407-
std::shared_ptr<Connection> getConnection(const sp<IBinder>& inputConnectionToken) const;
408-
409-
// Find a monitor pid by the provided token.
410-
std::optional<gui::Pid> findMonitorPidByToken(const sp<IBinder>& token) const;
411-
void forEachGlobalMonitorConnection(
412-
std::function<void(const std::shared_ptr<Connection>&)> f) const;
413-
void forEachGlobalMonitorConnection(
414-
ui::LogicalDisplayId displayId,
415-
std::function<void(const std::shared_ptr<Connection>&)> f) const;
416-
417-
void createGlobalInputMonitor(ui::LogicalDisplayId displayId,
418-
std::unique_ptr<InputChannel>&& inputChannel,
419-
const IdGenerator& idGenerator, gui::Pid pid,
420-
std::function<int(int)> callback);
421-
422-
status_t removeConnection(const std::shared_ptr<Connection>& connection);
423-
424-
void createConnection(std::unique_ptr<InputChannel>&& inputChannel,
425-
const IdGenerator& idGenerator, std::function<int(int)> callback);
426-
427-
std::string dump(nsecs_t currentTime) const;
428-
429-
private:
430-
const sp<Looper> mLooper;
431-
432-
// All registered connections mapped by input channel token.
433-
std::unordered_map<sp<IBinder>, std::shared_ptr<Connection>, StrongPointerHash<IBinder>>
434-
mConnectionsByToken;
435-
436-
// Input channels that will receive a copy of all input events sent to the provided display.
437-
std::unordered_map<ui::LogicalDisplayId, std::vector<Monitor>> mGlobalMonitorsByDisplay;
438-
439-
void removeMonitorChannel(const sp<IBinder>& connectionToken);
440-
};
441-
442-
ConnectionManager mConnectionManager GUARDED_BY(mLock);
443-
444455
void setInputWindowsLocked(
445456
const std::vector<sp<android::gui::WindowInfoHandle>>& inputWindowHandles,
446457
ui::LogicalDisplayId displayId) REQUIRES(mLock);
@@ -626,24 +637,12 @@ class InputDispatcher : public android::InputDispatcherInterface {
626637
void addDragEventLocked(const MotionEntry& entry) REQUIRES(mLock);
627638
void finishDragAndDrop(ui::LogicalDisplayId displayId, float x, float y) REQUIRES(mLock);
628639

629-
struct TouchOcclusionInfo {
630-
bool hasBlockingOcclusion;
631-
float obscuringOpacity;
632-
std::string obscuringPackage;
633-
gui::Uid obscuringUid = gui::Uid::INVALID;
634-
std::vector<std::string> debugInfo;
635-
};
636-
637-
TouchOcclusionInfo computeTouchOcclusionInfoLocked(
638-
const sp<android::gui::WindowInfoHandle>& windowHandle, float x, float y) const
640+
bool isTouchTrustedLocked(const DispatcherWindowInfo::TouchOcclusionInfo& occlusionInfo) const
639641
REQUIRES(mLock);
640-
bool isTouchTrustedLocked(const TouchOcclusionInfo& occlusionInfo) const REQUIRES(mLock);
641642
bool isWindowObscuredAtPointLocked(const sp<android::gui::WindowInfoHandle>& windowHandle,
642643
float x, float y) const REQUIRES(mLock);
643644
bool isWindowObscuredLocked(const sp<android::gui::WindowInfoHandle>& windowHandle) const
644645
REQUIRES(mLock);
645-
std::string dumpWindowForTouchOcclusion(const android::gui::WindowInfo* info,
646-
bool isTouchWindow) const;
647646
std::string getApplicationWindowLabel(const InputApplicationHandle* applicationHandle,
648647
const sp<android::gui::WindowInfoHandle>& windowHandle);
649648

0 commit comments

Comments
 (0)