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

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

Merge changes I02236342,I5e693d91,I0d3c09d4 into main

* changes:
  Consolidate layer snapshot functions for screenshots
  Encapsulate excludeLayerId logic into a function
  Cleaner ScreenshotArg struct init, function parameter cleanup
parents 2c540b72 5192a21b
Loading
Loading
Loading
Loading
+19 −18
Original line number Diff line number Diff line
@@ -339,8 +339,6 @@ void RegionSamplingThread::captureSample() {
        return layerFilterFn(snapshot.name.c_str(), snapshot.path.id, bounds, transform,
                             outStopTraversal);
    };
    auto getLayerSnapshotsFn =
            mFlinger.getLayerSnapshotsForScreenshots(layerStack, CaptureArgs::UNSET_UID, filterFn);

    std::shared_ptr<renderengine::ExternalTexture> buffer = nullptr;
    if (mCachedBuffer && mCachedBuffer->getBuffer()->getWidth() == sampledBounds.getWidth() &&
@@ -361,24 +359,27 @@ void RegionSamplingThread::captureSample() {
                                                             WRITEABLE);
    }

    constexpr bool kRegionSampling = true;
    constexpr bool kGrayscale = false;
    constexpr bool kIsProtected = false;

    SurfaceFlinger::ScreenshotArgs screenshotArgs;
    screenshotArgs.captureTypeVariant = displayWeak;
    screenshotArgs.displayIdVariant = std::nullopt;
    screenshotArgs.sourceCrop = sampledBounds.isEmpty() ? layerStackSpaceRect : sampledBounds;
    screenshotArgs.reqSize = sampledBounds.getSize();
    screenshotArgs.dataspace = ui::Dataspace::V0_SRGB;
    screenshotArgs.isSecure = true;
    screenshotArgs.seamlessTransition = false;
    SurfaceFlinger::ScreenshotArgs
            screenshotArgs{.captureTypeVariant = displayWeak,
                           .displayIdVariant = std::nullopt,
                           .snapshotRequest =
                                   SurfaceFlinger::SnapshotRequestArgs{.uid = gui::Uid::INVALID,
                                                                       .layerStack = layerStack,
                                                                       .snapshotFilterFn =
                                                                               filterFn},
                           .sourceCrop =
                                   sampledBounds.isEmpty() ? layerStackSpaceRect : sampledBounds,
                           .size = sampledBounds.getSize(),
                           .dataspace = ui::Dataspace::V0_SRGB,
                           .disableBlur = true,
                           .isGrayscale = false,
                           .isSecure = true,
                           .seamlessTransition = false,
                           .debugName = "RegionSampling"};

    std::vector<std::pair<Layer*, sp<LayerFE>>> layers;
    mFlinger.getSnapshotsFromMainThread(screenshotArgs, getLayerSnapshotsFn, layers);
    FenceResult fenceResult = mFlinger.captureScreenshot(screenshotArgs, buffer, kRegionSampling,
                                                         kGrayscale, kIsProtected, nullptr, layers)
                                      .get();
    mFlinger.getSnapshotsFromMainThread(screenshotArgs);
    FenceResult fenceResult = mFlinger.captureScreenshot(screenshotArgs, buffer, nullptr).get();
    if (fenceResult.ok()) {
        fenceResult.value()->waitForever(LOG_TAG);
    }
