Convert orientation values in input to ui::Rotation.

ui::Rotation both provides better typesafety as well as some convenience
functions (e.g. operator+, operator-).

Test: atest TouchVideoFrame_test.cpp InputReader_test.cpp
Change-Id: Ib423457c742ed3d41f2e3fc269ddf86809cbf247
diff --git a/services/inputflinger/reader/mapper/CursorInputMapper.cpp b/services/inputflinger/reader/mapper/CursorInputMapper.cpp
index a1a2af9..13e4d0c 100644
--- a/services/inputflinger/reader/mapper/CursorInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/CursorInputMapper.cpp
@@ -227,7 +227,7 @@
             mDisplayId = mPointerController->getDisplayId();
         }
 
-        mOrientation = DISPLAY_ORIENTATION_0;
+        mOrientation = ui::ROTATION_0;
         const bool isOrientedDevice =
                 (mParameters.orientationAware && mParameters.hasAssociatedDisplay);
         // InputReader works in the un-rotated display coordinate space, so we don't need to do
diff --git a/services/inputflinger/reader/mapper/CursorInputMapper.h b/services/inputflinger/reader/mapper/CursorInputMapper.h
index 20746e5..939cceb 100644
--- a/services/inputflinger/reader/mapper/CursorInputMapper.h
+++ b/services/inputflinger/reader/mapper/CursorInputMapper.h
@@ -22,6 +22,7 @@
 
 #include <PointerControllerInterface.h>
 #include <input/VelocityControl.h>
