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