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

Commit e37267db authored by Melody Hsu's avatar Melody Hsu Committed by Android (Google) Code Review
Browse files

Merge "Refactor of screenshot code on main thread." into main

parents cdb7c45c b43b5837
Loading
Loading
Loading
Loading
+2 −2
Original line number Original line Diff line number Diff line
@@ -377,7 +377,7 @@ void RegionSamplingThread::captureSample() {
    constexpr bool kIsProtected = false;
    constexpr bool kIsProtected = false;


    if (const auto fenceResult =
    if (const auto fenceResult =
                mFlinger.captureScreenCommon(std::move(renderAreaFuture), getLayerSnapshots, buffer,
                mFlinger.captureScreenshot(std::move(renderAreaFuture), getLayerSnapshots, buffer,
                                           kRegionSampling, kGrayscale, kIsProtected, nullptr)
                                           kRegionSampling, kGrayscale, kIsProtected, nullptr)
                        .get();
                        .get();
        fenceResult.ok()) {
        fenceResult.ok()) {
+55 −49
Original line number Original line Diff line number Diff line
@@ -8063,6 +8063,19 @@ void SurfaceFlinger::captureLayers(const LayerCaptureArgs& args,
                        args.allowProtected, args.grayscale, captureListener);
                        args.allowProtected, args.grayscale, captureListener);
}
}


bool SurfaceFlinger::layersHasProtectedLayer(
        const std::vector<std::pair<Layer*, sp<LayerFE>>>& layers) const {
    bool protectedLayerFound = false;
    for (auto& [_, layerFe] : layers) {
        protectedLayerFound |=
                (layerFe->mSnapshot->isVisible && layerFe->mSnapshot->hasProtectedContent);
        if (protectedLayerFound) {
            break;
        }
    }
    return protectedLayerFound;
}

