blob: e1887a882a41bde72ab06d8245ec9a937597cfe4 [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"
Vishnu Nairdbbe3852022-01-12 20:22:11 -080022#include "renderengine/impl/ExternalTexture.h"
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050023#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
29namespace android::renderengine::skia {
30
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040031namespace {
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -040032
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040033// clang-format off
34// Any non-identity matrix will do.
35const 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 Nifongafeac5b2021-05-27 10:52:30 -040039const 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 Nifong13491502021-06-30 17:28:29 -040043const 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 Nifongb9f27ef2021-04-01 16:44:12 -040047// clang-format on
Nathaniel Nifongbf6f7542021-04-27 12:05:16 -040048// When setting layer.sourceDataspace, whether it matches the destination or not determines whether
49// a color correction effect is added to the shader.
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040050constexpr auto kDestDataSpace = ui::Dataspace::SRGB;
Nathaniel Nifong21e021f2021-04-21 13:15:46 -040051constexpr auto kOtherDataSpace = ui::Dataspace::DISPLAY_P3;
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040052} // namespace
53
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -040054static void drawShadowLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
Alec Mouria90a5702021-04-16 16:36:21 +000055 const std::shared_ptr<ExternalTexture>& dstTexture) {
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050056 // Somewhat arbitrary dimensions, but on screen and slightly shorter, based
57 // on actual use.
Nathaniel Nifonga6b54232021-07-02 13:24:32 -040058 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 IIIb9216dc2021-03-08 17:19:01 -050062 LayerSettings layer{
63 .geometry =
64 Geometry{
65 .boundaries = rect,
66 .roundedCornersCrop = rect,
Vishnu Nair50c0afe2022-07-11 15:04:07 -070067 .roundedCornersRadius = {50.f, 50.f},
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050068 },
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -040069 // drawShadow ignores alpha
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050070 .shadow =
71 ShadowSettings{
Nathaniel Nifonga6b54232021-07-02 13:24:32 -040072 .boundaries = rect,
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050073 .ambientColor = vec4(0, 0, 0, 0.00935997f),
74 .spotColor = vec4(0, 0, 0, 0.0455841f),
Nathaniel Nifonga6b54232021-07-02 13:24:32 -040075 .lightPos = vec3(500.f, -1500.f, 1500.f),
76 .lightRadius = 2500.0f,
77 .length = 15.f,
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050078 },
Nathaniel Nifong490a9472021-06-23 16:44:19 -040079 // setting this is mandatory for shadows and blurs
80 .skipContentDraw = true,
Nathaniel Nifonga6b54232021-07-02 13:24:32 -040081 .alpha = 1,
82 };
83 LayerSettings caster{
84 .geometry =
85 Geometry{
86 .boundaries = smallerRect,
87 .roundedCornersCrop = rect,
Vishnu Nair50c0afe2022-07-11 15:04:07 -070088 .roundedCornersRadius = {50.f, 50.f},
Nathaniel Nifonga6b54232021-07-02 13:24:32 -040089 },
90 .source =
91 PixelSource{
92 .solidColor = half3(0.f, 0.f, 0.f),
93 },
94 .alpha = 1,
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050095 };
96
Nathaniel Nifong49a59582021-07-26 19:49:47 -040097 // 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 IIIae07fe52022-04-26 15:23:55 -0400115
116 auto layers = std::vector<LayerSettings>{layer, caster};
Alec Mourif29700f2023-08-17 21:53:31 +0000117 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500118 }
119}
120
121static void drawImageLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
Alec Mouria90a5702021-04-16 16:36:21 +0000122 const std::shared_ptr<ExternalTexture>& dstTexture,
123 const std::shared_ptr<ExternalTexture>& srcTexture) {
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500124 const Rect& displayRect = display.physicalDisplay;
125 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
126 LayerSettings layer{
127 .geometry =
128 Geometry{
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400129 // 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 IIIb9216dc2021-03-08 17:19:01 -0500132 .boundaries = rect,
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500133 .roundedCornersCrop = rect,
134 },
135 .source = PixelSource{.buffer =
136 Buffer{
Alec Mouria90a5702021-04-16 16:36:21 +0000137 .buffer = srcTexture,
John Reckac09e452021-04-07 16:35:37 -0400138 .maxLuminanceNits = 1000.f,
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500139 }},
140 };
141
Nathaniel Nifongbf6f7542021-04-27 12:05:16 -0400142 for (auto dataspace : {kDestDataSpace, kOtherDataSpace}) {
143 layer.sourceDataspace = dataspace;
Nathaniel Nifong13491502021-06-30 17:28:29 -0400144 // 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 Nifongf06a45b2021-06-25 17:24:26 -0400146 for (float roundedCornersRadius : {0.0f, 50.0f}) {
147 // roundedCornersCrop is always set, but the radius triggers the behavior
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700148 layer.geometry.roundedCornersRadius = {roundedCornersRadius, roundedCornersRadius};
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400149 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 IIIae07fe52022-04-26 15:23:55 -0400153 auto layers = std::vector<LayerSettings>{layer};
Alec Mourif29700f2023-08-17 21:53:31 +0000154 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500155 }
156 }
157 }
158 }
159}
160
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400161static void drawSolidLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
Alec Mouria90a5702021-04-16 16:36:21 +0000162 const std::shared_ptr<ExternalTexture>& dstTexture) {
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400163 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 Nifong4fc750d2021-03-19 11:37:36 -0400170 .source =
171 PixelSource{
172 .solidColor = half3(0.1f, 0.2f, 0.3f),
173 },
Nathaniel Nifong768693f2021-06-08 14:33:47 -0400174 .alpha = 0.5,
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400175 };
176
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400177 for (auto transform : {mat4(), kScaleAndTranslate}) {
178 layer.geometry.positionTransform = transform;
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400179 for (float roundedCornersRadius : {0.0f, 50.f}) {
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700180 layer.geometry.roundedCornersRadius = {roundedCornersRadius, roundedCornersRadius};
Leon Scroggins IIIae07fe52022-04-26 15:23:55 -0400181 auto layers = std::vector<LayerSettings>{layer};
Alec Mourif29700f2023-08-17 21:53:31 +0000182 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400183 }
184 }
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400185}
186
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400187static void drawBlurLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
Alec Mouria90a5702021-04-16 16:36:21 +0000188 const std::shared_ptr<ExternalTexture>& dstTexture) {
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400189 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 Nifong490a9472021-06-23 16:44:19 -0400197 // setting this is mandatory for shadows and blurs
198 .skipContentDraw = true,
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400199 };
200
Nathaniel Nifong490a9472021-06-23 16:44:19 -0400201 // Different blur code is invoked for radii less and greater than 30 pixels
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400202 for (int radius : {9, 60}) {
203 layer.backgroundBlurRadius = radius;
Leon Scroggins IIIae07fe52022-04-26 15:23:55 -0400204 auto layers = std::vector<LayerSettings>{layer};
Alec Mourif29700f2023-08-17 21:53:31 +0000205 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400206 }
207}
208
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400209// 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 Nifongafeac5b2021-05-27 10:52:30 -0400211// For buffers, this is done with a stage that computes coverage and it will differ for round and
212// elliptical corners.
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400213static void drawClippedLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
214 const std::shared_ptr<ExternalTexture>& dstTexture,
215 const std::shared_ptr<ExternalTexture>& srcTexture) {
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400216 const Rect& displayRect = display.physicalDisplay;
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400217 FloatRect rect(0, 0, displayRect.width(), displayRect.height() - 20); // boundary is smaller
218
Nathaniel Nifongafeac5b2021-05-27 10:52:30 -0400219 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 Nifong2d91c5e2021-05-13 17:14:00 -0400230
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400231 LayerSettings layer{
232 .geometry =
233 Geometry{
234 .boundaries = rect,
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700235 .roundedCornersRadius = {27.f, 27.f},
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400236 .roundedCornersCrop =
237 FloatRect(0, 0, displayRect.width(), displayRect.height()),
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400238 },
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400239 };
240
Nathaniel Nifongafeac5b2021-05-27 10:52:30 -0400241 for (auto pixelSource : {bufferSource, bufferOpaque, colorSource}) {
242 layer.source = pixelSource;
243 for (auto dataspace : {kDestDataSpace, kOtherDataSpace}) {
244 layer.sourceDataspace = dataspace;
Nathaniel Nifong13491502021-06-30 17:28:29 -0400245 // Produce a CircularRRect clip and an EllipticalRRect clip.
Nathaniel Nifongafeac5b2021-05-27 10:52:30 -0400246 for (auto transform : {kScaleAndTranslate, kScaleAsymmetric}) {
247 layer.geometry.positionTransform = transform;
Nathaniel Nifongafeac5b2021-05-27 10:52:30 -0400248 for (float alpha : {0.5f, 1.f}) {
Leon Scroggins IIIae07fe52022-04-26 15:23:55 -0400249 layer.alpha = alpha;
250 auto layers = std::vector<LayerSettings>{layer};
Alec Mourif29700f2023-08-17 21:53:31 +0000251 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
Nathaniel Nifongafeac5b2021-05-27 10:52:30 -0400252 }
253 }
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400254 }
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400255 }
256}
257
Nathaniel Nifong13491502021-06-30 17:28:29 -0400258static 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 Nair50c0afe2022-07-11 15:04:07 -0700271 .roundedCornersRadius = {94.2551f, 94.2551f},
272 .roundedCornersCrop = FloatRect(-93.75, 0, displayRect.width() + 93.75,
273 displayRect.height()),
Nathaniel Nifong13491502021-06-30 17:28:29 -0400274 },
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 Qi59a9f502021-10-12 18:53:23 +0000287 auto layers = std::vector<LayerSettings>{layer};
Alec Mourif29700f2023-08-17 21:53:31 +0000288 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
Nathaniel Nifong13491502021-06-30 17:28:29 -0400289}
290
291static 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 Nair50c0afe2022-07-11 15:04:07 -0700303 .roundedCornersRadius = {50.f, 50.f},
Nathaniel Nifong13491502021-06-30 17:28:29 -0400304 .roundedCornersCrop = rect,
305 },
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700306 .source =
307 PixelSource{
Nathaniel Nifong13491502021-06-30 17:28:29 -0400308 .solidColor = half3(0.f, 0.f, 0.f),
309 },
310 .sourceDataspace = kDestDataSpace,
311 .alpha = 0,
312 .disableBlending = true,
313
314 };
315
Sally Qi59a9f502021-10-12 18:53:23 +0000316 auto layers = std::vector<LayerSettings>{layer};
Alec Mourif29700f2023-08-17 21:53:31 +0000317 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
Nathaniel Nifong13491502021-06-30 17:28:29 -0400318}
319
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400320//
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 IIIb9216dc2021-03-08 17:19:01 -0500331void Cache::primeShaderCache(SkiaRenderEngine* renderengine) {
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400332 const int previousCount = renderengine->reportShadersCompiled();
333 if (previousCount) {
334 ALOGD("%d Shaders already compiled before Cache::primeShaderCache ran\n", previousCount);
335 }
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500336
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400337 // 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 Nifonga6b54232021-07-02 13:24:32 -0400350 DisplaySettings p3Display{
351 .physicalDisplay = displayRect,
352 .clip = displayRect,
353 .maxLuminance = 500,
354 .outputDataspace = kOtherDataSpace,
355 };
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500356
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400357 const int64_t usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Alec Mouria90a5702021-04-16 16:36:21 +0000358
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400359 sp<GraphicBuffer> dstBuffer =
Ady Abrahamd11bade2022-08-01 16:18:03 -0700360 sp<GraphicBuffer>::make(displayRect.width(), displayRect.height(),
361 PIXEL_FORMAT_RGBA_8888, 1, usage, "primeShaderCache_dst");
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400362
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400363 const auto dstTexture =
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800364 std::make_shared<impl::ExternalTexture>(dstBuffer, *renderengine,
365 impl::ExternalTexture::Usage::WRITEABLE);
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400366 // 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 Abrahamd11bade2022-08-01 16:18:03 -0700371 sp<GraphicBuffer>::make(displayRect.width(), displayRect.height(),
372 PIXEL_FORMAT_RGBA_8888, 1, usage, "drawImageLayer_src");
Alec Mouria90a5702021-04-16 16:36:21 +0000373
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800374 const auto srcTexture = std::make_shared<
375 impl::ExternalTexture>(srcBuffer, *renderengine,
376 impl::ExternalTexture::Usage::READABLE |
377 impl::ExternalTexture::Usage::WRITEABLE);
Nathaniel Nifong13491502021-06-30 17:28:29 -0400378 drawHolePunchLayer(renderengine, display, dstTexture);
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400379 drawSolidLayers(renderengine, display, dstTexture);
Nathaniel Nifong49a59582021-07-26 19:49:47 -0400380
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400381 drawShadowLayers(renderengine, display, srcTexture);
Nathaniel Nifonga6b54232021-07-02 13:24:32 -0400382 drawShadowLayers(renderengine, p3Display, srcTexture);
Nathaniel Nifongcda45e92021-06-10 15:01:42 -0400383
384 if (renderengine->supportsBackgroundBlur()) {
385 drawBlurLayers(renderengine, display, dstTexture);
386 }
387
Nathaniel Nifong73537a32021-08-06 15:07:26 -0400388 // 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 Sollenbergere9a51082021-05-06 14:01:38 -0400392 const int64_t usageExternal = GRALLOC_USAGE_HW_TEXTURE;
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400393 sp<GraphicBuffer> externalBuffer =
Ady Abrahamd11bade2022-08-01 16:18:03 -0700394 sp<GraphicBuffer>::make(displayRect.width(), displayRect.height(),
395 PIXEL_FORMAT_RGBA_8888, 1, usageExternal,
396 "primeShaderCache_external");
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400397 const auto externalTexture =
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800398 std::make_shared<impl::ExternalTexture>(externalBuffer, *renderengine,
399 impl::ExternalTexture::Usage::READABLE);
Nathaniel Nifong73537a32021-08-06 15:07:26 -0400400 std::vector<const std::shared_ptr<ExternalTexture>> textures =
401 {srcTexture, externalTexture};
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400402
Nathaniel Nifong73537a32021-08-06 15:07:26 -0400403 // 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 Nifongf06a45b2021-06-25 17:24:26 -0400405 sp<GraphicBuffer> f16ExternalBuffer =
Ady Abrahamd11bade2022-08-01 16:18:03 -0700406 sp<GraphicBuffer>::make(displayRect.width(), displayRect.height(),
407 PIXEL_FORMAT_RGBA_FP16, 1, usageExternal,
408 "primeShaderCache_external_f16");
Nathaniel Nifong73537a32021-08-06 15:07:26 -0400409 // 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 Nairdbbe3852022-01-12 20:22:11 -0800413 std::make_shared<impl::ExternalTexture>(f16ExternalBuffer, *renderengine,
414 impl::ExternalTexture::Usage::READABLE);
Nathaniel Nifong73537a32021-08-06 15:07:26 -0400415 textures.push_back(f16ExternalTexture);
416 }
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400417
Nathaniel Nifong73537a32021-08-06 15:07:26 -0400418 for (auto texture : textures) {
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400419 drawImageLayers(renderengine, display, dstTexture, texture);
420 // Draw layers for b/185569240.
421 drawClippedLayers(renderengine, display, dstTexture, texture);
422 }
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400423
Nathaniel Nifong13491502021-06-30 17:28:29 -0400424 drawPIPImageLayer(renderengine, display, dstTexture, externalTexture);
425
Nathaniel Nifong2d2f4322021-07-22 15:17:36 -0400426 // 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 Qi59a9f502021-10-12 18:53:23 +0000430 auto layers = std::vector<LayerSettings>{layer};
Sally Qi4cabdd02021-08-05 16:45:57 -0700431 // call get() to make it synchronous
Alec Mourif29700f2023-08-17 21:53:31 +0000432 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd()).get();
Nathaniel Nifong2d2f4322021-07-22 15:17:36 -0400433
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400434 const nsecs_t timeAfter = systemTime();
435 const float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6;
Leon Scroggins III45be9182022-04-27 10:37:11 -0400436 const int shadersCompiled = renderengine->reportShadersCompiled() - previousCount;
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400437 ALOGD("Shader cache generated %d shaders in %f ms\n", shadersCompiled, compileTimeMs);
438 }
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500439}
440
441} // namespace android::renderengine::skia