blob: 0eee56443c46c5c1ffa38956db66e5425f5c2b8f [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 Nifong21e021f2021-04-21 13:15:46 -040040const auto kScaleYOnly = mat4(1.f, 0.f, 0.f, 0.f,
41 0.f, 0.7f, 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,
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050075 };
76
77 auto layers = std::vector<const LayerSettings*>{&layer};
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040078 // The identity matrix will generate the fast shader
Alec Mouria90a5702021-04-16 16:36:21 +000079 renderengine->drawLayers(display, layers, dstTexture, kUseFrameBufferCache, base::unique_fd(),
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040080 nullptr);
81 // This matrix, which has different scales for x and y, will
82 // generate the slower (more general case) version, which has variants for translucent
83 // casters and rounded rects.
84 // clang-format off
85 layer.geometry.positionTransform = mat4(0.7f, 0.f, 0.f, 0.f,
86 0.f, 0.8f, 0.f, 0.f,
87 0.f, 0.f, 1.f, 0.f,
88 0.f, 0.f, 0.f, 1.f);
89 // clang-format on
90 for (auto translucent : {false, true}) {
91 layer.shadow.casterIsTranslucent = translucent;
Alec Mouria90a5702021-04-16 16:36:21 +000092 renderengine->drawLayers(display, layers, dstTexture, kUseFrameBufferCache,
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050093 base::unique_fd(), nullptr);
94 }
95}
96
97static void drawImageLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
Alec Mouria90a5702021-04-16 16:36:21 +000098 const std::shared_ptr<ExternalTexture>& dstTexture,
99 const std::shared_ptr<ExternalTexture>& srcTexture) {
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500100 const Rect& displayRect = display.physicalDisplay;
101 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
102 LayerSettings layer{
103 .geometry =
104 Geometry{
105 .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
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400115 auto threeCornerRadii = {0.0f, 0.05f, 50.f};
116 auto oneCornerRadius = {50.f};
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400117
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500118 // Test both drawRect and drawRRect
119 auto layers = std::vector<const LayerSettings*>{&layer};
Nathaniel Nifongbf6f7542021-04-27 12:05:16 -0400120 for (auto dataspace : {kDestDataSpace, kOtherDataSpace}) {
121 layer.sourceDataspace = dataspace;
122 for (bool identity : {true, false}) {
123 layer.geometry.positionTransform = identity ? mat4() : kScaleAndTranslate;
124 // Corner radii less than 0.5 creates a special shader. This likely occurs in real usage
125 // due to animating corner radius.
126 // For the non-idenity matrix, only the large corner radius will create a new shader.
127 for (float roundedCornersRadius : identity ? threeCornerRadii : oneCornerRadius) {
128 // roundedCornersCrop is always set, but it is this radius that triggers the
129 // behavior
130 layer.geometry.roundedCornersRadius = roundedCornersRadius;
131 for (bool isOpaque : {true, false}) {
132 layer.source.buffer.isOpaque = isOpaque;
133 for (auto alpha : {half(.23999f), half(1.0f)}) {
134 layer.alpha = alpha;
135 renderengine->drawLayers(display, layers, dstTexture, kUseFrameBufferCache,
136 base::unique_fd(), nullptr);
137 }
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500138 }
139 }
140 }
141 }
142}
143
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400144static void drawSolidLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
Alec Mouria90a5702021-04-16 16:36:21 +0000145 const std::shared_ptr<ExternalTexture>& dstTexture) {
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400146 const Rect& displayRect = display.physicalDisplay;
147 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
148 LayerSettings layer{
149 .geometry =
150 Geometry{
151 .boundaries = rect,
152 },
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400153 .source =
154 PixelSource{
155 .solidColor = half3(0.1f, 0.2f, 0.3f),
156 },
Alec Mouria90a5702021-04-16 16:36:21 +0000157 .alpha = 1,
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400158 };
159
160 auto layers = std::vector<const LayerSettings*>{&layer};
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400161 for (auto transform : {mat4(), kScaleAndTranslate}) {
162 layer.geometry.positionTransform = transform;
163 for (float roundedCornersRadius : {0.0f, 0.05f, 50.f}) {
164 layer.geometry.roundedCornersRadius = roundedCornersRadius;
Alec Mouria90a5702021-04-16 16:36:21 +0000165 renderengine->drawLayers(display, layers, dstTexture, kUseFrameBufferCache,
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400166 base::unique_fd(), nullptr);
167 }
168 }
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400169}
170
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400171static void drawBlurLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
Alec Mouria90a5702021-04-16 16:36:21 +0000172 const std::shared_ptr<ExternalTexture>& dstTexture) {
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400173 const Rect& displayRect = display.physicalDisplay;
174 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
175 LayerSettings layer{
176 .geometry =
177 Geometry{
178 .boundaries = rect,
179 },
180 .alpha = 1,
181 };
182
183 auto layers = std::vector<const LayerSettings*>{&layer};
184 for (int radius : {9, 60}) {
185 layer.backgroundBlurRadius = radius;
Alec Mouria90a5702021-04-16 16:36:21 +0000186 renderengine->drawLayers(display, layers, dstTexture, kUseFrameBufferCache,
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400187 base::unique_fd(), nullptr);
188 }
189}
190
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400191static void drawTextureScaleLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
192 const std::shared_ptr<ExternalTexture>& dstTexture,
193 const std::shared_ptr<ExternalTexture>& srcTexture) {
194 const Rect& displayRect = display.physicalDisplay;
195 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
196 LayerSettings layer{
197 .geometry =
198 Geometry{
199 .boundaries = rect,
200 .roundedCornersCrop = rect,
201 .positionTransform = kScaleAndTranslate,
202 .roundedCornersRadius = 300,
203 },
204 .source = PixelSource{.buffer =
205 Buffer{
206 .buffer = srcTexture,
John Reckac09e452021-04-07 16:35:37 -0400207 .maxLuminanceNits = 1000.f,
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400208 .textureTransform = kScaleYOnly,
209 }},
210 .sourceDataspace = kOtherDataSpace,
211 };
212
213 auto layers = std::vector<const LayerSettings*>{&layer};
214 for (float alpha : {0.5f, 1.f}) {
215 layer.alpha = alpha,
216 renderengine->drawLayers(display, layers, dstTexture, kUseFrameBufferCache,
217 base::unique_fd(), nullptr);
218 }
219}
220
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400221//
222// The collection of shaders cached here were found by using perfetto to record shader compiles
223// during actions that involve RenderEngine, logging the layer settings, and the shader code
224// and reproducing those settings here.
225//
226// It is helpful when debugging this to turn on
227// in SkGLRenderEngine.cpp:
228// kPrintLayerSettings = true
229// kFlushAfterEveryLayer = true
230// in external/skia/src/gpu/gl/builders/GrGLShaderStringBuilder.cpp
231// gPrintSKSL = true
232//
233// TODO(b/184631553) cache the shader involved in youtube pip return.
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500234void Cache::primeShaderCache(SkiaRenderEngine* renderengine) {
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400235 const int previousCount = renderengine->reportShadersCompiled();
236 if (previousCount) {
237 ALOGD("%d Shaders already compiled before Cache::primeShaderCache ran\n", previousCount);
238 }
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500239
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400240 // The loop is beneficial for debugging and should otherwise be optimized out by the compiler.
241 // Adding additional bounds to the loop is useful for verifying that the size of the dst buffer
242 // does not impact the shader compilation counts by triggering different behaviors in RE/Skia.
243 for (SkSize bounds : {SkSize::Make(128, 128), /*SkSize::Make(1080, 2340)*/}) {
244 const nsecs_t timeBefore = systemTime();
245 // The dimensions should not matter, so long as we draw inside them.
246 const Rect displayRect(0, 0, bounds.fWidth, bounds.fHeight);
247 DisplaySettings display{
248 .physicalDisplay = displayRect,
249 .clip = displayRect,
250 .maxLuminance = 500,
251 .outputDataspace = kDestDataSpace,
252 };
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500253
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400254 const int64_t usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Alec Mouria90a5702021-04-16 16:36:21 +0000255
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400256 sp<GraphicBuffer> dstBuffer =
257 new GraphicBuffer(displayRect.width(), displayRect.height(), PIXEL_FORMAT_RGBA_8888,
258 1, usage, "primeShaderCache_dst");
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400259
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400260 const auto dstTexture =
261 std::make_shared<ExternalTexture>(dstBuffer, *renderengine,
262 ExternalTexture::Usage::WRITEABLE);
263 // This buffer will be the source for the call to drawImageLayers. Draw
264 // something to it as a placeholder for what an app draws. We should draw
265 // something, but the details are not important. Make use of the shadow layer drawing step
266 // to populate it.
267 sp<GraphicBuffer> srcBuffer =
268 new GraphicBuffer(displayRect.width(), displayRect.height(), PIXEL_FORMAT_RGBA_8888,
269 1, usage, "drawImageLayer_src");
Alec Mouria90a5702021-04-16 16:36:21 +0000270
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400271 const auto srcTexture =
272 std::make_shared<ExternalTexture>(srcBuffer, *renderengine,
273 ExternalTexture::Usage::READABLE |
274 ExternalTexture::Usage::WRITEABLE);
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400275
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400276 drawSolidLayers(renderengine, display, dstTexture);
277 drawShadowLayers(renderengine, display, srcTexture);
278 drawBlurLayers(renderengine, display, dstTexture);
279 // The majority of shaders are related to sampling images.
280 drawImageLayers(renderengine, display, dstTexture, srcTexture);
Nathaniel Nifongf4362402021-04-12 11:41:05 -0400281
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400282 // should be the same as AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE;
283 const int64_t usageExternal = GRALLOC_USAGE_HW_TEXTURE;
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400284
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400285 sp<GraphicBuffer> externalBuffer =
286 new GraphicBuffer(displayRect.width(), displayRect.height(), PIXEL_FORMAT_RGBA_8888,
287 1, usageExternal, "primeShaderCache_external");
288 const auto externalTexture =
289 std::make_shared<ExternalTexture>(externalBuffer, *renderengine,
290 ExternalTexture::Usage::READABLE);
291 // TODO(b/184665179) doubles number of image shader compilations, but only somewhere
292 // between 6 and 8 will occur in real uses.
293 drawImageLayers(renderengine, display, dstTexture, externalTexture);
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400294
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400295 // Draw layers for b/185569240.
296 drawTextureScaleLayers(renderengine, display, dstTexture, externalTexture);
297
298 const nsecs_t timeAfter = systemTime();
299 const float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6;
300 const int shadersCompiled = renderengine->reportShadersCompiled();
301 ALOGD("Shader cache generated %d shaders in %f ms\n", shadersCompiled, compileTimeMs);
302 }
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500303}
304
305} // namespace android::renderengine::skia