void SurfaceFlinger::captureScreenCommon(RenderAreaFuture renderAreaFuture,
void SurfaceFlinger::captureScreenCommon(RenderAreaFuture renderAreaFuture,
                                         GetLayerSnapshotsFunction getLayerSnapshots,
                                         GetLayerSnapshotsFunction getLayerSnapshots,
                                         ui::Size bufferSize, ui::PixelFormat reqPixelFormat,
                                         ui::Size bufferSize, ui::PixelFormat reqPixelFormat,
@@ -8078,6 +8091,9 @@ void SurfaceFlinger::captureScreenCommon(RenderAreaFuture renderAreaFuture,
        return;
        return;
    }
    }


    // Snapshots must be taken from the main thread.
    auto layers = mScheduler->schedule([=]() { return getLayerSnapshots(); }).get();

    // Loop over all visible layers to see whether there's any protected layer. A protected layer is
    // Loop over all visible layers to see whether there's any protected layer. A protected layer is
    // typically a layer with DRM contents, or have the GRALLOC_USAGE_PROTECTED set on the buffer.
    // typically a layer with DRM contents, or have the GRALLOC_USAGE_PROTECTED set on the buffer.
    // A protected layer has no implication on whether it's secure, which is explicitly set by
    // A protected layer has no implication on whether it's secure, which is explicitly set by
@@ -8085,18 +8101,7 @@ void SurfaceFlinger::captureScreenCommon(RenderAreaFuture renderAreaFuture,
    const bool supportsProtected = getRenderEngine().supportsProtectedContent();
    const bool supportsProtected = getRenderEngine().supportsProtectedContent();
    bool hasProtectedLayer = false;
    bool hasProtectedLayer = false;
    if (allowProtected && supportsProtected) {
    if (allowProtected && supportsProtected) {
        hasProtectedLayer = mScheduler
        hasProtectedLayer = layersHasProtectedLayer(layers);
                                    ->schedule([=]() {
                                        bool protectedLayerFound = false;
                                        auto layers = getLayerSnapshots();
                                        for (auto& [_, layerFe] : layers) {
                                            protectedLayerFound |=
                                                    (layerFe->mSnapshot->isVisible &&
                                                     layerFe->mSnapshot->hasProtectedContent);
                                        }
                                        return protectedLayerFound;
                                    })
                                    .get();
    }
    }
    const bool isProtected = hasProtectedLayer && allowProtected && supportsProtected;
    const bool isProtected = hasProtectedLayer && allowProtected && supportsProtected;
    const uint32_t usage = GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_RENDER |
    const uint32_t usage = GRALLOC_USAGE_HW_COMPOSER | GRALLOC_USAGE_HW_RENDER |
@@ -8121,20 +8126,19 @@ void SurfaceFlinger::captureScreenCommon(RenderAreaFuture renderAreaFuture,
            renderengine::impl::ExternalTexture>(buffer, getRenderEngine(),
            renderengine::impl::ExternalTexture>(buffer, getRenderEngine(),
                                                 renderengine::impl::ExternalTexture::Usage::
                                                 renderengine::impl::ExternalTexture::Usage::
                                                         WRITEABLE);
                                                         WRITEABLE);
    auto fence = captureScreenCommon(std::move(renderAreaFuture), getLayerSnapshots, texture,
    auto futureFence =
                                     false /* regionSampling */, grayscale, isProtected,
            captureScreenshot(std::move(renderAreaFuture), getLayerSnapshots, texture,
                                     captureListener);
                              false /* regionSampling */, grayscale, isProtected, captureListener);
    fence.get();
    futureFence.get();
}
}


ftl::SharedFuture<FenceResult> SurfaceFlinger::captureScreenCommon(
ftl::SharedFuture<FenceResult> SurfaceFlinger::captureScreenshot(
        RenderAreaFuture renderAreaFuture, GetLayerSnapshotsFunction getLayerSnapshots,
        RenderAreaFuture renderAreaFuture, GetLayerSnapshotsFunction getLayerSnapshots,
        const std::shared_ptr<renderengine::ExternalTexture>& buffer, bool regionSampling,
        const std::shared_ptr<renderengine::ExternalTexture>& buffer, bool regionSampling,
        bool grayscale, bool isProtected, const sp<IScreenCaptureListener>& captureListener) {
        bool grayscale, bool isProtected, const sp<IScreenCaptureListener>& captureListener) {
    ATRACE_CALL();
    ATRACE_CALL();


    auto future = mScheduler->schedule(
    auto takeScreenshotFn = [=, this, renderAreaFuture = std::move(renderAreaFuture)]() REQUIRES(
            [=, this, renderAreaFuture = std::move(renderAreaFuture)]() FTL_FAKE_GUARD(
                                    kMainThreadContext) mutable -> ftl::SharedFuture<FenceResult> {
                                    kMainThreadContext) mutable -> ftl::SharedFuture<FenceResult> {
        ScreenCaptureResults captureResults;
        ScreenCaptureResults captureResults;
        std::shared_ptr<RenderArea> renderArea = renderAreaFuture.get();
        std::shared_ptr<RenderArea> renderArea = renderAreaFuture.get();
@@ -8149,8 +8153,7 @@ ftl::SharedFuture<FenceResult> SurfaceFlinger::captureScreenCommon(


        ftl::SharedFuture<FenceResult> renderFuture;
        ftl::SharedFuture<FenceResult> renderFuture;
        renderArea->render([&]() FTL_FAKE_GUARD(kMainThreadContext) {
        renderArea->render([&]() FTL_FAKE_GUARD(kMainThreadContext) {
                    renderFuture =
            renderFuture = renderScreenImpl(renderArea, getLayerSnapshots, buffer, regionSampling,
                            renderScreenImpl(renderArea, getLayerSnapshots, buffer, regionSampling,
                                            grayscale, isProtected, captureResults);
                                            grayscale, isProtected, captureResults);
        });
        });


@@ -8166,7 +8169,10 @@ ftl::SharedFuture<FenceResult> SurfaceFlinger::captureScreenCommon(
                    .share();
                    .share();
        }
        }
        return renderFuture;
        return renderFuture;
            });
    };

    auto future =
            mScheduler->schedule(FTL_FAKE_GUARD(kMainThreadContext, std::move(takeScreenshotFn)));


    // Flatten nested futures.
    // Flatten nested futures.
    auto chain = ftl::Future(std::move(future)).then([](ftl::SharedFuture<FenceResult> future) {
    auto chain = ftl::Future(std::move(future)).then([](ftl::SharedFuture<FenceResult> future) {
+5 −1
Original line number Original line Diff line number Diff line
@@ -874,13 +874,17 @@ private:
    // Boot animation, on/off animations and screen capture
    // Boot animation, on/off animations and screen capture
    void startBootAnim();
    void startBootAnim();


    bool layersHasProtectedLayer(const std::vector<std::pair<Layer*, sp<LayerFE>>>& layers) const;

    void captureScreenCommon(RenderAreaFuture, GetLayerSnapshotsFunction, ui::Size bufferSize,
    void captureScreenCommon(RenderAreaFuture, GetLayerSnapshotsFunction, ui::Size bufferSize,
                             ui::PixelFormat, bool allowProtected, bool grayscale,
                             ui::PixelFormat, bool allowProtected, bool grayscale,
                             const sp<IScreenCaptureListener>&);
                             const sp<IScreenCaptureListener>&);
    ftl::SharedFuture<FenceResult> captureScreenCommon(

    ftl::SharedFuture<FenceResult> captureScreenshot(
            RenderAreaFuture, GetLayerSnapshotsFunction,
            RenderAreaFuture, GetLayerSnapshotsFunction,
            const std::shared_ptr<renderengine::ExternalTexture>&, bool regionSampling,
            const std::shared_ptr<renderengine::ExternalTexture>&, bool regionSampling,
            bool grayscale, bool isProtected, const sp<IScreenCaptureListener>&);
            bool grayscale, bool isProtected, const sp<IScreenCaptureListener>&);

    ftl::SharedFuture<FenceResult> renderScreenImpl(
    ftl::SharedFuture<FenceResult> renderScreenImpl(
            std::shared_ptr<const RenderArea>, GetLayerSnapshotsFunction,
            std::shared_ptr<const RenderArea>, GetLayerSnapshotsFunction,
            const std::shared_ptr<renderengine::ExternalTexture>&, bool regionSampling,
            const std::shared_ptr<renderengine::ExternalTexture>&, bool regionSampling,