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

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

Merge "Add display transform to MotionEvent for raw coords"

parents e3ce49da b9b18509
Loading
Loading
Loading
Loading
+4 −8
Original line number Diff line number Diff line
@@ -579,9 +579,7 @@ public:

    void setCursorPosition(float x, float y);

    uint32_t getDisplayOrientation() const { return mDisplayOrientation; }

    int2 getDisplaySize() const { return {mDisplayWidth, mDisplayHeight}; }
    ui::Transform getRawTransform() const { return mRawTransform; }

    static inline bool isValidCursorPosition(float x, float y) { return !isnan(x) && !isnan(y); }

@@ -757,8 +755,8 @@ public:
                    int32_t flags, int32_t edgeFlags, int32_t metaState, int32_t buttonState,
                    MotionClassification classification, const ui::Transform& transform,
                    float xPrecision, float yPrecision, float rawXCursorPosition,
                    float rawYCursorPosition, uint32_t displayOrientation, int32_t displayWidth,
                    int32_t displayHeight, nsecs_t downTime, nsecs_t eventTime, size_t pointerCount,
                    float rawYCursorPosition, const ui::Transform& rawTransform, nsecs_t downTime,
                    nsecs_t eventTime, size_t pointerCount,
                    const PointerProperties* pointerProperties, const PointerCoords* pointerCoords);

    void copyFrom(const MotionEvent* other, bool keepHistory);
@@ -816,9 +814,7 @@ protected:
    float mYPrecision;
    float mRawXCursorPosition;
    float mRawYCursorPosition;
    uint32_t mDisplayOrientation;
    int32_t mDisplayWidth;
    int32_t mDisplayHeight;
    ui::Transform mRawTransform;
    nsecs_t mDownTime;
    Vector<PointerProperties> mPointerProperties;
    std::vector<nsecs_t> mSampleEventTimes;
