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