blob: 6e983524cecd88a37e6144c702184d3e90ce6c47 [file] [log] [blame]
Alec Mouri6e57f682018-09-29 20:45:08 -07001/*
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>
Alec Mouria90a5702021-04-16 16:36:21 +000021#include <renderengine/ExternalTexture.h>
Lucas Dupinc3800b82020-10-02 16:24:48 -070022#include <ui/BlurRegion.h>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080023#include <ui/Fence.h>
Alec Mouri6e57f682018-09-29 20:45:08 -070024#include <ui/FloatRect.h>
25#include <ui/GraphicBuffer.h>
26#include <ui/GraphicTypes.h>
27#include <ui/Rect.h>
28#include <ui/Region.h>
John Reckcdb4ed72021-02-04 13:39:33 -050029#include <ui/StretchEffect.h>
Alec Mouri6e57f682018-09-29 20:45:08 -070030#include <ui/Transform.h>
31
Alec Mouria90a5702021-04-16 16:36:21 +000032#include <iosfwd>
33
Alec Mouri6e57f682018-09-29 20:45:08 -070034namespace android {
35namespace renderengine {
36
37// Metadata describing the input buffer to render from.
38struct 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.
Alec Mouria90a5702021-04-16 16:36:21 +000042 std::shared_ptr<ExternalTexture> buffer = nullptr;
Alec Mouri6e57f682018-09-29 20:45:08 -070043
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080044 // Fence that will fire when the buffer is ready to be bound.
45 sp<Fence> fence = nullptr;
46
Alec Mouri6e57f682018-09-29 20:45:08 -070047 // Texture identifier to bind the external texture to.
48 // TODO(alecmouri): This is GL-specific...make the type backend-agnostic.
Alec Mouri1441bf72018-12-03 19:55:28 -080049 uint32_t textureName = 0;
Alec Mouri6e57f682018-09-29 20:45:08 -070050
51 // Whether to use filtering when rendering the texture.
Alec Mouri1441bf72018-12-03 19:55:28 -080052 bool useTextureFiltering = false;
Alec Mouri6e57f682018-09-29 20:45:08 -070053
54 // Transform matrix to apply to texture coordinates.
Alec Mouri1441bf72018-12-03 19:55:28 -080055 mat4 textureTransform = mat4();
Alec Mouri6e57f682018-09-29 20:45:08 -070056
Vishnu Nair9b079a22020-01-21 14:36:08 -080057 // Whether to use pre-multiplied alpha.
Alec Mouri1441bf72018-12-03 19:55:28 -080058 bool usePremultipliedAlpha = true;
Alec Mouri6e57f682018-09-29 20:45:08 -070059
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080060 // 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
Alec Mouri6e57f682018-09-29 20:45:08 -070065 // HDR color-space setting for Y410.
Alec Mouri1441bf72018-12-03 19:55:28 -080066 bool isY410BT2020 = false;
Peiyong Lin1a70eca2019-11-15 09:33:33 -080067 float maxMasteringLuminance = 0.0;
68 float maxContentLuminance = 0.0;
Alec Mouri6e57f682018-09-29 20:45:08 -070069};
70
71// Metadata describing the layer geometry.
72struct Geometry {
73 // Boundaries of the layer.
Alec Mouri1441bf72018-12-03 19:55:28 -080074 FloatRect boundaries = FloatRect();
Alec Mouri6e57f682018-09-29 20:45:08 -070075
76 // Transform matrix to apply to mesh coordinates.
Alec Mouri1441bf72018-12-03 19:55:28 -080077 mat4 positionTransform = mat4();
Alec Mouri7c94edb2018-12-03 21:23:26 -080078
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();
Alec Mouri6e57f682018-09-29 20:45:08 -070092};
93
94// Descriptor of the source pixels for this layer.
95struct PixelSource {
96 // Source buffer
Alec Mouri1441bf72018-12-03 19:55:28 -080097 Buffer buffer = Buffer();
Alec Mouri6e57f682018-09-29 20:45:08 -070098
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.
Alec Mouri1441bf72018-12-03 19:55:28 -0800102 half3 solidColor = half3(0.0f, 0.0f, 0.0f);
Alec Mouri6e57f682018-09-29 20:45:08 -0700103};
104
Vishnu Nair08f6eae2019-11-26 14:01:39 -0800105/*
106 * Contains the configuration for the shadows drawn by single layer. Shadow follows
107 * material design guidelines.
108 */
109struct ShadowSettings {
110 // Color to the ambient shadow. The alpha is premultiplied.
111 vec4 ambientColor = vec4();
112
113 // Color to the spot shadow. The alpha is premultiplied. The position of the spot shadow
114 // depends on the light position.
115 vec4 spotColor = vec4();
116
117 // Position of the light source used to cast the spot shadow.
118 vec3 lightPos = vec3();
119
120 // Radius of the spot light source. Smaller radius will have sharper edges,
121 // larger radius will have softer shadows
122 float lightRadius = 0.f;
123
124 // Length of the cast shadow. If length is <= 0.f no shadows will be drawn.
125 float length = 0.f;
126
127 // If true fill in the casting layer is translucent and the shadow needs to fill the bounds.
128 // Otherwise the shadow will only be drawn around the edges of the casting layer.
129 bool casterIsTranslucent = false;
130};
131
Alec Mouri6e57f682018-09-29 20:45:08 -0700132// The settings that RenderEngine requires for correctly rendering a Layer.
133struct LayerSettings {
134 // Geometry information
Alec Mouri1441bf72018-12-03 19:55:28 -0800135 Geometry geometry = Geometry();
Alec Mouri6e57f682018-09-29 20:45:08 -0700136
137 // Source pixels for this layer.
Alec Mouri1441bf72018-12-03 19:55:28 -0800138 PixelSource source = PixelSource();
Alec Mouri6e57f682018-09-29 20:45:08 -0700139
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800140 // Alpha option to blend with the source pixels
Alec Mouri1441bf72018-12-03 19:55:28 -0800141 half alpha = half(0.0);
Alec Mouri6e57f682018-09-29 20:45:08 -0700142
143 // Color space describing how the source pixels should be interpreted.
Alec Mouriac335532018-11-12 15:01:33 -0800144 ui::Dataspace sourceDataspace = ui::Dataspace::UNKNOWN;
Alec Mouri1089aed2018-10-25 21:33:57 -0700145
146 // Additional layer-specific color transform to be applied before the global
147 // transform.
Alec Mouriac335532018-11-12 15:01:33 -0800148 mat4 colorTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000149
150 // True if blending will be forced to be disabled.
151 bool disableBlending = false;
Vishnu Nair08f6eae2019-11-26 14:01:39 -0800152
153 ShadowSettings shadow;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800154
155 int backgroundBlurRadius = 0;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700156
157 std::vector<BlurRegion> blurRegions;
Ana Krulec6eab17a2020-12-09 15:52:36 -0800158
Derek Sollenberger39feade2021-04-27 16:08:40 -0400159 // Transform matrix used to convert the blurRegions geometry into the same
160 // coordinate space as LayerSettings.geometry
161 mat4 blurRegionTransform = mat4();
162
John Reckcdb4ed72021-02-04 13:39:33 -0500163 StretchEffect stretchEffect;
164
Ana Krulec6eab17a2020-12-09 15:52:36 -0800165 // Name associated with the layer for debugging purposes.
166 std::string name;
Alec Mouri6e57f682018-09-29 20:45:08 -0700167};
168
Vishnu Nair9b079a22020-01-21 14:36:08 -0800169// Keep in sync with custom comparison function in
170// compositionengine/impl/ClientCompositionRequestCache.cpp
Lloyd Pique6818fa52019-12-03 12:32:13 -0800171static inline bool operator==(const Buffer& lhs, const Buffer& rhs) {
172 return lhs.buffer == rhs.buffer && lhs.fence == rhs.fence &&
173 lhs.textureName == rhs.textureName &&
174 lhs.useTextureFiltering == rhs.useTextureFiltering &&
175 lhs.textureTransform == rhs.textureTransform &&
176 lhs.usePremultipliedAlpha == rhs.usePremultipliedAlpha &&
177 lhs.isOpaque == rhs.isOpaque && lhs.isY410BT2020 == rhs.isY410BT2020 &&
178 lhs.maxMasteringLuminance == rhs.maxMasteringLuminance &&
179 lhs.maxContentLuminance == rhs.maxContentLuminance;
180}
181
182static inline bool operator==(const Geometry& lhs, const Geometry& rhs) {
183 return lhs.boundaries == rhs.boundaries && lhs.positionTransform == rhs.positionTransform &&
184 lhs.roundedCornersRadius == rhs.roundedCornersRadius &&
185 lhs.roundedCornersCrop == rhs.roundedCornersCrop;
186}
187
188static inline bool operator==(const PixelSource& lhs, const PixelSource& rhs) {
189 return lhs.buffer == rhs.buffer && lhs.solidColor == rhs.solidColor;
190}
191
192static inline bool operator==(const ShadowSettings& lhs, const ShadowSettings& rhs) {
193 return lhs.ambientColor == rhs.ambientColor && lhs.spotColor == rhs.spotColor &&
194 lhs.lightPos == rhs.lightPos && lhs.lightRadius == rhs.lightRadius &&
195 lhs.length == rhs.length && lhs.casterIsTranslucent == rhs.casterIsTranslucent;
196}
197
Lucas Dupinc3800b82020-10-02 16:24:48 -0700198static inline bool operator==(const BlurRegion& lhs, const BlurRegion& rhs) {
199 return lhs.alpha == rhs.alpha && lhs.cornerRadiusTL == rhs.cornerRadiusTL &&
200 lhs.cornerRadiusTR == rhs.cornerRadiusTR && lhs.cornerRadiusBL == rhs.cornerRadiusBL &&
201 lhs.cornerRadiusBR == rhs.cornerRadiusBR && lhs.blurRadius == rhs.blurRadius &&
202 lhs.left == rhs.left && lhs.top == rhs.top && lhs.right == rhs.right &&
203 lhs.bottom == rhs.bottom;
204}
205
206static inline bool operator!=(const BlurRegion& lhs, const BlurRegion& rhs) {
207 return !(lhs == rhs);
208}
209
Lloyd Pique6818fa52019-12-03 12:32:13 -0800210static inline bool operator==(const LayerSettings& lhs, const LayerSettings& rhs) {
Lucas Dupinc3800b82020-10-02 16:24:48 -0700211 if (lhs.blurRegions.size() != rhs.blurRegions.size()) {
212 return false;
213 }
214 const auto size = lhs.blurRegions.size();
215 for (size_t i = 0; i < size; i++) {
216 if (lhs.blurRegions[i] != rhs.blurRegions[i]) {
217 return false;
218 }
219 }
220
Lloyd Pique6818fa52019-12-03 12:32:13 -0800221 return lhs.geometry == rhs.geometry && lhs.source == rhs.source && lhs.alpha == rhs.alpha &&
222 lhs.sourceDataspace == rhs.sourceDataspace &&
223 lhs.colorTransform == rhs.colorTransform &&
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800224 lhs.disableBlending == rhs.disableBlending && lhs.shadow == rhs.shadow &&
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700225 lhs.backgroundBlurRadius == rhs.backgroundBlurRadius &&
Derek Sollenberger83e3dd42021-05-04 11:48:25 -0400226 lhs.blurRegionTransform == rhs.blurRegionTransform &&
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700227 lhs.stretchEffect == rhs.stretchEffect;
Lloyd Pique6818fa52019-12-03 12:32:13 -0800228}
229
230// Defining PrintTo helps with Google Tests.
231
232static inline void PrintTo(const Buffer& settings, ::std::ostream* os) {
233 *os << "Buffer {";
234 *os << "\n .buffer = " << settings.buffer.get();
235 *os << "\n .fence = " << settings.fence.get();
236 *os << "\n .textureName = " << settings.textureName;
237 *os << "\n .useTextureFiltering = " << settings.useTextureFiltering;
238 *os << "\n .textureTransform = " << settings.textureTransform;
239 *os << "\n .usePremultipliedAlpha = " << settings.usePremultipliedAlpha;
240 *os << "\n .isOpaque = " << settings.isOpaque;
241 *os << "\n .isY410BT2020 = " << settings.isY410BT2020;
242 *os << "\n .maxMasteringLuminance = " << settings.maxMasteringLuminance;
243 *os << "\n .maxContentLuminance = " << settings.maxContentLuminance;
244 *os << "\n}";
245}
246
247static inline void PrintTo(const Geometry& settings, ::std::ostream* os) {
248 *os << "Geometry {";
249 *os << "\n .boundaries = ";
250 PrintTo(settings.boundaries, os);
251 *os << "\n .positionTransform = " << settings.positionTransform;
252 *os << "\n .roundedCornersRadius = " << settings.roundedCornersRadius;
253 *os << "\n .roundedCornersCrop = ";
254 PrintTo(settings.roundedCornersCrop, os);
255 *os << "\n}";
256}
257
258static inline void PrintTo(const PixelSource& settings, ::std::ostream* os) {
259 *os << "PixelSource {";
260 *os << "\n .buffer = ";
261 PrintTo(settings.buffer, os);
262 *os << "\n .solidColor = " << settings.solidColor;
263 *os << "\n}";
264}
265
266static inline void PrintTo(const ShadowSettings& settings, ::std::ostream* os) {
267 *os << "ShadowSettings {";
268 *os << "\n .ambientColor = " << settings.ambientColor;
269 *os << "\n .spotColor = " << settings.spotColor;
270 *os << "\n .lightPos = " << settings.lightPos;
271 *os << "\n .lightRadius = " << settings.lightRadius;
272 *os << "\n .length = " << settings.length;
273 *os << "\n .casterIsTranslucent = " << settings.casterIsTranslucent;
274 *os << "\n}";
275}
276
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700277static inline void PrintTo(const StretchEffect& effect, ::std::ostream* os) {
278 *os << "StretchEffect {";
279 *os << "\n .width = " << effect.width;
280 *os << "\n .height = " << effect.height;
281 *os << "\n .vectorX = " << effect.vectorX;
282 *os << "\n .vectorY = " << effect.vectorY;
283 *os << "\n .maxAmountX = " << effect.maxAmountX;
284 *os << "\n .maxAmountY = " << effect.maxAmountY;
285 *os << "\n .mappedLeft = " << effect.mappedChildBounds.left;
286 *os << "\n .mappedTop = " << effect.mappedChildBounds.top;
287 *os << "\n .mappedRight = " << effect.mappedChildBounds.right;
288 *os << "\n .mappedBottom = " << effect.mappedChildBounds.bottom;
289 *os << "\n}";
290}
291
Lloyd Pique6818fa52019-12-03 12:32:13 -0800292static inline void PrintTo(const LayerSettings& settings, ::std::ostream* os) {
293 *os << "LayerSettings {";
294 *os << "\n .geometry = ";
295 PrintTo(settings.geometry, os);
296 *os << "\n .source = ";
297 PrintTo(settings.source, os);
298 *os << "\n .alpha = " << settings.alpha;
299 *os << "\n .sourceDataspace = ";
300 PrintTo(settings.sourceDataspace, os);
301 *os << "\n .colorTransform = " << settings.colorTransform;
302 *os << "\n .disableBlending = " << settings.disableBlending;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800303 *os << "\n .backgroundBlurRadius = " << settings.backgroundBlurRadius;
Ana Krulece40f5f52020-12-17 11:24:12 -0800304 for (auto blurRegion : settings.blurRegions) {
305 *os << "\n";
306 PrintTo(blurRegion, os);
307 }
Lloyd Pique6818fa52019-12-03 12:32:13 -0800308 *os << "\n .shadow = ";
309 PrintTo(settings.shadow, os);
Nader Jawad2dfc98b2021-04-08 20:35:39 -0700310 *os << "\n .stretchEffect = ";
311 PrintTo(settings.stretchEffect, os);
Lloyd Pique6818fa52019-12-03 12:32:13 -0800312 *os << "\n}";
313}
314
Alec Mouri6e57f682018-09-29 20:45:08 -0700315} // namespace renderengine
316} // namespace android