SyncPointerCapture (5/n): Move Pointer Capture to InputDispatcher

This CL re-implements Pointer Capture in InputDispatcher, moving it away
for WindowManagerService.

The pipeline for Pointer Capture is changed so that requests to enable
and disable Pointer Capture from an app are redirected directly to
InputDispatcher by InputManagerService.

The new expected pipeline for Pointer Capture is as follows:
- App requests Pointer Capture through InputManagerService with its
window token.
- InputManagerService gets the InputWindowToken for the WindowToken, and
relays the Pointer Capture request to InputDispatcher.
- Pointer Capture is enabled in InputFlinger:
  1. InputDispatcher receives the Pointer Capture request, and ignores
  requests from windows that are not focused.
    InputDispatcherInterface::requestPointerCapture(windowToken,
        enabled)
  2. InputDispatcher sets the Pointer Capture state through its policy,
  which sends a configuration change to InputReader.
    InputDispatcherPolicyInterface::setPointerCapture(enabled)
  3. InputReader changes its configuration to enable/disable Pointer
  Capture, and sends notifyPointerCaptureChanged to InputDispatcher.
    InputListener::notifyPointerCaptureChanged(args)
  4. The Pointer Capture change notification is sent to the input
  channel.
- App receives a Pointer Capture change event through its InputChannel.

The window that has Pointer Capture is tracked in InputDispatcher. If
the window loses focus, Pointer Capture is disabled immediately, and
InputDispatcher synthesizes a Pointer Capture change event to dispatch
to the app before it sends the event signalling the focus loss.

Bug: 141749603
Test: atest inputflinger_tests
Test: atest PointerCaptureTest (CTS)
Test: manual: Pointer Capture works

Change-Id: Ie27f6e279af9b0d844160fb146476e1812fb02b3
diff --git a/services/inputflinger/dispatcher/InputDispatcher.h b/services/inputflinger/dispatcher/InputDispatcher.h
index 8f58785..5d37645 100644
--- a/services/inputflinger/dispatcher/InputDispatcher.h
+++ b/services/inputflinger/dispatcher/InputDispatcher.h
@@ -127,6 +127,7 @@
             int32_t displayId, bool isGestureMonitor, const std::string& name) override;
     virtual status_t removeInputChannel(const sp<IBinder>& connectionToken) override;
     virtual status_t pilferPointers(const sp<IBinder>& token) override;
+    virtual void requestPointerCapture(const sp<IBinder>& windowToken, bool enabled) override;
 
     std::array<uint8_t, 32> sign(const VerifiedInputEvent& event) const;
 
@@ -138,6 +139,7 @@
         DISABLED,
         BLOCKED,
         STALE,
+        NO_POINTER_CAPTURE,
     };
 
     enum class FocusResult {
@@ -351,6 +353,21 @@
     // Top focused display.
     int32_t mFocusedDisplayId GUARDED_BY(mLock);
 
+    // Whether the focused window on the focused display has requested Pointer Capture.
+    // The state of this variable should always be in sync with the state of Pointer Capture in the
+    // policy, which is updated through setPointerCaptureLocked(enabled).
+    bool mFocusedWindowRequestedPointerCapture GUARDED_BY(mLock);
+
+    // The window token that has Pointer Capture.
+    // This should be in sync with PointerCaptureChangedEvents dispatched to the input channel.
+    sp<IBinder> mWindowTokenWithPointerCapture GUARDED_BY(mLock);
+
+    // Disable Pointer Capture as a result of loss of window focus.
+    void disablePointerCaptureForcedLocked() REQUIRES(mLock);
+
+    // Set the Pointer Capture state in the Policy.
+    void setPointerCaptureLocked(bool enabled) REQUIRES(mLock);
+
     // Dispatcher state at time of last ANR.
     std::string mLastAnrState GUARDED_BY(mLock);
 
@@ -370,6 +387,9 @@
                               DropReason* dropReason, nsecs_t* nextWakeupTime) REQUIRES(mLock);
     void dispatchFocusLocked(nsecs_t currentTime, std::shared_ptr<FocusEntry> entry)
             REQUIRES(mLock);
+    void dispatchPointerCaptureChangedLocked(
+            nsecs_t currentTime, const std::shared_ptr<PointerCaptureChangedEntry>& entry,
+            DropReason& dropReason) REQUIRES(mLock);
     void dispatchEventLocked(nsecs_t currentTime, std::shared_ptr<EventEntry> entry,
                              const std::vector<InputTarget>& inputTargets) REQUIRES(mLock);
 
@@ -533,6 +553,7 @@
     void logDispatchStateLocked() REQUIRES(mLock);
     std::string dumpFocusedWindowsLocked() REQUIRES(mLock);
     std::string dumpPendingFocusRequestsLocked() REQUIRES(mLock);
+    std::string dumpPointerCaptureStateLocked() REQUIRES(mLock);
 
     // Registration.
     void removeMonitorChannelLocked(const sp<IBinder>& connectionToken) REQUIRES(mLock);
@@ -575,6 +596,7 @@
     void doInterceptKeyBeforeDispatchingLockedInterruptible(CommandEntry* commandEntry)
             REQUIRES(mLock);
     void doDispatchCycleFinishedLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
+    void doSetPointerCaptureLockedInterruptible(CommandEntry* commandEntry) REQUIRES(mLock);
     bool afterKeyEventLockedInterruptible(const sp<Connection>& connection,
                                           DispatchEntry* dispatchEntry, KeyEntry& keyEntry,
                                           bool handled) REQUIRES(mLock);