Move PointerController from sp to shared_ptr
Bug: 160010896
Test: atest PointerController_test, atest InputReader_test, manual usage
Change-Id: Ic43ab94ca76c736fde0d217efeab99b2afd6f622
Merged-In: Ic43ab94ca76c736fde0d217efeab99b2afd6f622
(cherry picked from commit 17db18e83fb3d4cdba0dcf25c2bcec350fab3d16)
diff --git a/services/inputflinger/include/InputReaderBase.h b/services/inputflinger/include/InputReaderBase.h
index f8d5351..0fa8787 100644
--- a/services/inputflinger/include/InputReaderBase.h
+++ b/services/inputflinger/include/InputReaderBase.h
@@ -335,7 +335,8 @@
virtual void getReaderConfiguration(InputReaderConfiguration* outConfig) = 0;
/* Gets a pointer controller associated with the specified cursor device (ie. a mouse). */
- virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) = 0;
+ virtual std::shared_ptr<PointerControllerInterface> 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 194c665..bffff88 100644
--- a/services/inputflinger/include/PointerControllerInterface.h
+++ b/services/inputflinger/include/PointerControllerInterface.h
@@ -33,7 +33,7 @@
* 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/InputReader.cpp b/services/inputflinger/reader/InputReader.cpp
index 657a134..d3441ac 100644
--- a/services/inputflinger/reader/InputReader.cpp
+++ b/services/inputflinger/reader/InputReader.cpp
@@ -390,8 +390,9 @@
}
}
-sp<PointerControllerInterface> InputReader::getPointerControllerLocked(int32_t deviceId) {
- sp<PointerControllerInterface> controller = mPointerController.promote();
+std::shared_ptr<PointerControllerInterface> InputReader::getPointerControllerLocked(
+ int32_t deviceId) {
+ std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
if (controller == nullptr) {
controller = mPolicy->obtainPointerController(deviceId);
mPointerController = controller;
@@ -401,7 +402,7 @@
}
void InputReader::updatePointerDisplayLocked() {
- sp<PointerControllerInterface> controller = mPointerController.promote();
+ std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
if (controller == nullptr) {
return;
}
@@ -424,7 +425,7 @@
}
void InputReader::fadePointerLocked() {
- sp<PointerControllerInterface> controller = mPointerController.promote();
+ std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
if (controller != nullptr) {
controller->fade(PointerControllerInterface::TRANSITION_GRADUAL);
}
@@ -725,7 +726,8 @@
mReader->fadePointerLocked();
}
-sp<PointerControllerInterface> InputReader::ContextImpl::getPointerController(int32_t deviceId) {
+std::shared_ptr<PointerControllerInterface> InputReader::ContextImpl::getPointerController(
+ int32_t deviceId) {
// lock is already held by the input loop
return mReader->getPointerControllerLocked(deviceId);
}
diff --git a/services/inputflinger/reader/include/InputReader.h b/services/inputflinger/reader/include/InputReader.h
index 693ec30..108b9c2 100644
--- a/services/inputflinger/reader/include/InputReader.h
+++ b/services/inputflinger/reader/include/InputReader.h
@@ -17,19 +17,20 @@
#ifndef _UI_INPUTREADER_INPUT_READER_H
#define _UI_INPUTREADER_INPUT_READER_H
+#include <PointerControllerInterface.h>
+#include <utils/Condition.h>
+#include <utils/Mutex.h>
+
+#include <memory>
+#include <unordered_map>
+#include <vector>
+
#include "EventHub.h"
#include "InputListener.h"
#include "InputReaderBase.h"
#include "InputReaderContext.h"
#include "InputThread.h"
-#include <PointerControllerInterface.h>
-#include <utils/Condition.h>
-#include <utils/Mutex.h>
-
-#include <unordered_map>
-#include <vector>
-
namespace android {
class InputDevice;
@@ -104,7 +105,8 @@
virtual void disableVirtualKeysUntil(nsecs_t time) override;
virtual bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) override;
virtual void fadePointer() override;
- virtual sp<PointerControllerInterface> getPointerController(int32_t deviceId) override;
+ virtual std::shared_ptr<PointerControllerInterface> getPointerController(
+ int32_t deviceId) override;
virtual void requestTimeoutAtTime(nsecs_t when) override;
virtual int32_t bumpGeneration() override;
virtual void getExternalStylusDevices(std::vector<InputDeviceInfo>& outDevices) override;
@@ -160,8 +162,8 @@
void dispatchExternalStylusState(const StylusState& state);
// The PointerController that is shared among all the input devices that need it.
- wp<PointerControllerInterface> mPointerController;
- sp<PointerControllerInterface> getPointerControllerLocked(int32_t deviceId);
+ std::weak_ptr<PointerControllerInterface> mPointerController;
+ std::shared_ptr<PointerControllerInterface> getPointerControllerLocked(int32_t deviceId);
void updatePointerDisplayLocked();
void fadePointerLocked();
diff --git a/services/inputflinger/reader/include/InputReaderContext.h b/services/inputflinger/reader/include/InputReaderContext.h
index 85701e4..ffb8d8c 100644
--- a/services/inputflinger/reader/include/InputReaderContext.h
+++ b/services/inputflinger/reader/include/InputReaderContext.h
@@ -46,7 +46,7 @@
virtual bool shouldDropVirtualKey(nsecs_t now, int32_t keyCode, int32_t scanCode) = 0;
virtual void fadePointer() = 0;
- virtual sp<PointerControllerInterface> getPointerController(int32_t deviceId) = 0;
+ virtual std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) = 0;
virtual void requestTimeoutAtTime(nsecs_t when) = 0;
virtual int32_t bumpGeneration() = 0;
diff --git a/services/inputflinger/reader/mapper/CursorInputMapper.h b/services/inputflinger/reader/mapper/CursorInputMapper.h
index f65ac39..05bbb26 100644
--- a/services/inputflinger/reader/mapper/CursorInputMapper.h
+++ b/services/inputflinger/reader/mapper/CursorInputMapper.h
@@ -106,7 +106,7 @@
int32_t mOrientation;
- sp<PointerControllerInterface> mPointerController;
+ std::shared_ptr<PointerControllerInterface> mPointerController;
int32_t mButtonState;
nsecs_t mDownTime;
diff --git a/services/inputflinger/reader/mapper/TouchCursorInputMapperCommon.h b/services/inputflinger/reader/mapper/TouchCursorInputMapperCommon.h
index 2a3e263..a86443d 100644
--- a/services/inputflinger/reader/mapper/TouchCursorInputMapperCommon.h
+++ b/services/inputflinger/reader/mapper/TouchCursorInputMapperCommon.h
@@ -17,12 +17,13 @@
#ifndef _UI_INPUTREADER_TOUCH_CURSOR_INPUT_MAPPER_COMMON_H
#define _UI_INPUTREADER_TOUCH_CURSOR_INPUT_MAPPER_COMMON_H
+#include <input/DisplayViewport.h>
+#include <stdint.h>
+
#include "EventHub.h"
#include "InputListener.h"
#include "InputReaderContext.h"
-#include <stdint.h>
-
namespace android {
// --- Static Definitions ---
diff --git a/services/inputflinger/reader/mapper/TouchInputMapper.cpp b/services/inputflinger/reader/mapper/TouchInputMapper.cpp
index 99a572a..10b6a18 100644
--- a/services/inputflinger/reader/mapper/TouchInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/TouchInputMapper.cpp
@@ -765,7 +765,7 @@
mPointerController = getContext()->getPointerController(getDeviceId());
}
} 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 58bfc5c..7f811a0 100644
--- a/services/inputflinger/reader/mapper/TouchInputMapper.h
+++ b/services/inputflinger/reader/mapper/TouchInputMapper.h
@@ -376,7 +376,7 @@
nsecs_t mDownTime;
// The pointer controller, or null if the device is not a pointer.
- sp<PointerControllerInterface> mPointerController;
+ std::shared_ptr<PointerControllerInterface> mPointerController;
std::vector<VirtualKey> mVirtualKeys;
diff --git a/services/inputflinger/tests/InputReader_test.cpp b/services/inputflinger/tests/InputReader_test.cpp
index c457a15..18bd3d0 100644
--- a/services/inputflinger/tests/InputReader_test.cpp
+++ b/services/inputflinger/tests/InputReader_test.cpp
@@ -27,12 +27,13 @@
#include <TestInputListener.h>
#include <TouchInputMapper.h>
#include <UinputDevice.h>
-
#include <android-base/thread_annotations.h>
#include <gtest/gtest.h>
#include <inttypes.h>
#include <math.h>
+#include <memory>
+
namespace android {
using std::chrono_literals::operator""ms;
@@ -76,15 +77,14 @@
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;
@@ -176,7 +176,7 @@
std::condition_variable mDevicesChangedCondition;
InputReaderConfiguration mConfig;
- KeyedVector<int32_t, sp<FakePointerController> > mPointerControllers;
+ std::unordered_map<int32_t, std::shared_ptr<FakePointerController>> mPointerControllers;
std::vector<InputDeviceInfo> mInputDevices GUARDED_BY(mLock);
bool mInputDevicesChanged GUARDED_BY(mLock){false};
std::vector<DisplayViewport> mViewports;
@@ -256,8 +256,8 @@
void removeDisabledDevice(int32_t deviceId) { mConfig.disabledDevices.erase(deviceId); }
- void setPointerController(int32_t deviceId, const sp<FakePointerController>& controller) {
- mPointerControllers.add(deviceId, controller);
+ void setPointerController(int32_t deviceId, std::shared_ptr<FakePointerController> controller) {
+ mPointerControllers.insert_or_assign(deviceId, std::move(controller));
}
const InputReaderConfiguration* getReaderConfiguration() const {
@@ -318,8 +318,8 @@
*outConfig = mConfig;
}
- virtual sp<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
- return mPointerControllers.valueFor(deviceId);
+ virtual std::shared_ptr<PointerControllerInterface> obtainPointerController(int32_t deviceId) {
+ return mPointerControllers[deviceId];
}
virtual void notifyInputDevicesChanged(const std::vector<InputDeviceInfo>& inputDevices) {
@@ -847,7 +847,7 @@
bool mUpdateGlobalMetaStateWasCalled;
int32_t mGeneration;
int32_t mNextId;
- wp<PointerControllerInterface> mPointerController;
+ std::weak_ptr<PointerControllerInterface> mPointerController;
public:
FakeInputReaderContext(std::shared_ptr<EventHubInterface> eventHub,
@@ -876,7 +876,7 @@
}
void updatePointerDisplay() {
- sp<PointerControllerInterface> controller = mPointerController.promote();
+ std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
if (controller != nullptr) {
InputReaderConfiguration config;
mPolicy->getReaderConfiguration(&config);
@@ -913,8 +913,8 @@
virtual bool shouldDropVirtualKey(nsecs_t, int32_t, int32_t) { return false; }
- virtual sp<PointerControllerInterface> getPointerController(int32_t deviceId) {
- sp<PointerControllerInterface> controller = mPointerController.promote();
+ virtual std::shared_ptr<PointerControllerInterface> getPointerController(int32_t deviceId) {
+ std::shared_ptr<PointerControllerInterface> controller = mPointerController.lock();
if (controller == nullptr) {
controller = mPolicy->obtainPointerController(deviceId);
mPointerController = controller;
@@ -2348,9 +2348,9 @@
ASSERT_NEAR(distance, coords.getAxisValue(AMOTION_EVENT_AXIS_DISTANCE), EPSILON);
}
- static void assertPosition(const sp<FakePointerController>& 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);
}
@@ -3021,12 +3021,12 @@
protected:
static const int32_t TRACKBALL_MOVEMENT_THRESHOLD;
- sp<FakePointerController> mFakePointerController;
+ std::shared_ptr<FakePointerController> mFakePointerController;
virtual void SetUp() override {
InputMapperTest::SetUp();
- mFakePointerController = new FakePointerController();
+ mFakePointerController = std::make_shared<FakePointerController>();
mFakePolicy->setPointerController(mDevice->getId(), mFakePointerController);
}
@@ -3682,7 +3682,7 @@
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) {
@@ -3710,7 +3710,7 @@
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);
@@ -3749,7 +3749,7 @@
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.
@@ -3770,7 +3770,7 @@
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) {
@@ -3798,7 +3798,7 @@
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);
}
@@ -6806,7 +6806,8 @@
TEST_F(MultiTouchInputMapperTest, Process_Pointer_ShouldHandleDisplayId) {
// Setup for second display.
- sp<FakePointerController> fakePointerController = new FakePointerController();
+ std::shared_ptr<FakePointerController> fakePointerController =
+ std::make_shared<FakePointerController>();
fakePointerController->setBounds(0, 0, DISPLAY_WIDTH - 1, DISPLAY_HEIGHT - 1);
fakePointerController->setPosition(100, 200);
fakePointerController->setButtonState(0);
@@ -6866,7 +6867,8 @@
device2->reset(ARBITRARY_TIME);
// Setup PointerController.
- sp<FakePointerController> fakePointerController = new FakePointerController();
+ std::shared_ptr<FakePointerController> fakePointerController =
+ std::make_shared<FakePointerController>();
mFakePolicy->setPointerController(mDevice->getId(), fakePointerController);
mFakePolicy->setPointerController(SECOND_DEVICE_ID, fakePointerController);