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/future.h>
20 #include <ftl/optional.h>
21 #include <cstdint>
22 #include <iterator>
23 #include <optional>
24 #include <string>
25 #include <type_traits>
26 #include <unordered_map>
27 #include <utility>
28 #include <vector>
29 
30 #include <compositionengine/LayerFE.h>
31 #include <renderengine/LayerSettings.h>
32 #include <ui/DisplayIdentification.h>
33 #include <ui/Fence.h>
34 #include <ui/FenceTime.h>
35 #include <ui/GraphicTypes.h>
36 #include <ui/LayerStack.h>
37 #include <ui/PictureProfileHandle.h>
38 #include <ui/Region.h>
39 #include <ui/Transform.h>
40 #include <utils/StrongPointer.h>
41 #include <utils/Vector.h>
42 
43 #include "DisplayHardware/HWComposer.h"
44 
45 namespace android {
46 
47 namespace HWC2 {
48 class Layer;
49 } // namespace HWC2
50 
51 namespace compositionengine {
52 
53 class DisplayColorProfile;
54 class LayerFE;
55 class RenderSurface;
56 class OutputLayer;
57 
58 struct CompositionRefreshArgs;
59 struct LayerFECompositionState;
60 
61 namespace impl {
62 struct OutputCompositionState;
63 struct GpuCompositionResult;
64 } // namespace impl
65 
66 /**
67  * Encapsulates all the states involved with composing layers for an output
68  */
69 class Output {
70 public:
71     using ReleasedLayers = std::vector<wp<LayerFE>>;
72     using UniqueFELayerStateMap = std::unordered_map<LayerFE*, LayerFECompositionState*>;
73 
74     // A helper class for enumerating the output layers using a C++11 ranged-based for loop
75     template <typename T>
76     class OutputLayersEnumerator {
77     public:
78         // TODO(lpique): Consider turning this into a C++20 view when possible.
79         template <bool IsConstIter>
80         class IteratorImpl {
81         public:
82             // Required definitions to be considered an iterator
83             using iterator_category = std::forward_iterator_tag;
84             using value_type = decltype(std::declval<T>().getOutputLayerOrderedByZByIndex(0));
85             using difference_type = std::ptrdiff_t;
86             using pointer = std::conditional_t<IsConstIter, const value_type*, value_type*>;
87             using reference = std::conditional_t<IsConstIter, const value_type&, value_type&>;
88 
89             IteratorImpl() = default;
IteratorImpl(const T * output,size_t index)90             IteratorImpl(const T* output, size_t index) : mOutput(output), mIndex(index) {}
91 
92             value_type operator*() const {
93                 return mOutput->getOutputLayerOrderedByZByIndex(mIndex);
94             }
95             value_type operator->() const {
96                 return mOutput->getOutputLayerOrderedByZByIndex(mIndex);
97             }
98 
99             bool operator==(const IteratorImpl& other) const {
100                 return mOutput == other.mOutput && mIndex == other.mIndex;
101             }
102             bool operator!=(const IteratorImpl& other) const { return !operator==(other); }
103 
104             IteratorImpl& operator++() {
105                 ++mIndex;
106                 return *this;
107             }
108             IteratorImpl operator++(int) {
109                 auto prev = *this;
110                 ++mIndex;
111                 return prev;
112             }
113 
114         private:
115             const T* mOutput{nullptr};
116             size_t mIndex{0};
117         };
118 
119         using iterator = IteratorImpl<false>;
120         using const_iterator = IteratorImpl<true>;
121 
OutputLayersEnumerator(const T & output)122         explicit OutputLayersEnumerator(const T& output) : mOutput(output) {}
begin()123         auto begin() const { return iterator(&mOutput, 0); }
end()124         auto end() const { return iterator(&mOutput, mOutput.getOutputLayerCount()); }
cbegin()125         auto cbegin() const { return const_iterator(&mOutput, 0); }
cend()126         auto cend() const { return const_iterator(&mOutput, mOutput.getOutputLayerCount()); }
127 
128     private:
129         const T& mOutput;
130     };
131 
132     struct FrameFences {
133         sp<Fence> presentFence{Fence::NO_FENCE};
134         sp<Fence> clientTargetAcquireFence{Fence::NO_FENCE};
135         std::unordered_map<HWC2::Layer*, sp<Fence>> layerFences;
136     };
137 
138     struct ColorProfile {
139         ui::ColorMode mode{ui::ColorMode::NATIVE};
140         ui::Dataspace dataspace{ui::Dataspace::UNKNOWN};
141         ui::RenderIntent renderIntent{ui::RenderIntent::COLORIMETRIC};
142     };
143 
144     // Use internally to incrementally compute visibility/coverage
145     struct CoverageState {
CoverageStateCoverageState146         explicit CoverageState(LayerFESet& latchedLayers) : latchedLayers(latchedLayers) {}
147 
148         // The set of layers that had been latched for the coverage calls, to
149         // avoid duplicate requests to obtain the same front-end layer state.
150         LayerFESet& latchedLayers;
151 
152         // The region of the output which is covered by layers
153         Region aboveCoveredLayers;
154         // The region of the output which is opaquely covered by layers
155         Region aboveOpaqueLayers;
156         // The region of the output which should be considered dirty
157         Region dirtyRegion;
158         // The region of the output which is covered by layers, excluding display overlays. This
159         // only has a value if there's something needing it, like when a TrustedPresentationListener
160         // is set
161         std::optional<Region> aboveCoveredLayersExcludingOverlays;
162     };
163 
164     virtual ~Output();
165 
166     // Returns true if the output is valid. This is meant to be checked post-
167     // construction and prior to use, as not everything is set up by the
168     // constructor.
169     virtual bool isValid() const = 0;
170 
171     // Returns the DisplayId the output represents, if it has one
172     virtual ftl::Optional<DisplayId> getDisplayId() const = 0;
173 
174     // Enables (or disables) composition on this output
175     virtual void setCompositionEnabled(bool) = 0;
176 
177     // Enables (or disables) layer caching on this output
178     virtual void setLayerCachingEnabled(bool) = 0;
179 
180     // Enables (or disables) layer caching texture pool on this output
181     virtual void setLayerCachingTexturePoolEnabled(bool) = 0;
182 
183     // Sets the projection state to use
184     virtual void setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
185                                const Rect& orientedDisplaySpaceRect) = 0;
186     // Sets the brightness that will take effect next frame.
187     virtual void setNextBrightness(float brightness) = 0;
188     // Sets the bounds to use
189     virtual void setDisplaySize(const ui::Size&) = 0;
190     // Gets the transform hint used in layers that belong to this output. Used to guide
191     // composition orientation so that HW overlay can be used when display isn't in its natural
192     // orientation on some devices. Therefore usually we only use transform hint from display
193     // output.
194     virtual ui::Transform::RotationFlags getTransformHint() const = 0;
195 
196     // Sets the filter for this output. See Output::includesLayer.
197     virtual void setLayerFilter(ui::LayerFilter) = 0;
198 
199     // Sets the output color mode
200     virtual void setColorProfile(const ColorProfile&) = 0;
201 
202     // Sets current calibrated display brightness information
203     virtual void setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) = 0;
204 
205     // Outputs a string with a state dump
206     virtual void dump(std::string&) const = 0;
207 
208     // Outputs planner information
209     virtual void dumpPlannerInfo(const Vector<String16>& args, std::string&) const = 0;
210 
211     // Gets the debug name for the output
212     virtual const std::string& getName() const = 0;
213 
214     // Sets a debug name for the output
215     virtual void setName(const std::string&) = 0;
216 
217     // Gets the current render color mode for the output
218     virtual DisplayColorProfile* getDisplayColorProfile() const = 0;
219 
220     // Gets the current render surface for the output
221     virtual RenderSurface* getRenderSurface() const = 0;
222 
223     using OutputCompositionState = compositionengine::impl::OutputCompositionState;
224 
225     // Gets the raw composition state data for the output
226     // TODO(lpique): Make this protected once it is only internally called.
227     virtual const OutputCompositionState& getState() const = 0;
228 
229     // Allows mutable access to the raw composition state data for the output.
230     // This is meant to be used by the various functions that are part of the
231     // composition process.
232     // TODO(lpique): Make this protected once it is only internally called.
233     virtual OutputCompositionState& editState() = 0;
234 
235     // Gets the dirty region in layer stack space.
236     virtual Region getDirtyRegion() const = 0;
237 
238     // Returns whether the output includes a layer, based on their respective filters.
239     // See Output::setLayerFilter.
240     virtual bool includesLayer(ui::LayerFilter) const = 0;
241     virtual bool includesLayer(const sp<LayerFE>&) const = 0;
242 
243     // Returns a pointer to the output layer corresponding to the given layer on
244     // this output, or nullptr if the layer does not have one
245     virtual OutputLayer* getOutputLayerForLayer(const sp<LayerFE>&) const = 0;
246 
247     // Immediately clears all layers from the output.
248     virtual void clearOutputLayers() = 0;
249 
250     // For tests use only. Creates and appends an OutputLayer into the output.
251     virtual OutputLayer* injectOutputLayerForTest(const sp<LayerFE>&) = 0;
252 
253     // Gets the count of output layers managed by this output
254     virtual size_t getOutputLayerCount() const = 0;
255 
256     // Gets an output layer in Z order given its index
257     virtual OutputLayer* getOutputLayerOrderedByZByIndex(size_t) const = 0;
258 
259     // A helper function for enumerating all the output layers in Z order using
260     // a C++11 range-based for loop.
getOutputLayersOrderedByZ()261     auto getOutputLayersOrderedByZ() const { return OutputLayersEnumerator(*this); }
262 
263     // Sets the new set of layers being released this frame
264     virtual void setReleasedLayers(ReleasedLayers&&) = 0;
265 
266     // Prepare the output, updating the OutputLayers used in the output
267     virtual void prepare(const CompositionRefreshArgs&, LayerFESet&) = 0;
268 
269     // Presents the output, finalizing all composition details. This may happen
270     // asynchronously, in which case the returned future must be waited upon.
271     virtual ftl::Future<std::monostate> present(const CompositionRefreshArgs&) = 0;
272 
273     // Whether this output can be presented from another thread.
274     virtual bool supportsOffloadPresent() const = 0;
275 
276     // Make the next call to `present` run asynchronously.
277     virtual void offloadPresentNextFrame() = 0;
278 
279     // Enables predicting composition strategy to run client composition earlier
280     virtual void setPredictCompositionStrategy(bool) = 0;
281 
282     // Enables overriding the 170M trasnfer function as sRGB
283     virtual void setTreat170mAsSrgb(bool) = 0;
284 
285 protected:
286     virtual void setDisplayColorProfile(std::unique_ptr<DisplayColorProfile>) = 0;
287     virtual void setRenderSurface(std::unique_ptr<RenderSurface>) = 0;
288 
289     virtual void uncacheBuffers(const std::vector<uint64_t>&) = 0;
290     virtual void rebuildLayerStacks(const CompositionRefreshArgs&, LayerFESet&) = 0;
291     virtual void collectVisibleLayers(const CompositionRefreshArgs&, CoverageState&) = 0;
292     virtual void ensureOutputLayerIfVisible(sp<LayerFE>&, CoverageState&) = 0;
293     virtual void setReleasedLayers(const CompositionRefreshArgs&) = 0;
294 
295     virtual void updateCompositionState(const CompositionRefreshArgs&) = 0;
296     virtual void planComposition() = 0;
297     virtual void writeCompositionState(const CompositionRefreshArgs&) = 0;
298     virtual void setColorTransform(const CompositionRefreshArgs&) = 0;
299     virtual void updateColorProfile(const CompositionRefreshArgs&) = 0;
300     virtual void beginFrame() = 0;
301     virtual void prepareFrame() = 0;
302 
303     using GpuCompositionResult = compositionengine::impl::GpuCompositionResult;
304     // Runs prepare frame in another thread while running client composition using
305     // the previous frame's composition strategy.
306     virtual GpuCompositionResult prepareFrameAsync() = 0;
307     virtual void devOptRepaintFlash(const CompositionRefreshArgs&) = 0;
308     virtual void finishFrame(GpuCompositionResult&&) = 0;
309     virtual std::optional<base::unique_fd> composeSurfaces(
310             const Region&, std::shared_ptr<renderengine::ExternalTexture>, base::unique_fd&) = 0;
311     virtual void presentFrameAndReleaseLayers(bool flushEvenWhenDisabled) = 0;
312     virtual void renderCachedSets(const CompositionRefreshArgs&) = 0;
313     virtual bool chooseCompositionStrategy(
314             std::optional<android::HWComposer::DeviceRequestedChanges>*) = 0;
315     virtual void applyCompositionStrategy(
316             const std::optional<android::HWComposer::DeviceRequestedChanges>& changes) = 0;
317     virtual bool getSkipColorTransform() const = 0;
318     virtual FrameFences presentFrame() = 0;
319     virtual void executeCommands() = 0;
320     virtual std::vector<LayerFE::LayerSettings> generateClientCompositionRequests(
321             bool supportsProtectedContent, ui::Dataspace outputDataspace,
322             std::vector<LayerFE*> &outLayerRef) = 0;
323     virtual void appendRegionFlashRequests(
324             const Region& flashRegion,
325             std::vector<LayerFE::LayerSettings>& clientCompositionLayers) = 0;
326     virtual void setExpensiveRenderingExpected(bool enabled) = 0;
327     virtual void setHintSessionGpuStart(TimePoint startTime) = 0;
328     virtual void setHintSessionGpuFence(std::unique_ptr<FenceTime>&& gpuFence) = 0;
329     virtual void setHintSessionRequiresRenderEngine(bool requiresRenderEngine) = 0;
330     virtual bool isPowerHintSessionEnabled() = 0;
331     virtual bool isPowerHintSessionGpuReportingEnabled() = 0;
332     virtual void cacheClientCompositionRequests(uint32_t cacheSize) = 0;
333     virtual bool canPredictCompositionStrategy(const CompositionRefreshArgs&) = 0;
334     virtual const aidl::android::hardware::graphics::composer3::OverlayProperties*
335     getOverlaySupport() = 0;
336     virtual bool hasPictureProcessing() const = 0;
337     virtual int32_t getMaxLayerPictureProfiles() const = 0;
338     virtual void applyPictureProfile() = 0;
339 };
340 
341 } // namespace compositionengine
342 } // namespace android
343