Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 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> |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 21 | #include <renderengine/ExternalTexture.h> |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 22 | #include <ui/BlurRegion.h> |
Alec Mouri | 0d5e1eb | 2018-11-10 20:40:12 -0800 | [diff] [blame] | 23 | #include <ui/Fence.h> |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 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> |
John Reck | cdb4ed7 | 2021-02-04 13:39:33 -0500 | [diff] [blame] | 29 | #include <ui/StretchEffect.h> |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 30 | #include <ui/Transform.h> |
| 31 | |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 32 | #include <iosfwd> |
| 33 | |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 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. |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 42 | std::shared_ptr<ExternalTexture> buffer = nullptr; |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 43 | |
Alec Mouri | 0d5e1eb | 2018-11-10 20:40:12 -0800 | [diff] [blame] | 44 | // Fence that will fire when the buffer is ready to be bound. |
| 45 | sp<Fence> fence = nullptr; |
| 46 | |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 47 | // Texture identifier to bind the external texture to. |
| 48 | // TODO(alecmouri): This is GL-specific...make the type backend-agnostic. |
Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 49 | uint32_t textureName = 0; |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 50 | |
| 51 | // Whether to use filtering when rendering the texture. |
Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 52 | bool useTextureFiltering = false; |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 53 | |
| 54 | // Transform matrix to apply to texture coordinates. |
Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 55 | mat4 textureTransform = mat4(); |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 56 | |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 57 | // Whether to use pre-multiplied alpha. |
Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 58 | bool usePremultipliedAlpha = true; |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 59 | |
Alec Mouri | 0d5e1eb | 2018-11-10 20:40:12 -0800 | [diff] [blame] | 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 | |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 65 | // HDR color-space setting for Y410. |
Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 66 | bool isY410BT2020 = false; |
Peiyong Lin | 1a70eca | 2019-11-15 09:33:33 -0800 | [diff] [blame] | 67 | float maxMasteringLuminance = 0.0; |
| 68 | float maxContentLuminance = 0.0; |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | // Metadata describing the layer geometry. |
| 72 | struct Geometry { |
| 73 | // Boundaries of the layer. |
Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 74 | FloatRect boundaries = FloatRect(); |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 75 | |
| 76 | // Transform matrix to apply to mesh coordinates. |
Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 77 | mat4 positionTransform = mat4(); |
Alec Mouri | 7c94edb | 2018-12-03 21:23:26 -0800 | [diff] [blame] | 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(); |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | // Descriptor of the source pixels for this layer. |
| 95 | struct PixelSource { |
| 96 | // Source buffer |
Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 97 | Buffer buffer = Buffer(); |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 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. |
Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 102 | half3 solidColor = half3(0.0f, 0.0f, 0.0f); |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 103 | }; |
| 104 | |
Vishnu Nair | 08f6eae | 2019-11-26 14:01:39 -0800 | [diff] [blame] | 105 | /* |
| 106 | * Contains the configuration for the shadows drawn by single layer. Shadow follows |
| 107 | * material design guidelines. |
| 108 | */ |
| 109 | struct 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 Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 132 | // The settings that RenderEngine requires for correctly rendering a Layer. |
| 133 | struct LayerSettings { |
| 134 | // Geometry information |
Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 135 | Geometry geometry = Geometry(); |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 136 | |
| 137 | // Source pixels for this layer. |
Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 138 | PixelSource source = PixelSource(); |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 139 | |
Alec Mouri | 0d5e1eb | 2018-11-10 20:40:12 -0800 | [diff] [blame] | 140 | // Alpha option to blend with the source pixels |
Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 141 | half alpha = half(0.0); |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 142 | |
| 143 | // Color space describing how the source pixels should be interpreted. |
Alec Mouri | ac33553 | 2018-11-12 15:01:33 -0800 | [diff] [blame] | 144 | ui::Dataspace sourceDataspace = ui::Dataspace::UNKNOWN; |
Alec Mouri | 1089aed | 2018-10-25 21:33:57 -0700 | [diff] [blame] | 145 | |
| 146 | // Additional layer-specific color transform to be applied before the global |
| 147 | // transform. |
Alec Mouri | ac33553 | 2018-11-12 15:01:33 -0800 | [diff] [blame] | 148 | mat4 colorTransform = mat4(); |
Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 149 | |
| 150 | // True if blending will be forced to be disabled. |
| 151 | bool disableBlending = false; |
Vishnu Nair | 08f6eae | 2019-11-26 14:01:39 -0800 | [diff] [blame] | 152 | |
| 153 | ShadowSettings shadow; |
Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 154 | |
| 155 | int backgroundBlurRadius = 0; |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 156 | |
| 157 | std::vector<BlurRegion> blurRegions; |
Ana Krulec | 6eab17a | 2020-12-09 15:52:36 -0800 | [diff] [blame] | 158 | |
Derek Sollenberger | 39feade | 2021-04-27 16:08:40 -0400 | [diff] [blame] | 159 | // Transform matrix used to convert the blurRegions geometry into the same |
| 160 | // coordinate space as LayerSettings.geometry |
| 161 | mat4 blurRegionTransform = mat4(); |
| 162 | |
John Reck | cdb4ed7 | 2021-02-04 13:39:33 -0500 | [diff] [blame] | 163 | StretchEffect stretchEffect; |
| 164 | |
Ana Krulec | 6eab17a | 2020-12-09 15:52:36 -0800 | [diff] [blame] | 165 | // Name associated with the layer for debugging purposes. |
| 166 | std::string name; |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 167 | }; |
| 168 | |
Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 169 | // Keep in sync with custom comparison function in |
| 170 | // compositionengine/impl/ClientCompositionRequestCache.cpp |
Lloyd Pique | 6818fa5 | 2019-12-03 12:32:13 -0800 | [diff] [blame] | 171 | static 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 | |
| 182 | static 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 | |
| 188 | static inline bool operator==(const PixelSource& lhs, const PixelSource& rhs) { |
| 189 | return lhs.buffer == rhs.buffer && lhs.solidColor == rhs.solidColor; |
| 190 | } |
| 191 | |
| 192 | static 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 Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 198 | static 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 | |
| 206 | static inline bool operator!=(const BlurRegion& lhs, const BlurRegion& rhs) { |
| 207 | return !(lhs == rhs); |
| 208 | } |
| 209 | |
Lloyd Pique | 6818fa5 | 2019-12-03 12:32:13 -0800 | [diff] [blame] | 210 | static inline bool operator==(const LayerSettings& lhs, const LayerSettings& rhs) { |
Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 211 | 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 Pique | 6818fa5 | 2019-12-03 12:32:13 -0800 | [diff] [blame] | 221 | return lhs.geometry == rhs.geometry && lhs.source == rhs.source && lhs.alpha == rhs.alpha && |
| 222 | lhs.sourceDataspace == rhs.sourceDataspace && |
| 223 | lhs.colorTransform == rhs.colorTransform && |
Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 224 | lhs.disableBlending == rhs.disableBlending && lhs.shadow == rhs.shadow && |
Nader Jawad | 2dfc98b | 2021-04-08 20:35:39 -0700 | [diff] [blame] | 225 | lhs.backgroundBlurRadius == rhs.backgroundBlurRadius && |
Derek Sollenberger | 83e3dd4 | 2021-05-04 11:48:25 -0400 | [diff] [blame] | 226 | lhs.blurRegionTransform == rhs.blurRegionTransform && |
Nader Jawad | 2dfc98b | 2021-04-08 20:35:39 -0700 | [diff] [blame] | 227 | lhs.stretchEffect == rhs.stretchEffect; |
Lloyd Pique | 6818fa5 | 2019-12-03 12:32:13 -0800 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | // Defining PrintTo helps with Google Tests. |
| 231 | |
| 232 | static 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 | |
| 247 | static 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 | |
| 258 | static 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 | |
| 266 | static 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 Jawad | 2dfc98b | 2021-04-08 20:35:39 -0700 | [diff] [blame] | 277 | static 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 Pique | 6818fa5 | 2019-12-03 12:32:13 -0800 | [diff] [blame] | 292 | static 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 Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 303 | *os << "\n .backgroundBlurRadius = " << settings.backgroundBlurRadius; |
Ana Krulec | e40f5f5 | 2020-12-17 11:24:12 -0800 | [diff] [blame] | 304 | for (auto blurRegion : settings.blurRegions) { |
| 305 | *os << "\n"; |
| 306 | PrintTo(blurRegion, os); |
| 307 | } |
Lloyd Pique | 6818fa5 | 2019-12-03 12:32:13 -0800 | [diff] [blame] | 308 | *os << "\n .shadow = "; |
| 309 | PrintTo(settings.shadow, os); |
Nader Jawad | 2dfc98b | 2021-04-08 20:35:39 -0700 | [diff] [blame] | 310 | *os << "\n .stretchEffect = "; |
| 311 | PrintTo(settings.stretchEffect, os); |
Lloyd Pique | 6818fa5 | 2019-12-03 12:32:13 -0800 | [diff] [blame] | 312 | *os << "\n}"; |
| 313 | } |
| 314 | |
Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 315 | } // namespace renderengine |
| 316 | } // namespace android |