1 /*
2 * Copyright 2018 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 <gui/DisplayLuts.h>
20 #include <math/mat4.h>
21 #include <math/vec3.h>
22 #include <renderengine/ExternalTexture.h>
23 #include <renderengine/PrintMatrix.h>
24 #include <ui/BlurRegion.h>
25 #include <ui/DebugUtils.h>
26 #include <ui/Fence.h>
27 #include <ui/FloatRect.h>
28 #include <ui/GraphicBuffer.h>
29 #include <ui/GraphicTypes.h>
30 #include <ui/Rect.h>
31 #include <ui/Region.h>
32 #include <ui/ShadowSettings.h>
33 #include <ui/StretchEffect.h>
34 #include <ui/Transform.h>
35 #include "ui/EdgeExtensionEffect.h"
36
37 #include <iosfwd>
38
39 namespace android {
40 namespace renderengine {
41
42 // Metadata describing the input buffer to render from.
43 struct Buffer {
44 // Buffer containing the image that we will render.
45 // If buffer == nullptr, then the rest of the fields in this struct will be
46 // ignored.
47 std::shared_ptr<ExternalTexture> buffer = nullptr;
48
49 // Fence that will fire when the buffer is ready to be bound.
50 sp<Fence> fence = nullptr;
51
52 // Whether to use filtering when rendering the texture.
53 bool useTextureFiltering = false;
54
55 // Transform matrix to apply to texture coordinates.
56 mat4 textureTransform = mat4();
57
58 // Whether to use pre-multiplied alpha.
59 bool usePremultipliedAlpha = true;
60
61 // Override flag that alpha for each pixel in the buffer *must* be 1.0.
62 // LayerSettings::alpha is still used if isOpaque==true - this flag only
63 // overrides the alpha channel of the buffer.
64 bool isOpaque = false;
65
66 float maxLuminanceNits = 0.0;
67 };
68
69 // Metadata describing the layer geometry.
70 struct Geometry {
71 // Boundaries of the layer.
72 FloatRect boundaries = FloatRect();
73
74 // Transform matrix to apply to mesh coordinates.
75 mat4 positionTransform = mat4();
76
77 // Radius of rounded corners, if greater than 0. Otherwise, this layer's
78 // corners are not rounded.
79 // Having corner radius will force GPU composition on the layer and its children, drawing it
80 // with a special shader. The shader will receive the radius and the crop rectangle as input,
81 // modifying the opacity of the destination texture, multiplying it by a number between 0 and 1.
82 // We query Layer#getRoundedCornerState() to retrieve the radius as well as the rounded crop
83 // rectangle to figure out how to apply the radius for this layer. The crop rectangle will be
84 // in local layer coordinate space, so we have to take the layer transform into account when
85 // walking up the tree.
86 vec2 roundedCornersRadius = vec2(0.0f, 0.0f);
87
88 // Rectangle within which corners will be rounded.
89 FloatRect roundedCornersCrop = FloatRect();
90 };
91
92 // Descriptor of the source pixels for this layer.
93 struct PixelSource {
94 // Source buffer
95 Buffer buffer = Buffer();
96
97 // The solid color with which to fill the layer.
98 // This should only be populated if we don't render from an application
99 // buffer.
100 half3 solidColor = half3(0.0f, 0.0f, 0.0f);
101 };
102
103 // The settings that RenderEngine requires for correctly rendering a Layer.
104 struct LayerSettings {
105 // Geometry information
106 Geometry geometry = Geometry();
107
108 // Source pixels for this layer.
109 PixelSource source = PixelSource();
110
111 // Alpha option to blend with the source pixels
112 half alpha = half(0.0);
113
114 // Color space describing how the source pixels should be interpreted.
115 ui::Dataspace sourceDataspace = ui::Dataspace::UNKNOWN;
116
117 // Additional layer-specific color transform to be applied before the global
118 // transform.
119 mat4 colorTransform = mat4();
120
121 // True if blending will be forced to be disabled.
122 bool disableBlending = false;
123
124 // If true, then this layer casts a shadow and/or blurs behind it, but it does
125 // not otherwise draw any of the layer's other contents.
126 bool skipContentDraw = false;
127
128 ShadowSettings shadow;
129
130 int backgroundBlurRadius = 0;
131
132 std::vector<BlurRegion> blurRegions;
133
134 // Transform matrix used to convert the blurRegions geometry into the same
135 // coordinate space as LayerSettings.geometry
136 mat4 blurRegionTransform = mat4();
137
138 StretchEffect stretchEffect;
139 EdgeExtensionEffect edgeExtensionEffect;
140
141 // Name associated with the layer for debugging purposes.
142 std::string name;
143
144 // Luminance of the white point for this layer. Used for linear dimming.
145 // Individual layers will be dimmed by (whitePointNits / maxWhitePoint).
146 // If white point nits are unknown, then this layer is assumed to have the
147 // same luminance as the brightest layer in the scene.
148 float whitePointNits = -1.f;
149
150 std::shared_ptr<gui::DisplayLuts> luts;
151 };
152
153 // Keep in sync with custom comparison function in
154 // compositionengine/impl/ClientCompositionRequestCache.cpp
155 static inline bool operator==(const Buffer& lhs, const Buffer& rhs) {
156 return lhs.buffer == rhs.buffer && lhs.fence == rhs.fence &&
157 lhs.useTextureFiltering == rhs.useTextureFiltering &&
158 lhs.textureTransform == rhs.textureTransform &&
159 lhs.usePremultipliedAlpha == rhs.usePremultipliedAlpha &&
160 lhs.isOpaque == rhs.isOpaque && lhs.maxLuminanceNits == rhs.maxLuminanceNits;
161 }
162
163 static inline bool operator==(const Geometry& lhs, const Geometry& rhs) {
164 return lhs.boundaries == rhs.boundaries && lhs.positionTransform == rhs.positionTransform &&
165 lhs.roundedCornersRadius == rhs.roundedCornersRadius &&
166 lhs.roundedCornersCrop == rhs.roundedCornersCrop;
167 }
168
169 static inline bool operator==(const PixelSource& lhs, const PixelSource& rhs) {
170 return lhs.buffer == rhs.buffer && lhs.solidColor == rhs.solidColor;
171 }
172
173 static inline bool operator==(const LayerSettings& lhs, const LayerSettings& rhs) {
174 if (lhs.blurRegions.size() != rhs.blurRegions.size()) {
175 return false;
176 }
177 const auto size = lhs.blurRegions.size();
178 for (size_t i = 0; i < size; i++) {
179 if (lhs.blurRegions[i] != rhs.blurRegions[i]) {
180 return false;
181 }
182 }
183
184 return lhs.geometry == rhs.geometry && lhs.source == rhs.source && lhs.alpha == rhs.alpha &&
185 lhs.sourceDataspace == rhs.sourceDataspace &&
186 lhs.colorTransform == rhs.colorTransform &&
187 lhs.disableBlending == rhs.disableBlending &&
188 lhs.skipContentDraw == rhs.skipContentDraw && lhs.shadow == rhs.shadow &&
189 lhs.backgroundBlurRadius == rhs.backgroundBlurRadius &&
190 lhs.blurRegionTransform == rhs.blurRegionTransform &&
191 lhs.stretchEffect == rhs.stretchEffect &&
192 lhs.edgeExtensionEffect == rhs.edgeExtensionEffect &&
193 lhs.whitePointNits == rhs.whitePointNits && lhs.luts == rhs.luts;
194 }
195
PrintTo(const Buffer & settings,::std::ostream * os)196 static inline void PrintTo(const Buffer& settings, ::std::ostream* os) {
197 *os << "Buffer {";
198 *os << "\n .buffer = " << settings.buffer.get() << " "
199 << (settings.buffer.get() ? decodePixelFormat(settings.buffer->getPixelFormat()).c_str()
200 : "");
201 *os << "\n .fence = " << settings.fence.get();
202 *os << "\n .useTextureFiltering = " << settings.useTextureFiltering;
203 *os << "\n .textureTransform = ";
204 PrintMatrix(settings.textureTransform, os);
205 *os << "\n .usePremultipliedAlpha = " << settings.usePremultipliedAlpha;
206 *os << "\n .isOpaque = " << settings.isOpaque;
207 *os << "\n .maxLuminanceNits = " << settings.maxLuminanceNits;
208 *os << "\n}";
209 }
210
PrintTo(const Geometry & settings,::std::ostream * os)211 static inline void PrintTo(const Geometry& settings, ::std::ostream* os) {
212 *os << "Geometry {";
213 *os << "\n .boundaries = ";
214 PrintTo(settings.boundaries, os);
215 *os << "\n .positionTransform = ";
216 PrintMatrix(settings.positionTransform, os);
217 *os << "\n .roundedCornersRadiusX = " << settings.roundedCornersRadius.x;
218 *os << "\n .roundedCornersRadiusY = " << settings.roundedCornersRadius.y;
219 *os << "\n .roundedCornersCrop = ";
220 PrintTo(settings.roundedCornersCrop, os);
221 *os << "\n}";
222 }
223
PrintTo(const PixelSource & settings,::std::ostream * os)224 static inline void PrintTo(const PixelSource& settings, ::std::ostream* os) {
225 *os << "PixelSource {";
226 if (settings.buffer.buffer) {
227 *os << "\n .buffer = ";
228 PrintTo(settings.buffer, os);
229 *os << "\n}";
230 } else {
231 *os << "\n .solidColor = " << settings.solidColor;
232 *os << "\n}";
233 }
234 }
235
PrintTo(const ShadowSettings & settings,::std::ostream * os)236 static inline void PrintTo(const ShadowSettings& settings, ::std::ostream* os) {
237 *os << "ShadowSettings {";
238 *os << "\n .boundaries = ";
239 PrintTo(settings.boundaries, os);
240 *os << "\n .ambientColor = " << settings.ambientColor;
241 *os << "\n .spotColor = " << settings.spotColor;
242 *os << "\n .lightPos = " << settings.lightPos;
243 *os << "\n .lightRadius = " << settings.lightRadius;
244 *os << "\n .length = " << settings.length;
245 *os << "\n .casterIsTranslucent = " << settings.casterIsTranslucent;
246 *os << "\n}";
247 }
248
PrintTo(const StretchEffect & effect,::std::ostream * os)249 static inline void PrintTo(const StretchEffect& effect, ::std::ostream* os) {
250 *os << "StretchEffect {";
251 *os << "\n .width = " << effect.width;
252 *os << "\n .height = " << effect.height;
253 *os << "\n .vectorX = " << effect.vectorX;
254 *os << "\n .vectorY = " << effect.vectorY;
255 *os << "\n .maxAmountX = " << effect.maxAmountX;
256 *os << "\n .maxAmountY = " << effect.maxAmountY;
257 *os << "\n .mappedLeft = " << effect.mappedChildBounds.left;
258 *os << "\n .mappedTop = " << effect.mappedChildBounds.top;
259 *os << "\n .mappedRight = " << effect.mappedChildBounds.right;
260 *os << "\n .mappedBottom = " << effect.mappedChildBounds.bottom;
261 *os << "\n}";
262 }
263
PrintTo(const EdgeExtensionEffect & effect,::std::ostream * os)264 static inline void PrintTo(const EdgeExtensionEffect& effect, ::std::ostream* os) {
265 *os << effect;
266 }
267
PrintTo(const LayerSettings & settings,::std::ostream * os)268 static inline void PrintTo(const LayerSettings& settings, ::std::ostream* os) {
269 *os << "LayerSettings for '" << settings.name.c_str() << "' {";
270 *os << "\n .geometry = ";
271 PrintTo(settings.geometry, os);
272 *os << "\n .source = ";
273 PrintTo(settings.source, os);
274 *os << "\n .alpha = " << settings.alpha;
275 *os << "\n .sourceDataspace = ";
276 PrintTo(settings.sourceDataspace, os);
277 *os << "\n .colorTransform = ";
278 PrintMatrix(settings.colorTransform, os);
279 *os << "\n .disableBlending = " << settings.disableBlending;
280 *os << "\n .skipContentDraw = " << settings.skipContentDraw;
281 if (settings.shadow != ShadowSettings()) {
282 *os << "\n .shadow = ";
283 PrintTo(settings.shadow, os);
284 }
285 *os << "\n .backgroundBlurRadius = " << settings.backgroundBlurRadius;
286 if (settings.blurRegions.size()) {
287 *os << "\n .blurRegions =";
288 for (auto blurRegion : settings.blurRegions) {
289 *os << "\n";
290 PrintTo(blurRegion, os);
291 }
292 }
293 *os << "\n .blurRegionTransform = ";
294 PrintMatrix(settings.blurRegionTransform, os);
295 if (settings.stretchEffect != StretchEffect()) {
296 *os << "\n .stretchEffect = ";
297 PrintTo(settings.stretchEffect, os);
298 }
299
300 if (settings.edgeExtensionEffect.hasEffect()) {
301 *os << "\n .edgeExtensionEffect = " << settings.edgeExtensionEffect;
302 }
303 *os << "\n .whitePointNits = " << settings.whitePointNits;
304 *os << "\n}";
305 }
306
307 } // namespace renderengine
308 } // namespace android
309