+15 −14
Original line number Diff line number Diff line
@@ -114,7 +114,7 @@ struct InputMessage {

        struct Motion {
            int32_t eventId;
            uint32_t empty1;
            uint32_t pointerCount;
            nsecs_t eventTime __attribute__((aligned(8)));
            int32_t deviceId;
            int32_t source;
@@ -129,20 +129,22 @@ struct InputMessage {
            uint8_t empty2[3];                   // 3 bytes to fill gap created by classification
            int32_t edgeFlags;
            nsecs_t downTime __attribute__((aligned(8)));
            float dsdx;
            float dtdx;
            float dtdy;
            float dsdy;
            float tx;
            float ty;
            float dsdx; // Begin window transform
            float dtdx; //
            float dtdy; //
            float dsdy; //
            float tx;   //
            float ty;   // End window transform
            float xPrecision;
            float yPrecision;
            float xCursorPosition;
            float yCursorPosition;
            uint32_t displayOrientation;
            int32_t displayWidth;
            int32_t displayHeight;
            uint32_t pointerCount;
            float dsdxRaw; // Begin raw transform
            float dtdxRaw; //
            float dtdyRaw; //
            float dsdyRaw; //
            float txRaw;   //
            float tyRaw;   // End raw transform
            /**
             * The "pointers" field must be the last field of the struct InputMessage.
             * When we send the struct InputMessage across the socket, we are not
@@ -367,9 +369,8 @@ public:
                                int32_t metaState, int32_t buttonState,
                                MotionClassification classification, const ui::Transform& transform,
                                float xPrecision, float yPrecision, float xCursorPosition,
                                float yCursorPosition, uint32_t displayOrientation,
                                int32_t displayWidth, int32_t displayHeight, nsecs_t downTime,
                                nsecs_t eventTime, uint32_t pointerCount,
                                float yCursorPosition, const ui::Transform& rawTransform,
                                nsecs_t downTime, nsecs_t eventTime, uint32_t pointerCount,
                                const PointerProperties* pointerProperties,
                                const PointerCoords* pointerCoords);

+29 −54
Original line number Diff line number Diff line
@@ -76,36 +76,13 @@ float transformAngle(const ui::Transform& transform, float angleRadians) {
    return result;
}

// Rotates the given point to the specified orientation. If the display width and height are
// provided, the point is rotated in the screen space. Otherwise, the point is rotated about the
// origin. This helper is used to avoid the extra overhead of creating new Transforms.
vec2 rotatePoint(uint32_t orientation, float x, float y, int32_t displayWidth = 0,
                 int32_t displayHeight = 0) {
    if (orientation == ui::Transform::ROT_0) {
        return {x, y};
    }

    vec2 xy(x, y);
    if (orientation == ui::Transform::ROT_90) {
        xy.x = displayHeight - y;
        xy.y = x;
    } else if (orientation == ui::Transform::ROT_180) {
        xy.x = displayWidth - x;
        xy.y = displayHeight - y;
    } else if (orientation == ui::Transform::ROT_270) {
        xy.x = y;
        xy.y = displayWidth - x;
    }
    return xy;
}

vec2 applyTransformWithoutTranslation(const ui::Transform& transform, float x, float y) {
vec2 transformWithoutTranslation(const ui::Transform& transform, float x, float y) {
    const vec2 transformedXy = transform.transform(x, y);
    const vec2 transformedOrigin = transform.transform(0, 0);
    return transformedXy - transformedOrigin;
}

bool shouldDisregardWindowTranslation(uint32_t source) {
bool shouldDisregardTranslation(uint32_t source) {
    // Pointer events are the only type of events that refer to absolute coordinates on the display,
    // so we should apply the entire window transform. For other types of events, we should make
    // sure to not apply the window translation/offset.
@@ -431,8 +408,7 @@ void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int3
                             int32_t buttonState, MotionClassification classification,
                             const ui::Transform& transform, float xPrecision, float yPrecision,
                             float rawXCursorPosition, float rawYCursorPosition,
                             uint32_t displayOrientation, int32_t displayWidth,
                             int32_t displayHeight, nsecs_t downTime, nsecs_t eventTime,
                             const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
                             size_t pointerCount, const PointerProperties* pointerProperties,
                             const PointerCoords* pointerCoords) {
    InputEvent::initialize(id, deviceId, source, displayId, hmac);
@@ -448,9 +424,7 @@ void MotionEvent::initialize(int32_t id, int32_t deviceId, uint32_t source, int3
    mYPrecision = yPrecision;
    mRawXCursorPosition = rawXCursorPosition;
    mRawYCursorPosition = rawYCursorPosition;
    mDisplayOrientation = displayOrientation;
    mDisplayWidth = displayWidth;
    mDisplayHeight = displayHeight;
    mRawTransform = rawTransform;
    mDownTime = downTime;
    mPointerProperties.clear();
    mPointerProperties.appendArray(pointerProperties, pointerCount);
@@ -474,9 +448,7 @@ void MotionEvent::copyFrom(const MotionEvent* other, bool keepHistory) {
    mYPrecision = other->mYPrecision;
    mRawXCursorPosition = other->mRawXCursorPosition;
    mRawYCursorPosition = other->mRawYCursorPosition;
    mDisplayOrientation = other->mDisplayOrientation;
    mDisplayWidth = other->mDisplayWidth;
    mDisplayHeight = other->mDisplayHeight;
    mRawTransform = other->mRawTransform;
    mDownTime = other->mDownTime;
    mPointerProperties = other->mPointerProperties;

@@ -542,20 +514,19 @@ float MotionEvent::getHistoricalRawAxisValue(int32_t axis, size_t pointerIndex,
    if (!isPerWindowInputRotationEnabled()) return coords->getAxisValue(axis);

    if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
        // For compatibility, convert raw coordinates into "oriented screen space". Once app
        // developers are educated about getRaw, we can consider removing this.
        const vec2 xy = shouldDisregardWindowTranslation(mSource)
                ? rotatePoint(mDisplayOrientation, coords->getX(), coords->getY())
                : rotatePoint(mDisplayOrientation, coords->getX(), coords->getY(), mDisplayWidth,
                              mDisplayHeight);
        // For compatibility, convert raw coordinates into logical display space.
        const vec2 xy = shouldDisregardTranslation(mSource)
                ? transformWithoutTranslation(mRawTransform, coords->getX(), coords->getY())
                : mRawTransform.transform(coords->getX(), coords->getY());
        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) {
        // For compatibility, since we convert raw coordinates into "oriented screen space", we
        // For compatibility, since we report raw coordinates in logical display space, we
        // need to convert the relative axes into the same orientation for consistency.
        const vec2 relativeXy = rotatePoint(mDisplayOrientation,
        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;
@@ -569,8 +540,8 @@ float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,
    const PointerCoords* coords = getHistoricalRawPointerCoords(pointerIndex, historicalIndex);

    if (axis == AMOTION_EVENT_AXIS_X || axis == AMOTION_EVENT_AXIS_Y) {
        const vec2 xy = shouldDisregardWindowTranslation(mSource)
                ? applyTransformWithoutTranslation(mTransform, coords->getX(), coords->getY())
        const vec2 xy = shouldDisregardTranslation(mSource)
                ? transformWithoutTranslation(mTransform, coords->getX(), coords->getY())
                : mTransform.transform(coords->getXYValue());
        static_assert(AMOTION_EVENT_AXIS_X == 0 && AMOTION_EVENT_AXIS_Y == 1);
        return xy[axis];
@@ -578,11 +549,9 @@ float MotionEvent::getHistoricalAxisValue(int32_t axis, size_t pointerIndex,

    if (axis == AMOTION_EVENT_AXIS_RELATIVE_X || axis == AMOTION_EVENT_AXIS_RELATIVE_Y) {
        const vec2 relativeXy =
                applyTransformWithoutTranslation(mTransform,
                                                 coords->getAxisValue(
                                                         AMOTION_EVENT_AXIS_RELATIVE_X),
                                                 coords->getAxisValue(
                                                         AMOTION_EVENT_AXIS_RELATIVE_Y));
                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;
    }

@@ -607,6 +576,8 @@ void MotionEvent::offsetLocation(float xOffset, float yOffset) {

void MotionEvent::scale(float globalScaleFactor) {
    mTransform.set(mTransform.tx() * globalScaleFactor, mTransform.ty() * globalScaleFactor);
    mRawTransform.set(mRawTransform.tx() * globalScaleFactor,
                      mRawTransform.ty() * globalScaleFactor);
    mXPrecision *= globalScaleFactor;
    mYPrecision *= globalScaleFactor;

@@ -708,9 +679,11 @@ status_t MotionEvent::readFromParcel(Parcel* parcel) {
    mYPrecision = parcel->readFloat();
    mRawXCursorPosition = parcel->readFloat();
    mRawYCursorPosition = parcel->readFloat();
    mDisplayOrientation = parcel->readUint32();
    mDisplayWidth = parcel->readInt32();
    mDisplayHeight = parcel->readInt32();

    result = android::readFromParcel(mRawTransform, *parcel);
    if (result != OK) {
        return result;
    }
    mDownTime = parcel->readInt64();

    mPointerProperties.clear();
@@ -770,9 +743,11 @@ status_t MotionEvent::writeToParcel(Parcel* parcel) const {
    parcel->writeFloat(mYPrecision);
    parcel->writeFloat(mRawXCursorPosition);
    parcel->writeFloat(mRawYCursorPosition);
    parcel->writeUint32(mDisplayOrientation);
    parcel->writeInt32(mDisplayWidth);
    parcel->writeInt32(mDisplayHeight);

    result = android::writeToParcel(mRawTransform, *parcel);
    if (result != OK) {
        return result;
    }
    parcel->writeInt64(mDownTime);

    for (size_t i = 0; i < pointerCount; i++) {
+24 −16
Original line number Diff line number Diff line
@@ -203,6 +203,8 @@ void InputMessage::getSanitizedCopy(InputMessage* msg) const {
        case InputMessage::Type::MOTION: {
            // int32_t eventId
            msg->body.motion.eventId = body.motion.eventId;
            // uint32_t pointerCount
            msg->body.motion.pointerCount = body.motion.pointerCount;
            // nsecs_t eventTime
            msg->body.motion.eventTime = body.motion.eventTime;
            // int32_t deviceId
@@ -245,14 +247,14 @@ void InputMessage::getSanitizedCopy(InputMessage* msg) const {
            msg->body.motion.xCursorPosition = body.motion.xCursorPosition;
            // float yCursorPosition
            msg->body.motion.yCursorPosition = body.motion.yCursorPosition;
            // uint32_t displayOrientation
            msg->body.motion.displayOrientation = body.motion.displayOrientation;
            // int32_t displayWidth
            msg->body.motion.displayWidth = body.motion.displayWidth;
            // int32_t displayHeight
            msg->body.motion.displayHeight = body.motion.displayHeight;
            // uint32_t pointerCount
            msg->body.motion.pointerCount = body.motion.pointerCount;

            msg->body.motion.dsdxRaw = body.motion.dsdxRaw;
            msg->body.motion.dtdxRaw = body.motion.dtdxRaw;
            msg->body.motion.dtdyRaw = body.motion.dtdyRaw;
            msg->body.motion.dsdyRaw = body.motion.dsdyRaw;
            msg->body.motion.txRaw = body.motion.txRaw;
            msg->body.motion.tyRaw = body.motion.tyRaw;

            //struct Pointer pointers[MAX_POINTERS]
            for (size_t i = 0; i < body.motion.pointerCount; i++) {
                // PointerProperties properties
@@ -542,8 +544,8 @@ status_t InputPublisher::publishMotionEvent(
        std::array<uint8_t, 32> hmac, int32_t action, int32_t actionButton, int32_t flags,
        int32_t edgeFlags, int32_t metaState, int32_t buttonState,
        MotionClassification classification, const ui::Transform& transform, float xPrecision,
        float yPrecision, float xCursorPosition, float yCursorPosition, uint32_t displayOrientation,
        int32_t displayWidth, int32_t displayHeight, nsecs_t downTime, nsecs_t eventTime,
        float yPrecision, float xCursorPosition, float yCursorPosition,
        const ui::Transform& rawTransform, nsecs_t downTime, nsecs_t eventTime,
        uint32_t pointerCount, const PointerProperties* pointerProperties,
        const PointerCoords* pointerCoords) {
    if (ATRACE_ENABLED()) {
@@ -603,9 +605,12 @@ status_t InputPublisher::publishMotionEvent(
    msg.body.motion.yPrecision = yPrecision;
    msg.body.motion.xCursorPosition = xCursorPosition;
    msg.body.motion.yCursorPosition = yCursorPosition;
    msg.body.motion.displayOrientation = displayOrientation;
    msg.body.motion.displayWidth = displayWidth;
    msg.body.motion.displayHeight = displayHeight;
    msg.body.motion.dsdxRaw = rawTransform.dsdx();
    msg.body.motion.dtdxRaw = rawTransform.dtdx();
    msg.body.motion.dtdyRaw = rawTransform.dtdy();
    msg.body.motion.dsdyRaw = rawTransform.dsdy();
    msg.body.motion.txRaw = rawTransform.tx();
    msg.body.motion.tyRaw = rawTransform.ty();
    msg.body.motion.downTime = downTime;
    msg.body.motion.eventTime = eventTime;
    msg.body.motion.pointerCount = pointerCount;
@@ -1391,6 +1396,10 @@ void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage
    ui::Transform transform;
    transform.set({msg->body.motion.dsdx, msg->body.motion.dtdx, msg->body.motion.tx,
                   msg->body.motion.dtdy, msg->body.motion.dsdy, msg->body.motion.ty, 0, 0, 1});
    ui::Transform displayTransform;
    displayTransform.set({msg->body.motion.dsdxRaw, msg->body.motion.dtdxRaw,
                          msg->body.motion.txRaw, msg->body.motion.dtdyRaw,
                          msg->body.motion.dsdyRaw, msg->body.motion.tyRaw, 0, 0, 1});
    event->initialize(msg->body.motion.eventId, msg->body.motion.deviceId, msg->body.motion.source,
                      msg->body.motion.displayId, msg->body.motion.hmac, msg->body.motion.action,
                      msg->body.motion.actionButton, msg->body.motion.flags,
@@ -1398,9 +1407,8 @@ void InputConsumer::initializeMotionEvent(MotionEvent* event, const InputMessage
                      msg->body.motion.buttonState, msg->body.motion.classification, transform,
                      msg->body.motion.xPrecision, msg->body.motion.yPrecision,
                      msg->body.motion.xCursorPosition, msg->body.motion.yCursorPosition,
                      msg->body.motion.displayOrientation, msg->body.motion.displayWidth,
                      msg->body.motion.displayHeight, msg->body.motion.downTime,
                      msg->body.motion.eventTime, pointerCount, pointerProperties, pointerCoords);
                      displayTransform, msg->body.motion.downTime, msg->body.motion.eventTime,
                      pointerCount, pointerProperties, pointerCoords);
}

void InputConsumer::initializeTouchModeEvent(TouchModeEvent* event, const InputMessage* msg) {
+89 −127

File changed.

Preview size limit exceeded, changes collapsed.

Loading