Loading include/input/Input.h +3 −0 Original line number Diff line number Diff line Loading @@ -803,6 +803,9 @@ public: static vec2 calculateTransformedXY(uint32_t source, const ui::Transform&, const vec2& xy); static float calculateTransformedAxisValue(int32_t axis, uint32_t source, const ui::Transform&, const PointerCoords&); protected: int32_t mAction; int32_t mActionButton; Loading libs/input/Input.cpp +30 −50 Original line number Diff line number Diff line Loading @@ -56,13 +56,8 @@ float transformAngle(const ui::Transform& transform, float angleRadians) { transformedPoint.y -= origin.y; // Derive the transformed vector's clockwise angle from vertical. float result = atan2f(transformedPoint.x, -transformedPoint.y); if (result < -M_PI_2) { result += M_PI; } else if (result > M_PI_2) { result -= M_PI; } return result; // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API. return atan2f(transformedPoint.x, -transformedPoint.y); } vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) { Loading Loading @@ -498,44 +493,14 @@ const PointerCoords* MotionEvent::getHistoricalRawPointerCoords( float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const { const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex); if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) { const vec2 xy = calculateTransformedXY(mSource, mRawTransform, coords->getXYValue()); static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1); return xy[axis]; } if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) { const vec2 relativeXy = transformWithoutTranslation(mRawTransform, {coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)}); return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y; } return coords->getAxisValue(axis); const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex); return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords); } float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const { const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex); if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) { const vec2 xy = calculateTransformedXY(mSource, mTransform, coords->getXYValue()); static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1); return xy[axis]; } if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) { const vec2 relativeXy = transformWithoutTranslation(mTransform, {coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)}); return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y; } return coords->getAxisValue(axis); const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex); return calculateTransformedAxisValue(axis, mSource, mTransform, coords); } ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const { Loading Loading @@ -574,15 +539,6 @@ void MotionEvent::transform(const std::array<float, 9>& matrix) { ui::Transform newTransform; newTransform.set(matrix); mTransform = newTransform * mTransform; // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the // orientation angle is not affected by the initial transformation set in the MotionEvent. std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(), [&newTransform](PointerCoords& c) { float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(newTransform, orientation)); }); } void MotionEvent::applyTransform(const std::array<float, 9>& matrix) { Loading Loading @@ -814,6 +770,30 @@ vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& t : transform.transform(xy); } float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source, const ui::Transform& transform, const PointerCoords& coords) { if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) { const vec2 xy = calculateTransformedXY(source, transform, coords.getXYValue()); static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1); return xy[axis]; } if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) { const vec2 relativeXy = transformWithoutTranslation(transform, {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)}); return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y; } if (axis == AMOTION_EVENT_AXIS_ORIENTATION) { return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); } return coords.getAxisValue(axis); } // --- FocusEvent --- void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) { Loading libs/input/tests/InputEvent_test.cpp +15 −7 Original line number Diff line number Diff line Loading @@ -444,12 +444,19 @@ void MotionEventTest::assertEqualsEventWithHistory(const MotionEvent* event) { ASSERT_EQ(217, event->getToolMinor(0)); ASSERT_EQ(227, event->getToolMinor(1)); ASSERT_EQ(18, event->getHistoricalOrientation(0, 0)); ASSERT_EQ(28, event->getHistoricalOrientation(1, 0)); ASSERT_EQ(118, event->getHistoricalOrientation(0, 1)); ASSERT_EQ(128, event->getHistoricalOrientation(1, 1)); ASSERT_EQ(218, event->getOrientation(0)); ASSERT_EQ(228, event->getOrientation(1)); // Calculate the orientation after scaling, keeping in mind that an orientation of 0 is "up", // and the positive y direction is "down". auto toScaledOrientation = [](float angle) { const float x = sinf(angle) * X_SCALE; const float y = -cosf(angle) * Y_SCALE; return atan2f(x, -y); }; ASSERT_EQ(toScaledOrientation(18), event->getHistoricalOrientation(0, 0)); ASSERT_EQ(toScaledOrientation(28), event->getHistoricalOrientation(1, 0)); ASSERT_EQ(toScaledOrientation(118), event->getHistoricalOrientation(0, 1)); ASSERT_EQ(toScaledOrientation(128), event->getHistoricalOrientation(1, 1)); ASSERT_EQ(toScaledOrientation(218), event->getOrientation(0)); ASSERT_EQ(toScaledOrientation(228), event->getOrientation(1)); } TEST_F(MotionEventTest, Properties) { Loading Loading @@ -518,6 +525,7 @@ TEST_F(MotionEventTest, OffsetLocation) { TEST_F(MotionEventTest, Scale) { MotionEvent event; initializeEventWithHistory(&event); const float unscaledOrientation = event.getOrientation(0); event.scale(2.0f); Loading @@ -534,7 +542,7 @@ TEST_F(MotionEventTest, Scale) { ASSERT_EQ(215 * 2, event.getTouchMinor(0)); ASSERT_EQ(216 * 2, event.getToolMajor(0)); ASSERT_EQ(217 * 2, event.getToolMinor(0)); ASSERT_EQ(218, event.getOrientation(0)); ASSERT_EQ(unscaledOrientation, event.getOrientation(0)); } TEST_F(MotionEventTest, Parcel) { Loading libs/input/tests/InputPublisherAndConsumer_test.cpp +7 −1 Original line number Diff line number Diff line Loading @@ -259,7 +259,13 @@ void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() { EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), motionEvent->getTouchMinor(i)); EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), motionEvent->getToolMajor(i)); EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), motionEvent->getToolMinor(i)); EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), motionEvent->getOrientation(i)); // Calculate the orientation after scaling, keeping in mind that an orientation of 0 is // "up", and the positive y direction is "down". const float unscaledOrientation = pc.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); const float x = sinf(unscaledOrientation) * xScale; const float y = -cosf(unscaledOrientation) * yScale; EXPECT_EQ(atan2f(x, -y), motionEvent->getOrientation(i)); } status = mConsumer->sendFinishedSignal(seq, false); Loading Loading
include/input/Input.h +3 −0 Original line number Diff line number Diff line Loading @@ -803,6 +803,9 @@ public: static vec2 calculateTransformedXY(uint32_t source, const ui::Transform&, const vec2& xy); static float calculateTransformedAxisValue(int32_t axis, uint32_t source, const ui::Transform&, const PointerCoords&); protected: int32_t mAction; int32_t mActionButton; Loading
libs/input/Input.cpp +30 −50 Original line number Diff line number Diff line Loading @@ -56,13 +56,8 @@ float transformAngle(const ui::Transform& transform, float angleRadians) { transformedPoint.y -= origin.y; // Derive the transformed vector's clockwise angle from vertical. float result = atan2f(transformedPoint.x, -transformedPoint.y); if (result < -M_PI_2) { result += M_PI; } else if (result > M_PI_2) { result -= M_PI; } return result; // The return value of atan2f is in range [-pi, pi] which conforms to the orientation API. return atan2f(transformedPoint.x, -transformedPoint.y); } vec2 transformWithoutTranslation(const ui::Transform& transform, const vec2& xy) { Loading Loading @@ -498,44 +493,14 @@ const PointerCoords* MotionEvent::getHistoricalRawPointerCoords( float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const { const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex); if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) { const vec2 xy = calculateTransformedXY(mSource, mRawTransform, coords->getXYValue()); static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1); return xy[axis]; } if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) { const vec2 relativeXy = transformWithoutTranslation(mRawTransform, {coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)}); return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y; } return coords->getAxisValue(axis); const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex); return calculateTransformedAxisValue(axis, mSource, mRawTransform, coords); } float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex, size_t historicalIndex) const { const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex); if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) { const vec2 xy = calculateTransformedXY(mSource, mTransform, coords->getXYValue()); static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1); return xy[axis]; } if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) { const vec2 relativeXy = transformWithoutTranslation(mTransform, {coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), coords->getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)}); return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y; } return coords->getAxisValue(axis); const PointerCoords& coords = *getHistoricalRawPointerCoords(pointerIndex, historicalIndex); return calculateTransformedAxisValue(axis, mSource, mTransform, coords); } ssize_t MotionEvent::findPointerIndex(int32_t pointerId) const { Loading Loading @@ -574,15 +539,6 @@ void MotionEvent::transform(const std::array<float, 9>& matrix) { ui::Transform newTransform; newTransform.set(matrix); mTransform = newTransform * mTransform; // We need to update the AXIS_ORIENTATION value here to maintain the old behavior where the // orientation angle is not affected by the initial transformation set in the MotionEvent. std::for_each(mSamplePointerCoords.begin(), mSamplePointerCoords.end(), [&newTransform](PointerCoords& c) { float orientation = c.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); c.setAxisValue(AMOTION_EVENT_AXIS_ORIENTATION, transformAngle(newTransform, orientation)); }); } void MotionEvent::applyTransform(const std::array<float, 9>& matrix) { Loading Loading @@ -814,6 +770,30 @@ vec2 MotionEvent::calculateTransformedXY(uint32_t source, const ui::Transform& t : transform.transform(xy); } float MotionEvent::calculateTransformedAxisValue(int32_t axis, uint32_t source, const ui::Transform& transform, const PointerCoords& coords) { if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) { const vec2 xy = calculateTransformedXY(source, transform, coords.getXYValue()); static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1); return xy[axis]; } if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) { const vec2 relativeXy = transformWithoutTranslation(transform, {coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_X), coords.getAxisValue(AMOTION_EVENT_AXIS_RELATIVE_Y)}); return axis == AMOTION_EVENT_AXIS_RELATIVE_X ? relativeXy.x : relativeXy.y; } if (axis == AMOTION_EVENT_AXIS_ORIENTATION) { return transformAngle(transform, coords.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION)); } return coords.getAxisValue(axis); } // --- FocusEvent --- void FocusEvent::initialize(int32_t id, bool hasFocus, bool inTouchMode) { Loading
libs/input/tests/InputEvent_test.cpp +15 −7 Original line number Diff line number Diff line Loading @@ -444,12 +444,19 @@ void MotionEventTest::assertEqualsEventWithHistory(const MotionEvent* event) { ASSERT_EQ(217, event->getToolMinor(0)); ASSERT_EQ(227, event->getToolMinor(1)); ASSERT_EQ(18, event->getHistoricalOrientation(0, 0)); ASSERT_EQ(28, event->getHistoricalOrientation(1, 0)); ASSERT_EQ(118, event->getHistoricalOrientation(0, 1)); ASSERT_EQ(128, event->getHistoricalOrientation(1, 1)); ASSERT_EQ(218, event->getOrientation(0)); ASSERT_EQ(228, event->getOrientation(1)); // Calculate the orientation after scaling, keeping in mind that an orientation of 0 is "up", // and the positive y direction is "down". auto toScaledOrientation = [](float angle) { const float x = sinf(angle) * X_SCALE; const float y = -cosf(angle) * Y_SCALE; return atan2f(x, -y); }; ASSERT_EQ(toScaledOrientation(18), event->getHistoricalOrientation(0, 0)); ASSERT_EQ(toScaledOrientation(28), event->getHistoricalOrientation(1, 0)); ASSERT_EQ(toScaledOrientation(118), event->getHistoricalOrientation(0, 1)); ASSERT_EQ(toScaledOrientation(128), event->getHistoricalOrientation(1, 1)); ASSERT_EQ(toScaledOrientation(218), event->getOrientation(0)); ASSERT_EQ(toScaledOrientation(228), event->getOrientation(1)); } TEST_F(MotionEventTest, Properties) { Loading Loading @@ -518,6 +525,7 @@ TEST_F(MotionEventTest, OffsetLocation) { TEST_F(MotionEventTest, Scale) { MotionEvent event; initializeEventWithHistory(&event); const float unscaledOrientation = event.getOrientation(0); event.scale(2.0f); Loading @@ -534,7 +542,7 @@ TEST_F(MotionEventTest, Scale) { ASSERT_EQ(215 * 2, event.getTouchMinor(0)); ASSERT_EQ(216 * 2, event.getToolMajor(0)); ASSERT_EQ(217 * 2, event.getToolMinor(0)); ASSERT_EQ(218, event.getOrientation(0)); ASSERT_EQ(unscaledOrientation, event.getOrientation(0)); } TEST_F(MotionEventTest, Parcel) { Loading
libs/input/tests/InputPublisherAndConsumer_test.cpp +7 −1 Original line number Diff line number Diff line Loading @@ -259,7 +259,13 @@ void InputPublisherAndConsumerTest::PublishAndConsumeMotionEvent() { EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOUCH_MINOR), motionEvent->getTouchMinor(i)); EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MAJOR), motionEvent->getToolMajor(i)); EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_TOOL_MINOR), motionEvent->getToolMinor(i)); EXPECT_EQ(pc.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION), motionEvent->getOrientation(i)); // Calculate the orientation after scaling, keeping in mind that an orientation of 0 is // "up", and the positive y direction is "down". const float unscaledOrientation = pc.getAxisValue(AMOTION_EVENT_AXIS_ORIENTATION); const float x = sinf(unscaledOrientation) * xScale; const float y = -cosf(unscaledOrientation) * yScale; EXPECT_EQ(atan2f(x, -y), motionEvent->getOrientation(i)); } status = mConsumer->sendFinishedSignal(seq, false); Loading