From a512bfda6d22f0ad8d523a9ea50880a9a89ab463 Mon Sep 17 00:00:00 2001 From: Michael Wright Date: Fri, 26 Jun 2020 20:51:44 +0100 Subject: Move PointerController from sp to shared_ptr Bug: 160010896 Test: atest PointerController_test, atest InputReader_test, manual usage Change-Id: Ic43ab94ca76c736fde0d217efeab99b2afd6f622 Merged-In: Ic43ab94ca76c736fde0d217efeab99b2afd6f622 --- services/inputflinger/include/InputReaderBase.h | 3 +- .../include/PointerControllerInterface.h | 2 +- .../inputflinger/reader/mapper/CursorInputMapper.h | 2 +- .../reader/mapper/TouchCursorInputMapperCommon.h | 4 +-- .../reader/mapper/TouchInputMapper.cpp | 2 +- .../inputflinger/reader/mapper/TouchInputMapper.h | 2 +- services/inputflinger/tests/InputReader_test.cpp | 42 +++++++++++----------- 7 files changed, 30 insertions(+), 27 deletions(-) diff --git a/services/inputflinger/include/InputReaderBase.h b/services/inputflinger/include/InputReaderBase.h index 86d91a92ce..ef567c7b71 100644 --- a/services/inputflinger/include/InputReaderBase.h +++ b/services/inputflinger/include/InputReaderBase.h @@ -332,7 +332,8 @@ public: virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0; /* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */ - virtual sp obtainPointerController(int32_t deviceId) = 0; + virtual std::shared_ptr obtainPointerController( + int32_t deviceId) = 0; /* Notifies the input reader policy that some input devices have changed * and provides information about all current input devices. diff --git a/services/inputflinger/include/PointerControllerInterface.h b/services/inputflinger/include/PointerControllerInterface.h index 194c66547f..bffff881b2 100644 --- a/services/inputflinger/include/PointerControllerInterface.h +++ b/services/inputflinger/include/PointerControllerInterface.h @@ -33,7 +33,7 @@ namespace android { * The pointer controller is responsible for providing synchronization and for tracking * display orientation changes if needed. */ -class PointerControllerInterface : public virtual RefBase { +class PointerControllerInterface { protected: PointerControllerInterface() { } virtual ~PointerControllerInterface() { } diff --git a/services/inputflinger/reader/mapper/CursorInputMapper.h b/services/inputflinger/reader/mapper/CursorInputMapper.h index 05b696789d..a1e9f100ac 100644 --- a/services/inputflinger/reader/mapper/CursorInputMapper.h +++ b/services/inputflinger/reader/mapper/CursorInputMapper.h @@ -107,7 +107,7 @@ private: int32_t mOrientation; - sp mPointerController; + std::shared_ptr mPointerController; int32_t mButtonState; nsecs_t mDownTime; diff --git a/services/inputflinger/reader/mapper/TouchCursorInputMapperCommon.h b/services/inputflinger/reader/mapper/TouchCursorInputMapperCommon.h index efa3d6d2b2..ca18edca9f 100644 --- a/services/inputflinger/reader/mapper/TouchCursorInputMapperCommon.h +++ b/services/inputflinger/reader/mapper/TouchCursorInputMapperCommon.h @@ -17,12 +17,12 @@ #ifndef _UI_INPUTREADER_TOUCH_CURSOR_INPUT_MAPPER_COMMON_H #define _UI_INPUTREADER_TOUCH_CURSOR_INPUT_MAPPER_COMMON_H +#include + #include "EventHub.h" #include "InputListener.h" #include "InputReaderContext.h" -#include - namespace android { // --- Static Definitions --- diff --git a/services/inputflinger/reader/mapper/TouchInputMapper.cpp b/services/inputflinger/reader/mapper/TouchInputMapper.cpp index fb35bee8a6..b1385aa7c5 100644 --- a/services/inputflinger/reader/mapper/TouchInputMapper.cpp +++ b/services/inputflinger/reader/mapper/TouchInputMapper.cpp @@ -778,7 +778,7 @@ void TouchInputMapper::configureSurface(nsecs_t when, bool* outResetNeeded) { mPointerController->setDisplayViewport(defaultViewport.value_or(mViewport)); } } else { - mPointerController.clear(); + mPointerController.reset(); } if (viewportChanged || deviceModeChanged) { diff --git a/services/inputflinger/reader/mapper/TouchInputMapper.h b/services/inputflinger/reader/mapper/TouchInputMapper.h index d14812aecd..b445981a9c 100644 --- a/services/inputflinger/reader/mapper/TouchInputMapper.h +++ b/services/inputflinger/reader/mapper/TouchInputMapper.h @@ -444,7 +444,7 @@ protected: nsecs_t mDownTime; // The pointer controller, or null if the device is not a pointer. - sp mPointerController; + std::shared_ptr mPointerController; std::vector mVirtualKeys; diff --git a/services/inputflinger/tests/InputReader_test.cpp b/services/inputflinger/tests/InputReader_test.cpp index 7442e68e17..8e80b63727 100644 --- a/services/inputflinger/tests/InputReader_test.cpp +++ b/services/inputflinger/tests/InputReader_test.cpp @@ -24,11 +24,12 @@ #include #include #include - #include #include #include +#include +#include namespace android { @@ -68,15 +69,14 @@ class FakePointerController : public PointerControllerInterface { int32_t mButtonState; int32_t mDisplayId; -protected: - virtual ~FakePointerController() { } - public: FakePointerController() : mHaveBounds(false), mMinX(0), mMinY(0), mMaxX(0), mMaxY(0), mX(0), mY(0), mButtonState(0), mDisplayId(ADISPLAY_ID_DEFAULT) { } + virtual ~FakePointerController() {} + void setBounds(float minX, float minY, float maxX, float maxY) { mHaveBounds = true; mMinX = minX; @@ -165,7 +165,7 @@ private: class FakeInputReaderPolicy : public InputReaderPolicyInterface { InputReaderConfiguration mConfig; - KeyedVector > mPointerControllers; + std::unordered_map> mPointerControllers; std::vector mInputDevices; std::vector mViewports; TouchAffineTransformation transform; @@ -226,8 +226,8 @@ public: } } - void setPointerController(int32_t deviceId, const sp& controller) { - mPointerControllers.add(deviceId, controller); + void setPointerController(int32_t deviceId, std::shared_ptr controller) { + mPointerControllers.insert_or_assign(deviceId, std::move(controller)); } const InputReaderConfiguration* getReaderConfiguration() const { @@ -288,8 +288,8 @@ private: *outConfig = mConfig; } - virtual sp obtainPointerController(int32_t deviceId) { - return mPointerControllers.valueFor(deviceId); + virtual std::shared_ptr obtainPointerController(int32_t deviceId) { + return mPointerControllers[deviceId]; } virtual void notifyInputDevicesChanged(const std::vector& inputDevices) { @@ -1887,9 +1887,9 @@ protected: ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON); } - static void assertPosition(const sp& controller, float x, float y) { + static void assertPosition(const FakePointerController& controller, float x, float y) { float actualX, actualY; - controller->getPosition(&actualX, &actualY); + controller.getPosition(&actualX, &actualY); ASSERT_NEAR(x, actualX, 1); ASSERT_NEAR(y, actualY, 1); } @@ -2395,12 +2395,12 @@ class CursorInputMapperTest : public InputMapperTest { protected: static const int32_t TRACKBALL_MOVEMENT_THRESHOLD; - sp mFakePointerController; + std::shared_ptr mFakePointerController; virtual void SetUp() { InputMapperTest::SetUp(); - mFakePointerController = new FakePointerController(); + mFakePointerController = std::make_shared(); mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController); } @@ -3067,7 +3067,7 @@ TEST_F(CursorInputMapperTest, Process_WhenModeIsPointer_ShouldMoveThePointerArou ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action); ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); - ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f)); + ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f)); } TEST_F(CursorInputMapperTest, Process_PointerCapture) { @@ -3096,7 +3096,7 @@ TEST_F(CursorInputMapperTest, Process_PointerCapture) { ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action); ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 10.0f, 20.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); - ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f)); + ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f)); // Button press. process(mapper, ARBITRARY_TIME, EV_KEY, BTN_MOUSE, 1); @@ -3135,7 +3135,7 @@ TEST_F(CursorInputMapperTest, Process_PointerCapture) { ASSERT_EQ(AMOTION_EVENT_ACTION_MOVE, args.action); ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 30.0f, 40.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); - ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 100.0f, 200.0f)); + ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 100.0f, 200.0f)); // Disable pointer capture and check that the device generation got bumped // and events are generated the usual way. @@ -3156,7 +3156,7 @@ TEST_F(CursorInputMapperTest, Process_PointerCapture) { ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action); ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); - ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f)); + ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f)); } TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) { @@ -3185,7 +3185,7 @@ TEST_F(CursorInputMapperTest, Process_ShouldHandleDisplayId) { ASSERT_EQ(AMOTION_EVENT_ACTION_HOVER_MOVE, args.action); ASSERT_NO_FATAL_FAILURE(assertPointerCoords(args.pointerCoords[0], 110.0f, 220.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f)); - ASSERT_NO_FATAL_FAILURE(assertPosition(mFakePointerController, 110.0f, 220.0f)); + ASSERT_NO_FATAL_FAILURE(assertPosition(*mFakePointerController, 110.0f, 220.0f)); ASSERT_EQ(SECOND_DISPLAY_ID, args.displayId); } @@ -6340,7 +6340,8 @@ TEST_F(MultiTouchInputMapperTest, Viewports_Fallback) { TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) { // Setup for second display. - sp fakePointerController = new FakePointerController(); + std::shared_ptr fakePointerController = + std::make_shared(); fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1); fakePointerController->setPosition(100, 200); fakePointerController->setButtonState(0); @@ -6401,7 +6402,8 @@ TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShowTouches) { device2->reset(ARBITRARY_TIME); // Setup PointerController. - sp fakePointerController = new FakePointerController(); + std::shared_ptr fakePointerController = + std::make_shared(); mFakePolicy->setPointerController(mDevice->getId(), fakePointerController); mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController); -- cgit v1.2.3-59-g8ed1b