| 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 | |
| Lloyd Pique | 6818fa5 | 2019-12-03 12:32:13 -0800 | [diff] [blame] | 19 | #include <iosfwd> |
| 20 | |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 21 | #include <math/mat4.h> |
| 22 | #include <math/vec3.h> |
| 23 | #include <renderengine/Texture.h> |
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 24 | #include <ui/BlurRegion.h> |
| Alec Mouri | 0d5e1eb | 2018-11-10 20:40:12 -0800 | [diff] [blame] | 25 | #include <ui/Fence.h> |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 26 | #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 | |
| 33 | namespace android { |
| 34 | namespace renderengine { |
| 35 | |
| 36 | // Metadata describing the input buffer to render from. |
| 37 | struct 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 Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 41 | sp<GraphicBuffer> buffer = nullptr; |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 42 | |
| Alec Mouri | 0d5e1eb | 2018-11-10 20:40:12 -0800 | [diff] [blame] | 43 | // Fence that will fire when the buffer is ready to be bound. |
| 44 | sp<Fence> fence = nullptr; |
| 45 | |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 46 | // Texture identifier to bind the external texture to. |
| 47 | // TODO(alecmouri): This is GL-specific...make the type backend-agnostic. |
| Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 48 | uint32_t textureName = 0; |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 49 | |
| 50 | // Whether to use filtering when rendering the texture. |
| Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 51 | bool useTextureFiltering = false; |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 52 | |
| 53 | // Transform matrix to apply to texture coordinates. |
| Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 54 | mat4 textureTransform = mat4(); |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 55 | |
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 56 | // Whether to use pre-multiplied alpha. |
| Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 57 | bool usePremultipliedAlpha = true; |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 58 | |
| Alec Mouri | 0d5e1eb | 2018-11-10 20:40:12 -0800 | [diff] [blame] | 59 | // 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 Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 64 | // HDR color-space setting for Y410. |
| Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 65 | bool isY410BT2020 = false; |
| Peiyong Lin | 1a70eca | 2019-11-15 09:33:33 -0800 | [diff] [blame] | 66 | float maxMasteringLuminance = 0.0; |
| 67 | float maxContentLuminance = 0.0; |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | // Metadata describing the layer geometry. |
| 71 | struct Geometry { |
| 72 | // Boundaries of the layer. |
| Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 73 | FloatRect boundaries = FloatRect(); |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 74 | |
| 75 | // Transform matrix to apply to mesh coordinates. |
| Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 76 | mat4 positionTransform = mat4(); |
| Alec Mouri | 7c94edb | 2018-12-03 21:23:26 -0800 | [diff] [blame] | 77 | |
| 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 Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 91 | }; |
| 92 | |
| 93 | // Descriptor of the source pixels for this layer. |
| 94 | struct PixelSource { |
| 95 | // Source buffer |
| Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 96 | Buffer buffer = Buffer(); |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 97 | |
| 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 Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 101 | half3 solidColor = half3(0.0f, 0.0f, 0.0f); |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 102 | }; |
| 103 | |
| Vishnu Nair | 08f6eae | 2019-11-26 14:01:39 -0800 | [diff] [blame] | 104 | /* |
| 105 | * Contains the configuration for the shadows drawn by single layer. Shadow follows |
| 106 | * material design guidelines. |
| 107 | */ |
| 108 | struct 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 Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 131 | // The settings that RenderEngine requires for correctly rendering a Layer. |
| 132 | struct LayerSettings { |
| 133 | // Geometry information |
| Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 134 | Geometry geometry = Geometry(); |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 135 | |
| 136 | // Source pixels for this layer. |
| Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 137 | PixelSource source = PixelSource(); |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 138 | |
| Alec Mouri | 0d5e1eb | 2018-11-10 20:40:12 -0800 | [diff] [blame] | 139 | // Alpha option to blend with the source pixels |
| Alec Mouri | 1441bf7 | 2018-12-03 19:55:28 -0800 | [diff] [blame] | 140 | half alpha = half(0.0); |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 141 | |
| 142 | // Color space describing how the source pixels should be interpreted. |
| Alec Mouri | ac33553 | 2018-11-12 15:01:33 -0800 | [diff] [blame] | 143 | ui::Dataspace sourceDataspace = ui::Dataspace::UNKNOWN; |
| Alec Mouri | 1089aed | 2018-10-25 21:33:57 -0700 | [diff] [blame] | 144 | |
| 145 | // Additional layer-specific color transform to be applied before the global |
| 146 | // transform. |
| Alec Mouri | ac33553 | 2018-11-12 15:01:33 -0800 | [diff] [blame] | 147 | mat4 colorTransform = mat4(); |
| Alec Mouri | e7d1d4a | 2019-02-05 01:13:46 +0000 | [diff] [blame] | 148 | |
| 149 | // True if blending will be forced to be disabled. |
| 150 | bool disableBlending = false; |
| Vishnu Nair | 08f6eae | 2019-11-26 14:01:39 -0800 | [diff] [blame] | 151 | |
| 152 | ShadowSettings shadow; |
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 153 | |
| 154 | int backgroundBlurRadius = 0; |
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 155 | |
| 156 | std::vector<BlurRegion> blurRegions; |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 157 | }; |
| 158 | |
| Vishnu Nair | 9b079a2 | 2020-01-21 14:36:08 -0800 | [diff] [blame] | 159 | // Keep in sync with custom comparison function in |
| 160 | // compositionengine/impl/ClientCompositionRequestCache.cpp |
| Lloyd Pique | 6818fa5 | 2019-12-03 12:32:13 -0800 | [diff] [blame] | 161 | static 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 | |
| 172 | static 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 | |
| 178 | static inline bool operator==(const PixelSource& lhs, const PixelSource& rhs) { |
| 179 | return lhs.buffer == rhs.buffer && lhs.solidColor == rhs.solidColor; |
| 180 | } |
| 181 | |
| 182 | static 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 Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 188 | static 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 | |
| 196 | static inline bool operator!=(const BlurRegion& lhs, const BlurRegion& rhs) { |
| 197 | return !(lhs == rhs); |
| 198 | } |
| 199 | |
| Lloyd Pique | 6818fa5 | 2019-12-03 12:32:13 -0800 | [diff] [blame] | 200 | static inline bool operator==(const LayerSettings& lhs, const LayerSettings& rhs) { |
| Lucas Dupin | c3800b8 | 2020-10-02 16:24:48 -0700 | [diff] [blame] | 201 | 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 Pique | 6818fa5 | 2019-12-03 12:32:13 -0800 | [diff] [blame] | 211 | return lhs.geometry == rhs.geometry && lhs.source == rhs.source && lhs.alpha == rhs.alpha && |
| 212 | lhs.sourceDataspace == rhs.sourceDataspace && |
| 213 | lhs.colorTransform == rhs.colorTransform && |
| Lucas Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 214 | lhs.disableBlending == rhs.disableBlending && lhs.shadow == rhs.shadow && |
| 215 | lhs.backgroundBlurRadius == rhs.backgroundBlurRadius; |
| Lloyd Pique | 6818fa5 | 2019-12-03 12:32:13 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| 218 | // Defining PrintTo helps with Google Tests. |
| 219 | |
| 220 | static 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 | |
| 235 | static 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 | |
| 246 | static 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 | |
| 254 | static 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 | |
| 265 | static 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 Dupin | 19c8f0e | 2019-11-25 17:55:44 -0800 | [diff] [blame] | 276 | *os << "\n .backgroundBlurRadius = " << settings.backgroundBlurRadius; |
| Ana Krulec | e40f5f5 | 2020-12-17 11:24:12 -0800 | [diff] [blame^] | 277 | for (auto blurRegion : settings.blurRegions) { |
| 278 | *os << "\n"; |
| 279 | PrintTo(blurRegion, os); |
| 280 | } |
| Lloyd Pique | 6818fa5 | 2019-12-03 12:32:13 -0800 | [diff] [blame] | 281 | *os << "\n .shadow = "; |
| 282 | PrintTo(settings.shadow, os); |
| 283 | *os << "\n}"; |
| 284 | } |
| 285 | |
| Alec Mouri | 6e57f68 | 2018-09-29 20:45:08 -0700 | [diff] [blame] | 286 | } // namespace renderengine |
| 287 | } // namespace android |