• 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/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     virtual ftl::Optional<DisplayIdVariant> getDisplayIdVariant() const = 0;
174 
175     // Enables (or disables) composition on this output
176     virtual void setCompositionEnabled(bool) = 0;
177 
178     // Enables (or disables) layer caching on this output
179     virtual void setLayerCachingEnabled(bool) = 0;
180 
181     // Enables (or disables) layer caching texture pool on this output
182     virtual void setLayerCachingTexturePoolEnabled(bool) = 0;
183 
184     // Sets the projection state to use
185     virtual void setProjection(ui::Rotation orientation, const Rect& layerStackSpaceRect,
186                                const Rect& orientedDisplaySpaceRect) = 0;
187     // Sets the brightness that will take effect next frame.
188     virtual void setNextBrightness(float brightness) = 0;
189     // Sets the bounds to use
190     virtual void setDisplaySize(const ui::Size&) = 0;
191     // Gets the transform hint used in layers that belong to this output. Used to guide
192     // composition orientation so that HW overlay can be used when display isn't in its natural
193     // orientation on some devices. Therefore usually we only use transform hint from display
194     // output.
195     virtual ui::Transform::RotationFlags getTransformHint() const = 0;
196 
197     // Sets the filter for this output. See Output::includesLayer.
198     virtual void setLayerFilter(ui::LayerFilter) = 0;
199 
200     // Sets the output color mode
201     virtual void setColorProfile(const ColorProfile&) = 0;
202 
203     // Sets current calibrated display brightness information
204     virtual void setDisplayBrightness(float sdrWhitePointNits, float displayBrightnessNits) = 0;
205 
206     // Outputs a string with a state dump
207     virtual void dump(std::string&) const = 0;
208 
209     // Outputs planner information
210     virtual void dumpPlannerInfo(const Vector<String16>& args, std::string&) const = 0;
211 
212     // Gets the debug name for the output
213     virtual const std::string& getName() const = 0;
214 
215     // Sets a debug name for the output
216     virtual void setName(const std::string&) = 0;
217 
218     // Gets the current render color mode for the output
219     virtual DisplayColorProfile* getDisplayColorProfile() const = 0;
220 
221     // Gets the current render surface for the output
222     virtual RenderSurface* getRenderSurface() const = 0;
223 
224     using OutputCompositionState = compositionengine::impl::OutputCompositionState;
225 
226     // Gets the raw composition state data for the output
227     // TODO(lpique): Make this protected once it is only internally called.
228     virtual const OutputCompositionState& getState() const = 0;
229 
230     // Allows mutable access to the raw composition state data for the output.
231     // This is meant to be used by the various functions that are part of the
232     // composition process.
233     // TODO(lpique): Make this protected once it is only internally called.
234     virtual OutputCompositionState& editState() = 0;
235 
236     // Gets the dirty region in layer stack space.
237     virtual Region getDirtyRegion() const = 0;
238 
239     // Returns whether the output includes a layer, based on their respective filters.
240     // See Output::setLayerFilter.
241     virtual bool includesLayer(ui::LayerFilter) const = 0;
242     virtual bool includesLayer(const sp<LayerFE>&) const = 0;
243 
244     // Returns a pointer to the output layer corresponding to the given layer on
245     // this output, or nullptr if the layer does not have one
246     virtual OutputLayer* getOutputLayerForLayer(const sp<LayerFE>&) const = 0;
247 
248     // Immediately clears all layers from the output.
249     virtual void clearOutputLayers() = 0;
250 
251     // For tests use only. Creates and appends an OutputLayer into the output.
252     virtual OutputLayer* injectOutputLayerForTest(const sp<LayerFE>&) = 0;
253 
254     // Gets the count of output layers managed by this output
255     virtual size_t getOutputLayerCount() const = 0;
256 
257     // Gets an output layer in Z order given its index
258     virtual OutputLayer* getOutputLayerOrderedByZByIndex(size_t) const = 0;
259 
260     // A helper function for enumerating all the output layers in Z order using
261     // a C++11 range-based for loop.
getOutputLayersOrderedByZ()262     auto getOutputLayersOrderedByZ() const { return OutputLayersEnumerator(*this); }
263 
264     // Sets the new set of layers being released this frame
265     virtual void setReleasedLayers(ReleasedLayers&&) = 0;
266 
267     // Prepare the output, updating the OutputLayers used in the output
268     virtual void prepare(const CompositionRefreshArgs&, LayerFESet&) = 0;
269 
270     // Presents the output, finalizing all composition details. This may happen
271     // asynchronously, in which case the returned future must be waited upon.
272     virtual ftl::Future<std::monostate> present(const CompositionRefreshArgs&) = 0;
273 
274     // Whether this output can be presented from another thread.
275     virtual bool supportsOffloadPresent() const = 0;
276 
277     // Make the next call to `present` run asynchronously.
278     virtual void offloadPresentNextFrame() = 0;
279 
280     // Enables predicting composition strategy to run client composition earlier
281     virtual void setPredictCompositionStrategy(bool) = 0;
282 
283     // Enables overriding the 170M trasnfer function as sRGB
284     virtual void setTreat170mAsSrgb(bool) = 0;
285 
286 protected:
287     virtual void setDisplayColorProfile(std::unique_ptr<DisplayColorProfile>) = 0;
288     virtual void setRenderSurface(std::unique_ptr<RenderSurface>) = 0;
289 
290     virtual void uncacheBuffers(const std::vector<uint64_t>&) = 0;
291     virtual void rebuildLayerStacks(const CompositionRefreshArgs&, LayerFESet&) = 0;
292     virtual void collectVisibleLayers(const CompositionRefreshArgs&, CoverageState&) = 0;
293     virtual void ensureOutputLayerIfVisible(sp<LayerFE>&, CoverageState&) = 0;
294     virtual void setReleasedLayers(const CompositionRefreshArgs&) = 0;
295 
296     virtual void updateCompositionState(const CompositionRefreshArgs&) = 0;
297     virtual void planComposition() = 0;
298     virtual void writeCompositionState(const CompositionRefreshArgs&) = 0;
299     virtual void setColorTransform(const CompositionRefreshArgs&) = 0;
300     virtual void updateColorProfile(const CompositionRefreshArgs&) = 0;
301     virtual void beginFrame() = 0;
302     virtual void prepareFrame() = 0;
303 
304     using GpuCompositionResult = compositionengine::impl::GpuCompositionResult;
305     // Runs prepare frame in another thread while running client composition using
306     // the previous frame's composition strategy.
307     virtual GpuCompositionResult prepareFrameAsync() = 0;
308     virtual void devOptRepaintFlash(const CompositionRefreshArgs&) = 0;
309     virtual void finishFrame(GpuCompositionResult&&) = 0;
310     virtual std::optional<base::unique_fd> composeSurfaces(
311             const Region&, std::shared_ptr<renderengine::ExternalTexture>, base::unique_fd&) = 0;
312     virtual void presentFrameAndReleaseLayers(bool flushEvenWhenDisabled) = 0;
313     virtual void renderCachedSets(const CompositionRefreshArgs&) = 0;
314     virtual bool chooseCompositionStrategy(
315             std::optional<android::HWComposer::DeviceRequestedChanges>*) = 0;
316     virtual void applyCompositionStrategy(
317             const std::optional<android::HWComposer::DeviceRequestedChanges>& changes) = 0;
318     virtual bool getSkipColorTransform() const = 0;
319     virtual FrameFences presentFrame() = 0;
320     virtual void executeCommands() = 0;
321     virtual std::vector<LayerFE::LayerSettings> generateClientCompositionRequests(
322             bool supportsProtectedContent, ui::Dataspace outputDataspace,
323             std::vector<LayerFE*> &outLayerRef) = 0;
324     virtual void appendRegionFlashRequests(
325             const Region& flashRegion,
326             std::vector<LayerFE::LayerSettings>& clientCompositionLayers) = 0;
327     virtual void setExpensiveRenderingExpected(bool enabled) = 0;
328     virtual void setHintSessionGpuStart(TimePoint startTime) = 0;
329     virtual void setHintSessionGpuFence(std::unique_ptr<FenceTime>&& gpuFence) = 0;
330     virtual void setHintSessionRequiresRenderEngine(bool requiresRenderEngine) = 0;
331     virtual bool isPowerHintSessionEnabled() = 0;
332     virtual bool isPowerHintSessionGpuReportingEnabled() = 0;
333     virtual void cacheClientCompositionRequests(uint32_t cacheSize) = 0;
334     virtual bool canPredictCompositionStrategy(const CompositionRefreshArgs&) = 0;
335     virtual const aidl::android::hardware::graphics::composer3::OverlayProperties*
336     getOverlaySupport() = 0;
337     virtual bool hasPictureProcessing() const = 0;
338     virtual int32_t getMaxLayerPictureProfiles() const = 0;
339     virtual void applyPictureProfile() = 0;
340 };
341 
342 } // namespace compositionengine
343 } // namespace android
344