From a09a94d83e421442f9c02166d3fde67c39a13a8c Mon Sep 17 00:00:00 2001 From: Arpit Singh Date: Tue, 26 Nov 2024 15:50:17 +0000 Subject: 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 --- libs/input/MouseCursorController.cpp | 46 ++++++++++++++++++------------------ libs/input/MouseCursorController.h | 11 ++++----- libs/input/PointerController.cpp | 14 ++++------- libs/input/PointerController.h | 8 +++---- 4 files changed, 37 insertions(+), 42 deletions(-) (limited to 'libs/input') diff --git a/libs/input/MouseCursorController.cpp b/libs/input/MouseCursorController.cpp index 28d96e3e5bb1..65663d065b8e 100644 --- a/libs/input/MouseCursorController.cpp +++ b/libs/input/MouseCursorController.cpp @@ -49,8 +49,7 @@ MouseCursorController::MouseCursorController(PointerControllerContext& context) 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 @@ MouseCursorController::~MouseCursorController() { 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 @@ FloatPoint MouseCursorController::move(float deltaX, float deltaY) { // 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 @@ FloatRect MouseCursorController::getBoundsLocked() REQUIRES(mLock) { }; } -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 @@ void MouseCursorController::setDisplayViewport(const DisplayViewport& viewport, 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 @@ void MouseCursorController::setDisplayViewport(const DisplayViewport& viewport, // 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 @@ void MouseCursorController::updatePointerLocked() REQUIRES(mLock) { 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 e14a55d938e6..7c674b53d620 100644 --- a/libs/input/MouseCursorController.h +++ b/libs/input/MouseCursorController.h @@ -41,9 +41,9 @@ public: ~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 @@ private: nsecs_t lastFrameUpdatedTime; int32_t pointerFadeDirection; - float pointerX; - float pointerY; + vec2 pointerPosition; float pointerAlpha; sp pointerSprite; SpriteIcon pointerIcon; @@ -101,7 +100,7 @@ private: } 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 a713f1d1ed4d..0b81211ee666 100644 --- a/libs/input/PointerController.cpp +++ b/libs/input/PointerController.cpp @@ -138,7 +138,7 @@ std::mutex& PointerController::getLock() const { 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 @@ FloatPoint PointerController::move(float deltaX, float deltaY) { } 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 @@ void PointerController::setPosition(float x, float y) { 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 8b33190cd35f..afd7215c7fba 100644 --- a/libs/input/PointerController.h +++ b/libs/input/PointerController.h @@ -51,9 +51,9 @@ public: ~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 @@ public: ~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 { -- cgit v1.2.3-59-g8ed1b