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 <iosfwd>
20 
21 #include <math/mat4.h>
22 #include <math/vec3.h>
23 #include <renderengine/Texture.h>
24 #include <ui/Fence.h>
25 #include <ui/FloatRect.h>
26 #include <ui/GraphicBuffer.h>
27 #include <ui/GraphicTypes.h>
28 #include <ui/Rect.h>
29 #include <ui/Region.h>
30 #include <ui/Transform.h>
31 
32 namespace android {
33 namespace renderengine {
34 
35 // Metadata describing the input buffer to render from.
36 struct Buffer {
37     // Buffer containing the image that we will render.
38     // If buffer == nullptr, then the rest of the fields in this struct will be
39     // ignored.
40     sp<GraphicBuffer> buffer = nullptr;
41 
42     // Fence that will fire when the buffer is ready to be bound.
43     sp<Fence> fence = nullptr;
44 
45     // Texture identifier to bind the external texture to.
46     // TODO(alecmouri): This is GL-specific...make the type backend-agnostic.
47     uint32_t textureName = 0;
48 
49     // Whether to use filtering when rendering the texture.
50     bool useTextureFiltering = false;
51 
52     // Transform matrix to apply to texture coordinates.
53     mat4 textureTransform = mat4();
54 
55     // Whether to use pre-multiplied alpha.
56     bool usePremultipliedAlpha = true;
57 
58     // Override flag that alpha for each pixel in the buffer *must* be 1.0.
59     // LayerSettings::alpha is still used if isOpaque==true - this flag only
60     // overrides the alpha channel of the buffer.
61     bool isOpaque = false;
62 
63     // HDR color-space setting for Y410.
64     bool isY410BT2020 = false;
65     float maxMasteringLuminance = 0.0;
66     float maxContentLuminance = 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     float roundedCornersRadius = 0.0;
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 /*
104  * Contains the configuration for the shadows drawn by single layer. Shadow follows
105  * material design guidelines.
106  */
107 struct ShadowSettings {
108     // Color to the ambient shadow. The alpha is premultiplied.
109     vec4 ambientColor = vec4();
110 
111     // Color to the spot shadow. The alpha is premultiplied. The position of the spot shadow
112     // depends on the light position.
113     vec4 spotColor = vec4();
114 
115     // Position of the light source used to cast the spot shadow.
116     vec3 lightPos = vec3();
117 
118     // Radius of the spot light source. Smaller radius will have sharper edges,
119     // larger radius will have softer shadows
120     float lightRadius = 0.f;
121 
122     // Length of the cast shadow. If length is <= 0.f no shadows will be drawn.
123     float length = 0.f;
124 
125     // If true fill in the casting layer is translucent and the shadow needs to fill the bounds.
126     // Otherwise the shadow will only be drawn around the edges of the casting layer.
127     bool casterIsTranslucent = false;
128 };
129 
130 // The settings that RenderEngine requires for correctly rendering a Layer.
131 struct LayerSettings {
132     // Geometry information
133     Geometry geometry = Geometry();
134 
135     // Source pixels for this layer.
136     PixelSource source = PixelSource();
137 
138     // Alpha option to blend with the source pixels
139     half alpha = half(0.0);
140 
141     // Color space describing how the source pixels should be interpreted.
142     ui::Dataspace sourceDataspace = ui::Dataspace::UNKNOWN;
143 
144     // Additional layer-specific color transform to be applied before the global
145     // transform.
146     mat4 colorTransform = mat4();
147 
148     // True if blending will be forced to be disabled.
149     bool disableBlending = false;
150 
151     ShadowSettings shadow;
152 
153     int backgroundBlurRadius = 0;
154 };
155 
156 // Keep in sync with custom comparison function in
157 // compositionengine/impl/ClientCompositionRequestCache.cpp
158 static inline bool operator==(const Buffer& lhs, const Buffer& rhs) {
159     return lhs.buffer == rhs.buffer && lhs.fence == rhs.fence &&
160             lhs.textureName == rhs.textureName &&
161             lhs.useTextureFiltering == rhs.useTextureFiltering &&
162             lhs.textureTransform == rhs.textureTransform &&
163             lhs.usePremultipliedAlpha == rhs.usePremultipliedAlpha &&
164             lhs.isOpaque == rhs.isOpaque && lhs.isY410BT2020 == rhs.isY410BT2020 &&
165             lhs.maxMasteringLuminance == rhs.maxMasteringLuminance &&
166             lhs.maxContentLuminance == rhs.maxContentLuminance;
167 }
168 
169 static inline bool operator==(const Geometry& lhs, const Geometry& rhs) {
170     return lhs.boundaries == rhs.boundaries && lhs.positionTransform == rhs.positionTransform &&
171             lhs.roundedCornersRadius == rhs.roundedCornersRadius &&
172             lhs.roundedCornersCrop == rhs.roundedCornersCrop;
173 }
174 
175 static inline bool operator==(const PixelSource& lhs, const PixelSource& rhs) {
176     return lhs.buffer == rhs.buffer && lhs.solidColor == rhs.solidColor;
177 }
178 
179 static inline bool operator==(const ShadowSettings& lhs, const ShadowSettings& rhs) {
180     return lhs.ambientColor == rhs.ambientColor && lhs.spotColor == rhs.spotColor &&
181             lhs.lightPos == rhs.lightPos && lhs.lightRadius == rhs.lightRadius &&
182             lhs.length == rhs.length && lhs.casterIsTranslucent == rhs.casterIsTranslucent;
183 }
184 
185 static inline bool operator==(const LayerSettings& lhs, const LayerSettings& rhs) {
186     return lhs.geometry == rhs.geometry && lhs.source == rhs.source && lhs.alpha == rhs.alpha &&
187             lhs.sourceDataspace == rhs.sourceDataspace &&
188             lhs.colorTransform == rhs.colorTransform &&
189             lhs.disableBlending == rhs.disableBlending && lhs.shadow == rhs.shadow &&
190             lhs.backgroundBlurRadius == rhs.backgroundBlurRadius;
191 }
192 
193 // Defining PrintTo helps with Google Tests.
194 
PrintTo(const Buffer & settings,::std::ostream * os)195 static inline void PrintTo(const Buffer& settings, ::std::ostream* os) {
196     *os << "Buffer {";
197     *os << "\n    .buffer = " << settings.buffer.get();
198     *os << "\n    .fence = " << settings.fence.get();
199     *os << "\n    .textureName = " << settings.textureName;
200     *os << "\n    .useTextureFiltering = " << settings.useTextureFiltering;
201     *os << "\n    .textureTransform = " << settings.textureTransform;
202     *os << "\n    .usePremultipliedAlpha = " << settings.usePremultipliedAlpha;
203     *os << "\n    .isOpaque = " << settings.isOpaque;
204     *os << "\n    .isY410BT2020 = " << settings.isY410BT2020;
205     *os << "\n    .maxMasteringLuminance = " << settings.maxMasteringLuminance;
206     *os << "\n    .maxContentLuminance = " << settings.maxContentLuminance;
207     *os << "\n}";
208 }
209 
PrintTo(const Geometry & settings,::std::ostream * os)210 static inline void PrintTo(const Geometry& settings, ::std::ostream* os) {
211     *os << "Geometry {";
212     *os << "\n    .boundaries = ";
213     PrintTo(settings.boundaries, os);
214     *os << "\n    .positionTransform = " << settings.positionTransform;
215     *os << "\n    .roundedCornersRadius = " << settings.roundedCornersRadius;
216     *os << "\n    .roundedCornersCrop = ";
217     PrintTo(settings.roundedCornersCrop, os);
218     *os << "\n}";
219 }
220 
PrintTo(const PixelSource & settings,::std::ostream * os)221 static inline void PrintTo(const PixelSource& settings, ::std::ostream* os) {
222     *os << "PixelSource {";
223     *os << "\n    .buffer = ";
224     PrintTo(settings.buffer, os);
225     *os << "\n    .solidColor = " << settings.solidColor;
226     *os << "\n}";
227 }
228 
PrintTo(const ShadowSettings & settings,::std::ostream * os)229 static inline void PrintTo(const ShadowSettings& settings, ::std::ostream* os) {
230     *os << "ShadowSettings {";
231     *os << "\n    .ambientColor = " << settings.ambientColor;
232     *os << "\n    .spotColor = " << settings.spotColor;
233     *os << "\n    .lightPos = " << settings.lightPos;
234     *os << "\n    .lightRadius = " << settings.lightRadius;
235     *os << "\n    .length = " << settings.length;
236     *os << "\n    .casterIsTranslucent = " << settings.casterIsTranslucent;
237     *os << "\n}";
238 }
239 
PrintTo(const LayerSettings & settings,::std::ostream * os)240 static inline void PrintTo(const LayerSettings& settings, ::std::ostream* os) {
241     *os << "LayerSettings {";
242     *os << "\n    .geometry = ";
243     PrintTo(settings.geometry, os);
244     *os << "\n    .source = ";
245     PrintTo(settings.source, os);
246     *os << "\n    .alpha = " << settings.alpha;
247     *os << "\n    .sourceDataspace = ";
248     PrintTo(settings.sourceDataspace, os);
249     *os << "\n    .colorTransform = " << settings.colorTransform;
250     *os << "\n    .disableBlending = " << settings.disableBlending;
251     *os << "\n    .backgroundBlurRadius = " << settings.backgroundBlurRadius;
252     *os << "\n    .shadow = ";
253     PrintTo(settings.shadow, os);
254     *os << "\n}";
255 }
256 
257 } // namespace renderengine
258 } // namespace android
259