• 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/LayerFECompositionState.h>
21 #include <compositionengine/Output.h>
22 #include <compositionengine/impl/ClientCompositionRequestCache.h>
23 #include <compositionengine/impl/GpuCompositionResult.h>
24 #include <compositionengine/impl/HwcAsyncWorker.h>
25 #include <compositionengine/impl/OutputCompositionState.h>
26 #include <compositionengine/impl/OutputLayerCompositionState.h>
27 #include <compositionengine/impl/planner/Planner.h>
28 #include <renderengine/DisplaySettings.h>
29 #include <renderengine/LayerSettings.h>
30 
31 #include <memory>
32 #include <utility>
33 #include <vector>
34 
35 namespace android::compositionengine::impl {
36 
37 // The implementation class contains the common implementation, but does not
38 // actually contain the final output state.
39 class Output : public virtual compositionengine::Output {
40 public:
41     Output() = default;
42     ~Output() override;
43 
44     // compositionengine::Output overrides
45     bool isValid() const override;
46     std::optional<DisplayId> getDisplayId() const override;
47     void setCompositionEnabled(bool) override;
48     void setLayerCachingEnabled(bool) override;
49     void setLayerCachingTexturePoolEnabled(bool) override;
50     void setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
51                        const Rect& orientedDisplaySpaceRect) override;
52     void setNextBrightness(float brightness) override;
53     void setDisplaySize(const ui::Size&) override;
54     void setLayerFilter(ui::LayerFilter) override;
55     ui::Transform::RotationFlags getTransformHint() const override;
56 
57     void setColorTransform(const compositionengine::CompositionRefreshArgs&) override;
58     void setColorProfile(const ColorProfile&) override;
59     void setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) override;
60 
61     void dump(std::string&) const override;
62     void dumpPlannerInfo(const Vector<String16>& args, std::string&) const override;
63 
64     const std::string& getName() const override;
65     void setName(const std::string&) override;
66 
67     compositionengine::DisplayColorProfile* getDisplayColorProfile() const override;
68     void setDisplayColorProfile(std::unique_ptr<compositionengine::DisplayColorProfile>) override;
69 
70     compositionengine::RenderSurface* getRenderSurface() const override;
71     void setRenderSurface(std::unique_ptr<compositionengine::RenderSurface>) override;
72 
73     Region getDirtyRegion() const override;
74 
75     bool includesLayer(ui::LayerFilter) const override;
76     bool includesLayer(const sp<LayerFE>&) const override;
77 
78     compositionengine::OutputLayer* getOutputLayerForLayer(const sp<LayerFE>&) const override;
79 
80     void setReleasedLayers(ReleasedLayers&&) override;
81 
82     void prepare(const CompositionRefreshArgs&, LayerFESet&) override;
83     ftl::Future<std::monostate> present(const CompositionRefreshArgs&) override;
supportsOffloadPresent()84     bool supportsOffloadPresent() const override { return false; }
85     void offloadPresentNextFrame() override;
86 
87     void uncacheBuffers(const std::vector<uint64_t>& bufferIdsToUncache) override;
88     void rebuildLayerStacks(const CompositionRefreshArgs&, LayerFESet&) override;
89     void collectVisibleLayers(const CompositionRefreshArgs&,
90                               compositionengine::Output::CoverageState&) override;
91     void ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>&,
92                                     compositionengine::Output::CoverageState&) override;
93     void setReleasedLayers(const compositionengine::CompositionRefreshArgs&) override;
94 
95     void updateCompositionState(const compositionengine::CompositionRefreshArgs&) override;
96     void planComposition() override;
97     void writeCompositionState(const compositionengine::CompositionRefreshArgs&) override;
98     void updateColorProfile(const compositionengine::CompositionRefreshArgs&) override;
99     void beginFrame() override;
100     void prepareFrame() override;
101     GpuCompositionResult prepareFrameAsync() override;
102     void devOptRepaintFlash(const CompositionRefreshArgs&) override;
103     void finishFrame(GpuCompositionResult&&) override;
104     std::optional<base::unique_fd> composeSurfaces(const Region&,
105                                                    std::shared_ptr<renderengine::ExternalTexture>,
106                                                    base::unique_fd&) override;
107     void presentFrameAndReleaseLayers(bool flushEvenWhenDisabled) override;
108     void renderCachedSets(const CompositionRefreshArgs&) override;
109     void cacheClientCompositionRequests(uint32_t) override;
110     bool canPredictCompositionStrategy(const CompositionRefreshArgs&) override;
111     void setPredictCompositionStrategy(bool) override;
112     void setTreat170mAsSrgb(bool) override;
113 
114     // Testing
115     const ReleasedLayers& getReleasedLayersForTest() const;
116     void setDisplayColorProfileForTest(std::unique_ptr<compositionengine::DisplayColorProfile>);
117     void setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface>);
plannerEnabled()118     bool plannerEnabled() const { return mPlanner != nullptr; }
119     virtual bool anyLayersRequireClientComposition() const;
120     virtual void updateProtectedContentState();
121     virtual bool dequeueRenderBuffer(base::unique_fd*,
122                                      std::shared_ptr<renderengine::ExternalTexture>*);
123     virtual std::future<bool> chooseCompositionStrategyAsync(
124             std::optional<android::HWComposer::DeviceRequestedChanges>*);
125     virtual void resetCompositionStrategy();
126     virtual ftl::Future<std::monostate> presentFrameAndReleaseLayersAsync(
127             bool flushEvenWhenDisabled);
128 
129 protected:
130     std::unique_ptr<compositionengine::OutputLayer> createOutputLayer(const sp<LayerFE>&) const;
131     std::optional<size_t> findCurrentOutputLayerForLayer(
132             const sp<compositionengine::LayerFE>&) const;
133     using DeviceRequestedChanges = android::HWComposer::DeviceRequestedChanges;
chooseCompositionStrategy(std::optional<android::HWComposer::DeviceRequestedChanges> *)134     bool chooseCompositionStrategy(
135             std::optional<android::HWComposer::DeviceRequestedChanges>*) override {
136         return true;
137     };
applyCompositionStrategy(const std::optional<DeviceRequestedChanges> &)138     void applyCompositionStrategy(const std::optional<DeviceRequestedChanges>&) override{};
139     bool getSkipColorTransform() const override;
140     compositionengine::Output::FrameFences presentFrame() override;
executeCommands()141     void executeCommands() override {}
142     virtual renderengine::DisplaySettings generateClientCompositionDisplaySettings(
143             const std::shared_ptr<renderengine::ExternalTexture>& buffer) const;
144     std::vector<LayerFE::LayerSettings> generateClientCompositionRequests(
145             bool supportsProtectedContent, ui::Dataspace outputDataspace,
146             std::vector<LayerFE*>& outLayerFEs) override;
147     void appendRegionFlashRequests(const Region&, std::vector<LayerFE::LayerSettings>&) override;
148     void setExpensiveRenderingExpected(bool enabled) override;
149     void setHintSessionGpuStart(TimePoint startTime) override;
150     void setHintSessionGpuFence(std::unique_ptr<FenceTime>&& gpuFence) override;
151     void setHintSessionRequiresRenderEngine(bool requiresRenderEngine) override;
152     bool isPowerHintSessionEnabled() override;
153     bool isPowerHintSessionGpuReportingEnabled() override;
154     void dumpBase(std::string&) const;
155 
156     // Implemented by the final implementation for the final state it uses.
157     virtual compositionengine::OutputLayer* ensureOutputLayer(std::optional<size_t>,
158                                                               const sp<LayerFE>&) = 0;
159     virtual compositionengine::OutputLayer* injectOutputLayerForTest(const sp<LayerFE>&) = 0;
160     virtual void finalizePendingOutputLayers() = 0;
161     virtual const compositionengine::CompositionEngine& getCompositionEngine() const = 0;
162     virtual void dumpState(std::string& out) const = 0;
163 
164     bool mustRecompose() const;
165 
getNamePlusId()166     const std::string& getNamePlusId() const { return mNamePlusId; }
167 
168 private:
169     void dirtyEntireOutput();
170     compositionengine::OutputLayer* findLayerRequestingBackgroundComposition() const;
171     void finishPrepareFrame();
172     ui::Dataspace getBestDataspace(ui::Dataspace*, bool*) const;
173     compositionengine::Output::ColorProfile pickColorProfile(
174             const compositionengine::CompositionRefreshArgs&) const;
175     void updateHwcAsyncWorker();
176     float getHdrSdrRatio(const std::shared_ptr<renderengine::ExternalTexture>& buffer) const;
177 
178     std::string mName;
179     std::string mNamePlusId;
180 
181     std::unique_ptr<compositionengine::DisplayColorProfile> mDisplayColorProfile;
182     std::unique_ptr<compositionengine::RenderSurface> mRenderSurface;
183 
184     ReleasedLayers mReleasedLayers;
185     OutputLayer* mLayerRequestingBackgroundBlur = nullptr;
186     std::unique_ptr<ClientCompositionRequestCache> mClientCompositionRequestCache;
187     std::unique_ptr<planner::Planner> mPlanner;
188     std::unique_ptr<HwcAsyncWorker> mHwComposerAsyncWorker;
189 
190     bool mPredictCompositionStrategy = false;
191     bool mOffloadPresent = false;
192 
193     // Whether the content must be recomposed this frame.
194     bool mMustRecompose = false;
195 };
196 
197 // This template factory function standardizes the implementation details of the
198 // final class using the types actually required by the implementation. This is
199 // not possible to do in the base class as those types may not even be visible
200 // to the base code.
201 template <typename BaseOutput, typename CompositionEngine, typename... Args>
createOutputTemplated(const CompositionEngine & compositionEngine,Args...args)202 std::shared_ptr<BaseOutput> createOutputTemplated(const CompositionEngine& compositionEngine,
203                                                   Args... args) {
204     class Output final : public BaseOutput {
205     public:
206 // Clang incorrectly complains that these are unused.
207 #pragma clang diagnostic push
208 #pragma clang diagnostic ignored "-Wunused-local-typedef"
209 
210         using OutputCompositionState = std::remove_const_t<
211                 std::remove_reference_t<decltype(std::declval<BaseOutput>().getState())>>;
212         using OutputLayer = std::remove_pointer_t<decltype(
213                 std::declval<BaseOutput>().getOutputLayerOrderedByZByIndex(0))>;
214 
215 #pragma clang diagnostic pop
216 
217         explicit Output(const CompositionEngine& compositionEngine, Args... args)
218               : BaseOutput(std::forward<Args>(args)...), mCompositionEngine(compositionEngine) {}
219         ~Output() override = default;
220 
221     private:
222         // compositionengine::Output overrides
223         const OutputCompositionState& getState() const override { return mState; }
224 
225         OutputCompositionState& editState() override { return mState; }
226 
227         size_t getOutputLayerCount() const override {
228             return mCurrentOutputLayersOrderedByZ.size();
229         }
230 
231         OutputLayer* getOutputLayerOrderedByZByIndex(size_t index) const override {
232             if (index >= mCurrentOutputLayersOrderedByZ.size()) {
233                 return nullptr;
234             }
235             return mCurrentOutputLayersOrderedByZ[index].get();
236         }
237 
238         // compositionengine::impl::Output overrides
239         const CompositionEngine& getCompositionEngine() const override {
240             return mCompositionEngine;
241         };
242 
243         OutputLayer* ensureOutputLayer(std::optional<size_t> prevIndex,
244                                        const sp<LayerFE>& layerFE) {
245             auto outputLayer = (prevIndex && *prevIndex <= mCurrentOutputLayersOrderedByZ.size())
246                     ? std::move(mCurrentOutputLayersOrderedByZ[*prevIndex])
247                     : BaseOutput::createOutputLayer(layerFE);
248             auto result = outputLayer.get();
249             mPendingOutputLayersOrderedByZ.emplace_back(std::move(outputLayer));
250             return result;
251         }
252 
253         void finalizePendingOutputLayers() override {
254             // The pending layers are added in reverse order. Reverse them to
255             // get the back-to-front ordered list of layers.
256             std::reverse(mPendingOutputLayersOrderedByZ.begin(),
257                          mPendingOutputLayersOrderedByZ.end());
258 
259             mCurrentOutputLayersOrderedByZ = std::move(mPendingOutputLayersOrderedByZ);
260         }
261 
262         void dumpState(std::string& out) const override { mState.dump(out); }
263 
264         OutputLayer* injectOutputLayerForTest(const sp<LayerFE>& layerFE) override {
265             auto outputLayer = BaseOutput::createOutputLayer(layerFE);
266             auto result = outputLayer.get();
267             mCurrentOutputLayersOrderedByZ.emplace_back(std::move(outputLayer));
268             return result;
269         }
270 
271         // Note: This is declared as a private virtual non-override so it can be
272         // an override implementation in the unit tests, but otherwise is not an
273         // accessible override for the normal implementation.
274         virtual void injectOutputLayerForTest(std::unique_ptr<OutputLayer> outputLayer) {
275             mCurrentOutputLayersOrderedByZ.emplace_back(std::move(outputLayer));
276         }
277 
278         void clearOutputLayers() override {
279             mCurrentOutputLayersOrderedByZ.clear();
280             mPendingOutputLayersOrderedByZ.clear();
281         }
282 
283         const CompositionEngine& mCompositionEngine;
284         OutputCompositionState mState;
285         std::vector<std::unique_ptr<OutputLayer>> mCurrentOutputLayersOrderedByZ;
286         std::vector<std::unique_ptr<OutputLayer>> mPendingOutputLayersOrderedByZ;
287     };
288 
289     return std::make_shared<Output>(compositionEngine, std::forward<Args>(args)...);
290 }
291 
292 std::shared_ptr<Output> createOutput(const compositionengine::CompositionEngine&);
293 
294 } // namespace android::compositionengine::impl
295