• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 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 <compositionengine/CompositionEngine.h>
20 #include <compositionengine/Output.h>
21 #include <compositionengine/impl/ClientCompositionRequestCache.h>
22 #include <compositionengine/impl/OutputCompositionState.h>
23 #include <compositionengine/impl/planner/Planner.h>
24 #include <renderengine/DisplaySettings.h>
25 #include <renderengine/LayerSettings.h>
26 #include <memory>
27 #include <utility>
28 #include <vector>
29 
30 namespace android::compositionengine::impl {
31 
32 // The implementation class contains the common implementation, but does not
33 // actually contain the final output state.
34 class Output : public virtual compositionengine::Output {
35 public:
36     Output() = default;
37     ~Output() override;
38 
39     // compositionengine::Output overrides
40     bool isValid() const override;
41     std::optional<DisplayId> getDisplayId() const override;
42     void setCompositionEnabled(bool) override;
43     void setLayerCachingEnabled(bool) override;
44     void setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
45                        const Rect& orientedDisplaySpaceRect) override;
46     void setDisplaySize(const ui::Size&) override;
47     void setLayerStackFilter(uint32_t layerStackId, bool isInternal) override;
48     ui::Transform::RotationFlags getTransformHint() const override;
49 
50     void setColorTransform(const compositionengine::CompositionRefreshArgs&) override;
51     void setColorProfile(const ColorProfile&) override;
52     void setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) override;
53 
54     void dump(std::string&) const override;
55     void dumpPlannerInfo(const Vector<String16>& args, std::string&) const override;
56 
57     const std::string& getName() const override;
58     void setName(const std::string&) override;
59 
60     compositionengine::DisplayColorProfile* getDisplayColorProfile() const override;
61     void setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile>) override;
62 
63     compositionengine::RenderSurface* getRenderSurface() const override;
64     void setRenderSurface(std::unique_ptr<compositionengine::RenderSurface>) override;
65 
66     Region getDirtyRegion(bool repaintEverything) const override;
67     bool belongsInOutput(std::optional<uint32_t>, bool) const override;
68     bool belongsInOutput(const sp<LayerFE>&) const override;
69 
70     compositionengine::OutputLayer* getOutputLayerForLayer(const sp<LayerFE>&) const override;
71 
72     void setReleasedLayers(ReleasedLayers&&) override;
73 
74     void prepare(const CompositionRefreshArgs&, LayerFESet&) override;
75     void present(const CompositionRefreshArgs&) override;
76 
77     void rebuildLayerStacks(const CompositionRefreshArgs&, LayerFESet&) override;
78     void collectVisibleLayers(const CompositionRefreshArgs&,
79                               compositionengine::Output::CoverageState&) override;
80     void ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>&,
81                                     compositionengine::Output::CoverageState&) override;
82     void setReleasedLayers(const compositionengine::CompositionRefreshArgs&) override;
83 
84     void updateLayerStateFromFE(const CompositionRefreshArgs&) const override;
85     void updateCompositionState(const compositionengine::CompositionRefreshArgs&) override;
86     void planComposition() override;
87     void writeCompositionState(const compositionengine::CompositionRefreshArgs&) override;
88     void updateColorProfile(const compositionengine::CompositionRefreshArgs&) override;
89     void beginFrame() override;
90     void prepareFrame() override;
91     void devOptRepaintFlash(const CompositionRefreshArgs&) override;
92     void finishFrame(const CompositionRefreshArgs&) override;
93     std::optional<base::unique_fd> composeSurfaces(
94             const Region&, const compositionengine::CompositionRefreshArgs& refreshArgs) override;
95     void postFramebuffer() override;
96     void renderCachedSets(const CompositionRefreshArgs&) override;
97     void cacheClientCompositionRequests(uint32_t) override;
98 
99     // Testing
100     const ReleasedLayers& getReleasedLayersForTest() const;
101     void setDisplayColorProfileForTest(std::unique_ptr<compositionengine::DisplayColorProfile>);
102     void setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface>);
plannerEnabled()103     bool plannerEnabled() const { return mPlanner != nullptr; }
104 
105 protected:
106     std::unique_ptr<compositionengine::OutputLayer> createOutputLayer(const sp<LayerFE>&) const;
107     std::optional<size_t> findCurrentOutputLayerForLayer(
108             const sp<compositionengine::LayerFE>&) const;
109     void chooseCompositionStrategy() override;
110     bool getSkipColorTransform() const override;
111     compositionengine::Output::FrameFences presentAndGetFrameFences() override;
112     std::vector<LayerFE::LayerSettings> generateClientCompositionRequests(
113             bool supportsProtectedContent, Region& clearRegion,
114             ui::Dataspace outputDataspace) override;
115     void appendRegionFlashRequests(const Region&, std::vector<LayerFE::LayerSettings>&) override;
116     void setExpensiveRenderingExpected(bool enabled) override;
117     void dumpBase(std::string&) const;
118 
119     // Implemented by the final implementation for the final state it uses.
120     virtual compositionengine::OutputLayer* ensureOutputLayer(std::optional<size_t>,
121                                                               const sp<LayerFE>&) = 0;
122     virtual compositionengine::OutputLayer* injectOutputLayerForTest(const sp<LayerFE>&) = 0;
123     virtual void finalizePendingOutputLayers() = 0;
124     virtual const compositionengine::CompositionEngine& getCompositionEngine() const = 0;
125     virtual void dumpState(std::string& out) const = 0;
126 
127 private:
128     void dirtyEntireOutput();
129     compositionengine::OutputLayer* findLayerRequestingBackgroundComposition() const;
130     ui::Dataspace getBestDataspace(ui::Dataspace*, bool*) const;
131     compositionengine::Output::ColorProfile pickColorProfile(
132             const compositionengine::CompositionRefreshArgs&) const;
133 
134     std::string mName;
135 
136     std::unique_ptr<compositionengine::DisplayColorProfile> mDisplayColorProfile;
137     std::unique_ptr<compositionengine::RenderSurface> mRenderSurface;
138 
139     ReleasedLayers mReleasedLayers;
140     OutputLayer* mLayerRequestingBackgroundBlur = nullptr;
141     std::unique_ptr<ClientCompositionRequestCache> mClientCompositionRequestCache;
142     std::unique_ptr<planner::Planner> mPlanner;
143 };
144 
145 // This template factory function standardizes the implementation details of the
146 // final class using the types actually required by the implementation. This is
147 // not possible to do in the base class as those types may not even be visible
148 // to the base code.
149 template <typename BaseOutput, typename CompositionEngine, typename... Args>
createOutputTemplated(const CompositionEngine & compositionEngine,Args...args)150 std::shared_ptr<BaseOutput> createOutputTemplated(const CompositionEngine& compositionEngine,
151                                                   Args... args) {
152     class Output final : public BaseOutput {
153     public:
154 // Clang incorrectly complains that these are unused.
155 #pragma clang diagnostic push
156 #pragma clang diagnostic ignored "-Wunused-local-typedef"
157 
158         using OutputCompositionState = std::remove_const_t<
159                 std::remove_reference_t<decltype(std::declval<BaseOutput>().getState())>>;
160         using OutputLayer = std::remove_pointer_t<decltype(
161                 std::declval<BaseOutput>().getOutputLayerOrderedByZByIndex(0))>;
162 
163 #pragma clang diagnostic pop
164 
165         explicit Output(const CompositionEngine& compositionEngine, Args... args)
166               : BaseOutput(std::forward<Args>(args)...), mCompositionEngine(compositionEngine) {}
167         ~Output() override = default;
168 
169     private:
170         // compositionengine::Output overrides
171         const OutputCompositionState& getState() const override { return mState; }
172 
173         OutputCompositionState& editState() override { return mState; }
174 
175         size_t getOutputLayerCount() const override {
176             return mCurrentOutputLayersOrderedByZ.size();
177         }
178 
179         OutputLayer* getOutputLayerOrderedByZByIndex(size_t index) const override {
180             if (index >= mCurrentOutputLayersOrderedByZ.size()) {
181                 return nullptr;
182             }
183             return mCurrentOutputLayersOrderedByZ[index].get();
184         }
185 
186         // compositionengine::impl::Output overrides
187         const CompositionEngine& getCompositionEngine() const override {
188             return mCompositionEngine;
189         };
190 
191         OutputLayer* ensureOutputLayer(std::optional<size_t> prevIndex,
192                                        const sp<LayerFE>& layerFE) {
193             auto outputLayer = (prevIndex && *prevIndex <= mCurrentOutputLayersOrderedByZ.size())
194                     ? std::move(mCurrentOutputLayersOrderedByZ[*prevIndex])
195                     : BaseOutput::createOutputLayer(layerFE);
196             auto result = outputLayer.get();
197             mPendingOutputLayersOrderedByZ.emplace_back(std::move(outputLayer));
198             return result;
199         }
200 
201         void finalizePendingOutputLayers() override {
202             // The pending layers are added in reverse order. Reverse them to
203             // get the back-to-front ordered list of layers.
204             std::reverse(mPendingOutputLayersOrderedByZ.begin(),
205                          mPendingOutputLayersOrderedByZ.end());
206 
207             mCurrentOutputLayersOrderedByZ = std::move(mPendingOutputLayersOrderedByZ);
208         }
209 
210         void dumpState(std::string& out) const override { mState.dump(out); }
211 
212         OutputLayer* injectOutputLayerForTest(const sp<LayerFE>& layerFE) override {
213             auto outputLayer = BaseOutput::createOutputLayer(layerFE);
214             auto result = outputLayer.get();
215             mCurrentOutputLayersOrderedByZ.emplace_back(std::move(outputLayer));
216             return result;
217         }
218 
219         // Note: This is declared as a private virtual non-override so it can be
220         // an override implementation in the unit tests, but otherwise is not an
221         // accessible override for the normal implementation.
222         virtual void injectOutputLayerForTest(std::unique_ptr<OutputLayer> outputLayer) {
223             mCurrentOutputLayersOrderedByZ.emplace_back(std::move(outputLayer));
224         }
225 
226         void clearOutputLayers() override {
227             mCurrentOutputLayersOrderedByZ.clear();
228             mPendingOutputLayersOrderedByZ.clear();
229         }
230 
231         const CompositionEngine& mCompositionEngine;
232         OutputCompositionState mState;
233         std::vector<std::unique_ptr<OutputLayer>> mCurrentOutputLayersOrderedByZ;
234         std::vector<std::unique_ptr<OutputLayer>> mPendingOutputLayersOrderedByZ;
235     };
236 
237     return std::make_shared<Output>(compositionEngine, std::forward<Args>(args)...);
238 }
239 
240 std::shared_ptr<Output> createOutput(const compositionengine::CompositionEngine&);
241 
242 } // namespace android::compositionengine::impl
243