InputDispatcher: Ensure synthesized events are associated with windows

For tracing purposes, we want as much information as possible about
where input events are dispatched. Ideally, we will trace the windowId
that is the target of each dispatched event. That way, we can easily
look up properties of the window that received the event.

The difficulty with this is that there is a many-to-one mappings between
windows and input channels, where more than one window can receive input
through the same channel. A channel might also receive input when there
no windows associated with it - such as if a window has been removed and
we want to generate cancelation events for it, or such as for global
monitors that don't have windows at all.

We make the best effort to associate each event dispatch with a
windowId. Then in the tests, we consume events from the window that we
expect to be associated with the event.

For cloned windows that are both touched at the same time, the entire
gesture is usually associated with the window that was first touched,
because the pointers from both windows will be merged into the same
event.

For focus events, we track focus by token and not by the window, so when
there is more than one window for a focused token, we arbitrarily pick
the top-most window to associate events with for tracing.

Bug: 210460522
Test: atest inputflinger_tests
Change-Id: I47744cbd677cc74e26a102c50a2c11c68bc8aa89
diff --git a/services/inputflinger/dispatcher/InputDispatcher.h b/services/inputflinger/dispatcher/InputDispatcher.h
index 90a5250..f2fd0ca 100644
--- a/services/inputflinger/dispatcher/InputDispatcher.h
+++ b/services/inputflinger/dispatcher/InputDispatcher.h
@@ -615,18 +615,21 @@
             REQUIRES(mLock);
     void synthesizeCancelationEventsForMonitorsLocked(const CancelationOptions& options)
             REQUIRES(mLock);
-    void synthesizeCancelationEventsForConnectionLocked(
-            const std::shared_ptr<Connection>& connection, const CancelationOptions& options)
+    void synthesizeCancelationEventsForWindowLocked(const sp<gui::WindowInfoHandle>&,
+                                                    const CancelationOptions&,
+                                                    const std::shared_ptr<Connection>& = nullptr)
             REQUIRES(mLock);
+    // This is a convenience function used to generate cancellation for a connection without having
+    // to check whether it's a monitor or a window. For non-monitors, the window handle must not be
+    // null. Always prefer the "-ForWindow" method above when explicitly dealing with windows.
+    void synthesizeCancelationEventsForConnectionLocked(
+            const std::shared_ptr<Connection>& connection, const CancelationOptions& options,
+            const sp<gui::WindowInfoHandle>& window) REQUIRES(mLock);
 
     void synthesizePointerDownEventsForConnectionLocked(
             const nsecs_t downTime, const std::shared_ptr<Connection>& connection,
             ftl::Flags<InputTarget::Flags> targetFlags) REQUIRES(mLock);
 
-    void synthesizeCancelationEventsForWindowLocked(
-            const sp<android::gui::WindowInfoHandle>& windowHandle,
-            const CancelationOptions& options) REQUIRES(mLock);
-
     // Splitting motion events across windows. When splitting motion event for a target,
     // splitDownTime refers to the time of first 'down' event on that particular target
     std::unique_ptr<MotionEntry> splitMotionEvent(const MotionEntry& originalMotionEntry,