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