1 /*
2  * Copyright 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include <android/gui/CachingHint.h>
20 #include <gui/LayerMetadata.h>
21 #include "FrontEnd/LayerSnapshot.h"
22 #include "compositionengine/LayerFE.h"
23 #include "compositionengine/LayerFECompositionState.h"
24 #include "renderengine/LayerSettings.h"
25 #include "ui/LayerStack.h"
26 
27 #include <ftl/future.h>
28 
29 namespace android {
30 
31 struct CompositionResult {
32     std::vector<std::pair<ftl::SharedFuture<FenceResult>, ui::LayerStack>> releaseFences;
33     sp<Fence> lastClientCompositionFence = nullptr;
34 };
35 
36 class LayerFE : public virtual RefBase, public virtual compositionengine::LayerFE {
37 public:
38     LayerFE(const std::string& name);
39     virtual ~LayerFE();
40 
41     // compositionengine::LayerFE overrides
42     const compositionengine::LayerFECompositionState* getCompositionState() const override;
43     bool onPreComposition(bool updatingOutputGeometryThisFrame) override;
44     void onLayerDisplayed(ftl::SharedFuture<FenceResult>, ui::LayerStack) override;
45     const char* getDebugName() const override;
46     int32_t getSequence() const override;
47     bool hasRoundedCorners() const override;
48     void setWasClientComposed(const sp<Fence>&) override;
49     const gui::LayerMetadata* getMetadata() const override;
50     const gui::LayerMetadata* getRelativeMetadata() const override;
51     std::optional<compositionengine::LayerFE::LayerSettings> prepareClientComposition(
52             compositionengine::LayerFE::ClientCompositionTargetSettings&) const;
53     CompositionResult&& stealCompositionResult();
54     ftl::Future<FenceResult> createReleaseFenceFuture() override;
55     void setReleaseFence(const FenceResult& releaseFence) override;
56     LayerFE::ReleaseFencePromiseStatus getReleaseFencePromiseStatus() override;
57 
58     std::unique_ptr<surfaceflinger::frontend::LayerSnapshot> mSnapshot;
59 
60 private:
61     std::optional<compositionengine::LayerFE::LayerSettings> prepareClientCompositionInternal(
62             compositionengine::LayerFE::ClientCompositionTargetSettings&) const;
63     // Modifies the passed in layer settings to clear the contents. If the blackout flag is set,
64     // the settings clears the content with a solid black fill.
65     void prepareClearClientComposition(LayerFE::LayerSettings&, bool blackout) const;
66     void prepareShadowClientComposition(LayerFE::LayerSettings& caster,
67                                         const Rect& layerStackRect) const;
68     void prepareBufferStateClientComposition(
69             compositionengine::LayerFE::LayerSettings&,
70             compositionengine::LayerFE::ClientCompositionTargetSettings&) const;
71     void prepareEffectsClientComposition(
72             compositionengine::LayerFE::LayerSettings&,
73             compositionengine::LayerFE::ClientCompositionTargetSettings&) const;
74 
hasEffect()75     bool hasEffect() const { return fillsColor() || drawShadows() || hasBlur(); }
76     bool hasBufferOrSidebandStream() const;
77 
78     bool fillsColor() const;
79     bool hasBlur() const;
80     bool drawShadows() const;
81 
82     const sp<GraphicBuffer> getBuffer() const;
83 
84     CompositionResult mCompositionResult;
85     std::string mName;
86     std::promise<FenceResult> mReleaseFence;
87     ReleaseFencePromiseStatus mReleaseFencePromiseStatus = ReleaseFencePromiseStatus::UNINITIALIZED;
88 };
89 
90 } // namespace android
91