Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021 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 | */ |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 16 | #include "Cache.h" |
| 17 | #include "AutoBackendTexture.h" |
| 18 | #include "SkiaRenderEngine.h" |
| 19 | #include "android-base/unique_fd.h" |
| 20 | #include "renderengine/DisplaySettings.h" |
| 21 | #include "renderengine/LayerSettings.h" |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 22 | #include "renderengine/impl/ExternalTexture.h" |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 23 | #include "ui/GraphicBuffer.h" |
| 24 | #include "ui/GraphicTypes.h" |
| 25 | #include "ui/PixelFormat.h" |
| 26 | #include "ui/Rect.h" |
| 27 | #include "utils/Timers.h" |
| 28 | |
| 29 | namespace android::renderengine::skia { |
| 30 | |
Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 31 | namespace { |
Nathaniel Nifong | 4fc750d | 2021-03-19 11:37:36 -0400 | [diff] [blame] | 32 | |
Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 33 | // clang-format off |
| 34 | // Any non-identity matrix will do. |
| 35 | const auto kScaleAndTranslate = mat4(0.7f, 0.f, 0.f, 0.f, |
| 36 | 0.f, 0.7f, 0.f, 0.f, |
| 37 | 0.f, 0.f, 1.f, 0.f, |
| 38 | 67.3f, 52.2f, 0.f, 1.f); |
Nathaniel Nifong | afeac5b | 2021-05-27 10:52:30 -0400 | [diff] [blame] | 39 | const auto kScaleAsymmetric = mat4(0.8f, 0.f, 0.f, 0.f, |
| 40 | 0.f, 1.1f, 0.f, 0.f, |
| 41 | 0.f, 0.f, 1.f, 0.f, |
| 42 | 0.f, 0.f, 0.f, 1.f); |
Nathaniel Nifong | 1349150 | 2021-06-30 17:28:29 -0400 | [diff] [blame] | 43 | const auto kFlip = mat4(1.1f, -0.1f, 0.f, 0.f, |
| 44 | 0.1f, 1.1f, 0.f, 0.f, |
| 45 | 0.f, 0.f, 1.f, 0.f, |
| 46 | 2.f, 2.f, 0.f, 1.f); |
Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 47 | // clang-format on |
Nathaniel Nifong | bf6f754 | 2021-04-27 12:05:16 -0400 | [diff] [blame] | 48 | // When setting layer.sourceDataspace, whether it matches the destination or not determines whether |
| 49 | // a color correction effect is added to the shader. |
Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 50 | constexpr auto kDestDataSpace = ui::Dataspace::SRGB; |
Nathaniel Nifong | 21e021f | 2021-04-21 13:15:46 -0400 | [diff] [blame] | 51 | constexpr auto kOtherDataSpace = ui::Dataspace::DISPLAY_P3; |
Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 52 | } // namespace |
| 53 | |
Nathaniel Nifong | 4fc750d | 2021-03-19 11:37:36 -0400 | [diff] [blame] | 54 | static void drawShadowLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display, |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 55 | const std::shared_ptr<ExternalTexture>& dstTexture) { |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 56 | // Somewhat arbitrary dimensions, but on screen and slightly shorter, based |
| 57 | // on actual use. |
Nathaniel Nifong | a6b5423 | 2021-07-02 13:24:32 -0400 | [diff] [blame] | 58 | const Rect& displayRect = display.physicalDisplay; |
| 59 | FloatRect rect(0, 0, displayRect.width(), displayRect.height()); |
| 60 | FloatRect smallerRect(20, 20, displayRect.width()-20, displayRect.height()-20); |
| 61 | |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 62 | LayerSettings layer{ |
| 63 | .geometry = |
| 64 | Geometry{ |
| 65 | .boundaries = rect, |
| 66 | .roundedCornersCrop = rect, |
Vishnu Nair | 50c0afe | 2022-07-11 15:04:07 -0700 | [diff] [blame] | 67 | .roundedCornersRadius = {50.f, 50.f}, |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 68 | }, |
Nathaniel Nifong | 4fc750d | 2021-03-19 11:37:36 -0400 | [diff] [blame] | 69 | // drawShadow ignores alpha |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 70 | .shadow = |
| 71 | ShadowSettings{ |
Nathaniel Nifong | a6b5423 | 2021-07-02 13:24:32 -0400 | [diff] [blame] | 72 | .boundaries = rect, |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 73 | .ambientColor = vec4(0, 0, 0, 0.00935997f), |
| 74 | .spotColor = vec4(0, 0, 0, 0.0455841f), |
Nathaniel Nifong | a6b5423 | 2021-07-02 13:24:32 -0400 | [diff] [blame] | 75 | .lightPos = vec3(500.f, -1500.f, 1500.f), |
| 76 | .lightRadius = 2500.0f, |
| 77 | .length = 15.f, |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 78 | }, |
Nathaniel Nifong | 490a947 | 2021-06-23 16:44:19 -0400 | [diff] [blame] | 79 | // setting this is mandatory for shadows and blurs |
| 80 | .skipContentDraw = true, |
Nathaniel Nifong | a6b5423 | 2021-07-02 13:24:32 -0400 | [diff] [blame] | 81 | .alpha = 1, |
| 82 | }; |
| 83 | LayerSettings caster{ |
| 84 | .geometry = |
| 85 | Geometry{ |
| 86 | .boundaries = smallerRect, |
| 87 | .roundedCornersCrop = rect, |
Vishnu Nair | 50c0afe | 2022-07-11 15:04:07 -0700 | [diff] [blame] | 88 | .roundedCornersRadius = {50.f, 50.f}, |
Nathaniel Nifong | a6b5423 | 2021-07-02 13:24:32 -0400 | [diff] [blame] | 89 | }, |
| 90 | .source = |
| 91 | PixelSource{ |
| 92 | .solidColor = half3(0.f, 0.f, 0.f), |
| 93 | }, |
| 94 | .alpha = 1, |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 95 | }; |
| 96 | |
Nathaniel Nifong | 49a5958 | 2021-07-26 19:49:47 -0400 | [diff] [blame] | 97 | // Four combinations of settings are used (two transforms here, and drawShadowLayers is |
| 98 | // called with two different destination data spaces) They're all rounded rect. |
| 99 | // Three of these are cache misses that generate new shaders. |
| 100 | // The first combination generates a short and simple shadow shader. |
| 101 | // The second combination, flip transform, generates two shaders. The first appears to involve |
| 102 | // gaussian_fp. The second is a long and general purpose shadow shader with a device space |
| 103 | // transformation stage. |
| 104 | // The third combination is a cache hit, nothing new. |
| 105 | // The fourth combination, flip transform with a non-SRGB destination dataspace, is new. |
| 106 | // It is unique in that nearly everything is done in the vertex shader, and that vertex shader |
| 107 | // requires color correction. This is triggered differently from every other instance of color |
| 108 | // correction. All other instances are triggered when src and dst dataspaces differ, while |
| 109 | // this one is triggered by the destination being non-srgb. Apparently since the third |
| 110 | // combination is a cache hit, this color correction is only added when the vertex shader is |
| 111 | // doing something non-trivial. |
| 112 | for (auto transform : {mat4(), kFlip}) { |
| 113 | layer.geometry.positionTransform = transform; |
| 114 | caster.geometry.positionTransform = transform; |
Leon Scroggins III | ae07fe5 | 2022-04-26 15:23:55 -0400 | [diff] [blame] | 115 | |
| 116 | auto layers = std::vector<LayerSettings>{layer, caster}; |
Alec Mouri | f29700f | 2023-08-17 21:53:31 +0000 | [diff] [blame^] | 117 | renderengine->drawLayers(display, layers, dstTexture, base::unique_fd()); |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 118 | } |
| 119 | } |
| 120 | |
| 121 | static void drawImageLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display, |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 122 | const std::shared_ptr<ExternalTexture>& dstTexture, |
| 123 | const std::shared_ptr<ExternalTexture>& srcTexture) { |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 124 | const Rect& displayRect = display.physicalDisplay; |
| 125 | FloatRect rect(0, 0, displayRect.width(), displayRect.height()); |
| 126 | LayerSettings layer{ |
| 127 | .geometry = |
| 128 | Geometry{ |
Nathaniel Nifong | f06a45b | 2021-06-25 17:24:26 -0400 | [diff] [blame] | 129 | // The position transform doesn't matter when the reduced shader mode |
| 130 | // in in effect. A matrix transform stage is always included. |
| 131 | .positionTransform = mat4(), |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 132 | .boundaries = rect, |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 133 | .roundedCornersCrop = rect, |
| 134 | }, |
| 135 | .source = PixelSource{.buffer = |
| 136 | Buffer{ |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 137 | .buffer = srcTexture, |
John Reck | ac09e45 | 2021-04-07 16:35:37 -0400 | [diff] [blame] | 138 | .maxLuminanceNits = 1000.f, |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 139 | }}, |
| 140 | }; |
| 141 | |
Nathaniel Nifong | bf6f754 | 2021-04-27 12:05:16 -0400 | [diff] [blame] | 142 | for (auto dataspace : {kDestDataSpace, kOtherDataSpace}) { |
| 143 | layer.sourceDataspace = dataspace; |
Nathaniel Nifong | 1349150 | 2021-06-30 17:28:29 -0400 | [diff] [blame] | 144 | // Cache shaders for both rects and round rects. |
| 145 | // In reduced shader mode, all non-zero round rect radii get the same code path. |
Nathaniel Nifong | f06a45b | 2021-06-25 17:24:26 -0400 | [diff] [blame] | 146 | for (float roundedCornersRadius : {0.0f, 50.0f}) { |
| 147 | // roundedCornersCrop is always set, but the radius triggers the behavior |
Vishnu Nair | 50c0afe | 2022-07-11 15:04:07 -0700 | [diff] [blame] | 148 | layer.geometry.roundedCornersRadius = {roundedCornersRadius, roundedCornersRadius}; |
Nathaniel Nifong | f06a45b | 2021-06-25 17:24:26 -0400 | [diff] [blame] | 149 | for (bool isOpaque : {true, false}) { |
| 150 | layer.source.buffer.isOpaque = isOpaque; |
| 151 | for (auto alpha : {half(.2f), half(1.0f)}) { |
| 152 | layer.alpha = alpha; |
Leon Scroggins III | ae07fe5 | 2022-04-26 15:23:55 -0400 | [diff] [blame] | 153 | auto layers = std::vector<LayerSettings>{layer}; |
Alec Mouri | f29700f | 2023-08-17 21:53:31 +0000 | [diff] [blame^] | 154 | renderengine->drawLayers(display, layers, dstTexture, base::unique_fd()); |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | } |
| 159 | } |
| 160 | |
Nathaniel Nifong | 4fc750d | 2021-03-19 11:37:36 -0400 | [diff] [blame] | 161 | static void drawSolidLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display, |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 162 | const std::shared_ptr<ExternalTexture>& dstTexture) { |
Nathaniel Nifong | 4fc750d | 2021-03-19 11:37:36 -0400 | [diff] [blame] | 163 | const Rect& displayRect = display.physicalDisplay; |
| 164 | FloatRect rect(0, 0, displayRect.width(), displayRect.height()); |
| 165 | LayerSettings layer{ |
| 166 | .geometry = |
| 167 | Geometry{ |
| 168 | .boundaries = rect, |
| 169 | }, |
Nathaniel Nifong | 4fc750d | 2021-03-19 11:37:36 -0400 | [diff] [blame] | 170 | .source = |
| 171 | PixelSource{ |
| 172 | .solidColor = half3(0.1f, 0.2f, 0.3f), |
| 173 | }, |
Nathaniel Nifong | 768693f | 2021-06-08 14:33:47 -0400 | [diff] [blame] | 174 | .alpha = 0.5, |
Nathaniel Nifong | 4fc750d | 2021-03-19 11:37:36 -0400 | [diff] [blame] | 175 | }; |
| 176 | |
Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 177 | for (auto transform : {mat4(), kScaleAndTranslate}) { |
| 178 | layer.geometry.positionTransform = transform; |
Nathaniel Nifong | f06a45b | 2021-06-25 17:24:26 -0400 | [diff] [blame] | 179 | for (float roundedCornersRadius : {0.0f, 50.f}) { |
Vishnu Nair | 50c0afe | 2022-07-11 15:04:07 -0700 | [diff] [blame] | 180 | layer.geometry.roundedCornersRadius = {roundedCornersRadius, roundedCornersRadius}; |
Leon Scroggins III | ae07fe5 | 2022-04-26 15:23:55 -0400 | [diff] [blame] | 181 | auto layers = std::vector<LayerSettings>{layer}; |
Alec Mouri | f29700f | 2023-08-17 21:53:31 +0000 | [diff] [blame^] | 182 | renderengine->drawLayers(display, layers, dstTexture, base::unique_fd()); |
Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 183 | } |
| 184 | } |
Nathaniel Nifong | 4fc750d | 2021-03-19 11:37:36 -0400 | [diff] [blame] | 185 | } |
| 186 | |
Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 187 | static void drawBlurLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display, |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 188 | const std::shared_ptr<ExternalTexture>& dstTexture) { |
Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 189 | const Rect& displayRect = display.physicalDisplay; |
| 190 | FloatRect rect(0, 0, displayRect.width(), displayRect.height()); |
| 191 | LayerSettings layer{ |
| 192 | .geometry = |
| 193 | Geometry{ |
| 194 | .boundaries = rect, |
| 195 | }, |
| 196 | .alpha = 1, |
Nathaniel Nifong | 490a947 | 2021-06-23 16:44:19 -0400 | [diff] [blame] | 197 | // setting this is mandatory for shadows and blurs |
| 198 | .skipContentDraw = true, |
Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 199 | }; |
| 200 | |
Nathaniel Nifong | 490a947 | 2021-06-23 16:44:19 -0400 | [diff] [blame] | 201 | // Different blur code is invoked for radii less and greater than 30 pixels |
Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 202 | for (int radius : {9, 60}) { |
| 203 | layer.backgroundBlurRadius = radius; |
Leon Scroggins III | ae07fe5 | 2022-04-26 15:23:55 -0400 | [diff] [blame] | 204 | auto layers = std::vector<LayerSettings>{layer}; |
Alec Mouri | f29700f | 2023-08-17 21:53:31 +0000 | [diff] [blame^] | 205 | renderengine->drawLayers(display, layers, dstTexture, base::unique_fd()); |
Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 206 | } |
| 207 | } |
| 208 | |
Nathaniel Nifong | 2d91c5e | 2021-05-13 17:14:00 -0400 | [diff] [blame] | 209 | // The unique feature of these layers is that the boundary is slightly smaller than the rounded |
| 210 | // rect crop, so the rounded edges intersect that boundary and require a different clipping method. |
Nathaniel Nifong | afeac5b | 2021-05-27 10:52:30 -0400 | [diff] [blame] | 211 | // For buffers, this is done with a stage that computes coverage and it will differ for round and |
| 212 | // elliptical corners. |
Nathaniel Nifong | 2d91c5e | 2021-05-13 17:14:00 -0400 | [diff] [blame] | 213 | static void drawClippedLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display, |
| 214 | const std::shared_ptr<ExternalTexture>& dstTexture, |
| 215 | const std::shared_ptr<ExternalTexture>& srcTexture) { |
Nathaniel Nifong | 21e021f | 2021-04-21 13:15:46 -0400 | [diff] [blame] | 216 | const Rect& displayRect = display.physicalDisplay; |
Nathaniel Nifong | 2d91c5e | 2021-05-13 17:14:00 -0400 | [diff] [blame] | 217 | FloatRect rect(0, 0, displayRect.width(), displayRect.height() - 20); // boundary is smaller |
| 218 | |
Nathaniel Nifong | afeac5b | 2021-05-27 10:52:30 -0400 | [diff] [blame] | 219 | PixelSource bufferSource{.buffer = Buffer{ |
| 220 | .buffer = srcTexture, |
| 221 | .isOpaque = 0, |
| 222 | .maxLuminanceNits = 1000.f, |
| 223 | }}; |
| 224 | PixelSource bufferOpaque{.buffer = Buffer{ |
| 225 | .buffer = srcTexture, |
| 226 | .isOpaque = 1, |
| 227 | .maxLuminanceNits = 1000.f, |
| 228 | }}; |
| 229 | PixelSource colorSource{.solidColor = half3(0.1f, 0.2f, 0.3f)}; |
Nathaniel Nifong | 2d91c5e | 2021-05-13 17:14:00 -0400 | [diff] [blame] | 230 | |
Nathaniel Nifong | 21e021f | 2021-04-21 13:15:46 -0400 | [diff] [blame] | 231 | LayerSettings layer{ |
| 232 | .geometry = |
| 233 | Geometry{ |
| 234 | .boundaries = rect, |
Vishnu Nair | 50c0afe | 2022-07-11 15:04:07 -0700 | [diff] [blame] | 235 | .roundedCornersRadius = {27.f, 27.f}, |
Nathaniel Nifong | 2d91c5e | 2021-05-13 17:14:00 -0400 | [diff] [blame] | 236 | .roundedCornersCrop = |
| 237 | FloatRect(0, 0, displayRect.width(), displayRect.height()), |
Nathaniel Nifong | 21e021f | 2021-04-21 13:15:46 -0400 | [diff] [blame] | 238 | }, |
Nathaniel Nifong | 21e021f | 2021-04-21 13:15:46 -0400 | [diff] [blame] | 239 | }; |
| 240 | |
Nathaniel Nifong | afeac5b | 2021-05-27 10:52:30 -0400 | [diff] [blame] | 241 | for (auto pixelSource : {bufferSource, bufferOpaque, colorSource}) { |
| 242 | layer.source = pixelSource; |
| 243 | for (auto dataspace : {kDestDataSpace, kOtherDataSpace}) { |
| 244 | layer.sourceDataspace = dataspace; |
Nathaniel Nifong | 1349150 | 2021-06-30 17:28:29 -0400 | [diff] [blame] | 245 | // Produce a CircularRRect clip and an EllipticalRRect clip. |
Nathaniel Nifong | afeac5b | 2021-05-27 10:52:30 -0400 | [diff] [blame] | 246 | for (auto transform : {kScaleAndTranslate, kScaleAsymmetric}) { |
| 247 | layer.geometry.positionTransform = transform; |
Nathaniel Nifong | afeac5b | 2021-05-27 10:52:30 -0400 | [diff] [blame] | 248 | for (float alpha : {0.5f, 1.f}) { |
Leon Scroggins III | ae07fe5 | 2022-04-26 15:23:55 -0400 | [diff] [blame] | 249 | layer.alpha = alpha; |
| 250 | auto layers = std::vector<LayerSettings>{layer}; |
Alec Mouri | f29700f | 2023-08-17 21:53:31 +0000 | [diff] [blame^] | 251 | renderengine->drawLayers(display, layers, dstTexture, base::unique_fd()); |
Nathaniel Nifong | afeac5b | 2021-05-27 10:52:30 -0400 | [diff] [blame] | 252 | } |
| 253 | } |
Nathaniel Nifong | 2d91c5e | 2021-05-13 17:14:00 -0400 | [diff] [blame] | 254 | } |
Nathaniel Nifong | 21e021f | 2021-04-21 13:15:46 -0400 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
Nathaniel Nifong | 1349150 | 2021-06-30 17:28:29 -0400 | [diff] [blame] | 258 | static void drawPIPImageLayer(SkiaRenderEngine* renderengine, const DisplaySettings& display, |
| 259 | const std::shared_ptr<ExternalTexture>& dstTexture, |
| 260 | const std::shared_ptr<ExternalTexture>& srcTexture) { |
| 261 | const Rect& displayRect = display.physicalDisplay; |
| 262 | FloatRect rect(0, 0, displayRect.width(), displayRect.height()); |
| 263 | LayerSettings layer{ |
| 264 | .geometry = |
| 265 | Geometry{ |
| 266 | // Note that this flip matrix only makes a difference when clipping, |
| 267 | // which happens in this layer because the roundrect crop is just a bit |
| 268 | // larger than the layer bounds. |
| 269 | .positionTransform = kFlip, |
| 270 | .boundaries = rect, |
Vishnu Nair | 50c0afe | 2022-07-11 15:04:07 -0700 | [diff] [blame] | 271 | .roundedCornersRadius = {94.2551f, 94.2551f}, |
| 272 | .roundedCornersCrop = FloatRect(-93.75, 0, displayRect.width() + 93.75, |
| 273 | displayRect.height()), |
Nathaniel Nifong | 1349150 | 2021-06-30 17:28:29 -0400 | [diff] [blame] | 274 | }, |
| 275 | .source = PixelSource{.buffer = |
| 276 | Buffer{ |
| 277 | .buffer = srcTexture, |
| 278 | .maxLuminanceNits = 1000.f, |
| 279 | .isOpaque = 0, |
| 280 | .usePremultipliedAlpha = 1, |
| 281 | }}, |
| 282 | .sourceDataspace = kOtherDataSpace, |
| 283 | .alpha = 1, |
| 284 | |
| 285 | }; |
| 286 | |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 287 | auto layers = std::vector<LayerSettings>{layer}; |
Alec Mouri | f29700f | 2023-08-17 21:53:31 +0000 | [diff] [blame^] | 288 | renderengine->drawLayers(display, layers, dstTexture, base::unique_fd()); |
Nathaniel Nifong | 1349150 | 2021-06-30 17:28:29 -0400 | [diff] [blame] | 289 | } |
| 290 | |
| 291 | static void drawHolePunchLayer(SkiaRenderEngine* renderengine, const DisplaySettings& display, |
| 292 | const std::shared_ptr<ExternalTexture>& dstTexture) { |
| 293 | const Rect& displayRect = display.physicalDisplay; |
| 294 | FloatRect rect(0, 0, displayRect.width(), displayRect.height()); |
| 295 | FloatRect small(0, 0, displayRect.width()-20, displayRect.height()+20); |
| 296 | LayerSettings layer{ |
| 297 | .geometry = |
| 298 | Geometry{ |
| 299 | .positionTransform = kScaleAndTranslate, |
| 300 | // the boundaries have to be smaller than the rounded crop so that |
| 301 | // clipRRect is used instead of drawRRect |
| 302 | .boundaries = small, |
Vishnu Nair | 50c0afe | 2022-07-11 15:04:07 -0700 | [diff] [blame] | 303 | .roundedCornersRadius = {50.f, 50.f}, |
Nathaniel Nifong | 1349150 | 2021-06-30 17:28:29 -0400 | [diff] [blame] | 304 | .roundedCornersCrop = rect, |
| 305 | }, |
Vishnu Nair | 50c0afe | 2022-07-11 15:04:07 -0700 | [diff] [blame] | 306 | .source = |
| 307 | PixelSource{ |
Nathaniel Nifong | 1349150 | 2021-06-30 17:28:29 -0400 | [diff] [blame] | 308 | .solidColor = half3(0.f, 0.f, 0.f), |
| 309 | }, |
| 310 | .sourceDataspace = kDestDataSpace, |
| 311 | .alpha = 0, |
| 312 | .disableBlending = true, |
| 313 | |
| 314 | }; |
| 315 | |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 316 | auto layers = std::vector<LayerSettings>{layer}; |
Alec Mouri | f29700f | 2023-08-17 21:53:31 +0000 | [diff] [blame^] | 317 | renderengine->drawLayers(display, layers, dstTexture, base::unique_fd()); |
Nathaniel Nifong | 1349150 | 2021-06-30 17:28:29 -0400 | [diff] [blame] | 318 | } |
| 319 | |
Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 320 | // |
| 321 | // The collection of shaders cached here were found by using perfetto to record shader compiles |
| 322 | // during actions that involve RenderEngine, logging the layer settings, and the shader code |
| 323 | // and reproducing those settings here. |
| 324 | // |
| 325 | // It is helpful when debugging this to turn on |
| 326 | // in SkGLRenderEngine.cpp: |
| 327 | // kPrintLayerSettings = true |
| 328 | // kFlushAfterEveryLayer = true |
| 329 | // in external/skia/src/gpu/gl/builders/GrGLShaderStringBuilder.cpp |
| 330 | // gPrintSKSL = true |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 331 | void Cache::primeShaderCache(SkiaRenderEngine* renderengine) { |
Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 332 | const int previousCount = renderengine->reportShadersCompiled(); |
| 333 | if (previousCount) { |
| 334 | ALOGD("%d Shaders already compiled before Cache::primeShaderCache ran\n", previousCount); |
| 335 | } |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 336 | |
Derek Sollenberger | e9a5108 | 2021-05-06 14:01:38 -0400 | [diff] [blame] | 337 | // The loop is beneficial for debugging and should otherwise be optimized out by the compiler. |
| 338 | // Adding additional bounds to the loop is useful for verifying that the size of the dst buffer |
| 339 | // does not impact the shader compilation counts by triggering different behaviors in RE/Skia. |
| 340 | for (SkSize bounds : {SkSize::Make(128, 128), /*SkSize::Make(1080, 2340)*/}) { |
| 341 | const nsecs_t timeBefore = systemTime(); |
| 342 | // The dimensions should not matter, so long as we draw inside them. |
| 343 | const Rect displayRect(0, 0, bounds.fWidth, bounds.fHeight); |
| 344 | DisplaySettings display{ |
| 345 | .physicalDisplay = displayRect, |
| 346 | .clip = displayRect, |
| 347 | .maxLuminance = 500, |
| 348 | .outputDataspace = kDestDataSpace, |
| 349 | }; |
Nathaniel Nifong | a6b5423 | 2021-07-02 13:24:32 -0400 | [diff] [blame] | 350 | DisplaySettings p3Display{ |
| 351 | .physicalDisplay = displayRect, |
| 352 | .clip = displayRect, |
| 353 | .maxLuminance = 500, |
| 354 | .outputDataspace = kOtherDataSpace, |
| 355 | }; |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 356 | |
Derek Sollenberger | e9a5108 | 2021-05-06 14:01:38 -0400 | [diff] [blame] | 357 | const int64_t usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE; |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 358 | |
Derek Sollenberger | e9a5108 | 2021-05-06 14:01:38 -0400 | [diff] [blame] | 359 | sp<GraphicBuffer> dstBuffer = |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 360 | sp<GraphicBuffer>::make(displayRect.width(), displayRect.height(), |
| 361 | PIXEL_FORMAT_RGBA_8888, 1, usage, "primeShaderCache_dst"); |
Nathaniel Nifong | b9f27ef | 2021-04-01 16:44:12 -0400 | [diff] [blame] | 362 | |
Derek Sollenberger | e9a5108 | 2021-05-06 14:01:38 -0400 | [diff] [blame] | 363 | const auto dstTexture = |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 364 | std::make_shared<impl::ExternalTexture>(dstBuffer, *renderengine, |
| 365 | impl::ExternalTexture::Usage::WRITEABLE); |
Derek Sollenberger | e9a5108 | 2021-05-06 14:01:38 -0400 | [diff] [blame] | 366 | // This buffer will be the source for the call to drawImageLayers. Draw |
| 367 | // something to it as a placeholder for what an app draws. We should draw |
| 368 | // something, but the details are not important. Make use of the shadow layer drawing step |
| 369 | // to populate it. |
| 370 | sp<GraphicBuffer> srcBuffer = |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 371 | sp<GraphicBuffer>::make(displayRect.width(), displayRect.height(), |
| 372 | PIXEL_FORMAT_RGBA_8888, 1, usage, "drawImageLayer_src"); |
Alec Mouri | a90a570 | 2021-04-16 16:36:21 +0000 | [diff] [blame] | 373 | |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 374 | const auto srcTexture = std::make_shared< |
| 375 | impl::ExternalTexture>(srcBuffer, *renderengine, |
| 376 | impl::ExternalTexture::Usage::READABLE | |
| 377 | impl::ExternalTexture::Usage::WRITEABLE); |
Nathaniel Nifong | 1349150 | 2021-06-30 17:28:29 -0400 | [diff] [blame] | 378 | drawHolePunchLayer(renderengine, display, dstTexture); |
Derek Sollenberger | e9a5108 | 2021-05-06 14:01:38 -0400 | [diff] [blame] | 379 | drawSolidLayers(renderengine, display, dstTexture); |
Nathaniel Nifong | 49a5958 | 2021-07-26 19:49:47 -0400 | [diff] [blame] | 380 | |
Derek Sollenberger | e9a5108 | 2021-05-06 14:01:38 -0400 | [diff] [blame] | 381 | drawShadowLayers(renderengine, display, srcTexture); |
Nathaniel Nifong | a6b5423 | 2021-07-02 13:24:32 -0400 | [diff] [blame] | 382 | drawShadowLayers(renderengine, p3Display, srcTexture); |
Nathaniel Nifong | cda45e9 | 2021-06-10 15:01:42 -0400 | [diff] [blame] | 383 | |
| 384 | if (renderengine->supportsBackgroundBlur()) { |
| 385 | drawBlurLayers(renderengine, display, dstTexture); |
| 386 | } |
| 387 | |
Nathaniel Nifong | 73537a3 | 2021-08-06 15:07:26 -0400 | [diff] [blame] | 388 | // The majority of skia shaders needed by RenderEngine are related to sampling images. |
| 389 | // These need to be generated with various source textures. |
| 390 | // Make a list of applicable sources. |
| 391 | // GRALLOC_USAGE_HW_TEXTURE should be the same as AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE. |
Derek Sollenberger | e9a5108 | 2021-05-06 14:01:38 -0400 | [diff] [blame] | 392 | const int64_t usageExternal = GRALLOC_USAGE_HW_TEXTURE; |
Derek Sollenberger | e9a5108 | 2021-05-06 14:01:38 -0400 | [diff] [blame] | 393 | sp<GraphicBuffer> externalBuffer = |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 394 | sp<GraphicBuffer>::make(displayRect.width(), displayRect.height(), |
| 395 | PIXEL_FORMAT_RGBA_8888, 1, usageExternal, |
| 396 | "primeShaderCache_external"); |
Derek Sollenberger | e9a5108 | 2021-05-06 14:01:38 -0400 | [diff] [blame] | 397 | const auto externalTexture = |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 398 | std::make_shared<impl::ExternalTexture>(externalBuffer, *renderengine, |
| 399 | impl::ExternalTexture::Usage::READABLE); |
Nathaniel Nifong | 73537a3 | 2021-08-06 15:07:26 -0400 | [diff] [blame] | 400 | std::vector<const std::shared_ptr<ExternalTexture>> textures = |
| 401 | {srcTexture, externalTexture}; |
Nathaniel Nifong | 21e021f | 2021-04-21 13:15:46 -0400 | [diff] [blame] | 402 | |
Nathaniel Nifong | 73537a3 | 2021-08-06 15:07:26 -0400 | [diff] [blame] | 403 | // Another external texture with a different pixel format triggers useIsOpaqueWorkaround. |
| 404 | // It doesn't have to be f16, but it can't be the usual 8888. |
Nathaniel Nifong | f06a45b | 2021-06-25 17:24:26 -0400 | [diff] [blame] | 405 | sp<GraphicBuffer> f16ExternalBuffer = |
Ady Abraham | d11bade | 2022-08-01 16:18:03 -0700 | [diff] [blame] | 406 | sp<GraphicBuffer>::make(displayRect.width(), displayRect.height(), |
| 407 | PIXEL_FORMAT_RGBA_FP16, 1, usageExternal, |
| 408 | "primeShaderCache_external_f16"); |
Nathaniel Nifong | 73537a3 | 2021-08-06 15:07:26 -0400 | [diff] [blame] | 409 | // The F16 texture may not be usable on all devices, so check first that it was created. |
| 410 | status_t error = f16ExternalBuffer->initCheck(); |
| 411 | if (!error) { |
| 412 | const auto f16ExternalTexture = |
Vishnu Nair | dbbe385 | 2022-01-12 20:22:11 -0800 | [diff] [blame] | 413 | std::make_shared<impl::ExternalTexture>(f16ExternalBuffer, *renderengine, |
| 414 | impl::ExternalTexture::Usage::READABLE); |
Nathaniel Nifong | 73537a3 | 2021-08-06 15:07:26 -0400 | [diff] [blame] | 415 | textures.push_back(f16ExternalTexture); |
| 416 | } |
Nathaniel Nifong | f06a45b | 2021-06-25 17:24:26 -0400 | [diff] [blame] | 417 | |
Nathaniel Nifong | 73537a3 | 2021-08-06 15:07:26 -0400 | [diff] [blame] | 418 | for (auto texture : textures) { |
Nathaniel Nifong | f06a45b | 2021-06-25 17:24:26 -0400 | [diff] [blame] | 419 | drawImageLayers(renderengine, display, dstTexture, texture); |
| 420 | // Draw layers for b/185569240. |
| 421 | drawClippedLayers(renderengine, display, dstTexture, texture); |
| 422 | } |
Derek Sollenberger | e9a5108 | 2021-05-06 14:01:38 -0400 | [diff] [blame] | 423 | |
Nathaniel Nifong | 1349150 | 2021-06-30 17:28:29 -0400 | [diff] [blame] | 424 | drawPIPImageLayer(renderengine, display, dstTexture, externalTexture); |
| 425 | |
Nathaniel Nifong | 2d2f432 | 2021-07-22 15:17:36 -0400 | [diff] [blame] | 426 | // draw one final layer synchronously to force GL submit |
| 427 | LayerSettings layer{ |
| 428 | .source = PixelSource{.solidColor = half3(0.f, 0.f, 0.f)}, |
| 429 | }; |
Sally Qi | 59a9f50 | 2021-10-12 18:53:23 +0000 | [diff] [blame] | 430 | auto layers = std::vector<LayerSettings>{layer}; |
Sally Qi | 4cabdd0 | 2021-08-05 16:45:57 -0700 | [diff] [blame] | 431 | // call get() to make it synchronous |
Alec Mouri | f29700f | 2023-08-17 21:53:31 +0000 | [diff] [blame^] | 432 | renderengine->drawLayers(display, layers, dstTexture, base::unique_fd()).get(); |
Nathaniel Nifong | 2d2f432 | 2021-07-22 15:17:36 -0400 | [diff] [blame] | 433 | |
Derek Sollenberger | e9a5108 | 2021-05-06 14:01:38 -0400 | [diff] [blame] | 434 | const nsecs_t timeAfter = systemTime(); |
| 435 | const float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6; |
Leon Scroggins III | 45be918 | 2022-04-27 10:37:11 -0400 | [diff] [blame] | 436 | const int shadersCompiled = renderengine->reportShadersCompiled() - previousCount; |
Derek Sollenberger | e9a5108 | 2021-05-06 14:01:38 -0400 | [diff] [blame] | 437 | ALOGD("Shader cache generated %d shaders in %f ms\n", shadersCompiled, compileTimeMs); |
| 438 | } |
Leon Scroggins III | b9216dc | 2021-03-08 17:19:01 -0500 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | } // namespace android::renderengine::skia |