Let InputReaderContext hold a single PointerController

InputMappers obtain the pointer controller directly from the policy,
which, when we support multiple pointer controller, makes it natural to
have one pointer controller for an input device.

Rather than that, we want to build a system that makes it more natural
to have one pointer per display. To give more control over the
management of pointers, we make it so that InputReaderContext is
responsible for managing the pointer controller.

Bug: 146385350
Test: atest inputflinger_tests
Change-Id: I0b9276ed4a7f55f04f910dd484ecc7f767b8723b
diff --git a/services/inputflinger/tests/InputReader_test.cpp b/services/inputflinger/tests/InputReader_test.cpp
index 578605f..752213a 100644
--- a/services/inputflinger/tests/InputReader_test.cpp
+++ b/services/inputflinger/tests/InputReader_test.cpp
@@ -830,6 +830,7 @@
     bool mUpdateGlobalMetaStateWasCalled;
     int32_t mGeneration;
     uint32_t mNextSequenceNum;
+    wp<PointerControllerInterface> mPointerController;
 
 public:
     FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
@@ -857,6 +858,18 @@
         return mGeneration;
     }
 
+    void updatePointerDisplay() {
+        sp<PointerControllerInterface> controller = mPointerController.promote();
+        if (controller != nullptr) {
+            InputReaderConfiguration config;
+            mPolicy->getReaderConfiguration(&config);
+            auto viewport = config.getDisplayViewportById(config.defaultPointerDisplayId);
+            if (viewport) {
+                controller->setDisplayViewport(*viewport);
+            }
+        }
+    }
+
 private:
     virtual void updateGlobalMetaState() {
         mUpdateGlobalMetaStateWasCalled = true;
@@ -883,6 +896,16 @@
 
     virtual bool shouldDropVirtualKey(nsecs_t, int32_t, int32_t) { return false; }
 
+    virtual sp<PointerControllerInterface> getPointerController(int32_t deviceId) {
+        sp<PointerControllerInterface> controller = mPointerController.promote();
+        if (controller == nullptr) {
+            controller = mPolicy->obtainPointerController(deviceId);
+            mPointerController = controller;
+            updatePointerDisplay();
+        }
+        return controller;
+    }
+
     virtual void fadePointer() {
     }
 
@@ -1965,6 +1988,9 @@
     }
 
     void configureDevice(uint32_t changes) {
+        if (!changes || (changes & InputReaderConfiguration::CHANGE_DISPLAY_INFO)) {
+            mFakeContext->updatePointerDisplay();
+        }
         mDevice->configure(ARBITRARY_TIME, mFakePolicy->getReaderConfiguration(), changes);
     }