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

Commit d691322f authored by Vishnu Nair's avatar Vishnu Nair
Browse files

SF: Introduce new frontend logic

Changes to allow creating layer snapshots using the new
and legacy frontend logic. Switching the logic is controlled
by debug flags. By default SF will continue to use the
legacy logic so there should be no functional changes with
this cl.

Bug: 238781169
Test: presubmit

Change-Id: I33a4de1b8e93fbfe02ac1787d57d6e07a8c3ef26
parent ba75fbf7
Loading
Loading
Loading
Loading
+19 −2
Original line number Original line Diff line number Diff line
@@ -391,7 +391,9 @@ bool LayerSnapshotBuilder::tryFastUpdate(const Args& args) {


void LayerSnapshotBuilder::updateSnapshots(const Args& args) {
void LayerSnapshotBuilder::updateSnapshots(const Args& args) {
    ATRACE_NAME("UpdateSnapshots");
    ATRACE_NAME("UpdateSnapshots");
    if (args.forceUpdate || args.displayChanges) {
    if (args.parentCrop) {
        mRootSnapshot.geomLayerBounds = *args.parentCrop;
    } else if (args.forceUpdate || args.displayChanges) {
        mRootSnapshot.geomLayerBounds = getMaxDisplayBounds(args.displays);
        mRootSnapshot.geomLayerBounds = getMaxDisplayBounds(args.displays);
    }
    }
    if (args.displayChanges) {
    if (args.displayChanges) {
@@ -618,7 +620,8 @@ void LayerSnapshotBuilder::updateSnapshot(LayerSnapshot& snapshot, const Args& a
             RequestedLayerState::Changes::AffectsChildren);
             RequestedLayerState::Changes::AffectsChildren);
    snapshot.changes = parentChanges | requested.changes;
    snapshot.changes = parentChanges | requested.changes;
    snapshot.isHiddenByPolicyFromParent = parentSnapshot.isHiddenByPolicyFromParent ||
    snapshot.isHiddenByPolicyFromParent = parentSnapshot.isHiddenByPolicyFromParent ||
            parentSnapshot.invalidTransform || requested.isHiddenByPolicy();
            parentSnapshot.invalidTransform || requested.isHiddenByPolicy() ||
            (args.excludeLayerIds.find(path.id) != args.excludeLayerIds.end());
    snapshot.contentDirty = requested.what & layer_state_t::CONTENT_DIRTY;
    snapshot.contentDirty = requested.what & layer_state_t::CONTENT_DIRTY;
    // TODO(b/238781169) scope down the changes to only buffer updates.
    // TODO(b/238781169) scope down the changes to only buffer updates.
    snapshot.hasReadyFrame =
    snapshot.hasReadyFrame =
@@ -983,6 +986,20 @@ void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor) c
    }
    }
}
}


// Visit each visible snapshot in z-order
void LayerSnapshotBuilder::forEachVisibleSnapshot(const ConstVisitor& visitor,
                                                  const LayerHierarchy& root) const {
    root.traverseInZOrder(
            [this, visitor](const LayerHierarchy&,
                            const LayerHierarchy::TraversalPath& traversalPath) -> bool {
                LayerSnapshot* snapshot = getSnapshot(traversalPath);
                if (snapshot && snapshot->isVisible) {
                    visitor(*snapshot);
                }
                return true;
            });
}

