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