blob: baf52588e64f631d48e72d2d42e6fc1384735b95 [file] [log] [blame]
Lloyd Pique32cbe282018-10-19 13:09:22 -07001/*
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>
Lloyd Pique01c77c12019-04-17 12:48:32 -070020#include <iterator>
Lloyd Pique07e33212018-12-18 16:33:37 -080021#include <optional>
Lloyd Pique32cbe282018-10-19 13:09:22 -070022#include <string>
Lloyd Pique01c77c12019-04-17 12:48:32 -070023#include <type_traits>
Lloyd Pique35fca9d2019-02-13 14:24:11 -080024#include <unordered_map>
Lloyd Pique01c77c12019-04-17 12:48:32 -070025#include <utility>
Lloyd Pique32cbe282018-10-19 13:09:22 -070026
Lloyd Piquec29e4c62019-03-07 21:48:19 -080027#include <compositionengine/LayerFE.h>
Lloyd Pique688abd42019-02-15 15:42:24 -080028#include <renderengine/LayerSettings.h>
Lloyd Pique35fca9d2019-02-13 14:24:11 -080029#include <ui/Fence.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070030#include <ui/GraphicTypes.h>
31#include <ui/Region.h>
32#include <ui/Transform.h>
Lloyd Piquecc01a452018-12-04 17:24:00 -080033#include <utils/StrongPointer.h>
Lloyd Pique32cbe282018-10-19 13:09:22 -070034
Lloyd Pique07e33212018-12-18 16:33:37 -080035#include "DisplayHardware/DisplayIdentification.h"
36
Peiyong Line9d809e2020-04-14 13:10:48 -070037namespace android {
38
Lloyd Pique35fca9d2019-02-13 14:24:11 -080039namespace HWC2 {
40class Layer;
41} // namespace HWC2
42
Peiyong Line9d809e2020-04-14 13:10:48 -070043namespace compositionengine {
Lloyd Pique32cbe282018-10-19 13:09:22 -070044
Lloyd Pique3d0c02e2018-10-19 18:38:12 -070045class DisplayColorProfile;
Lloyd Piquecc01a452018-12-04 17:24:00 -080046class LayerFE;
Lloyd Pique31cb2942018-10-19 17:23:03 -070047class RenderSurface;
Lloyd Piquecc01a452018-12-04 17:24:00 -080048class OutputLayer;
Lloyd Pique31cb2942018-10-19 17:23:03 -070049
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080050struct CompositionRefreshArgs;
Lloyd Pique3eb1b212019-03-07 21:15:40 -080051struct LayerFECompositionState;
Lloyd Piquef8cf14d2019-02-28 16:03:12 -080052
Lloyd Pique32cbe282018-10-19 13:09:22 -070053namespace impl {
54struct OutputCompositionState;
55} // namespace impl
56
57/**
58 * Encapsulates all the state involved with composing layers for an output
59 */
60class Output {
61public:
Lloyd Piquec7ef21b2019-01-29 18:43:00 -080062 using ReleasedLayers = std::vector<wp<LayerFE>>;
Lloyd Pique3eb1b212019-03-07 21:15:40 -080063 using UniqueFELayerStateMap = std::unordered_map<LayerFE*, LayerFECompositionState*>;
Lloyd Piquecc01a452018-12-04 17:24:00 -080064
Lloyd Pique01c77c12019-04-17 12:48:32 -070065 // A helper class for enumerating the output layers using a C++11 ranged-based for loop
66 template <typename T>
67 class OutputLayersEnumerator {
68 public:
69 // TODO(lpique): Consider turning this into a C++20 view when possible.
70 template <bool IsConstIter>
71 class IteratorImpl {
72 public:
73 // Required definitions to be considered an iterator
74 using iterator_category = std::forward_iterator_tag;
75 using value_type = decltype(std::declval<T>().getOutputLayerOrderedByZByIndex(0));
76 using difference_type = std::ptrdiff_t;
77 using pointer = std::conditional_t<IsConstIter, const value_type*, value_type*>;
78 using reference = std::conditional_t<IsConstIter, const value_type&, value_type&>;
79
80 IteratorImpl() = default;
81 IteratorImpl(const T* output, size_t index) : mOutput(output), mIndex(index) {}
82
83 value_type operator*() const {
84 return mOutput->getOutputLayerOrderedByZByIndex(mIndex);
85 }
86 value_type operator->() const {
87 return mOutput->getOutputLayerOrderedByZByIndex(mIndex);
88 }
89
90 bool operator==(const IteratorImpl& other) const {
91 return mOutput == other.mOutput && mIndex == other.mIndex;
92 }
93 bool operator!=(const IteratorImpl& other) const { return !operator==(other); }
94
95 IteratorImpl& operator++() {
96 ++mIndex;
97 return *this;
98 }
99 IteratorImpl operator++(int) {
100 auto prev = *this;
101 ++mIndex;
102 return prev;
103 }
104
105 private:
106 const T* mOutput{nullptr};
107 size_t mIndex{0};
108 };
109
110 using iterator = IteratorImpl<false>;
111 using const_iterator = IteratorImpl<true>;
112
113 explicit OutputLayersEnumerator(const T& output) : mOutput(output) {}
114 auto begin() const { return iterator(&mOutput, 0); }
115 auto end() const { return iterator(&mOutput, mOutput.getOutputLayerCount()); }
116 auto cbegin() const { return const_iterator(&mOutput, 0); }
117 auto cend() const { return const_iterator(&mOutput, mOutput.getOutputLayerCount()); }
118
119 private:
120 const T& mOutput;
121 };
122
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800123 struct FrameFences {
124 sp<Fence> presentFence{Fence::NO_FENCE};
125 sp<Fence> clientTargetAcquireFence{Fence::NO_FENCE};
126 std::unordered_map<HWC2::Layer*, sp<Fence>> layerFences;
127 };
128
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800129 struct ColorProfile {
130 ui::ColorMode mode{ui::ColorMode::NATIVE};
131 ui::Dataspace dataspace{ui::Dataspace::UNKNOWN};
132 ui::RenderIntent renderIntent{ui::RenderIntent::COLORIMETRIC};
133 ui::Dataspace colorSpaceAgnosticDataspace{ui::Dataspace::UNKNOWN};
134 };
135
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800136 // Use internally to incrementally compute visibility/coverage
137 struct CoverageState {
138 explicit CoverageState(LayerFESet& latchedLayers) : latchedLayers(latchedLayers) {}
139
140 // The set of layers that had been latched for the coverage calls, to
141 // avoid duplicate requests to obtain the same front-end layer state.
142 LayerFESet& latchedLayers;
143
144 // The region of the output which is covered by layers
145 Region aboveCoveredLayers;
146 // The region of the output which is opaquely covered by layers
147 Region aboveOpaqueLayers;
148 // The region of the output which should be considered dirty
149 Region dirtyRegion;
150 };
151
Lloyd Piquefeb73d72018-12-04 17:23:44 -0800152 virtual ~Output();
153
Lloyd Pique32cbe282018-10-19 13:09:22 -0700154 // Returns true if the output is valid. This is meant to be checked post-
155 // construction and prior to use, as not everything is set up by the
156 // constructor.
157 virtual bool isValid() const = 0;
158
Lloyd Pique6c564cf2019-05-17 17:31:36 -0700159 // Returns the DisplayId the output represents, if it has one
160 virtual std::optional<DisplayId> getDisplayId() const = 0;
161
Lloyd Pique32cbe282018-10-19 13:09:22 -0700162 // Enables (or disables) composition on this output
163 virtual void setCompositionEnabled(bool) = 0;
164
165 // Sets the projection state to use
Lloyd Pique0a456232020-01-16 17:51:13 -0800166 virtual void setProjection(const ui::Transform&, uint32_t orientation, const Rect& frame,
Lloyd Piquee8fe4742020-01-21 15:26:18 -0800167 const Rect& viewport, const Rect& sourceClip,
168 const Rect& destinationClip, bool needsFiltering) = 0;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700169 // Sets the bounds to use
Lloyd Pique31cb2942018-10-19 17:23:03 -0700170 virtual void setBounds(const ui::Size&) = 0;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700171
Lloyd Piqueef36b002019-01-23 17:52:04 -0800172 // Sets the layer stack filtering settings for this output. See
173 // belongsInOutput for full details.
174 virtual void setLayerStackFilter(uint32_t layerStackId, bool isInternal) = 0;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700175
Lloyd Pique32cbe282018-10-19 13:09:22 -0700176 // Sets the output color mode
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800177 virtual void setColorProfile(const ColorProfile&) = 0;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700178
179 // Outputs a string with a state dump
180 virtual void dump(std::string&) const = 0;
181
182 // Gets the debug name for the output
183 virtual const std::string& getName() const = 0;
184
185 // Sets a debug name for the output
186 virtual void setName(const std::string&) = 0;
187
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700188 // Gets the current render color mode for the output
189 virtual DisplayColorProfile* getDisplayColorProfile() const = 0;
190
Lloyd Pique31cb2942018-10-19 17:23:03 -0700191 // Gets the current render surface for the output
192 virtual RenderSurface* getRenderSurface() const = 0;
193
Lloyd Pique32cbe282018-10-19 13:09:22 -0700194 using OutputCompositionState = compositionengine::impl::OutputCompositionState;
195
196 // Gets the raw composition state data for the output
197 // TODO(lpique): Make this protected once it is only internally called.
198 virtual const OutputCompositionState& getState() const = 0;
199
200 // Allows mutable access to the raw composition state data for the output.
201 // This is meant to be used by the various functions that are part of the
202 // composition process.
203 // TODO(lpique): Make this protected once it is only internally called.
204 virtual OutputCompositionState& editState() = 0;
205
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000206 // Gets the dirty region in layer stack space.
207 // If repaintEverything is true, this will be the full display bounds.
208 virtual Region getDirtyRegion(bool repaintEverything) const = 0;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700209
Lloyd Piqueef36b002019-01-23 17:52:04 -0800210 // Tests whether a given layerStackId belongs in this output.
211 // A layer belongs to the output if its layerStackId matches the of the output layerStackId,
212 // unless the layer should display on the primary output only and this is not the primary output
213
214 // A layer belongs to the output if its layerStackId matches. Additionally
215 // if the layer should only show in the internal (primary) display only and
216 // this output allows that.
Lloyd Piquec6687342019-03-07 21:34:57 -0800217 virtual bool belongsInOutput(std::optional<uint32_t> layerStackId, bool internalOnly) const = 0;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700218
Lloyd Pique66c20c42019-03-07 21:44:02 -0800219 // Determines if a layer belongs to the output.
Lloyd Piquede196652020-01-22 17:29:58 -0800220 virtual bool belongsInOutput(const sp<LayerFE>&) const = 0;
Lloyd Pique66c20c42019-03-07 21:44:02 -0800221
Lloyd Piquecc01a452018-12-04 17:24:00 -0800222 // Returns a pointer to the output layer corresponding to the given layer on
223 // this output, or nullptr if the layer does not have one
Lloyd Piquede196652020-01-22 17:29:58 -0800224 virtual OutputLayer* getOutputLayerForLayer(const sp<LayerFE>&) const = 0;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800225
Lloyd Pique01c77c12019-04-17 12:48:32 -0700226 // Immediately clears all layers from the output.
227 virtual void clearOutputLayers() = 0;
Lloyd Piquedf336d92019-03-07 21:38:42 -0800228
Lloyd Pique01c77c12019-04-17 12:48:32 -0700229 // For tests use only. Creates and appends an OutputLayer into the output.
Lloyd Piquede196652020-01-22 17:29:58 -0800230 virtual OutputLayer* injectOutputLayerForTest(const sp<LayerFE>&) = 0;
Lloyd Piquecc01a452018-12-04 17:24:00 -0800231
Lloyd Pique01c77c12019-04-17 12:48:32 -0700232 // Gets the count of output layers managed by this output
233 virtual size_t getOutputLayerCount() const = 0;
234
235 // Gets an output layer in Z order given its index
236 virtual OutputLayer* getOutputLayerOrderedByZByIndex(size_t) const = 0;
237
238 // A helper function for enumerating all the output layers in Z order using
239 // a C++11 range-based for loop.
240 auto getOutputLayersOrderedByZ() const { return OutputLayersEnumerator(*this); }
Lloyd Piquecc01a452018-12-04 17:24:00 -0800241
Lloyd Pique66d68602019-02-13 14:23:31 -0800242 // Sets the new set of layers being released this frame
Lloyd Piquec7ef21b2019-01-29 18:43:00 -0800243 virtual void setReleasedLayers(ReleasedLayers&&) = 0;
244
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800245 // Prepare the output, updating the OutputLayers used in the output
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800246 virtual void prepare(const CompositionRefreshArgs&, LayerFESet&) = 0;
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800247
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800248 // Presents the output, finalizing all composition details
249 virtual void present(const CompositionRefreshArgs&) = 0;
250
251 // Latches the front-end layer state for each output layer
252 virtual void updateLayerStateFromFE(const CompositionRefreshArgs&) const = 0;
Lloyd Pique6a3b4462019-03-07 20:58:12 -0800253
Lloyd Pique32cbe282018-10-19 13:09:22 -0700254protected:
Lloyd Pique3d0c02e2018-10-19 18:38:12 -0700255 virtual void setDisplayColorProfile(std::unique_ptr<DisplayColorProfile>) = 0;
256 virtual void setRenderSurface(std::unique_ptr<RenderSurface>) = 0;
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800257
Lloyd Pique01c77c12019-04-17 12:48:32 -0700258 virtual void rebuildLayerStacks(const CompositionRefreshArgs&, LayerFESet&) = 0;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800259 virtual void collectVisibleLayers(const CompositionRefreshArgs&, CoverageState&) = 0;
Lloyd Piquede196652020-01-22 17:29:58 -0800260 virtual void ensureOutputLayerIfVisible(sp<LayerFE>&, CoverageState&) = 0;
Lloyd Pique01c77c12019-04-17 12:48:32 -0700261 virtual void setReleasedLayers(const CompositionRefreshArgs&) = 0;
Lloyd Piquec29e4c62019-03-07 21:48:19 -0800262
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800263 virtual void updateAndWriteCompositionState(const CompositionRefreshArgs&) = 0;
264 virtual void setColorTransform(const CompositionRefreshArgs&) = 0;
265 virtual void updateColorProfile(const CompositionRefreshArgs&) = 0;
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800266 virtual void beginFrame() = 0;
267 virtual void prepareFrame() = 0;
Lloyd Pique3eb1b212019-03-07 21:15:40 -0800268 virtual void devOptRepaintFlash(const CompositionRefreshArgs&) = 0;
269 virtual void finishFrame(const CompositionRefreshArgs&) = 0;
Lucas Dupin2dd6f392020-02-18 17:43:36 -0800270 virtual std::optional<base::unique_fd> composeSurfaces(
271 const Region&, const compositionengine::CompositionRefreshArgs& refreshArgs) = 0;
Lloyd Piqued7b429f2019-03-07 21:11:02 -0800272 virtual void postFramebuffer() = 0;
Lloyd Pique66d68602019-02-13 14:23:31 -0800273 virtual void chooseCompositionStrategy() = 0;
Lloyd Pique688abd42019-02-15 15:42:24 -0800274 virtual bool getSkipColorTransform() const = 0;
Lloyd Pique35fca9d2019-02-13 14:24:11 -0800275 virtual FrameFences presentAndGetFrameFences() = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800276 virtual std::vector<LayerFE::LayerSettings> generateClientCompositionRequests(
Vishnu Nair3a7346c2019-12-04 08:09:09 -0800277 bool supportsProtectedContent, Region& clearRegion, ui::Dataspace outputDataspace) = 0;
Lloyd Pique688abd42019-02-15 15:42:24 -0800278 virtual void appendRegionFlashRequests(
279 const Region& flashRegion,
Vishnu Nair9b079a22020-01-21 14:36:08 -0800280 std::vector<LayerFE::LayerSettings>& clientCompositionLayers) = 0;
Lloyd Pique688abd42019-02-15 15:42:24 -0800281 virtual void setExpensiveRenderingExpected(bool enabled) = 0;
Vishnu Nair9b079a22020-01-21 14:36:08 -0800282 virtual void cacheClientCompositionRequests(uint32_t cacheSize) = 0;
Lloyd Pique32cbe282018-10-19 13:09:22 -0700283};
284
Peiyong Line9d809e2020-04-14 13:10:48 -0700285} // namespace compositionengine
286} // namespace android