+#include <ui/Rotation.h>
 
 namespace android {
 
@@ -115,7 +116,7 @@
     // ADISPLAY_ID_NONE to target the focused display. If there is no display target (i.e.
     // std::nullopt), all events will be ignored.
     std::optional<int32_t> mDisplayId;
-    int32_t mOrientation;
+    ui::Rotation mOrientation;
 
     std::shared_ptr<PointerControllerInterface> mPointerController;
 
diff --git a/services/inputflinger/reader/mapper/KeyboardInputMapper.cpp b/services/inputflinger/reader/mapper/KeyboardInputMapper.cpp
index da9413e..44f0dfe 100644
--- a/services/inputflinger/reader/mapper/KeyboardInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/KeyboardInputMapper.cpp
@@ -20,11 +20,13 @@
 
 #include "KeyboardInputMapper.h"
 
+#include <ui/Rotation.h>
+
 namespace android {
 
 // --- Static Definitions ---
 
-static int32_t rotateKeyCode(int32_t keyCode, int32_t orientation) {
+static int32_t rotateKeyCode(int32_t keyCode, ui::Rotation orientation) {
     static constexpr int32_t KEYCODE_ROTATION_MAP[][4] = {
             // key codes enumerated counter-clockwise with the original (unrotated) key first
             // no rotation,        90 degree rotation,  180 degree rotation, 270 degree rotation
@@ -42,11 +44,10 @@
              AKEYCODE_SYSTEM_NAVIGATION_RIGHT, AKEYCODE_SYSTEM_NAVIGATION_UP},
     };
 
-    LOG_ALWAYS_FATAL_IF(orientation < 0 || orientation > 3, "Invalid orientation: %d", orientation);
-    if (orientation != DISPLAY_ORIENTATION_0) {
+    if (orientation != ui::ROTATION_0) {
         for (const auto& rotation : KEYCODE_ROTATION_MAP) {
-            if (rotation[DISPLAY_ORIENTATION_0] == keyCode) {
-                return rotation[orientation];
+            if (rotation[static_cast<size_t>(ui::ROTATION_0)] == keyCode) {
+                return rotation[static_cast<size_t>(orientation)];
             }
         }
     }
@@ -100,11 +101,11 @@
     return mSource;
 }
 
-int32_t KeyboardInputMapper::getOrientation() {
+ui::Rotation KeyboardInputMapper::getOrientation() {
     if (mViewport) {
         return mViewport->orientation;
     }
-    return DISPLAY_ORIENTATION_0;
+    return ui::ROTATION_0;
 }
 
 int32_t KeyboardInputMapper::getDisplayId() {
diff --git a/services/inputflinger/reader/mapper/KeyboardInputMapper.h b/services/inputflinger/reader/mapper/KeyboardInputMapper.h
index 11d5ad2..0526fd8 100644
--- a/services/inputflinger/reader/mapper/KeyboardInputMapper.h
+++ b/services/inputflinger/reader/mapper/KeyboardInputMapper.h
@@ -82,7 +82,7 @@
     void configureParameters();
     void dumpParameters(std::string& dump) const;
 
-    int32_t getOrientation();
+    ui::Rotation getOrientation();
     int32_t getDisplayId();
 
     [[nodiscard]] std::list<NotifyArgs> processKey(nsecs_t when, nsecs_t readTime, bool down,
diff --git a/services/inputflinger/reader/mapper/RotaryEncoderInputMapper.cpp b/services/inputflinger/reader/mapper/RotaryEncoderInputMapper.cpp
index 06d4dc3..19a79d7 100644
--- a/services/inputflinger/reader/mapper/RotaryEncoderInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/RotaryEncoderInputMapper.cpp
@@ -25,7 +25,7 @@
 namespace android {
 
 RotaryEncoderInputMapper::RotaryEncoderInputMapper(InputDeviceContext& deviceContext)
-      : InputMapper(deviceContext), mOrientation(DISPLAY_ORIENTATION_0) {
+      : InputMapper(deviceContext), mOrientation(ui::ROTATION_0) {
     mSource = AINPUT_SOURCE_ROTARY_ENCODER;
 }
 
@@ -73,7 +73,7 @@
         if (internalViewport) {
             mOrientation = internalViewport->orientation;
         } else {
-            mOrientation = DISPLAY_ORIENTATION_0;
+            mOrientation = ui::ROTATION_0;
         }
     }
     return out;
@@ -107,7 +107,7 @@
         // This is not a pointer, so it's not associated with a display.
         int32_t displayId = ADISPLAY_ID_NONE;
 
-        if (mOrientation == DISPLAY_ORIENTATION_180) {
+        if (mOrientation == ui::ROTATION_180) {
             scroll = -scroll;
         }
 
diff --git a/services/inputflinger/reader/mapper/RotaryEncoderInputMapper.h b/services/inputflinger/reader/mapper/RotaryEncoderInputMapper.h
index f4352e7..cb5fd88 100644
--- a/services/inputflinger/reader/mapper/RotaryEncoderInputMapper.h
+++ b/services/inputflinger/reader/mapper/RotaryEncoderInputMapper.h
@@ -16,6 +16,8 @@
 
 #pragma once
 
+#include <ui/Rotation.h>
+
 #include "CursorScrollAccumulator.h"
 #include "InputMapper.h"
 
@@ -40,7 +42,7 @@
 
     int32_t mSource;
     float mScalingFactor;
-    int32_t mOrientation;
+    ui::Rotation mOrientation;
 
     [[nodiscard]] std::list<NotifyArgs> sync(nsecs_t when, nsecs_t readTime);
 };
diff --git a/services/inputflinger/reader/mapper/TouchCursorInputMapperCommon.h b/services/inputflinger/reader/mapper/TouchCursorInputMapperCommon.h
index d8a4d34..1c3ca97 100644
--- a/services/inputflinger/reader/mapper/TouchCursorInputMapperCommon.h
+++ b/services/inputflinger/reader/mapper/TouchCursorInputMapperCommon.h
@@ -18,6 +18,7 @@
 
 #include <input/DisplayViewport.h>
 #include <stdint.h>
+#include <ui/Rotation.h>
 
 #include "EventHub.h"
 #include "InputListener.h"
@@ -27,32 +28,32 @@
 
 // --- Static Definitions ---
 
-static int32_t getInverseRotation(int32_t orientation) {
+static ui::Rotation getInverseRotation(ui::Rotation orientation) {
     switch (orientation) {
-        case DISPLAY_ORIENTATION_90:
-            return DISPLAY_ORIENTATION_270;
-        case DISPLAY_ORIENTATION_270:
-            return DISPLAY_ORIENTATION_90;
+        case ui::ROTATION_90:
+            return ui::ROTATION_270;
+        case ui::ROTATION_270:
+            return ui::ROTATION_90;
         default:
             return orientation;
     }
 }
 
-static void rotateDelta(int32_t orientation, float* deltaX, float* deltaY) {
+static void rotateDelta(ui::Rotation orientation, float* deltaX, float* deltaY) {
     float temp;
     switch (orientation) {
-        case DISPLAY_ORIENTATION_90:
+        case ui::ROTATION_90:
             temp = *deltaX;
             *deltaX = *deltaY;
             *deltaY = -temp;
             break;
 
-        case DISPLAY_ORIENTATION_180:
+        case ui::ROTATION_180:
             *deltaX = -*deltaX;
             *deltaY = -*deltaY;
             break;
 
-        case DISPLAY_ORIENTATION_270:
+        case ui::ROTATION_270:
             temp = *deltaX;
             *deltaX = -*deltaY;
             *deltaY = temp;
diff --git a/services/inputflinger/reader/mapper/TouchInputMapper.cpp b/services/inputflinger/reader/mapper/TouchInputMapper.cpp
index 5631a10..cefc44e 100644
--- a/services/inputflinger/reader/mapper/TouchInputMapper.cpp
+++ b/services/inputflinger/reader/mapper/TouchInputMapper.cpp
@@ -27,6 +27,7 @@
 #include "CursorScrollAccumulator.h"
 #include "TouchButtonAccumulator.h"
 #include "TouchCursorInputMapperCommon.h"
+#include "ui/Rotation.h"
 
 namespace android {
 
@@ -81,16 +82,14 @@
 }
 
 static std::tuple<ui::Size /*displayBounds*/, Rect /*physicalFrame*/> getNaturalDisplayInfo(
-        const DisplayViewport& viewport, int32_t naturalOrientation) {
-    const auto rotation = ui::toRotation(naturalOrientation);
-
+        const DisplayViewport& viewport, ui::Rotation naturalOrientation) {
     ui::Size rotatedDisplaySize{viewport.deviceWidth, viewport.deviceHeight};
-    if (rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270) {
+    if (naturalOrientation == ui::ROTATION_90 || naturalOrientation == ui::ROTATION_270) {
         std::swap(rotatedDisplaySize.width, rotatedDisplaySize.height);
     }
 
-    ui::Transform rotate(ui::Transform::toRotationFlags(rotation), rotatedDisplaySize.width,
-                         rotatedDisplaySize.height);
+    ui::Transform rotate(ui::Transform::toRotationFlags(naturalOrientation),
+                         rotatedDisplaySize.width, rotatedDisplaySize.height);
 
     Rect physicalFrame{viewport.physicalLeft, viewport.physicalTop, viewport.physicalRight,
                        viewport.physicalBottom};
@@ -133,7 +132,7 @@
         mTouchButtonAccumulator(deviceContext),
         mSource(0),
         mDeviceMode(DeviceMode::DISABLED),
-        mInputDeviceOrientation(DISPLAY_ORIENTATION_0) {}
+        mInputDeviceOrientation(ui::ROTATION_0) {}
 
 TouchInputMapper::~TouchInputMapper() {}
 
@@ -424,18 +423,18 @@
     getDeviceContext().getConfiguration().tryGetProperty("touch.orientationAware",
                                                          mParameters.orientationAware);
 
-    mParameters.orientation = Parameters::Orientation::ORIENTATION_0;
+    mParameters.orientation = ui::ROTATION_0;
     std::string orientationString;
     if (getDeviceContext().getConfiguration().tryGetProperty("touch.orientation",
                                                              orientationString)) {
         if (mParameters.deviceType != Parameters::DeviceType::TOUCH_SCREEN) {
             ALOGW("The configuration 'touch.orientation' is only supported for touchscreens.");
         } else if (orientationString == "ORIENTATION_90") {
-            mParameters.orientation = Parameters::Orientation::ORIENTATION_90;
+            mParameters.orientation = ui::ROTATION_90;
         } else if (orientationString == "ORIENTATION_180") {
-            mParameters.orientation = Parameters::Orientation::ORIENTATION_180;
+            mParameters.orientation = ui::ROTATION_180;
         } else if (orientationString == "ORIENTATION_270") {
-            mParameters.orientation = Parameters::Orientation::ORIENTATION_270;
+            mParameters.orientation = ui::ROTATION_270;
         } else if (orientationString != "ORIENTATION_0") {
             ALOGW("Invalid value for touch.orientation: '%s'", orientationString.c_str());
         }
@@ -812,8 +811,8 @@
     // Note that the maximum value reported is an inclusive maximum value so it is one
     // unit less than the total width or height of the display.
     switch (mInputDeviceOrientation) {
-        case DISPLAY_ORIENTATION_90:
-        case DISPLAY_ORIENTATION_270:
+        case ui::ROTATION_90:
+        case ui::ROTATION_270:
             mOrientedXPrecision = mYPrecision;
             mOrientedYPrecision = mXPrecision;
 
@@ -923,8 +922,8 @@
             // Apply the inverse of the input device orientation so that the input device is
             // configured in the same orientation as the viewport. The input device orientation will
             // be re-applied by mInputDeviceOrientation.
-            const int32_t naturalDeviceOrientation =
-                    (mViewport.orientation - static_cast<int32_t>(mParameters.orientation) + 4) % 4;
+            const ui::Rotation naturalDeviceOrientation =
+                    mViewport.orientation - mParameters.orientation;
 
             std::tie(mDisplayBounds, mPhysicalFrameInDisplay) =
                     getNaturalDisplayInfo(mViewport, naturalDeviceOrientation);
@@ -935,7 +934,7 @@
             // when the display rotation is applied later as a part of the per-window transform, we
             // get the expected screen coordinates.
             mInputDeviceOrientation = mParameters.orientationAware
-                    ? DISPLAY_ORIENTATION_0
+                    ? ui::ROTATION_0
                     : getInverseRotation(mViewport.orientation);
             // For orientation-aware devices that work in the un-rotated coordinate space, the
             // viewport update should be skipped if it is only a change in the orientation.
@@ -943,12 +942,11 @@
                     mDisplayBounds == oldDisplayBounds && viewportOrientationChanged;
 
             // Apply the input device orientation for the device.
-            mInputDeviceOrientation =
-                    (mInputDeviceOrientation + static_cast<int32_t>(mParameters.orientation)) % 4;
+            mInputDeviceOrientation = mInputDeviceOrientation + mParameters.orientation;
         } else {
             mDisplayBounds = rawSize;
             mPhysicalFrameInDisplay = Rect{mDisplayBounds};
-            mInputDeviceOrientation = DISPLAY_ORIENTATION_0;
+            mInputDeviceOrientation = ui::ROTATION_0;
         }
     }
 
@@ -2349,7 +2347,7 @@
         float left, top, right, bottom;
 
         switch (mInputDeviceOrientation) {
-            case DISPLAY_ORIENTATION_90:
+            case ui::ROTATION_90:
                 left = float(rawTop - mRawPointerAxes.y.minValue) * mYScale;
                 right = float(rawBottom - mRawPointerAxes.y.minValue) * mYScale;
                 bottom = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
@@ -2360,7 +2358,7 @@
                             (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
                 }
                 break;
-            case DISPLAY_ORIENTATION_180:
+            case ui::ROTATION_180:
                 left = float(mRawPointerAxes.x.maxValue - rawRight) * mXScale;
                 right = float(mRawPointerAxes.x.maxValue - rawLeft) * mXScale;
                 bottom = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
@@ -2371,7 +2369,7 @@
                             (mOrientedRanges.orientation->max - mOrientedRanges.orientation->min);
                 }
                 break;
-            case DISPLAY_ORIENTATION_270:
+            case ui::ROTATION_270:
                 left = float(mRawPointerAxes.y.maxValue - rawBottom) * mYScale;
                 right = float(mRawPointerAxes.y.maxValue - rawTop) * mYScale;
                 bottom = float(rawRight - mRawPointerAxes.x.minValue) * mXScale;
@@ -3805,19 +3803,19 @@
     // 180 - reverse x, y.
     // 270 - swap x/y and reverse x.
     switch (mInputDeviceOrientation) {
-        case DISPLAY_ORIENTATION_0:
+        case ui::ROTATION_0:
             x = xScaled;
             y = yScaled;
             break;
-        case DISPLAY_ORIENTATION_90:
+        case ui::ROTATION_90:
             y = xScaledMax;
             x = yScaled;
             break;
-        case DISPLAY_ORIENTATION_180:
+        case ui::ROTATION_180:
             x = xScaledMax;
             y = yScaledMax;
             break;
-        case DISPLAY_ORIENTATION_270:
+        case ui::ROTATION_270:
             y = xScaled;
             x = yScaledMax;
             break;
diff --git a/services/inputflinger/reader/mapper/TouchInputMapper.h b/services/inputflinger/reader/mapper/TouchInputMapper.h
index 3962b2a..34ba625 100644
--- a/services/inputflinger/reader/mapper/TouchInputMapper.h
+++ b/services/inputflinger/reader/mapper/TouchInputMapper.h
@@ -17,6 +17,7 @@
 #pragma once
 
 #include <stdint.h>
+#include <ui/Rotation.h>
 
 #include "CursorButtonAccumulator.h"
 #include "CursorScrollAccumulator.h"
@@ -218,15 +219,7 @@
         bool associatedDisplayIsExternal;
         bool orientationAware;
 
-        enum class Orientation : int32_t {
-            ORIENTATION_0 = DISPLAY_ORIENTATION_0,
-            ORIENTATION_90 = DISPLAY_ORIENTATION_90,
-            ORIENTATION_180 = DISPLAY_ORIENTATION_180,
-            ORIENTATION_270 = DISPLAY_ORIENTATION_270,
-
-            ftl_last = ORIENTATION_270
-        };
-        Orientation orientation;
+        ui::Rotation orientation;
 
         bool hasButtonUnderPad;
         std::string uniqueDisplayId;
@@ -424,7 +417,7 @@
     // The orientation of the input device relative to that of the display panel. It specifies
     // the rotation of the input device coordinates required to produce the display panel
     // orientation, so it will depend on whether the device is orientation aware.
-    int32_t mInputDeviceOrientation;
+    ui::Rotation mInputDeviceOrientation;
 
     // Translation and scaling factors, orientation-independent.
     float mXScale;