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