blob: 965d1669512c9fe489c0a72931d236d75aa1f1ea [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
Lloyd Pique6818fa52019-12-03 12:32:13 -080019#include <iosfwd>
20
Alec Mouri6e57f682018-09-29 20:45:08 -070021#include <math/mat4.h>
22#include <math/vec3.h>
23#include <renderengine/Texture.h>
Lucas Dupinc3800b82020-10-02 16:24:48 -070024#include <ui/BlurRegion.h>
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080025#include <ui/Fence.h>
Alec Mouri6e57f682018-09-29 20:45:08 -070026#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/Transform.h>
32
33namespace android {
34namespace renderengine {
35
36// Metadata describing the input buffer to render from.
37struct Buffer {
38 // Buffer containing the image that we will render.
39 // If buffer == nullptr, then the rest of the fields in this struct will be
40 // ignored.
Alec Mouri1441bf72018-12-03 19:55:28 -080041 sp<GraphicBuffer> buffer = nullptr;
Alec Mouri6e57f682018-09-29 20:45:08 -070042
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080043 // Fence that will fire when the buffer is ready to be bound.
44 sp<Fence> fence = nullptr;
45
Alec Mouri6e57f682018-09-29 20:45:08 -070046 // Texture identifier to bind the external texture to.
47 // TODO(alecmouri): This is GL-specific...make the type backend-agnostic.
Alec Mouri1441bf72018-12-03 19:55:28 -080048 uint32_t textureName = 0;
Alec Mouri6e57f682018-09-29 20:45:08 -070049
50 // Whether to use filtering when rendering the texture.
Alec Mouri1441bf72018-12-03 19:55:28 -080051 bool useTextureFiltering = false;
Alec Mouri6e57f682018-09-29 20:45:08 -070052
53 // Transform matrix to apply to texture coordinates.
Alec Mouri1441bf72018-12-03 19:55:28 -080054 mat4 textureTransform = mat4();
Alec Mouri6e57f682018-09-29 20:45:08 -070055
Vishnu Nair9b079a22020-01-21 14:36:08 -080056 // Whether to use pre-multiplied alpha.
Alec Mouri1441bf72018-12-03 19:55:28 -080057 bool usePremultipliedAlpha = true;
Alec Mouri6e57f682018-09-29 20:45:08 -070058
Alec Mouri0d5e1eb2018-11-10 20:40:12 -080059 // Override flag that alpha for each pixel in the buffer *must* be 1.0.
60 // LayerSettings::alpha is still used if isOpaque==true - this flag only
61 // overrides the alpha channel of the buffer.
62 bool isOpaque = false;
63
Alec Mouri6e57f682018-09-29 20:45:08 -070064 // HDR color-space setting for Y410.
Alec Mouri1441bf72018-12-03 19:55:28 -080065 bool isY410BT2020 = false;
Peiyong Lin1a70eca2019-11-15 09:33:33 -080066 float maxMasteringLuminance = 0.0;
67 float maxContentLuminance = 0.0;
Alec Mouri6e57f682018-09-29 20:45:08 -070068};
69
70// Metadata describing the layer geometry.
71struct Geometry {
72 // Boundaries of the layer.
Alec Mouri1441bf72018-12-03 19:55:28 -080073 FloatRect boundaries = FloatRect();
Alec Mouri6e57f682018-09-29 20:45:08 -070074
75 // Transform matrix to apply to mesh coordinates.
Alec Mouri1441bf72018-12-03 19:55:28 -080076 mat4 positionTransform = mat4();
Alec Mouri7c94edb2018-12-03 21:23:26 -080077
78 // Radius of rounded corners, if greater than 0. Otherwise, this layer's
79 // corners are not rounded.
80 // Having corner radius will force GPU composition on the layer and its children, drawing it
81 // with a special shader. The shader will receive the radius and the crop rectangle as input,
82 // modifying the opacity of the destination texture, multiplying it by a number between 0 and 1.
83 // We query Layer#getRoundedCornerState() to retrieve the radius as well as the rounded crop
84 // rectangle to figure out how to apply the radius for this layer. The crop rectangle will be
85 // in local layer coordinate space, so we have to take the layer transform into account when
86 // walking up the tree.
87 float roundedCornersRadius = 0.0;
88
89 // Rectangle within which corners will be rounded.
90 FloatRect roundedCornersCrop = FloatRect();
Alec Mouri6e57f682018-09-29 20:45:08 -070091};
92
93// Descriptor of the source pixels for this layer.
94struct PixelSource {
95 // Source buffer
Alec Mouri1441bf72018-12-03 19:55:28 -080096 Buffer buffer = Buffer();
Alec Mouri6e57f682018-09-29 20:45:08 -070097
98 // The solid color with which to fill the layer.
99 // This should only be populated if we don't render from an application
100 // buffer.
Alec Mouri1441bf72018-12-03 19:55:28 -0800101 half3 solidColor = half3(0.0f, 0.0f, 0.0f);
Alec Mouri6e57f682018-09-29 20:45:08 -0700102};
103
Vishnu Nair08f6eae2019-11-26 14:01:39 -0800104/*
105 * Contains the configuration for the shadows drawn by single layer. Shadow follows
106 * material design guidelines.
107 */
108struct ShadowSettings {
109 // Color to the ambient shadow. The alpha is premultiplied.
110 vec4 ambientColor = vec4();
111
112 // Color to the spot shadow. The alpha is premultiplied. The position of the spot shadow
113 // depends on the light position.
114 vec4 spotColor = vec4();
115
116 // Position of the light source used to cast the spot shadow.
117 vec3 lightPos = vec3();
118
119 // Radius of the spot light source. Smaller radius will have sharper edges,
120 // larger radius will have softer shadows
121 float lightRadius = 0.f;
122
123 // Length of the cast shadow. If length is <= 0.f no shadows will be drawn.
124 float length = 0.f;
125
126 // If true fill in the casting layer is translucent and the shadow needs to fill the bounds.
127 // Otherwise the shadow will only be drawn around the edges of the casting layer.
128 bool casterIsTranslucent = false;
129};
130
Alec Mouri6e57f682018-09-29 20:45:08 -0700131// The settings that RenderEngine requires for correctly rendering a Layer.
132struct LayerSettings {
133 // Geometry information
Alec Mouri1441bf72018-12-03 19:55:28 -0800134 Geometry geometry = Geometry();
Alec Mouri6e57f682018-09-29 20:45:08 -0700135
136 // Source pixels for this layer.
Alec Mouri1441bf72018-12-03 19:55:28 -0800137 PixelSource source = PixelSource();
Alec Mouri6e57f682018-09-29 20:45:08 -0700138
Alec Mouri0d5e1eb2018-11-10 20:40:12 -0800139 // Alpha option to blend with the source pixels
Alec Mouri1441bf72018-12-03 19:55:28 -0800140 half alpha = half(0.0);
Alec Mouri6e57f682018-09-29 20:45:08 -0700141
142 // Color space describing how the source pixels should be interpreted.
Alec Mouriac335532018-11-12 15:01:33 -0800143 ui::Dataspace sourceDataspace = ui::Dataspace::UNKNOWN;
Alec Mouri1089aed2018-10-25 21:33:57 -0700144
145 // Additional layer-specific color transform to be applied before the global
146 // transform.
Alec Mouriac335532018-11-12 15:01:33 -0800147 mat4 colorTransform = mat4();
Alec Mourie7d1d4a2019-02-05 01:13:46 +0000148
149 // True if blending will be forced to be disabled.
150 bool disableBlending = false;
Vishnu Nair08f6eae2019-11-26 14:01:39 -0800151
152 ShadowSettings shadow;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800153
154 int backgroundBlurRadius = 0;
Lucas Dupinc3800b82020-10-02 16:24:48 -0700155
156 std::vector<BlurRegion> blurRegions;
Alec Mouri6e57f682018-09-29 20:45:08 -0700157};
158
Vishnu Nair9b079a22020-01-21 14:36:08 -0800159// Keep in sync with custom comparison function in
160// compositionengine/impl/ClientCompositionRequestCache.cpp
Lloyd Pique6818fa52019-12-03 12:32:13 -0800161static inline bool operator==(const Buffer& lhs, const Buffer& rhs) {
162 return lhs.buffer == rhs.buffer && lhs.fence == rhs.fence &&
163 lhs.textureName == rhs.textureName &&
164 lhs.useTextureFiltering == rhs.useTextureFiltering &&
165 lhs.textureTransform == rhs.textureTransform &&
166 lhs.usePremultipliedAlpha == rhs.usePremultipliedAlpha &&
167 lhs.isOpaque == rhs.isOpaque && lhs.isY410BT2020 == rhs.isY410BT2020 &&
168 lhs.maxMasteringLuminance == rhs.maxMasteringLuminance &&
169 lhs.maxContentLuminance == rhs.maxContentLuminance;
170}
171
172static inline bool operator==(const Geometry& lhs, const Geometry& rhs) {
173 return lhs.boundaries == rhs.boundaries && lhs.positionTransform == rhs.positionTransform &&
174 lhs.roundedCornersRadius == rhs.roundedCornersRadius &&
175 lhs.roundedCornersCrop == rhs.roundedCornersCrop;
176}
177
178static inline bool operator==(const PixelSource& lhs, const PixelSource& rhs) {
179 return lhs.buffer == rhs.buffer && lhs.solidColor == rhs.solidColor;
180}
181
182static inline bool operator==(const ShadowSettings& lhs, const ShadowSettings& rhs) {
183 return lhs.ambientColor == rhs.ambientColor && lhs.spotColor == rhs.spotColor &&
184 lhs.lightPos == rhs.lightPos && lhs.lightRadius == rhs.lightRadius &&
185 lhs.length == rhs.length && lhs.casterIsTranslucent == rhs.casterIsTranslucent;
186}
187
Lucas Dupinc3800b82020-10-02 16:24:48 -0700188static inline bool operator==(const BlurRegion& lhs, const BlurRegion& rhs) {
189 return lhs.alpha == rhs.alpha && lhs.cornerRadiusTL == rhs.cornerRadiusTL &&
190 lhs.cornerRadiusTR == rhs.cornerRadiusTR && lhs.cornerRadiusBL == rhs.cornerRadiusBL &&
191 lhs.cornerRadiusBR == rhs.cornerRadiusBR && lhs.blurRadius == rhs.blurRadius &&
192 lhs.left == rhs.left && lhs.top == rhs.top && lhs.right == rhs.right &&
193 lhs.bottom == rhs.bottom;
194}
195
196static inline bool operator!=(const BlurRegion& lhs, const BlurRegion& rhs) {
197 return !(lhs == rhs);
198}
199
Lloyd Pique6818fa52019-12-03 12:32:13 -0800200static inline bool operator==(const LayerSettings& lhs, const LayerSettings& rhs) {
Lucas Dupinc3800b82020-10-02 16:24:48 -0700201 if (lhs.blurRegions.size() != rhs.blurRegions.size()) {
202 return false;
203 }
204 const auto size = lhs.blurRegions.size();
205 for (size_t i = 0; i < size; i++) {
206 if (lhs.blurRegions[i] != rhs.blurRegions[i]) {
207 return false;
208 }
209 }
210
Lloyd Pique6818fa52019-12-03 12:32:13 -0800211 return lhs.geometry == rhs.geometry && lhs.source == rhs.source && lhs.alpha == rhs.alpha &&
212 lhs.sourceDataspace == rhs.sourceDataspace &&
213 lhs.colorTransform == rhs.colorTransform &&
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800214 lhs.disableBlending == rhs.disableBlending && lhs.shadow == rhs.shadow &&
215 lhs.backgroundBlurRadius == rhs.backgroundBlurRadius;
Lloyd Pique6818fa52019-12-03 12:32:13 -0800216}
217
218// Defining PrintTo helps with Google Tests.
219
220static inline void PrintTo(const Buffer& settings, ::std::ostream* os) {
221 *os << "Buffer {";
222 *os << "\n .buffer = " << settings.buffer.get();
223 *os << "\n .fence = " << settings.fence.get();
224 *os << "\n .textureName = " << settings.textureName;
225 *os << "\n .useTextureFiltering = " << settings.useTextureFiltering;
226 *os << "\n .textureTransform = " << settings.textureTransform;
227 *os << "\n .usePremultipliedAlpha = " << settings.usePremultipliedAlpha;
228 *os << "\n .isOpaque = " << settings.isOpaque;
229 *os << "\n .isY410BT2020 = " << settings.isY410BT2020;
230 *os << "\n .maxMasteringLuminance = " << settings.maxMasteringLuminance;
231 *os << "\n .maxContentLuminance = " << settings.maxContentLuminance;
232 *os << "\n}";
233}
234
235static inline void PrintTo(const Geometry& settings, ::std::ostream* os) {
236 *os << "Geometry {";
237 *os << "\n .boundaries = ";
238 PrintTo(settings.boundaries, os);
239 *os << "\n .positionTransform = " << settings.positionTransform;
240 *os << "\n .roundedCornersRadius = " << settings.roundedCornersRadius;
241 *os << "\n .roundedCornersCrop = ";
242 PrintTo(settings.roundedCornersCrop, os);
243 *os << "\n}";
244}
245
246static inline void PrintTo(const PixelSource& settings, ::std::ostream* os) {
247 *os << "PixelSource {";
248 *os << "\n .buffer = ";
249 PrintTo(settings.buffer, os);
250 *os << "\n .solidColor = " << settings.solidColor;
251 *os << "\n}";
252}
253
254static inline void PrintTo(const ShadowSettings& settings, ::std::ostream* os) {
255 *os << "ShadowSettings {";
256 *os << "\n .ambientColor = " << settings.ambientColor;
257 *os << "\n .spotColor = " << settings.spotColor;
258 *os << "\n .lightPos = " << settings.lightPos;
259 *os << "\n .lightRadius = " << settings.lightRadius;
260 *os << "\n .length = " << settings.length;
261 *os << "\n .casterIsTranslucent = " << settings.casterIsTranslucent;
262 *os << "\n}";
263}
264
265static inline void PrintTo(const LayerSettings& settings, ::std::ostream* os) {
266 *os << "LayerSettings {";
267 *os << "\n .geometry = ";
268 PrintTo(settings.geometry, os);
269 *os << "\n .source = ";
270 PrintTo(settings.source, os);
271 *os << "\n .alpha = " << settings.alpha;
272 *os << "\n .sourceDataspace = ";
273 PrintTo(settings.sourceDataspace, os);
274 *os << "\n .colorTransform = " << settings.colorTransform;
275 *os << "\n .disableBlending = " << settings.disableBlending;
Lucas Dupin19c8f0e2019-11-25 17:55:44 -0800276 *os << "\n .backgroundBlurRadius = " << settings.backgroundBlurRadius;
Ana Krulece40f5f52020-12-17 11:24:12 -0800277 for (auto blurRegion : settings.blurRegions) {
278 *os << "\n";
279 PrintTo(blurRegion, os);
280 }
Lloyd Pique6818fa52019-12-03 12:32:13 -0800281 *os << "\n .shadow = ";
282 PrintTo(settings.shadow, os);
283 *os << "\n}";
284}
285
Alec Mouri6e57f682018-09-29 20:45:08 -0700286} // namespace renderengine
287} // namespace android