void LayerSnapshotBuilder::forEachVisibleSnapshot(const Visitor& visitor) {
void LayerSnapshotBuilder::forEachVisibleSnapshot(const Visitor& visitor) {
    for (int i = 0; i < mNumInterestingSnapshots; i++) {
    for (int i = 0; i < mNumInterestingSnapshots; i++) {
        std::unique_ptr<LayerSnapshot>& snapshot = mSnapshots.at((size_t)i);
        std::unique_ptr<LayerSnapshot>& snapshot = mSnapshots.at((size_t)i);
+6 −1
Original line number Original line Diff line number Diff line
@@ -36,7 +36,7 @@ namespace android::surfaceflinger::frontend {
class LayerSnapshotBuilder {
class LayerSnapshotBuilder {
public:
public:
    struct Args {
    struct Args {
        const LayerHierarchy& root;
        LayerHierarchy root;
        const LayerLifecycleManager& layerLifecycleManager;
        const LayerLifecycleManager& layerLifecycleManager;
        bool forceUpdate = false;
        bool forceUpdate = false;
        bool includeMetadata = false;
        bool includeMetadata = false;
@@ -46,6 +46,8 @@ public:
        const renderengine::ShadowSettings& globalShadowSettings;
        const renderengine::ShadowSettings& globalShadowSettings;
        bool supportsBlur = true;
        bool supportsBlur = true;
        bool forceFullDamage = false;
        bool forceFullDamage = false;
        std::optional<FloatRect> parentCrop = std::nullopt;
        std::unordered_set<uint32_t> excludeLayerIds;
    };
    };
    LayerSnapshotBuilder();
    LayerSnapshotBuilder();


@@ -65,6 +67,9 @@ public:
    // Visit each visible snapshot in z-order
    // Visit each visible snapshot in z-order
    void forEachVisibleSnapshot(const ConstVisitor& visitor) const;
    void forEachVisibleSnapshot(const ConstVisitor& visitor) const;


    // Visit each visible snapshot in z-order
    void forEachVisibleSnapshot(const ConstVisitor& visitor, const LayerHierarchy& root) const;

    typedef std::function<void(std::unique_ptr<LayerSnapshot>& snapshot)> Visitor;
    typedef std::function<void(std::unique_ptr<LayerSnapshot>& snapshot)> Visitor;
    // Visit each visible snapshot in z-order and move the snapshot if needed
    // Visit each visible snapshot in z-order and move the snapshot if needed
    void forEachVisibleSnapshot(const Visitor& visitor);
    void forEachVisibleSnapshot(const Visitor& visitor);
+26 −30
Original line number Original line Diff line number Diff line
@@ -146,7 +146,7 @@ Layer::Layer(const LayerCreationArgs& args)
        mLayerCreationFlags(args.flags),
        mLayerCreationFlags(args.flags),
        mBorderEnabled(false),
        mBorderEnabled(false),
        mTextureName(args.textureName),
        mTextureName(args.textureName),
        mLayerFE(args.flinger->getFactory().createLayerFE(mName)) {
        mLegacyLayerFE(args.flinger->getFactory().createLayerFE(mName)) {
    ALOGV("Creating Layer %s", getDebugName());
    ALOGV("Creating Layer %s", getDebugName());


    uint32_t layerFlags = 0;
    uint32_t layerFlags = 0;
@@ -3098,15 +3098,14 @@ bool Layer::setSidebandStream(const sp<NativeHandle>& sidebandStream) {
    return true;
    return true;
}
}


bool Layer::setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles) {
bool Layer::setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& handles,
                                             bool willPresent) {
    // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
    // If there is no handle, we will not send a callback so reset mReleasePreviousBuffer and return
    if (handles.empty()) {
    if (handles.empty()) {
        mReleasePreviousBuffer = false;
        mReleasePreviousBuffer = false;
        return false;
        return false;
    }
    }


    const bool willPresent = willPresentCurrentTransaction();

    for (const auto& handle : handles) {
    for (const auto& handle : handles) {
        // If this transaction set a buffer on this layer, release its previous buffer
        // If this transaction set a buffer on this layer, release its previous buffer
        handle->releasePreviousBuffer = mReleasePreviousBuffer;
        handle->releasePreviousBuffer = mReleasePreviousBuffer;
@@ -3180,11 +3179,10 @@ bool Layer::fenceHasSignaled() const {
    return fenceSignaled;
    return fenceSignaled;
}
}


bool Layer::onPreComposition(nsecs_t refreshStartTime) {
void Layer::onPreComposition(nsecs_t refreshStartTime) {
    for (const auto& handle : mDrawingState.callbackHandles) {
    for (const auto& handle : mDrawingState.callbackHandles) {
        handle->refreshStartTime = refreshStartTime;
        handle->refreshStartTime = refreshStartTime;
    }
    }
    return hasReadyFrame();
}
}


void Layer::setAutoRefresh(bool autoRefresh) {
void Layer::setAutoRefresh(bool autoRefresh) {
@@ -3570,7 +3568,7 @@ bool Layer::isHdrY410() const {


sp<LayerFE> Layer::getCompositionEngineLayerFE() const {
sp<LayerFE> Layer::getCompositionEngineLayerFE() const {
    // There's no need to get a CE Layer if the layer isn't going to draw anything.
    // There's no need to get a CE Layer if the layer isn't going to draw anything.
    return hasSomethingToDraw() ? mLayerFE : nullptr;
    return hasSomethingToDraw() ? mLegacyLayerFE : nullptr;
}
}


const LayerSnapshot* Layer::getLayerSnapshot() const {
const LayerSnapshot* Layer::getLayerSnapshot() const {
@@ -3581,16 +3579,36 @@ LayerSnapshot* Layer::editLayerSnapshot() {
    return mSnapshot.get();
    return mSnapshot.get();
}
}


std::unique_ptr<frontend::LayerSnapshot> Layer::stealLayerSnapshot() {
    return std::move(mSnapshot);
}

void Layer::updateLayerSnapshot(std::unique_ptr<frontend::LayerSnapshot> snapshot) {
    mSnapshot = std::move(snapshot);
}

const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
const compositionengine::LayerFECompositionState* Layer::getCompositionState() const {
    return mSnapshot.get();
    return mSnapshot.get();
}
}


sp<LayerFE> Layer::copyCompositionEngineLayerFE() const {
sp<LayerFE> Layer::copyCompositionEngineLayerFE() const {
    auto result = mFlinger->getFactory().createLayerFE(mLayerFE->getDebugName());
    auto result = mFlinger->getFactory().createLayerFE(mName);
    result->mSnapshot = std::make_unique<LayerSnapshot>(*mSnapshot);
    result->mSnapshot = std::make_unique<LayerSnapshot>(*mSnapshot);
    return result;
    return result;
}
}


sp<LayerFE> Layer::getCompositionEngineLayerFE(
        const frontend::LayerHierarchy::TraversalPath& path) {
    for (auto& [p, layerFE] : mLayerFEs) {
        if (p == path) {
            return layerFE;
        }
    }
    auto layerFE = mFlinger->getFactory().createLayerFE(mName);
    mLayerFEs.emplace_back(path, layerFE);
    return layerFE;
}

void Layer::useSurfaceDamage() {
void Layer::useSurfaceDamage() {
    if (mFlinger->mForceFullDamage) {
    if (mFlinger->mForceFullDamage) {
        surfaceDamageRegion = Region::INVALID_REGION;
        surfaceDamageRegion = Region::INVALID_REGION;
@@ -3986,28 +4004,6 @@ void Layer::updateRelativeMetadataSnapshot(const LayerMetadata& relativeLayerMet
    }
    }
}
}


LayerSnapshotGuard::LayerSnapshotGuard(Layer* layer) : mLayer(layer) {
    if (mLayer) {
        mLayer->mLayerFE->mSnapshot = std::move(mLayer->mSnapshot);
    }
}

LayerSnapshotGuard::~LayerSnapshotGuard() {
    if (mLayer) {
        mLayer->mSnapshot = std::move(mLayer->mLayerFE->mSnapshot);
    }
}

LayerSnapshotGuard::LayerSnapshotGuard(LayerSnapshotGuard&& other) : mLayer(other.mLayer) {
    other.mLayer = nullptr;
}

LayerSnapshotGuard& LayerSnapshotGuard::operator=(LayerSnapshotGuard&& other) {
    mLayer = other.mLayer;
    other.mLayer = nullptr;
    return *this;
}

void Layer::setTrustedPresentationInfo(TrustedPresentationThresholds const& thresholds,
void Layer::setTrustedPresentationInfo(TrustedPresentationThresholds const& thresholds,
                                       TrustedPresentationListener const& listener) {
                                       TrustedPresentationListener const& listener) {
    bool hadTrustedPresentationListener = hasTrustedPresentationListener();
    bool hadTrustedPresentationListener = hasTrustedPresentationListener();
+9 −30
Original line number Original line Diff line number Diff line
@@ -307,7 +307,8 @@ public:
    bool setSurfaceDamageRegion(const Region& /*surfaceDamage*/);
    bool setSurfaceDamageRegion(const Region& /*surfaceDamage*/);
    bool setApi(int32_t /*api*/);
    bool setApi(int32_t /*api*/);
    bool setSidebandStream(const sp<NativeHandle>& /*sidebandStream*/);
    bool setSidebandStream(const sp<NativeHandle>& /*sidebandStream*/);
    bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& /*handles*/);
    bool setTransactionCompletedListeners(const std::vector<sp<CallbackHandle>>& /*handles*/,
                                          bool willPresent);
    virtual bool setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace);
    virtual bool setBackgroundColor(const half3& color, float alpha, ui::Dataspace dataspace);
    virtual bool setColorSpaceAgnostic(const bool agnostic);
    virtual bool setColorSpaceAgnostic(const bool agnostic);
    virtual bool setDimmingEnabled(const bool dimmingEnabled);
    virtual bool setDimmingEnabled(const bool dimmingEnabled);
@@ -328,9 +329,12 @@ public:


    virtual sp<LayerFE> getCompositionEngineLayerFE() const;
    virtual sp<LayerFE> getCompositionEngineLayerFE() const;
    virtual sp<LayerFE> copyCompositionEngineLayerFE() const;
    virtual sp<LayerFE> copyCompositionEngineLayerFE() const;
    sp<LayerFE> getCompositionEngineLayerFE(const frontend::LayerHierarchy::TraversalPath&);


    const frontend::LayerSnapshot* getLayerSnapshot() const;
    const frontend::LayerSnapshot* getLayerSnapshot() const;
    frontend::LayerSnapshot* editLayerSnapshot();
    frontend::LayerSnapshot* editLayerSnapshot();
    std::unique_ptr<frontend::LayerSnapshot> stealLayerSnapshot();
    void updateLayerSnapshot(std::unique_ptr<frontend::LayerSnapshot> snapshot);


    // If we have received a new buffer this frame, we will pass its surface
    // If we have received a new buffer this frame, we will pass its surface
    // damage down to hardware composer. Otherwise, we must send a region with
    // damage down to hardware composer. Otherwise, we must send a region with
@@ -512,7 +516,7 @@ public:
    // implements compositionengine::LayerFE
    // implements compositionengine::LayerFE
    const compositionengine::LayerFECompositionState* getCompositionState() const;
    const compositionengine::LayerFECompositionState* getCompositionState() const;
    bool fenceHasSignaled() const;
    bool fenceHasSignaled() const;
    bool onPreComposition(nsecs_t refreshStartTime);
    void onPreComposition(nsecs_t refreshStartTime);
    void onLayerDisplayed(ftl::SharedFuture<FenceResult>);
    void onLayerDisplayed(ftl::SharedFuture<FenceResult>);


    void setWasClientComposed(const sp<Fence>& fence) {
    void setWasClientComposed(const sp<Fence>& fence) {
@@ -832,6 +836,7 @@ public:
    void updateMetadataSnapshot(const LayerMetadata& parentMetadata);
    void updateMetadataSnapshot(const LayerMetadata& parentMetadata);
    void updateRelativeMetadataSnapshot(const LayerMetadata& relativeLayerMetadata,
    void updateRelativeMetadataSnapshot(const LayerMetadata& relativeLayerMetadata,
                                        std::unordered_set<Layer*>& visited);
                                        std::unordered_set<Layer*>& visited);
    bool willPresentCurrentTransaction() const;


protected:
protected:
    // For unit tests
    // For unit tests
@@ -1037,8 +1042,6 @@ private:
    // Crop that applies to the buffer
    // Crop that applies to the buffer
    Rect computeBufferCrop(const State& s);
    Rect computeBufferCrop(const State& s);


    bool willPresentCurrentTransaction() const;

    void callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener,
    void callReleaseBufferCallback(const sp<ITransactionCompletedListener>& listener,
                                   const sp<GraphicBuffer>& buffer, uint64_t framenumber,
                                   const sp<GraphicBuffer>& buffer, uint64_t framenumber,
                                   const sp<Fence>& releaseFence,
                                   const sp<Fence>& releaseFence,
@@ -1146,34 +1149,10 @@ private:
    // not specify a destination frame.
    // not specify a destination frame.
    ui::Transform mRequestedTransform;
    ui::Transform mRequestedTransform;


    sp<LayerFE> mLayerFE;
    sp<LayerFE> mLegacyLayerFE;
    std::vector<std::pair<frontend::LayerHierarchy::TraversalPath, sp<LayerFE>>> mLayerFEs;
    std::unique_ptr<frontend::LayerSnapshot> mSnapshot =
    std::unique_ptr<frontend::LayerSnapshot> mSnapshot =
            std::make_unique<frontend::LayerSnapshot>();
            std::make_unique<frontend::LayerSnapshot>();

    friend class LayerSnapshotGuard;
};

// LayerSnapshotGuard manages the movement of LayerSnapshot between a Layer and its corresponding
// LayerFE. This class must be used whenever LayerFEs are passed to CompositionEngine. Instances of
// LayerSnapshotGuard should only be constructed on the main thread and should not be moved outside
// the main thread.
//
// Moving the snapshot instead of sharing common state prevents use of LayerFE outside the main
// thread by making errors obvious (i.e. use outside the main thread results in SEGFAULTs due to
// nullptr dereference).
class LayerSnapshotGuard {
public:
    LayerSnapshotGuard(Layer* layer) REQUIRES(kMainThreadContext);
    ~LayerSnapshotGuard() REQUIRES(kMainThreadContext);

    LayerSnapshotGuard(const LayerSnapshotGuard&) = delete;
    LayerSnapshotGuard& operator=(const LayerSnapshotGuard&) = delete;

    LayerSnapshotGuard(LayerSnapshotGuard&& other) REQUIRES(kMainThreadContext);
    LayerSnapshotGuard& operator=(LayerSnapshotGuard&& other) REQUIRES(kMainThreadContext);

private:
    Layer* mLayer;
};
};


std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate);
std::ostream& operator<<(std::ostream& stream, const Layer::FrameRate& rate);
+8 −1
Original line number Original line Diff line number Diff line
@@ -69,6 +69,14 @@ Rect LayerRenderArea::getSourceCrop() const {
void LayerRenderArea::render(std::function<void()> drawLayers) {
void LayerRenderArea::render(std::function<void()> drawLayers) {
    using namespace std::string_literals;
    using namespace std::string_literals;


    if (!mChildrenOnly) {
        mTransform = mLayer->getTransform().inverse();
    }

    if (mFlinger.mLayerLifecycleManagerEnabled) {
        drawLayers();
        return;
    }
    // If layer is offscreen, update mirroring info if it exists
    // If layer is offscreen, update mirroring info if it exists
    if (mLayer->isRemovedFromCurrentState()) {
    if (mLayer->isRemovedFromCurrentState()) {
        mLayer->traverse(LayerVector::StateSet::Drawing,
        mLayer->traverse(LayerVector::StateSet::Drawing,
@@ -78,7 +86,6 @@ void LayerRenderArea::render(std::function<void()> drawLayers) {
    }
    }


    if (!mChildrenOnly) {
    if (!mChildrenOnly) {
        mTransform = mLayer->getTransform().inverse();
        // If the layer is offscreen, compute bounds since we don't compute bounds for offscreen
        // If the layer is offscreen, compute bounds since we don't compute bounds for offscreen
        // layers in a regular cycles.
        // layers in a regular cycles.
        if (mLayer->isRemovedFromCurrentState()) {
        if (mLayer->isRemovedFromCurrentState()) {
Loading