+9 −18
Original line number Diff line number Diff line
@@ -33,13 +33,12 @@ std::shared_ptr<ScreenCaptureOutput> createScreenCaptureOutput(ScreenCaptureOutp
            /* sourceCrop */ const Rect, ftl::Optional<DisplayIdVariant>,
            const compositionengine::Output::ColorProfile&,
            /* layerAlpha */ float,
            /* regionSampling */ bool>(args.compositionEngine, args.sourceCrop,
                                       args.displayIdVariant, args.colorProfile, args.layerAlpha,
                                       args.regionSampling,
            /* disableBlur */ bool>(args.compositionEngine, args.sourceCrop, args.displayIdVariant,
                                    args.colorProfile, args.layerAlpha, args.disableBlur,
                                    args.dimInGammaSpaceForEnhancedScreenshots,
                                    args.enableLocalTonemapping);
    output->editState().isSecure = args.isSecure;
    output->editState().isProtected = args.isProtected;
    output->editState().isProtected = args.buffer->getUsage() & GRALLOC_USAGE_PROTECTED;
    output->setCompositionEnabled(true);
    output->setLayerFilter({args.layerStack});
    output->setRenderSurface(std::make_unique<ScreenCaptureRenderSurface>(std::move(args.buffer)));
@@ -57,27 +56,19 @@ std::shared_ptr<ScreenCaptureOutput> createScreenCaptureOutput(ScreenCaptureOutp
    output->setDisplaySize({sourceCrop.getWidth(), sourceCrop.getHeight()});
    output->setProjection(orientation, sourceCrop,
                          {args.reqBufferSize.width, args.reqBufferSize.height});

    {
        std::string name = args.regionSampling ? "RegionSampling" : "ScreenCaptureOutput";
        if (const auto id = args.displayIdVariant.and_then(asDisplayIdOfType<DisplayId>)) {
            base::StringAppendF(&name, " for %" PRIu64, id->value);
        }
        output->setName(name);
    }
    output->setName(args.debugName);
    return output;
}

ScreenCaptureOutput::ScreenCaptureOutput(
        const Rect sourceCrop, ftl::Optional<DisplayIdVariant> displayIdVariant,
        const compositionengine::Output::ColorProfile& colorProfile, float layerAlpha,
        bool regionSampling, bool dimInGammaSpaceForEnhancedScreenshots,
        bool enableLocalTonemapping)
        bool disableBlur, bool dimInGammaSpaceForEnhancedScreenshots, bool enableLocalTonemapping)
      : mSourceCrop(sourceCrop),
        mDisplayIdVariant(displayIdVariant),
        mColorProfile(colorProfile),
        mLayerAlpha(layerAlpha),
        mRegionSampling(regionSampling),
        mDisableBlur(disableBlur),
        mDimInGammaSpaceForEnhancedScreenshots(dimInGammaSpaceForEnhancedScreenshots),
        mEnableLocalTonemapping(enableLocalTonemapping) {}

@@ -189,7 +180,7 @@ ScreenCaptureOutput::generateClientCompositionRequests(
        }
    }

    if (mRegionSampling) {
    if (mDisableBlur) {
        for (auto& layer : clientCompositionLayers) {
            layer.backgroundBlurRadius = 0;
            layer.blurRegions.clear();
+4 −4
Original line number Diff line number Diff line
@@ -37,12 +37,12 @@ struct ScreenCaptureOutputArgs {
    // Counterintuitively, when targetBrightness > 1.0 then dim the scene.
    float targetBrightness;
    float layerAlpha;
    bool regionSampling;
    bool disableBlur;
    bool treat170mAsSrgb;
    bool dimInGammaSpaceForEnhancedScreenshots;
    bool isSecure = false;
    bool isProtected = false;
    bool enableLocalTonemapping = false;
    std::string debugName;
};

// ScreenCaptureOutput is used to compose a set of layers into a preallocated buffer.
@@ -53,7 +53,7 @@ class ScreenCaptureOutput : public compositionengine::impl::Output {
public:
    ScreenCaptureOutput(const Rect sourceCrop, ftl::Optional<DisplayIdVariant> displayIdVariant,
                        const compositionengine::Output::ColorProfile& colorProfile,
                        float layerAlpha, bool regionSampling,
                        float layerAlpha, bool disableBlur,
                        bool dimInGammaSpaceForEnhancedScreenshots, bool enableLocalTonemapping);

    void updateColorProfile(const compositionengine::CompositionRefreshArgs&) override;
@@ -73,7 +73,7 @@ private:
    const ftl::Optional<DisplayIdVariant> mDisplayIdVariant;
    const compositionengine::Output::ColorProfile& mColorProfile;
    const float mLayerAlpha;
    const bool mRegionSampling;
    const bool mDisableBlur;
    const bool mDimInGammaSpaceForEnhancedScreenshots;
    const bool mEnableLocalTonemapping;
};
+173 −206

File changed.

Preview size limit exceeded, changes collapsed.

+44 −23
Original line number Diff line number Diff line
@@ -379,7 +379,6 @@ private:
    friend class TunnelModeEnabledReporterTest;

    using TransactionSchedule = scheduler::TransactionSchedule;
    using GetLayerSnapshotsFunction = std::function<std::vector<std::pair<Layer*, sp<LayerFE>>>()>;
    using DumpArgs = Vector<String16>;
    using Dumper = std::function<void(const DumpArgs&, bool asProto, std::string&)>;

@@ -879,6 +878,26 @@ private:

    using OutputCompositionState = compositionengine::impl::OutputCompositionState;

    struct SnapshotRequestArgs {
        // Uid initiating the screenshot request
        gui::Uid uid{gui::Uid::INVALID};

        std::optional<ui::LayerStack> layerStack{std::nullopt};

        std::optional<uint32_t> rootLayerId{std::nullopt};

        // IDs of layers that will be excluded from the screenshot
        std::unordered_set<uint32_t> excludeLayerIds{{}};

        // If true, transform is inverted from the parent layer snapshot
        bool childrenOnly{false};

        std::optional<FloatRect> parentCrop{std::nullopt};

        std::function<bool(const frontend::LayerSnapshot&, bool& outStopTraversal)>
                snapshotFilterFn{nullptr};
    };

    /*
     * Parameters used across screenshot methods.
     */
@@ -891,8 +910,11 @@ private:
        // Display ID of the display the result will be on
        ftl::Optional<DisplayIdVariant> displayIdVariant{std::nullopt};

        // If true, transform is inverted from the parent layer snapshot
        bool childrenOnly{false};
        // Arguments provided to screenshot function for getting layer snapshots
        SnapshotRequestArgs snapshotRequest;

        // List of all layer snapshots that are included in the screenshot
        std::vector<std::pair<Layer*, sp<LayerFE>>> layers;

        // Source crop of the render area
        Rect sourceCrop;
@@ -902,15 +924,25 @@ private:
        ui::Transform transform;

        // Size of the physical render area
        ui::Size reqSize;
        ui::Size size;

        // Composition dataspace of the render area
        ui::Dataspace dataspace;

        // Whether blur should be disabled, such as for region sampling
        bool disableBlur{false};

        // If true, screenshot is captured in grayscale
        bool isGrayscale{false};

        // If false, the secure layer is blacked out or skipped
        // when rendered to an insecure render area
        bool isSecure{false};

        // If true, includes protected layers which, if screenshotted, results
        // in a protected output buffer
        bool includeProtected{false};

        // If true, the render result may be used for system animations
        // that must preserve the exact colors of the display
        bool seamlessTransition{false};
@@ -926,30 +958,26 @@ private:

        // Current active render intent of the output composition state
        ui::RenderIntent renderIntent{ui::RenderIntent::COLORIMETRIC};

        std::string debugName;
    };

    bool getSnapshotsFromMainThread(ScreenshotArgs& args,
                                    GetLayerSnapshotsFunction getLayerSnapshotsFn,
                                    std::vector<std::pair<Layer*, sp<LayerFE>>>& layers);
    bool getSnapshotsFromMainThread(ScreenshotArgs& args);

    void captureScreenCommon(ScreenshotArgs& args, GetLayerSnapshotsFunction, ui::Size bufferSize,
                             ui::PixelFormat, bool allowProtected, bool grayscale,
    void captureScreenCommon(ScreenshotArgs& args, ui::PixelFormat,
                             const sp<IScreenCaptureListener>&);

    bool getDisplayStateOnMainThread(ScreenshotArgs& args) REQUIRES(kMainThreadContext);

    ftl::SharedFuture<FenceResult> captureScreenshot(
            ScreenshotArgs& args, const std::shared_ptr<renderengine::ExternalTexture>& buffer,
            bool regionSampling, bool grayscale, bool isProtected,
            const sp<IScreenCaptureListener>& captureListener,
            const std::vector<std::pair<Layer*, sp<LayerFE>>>& layers,
            const std::shared_ptr<renderengine::ExternalTexture>& hdrBuffer = nullptr,
            const std::shared_ptr<renderengine::ExternalTexture>& gainmapBuffer = nullptr);

    ftl::SharedFuture<FenceResult> renderScreenImpl(
            ScreenshotArgs& args, const std::shared_ptr<renderengine::ExternalTexture>&,
            bool regionSampling, bool grayscale, bool isProtected, ScreenCaptureResults&,
            const std::vector<std::pair<Layer*, sp<LayerFE>>>& layers);
            ScreenCaptureResults&);

    void readPersistentProperties();

@@ -1482,16 +1510,9 @@ private:
        return hasDisplay(
                [](const auto& display) { return display.isHdrSdrRatioOverlayEnabled(); });
    }
    std::function<std::vector<std::pair<Layer*, sp<LayerFE>>>()> getLayerSnapshotsForScreenshots(
            std::optional<ui::LayerStack> layerStack, uint32_t uid,
            std::function<bool(const frontend::LayerSnapshot&, bool& outStopTraversal)>
                    snapshotFilterFn);
    std::function<std::vector<std::pair<Layer*, sp<LayerFE>>>()> getLayerSnapshotsForScreenshots(
            std::optional<ui::LayerStack> layerStack, uint32_t uid,
            std::unordered_set<uint32_t> excludeLayerIds);
    std::function<std::vector<std::pair<Layer*, sp<LayerFE>>>()> getLayerSnapshotsForScreenshots(
            uint32_t rootLayerId, uint32_t uid, std::unordered_set<uint32_t> excludeLayerIds,
            bool childrenOnly, const std::optional<FloatRect>& optionalParentCrop);

    std::vector<std::pair<Layer*, sp<LayerFE>>> getLayerSnapshotsForScreenshots(
            const SnapshotRequestArgs& args) REQUIRES(kMainThreadContext);

    const sp<WindowInfosListenerInvoker> mWindowInfosListenerInvoker;

Loading