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