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

Commit 5d2ca663 authored by Yiwei Zhang's avatar Yiwei Zhang
Browse files

Get screenrecord to exclude black cutout

Bug: b/112869712
Test: adb shell screenrecord
Change-Id: I69b497b81cfca93a8ad9ed46f25cdbb5dda81a89
Merged-In: I69b497b81cfca93a8ad9ed46f25cdbb5dda81a89
parent 3e499175
Loading
Loading
Loading
Loading
+48 −32
Original line number Original line Diff line number Diff line
@@ -139,14 +139,6 @@ static status_t configureSignals() {
    return NO_ERROR;
    return NO_ERROR;
}
}


/*
 * Returns "true" if the device is rotated 90 degrees.
 */
static bool isDeviceRotated(int orientation) {
    return orientation != DISPLAY_ORIENTATION_0 &&
            orientation != DISPLAY_ORIENTATION_180;
}

/*
/*
 * Configures and starts the MediaCodec encoder.  Obtains an input surface
 * Configures and starts the MediaCodec encoder.  Obtains an input surface
 * from the codec.
 * from the codec.
@@ -242,22 +234,11 @@ static status_t setDisplayProjection(
        const DisplayInfo& mainDpyInfo) {
        const DisplayInfo& mainDpyInfo) {


    // Set the region of the layer stack we're interested in, which in our
    // Set the region of the layer stack we're interested in, which in our
    // case is "all of it".  If the app is rotated (so that the width of the
    // case is "all of it".
    // app is based on the height of the display), reverse width/height.
    Rect layerStackRect(mainDpyInfo.w, mainDpyInfo.h);
    bool deviceRotated = isDeviceRotated(mainDpyInfo.orientation);
    uint32_t sourceWidth, sourceHeight;
    if (!deviceRotated) {
        sourceWidth = mainDpyInfo.w;
        sourceHeight = mainDpyInfo.h;
    } else {
        ALOGV("using rotated width/height");
        sourceHeight = mainDpyInfo.w;
        sourceWidth = mainDpyInfo.h;
    }
    Rect layerStackRect(sourceWidth, sourceHeight);


    // We need to preserve the aspect ratio of the display.
    // We need to preserve the aspect ratio of the display.
    float displayAspect = (float) sourceHeight / (float) sourceWidth;
    float displayAspect = (float) mainDpyInfo.h / (float) mainDpyInfo.w;




    // Set the way we map the output onto the display surface (which will
    // Set the way we map the output onto the display surface (which will
@@ -333,6 +314,22 @@ static status_t prepareVirtualDisplay(const DisplayInfo& mainDpyInfo,
    return NO_ERROR;
    return NO_ERROR;
}
}


/*
 * Set the main display width and height to the actual width and height
 */
static status_t getActualDisplaySize(const sp<IBinder>& mainDpy, DisplayInfo* mainDpyInfo) {
    Rect viewport;
    status_t err = SurfaceComposerClient::getDisplayViewport(mainDpy, &viewport);
    if (err != NO_ERROR) {
        fprintf(stderr, "ERROR: unable to get display viewport\n");
        return err;
    }
    mainDpyInfo->w = viewport.width();
    mainDpyInfo->h = viewport.height();

    return NO_ERROR;
}

/*
/*
 * Runs the MediaCodec encoder, sending the output to the MediaMuxer.  The
 * Runs the MediaCodec encoder, sending the output to the MediaMuxer.  The
 * input frames are coming from the virtual display as fast as SurfaceFlinger
 * input frames are coming from the virtual display as fast as SurfaceFlinger
@@ -403,15 +400,23 @@ static status_t runEncoder(const sp<MediaCodec>& encoder,
                    // useful stuff is hard to get at without a Dalvik VM.
                    // useful stuff is hard to get at without a Dalvik VM.
                    err = SurfaceComposerClient::getDisplayInfo(mainDpy,
                    err = SurfaceComposerClient::getDisplayInfo(mainDpy,
                            &mainDpyInfo);
                            &mainDpyInfo);
                    if (err == NO_ERROR) {
                        err = getActualDisplaySize(mainDpy, &mainDpyInfo);
                        if (err != NO_ERROR) {
                        if (err != NO_ERROR) {
                        ALOGW("getDisplayInfo(main) failed: %d", err);
                            fprintf(stderr, "ERROR: unable to set actual display size\n");
                    } else if (orientation != mainDpyInfo.orientation) {
                            return err;
                        }

                        if (orientation != mainDpyInfo.orientation) {
                            ALOGD("orientation changed, now %d", mainDpyInfo.orientation);
                            ALOGD("orientation changed, now %d", mainDpyInfo.orientation);
                            SurfaceComposerClient::Transaction t;
                            SurfaceComposerClient::Transaction t;
                            setDisplayProjection(t, virtualDpy, mainDpyInfo);
                            setDisplayProjection(t, virtualDpy, mainDpyInfo);
                            t.apply();
                            t.apply();
                            orientation = mainDpyInfo.orientation;
                            orientation = mainDpyInfo.orientation;
                        }
                        }
                    } else {
                        ALOGW("getDisplayInfo(main) failed: %d", err);
                    }
                }
                }


                // If the virtual display isn't providing us with timestamps,
                // If the virtual display isn't providing us with timestamps,
@@ -552,6 +557,10 @@ static FILE* prepareRawOutput(const char* fileName) {
    return rawFp;
    return rawFp;
}
}


static inline uint32_t floorToEven(uint32_t num) {
    return num & ~1;
}

/*
/*
 * Main "do work" start point.
 * Main "do work" start point.
 *
 *
@@ -579,6 +588,13 @@ static status_t recordScreen(const char* fileName) {
        fprintf(stderr, "ERROR: unable to get display characteristics\n");
        fprintf(stderr, "ERROR: unable to get display characteristics\n");
        return err;
        return err;
    }
    }

    err = getActualDisplaySize(mainDpy, &mainDpyInfo);
    if (err != NO_ERROR) {
        fprintf(stderr, "ERROR: unable to set actual display size\n");
        return err;
    }

    if (gVerbose) {
    if (gVerbose) {
        printf("Main display is %dx%d @%.2ffps (orientation=%u)\n",
        printf("Main display is %dx%d @%.2ffps (orientation=%u)\n",
                mainDpyInfo.w, mainDpyInfo.h, mainDpyInfo.fps,
                mainDpyInfo.w, mainDpyInfo.h, mainDpyInfo.fps,
@@ -586,12 +602,12 @@ static status_t recordScreen(const char* fileName) {
        fflush(stdout);
        fflush(stdout);
    }
    }


    bool rotated = isDeviceRotated(mainDpyInfo.orientation);
    // Encoder can't take odd number as config
    if (gVideoWidth == 0) {
    if (gVideoWidth == 0) {
        gVideoWidth = rotated ? mainDpyInfo.h : mainDpyInfo.w;
        gVideoWidth = floorToEven(mainDpyInfo.w);
    }
    }
    if (gVideoHeight == 0) {
    if (gVideoHeight == 0) {
        gVideoHeight = rotated ? mainDpyInfo.w : mainDpyInfo.h;
        gVideoHeight = floorToEven(mainDpyInfo.h);
    }
    }


    // Configure and start the encoder.
    // Configure and start the encoder.