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 void present(const CompositionRefreshArgs&) override;
84
85 void rebuildLayerStacks(const CompositionRefreshArgs&, LayerFESet&) override;
86 void collectVisibleLayers(const CompositionRefreshArgs&,
87 compositionengine::Output::CoverageState&) override;
88 void ensureOutputLayerIfVisible(sp<compositionengine::LayerFE>&,
89 compositionengine::Output::CoverageState&) override;
90 void setReleasedLayers(const compositionengine::CompositionRefreshArgs&) override;
91
92 void updateLayerStateFromFE(const CompositionRefreshArgs&) const override;
93 void updateCompositionState(const compositionengine::CompositionRefreshArgs&) override;
94 void planComposition() override;
95 void writeCompositionState(const compositionengine::CompositionRefreshArgs&) override;
96 void updateColorProfile(const compositionengine::CompositionRefreshArgs&) override;
97 void beginFrame() override;
98 void prepareFrame() override;
99 GpuCompositionResult prepareFrameAsync(const CompositionRefreshArgs&) override;
100 void devOptRepaintFlash(const CompositionRefreshArgs&) override;
101 void finishFrame(const CompositionRefreshArgs&, GpuCompositionResult&&) override;
102 std::optional<base::unique_fd> composeSurfaces(const Region&,
103 const compositionengine::CompositionRefreshArgs&,
104 std::shared_ptr<renderengine::ExternalTexture>,
105 base::unique_fd&) override;
106 void postFramebuffer() override;
107 void renderCachedSets(const CompositionRefreshArgs&) override;
108 void cacheClientCompositionRequests(uint32_t) override;
109 bool canPredictCompositionStrategy(const CompositionRefreshArgs&) override;
110 void setPredictCompositionStrategy(bool) override;
111 void setTreat170mAsSrgb(bool) override;
112
113 // Testing
114 const ReleasedLayers& getReleasedLayersForTest() const;
115 void setDisplayColorProfileForTest(std::unique_ptr<compositionengine::DisplayColorProfile>);
116 void setRenderSurfaceForTest(std::unique_ptr<compositionengine::RenderSurface>);
plannerEnabled()117 bool plannerEnabled() const { return mPlanner != nullptr; }
118 virtual bool anyLayersRequireClientComposition() const;
119 virtual void updateProtectedContentState();
120 virtual bool dequeueRenderBuffer(base::unique_fd*,
121 std::shared_ptr<renderengine::ExternalTexture>*);
122 virtual std::future<bool> chooseCompositionStrategyAsync(
123 std::optional<android::HWComposer::DeviceRequestedChanges>*);
124 virtual void resetCompositionStrategy();
125
126 protected:
127 std::unique_ptr<compositionengine::OutputLayer> createOutputLayer(const sp<LayerFE>&) const;
128 std::optional<size_t> findCurrentOutputLayerForLayer(
129 const sp<compositionengine::LayerFE>&) const;
130 using DeviceRequestedChanges = android::HWComposer::DeviceRequestedChanges;
chooseCompositionStrategy(std::optional<android::HWComposer::DeviceRequestedChanges> *)131 bool chooseCompositionStrategy(
132 std::optional<android::HWComposer::DeviceRequestedChanges>*) override {
133 return true;
134 };
applyCompositionStrategy(const std::optional<DeviceRequestedChanges> &)135 void applyCompositionStrategy(const std::optional<DeviceRequestedChanges>&) override{};
136 bool getSkipColorTransform() const override;
137 compositionengine::Output::FrameFences presentAndGetFrameFences() override;
138 std::vector<LayerFE::LayerSettings> generateClientCompositionRequests(
139 bool supportsProtectedContent, ui::Dataspace outputDataspace,
140 std::vector<LayerFE*> &outLayerFEs) override;
141 void appendRegionFlashRequests(const Region&, std::vector<LayerFE::LayerSettings>&) override;
142 void setExpensiveRenderingExpected(bool enabled) override;
143 void setHintSessionGpuFence(std::unique_ptr<FenceTime>&& gpuFence) override;
144 bool isPowerHintSessionEnabled() override;
145 void dumpBase(std::string&) const;
146
147 // Implemented by the final implementation for the final state it uses.
148 virtual compositionengine::OutputLayer* ensureOutputLayer(std::optional<size_t>,
149 const sp<LayerFE>&) = 0;
150 virtual compositionengine::OutputLayer* injectOutputLayerForTest(const sp<LayerFE>&) = 0;
151 virtual void finalizePendingOutputLayers() = 0;
152 virtual const compositionengine::CompositionEngine& getCompositionEngine() const = 0;
153 virtual void dumpState(std::string& out) const = 0;
154
155 bool mustRecompose() const;
156
157 private:
158 void dirtyEntireOutput();
159 compositionengine::OutputLayer* findLayerRequestingBackgroundComposition() const;
160 void finishPrepareFrame();
161 ui::Dataspace getBestDataspace(ui::Dataspace*, bool*) const;
162 compositionengine::Output::ColorProfile pickColorProfile(
163 const compositionengine::CompositionRefreshArgs&) const;
164
165 std::string mName;
166
167 std::unique_ptr<compositionengine::DisplayColorProfile> mDisplayColorProfile;
168 std::unique_ptr<compositionengine::RenderSurface> mRenderSurface;
169
170 ReleasedLayers mReleasedLayers;
171 OutputLayer* mLayerRequestingBackgroundBlur = nullptr;
172 std::unique_ptr<ClientCompositionRequestCache> mClientCompositionRequestCache;
173 std::unique_ptr<planner::Planner> mPlanner;
174 std::unique_ptr<HwcAsyncWorker> mHwComposerAsyncWorker;
175
176 // Whether the content must be recomposed this frame.
177 bool mMustRecompose = false;
178 };
179
180 // This template factory function standardizes the implementation details of the
181 // final class using the types actually required by the implementation. This is
182 // not possible to do in the base class as those types may not even be visible
183 // to the base code.
184 template <typename BaseOutput, typename CompositionEngine, typename... Args>
createOutputTemplated(const CompositionEngine & compositionEngine,Args...args)185 std::shared_ptr<BaseOutput> createOutputTemplated(const CompositionEngine& compositionEngine,
186 Args... args) {
187 class Output final : public BaseOutput {
188 public:
189 // Clang incorrectly complains that these are unused.
190 #pragma clang diagnostic push
191 #pragma clang diagnostic ignored "-Wunused-local-typedef"
192
193 using OutputCompositionState = std::remove_const_t<
194 std::remove_reference_t<decltype(std::declval<BaseOutput>().getState())>>;
195 using OutputLayer = std::remove_pointer_t<decltype(
196 std::declval<BaseOutput>().getOutputLayerOrderedByZByIndex(0))>;
197
198 #pragma clang diagnostic pop
199
200 explicit Output(const CompositionEngine& compositionEngine, Args... args)
201 : BaseOutput(std::forward<Args>(args)...), mCompositionEngine(compositionEngine) {}
202 ~Output() override = default;
203
204 private:
205 // compositionengine::Output overrides
206 const OutputCompositionState& getState() const override { return mState; }
207
208 OutputCompositionState& editState() override { return mState; }
209
210 size_t getOutputLayerCount() const override {
211 return mCurrentOutputLayersOrderedByZ.size();
212 }
213
214 OutputLayer* getOutputLayerOrderedByZByIndex(size_t index) const override {
215 if (index >= mCurrentOutputLayersOrderedByZ.size()) {
216 return nullptr;
217 }
218 return mCurrentOutputLayersOrderedByZ[index].get();
219 }
220
221 // compositionengine::impl::Output overrides
222 const CompositionEngine& getCompositionEngine() const override {
223 return mCompositionEngine;
224 };
225
226 OutputLayer* ensureOutputLayer(std::optional<size_t> prevIndex,
227 const sp<LayerFE>& layerFE) {
228 auto outputLayer = (prevIndex && *prevIndex <= mCurrentOutputLayersOrderedByZ.size())
229 ? std::move(mCurrentOutputLayersOrderedByZ[*prevIndex])
230 : BaseOutput::createOutputLayer(layerFE);
231 auto result = outputLayer.get();
232 mPendingOutputLayersOrderedByZ.emplace_back(std::move(outputLayer));
233 return result;
234 }
235
236 void finalizePendingOutputLayers() override {
237 // The pending layers are added in reverse order. Reverse them to
238 // get the back-to-front ordered list of layers.
239 std::reverse(mPendingOutputLayersOrderedByZ.begin(),
240 mPendingOutputLayersOrderedByZ.end());
241
242 mCurrentOutputLayersOrderedByZ = std::move(mPendingOutputLayersOrderedByZ);
243 }
244
245 void dumpState(std::string& out) const override { mState.dump(out); }
246
247 OutputLayer* injectOutputLayerForTest(const sp<LayerFE>& layerFE) override {
248 auto outputLayer = BaseOutput::createOutputLayer(layerFE);
249 auto result = outputLayer.get();
250 mCurrentOutputLayersOrderedByZ.emplace_back(std::move(outputLayer));
251 return result;
252 }
253
254 // Note: This is declared as a private virtual non-override so it can be
255 // an override implementation in the unit tests, but otherwise is not an
256 // accessible override for the normal implementation.
257 virtual void injectOutputLayerForTest(std::unique_ptr<OutputLayer> outputLayer) {
258 mCurrentOutputLayersOrderedByZ.emplace_back(std::move(outputLayer));
259 }
260
261 void clearOutputLayers() override {
262 mCurrentOutputLayersOrderedByZ.clear();
263 mPendingOutputLayersOrderedByZ.clear();
264 }
265
266 const CompositionEngine& mCompositionEngine;
267 OutputCompositionState mState;
268 std::vector<std::unique_ptr<OutputLayer>> mCurrentOutputLayersOrderedByZ;
269 std::vector<std::unique_ptr<OutputLayer>> mPendingOutputLayersOrderedByZ;
270 };
271
272 return std::make_shared<Output>(compositionEngine, std::forward<Args>(args)...);
273 }
274
275 std::shared_ptr<Output> createOutput(const compositionengine::CompositionEngine&);
276
277 } // namespace android::compositionengine::impl
278