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

Commit 84cdf9cc authored by Andrii Kulian's avatar Andrii Kulian Committed by Arthur Hung
Browse files

Support mouse pointer on external displays (2/3)

Limit mouse boundary to one display viewport.
Currently if desktop mode is enable, selects the topmost freeform display,
or find first external display if there is no freeform display.
Return the default display if desktop mode is not enable.

- Add getPointerDisplayId to find the preferred display.
- Limit update viewports from InputReader. Also implements updatePointerDisplay
  to find and associate with the specified display viewport.
- Reload cursor resources if resolution or display changed.

Bug: 113559891
Test: atest inputfliger_tests
Test: ActivityView test app
Change-Id: Ief3410a517be457dfe6f1180342bb1dc88833635
parent 9be59f96
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -14,7 +14,7 @@

cc_library_shared {
    name: "libinputservice",

    cpp_std: "c++17",
    srcs: [
        "PointerController.cpp",
        "SpriteController.cpp",
+72 −64
Original line number Diff line number Diff line
@@ -89,10 +89,6 @@ PointerController::PointerController(const sp<PointerControllerPolicyInterface>&

    mLocked.animationPending = false;

    mLocked.displayWidth = -1;
    mLocked.displayHeight = -1;
    mLocked.displayOrientation = DISPLAY_ORIENTATION_0;

    mLocked.presentation = PRESENTATION_POINTER;
    mLocked.presentationChanged = false;

@@ -110,15 +106,6 @@ PointerController::PointerController(const sp<PointerControllerPolicyInterface>&
    mLocked.lastFrameUpdatedTime = 0;

    mLocked.buttonState = 0;

    mPolicy->loadPointerIcon(&mLocked.pointerIcon);

    loadResources();

    if (mLocked.pointerIcon.isValid()) {
        mLocked.pointerIconChanged = true;
        updatePointerLocked();
    }
}

PointerController::~PointerController() {
@@ -144,23 +131,15 @@ bool PointerController::getBounds(float* outMinX, float* outMinY,

bool PointerController::getBoundsLocked(float* outMinX, float* outMinY,
        float* outMaxX, float* outMaxY) const {
    if (mLocked.displayWidth <= 0 || mLocked.displayHeight <= 0) {

    if (!mLocked.viewport.isValid()) {
        return false;
    }

    *outMinX = 0;
    *outMinY = 0;
    switch (mLocked.displayOrientation) {
    case DISPLAY_ORIENTATION_90:
    case DISPLAY_ORIENTATION_270:
        *outMaxX = mLocked.displayHeight - 1;
        *outMaxY = mLocked.displayWidth - 1;
        break;
    default:
        *outMaxX = mLocked.displayWidth - 1;
        *outMaxY = mLocked.displayHeight - 1;
        break;
    }
    *outMinX = mLocked.viewport.logicalLeft;
    *outMinY = mLocked.viewport.logicalTop;
    *outMaxX = mLocked.viewport.logicalRight - 1;
    *outMaxY = mLocked.viewport.logicalBottom - 1;
    return true;
}

@@ -231,6 +210,12 @@ void PointerController::getPosition(float* outX, float* outY) const {
    *outY = mLocked.pointerY;
}

int32_t PointerController::getDisplayId() const {
    AutoMutex _l(mLock);

    return mLocked.viewport.displayId;
}

void PointerController::fade(Transition transition) {
    AutoMutex _l(mLock);

@@ -355,48 +340,57 @@ void PointerController::setInactivityTimeout(InactivityTimeout inactivityTimeout
void PointerController::reloadPointerResources() {
    AutoMutex _l(mLock);

    loadResources();

    if (mLocked.presentation == PRESENTATION_POINTER) {
        mLocked.additionalMouseResources.clear();
        mLocked.animationResources.clear();
        mPolicy->loadPointerIcon(&mLocked.pointerIcon);
        mPolicy->loadAdditionalMouseResources(&mLocked.additionalMouseResources,
                                              &mLocked.animationResources);
    loadResourcesLocked();
    updatePointerLocked();
}

    mLocked.presentationChanged = true;
    updatePointerLocked();
/**
 * The viewport values for deviceHeight and deviceWidth have already been adjusted for rotation,
 * so here we are getting the dimensions in the original, unrotated orientation (orientation 0).
 */
static void getNonRotatedSize(const DisplayViewport& viewport, int32_t& width, int32_t& height) {
    if (viewport.orientation == DISPLAY_ORIENTATION_90
            || viewport.orientation == DISPLAY_ORIENTATION_270) {
        width = viewport.deviceHeight;
        height = viewport.deviceWidth;
    } else {
        width = viewport.deviceWidth;
        height = viewport.deviceHeight;
    }
}

void PointerController::setDisplayViewport(int32_t width, int32_t height, int32_t orientation) {
void PointerController::setDisplayViewport(const DisplayViewport& viewport) {
    AutoMutex _l(mLock);

    // Adjust to use the display's unrotated coordinate frame.
    if (orientation == DISPLAY_ORIENTATION_90
            || orientation == DISPLAY_ORIENTATION_270) {
        int32_t temp = height;
        height = width;
        width = temp;
    if (viewport == mLocked.viewport) {
        return;
    }

    if (mLocked.displayWidth != width || mLocked.displayHeight != height) {
        mLocked.displayWidth = width;
        mLocked.displayHeight = height;
    const DisplayViewport oldViewport = mLocked.viewport;
    mLocked.viewport = viewport;

    int32_t oldDisplayWidth, oldDisplayHeight;
    getNonRotatedSize(oldViewport, oldDisplayWidth, oldDisplayHeight);
    int32_t newDisplayWidth, newDisplayHeight;
    getNonRotatedSize(viewport, newDisplayWidth, newDisplayHeight);

    // Reset cursor position to center if size or display changed.
    if (oldViewport.displayId != viewport.displayId
            || oldDisplayWidth != newDisplayWidth
            || oldDisplayHeight != newDisplayHeight) {

        float minX, minY, maxX, maxY;
        if (getBoundsLocked(&minX, &minY, &maxX, &maxY)) {
            mLocked.pointerX = (minX + maxX) * 0.5f;
            mLocked.pointerY = (minY + maxY) * 0.5f;
            // Reload icon resources for density may be changed.
            loadResourcesLocked();
        } else {
            mLocked.pointerX = 0;
            mLocked.pointerY = 0;
        }

        fadeOutAndReleaseAllSpotsLocked();
    }

    if (mLocked.displayOrientation != orientation) {
    } 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.
@@ -405,37 +399,37 @@ void PointerController::setDisplayViewport(int32_t width, int32_t height, int32_
        float temp;

        // Undo the previous rotation.
        switch (mLocked.displayOrientation) {
        switch (oldViewport.orientation) {
        case DISPLAY_ORIENTATION_90:
            temp = x;
            x = mLocked.displayWidth - y;
            x =  oldViewport.deviceHeight - y;
            y = temp;
            break;
        case DISPLAY_ORIENTATION_180:
            x = mLocked.displayWidth - x;
            y = mLocked.displayHeight - y;
            x = oldViewport.deviceWidth - x;
            y = oldViewport.deviceHeight - y;
            break;
        case DISPLAY_ORIENTATION_270:
            temp = x;
            x = y;
            y = mLocked.displayHeight - temp;
            y = oldViewport.deviceWidth - temp;
            break;
        }

        // Perform the new rotation.
        switch (orientation) {
        switch (viewport.orientation) {
        case DISPLAY_ORIENTATION_90:
            temp = x;
            x = y;
            y = mLocked.displayWidth - temp;
            y = viewport.deviceHeight - temp;
            break;
        case DISPLAY_ORIENTATION_180:
            x = mLocked.displayWidth - x;
            y = mLocked.displayHeight - y;
            x = viewport.deviceWidth - x;
            y = viewport.deviceHeight - y;
            break;
        case DISPLAY_ORIENTATION_270:
            temp = x;
            x = mLocked.displayHeight - y;
            x = viewport.deviceWidth - y;
            y = temp;
            break;
        }
@@ -444,7 +438,6 @@ void PointerController::setDisplayViewport(int32_t width, int32_t height, int32_
        // and save the results.
        mLocked.pointerX = x - 0.5f;
        mLocked.pointerY = y - 0.5f;
        mLocked.displayOrientation = orientation;
    }

    updatePointerLocked();
@@ -614,11 +607,16 @@ void PointerController::removeInactivityTimeoutLocked() {
    mLooper->removeMessages(mHandler, MSG_INACTIVITY_TIMEOUT);
}

void PointerController::updatePointerLocked() {
void PointerController::updatePointerLocked() REQUIRES(mLock) {
    if (!mLocked.viewport.isValid()) {
        return;
    }

    mSpriteController->openTransaction();

    mLocked.pointerSprite->setLayer(Sprite::BASE_LAYER_POINTER);
    mLocked.pointerSprite->setPosition(mLocked.pointerX, mLocked.pointerY);
    mLocked.pointerSprite->setDisplayId(mLocked.viewport.displayId);

    if (mLocked.pointerAlpha > 0) {
        mLocked.pointerSprite->setAlpha(mLocked.pointerAlpha);
@@ -729,8 +727,18 @@ void PointerController::fadeOutAndReleaseAllSpotsLocked() {
    }
}

void PointerController::loadResources() {
void PointerController::loadResourcesLocked() REQUIRES(mLock) {
    mPolicy->loadPointerResources(&mResources);

    if (mLocked.presentation == PRESENTATION_POINTER) {
        mLocked.additionalMouseResources.clear();
        mLocked.animationResources.clear();
        mPolicy->loadPointerIcon(&mLocked.pointerIcon);
        mPolicy->loadAdditionalMouseResources(&mLocked.additionalMouseResources,
                                              &mLocked.animationResources);
    }

    mLocked.pointerIconChanged = true;
}


+6 −6
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@
#include <vector>

#include <ui/DisplayInfo.h>
#include <input/DisplayViewport.h>
#include <input/Input.h>
#include <PointerControllerInterface.h>
#include <utils/BitSet.h>
@@ -96,6 +97,7 @@ public:
    virtual int32_t getButtonState() const;
    virtual void setPosition(float x, float y);
    virtual void getPosition(float* outX, float* outY) const;
    virtual int32_t getDisplayId() const;
    virtual void fade(Transition transition);
    virtual void unfade(Transition transition);

@@ -106,7 +108,7 @@ public:

    void updatePointerIcon(int32_t iconId);
    void setCustomPointerIcon(const SpriteIcon& icon);
    void setDisplayViewport(int32_t width, int32_t height, int32_t orientation);
    void setDisplayViewport(const DisplayViewport& viewport);
    void setInactivityTimeout(InactivityTimeout inactivityTimeout);
    void reloadPointerResources();

@@ -156,9 +158,7 @@ private:
        size_t animationFrameIndex;
        nsecs_t lastFrameUpdatedTime;

        int32_t displayWidth;
        int32_t displayHeight;
        int32_t displayOrientation;
        DisplayViewport viewport;

        InactivityTimeout inactivityTimeout;

@@ -182,7 +182,7 @@ private:

        Vector<Spot*> spots;
        Vector<sp<Sprite> > recycledSprites;
    } mLocked;
    } mLocked GUARDED_BY(mLock);

    bool getBoundsLocked(float* outMinX, float* outMinY, float* outMaxX, float* outMaxY) const;
    void setPositionLocked(float x, float y);
@@ -207,7 +207,7 @@ private:
    void fadeOutAndReleaseSpotLocked(Spot* spot);
    void fadeOutAndReleaseAllSpotsLocked();

    void loadResources();
    void loadResourcesLocked();
};

} // namespace android
+21 −3
Original line number Diff line number Diff line
@@ -144,13 +144,16 @@ void SpriteController::doUpdateSprites() {
        }
    }

    // Resize sprites if needed.
    // Resize and/or reparent sprites if needed.
    SurfaceComposerClient::Transaction t;
    bool needApplyTransaction = false;
    for (size_t i = 0; i < numSprites; i++) {
        SpriteUpdate& update = updates.editItemAt(i);
        if (update.state.surfaceControl == nullptr) {
            continue;
        }

        if (update.state.surfaceControl != NULL && update.state.wantSurfaceVisible()) {
        if (update.state.wantSurfaceVisible()) {
            int32_t desiredWidth = update.state.icon.bitmap.width();
            int32_t desiredHeight = update.state.icon.bitmap.height();
            if (update.state.surfaceWidth < desiredWidth
@@ -170,6 +173,12 @@ void SpriteController::doUpdateSprites() {
                }
            }
        }

        // If surface is a new one, we have to set right layer stack.
        if (update.surfaceChanged || update.state.dirty & DIRTY_DISPLAY_ID) {
            t.setLayerStack(update.state.surfaceControl, update.state.displayId);
            needApplyTransaction = true;
        }
    }
    if (needApplyTransaction) {
        t.apply();
@@ -236,7 +245,7 @@ void SpriteController::doUpdateSprites() {
        if (update.state.surfaceControl != NULL && (becomingVisible || becomingHidden
                || (wantSurfaceVisibleAndDrawn && (update.state.dirty & (DIRTY_ALPHA
                        | DIRTY_POSITION | DIRTY_TRANSFORMATION_MATRIX | DIRTY_LAYER
                        | DIRTY_VISIBILITY | DIRTY_HOTSPOT))))) {
                        | DIRTY_VISIBILITY | DIRTY_HOTSPOT | DIRTY_DISPLAY_ID))))) {
            needApplyTransaction = true;

            if (wantSurfaceVisibleAndDrawn
@@ -445,6 +454,15 @@ void SpriteController::SpriteImpl::setTransformationMatrix(
    }
}

void SpriteController::SpriteImpl::setDisplayId(int32_t displayId) {
    AutoMutex _l(mController->mLock);

    if (mLocked.state.displayId != displayId) {
        mLocked.state.displayId = displayId;
        invalidateLocked(DIRTY_DISPLAY_ID);
    }
}

void SpriteController::SpriteImpl::invalidateLocked(uint32_t dirty) {
    bool wasDirty = mLocked.state.dirty;
    mLocked.state.dirty |= dirty;
+7 −1
Original line number Diff line number Diff line
@@ -125,6 +125,9 @@ public:

    /* Sets the sprite transformation matrix. */
    virtual void setTransformationMatrix(const SpriteTransformationMatrix& matrix) = 0;

    /* Sets the id of the display where the sprite should be shown. */
    virtual void setDisplayId(int32_t displayId) = 0;
};

/*
@@ -170,6 +173,7 @@ private:
        DIRTY_LAYER = 1 << 4,
        DIRTY_VISIBILITY = 1 << 5,
        DIRTY_HOTSPOT = 1 << 6,
        DIRTY_DISPLAY_ID = 1 << 7,
    };

    /* Describes the state of a sprite.
@@ -180,7 +184,7 @@ private:
    struct SpriteState {
        inline SpriteState() :
                dirty(0), visible(false),
                positionX(0), positionY(0), layer(0), alpha(1.0f),
                positionX(0), positionY(0), layer(0), alpha(1.0f), displayId(ADISPLAY_ID_DEFAULT),
                surfaceWidth(0), surfaceHeight(0), surfaceDrawn(false), surfaceVisible(false) {
        }

@@ -193,6 +197,7 @@ private:
        int32_t layer;
        float alpha;
        SpriteTransformationMatrix transformationMatrix;
        int32_t displayId;

        sp<SurfaceControl> surfaceControl;
        int32_t surfaceWidth;
@@ -225,6 +230,7 @@ private:
        virtual void setLayer(int32_t layer);
        virtual void setAlpha(float alpha);
        virtual void setTransformationMatrix(const SpriteTransformationMatrix& matrix);
        virtual void setDisplayId(int32_t displayId);

        inline const SpriteState& getStateLocked() const {
            return mLocked.state;
Loading