Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 9186b48e authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes Iedde603f,Iba3bd728

* changes:
  TouchInputMapper: Use ui::Transform to calculate natural display info
  TouchInputMapper: Use ui::Size and Rect for display info
parents f513fd47 2d613f41
Loading
Loading
Loading
Loading
+69 −98
Original line number Diff line number Diff line
@@ -42,6 +42,19 @@ static const float MIN_FREEFORM_GESTURE_WIDTH_IN_MILLIMETER = 30;

static const DisplayViewport kUninitializedViewport;

static std::string toString(const Rect& rect) {
    return base::StringPrintf("Rect{%d, %d, %d, %d}", rect.left, rect.top, rect.right, rect.bottom);
}

static std::string toString(const ui::Size& size) {
    return base::StringPrintf("%dx%d", size.width, size.height);
}

static bool isPointInRect(const Rect& rect, int32_t x, int32_t y) {
    // Consider all four sides as "inclusive".
    return x >= rect.left && x <= rect.right && y >= rect.top && y <= rect.bottom;
}

template <typename T>
inline static void swap(T& a, T& b) {
    T temp = a;
@@ -67,6 +80,33 @@ inline static int32_t signExtendNybble(int32_t value) {
    return value >= 8 ? value - 16 : value;
}

static std::tuple<ui::Size /*displayBounds*/, Rect /*physicalFrame*/> getNaturalDisplayInfo(
        const DisplayViewport& viewport, int32_t naturalOrientation) {
    const auto rotation = ui::toRotation(naturalOrientation);

    ui::Size rotatedDisplaySize{viewport.deviceWidth, viewport.deviceHeight};
    if (rotation == ui::ROTATION_90 || rotation == ui::ROTATION_270) {
        std::swap(rotatedDisplaySize.width, rotatedDisplaySize.height);
    }

    ui::Transform rotate(ui::Transform::toRotationFlags(rotation), rotatedDisplaySize.width,
                         rotatedDisplaySize.height);

    Rect physicalFrame{viewport.physicalLeft, viewport.physicalTop, viewport.physicalRight,
                       viewport.physicalBottom};
    physicalFrame = rotate.transform(physicalFrame);

    LOG_ALWAYS_FATAL_IF(!physicalFrame.isValid());
    if (physicalFrame.isEmpty()) {
        ALOGE("Viewport is not set properly: %s", viewport.toString().c_str());
        physicalFrame.right =
                physicalFrame.left + (physicalFrame.width() == 0 ? 1 : physicalFrame.width());
        physicalFrame.bottom =
                physicalFrame.top + (physicalFrame.height() == 0 ? 1 : physicalFrame.height());
    }
    return {rotatedDisplaySize, physicalFrame};
}

// --- RawPointerData ---

void RawPointerData::getCentroidOfTouchingPointers(float* outX, float* outY) const {
@@ -93,12 +133,6 @@ TouchInputMapper::TouchInputMapper(InputDeviceContext& deviceContext)
        mTouchButtonAccumulator(deviceContext),
        mSource(0),
        mDeviceMode(DeviceMode::DISABLED),
        mDisplayWidth(-1),
        mDisplayHeight(-1),
        mPhysicalWidth(-1),
        mPhysicalHeight(-1),
        mPhysicalLeft(0),
        mPhysicalTop(0),
        mInputDeviceOrientation(DISPLAY_ORIENTATION_0) {}

TouchInputMapper::~TouchInputMapper() {}
@@ -564,7 +598,7 @@ void TouchInputMapper::initializeSizeRanges() {
    }

    // Size of diagonal axis.
    const float diagonalSize = hypotf(mDisplayWidth, mDisplayHeight);
    const float diagonalSize = hypotf(mDisplayBounds.width, mDisplayBounds.height);

    // Size factors.
    if (mRawPointerAxes.touchMajor.valid && mRawPointerAxes.touchMajor.maxValue != 0) {
@@ -647,8 +681,8 @@ void TouchInputMapper::initializeSizeRanges() {

void TouchInputMapper::initializeOrientedRanges() {
    // Configure X and Y factors.
    mXScale = float(mDisplayWidth) / mRawPointerAxes.getRawWidth();
    mYScale = float(mDisplayHeight) / mRawPointerAxes.getRawHeight();
    mXScale = float(mDisplayBounds.width) / mRawPointerAxes.getRawWidth();
    mYScale = float(mDisplayBounds.height) / mRawPointerAxes.getRawHeight();
    mXPrecision = 1.0f / mXScale;
    mYPrecision = 1.0f / mYScale;

@@ -784,13 +818,13 @@ void TouchInputMapper::initializeOrientedRanges() {
            mOrientedYPrecision = mXPrecision;

            mOrientedRanges.x.min = 0;
            mOrientedRanges.x.max = mDisplayHeight - 1;
            mOrientedRanges.x.max = mDisplayBounds.height - 1;
            mOrientedRanges.x.flat = 0;
            mOrientedRanges.x.fuzz = 0;
            mOrientedRanges.x.resolution = mRawPointerAxes.y.resolution * mYScale;

            mOrientedRanges.y.min = 0;
            mOrientedRanges.y.max = mDisplayWidth - 1;
            mOrientedRanges.y.max = mDisplayBounds.width - 1;
            mOrientedRanges.y.flat = 0;
            mOrientedRanges.y.fuzz = 0;
            mOrientedRanges.y.resolution = mRawPointerAxes.x.resolution * mXScale;
@@ -801,13 +835,13 @@ void TouchInputMapper::initializeOrientedRanges() {
            mOrientedYPrecision = mYPrecision;

            mOrientedRanges.x.min = 0;
            mOrientedRanges.x.max = mDisplayWidth - 1;
            mOrientedRanges.x.max = mDisplayBounds.width - 1;
            mOrientedRanges.x.flat = 0;
            mOrientedRanges.x.fuzz = 0;
            mOrientedRanges.x.resolution = mRawPointerAxes.x.resolution * mXScale;

            mOrientedRanges.y.min = 0;
            mOrientedRanges.y.max = mDisplayHeight - 1;
            mOrientedRanges.y.max = mDisplayBounds.height - 1;
            mOrientedRanges.y.flat = 0;
            mOrientedRanges.y.fuzz = 0;
            mOrientedRanges.y.resolution = mRawPointerAxes.y.resolution * mYScale;
@@ -868,8 +902,7 @@ void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded)
    }

    // Raw width and height in the natural orientation.
    const int32_t rawWidth = mRawPointerAxes.getRawWidth();
    const int32_t rawHeight = mRawPointerAxes.getRawHeight();
    const ui::Size rawSize{mRawPointerAxes.getRawWidth(), mRawPointerAxes.getRawHeight()};
    const int32_t rawXResolution = mRawPointerAxes.x.resolution;
    const int32_t rawYResolution = mRawPointerAxes.y.resolution;
    // Calculate the mean resolution when both x and y resolution are set, otherwise set it to 0.
@@ -885,67 +918,16 @@ void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded)
        mViewport = newViewport;

        if (mDeviceMode == DeviceMode::DIRECT || mDeviceMode == DeviceMode::POINTER) {
            // Convert rotated viewport to the natural orientation.
            int32_t naturalPhysicalWidth, naturalPhysicalHeight;
            int32_t naturalPhysicalLeft, naturalPhysicalTop;
            int32_t naturalDeviceWidth, naturalDeviceHeight;
            const auto oldDisplayBounds = mDisplayBounds;

            // 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;
            switch (naturalDeviceOrientation) {
                case DISPLAY_ORIENTATION_90:
                    naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
                    naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
                    naturalPhysicalLeft = mViewport.deviceHeight - mViewport.physicalBottom;
                    naturalPhysicalTop = mViewport.physicalLeft;
                    naturalDeviceWidth = mViewport.deviceHeight;
                    naturalDeviceHeight = mViewport.deviceWidth;
                    break;
                case DISPLAY_ORIENTATION_180:
                    naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
                    naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
                    naturalPhysicalLeft = mViewport.deviceWidth - mViewport.physicalRight;
                    naturalPhysicalTop = mViewport.deviceHeight - mViewport.physicalBottom;
                    naturalDeviceWidth = mViewport.deviceWidth;
                    naturalDeviceHeight = mViewport.deviceHeight;
                    break;
                case DISPLAY_ORIENTATION_270:
                    naturalPhysicalWidth = mViewport.physicalBottom - mViewport.physicalTop;
                    naturalPhysicalHeight = mViewport.physicalRight - mViewport.physicalLeft;
                    naturalPhysicalLeft = mViewport.physicalTop;
                    naturalPhysicalTop = mViewport.deviceWidth - mViewport.physicalRight;
                    naturalDeviceWidth = mViewport.deviceHeight;
                    naturalDeviceHeight = mViewport.deviceWidth;
                    break;
                case DISPLAY_ORIENTATION_0:
                default:
                    naturalPhysicalWidth = mViewport.physicalRight - mViewport.physicalLeft;
                    naturalPhysicalHeight = mViewport.physicalBottom - mViewport.physicalTop;
                    naturalPhysicalLeft = mViewport.physicalLeft;
                    naturalPhysicalTop = mViewport.physicalTop;
                    naturalDeviceWidth = mViewport.deviceWidth;
                    naturalDeviceHeight = mViewport.deviceHeight;
                    break;
            }

            if (naturalPhysicalHeight == 0 || naturalPhysicalWidth == 0) {
                ALOGE("Viewport is not set properly: %s", mViewport.toString().c_str());
                naturalPhysicalHeight = naturalPhysicalHeight == 0 ? 1 : naturalPhysicalHeight;
                naturalPhysicalWidth = naturalPhysicalWidth == 0 ? 1 : naturalPhysicalWidth;
            }

            mPhysicalWidth = naturalPhysicalWidth;
            mPhysicalHeight = naturalPhysicalHeight;
            mPhysicalLeft = naturalPhysicalLeft;
            mPhysicalTop = naturalPhysicalTop;

            const int32_t oldDisplayWidth = mDisplayWidth;
            const int32_t oldDisplayHeight = mDisplayHeight;
            mDisplayWidth = naturalDeviceWidth;
            mDisplayHeight = naturalDeviceHeight;
            std::tie(mDisplayBounds, mPhysicalFrameInDisplay) =
                    getNaturalDisplayInfo(mViewport, naturalDeviceOrientation);

            // InputReader works in the un-rotated display coordinate space, so we don't need to do
            // anything if the device is already orientation-aware. If the device is not
@@ -958,20 +940,14 @@ void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded)
            // 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.
            skipViewportUpdate = !viewportDisplayIdChanged && mParameters.orientationAware &&
                    mDisplayWidth == oldDisplayWidth && mDisplayHeight == oldDisplayHeight &&
                    viewportOrientationChanged;
                    mDisplayBounds == oldDisplayBounds && viewportOrientationChanged;

            // Apply the input device orientation for the device.
            mInputDeviceOrientation =
                    (mInputDeviceOrientation + static_cast<int32_t>(mParameters.orientation)) % 4;
        } else {
            mPhysicalWidth = rawWidth;
            mPhysicalHeight = rawHeight;
            mPhysicalLeft = 0;
            mPhysicalTop = 0;

            mDisplayWidth = rawWidth;
            mDisplayHeight = rawHeight;
            mDisplayBounds = rawSize;
            mPhysicalFrameInDisplay = Rect{mDisplayBounds};
            mInputDeviceOrientation = DISPLAY_ORIENTATION_0;
        }
    }
@@ -1003,9 +979,9 @@ void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded)
    }

    if ((viewportChanged && !skipViewportUpdate) || deviceModeChanged) {
        ALOGI("Device reconfigured: id=%d, name='%s', size %dx%d, orientation %d, mode %d, "
        ALOGI("Device reconfigured: id=%d, name='%s', size %s, orientation %d, mode %d, "
              "display id %d",
              getDeviceId(), getDeviceName().c_str(), mDisplayWidth, mDisplayHeight,
              getDeviceId(), getDeviceName().c_str(), toString(mDisplayBounds).c_str(),
              mInputDeviceOrientation, mDeviceMode, mViewport.displayId);

        configureVirtualKeys();
@@ -1017,8 +993,8 @@ void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded)

        if (mDeviceMode == DeviceMode::POINTER) {
            // Compute pointer gesture detection parameters.
            float rawDiagonal = hypotf(rawWidth, rawHeight);
            float displayDiagonal = hypotf(mDisplayWidth, mDisplayHeight);
            float rawDiagonal = hypotf(rawSize.width, rawSize.height);
            float displayDiagonal = hypotf(mDisplayBounds.width, mDisplayBounds.height);

            // Scale movements such that one whole swipe of the touch pad covers a
            // given area relative to the diagonal size of the display when no acceleration
@@ -1054,12 +1030,8 @@ void TouchInputMapper::configureInputDevice(nsecs_t when, bool* outResetNeeded)

void TouchInputMapper::dumpDisplay(std::string& dump) {
    dump += StringPrintf(INDENT3 "%s\n", mViewport.toString().c_str());
    dump += StringPrintf(INDENT3 "DisplayWidth: %dpx\n", mDisplayWidth);
    dump += StringPrintf(INDENT3 "DisplayHeight: %dpx\n", mDisplayHeight);
    dump += StringPrintf(INDENT3 "PhysicalWidth: %dpx\n", mPhysicalWidth);
    dump += StringPrintf(INDENT3 "PhysicalHeight: %dpx\n", mPhysicalHeight);
    dump += StringPrintf(INDENT3 "PhysicalLeft: %d\n", mPhysicalLeft);
    dump += StringPrintf(INDENT3 "PhysicalTop: %d\n", mPhysicalTop);
    dump += StringPrintf(INDENT3 "DisplayBounds: %s\n", toString(mDisplayBounds).c_str());
    dump += StringPrintf(INDENT3 "PhysicalFrame: %s\n", toString(mPhysicalFrameInDisplay).c_str());
    dump += StringPrintf(INDENT3 "InputDeviceOrientation: %d\n", mInputDeviceOrientation);
}

@@ -1098,17 +1070,17 @@ void TouchInputMapper::configureVirtualKeys() {
        int32_t halfWidth = virtualKeyDefinition.width / 2;
        int32_t halfHeight = virtualKeyDefinition.height / 2;

        virtualKey.hitLeft =
                (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth / mDisplayWidth +
        virtualKey.hitLeft = (virtualKeyDefinition.centerX - halfWidth) * touchScreenWidth /
                        mDisplayBounds.width +
                touchScreenLeft;
        virtualKey.hitRight =
                (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth / mDisplayWidth +
        virtualKey.hitRight = (virtualKeyDefinition.centerX + halfWidth) * touchScreenWidth /
                        mDisplayBounds.width +
                touchScreenLeft;
        virtualKey.hitTop =
                (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight / mDisplayHeight +
        virtualKey.hitTop = (virtualKeyDefinition.centerY - halfHeight) * touchScreenHeight /
                        mDisplayBounds.height +
                touchScreenTop;
        virtualKey.hitBottom =
                (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight / mDisplayHeight +
        virtualKey.hitBottom = (virtualKeyDefinition.centerY + halfHeight) * touchScreenHeight /
                        mDisplayBounds.height +
                touchScreenTop;
        mVirtualKeys.push_back(virtualKey);
    }
@@ -3859,9 +3831,8 @@ bool TouchInputMapper::isPointInsidePhysicalFrame(int32_t x, int32_t y) const {
    const float yScaled = (y - mRawPointerAxes.y.minValue) * mYScale;

    return x >= mRawPointerAxes.x.minValue && x <= mRawPointerAxes.x.maxValue &&
            xScaled >= mPhysicalLeft && xScaled <= (mPhysicalLeft + mPhysicalWidth) &&
            y >= mRawPointerAxes.y.minValue && y <= mRawPointerAxes.y.maxValue &&
            yScaled >= mPhysicalTop && yScaled <= (mPhysicalTop + mPhysicalHeight);
            isPointInRect(mPhysicalFrameInDisplay, xScaled, yScaled);
}

const TouchInputMapper::VirtualKey* TouchInputMapper::findVirtualKeyHit(int32_t x, int32_t y) {
+6 −9
Original line number Diff line number Diff line
@@ -412,17 +412,14 @@ private:
    // The components of the viewport are specified in the display's rotated orientation.
    DisplayViewport mViewport;

    // The width and height are obtained from the viewport and are specified
    // in the natural orientation.
    int32_t mDisplayWidth;
    int32_t mDisplayHeight;
    // We refer to the display as being in the "natural orientation" when there is no rotation
    // applied. The display size obtained from the viewport in the natural orientation.
    // Always starts at (0, 0).
    ui::Size mDisplayBounds{ui::kInvalidSize};

    // The physical frame is the rectangle in the display's coordinate space that maps to the
    // The physical frame is the rectangle in the natural display's coordinate space that maps to
    // the logical display frame.
    int32_t mPhysicalWidth;
    int32_t mPhysicalHeight;
    int32_t mPhysicalLeft;
    int32_t mPhysicalTop;
    Rect mPhysicalFrameInDisplay{Rect::INVALID_RECT};

    // 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