blob: d9d249669d45d0ea0795335b3f7fa1fc5c56d20b [file] [log] [blame]
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -05001/*
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 IIIb9216dc2021-03-08 17:19:01 -050016#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
28namespace android::renderengine::skia {
29
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040030namespace {
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -040031// Warming shader cache, not framebuffer cache.
32constexpr bool kUseFrameBufferCache = false;
33
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040034// clang-format off
35// Any non-identity matrix will do.
36const 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 Nifongafeac5b2021-05-27 10:52:30 -040040const 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 Nifongb9f27ef2021-04-01 16:44:12 -040044// clang-format on
Nathaniel Nifongbf6f7542021-04-27 12:05:16 -040045// When setting layer.sourceDataspace, whether it matches the destination or not determines whether
46// a color correction effect is added to the shader.
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040047constexpr auto kDestDataSpace = ui::Dataspace::SRGB;
Nathaniel Nifong21e021f2021-04-21 13:15:46 -040048constexpr auto kOtherDataSpace = ui::Dataspace::DISPLAY_P3;
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040049} // namespace
50
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -040051static void drawShadowLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
Alec Mouria90a5702021-04-16 16:36:21 +000052 const std::shared_ptr<ExternalTexture>& dstTexture) {
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050053 // Somewhat arbitrary dimensions, but on screen and slightly shorter, based
54 // on actual use.
55 FloatRect rect(0, 0, display.physicalDisplay.width(), display.physicalDisplay.height() - 30);
56 LayerSettings layer{
57 .geometry =
58 Geometry{
59 .boundaries = rect,
60 .roundedCornersCrop = rect,
61 },
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -040062 // drawShadow ignores alpha
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050063 .shadow =
64 ShadowSettings{
65 .ambientColor = vec4(0, 0, 0, 0.00935997f),
66 .spotColor = vec4(0, 0, 0, 0.0455841f),
67 .lightPos = vec3(370.508f, -1527.03f, 1650.f),
68 .lightRadius = 2200.0f,
69 .length = 0.955342f,
70 },
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040071 // important that this matches dest so the general shadow fragment shader doesn't
72 // have color correction added, and important that it be srgb, so the *vertex* shader
73 // doesn't have color correction added.
74 .sourceDataspace = kDestDataSpace,
Nathaniel Nifong490a9472021-06-23 16:44:19 -040075 // setting this is mandatory for shadows and blurs
76 .skipContentDraw = true,
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050077 };
78
79 auto layers = std::vector<const LayerSettings*>{&layer};
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040080 // The identity matrix will generate the fast shader
Alec Mouria90a5702021-04-16 16:36:21 +000081 renderengine->drawLayers(display, layers, dstTexture, kUseFrameBufferCache, base::unique_fd(),
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040082 nullptr);
83 // This matrix, which has different scales for x and y, will
84 // generate the slower (more general case) version, which has variants for translucent
85 // casters and rounded rects.
Nathaniel Nifongafeac5b2021-05-27 10:52:30 -040086 layer.geometry.positionTransform = kScaleAsymmetric;
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040087 for (auto translucent : {false, true}) {
88 layer.shadow.casterIsTranslucent = translucent;
Alec Mouria90a5702021-04-16 16:36:21 +000089 renderengine->drawLayers(display, layers, dstTexture, kUseFrameBufferCache,
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050090 base::unique_fd(), nullptr);
91 }
92}
93
94static void drawImageLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
Alec Mouria90a5702021-04-16 16:36:21 +000095 const std::shared_ptr<ExternalTexture>& dstTexture,
96 const std::shared_ptr<ExternalTexture>& srcTexture) {
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050097 const Rect& displayRect = display.physicalDisplay;
98 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
99 LayerSettings layer{
100 .geometry =
101 Geometry{
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400102 // The position transform doesn't matter when the reduced shader mode
103 // in in effect. A matrix transform stage is always included.
104 .positionTransform = mat4(),
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500105 .boundaries = rect,
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500106 .roundedCornersCrop = rect,
107 },
108 .source = PixelSource{.buffer =
109 Buffer{
Alec Mouria90a5702021-04-16 16:36:21 +0000110 .buffer = srcTexture,
John Reckac09e452021-04-07 16:35:37 -0400111 .maxLuminanceNits = 1000.f,
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500112 }},
113 };
114
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500115 auto layers = std::vector<const LayerSettings*>{&layer};
Nathaniel Nifongbf6f7542021-04-27 12:05:16 -0400116 for (auto dataspace : {kDestDataSpace, kOtherDataSpace}) {
117 layer.sourceDataspace = dataspace;
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400118 for (float roundedCornersRadius : {0.0f, 50.0f}) {
119 // roundedCornersCrop is always set, but the radius triggers the behavior
120 layer.geometry.roundedCornersRadius = roundedCornersRadius;
121 for (bool isOpaque : {true, false}) {
122 layer.source.buffer.isOpaque = isOpaque;
123 for (auto alpha : {half(.2f), half(1.0f)}) {
124 layer.alpha = alpha;
125 renderengine->drawLayers(display, layers, dstTexture, kUseFrameBufferCache,
126 base::unique_fd(), nullptr);
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500127 }
128 }
129 }
130 }
131}
132
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400133static void drawSolidLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
Alec Mouria90a5702021-04-16 16:36:21 +0000134 const std::shared_ptr<ExternalTexture>& dstTexture) {
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400135 const Rect& displayRect = display.physicalDisplay;
136 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
137 LayerSettings layer{
138 .geometry =
139 Geometry{
140 .boundaries = rect,
141 },
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400142 .source =
143 PixelSource{
144 .solidColor = half3(0.1f, 0.2f, 0.3f),
145 },
Nathaniel Nifong768693f2021-06-08 14:33:47 -0400146 .alpha = 0.5,
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400147 };
148
149 auto layers = std::vector<const LayerSettings*>{&layer};
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400150 for (auto transform : {mat4(), kScaleAndTranslate}) {
151 layer.geometry.positionTransform = transform;
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400152 for (float roundedCornersRadius : {0.0f, 50.f}) {
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400153 layer.geometry.roundedCornersRadius = roundedCornersRadius;
Alec Mouria90a5702021-04-16 16:36:21 +0000154 renderengine->drawLayers(display, layers, dstTexture, kUseFrameBufferCache,
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400155 base::unique_fd(), nullptr);
156 }
157 }
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400158}
159
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400160static void drawBlurLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
Alec Mouria90a5702021-04-16 16:36:21 +0000161 const std::shared_ptr<ExternalTexture>& dstTexture) {
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400162 const Rect& displayRect = display.physicalDisplay;
163 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
164 LayerSettings layer{
165 .geometry =
166 Geometry{
167 .boundaries = rect,
168 },
169 .alpha = 1,
Nathaniel Nifong490a9472021-06-23 16:44:19 -0400170 // setting this is mandatory for shadows and blurs
171 .skipContentDraw = true,
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400172 };
173
174 auto layers = std::vector<const LayerSettings*>{&layer};
Nathaniel Nifong490a9472021-06-23 16:44:19 -0400175 // Different blur code is invoked for radii less and greater than 30 pixels
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400176 for (int radius : {9, 60}) {
177 layer.backgroundBlurRadius = radius;
Alec Mouria90a5702021-04-16 16:36:21 +0000178 renderengine->drawLayers(display, layers, dstTexture, kUseFrameBufferCache,
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400179 base::unique_fd(), nullptr);
180 }
181}
182
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400183// The unique feature of these layers is that the boundary is slightly smaller than the rounded
184// rect crop, so the rounded edges intersect that boundary and require a different clipping method.
Nathaniel Nifongafeac5b2021-05-27 10:52:30 -0400185// For buffers, this is done with a stage that computes coverage and it will differ for round and
186// elliptical corners.
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400187static void drawClippedLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
188 const std::shared_ptr<ExternalTexture>& dstTexture,
189 const std::shared_ptr<ExternalTexture>& srcTexture) {
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400190 const Rect& displayRect = display.physicalDisplay;
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400191 FloatRect rect(0, 0, displayRect.width(), displayRect.height() - 20); // boundary is smaller
192
Nathaniel Nifongafeac5b2021-05-27 10:52:30 -0400193 PixelSource bufferSource{.buffer = Buffer{
194 .buffer = srcTexture,
195 .isOpaque = 0,
196 .maxLuminanceNits = 1000.f,
197 }};
198 PixelSource bufferOpaque{.buffer = Buffer{
199 .buffer = srcTexture,
200 .isOpaque = 1,
201 .maxLuminanceNits = 1000.f,
202 }};
203 PixelSource colorSource{.solidColor = half3(0.1f, 0.2f, 0.3f)};
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400204
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400205 LayerSettings layer{
206 .geometry =
207 Geometry{
208 .boundaries = rect,
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400209 .roundedCornersRadius = 27, // larger than the 20 above.
210 .roundedCornersCrop =
211 FloatRect(0, 0, displayRect.width(), displayRect.height()),
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400212 },
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400213 };
214
215 auto layers = std::vector<const LayerSettings*>{&layer};
Nathaniel Nifongafeac5b2021-05-27 10:52:30 -0400216 for (auto pixelSource : {bufferSource, bufferOpaque, colorSource}) {
217 layer.source = pixelSource;
218 for (auto dataspace : {kDestDataSpace, kOtherDataSpace}) {
219 layer.sourceDataspace = dataspace;
220 // Produce a CircularRRect clip and an EllipticalRRect clip
221 for (auto transform : {kScaleAndTranslate, kScaleAsymmetric}) {
222 layer.geometry.positionTransform = transform;
223 // In real use, I saw alpha of 1.0 and 0.999, probably a mistake, but cache both
224 // shaders.
225 for (float alpha : {0.5f, 1.f}) {
226 layer.alpha = alpha,
227 renderengine->drawLayers(display, layers, dstTexture, kUseFrameBufferCache,
228 base::unique_fd(), nullptr);
229 }
230 }
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400231 }
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400232 }
233}
234
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400235//
236// The collection of shaders cached here were found by using perfetto to record shader compiles
237// during actions that involve RenderEngine, logging the layer settings, and the shader code
238// and reproducing those settings here.
239//
240// It is helpful when debugging this to turn on
241// in SkGLRenderEngine.cpp:
242// kPrintLayerSettings = true
243// kFlushAfterEveryLayer = true
244// in external/skia/src/gpu/gl/builders/GrGLShaderStringBuilder.cpp
245// gPrintSKSL = true
246//
247// TODO(b/184631553) cache the shader involved in youtube pip return.
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500248void Cache::primeShaderCache(SkiaRenderEngine* renderengine) {
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400249 const int previousCount = renderengine->reportShadersCompiled();
250 if (previousCount) {
251 ALOGD("%d Shaders already compiled before Cache::primeShaderCache ran\n", previousCount);
252 }
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500253
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400254 // The loop is beneficial for debugging and should otherwise be optimized out by the compiler.
255 // Adding additional bounds to the loop is useful for verifying that the size of the dst buffer
256 // does not impact the shader compilation counts by triggering different behaviors in RE/Skia.
257 for (SkSize bounds : {SkSize::Make(128, 128), /*SkSize::Make(1080, 2340)*/}) {
258 const nsecs_t timeBefore = systemTime();
259 // The dimensions should not matter, so long as we draw inside them.
260 const Rect displayRect(0, 0, bounds.fWidth, bounds.fHeight);
261 DisplaySettings display{
262 .physicalDisplay = displayRect,
263 .clip = displayRect,
264 .maxLuminance = 500,
265 .outputDataspace = kDestDataSpace,
266 };
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500267
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400268 const int64_t usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Alec Mouria90a5702021-04-16 16:36:21 +0000269
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400270 sp<GraphicBuffer> dstBuffer =
271 new GraphicBuffer(displayRect.width(), displayRect.height(), PIXEL_FORMAT_RGBA_8888,
272 1, usage, "primeShaderCache_dst");
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400273
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400274 const auto dstTexture =
275 std::make_shared<ExternalTexture>(dstBuffer, *renderengine,
276 ExternalTexture::Usage::WRITEABLE);
277 // This buffer will be the source for the call to drawImageLayers. Draw
278 // something to it as a placeholder for what an app draws. We should draw
279 // something, but the details are not important. Make use of the shadow layer drawing step
280 // to populate it.
281 sp<GraphicBuffer> srcBuffer =
282 new GraphicBuffer(displayRect.width(), displayRect.height(), PIXEL_FORMAT_RGBA_8888,
283 1, usage, "drawImageLayer_src");
Alec Mouria90a5702021-04-16 16:36:21 +0000284
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400285 const auto srcTexture =
286 std::make_shared<ExternalTexture>(srcBuffer, *renderengine,
287 ExternalTexture::Usage::READABLE |
288 ExternalTexture::Usage::WRITEABLE);
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400289
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400290 drawSolidLayers(renderengine, display, dstTexture);
291 drawShadowLayers(renderengine, display, srcTexture);
Nathaniel Nifongcda45e92021-06-10 15:01:42 -0400292
293 if (renderengine->supportsBackgroundBlur()) {
294 drawBlurLayers(renderengine, display, dstTexture);
295 }
296
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400297 // should be the same as AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
298 const int64_t usageExternal = GRALLOC_USAGE_HW_TEXTURE;
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400299 sp<GraphicBuffer> externalBuffer =
300 new GraphicBuffer(displayRect.width(), displayRect.height(), PIXEL_FORMAT_RGBA_8888,
301 1, usageExternal, "primeShaderCache_external");
302 const auto externalTexture =
303 std::make_shared<ExternalTexture>(externalBuffer, *renderengine,
304 ExternalTexture::Usage::READABLE);
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400305
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400306 // Another external texture with a different pixel format triggers useIsOpaqueWorkaround
307 sp<GraphicBuffer> f16ExternalBuffer =
308 new GraphicBuffer(displayRect.width(), displayRect.height(), PIXEL_FORMAT_RGBA_FP16,
309 1, usageExternal, "primeShaderCache_external_f16");
310 const auto f16ExternalTexture =
311 std::make_shared<ExternalTexture>(f16ExternalBuffer, *renderengine,
312 ExternalTexture::Usage::READABLE);
313
314 // The majority of shaders are related to sampling images.
315 // These need to be generated with various source textures
Nathaniel Nifong628feb72021-07-01 14:11:05 -0400316 // The F16 texture may not be usable on all devices, so check first that it was created with
317 // the requested usage bit.
318 auto textures = {srcTexture, externalTexture};
319 auto texturesWithF16 = {srcTexture, externalTexture, f16ExternalTexture};
320 bool canUsef16 = f16ExternalBuffer->getUsage() & GRALLOC_USAGE_HW_TEXTURE;
321
322 for (auto texture : canUsef16 ? texturesWithF16 : textures) {
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400323 drawImageLayers(renderengine, display, dstTexture, texture);
324 // Draw layers for b/185569240.
325 drawClippedLayers(renderengine, display, dstTexture, texture);
326 }
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400327
328 const nsecs_t timeAfter = systemTime();
329 const float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6;
330 const int shadersCompiled = renderengine->reportShadersCompiled();
331 ALOGD("Shader cache generated %d shaders in %f ms\n", shadersCompiled, compileTimeMs);
332 }
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500333}
334
335} // namespace android::renderengine::skia