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 <optional>
20 #include <ostream>
21 #include <unordered_set>
22 #include "aidl/android/hardware/graphics/composer3/Composition.h"
23 #include "ui/LayerStack.h"
24
25 // TODO(b/129481165): remove the #pragma below and fix conversion issues
26 #pragma clang diagnostic push
27 #pragma clang diagnostic ignored "-Wconversion"
28 #pragma clang diagnostic ignored "-Wextra"
29
30 #include <renderengine/LayerSettings.h>
31
32 // TODO(b/129481165): remove the #pragma below and fix conversion issues
33 #pragma clang diagnostic pop // ignored "-Wconversion -Wextra"
34
35 #include <ftl/future.h>
36 #include <ui/FenceResult.h>
37 #include <utils/RefBase.h>
38 #include <utils/Timers.h>
39
40 namespace android {
41
42 class Fence;
43
44 namespace gui {
45 struct LayerMetadata;
46 }
47
48 namespace compositionengine {
49
50 struct LayerFECompositionState;
51
52 // Defines the interface used by the CompositionEngine to make requests
53 // of the front-end layer
54 class LayerFE : public virtual RefBase {
55 public:
56 // Gets the raw front-end composition state data for the layer
57 virtual const LayerFECompositionState* getCompositionState() const = 0;
58
59 // Called before composition starts. Should return true if this layer has
60 // pending updates which would require an extra display refresh cycle to
61 // process.
62 virtual bool onPreComposition(bool updatingOutputGeometryThisFrame) = 0;
63
64 struct ClientCompositionTargetSettings {
65 enum class BlurSetting {
66 Disabled,
67 BackgroundBlurOnly,
68 BlurRegionsOnly,
69 Enabled,
70 };
71
toStringClientCompositionTargetSettings72 friend std::string toString(BlurSetting blurSetting) {
73 switch (blurSetting) {
74 case BlurSetting::Enabled:
75 return "Enabled";
76 case BlurSetting::BlurRegionsOnly:
77 return "BlurRegionsOnly";
78 case BlurSetting::BackgroundBlurOnly:
79 return "BackgroundBlurOnly";
80 case BlurSetting::Disabled:
81 return "Disabled";
82 }
83 }
84
85 friend std::ostream& operator<<(std::ostream& os, const BlurSetting& setting) {
86 return os << toString(setting);
87 }
88
89 // The clip region, or visible region that is being rendered to
90 const Region& clip;
91
92 // If set to true, the layer should enable filtering when rendering.
93 const bool needsFiltering;
94
95 // If set to true, the buffer is being sent to a destination that is
96 // expected to treat the buffer contents as secure.
97 const bool isSecure;
98
99 // If set to true, the target buffer has protected content support.
100 const bool isProtected;
101
102 // Viewport of the target being rendered to. This is used to determine
103 // the shadow light position.
104 const Rect& viewport;
105
106 // Dataspace of the output so we can optimize how to render the shadow
107 // by avoiding unnecessary color space conversions.
108 const ui::Dataspace dataspace;
109
110 // True if the region excluding the shadow is visible.
111 const bool realContentIsVisible;
112
113 // If set to true, change the layer settings to render a clear output.
114 // This may be requested by the HWC
115 const bool clearContent;
116
117 // Configure layer settings for using blurs
118 BlurSetting blurSetting;
119
120 // Requested white point of the layer in nits
121 const float whitePointNits;
122
123 // True if layers with 170M dataspace should be overridden to sRGB.
124 const bool treat170mAsSrgb;
125
126 std::shared_ptr<gui::DisplayLuts> luts;
127 };
128
129 // A superset of LayerSettings required by RenderEngine to compose a layer
130 // and buffer info to determine duplicate client composition requests.
131 struct LayerSettings : renderengine::LayerSettings {
132 // Currently latched buffer if, 0 if invalid.
133 uint64_t bufferId = 0;
134
135 // Currently latched frame number, 0 if invalid.
136 uint64_t frameNumber = 0;
137
138 // layer serial number, -1 if invalid.
139 int32_t sequence = -1;
140 };
141
142 // Describes the states of the release fence. Checking the states allows checks
143 // to ensure that set_value() is not called on the same promise multiple times,
144 // and can indicate if the promise has been fulfilled.
145 enum class ReleaseFencePromiseStatus {
146 UNINITIALIZED, // Promise not created
147 INITIALIZED, // Promise created, fence has not been set
148 FULFILLED // Promise fulfilled, fence is set
149 };
150
151 // Returns the LayerSettings to pass to RenderEngine::drawLayers. The state may contain shadows
152 // casted by the layer or the content of the layer itself. If the layer does not render then an
153 // empty optional will be returned.
154 virtual std::optional<LayerSettings> prepareClientComposition(
155 ClientCompositionTargetSettings&) const = 0;
156
157 // Initializes a promise for a buffer release fence and provides the future for that
158 // fence. This should only be called when a promise has not yet been created, or
159 // after the previous promise has already been fulfilled. Attempting to call this
160 // when an existing promise is INITIALIZED will fail because the promise has not
161 // yet been fulfilled.
162 virtual ftl::Future<FenceResult> createReleaseFenceFuture() = 0;
163
164 // Sets promise with its buffer's release fence
165 virtual void setReleaseFence(const FenceResult& releaseFence) = 0;
166
167 // Checks if the buffer's release fence has been set
168 virtual LayerFE::ReleaseFencePromiseStatus getReleaseFencePromiseStatus() = 0;
169
170 virtual void setReleasedBuffer(sp<GraphicBuffer> buffer) = 0;
171
172 // Indicates that the picture profile request was applied to this layer.
173 virtual void onPictureProfileCommitted() = 0;
174
175 // Gets some kind of identifier for the layer for debug purposes.
176 virtual const char* getDebugName() const = 0;
177
178 // Gets the sequence number: a serial number that uniquely identifies a Layer
179 virtual int32_t getSequence() const = 0;
180
181 // Whether the layer should be rendered with rounded corners.
182 virtual bool hasRoundedCorners() const = 0;
setWasClientComposed(const sp<Fence> &)183 virtual void setWasClientComposed(const sp<Fence>&) {}
184
185 // These fields are all copied from the last written HWC state.
186 // This state is only used for debugging purposes.
187 struct HwcLayerDebugState {
188 aidl::android::hardware::graphics::composer3::Composition lastCompositionType =
189 aidl::android::hardware::graphics::composer3::Composition::INVALID;
190 // Corresponds to passing an alpha of 0 to HWC2::Layer::setPlaneAlpha.
191 bool wasSkipped = false;
192
193 // Indicates whether the compositionengine::OutputLayer had properties overwritten.
194 // Not directly passed to HWC.
195 bool wasOverridden = false;
196
197 // Corresponds to the GraphicBuffer ID of the buffer passed to HWC2::Layer::setBuffer.
198 // This buffer corresponds to a CachedSet that the LayerFE was flattened to.
199 uint64_t overrideBufferId = 0;
200 };
201
202 // Used for debugging purposes, e.g. perfetto tracing, dumpsys.
203 virtual void setLastHwcState(const LayerFE::HwcLayerDebugState &hwcState) = 0;
204 virtual const HwcLayerDebugState &getLastHwcState() const = 0;
205
206 virtual const gui::LayerMetadata* getMetadata() const = 0;
207 virtual const gui::LayerMetadata* getRelativeMetadata() const = 0;
208 };
209
210 // TODO(b/121291683): Specialize std::hash<> for sp<T> so these and others can
211 // be removed.
212 struct LayerFESpHash {
operatorLayerFESpHash213 size_t operator()(const sp<LayerFE>& p) const { return std::hash<LayerFE*>()(p.get()); }
214 };
215
216 using LayerFESet = std::unordered_set<sp<LayerFE>, LayerFESpHash>;
217
218 static inline bool operator==(const LayerFE::ClientCompositionTargetSettings& lhs,
219 const LayerFE::ClientCompositionTargetSettings& rhs) {
220 return lhs.clip.hasSameRects(rhs.clip) && lhs.needsFiltering == rhs.needsFiltering &&
221 lhs.isSecure == rhs.isSecure && lhs.isProtected == rhs.isProtected &&
222 lhs.viewport == rhs.viewport && lhs.dataspace == rhs.dataspace &&
223 lhs.realContentIsVisible == rhs.realContentIsVisible &&
224 lhs.clearContent == rhs.clearContent;
225 }
226
227 static inline bool operator==(const LayerFE::LayerSettings& lhs,
228 const LayerFE::LayerSettings& rhs) {
229 return static_cast<const renderengine::LayerSettings&>(lhs) ==
230 static_cast<const renderengine::LayerSettings&>(rhs) &&
231 lhs.bufferId == rhs.bufferId && lhs.frameNumber == rhs.frameNumber;
232 }
233
234 // Defining PrintTo helps with Google Tests.
PrintTo(const LayerFE::ClientCompositionTargetSettings & settings,::std::ostream * os)235 static inline void PrintTo(const LayerFE::ClientCompositionTargetSettings& settings,
236 ::std::ostream* os) {
237 *os << "ClientCompositionTargetSettings{";
238 *os << "\n .clip = \n";
239 PrintTo(settings.clip, os);
240 *os << "\n .needsFiltering = " << settings.needsFiltering;
241 *os << "\n .isSecure = " << settings.isSecure;
242 *os << "\n .isProtected = " << settings.isProtected;
243 *os << "\n .viewport = ";
244 PrintTo(settings.viewport, os);
245 *os << "\n .dataspace = ";
246 PrintTo(settings.dataspace, os);
247 *os << "\n .realContentIsVisible = " << settings.realContentIsVisible;
248 *os << "\n .clearContent = " << settings.clearContent;
249 *os << "\n .blurSetting = " << settings.blurSetting;
250 *os << "\n}";
251 }
252
PrintTo(const LayerFE::LayerSettings & settings,::std::ostream * os)253 static inline void PrintTo(const LayerFE::LayerSettings& settings, ::std::ostream* os) {
254 *os << "LayerFE::LayerSettings{";
255 PrintTo(static_cast<const renderengine::LayerSettings&>(settings), os);
256 *os << "\n .bufferId = " << settings.bufferId;
257 *os << "\n .frameNumber = " << settings.frameNumber;
258 *os << "\n}";
259 }
260
261 } // namespace compositionengine
262 } // namespace android
263