Refactor to replace FloatPoint by vec2
Using FloatPoint requires unnecessory conversion between vec2 and
FloatPoint, which is inefficient.
This CL replaces FloatPoint by vec2 everywhere.
Test: atest inputflinger_tests
Bug: 245989146
Flag: EXEMPT refactor
Change-Id: I0bf9cd35392bff424ec65bbaa43918176d31de37
diff --git a/libs/input/MouseCursorController.cpp b/libs/input/MouseCursorController.cpp
index 28d96e3..65663d0 100644
--- a/libs/input/MouseCursorController.cpp
+++ b/libs/input/MouseCursorController.cpp
@@ -49,8 +49,7 @@
mLocked.lastFrameUpdatedTime = 0;
mLocked.pointerFadeDirection = 0;
- mLocked.pointerX = 0;
- mLocked.pointerY = 0;
+ mLocked.pointerPosition = {0, 0};
mLocked.pointerAlpha = 0.0f; // pointer is initially faded
mLocked.pointerSprite = mContext.getSpriteController().createSprite();
mLocked.updatePointerIcon = false;
@@ -66,11 +65,11 @@
mLocked.pointerSprite.clear();
}
-FloatPoint MouseCursorController::move(float deltaX, float deltaY) {
+vec2 MouseCursorController::move(vec2 delta) {
#if DEBUG_MOUSE_CURSOR_UPDATES
ALOGD("Move pointer by deltaX=%0.3f, deltaY=%0.3f", deltaX, deltaY);
#endif
- if (deltaX == 0.0f && deltaY == 0.0f) {
+ if (delta.x == 0.0f && delta.y == 0.0f) {
return {0, 0};
}
@@ -78,19 +77,19 @@
// if there's another display on the other side of the transition. At this point we still need
// to move the cursor to the boundary.
std::scoped_lock lock(mLock);
- const FloatPoint position{mLocked.pointerX + deltaX, mLocked.pointerY + deltaY};
- setPositionLocked(position.x, position.y);
+ const vec2 targetPosition = mLocked.pointerPosition + delta;
+ setPositionLocked(targetPosition);
// The amount of the delta that was not consumed as a result of the cursor
// hitting the edge of the display.
- return {position.x - mLocked.pointerX, position.y - mLocked.pointerY};
+ return targetPosition - mLocked.pointerPosition;
}
-void MouseCursorController::setPosition(float x, float y) {
+void MouseCursorController::setPosition(vec2 position) {
#if DEBUG_MOUSE_CURSOR_UPDATES
ALOGD("Set pointer position to x=%0.3f, y=%0.3f", x, y);
#endif
std::scoped_lock lock(mLock);
- setPositionLocked(x, y);
+ setPositionLocked(position);
}
FloatRect MouseCursorController::getBoundsLocked() REQUIRES(mLock) {
@@ -105,21 +104,21 @@
};
}
-void MouseCursorController::setPositionLocked(float x, float y) REQUIRES(mLock) {
+void MouseCursorController::setPositionLocked(vec2 position) REQUIRES(mLock) {
const auto& v = mLocked.viewport;
if (!v.isValid()) return;
const FloatRect bounds = getBoundsLocked();
- mLocked.pointerX = std::max(bounds.left, std::min(bounds.right, x));
- mLocked.pointerY = std::max(bounds.top, std::min(bounds.bottom, y));
+ mLocked.pointerPosition.x = std::max(bounds.left, std::min(bounds.right, position.x));
+ mLocked.pointerPosition.y = std::max(bounds.top, std::min(bounds.bottom, position.y));
updatePointerLocked();
}
-FloatPoint MouseCursorController::getPosition() const {
+vec2 MouseCursorController::getPosition() const {
std::scoped_lock lock(mLock);
- return {mLocked.pointerX, mLocked.pointerY};
+ return mLocked.pointerPosition;
}
ui::LogicalDisplayId MouseCursorController::getDisplayId() const {
@@ -221,20 +220,21 @@
if (viewport.isValid()) {
// Use integer coordinates as the starting point for the cursor location.
// We usually expect display sizes to be even numbers, so the flooring is precautionary.
- mLocked.pointerX = std::floor((viewport.logicalLeft + viewport.logicalRight) / 2);
- mLocked.pointerY = std::floor((viewport.logicalTop + viewport.logicalBottom) / 2);
+ mLocked.pointerPosition.x =
+ std::floor((viewport.logicalLeft + viewport.logicalRight) / 2);
+ mLocked.pointerPosition.y =
+ std::floor((viewport.logicalTop + viewport.logicalBottom) / 2);
// Reload icon resources for density may be changed.
loadResourcesLocked(getAdditionalMouseResources);
} else {
- mLocked.pointerX = 0;
- mLocked.pointerY = 0;
+ mLocked.pointerPosition = {0, 0};
}
} else if (oldViewport.orientation != viewport.orientation) {
// Apply offsets to convert from the pixel top-left corner position to the pixel center.
// This creates an invariant frame of reference that we can easily rotate when
// taking into account that the pointer may be located at fractional pixel offsets.
- float x = mLocked.pointerX + 0.5f;
- float y = mLocked.pointerY + 0.5f;
+ float x = mLocked.pointerPosition.x + 0.5f;
+ float y = mLocked.pointerPosition.y + 0.5f;
float temp;
// Undo the previous rotation.
@@ -279,8 +279,8 @@
// Apply offsets to convert from the pixel center to the pixel top-left corner position
// and save the results.
- mLocked.pointerX = x - 0.5f;
- mLocked.pointerY = y - 0.5f;
+ mLocked.pointerPosition.x = x - 0.5f;
+ mLocked.pointerPosition.y = y - 0.5f;
}
updatePointerLocked();
@@ -366,7 +366,7 @@
spriteController.openTransaction();
mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER);
- mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY);
+ mLocked.pointerSprite->setPosition(mLocked.pointerPosition.x, mLocked.pointerPosition.y);
mLocked.pointerSprite->setDisplayId(mLocked.viewport.displayId);
mLocked.pointerSprite->setSkipScreenshot(mLocked.skipScreenshot);
diff --git a/libs/input/MouseCursorController.h b/libs/input/MouseCursorController.h
index e14a55d..7c674b5 100644
--- a/libs/input/MouseCursorController.h
+++ b/libs/input/MouseCursorController.h
@@ -41,9 +41,9 @@
~MouseCursorController();
// Move the pointer and return unconsumed delta
- FloatPoint move(float deltaX, float deltaY);
- void setPosition(float x, float y);
- FloatPoint getPosition() const;
+ vec2 move(vec2 delta);
+ void setPosition(vec2 position);
+ vec2 getPosition() const;
ui::LogicalDisplayId getDisplayId() const;
void fade(PointerControllerInterface::Transition transition);
void unfade(PointerControllerInterface::Transition transition);
@@ -81,8 +81,7 @@
nsecs_t lastFrameUpdatedTime;
int32_t pointerFadeDirection;
- float pointerX;
- float pointerY;
+ vec2 pointerPosition;
float pointerAlpha;
sp<Sprite> pointerSprite;
SpriteIcon pointerIcon;
@@ -101,7 +100,7 @@
} mLocked GUARDED_BY(mLock);
- void setPositionLocked(float x, float y);
+ void setPositionLocked(vec2 position);
void updatePointerLocked();
diff --git a/libs/input/PointerController.cpp b/libs/input/PointerController.cpp
index a713f1d..0b81211 100644
--- a/libs/input/PointerController.cpp
+++ b/libs/input/PointerController.cpp
@@ -138,7 +138,7 @@
return mDisplayInfoListener->mLock;
}
-FloatPoint PointerController::move(float deltaX, float deltaY) {
+vec2 PointerController::move(float deltaX, float deltaY) {
const ui::LogicalDisplayId displayId = mCursorController.getDisplayId();
ui::Transform transform;
{
@@ -147,10 +147,7 @@
}
const vec2 transformed = transformWithoutTranslation(transform, {deltaX, deltaY});
-
- const FloatPoint unconsumedDelta = mCursorController.move(transformed.x, transformed.y);
- return FloatPoint(transformWithoutTranslation(transform.inverse(),
- {unconsumedDelta.x, unconsumedDelta.y}));
+ return transformWithoutTranslation(transform.inverse(), mCursorController.move(transformed));
}
void PointerController::setPosition(float x, float y) {
@@ -161,16 +158,15 @@
const auto& transform = getTransformForDisplayLocked(displayId);
transformed = transform.transform(x, y);
}
- mCursorController.setPosition(transformed.x, transformed.y);
+ mCursorController.setPosition(transformed);
}
-FloatPoint PointerController::getPosition() const {
+vec2 PointerController::getPosition() const {
const ui::LogicalDisplayId displayId = mCursorController.getDisplayId();
const auto p = mCursorController.getPosition();
{
std::scoped_lock lock(getLock());
- const auto& transform = getTransformForDisplayLocked(displayId);
- return FloatPoint{transform.inverse().transform(p.x, p.y)};
+ return getTransformForDisplayLocked(displayId).inverse().transform(p.x, p.y);
}
}
diff --git a/libs/input/PointerController.h b/libs/input/PointerController.h
index 8b33190..afd7215 100644
--- a/libs/input/PointerController.h
+++ b/libs/input/PointerController.h
@@ -51,9 +51,9 @@
~PointerController() override;
- FloatPoint move(float deltaX, float deltaY) override;
+ vec2 move(float deltaX, float deltaY) override;
void setPosition(float x, float y) override;
- FloatPoint getPosition() const override;
+ vec2 getPosition() const override;
ui::LogicalDisplayId getDisplayId() const override;
void fade(Transition transition) override;
void unfade(Transition transition) override;
@@ -166,13 +166,13 @@
~TouchPointerController() override;
- FloatPoint move(float, float) override {
+ vec2 move(float, float) override {
LOG_ALWAYS_FATAL("Should not be called");
}
void setPosition(float, float) override {
LOG_ALWAYS_FATAL("Should not be called");
}
- FloatPoint getPosition() const override {
+ vec2 getPosition() const override {
LOG_ALWAYS_FATAL("Should not be called");
}
ui::LogicalDisplayId getDisplayId() const override {
diff --git a/services/core/jni/com_android_server_input_InputManagerService.cpp b/services/core/jni/com_android_server_input_InputManagerService.cpp
index 9430194..5a45730 100644
--- a/services/core/jni/com_android_server_input_InputManagerService.cpp
+++ b/services/core/jni/com_android_server_input_InputManagerService.cpp
@@ -362,7 +362,7 @@
void setMotionClassifierEnabled(bool enabled);
std::optional<std::string> getBluetoothAddress(int32_t deviceId);
void setStylusButtonMotionEventsEnabled(bool enabled);
- FloatPoint getMouseCursorPosition(ui::LogicalDisplayId displayId);
+ vec2 getMouseCursorPosition(ui::LogicalDisplayId displayId);
void setStylusPointerIconEnabled(bool enabled);
void setInputMethodConnectionIsActive(bool isActive);
void setKeyRemapping(const std::map<int32_t, int32_t>& keyRemapping);
@@ -441,7 +441,7 @@
std::shared_ptr<PointerControllerInterface> createPointerController(
PointerControllerInterface::ControllerType type) override;
void notifyPointerDisplayIdChanged(ui::LogicalDisplayId displayId,
- const FloatPoint& position) override;
+ const vec2& position) override;
void notifyMouseCursorFadedOnTyping() override;
/* --- InputFilterPolicyInterface implementation --- */
@@ -871,7 +871,7 @@
}
void NativeInputManager::notifyPointerDisplayIdChanged(ui::LogicalDisplayId pointerDisplayId,
- const FloatPoint& position) {
+ const vec2& position) {
// Notify the Reader so that devices can be reconfigured.
{ // acquire lock
std::scoped_lock _l(mLock);
@@ -2023,7 +2023,7 @@
InputReaderConfiguration::Change::STYLUS_BUTTON_REPORTING);
}
-FloatPoint NativeInputManager::getMouseCursorPosition(ui::LogicalDisplayId displayId) {
+vec2 NativeInputManager::getMouseCursorPosition(ui::LogicalDisplayId displayId) {
return mInputManager->getChoreographer().getMouseCursorPosition(displayId);
}