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

Commit 93b122f4 authored by Chia-I Wu's avatar Chia-I Wu
Browse files

libgui: add a helper for GLConsumer::getCurrentCrop

Add static GLConsumer::scaleDownCrop for use with
NATIVE_WINDOW_SCALING_MODE_SCALE_CROP.  This also allows it to be
called from other consumers.

Test: libgui_test
Change-Id: I2b5f7d14038b36709b52991b7b7e9379117b708b
parent 3bd03ff5
Loading
Loading
Loading
Loading
+43 −39
Original line number Original line Diff line number Diff line
@@ -872,6 +872,46 @@ void GLConsumer::computeTransformMatrix(float outTransform[16],
    memcpy(outTransform, xform.asArray(), sizeof(xform));
    memcpy(outTransform, xform.asArray(), sizeof(xform));
}
}


Rect GLConsumer::scaleDownCrop(const Rect& crop, uint32_t bufferWidth, uint32_t bufferHeight) {
    Rect outCrop = crop;

    uint32_t newWidth = static_cast<uint32_t>(crop.width());
    uint32_t newHeight = static_cast<uint32_t>(crop.height());

    if (newWidth * bufferHeight > newHeight * bufferWidth) {
        newWidth = newHeight * bufferWidth / bufferHeight;
        ALOGV("too wide: newWidth = %d", newWidth);
    } else if (newWidth * bufferHeight < newHeight * bufferWidth) {
        newHeight = newWidth * bufferHeight / bufferWidth;
        ALOGV("too tall: newHeight = %d", newHeight);
    }

    uint32_t currentWidth = static_cast<uint32_t>(crop.width());
    uint32_t currentHeight = static_cast<uint32_t>(crop.height());

    // The crop is too wide
    if (newWidth < currentWidth) {
        uint32_t dw = currentWidth - newWidth;
        auto halfdw = dw / 2;
        outCrop.left += halfdw;
        // Not halfdw because it would subtract 1 too few when dw is odd
        outCrop.right -= (dw - halfdw);
        // The crop is too tall
    } else if (newHeight < currentHeight) {
        uint32_t dh = currentHeight - newHeight;
        auto halfdh = dh / 2;
        outCrop.top += halfdh;
        // Not halfdh because it would subtract 1 too few when dh is odd
        outCrop.bottom -= (dh - halfdh);
    }

    ALOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
            outCrop.left, outCrop.top,
            outCrop.right,outCrop.bottom);

    return outCrop;
}

nsecs_t GLConsumer::getTimestamp() {
nsecs_t GLConsumer::getTimestamp() {
    GLC_LOGV("getTimestamp");
    GLC_LOGV("getTimestamp");
    Mutex::Autolock lock(mMutex);
    Mutex::Autolock lock(mMutex);
@@ -903,45 +943,9 @@ sp<GraphicBuffer> GLConsumer::getCurrentBuffer(int* outSlot) const {


Rect GLConsumer::getCurrentCrop() const {
Rect GLConsumer::getCurrentCrop() const {
    Mutex::Autolock lock(mMutex);
    Mutex::Autolock lock(mMutex);

    return (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP)
    Rect outCrop = mCurrentCrop;
        ? scaleDownCrop(mCurrentCrop, mDefaultWidth, mDefaultHeight)
    if (mCurrentScalingMode == NATIVE_WINDOW_SCALING_MODE_SCALE_CROP) {
        : mCurrentCrop;
        uint32_t newWidth = static_cast<uint32_t>(mCurrentCrop.width());
        uint32_t newHeight = static_cast<uint32_t>(mCurrentCrop.height());

        if (newWidth * mDefaultHeight > newHeight * mDefaultWidth) {
            newWidth = newHeight * mDefaultWidth / mDefaultHeight;
            GLC_LOGV("too wide: newWidth = %d", newWidth);
        } else if (newWidth * mDefaultHeight < newHeight * mDefaultWidth) {
            newHeight = newWidth * mDefaultHeight / mDefaultWidth;
            GLC_LOGV("too tall: newHeight = %d", newHeight);
        }

        uint32_t currentWidth = static_cast<uint32_t>(mCurrentCrop.width());
        uint32_t currentHeight = static_cast<uint32_t>(mCurrentCrop.height());

        // The crop is too wide
        if (newWidth < currentWidth) {
            uint32_t dw = currentWidth - newWidth;
            auto halfdw = dw / 2;
            outCrop.left += halfdw;
            // Not halfdw because it would subtract 1 too few when dw is odd
            outCrop.right -= (dw - halfdw);
        // The crop is too tall
        } else if (newHeight < currentHeight) {
            uint32_t dh = currentHeight - newHeight;
            auto halfdh = dh / 2;
            outCrop.top += halfdh;
            // Not halfdh because it would subtract 1 too few when dh is odd
            outCrop.bottom -= (dh - halfdh);
        }

        GLC_LOGV("getCurrentCrop final crop [%d,%d,%d,%d]",
            outCrop.left, outCrop.top,
            outCrop.right,outCrop.bottom);
    }

    return outCrop;
}
}


uint32_t GLConsumer::getCurrentTransform() const {
uint32_t GLConsumer::getCurrentTransform() const {
+4 −0
Original line number Original line Diff line number Diff line
@@ -138,6 +138,10 @@ public:
            const sp<GraphicBuffer>& buf, const Rect& cropRect,
            const sp<GraphicBuffer>& buf, const Rect& cropRect,
            uint32_t transform, bool filtering);
            uint32_t transform, bool filtering);


    // Scale the crop down horizontally or vertically such that it has the
    // same aspect ratio as the buffer does.
    static Rect scaleDownCrop(const Rect& crop, uint32_t bufferWidth, uint32_t bufferHeight);

    // getTimestamp retrieves the timestamp associated with the texture image
    // getTimestamp retrieves the timestamp associated with the texture image
    // set by the most recent call to updateTexImage.
    // set by the most recent call to updateTexImage.
    //
    //