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