blob: 25296f06ee0f9fbcc8724e2348e8ab9ea2bd74eb [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;
Mattias Simonsson458adf12023-08-16 10:49:30 +000052constexpr auto kBT2020DataSpace = ui::Dataspace::BT2020_ITU_PQ;
Mattias Simonsson5860d922023-08-16 13:47:41 +000053constexpr auto kExtendedHdrDataSpce =
54 static_cast<ui::Dataspace>(ui::Dataspace::RANGE_EXTENDED | ui::Dataspace::TRANSFER_SRGB |
55 ui::Dataspace::STANDARD_DCI_P3);
Mattias Simonsson65746132023-08-15 13:26:16 +000056// Dimming is needed to trigger linear effects for some dataspace pairs
57const std::array<float, 3> kLayerWhitePoints = {
58 1000.0f, 500.0f,
59 100.0f, // trigger dithering by dimming below 20%
60};
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -040061} // namespace
62
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -040063static void drawShadowLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
Alec Mouria90a5702021-04-16 16:36:21 +000064 const std::shared_ptr<ExternalTexture>& dstTexture) {
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050065 // Somewhat arbitrary dimensions, but on screen and slightly shorter, based
66 // on actual use.
Nathaniel Nifonga6b54232021-07-02 13:24:32 -040067 const Rect& displayRect = display.physicalDisplay;
68 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
69 FloatRect smallerRect(20, 20, displayRect.width()-20, displayRect.height()-20);
70
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050071 LayerSettings layer{
72 .geometry =
73 Geometry{
74 .boundaries = rect,
Vishnu Nair50c0afe2022-07-11 15:04:07 -070075 .roundedCornersRadius = {50.f, 50.f},
Leon Scroggins III894c5f82023-08-25 14:43:54 -040076 .roundedCornersCrop = rect,
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050077 },
Leon Scroggins III894c5f82023-08-25 14:43:54 -040078 .alpha = 1,
79 // setting this is mandatory for shadows and blurs
80 .skipContentDraw = true,
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -040081 // drawShadow ignores alpha
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050082 .shadow =
83 ShadowSettings{
Nathaniel Nifonga6b54232021-07-02 13:24:32 -040084 .boundaries = rect,
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050085 .ambientColor = vec4(0, 0, 0, 0.00935997f),
86 .spotColor = vec4(0, 0, 0, 0.0455841f),
Nathaniel Nifonga6b54232021-07-02 13:24:32 -040087 .lightPos = vec3(500.f, -1500.f, 1500.f),
88 .lightRadius = 2500.0f,
89 .length = 15.f,
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -050090 },
Nathaniel Nifonga6b54232021-07-02 13:24:32 -040091 };
92 LayerSettings caster{
93 .geometry =
94 Geometry{
95 .boundaries = smallerRect,
Vishnu Nair50c0afe2022-07-11 15:04:07 -070096 .roundedCornersRadius = {50.f, 50.f},
Leon Scroggins III894c5f82023-08-25 14:43:54 -040097 .roundedCornersCrop = rect,
Nathaniel Nifonga6b54232021-07-02 13:24:32 -040098 },
99 .source =
100 PixelSource{
101 .solidColor = half3(0.f, 0.f, 0.f),
102 },
103 .alpha = 1,
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500104 };
105
Nathaniel Nifong49a59582021-07-26 19:49:47 -0400106 // Four combinations of settings are used (two transforms here, and drawShadowLayers is
107 // called with two different destination data spaces) They're all rounded rect.
108 // Three of these are cache misses that generate new shaders.
109 // The first combination generates a short and simple shadow shader.
110 // The second combination, flip transform, generates two shaders. The first appears to involve
111 // gaussian_fp. The second is a long and general purpose shadow shader with a device space
112 // transformation stage.
113 // The third combination is a cache hit, nothing new.
114 // The fourth combination, flip transform with a non-SRGB destination dataspace, is new.
115 // It is unique in that nearly everything is done in the vertex shader, and that vertex shader
116 // requires color correction. This is triggered differently from every other instance of color
117 // correction. All other instances are triggered when src and dst dataspaces differ, while
118 // this one is triggered by the destination being non-srgb. Apparently since the third
119 // combination is a cache hit, this color correction is only added when the vertex shader is
120 // doing something non-trivial.
121 for (auto transform : {mat4(), kFlip}) {
122 layer.geometry.positionTransform = transform;
123 caster.geometry.positionTransform = transform;
Leon Scroggins IIIae07fe52022-04-26 15:23:55 -0400124
125 auto layers = std::vector<LayerSettings>{layer, caster};
Alec Mourif29700f2023-08-17 21:53:31 +0000126 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500127 }
128}
129
130static void drawImageLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
Alec Mouria90a5702021-04-16 16:36:21 +0000131 const std::shared_ptr<ExternalTexture>& dstTexture,
132 const std::shared_ptr<ExternalTexture>& srcTexture) {
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500133 const Rect& displayRect = display.physicalDisplay;
134 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
135 LayerSettings layer{
136 .geometry =
137 Geometry{
Leon Scroggins III894c5f82023-08-25 14:43:54 -0400138 .boundaries = rect,
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400139 // The position transform doesn't matter when the reduced shader mode
140 // in in effect. A matrix transform stage is always included.
141 .positionTransform = mat4(),
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500142 .roundedCornersCrop = rect,
143 },
144 .source = PixelSource{.buffer =
145 Buffer{
Alec Mouria90a5702021-04-16 16:36:21 +0000146 .buffer = srcTexture,
John Reckac09e452021-04-07 16:35:37 -0400147 .maxLuminanceNits = 1000.f,
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500148 }},
149 };
150
Nathaniel Nifongbf6f7542021-04-27 12:05:16 -0400151 for (auto dataspace : {kDestDataSpace, kOtherDataSpace}) {
152 layer.sourceDataspace = dataspace;
Nathaniel Nifong13491502021-06-30 17:28:29 -0400153 // Cache shaders for both rects and round rects.
154 // In reduced shader mode, all non-zero round rect radii get the same code path.
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400155 for (float roundedCornersRadius : {0.0f, 50.0f}) {
156 // roundedCornersCrop is always set, but the radius triggers the behavior
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700157 layer.geometry.roundedCornersRadius = {roundedCornersRadius, roundedCornersRadius};
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400158 for (bool isOpaque : {true, false}) {
159 layer.source.buffer.isOpaque = isOpaque;
160 for (auto alpha : {half(.2f), half(1.0f)}) {
161 layer.alpha = alpha;
Leon Scroggins IIIae07fe52022-04-26 15:23:55 -0400162 auto layers = std::vector<LayerSettings>{layer};
Alec Mourif29700f2023-08-17 21:53:31 +0000163 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500164 }
165 }
166 }
167 }
168}
169
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400170static void drawSolidLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
Alec Mouria90a5702021-04-16 16:36:21 +0000171 const std::shared_ptr<ExternalTexture>& dstTexture) {
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400172 const Rect& displayRect = display.physicalDisplay;
173 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
174 LayerSettings layer{
175 .geometry =
176 Geometry{
177 .boundaries = rect,
178 },
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400179 .source =
180 PixelSource{
181 .solidColor = half3(0.1f, 0.2f, 0.3f),
182 },
Nathaniel Nifong768693f2021-06-08 14:33:47 -0400183 .alpha = 0.5,
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400184 };
185
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400186 for (auto transform : {mat4(), kScaleAndTranslate}) {
187 layer.geometry.positionTransform = transform;
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400188 for (float roundedCornersRadius : {0.0f, 50.f}) {
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700189 layer.geometry.roundedCornersRadius = {roundedCornersRadius, roundedCornersRadius};
Leon Scroggins IIIae07fe52022-04-26 15:23:55 -0400190 auto layers = std::vector<LayerSettings>{layer};
Alec Mourif29700f2023-08-17 21:53:31 +0000191 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400192 }
193 }
Nathaniel Nifong4fc750d2021-03-19 11:37:36 -0400194}
195
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400196static void drawBlurLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
Alec Mouria90a5702021-04-16 16:36:21 +0000197 const std::shared_ptr<ExternalTexture>& dstTexture) {
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400198 const Rect& displayRect = display.physicalDisplay;
199 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
200 LayerSettings layer{
201 .geometry =
202 Geometry{
203 .boundaries = rect,
204 },
205 .alpha = 1,
Nathaniel Nifong490a9472021-06-23 16:44:19 -0400206 // setting this is mandatory for shadows and blurs
207 .skipContentDraw = true,
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400208 };
209
Nathaniel Nifong490a9472021-06-23 16:44:19 -0400210 // Different blur code is invoked for radii less and greater than 30 pixels
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400211 for (int radius : {9, 60}) {
212 layer.backgroundBlurRadius = radius;
Leon Scroggins IIIae07fe52022-04-26 15:23:55 -0400213 auto layers = std::vector<LayerSettings>{layer};
Alec Mourif29700f2023-08-17 21:53:31 +0000214 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400215 }
216}
217
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400218// The unique feature of these layers is that the boundary is slightly smaller than the rounded
219// rect crop, so the rounded edges intersect that boundary and require a different clipping method.
Nathaniel Nifongafeac5b2021-05-27 10:52:30 -0400220// For buffers, this is done with a stage that computes coverage and it will differ for round and
221// elliptical corners.
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400222static void drawClippedLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
223 const std::shared_ptr<ExternalTexture>& dstTexture,
224 const std::shared_ptr<ExternalTexture>& srcTexture) {
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400225 const Rect& displayRect = display.physicalDisplay;
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400226 FloatRect rect(0, 0, displayRect.width(), displayRect.height() - 20); // boundary is smaller
227
Nathaniel Nifongafeac5b2021-05-27 10:52:30 -0400228 PixelSource bufferSource{.buffer = Buffer{
229 .buffer = srcTexture,
230 .isOpaque = 0,
231 .maxLuminanceNits = 1000.f,
232 }};
233 PixelSource bufferOpaque{.buffer = Buffer{
234 .buffer = srcTexture,
235 .isOpaque = 1,
236 .maxLuminanceNits = 1000.f,
237 }};
238 PixelSource colorSource{.solidColor = half3(0.1f, 0.2f, 0.3f)};
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400239
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400240 LayerSettings layer{
241 .geometry =
242 Geometry{
243 .boundaries = rect,
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700244 .roundedCornersRadius = {27.f, 27.f},
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400245 .roundedCornersCrop =
246 FloatRect(0, 0, displayRect.width(), displayRect.height()),
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400247 },
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400248 };
249
Nathaniel Nifongafeac5b2021-05-27 10:52:30 -0400250 for (auto pixelSource : {bufferSource, bufferOpaque, colorSource}) {
251 layer.source = pixelSource;
252 for (auto dataspace : {kDestDataSpace, kOtherDataSpace}) {
253 layer.sourceDataspace = dataspace;
Nathaniel Nifong13491502021-06-30 17:28:29 -0400254 // Produce a CircularRRect clip and an EllipticalRRect clip.
Nathaniel Nifongafeac5b2021-05-27 10:52:30 -0400255 for (auto transform : {kScaleAndTranslate, kScaleAsymmetric}) {
256 layer.geometry.positionTransform = transform;
Nathaniel Nifongafeac5b2021-05-27 10:52:30 -0400257 for (float alpha : {0.5f, 1.f}) {
Leon Scroggins IIIae07fe52022-04-26 15:23:55 -0400258 layer.alpha = alpha;
259 auto layers = std::vector<LayerSettings>{layer};
Alec Mourif29700f2023-08-17 21:53:31 +0000260 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
Nathaniel Nifongafeac5b2021-05-27 10:52:30 -0400261 }
262 }
Nathaniel Nifong2d91c5e2021-05-13 17:14:00 -0400263 }
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400264 }
265}
266
Nathaniel Nifong13491502021-06-30 17:28:29 -0400267static void drawPIPImageLayer(SkiaRenderEngine* renderengine, const DisplaySettings& display,
268 const std::shared_ptr<ExternalTexture>& dstTexture,
269 const std::shared_ptr<ExternalTexture>& srcTexture) {
270 const Rect& displayRect = display.physicalDisplay;
271 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
272 LayerSettings layer{
273 .geometry =
274 Geometry{
Leon Scroggins III894c5f82023-08-25 14:43:54 -0400275 .boundaries = rect,
Nathaniel Nifong13491502021-06-30 17:28:29 -0400276 // Note that this flip matrix only makes a difference when clipping,
277 // which happens in this layer because the roundrect crop is just a bit
278 // larger than the layer bounds.
279 .positionTransform = kFlip,
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700280 .roundedCornersRadius = {94.2551f, 94.2551f},
281 .roundedCornersCrop = FloatRect(-93.75, 0, displayRect.width() + 93.75,
282 displayRect.height()),
Nathaniel Nifong13491502021-06-30 17:28:29 -0400283 },
284 .source = PixelSource{.buffer =
285 Buffer{
286 .buffer = srcTexture,
Nathaniel Nifong13491502021-06-30 17:28:29 -0400287 .usePremultipliedAlpha = 1,
Leon Scroggins III894c5f82023-08-25 14:43:54 -0400288 .isOpaque = 0,
289 .maxLuminanceNits = 1000.f,
Nathaniel Nifong13491502021-06-30 17:28:29 -0400290 }},
Nathaniel Nifong13491502021-06-30 17:28:29 -0400291 .alpha = 1,
Leon Scroggins III894c5f82023-08-25 14:43:54 -0400292 .sourceDataspace = kOtherDataSpace,
Nathaniel Nifong13491502021-06-30 17:28:29 -0400293
294 };
295
Sally Qi59a9f502021-10-12 18:53:23 +0000296 auto layers = std::vector<LayerSettings>{layer};
Alec Mourif29700f2023-08-17 21:53:31 +0000297 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
Nathaniel Nifong13491502021-06-30 17:28:29 -0400298}
299
300static void drawHolePunchLayer(SkiaRenderEngine* renderengine, const DisplaySettings& display,
301 const std::shared_ptr<ExternalTexture>& dstTexture) {
302 const Rect& displayRect = display.physicalDisplay;
303 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
304 FloatRect small(0, 0, displayRect.width()-20, displayRect.height()+20);
305 LayerSettings layer{
306 .geometry =
307 Geometry{
Nathaniel Nifong13491502021-06-30 17:28:29 -0400308 // the boundaries have to be smaller than the rounded crop so that
309 // clipRRect is used instead of drawRRect
310 .boundaries = small,
Leon Scroggins III894c5f82023-08-25 14:43:54 -0400311 .positionTransform = kScaleAndTranslate,
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700312 .roundedCornersRadius = {50.f, 50.f},
Nathaniel Nifong13491502021-06-30 17:28:29 -0400313 .roundedCornersCrop = rect,
314 },
Vishnu Nair50c0afe2022-07-11 15:04:07 -0700315 .source =
316 PixelSource{
Nathaniel Nifong13491502021-06-30 17:28:29 -0400317 .solidColor = half3(0.f, 0.f, 0.f),
318 },
Nathaniel Nifong13491502021-06-30 17:28:29 -0400319 .alpha = 0,
Leon Scroggins III894c5f82023-08-25 14:43:54 -0400320 .sourceDataspace = kDestDataSpace,
Nathaniel Nifong13491502021-06-30 17:28:29 -0400321 .disableBlending = true,
322
323 };
324
Sally Qi59a9f502021-10-12 18:53:23 +0000325 auto layers = std::vector<LayerSettings>{layer};
Alec Mourif29700f2023-08-17 21:53:31 +0000326 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
Nathaniel Nifong13491502021-06-30 17:28:29 -0400327}
328
Mattias Simonsson65746132023-08-15 13:26:16 +0000329static void drawImageDimmedLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
330 const std::shared_ptr<ExternalTexture>& dstTexture,
331 const std::shared_ptr<ExternalTexture>& srcTexture) {
332 const Rect& displayRect = display.physicalDisplay;
333 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
334 LayerSettings layer{
335 .geometry =
336 Geometry{
337 // The position transform doesn't matter when the reduced shader mode
338 // in in effect. A matrix transform stage is always included.
339 .positionTransform = mat4(),
340 .boundaries = rect,
341 .roundedCornersCrop = rect,
342 .roundedCornersRadius = {0.f, 0.f},
343 },
344 .source = PixelSource{.buffer = Buffer{.buffer = srcTexture,
345 .maxLuminanceNits = 1000.f,
346 .usePremultipliedAlpha = true,
347 .isOpaque = true}},
348 .alpha = 1.f,
349 .sourceDataspace = kDestDataSpace,
350 };
351
352 std::vector<LayerSettings> layers;
353
354 for (auto layerWhitePoint : kLayerWhitePoints) {
355 layer.whitePointNits = layerWhitePoint;
356 layers.push_back(layer);
357 }
358 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
359}
360
361static void drawTransparentImageDimmedLayers(SkiaRenderEngine* renderengine,
362 const DisplaySettings& display,
363 const std::shared_ptr<ExternalTexture>& dstTexture,
364 const std::shared_ptr<ExternalTexture>& srcTexture) {
365 const Rect& displayRect = display.physicalDisplay;
366 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
367 LayerSettings layer{
368 .geometry =
369 Geometry{
370 .positionTransform = mat4(),
371 .boundaries = rect,
372 .roundedCornersCrop = rect,
373 },
374 .source = PixelSource{.buffer =
375 Buffer{
376 .buffer = srcTexture,
377 .maxLuminanceNits = 1000.f,
378 .usePremultipliedAlpha = true,
379 .isOpaque = false,
380 }},
381 .sourceDataspace = kDestDataSpace,
382 };
383
384 for (auto roundedCornerRadius : {0.f, 50.f}) {
385 layer.geometry.roundedCornersRadius = {roundedCornerRadius, roundedCornerRadius};
386 for (auto alpha : {0.5f, 1.0f}) {
387 layer.alpha = alpha;
Mattias Simonsson5860d922023-08-16 13:47:41 +0000388 for (auto isOpaque : {true, false}) {
389 if (roundedCornerRadius == 0.f && isOpaque) {
390 // already covered in drawImageDimmedLayers
391 continue;
392 }
Mattias Simonsson65746132023-08-15 13:26:16 +0000393
Mattias Simonsson5860d922023-08-16 13:47:41 +0000394 layer.source.buffer.isOpaque = isOpaque;
395 std::vector<LayerSettings> layers;
396
397 for (auto layerWhitePoint : kLayerWhitePoints) {
398 layer.whitePointNits = layerWhitePoint;
399 layers.push_back(layer);
400 }
401 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
Mattias Simonsson65746132023-08-15 13:26:16 +0000402 }
Mattias Simonsson65746132023-08-15 13:26:16 +0000403 }
404 }
405}
406
407static void drawClippedDimmedImageLayers(SkiaRenderEngine* renderengine,
408 const DisplaySettings& display,
409 const std::shared_ptr<ExternalTexture>& dstTexture,
410 const std::shared_ptr<ExternalTexture>& srcTexture) {
411 const Rect& displayRect = display.physicalDisplay;
412
413 // If rect and boundary is too small compared to roundedCornersRadius, Skia will switch to
414 // blending instead of EllipticalRRect, so enlarge them a bit.
415 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
416 FloatRect boundary(0, 0, displayRect.width(),
417 displayRect.height() - 20); // boundary is smaller
418 LayerSettings layer{
419 .geometry =
420 Geometry{
421 .positionTransform = mat4(),
422 .boundaries = boundary,
423 .roundedCornersCrop = rect,
424 .roundedCornersRadius = {27.f, 27.f},
425 },
426 .source = PixelSource{.buffer =
427 Buffer{
428 .buffer = srcTexture,
429 .maxLuminanceNits = 1000.f,
430 .usePremultipliedAlpha = true,
431 .isOpaque = false,
432 }},
433 .alpha = 1.f,
434 .sourceDataspace = kDestDataSpace,
435 };
436
437 std::array<mat4, 2> transforms = {kScaleAndTranslate, kScaleAsymmetric};
438
439 constexpr float radius = 27.f;
440
441 for (size_t i = 0; i < transforms.size(); i++) {
442 layer.geometry.positionTransform = transforms[i];
443 layer.geometry.roundedCornersRadius = {radius, radius};
444
445 std::vector<LayerSettings> layers;
446
447 for (auto layerWhitePoint : kLayerWhitePoints) {
448 layer.whitePointNits = layerWhitePoint;
449 layers.push_back(layer);
450 }
451 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
452 }
453}
454
455static void drawSolidDimmedLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
456 const std::shared_ptr<ExternalTexture>& dstTexture) {
457 const Rect& displayRect = display.physicalDisplay;
458 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
459 LayerSettings layer{
460 .geometry =
461 Geometry{
462 .boundaries = rect,
463 .roundedCornersCrop = rect,
464 },
465 .source =
466 PixelSource{
467 .solidColor = half3(0.1f, 0.2f, 0.3f),
468 },
469 .alpha = 1.f,
470 };
471
472 std::vector<LayerSettings> layers;
473
474 for (auto layerWhitePoint : kLayerWhitePoints) {
475 layer.whitePointNits = layerWhitePoint;
476 layers.push_back(layer);
477 }
478 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
479}
480
Mattias Simonsson458adf12023-08-16 10:49:30 +0000481static void drawBT2020ImageLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
482 const std::shared_ptr<ExternalTexture>& dstTexture,
483 const std::shared_ptr<ExternalTexture>& srcTexture) {
484 const Rect& displayRect = display.physicalDisplay;
485 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
486 LayerSettings layer{
487 .geometry =
488 Geometry{
489 // The position transform doesn't matter when the reduced shader mode
490 // in in effect. A matrix transform stage is always included.
491 .positionTransform = mat4(),
492 .boundaries = rect,
493 .roundedCornersCrop = rect,
494 .roundedCornersRadius = {0.f, 0.f},
495 },
496 .source = PixelSource{.buffer = Buffer{.buffer = srcTexture,
497 .maxLuminanceNits = 1000.f,
498 .usePremultipliedAlpha = true,
499 .isOpaque = true}},
500 .alpha = 1.f,
501 .sourceDataspace = kBT2020DataSpace,
502 };
503
504 for (auto alpha : {0.5f, 1.f}) {
505 layer.alpha = alpha;
506 std::vector<LayerSettings> layers;
507 layer.whitePointNits = -1.f;
508 layers.push_back(layer);
509
510 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
511 }
512}
513static void drawBT2020ClippedImageLayers(SkiaRenderEngine* renderengine,
514 const DisplaySettings& display,
515 const std::shared_ptr<ExternalTexture>& dstTexture,
516 const std::shared_ptr<ExternalTexture>& srcTexture) {
517 const Rect& displayRect = display.physicalDisplay;
518
519 // If rect and boundary is too small compared to roundedCornersRadius, Skia will switch to
520 // blending instead of EllipticalRRect, so enlarge them a bit.
521 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
522 FloatRect boundary(0, 0, displayRect.width(),
523 displayRect.height() - 10); // boundary is smaller
524 LayerSettings layer{
525 .geometry =
526 Geometry{
527 .positionTransform = kScaleAsymmetric,
528 .boundaries = boundary,
529 .roundedCornersCrop = rect,
530 .roundedCornersRadius = {64.1f, 64.1f},
531 },
532 .source = PixelSource{.buffer =
533 Buffer{
534 .buffer = srcTexture,
535 .maxLuminanceNits = 1000.f,
536 .usePremultipliedAlpha = true,
537 .isOpaque = true,
538 }},
539 .alpha = 0.5f,
540 .sourceDataspace = kBT2020DataSpace,
541 };
542
543 std::vector<LayerSettings> layers = {layer};
544 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
545}
546
Mattias Simonsson5860d922023-08-16 13:47:41 +0000547static void drawExtendedHDRImageLayers(SkiaRenderEngine* renderengine,
548 const DisplaySettings& display,
549 const std::shared_ptr<ExternalTexture>& dstTexture,
550 const std::shared_ptr<ExternalTexture>& srcTexture) {
551 const Rect& displayRect = display.physicalDisplay;
552 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
553 LayerSettings layer{
554 .geometry =
555 Geometry{
556 // The position transform doesn't matter when the reduced shader mode
557 // in in effect. A matrix transform stage is always included.
558 .positionTransform = mat4(),
559 .boundaries = rect,
560 .roundedCornersCrop = rect,
561 .roundedCornersRadius = {50.f, 50.f},
562 },
563 .source = PixelSource{.buffer = Buffer{.buffer = srcTexture,
564 .maxLuminanceNits = 1000.f,
565 .usePremultipliedAlpha = true,
566 .isOpaque = true}},
567 .alpha = 0.5f,
568 .sourceDataspace = kExtendedHdrDataSpce,
569 };
570
571 for (auto roundedCornerRadius : {0.f, 50.f}) {
572 layer.geometry.roundedCornersRadius = {roundedCornerRadius, roundedCornerRadius};
573 for (auto alpha : {0.5f, 1.f}) {
574 layer.alpha = alpha;
575 std::vector<LayerSettings> layers;
576
577 for (auto layerWhitePoint : kLayerWhitePoints) {
578 layer.whitePointNits = layerWhitePoint;
579 layers.push_back(layer);
580 }
581 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
582 }
583 }
584}
585
586static void drawP3ImageLayers(SkiaRenderEngine* renderengine, const DisplaySettings& display,
587 const std::shared_ptr<ExternalTexture>& dstTexture,
588 const std::shared_ptr<ExternalTexture>& srcTexture) {
589 const Rect& displayRect = display.physicalDisplay;
590 FloatRect rect(0, 0, displayRect.width(), displayRect.height());
591 LayerSettings layer{
592 .geometry =
593 Geometry{
594 // The position transform doesn't matter when the reduced shader mode
595 // in in effect. A matrix transform stage is always included.
596 .positionTransform = mat4(),
597 .boundaries = rect,
598 .roundedCornersCrop = rect,
599 .roundedCornersRadius = {50.f, 50.f},
600 },
601 .source = PixelSource{.buffer = Buffer{.buffer = srcTexture,
602 .maxLuminanceNits = 1000.f,
603 .usePremultipliedAlpha = true,
604 .isOpaque = false}},
605 .alpha = 0.5f,
606 .sourceDataspace = kOtherDataSpace,
607 };
608
609 for (auto alpha : {0.5f, 1.f}) {
610 layer.alpha = alpha;
611 std::vector<LayerSettings> layers;
612
613 for (auto layerWhitePoint : kLayerWhitePoints) {
614 layer.whitePointNits = layerWhitePoint;
615 layers.push_back(layer);
616 }
617 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd());
618 }
619}
620
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400621//
622// The collection of shaders cached here were found by using perfetto to record shader compiles
623// during actions that involve RenderEngine, logging the layer settings, and the shader code
624// and reproducing those settings here.
625//
626// It is helpful when debugging this to turn on
627// in SkGLRenderEngine.cpp:
628// kPrintLayerSettings = true
629// kFlushAfterEveryLayer = true
630// in external/skia/src/gpu/gl/builders/GrGLShaderStringBuilder.cpp
631// gPrintSKSL = true
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500632void Cache::primeShaderCache(SkiaRenderEngine* renderengine) {
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400633 const int previousCount = renderengine->reportShadersCompiled();
634 if (previousCount) {
635 ALOGD("%d Shaders already compiled before Cache::primeShaderCache ran\n", previousCount);
636 }
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500637
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400638 // The loop is beneficial for debugging and should otherwise be optimized out by the compiler.
639 // Adding additional bounds to the loop is useful for verifying that the size of the dst buffer
640 // does not impact the shader compilation counts by triggering different behaviors in RE/Skia.
641 for (SkSize bounds : {SkSize::Make(128, 128), /*SkSize::Make(1080, 2340)*/}) {
642 const nsecs_t timeBefore = systemTime();
643 // The dimensions should not matter, so long as we draw inside them.
644 const Rect displayRect(0, 0, bounds.fWidth, bounds.fHeight);
645 DisplaySettings display{
646 .physicalDisplay = displayRect,
647 .clip = displayRect,
648 .maxLuminance = 500,
649 .outputDataspace = kDestDataSpace,
650 };
Nathaniel Nifonga6b54232021-07-02 13:24:32 -0400651 DisplaySettings p3Display{
652 .physicalDisplay = displayRect,
653 .clip = displayRect,
654 .maxLuminance = 500,
655 .outputDataspace = kOtherDataSpace,
656 };
Mattias Simonsson5860d922023-08-16 13:47:41 +0000657 DisplaySettings p3DisplayEnhance{.physicalDisplay = displayRect,
658 .clip = displayRect,
659 .maxLuminance = 500,
660 .outputDataspace = kOtherDataSpace,
661 .dimmingStage = aidl::android::hardware::graphics::
662 composer3::DimmingStage::GAMMA_OETF,
663 .renderIntent = aidl::android::hardware::graphics::
664 composer3::RenderIntent::ENHANCE};
Mattias Simonsson65746132023-08-15 13:26:16 +0000665 DisplaySettings bt2020Display{.physicalDisplay = displayRect,
666 .clip = displayRect,
667 .maxLuminance = 500,
668 .outputDataspace = ui::Dataspace::BT2020,
669 .deviceHandlesColorTransform = true,
670 .dimmingStage = aidl::android::hardware::graphics::composer3::
671 DimmingStage::GAMMA_OETF,
672 .renderIntent = aidl::android::hardware::graphics::composer3::
673 RenderIntent::TONE_MAP_ENHANCE};
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500674
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400675 const int64_t usage = GRALLOC_USAGE_HW_RENDER | GRALLOC_USAGE_HW_TEXTURE;
Alec Mouria90a5702021-04-16 16:36:21 +0000676
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400677 sp<GraphicBuffer> dstBuffer =
Ady Abrahamd11bade2022-08-01 16:18:03 -0700678 sp<GraphicBuffer>::make(displayRect.width(), displayRect.height(),
679 PIXEL_FORMAT_RGBA_8888, 1, usage, "primeShaderCache_dst");
Nathaniel Nifongb9f27ef2021-04-01 16:44:12 -0400680
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400681 const auto dstTexture =
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800682 std::make_shared<impl::ExternalTexture>(dstBuffer, *renderengine,
683 impl::ExternalTexture::Usage::WRITEABLE);
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400684 // This buffer will be the source for the call to drawImageLayers. Draw
685 // something to it as a placeholder for what an app draws. We should draw
686 // something, but the details are not important. Make use of the shadow layer drawing step
687 // to populate it.
688 sp<GraphicBuffer> srcBuffer =
Ady Abrahamd11bade2022-08-01 16:18:03 -0700689 sp<GraphicBuffer>::make(displayRect.width(), displayRect.height(),
690 PIXEL_FORMAT_RGBA_8888, 1, usage, "drawImageLayer_src");
Alec Mouria90a5702021-04-16 16:36:21 +0000691
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800692 const auto srcTexture = std::make_shared<
693 impl::ExternalTexture>(srcBuffer, *renderengine,
694 impl::ExternalTexture::Usage::READABLE |
695 impl::ExternalTexture::Usage::WRITEABLE);
Nathaniel Nifong13491502021-06-30 17:28:29 -0400696 drawHolePunchLayer(renderengine, display, dstTexture);
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400697 drawSolidLayers(renderengine, display, dstTexture);
Mattias Simonsson5860d922023-08-16 13:47:41 +0000698 drawSolidLayers(renderengine, p3Display, dstTexture);
Mattias Simonsson65746132023-08-15 13:26:16 +0000699 drawSolidDimmedLayers(renderengine, display, dstTexture);
Nathaniel Nifong49a59582021-07-26 19:49:47 -0400700
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400701 drawShadowLayers(renderengine, display, srcTexture);
Nathaniel Nifonga6b54232021-07-02 13:24:32 -0400702 drawShadowLayers(renderengine, p3Display, srcTexture);
Nathaniel Nifongcda45e92021-06-10 15:01:42 -0400703
704 if (renderengine->supportsBackgroundBlur()) {
705 drawBlurLayers(renderengine, display, dstTexture);
706 }
707
Nathaniel Nifong73537a32021-08-06 15:07:26 -0400708 // The majority of skia shaders needed by RenderEngine are related to sampling images.
709 // These need to be generated with various source textures.
710 // Make a list of applicable sources.
711 // GRALLOC_USAGE_HW_TEXTURE should be the same as AHARDWAREBUFFER_USAGE_GPU_SAMPLED_IMAGE.
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400712 const int64_t usageExternal = GRALLOC_USAGE_HW_TEXTURE;
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400713 sp<GraphicBuffer> externalBuffer =
Ady Abrahamd11bade2022-08-01 16:18:03 -0700714 sp<GraphicBuffer>::make(displayRect.width(), displayRect.height(),
715 PIXEL_FORMAT_RGBA_8888, 1, usageExternal,
716 "primeShaderCache_external");
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400717 const auto externalTexture =
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800718 std::make_shared<impl::ExternalTexture>(externalBuffer, *renderengine,
719 impl::ExternalTexture::Usage::READABLE);
Nathaniel Nifong73537a32021-08-06 15:07:26 -0400720 std::vector<const std::shared_ptr<ExternalTexture>> textures =
721 {srcTexture, externalTexture};
Nathaniel Nifong21e021f2021-04-21 13:15:46 -0400722
Nathaniel Nifong73537a32021-08-06 15:07:26 -0400723 // Another external texture with a different pixel format triggers useIsOpaqueWorkaround.
724 // It doesn't have to be f16, but it can't be the usual 8888.
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400725 sp<GraphicBuffer> f16ExternalBuffer =
Ady Abrahamd11bade2022-08-01 16:18:03 -0700726 sp<GraphicBuffer>::make(displayRect.width(), displayRect.height(),
727 PIXEL_FORMAT_RGBA_FP16, 1, usageExternal,
728 "primeShaderCache_external_f16");
Nathaniel Nifong73537a32021-08-06 15:07:26 -0400729 // The F16 texture may not be usable on all devices, so check first that it was created.
730 status_t error = f16ExternalBuffer->initCheck();
731 if (!error) {
732 const auto f16ExternalTexture =
Vishnu Nairdbbe3852022-01-12 20:22:11 -0800733 std::make_shared<impl::ExternalTexture>(f16ExternalBuffer, *renderengine,
734 impl::ExternalTexture::Usage::READABLE);
Nathaniel Nifong73537a32021-08-06 15:07:26 -0400735 textures.push_back(f16ExternalTexture);
736 }
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400737
Nathaniel Nifong73537a32021-08-06 15:07:26 -0400738 for (auto texture : textures) {
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400739 drawImageLayers(renderengine, display, dstTexture, texture);
Mattias Simonsson5860d922023-08-16 13:47:41 +0000740
Mattias Simonsson65746132023-08-15 13:26:16 +0000741 drawImageDimmedLayers(renderengine, display, dstTexture, texture);
742 drawImageDimmedLayers(renderengine, p3Display, dstTexture, texture);
743 drawImageDimmedLayers(renderengine, bt2020Display, dstTexture, texture);
Mattias Simonsson5860d922023-08-16 13:47:41 +0000744
Nathaniel Nifongf06a45b2021-06-25 17:24:26 -0400745 // Draw layers for b/185569240.
746 drawClippedLayers(renderengine, display, dstTexture, texture);
747 }
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400748
Nathaniel Nifong13491502021-06-30 17:28:29 -0400749 drawPIPImageLayer(renderengine, display, dstTexture, externalTexture);
750
Mattias Simonsson65746132023-08-15 13:26:16 +0000751 drawTransparentImageDimmedLayers(renderengine, bt2020Display, dstTexture, externalTexture);
752 drawTransparentImageDimmedLayers(renderengine, display, dstTexture, externalTexture);
753 drawTransparentImageDimmedLayers(renderengine, p3Display, dstTexture, externalTexture);
Mattias Simonsson5860d922023-08-16 13:47:41 +0000754 drawTransparentImageDimmedLayers(renderengine, p3DisplayEnhance, dstTexture,
755 externalTexture);
Mattias Simonsson65746132023-08-15 13:26:16 +0000756
757 drawClippedDimmedImageLayers(renderengine, bt2020Display, dstTexture, externalTexture);
Mattias Simonsson458adf12023-08-16 10:49:30 +0000758 drawBT2020ClippedImageLayers(renderengine, bt2020Display, dstTexture, externalTexture);
759
760 drawBT2020ImageLayers(renderengine, bt2020Display, dstTexture, externalTexture);
761 drawBT2020ImageLayers(renderengine, p3Display, dstTexture, externalTexture);
Mattias Simonsson65746132023-08-15 13:26:16 +0000762
Mattias Simonsson5860d922023-08-16 13:47:41 +0000763 drawExtendedHDRImageLayers(renderengine, display, dstTexture, externalTexture);
764 drawExtendedHDRImageLayers(renderengine, p3Display, dstTexture, externalTexture);
765 drawExtendedHDRImageLayers(renderengine, p3DisplayEnhance, dstTexture, externalTexture);
766
767 drawP3ImageLayers(renderengine, p3DisplayEnhance, dstTexture, externalTexture);
768
Nathaniel Nifong2d2f4322021-07-22 15:17:36 -0400769 // draw one final layer synchronously to force GL submit
770 LayerSettings layer{
771 .source = PixelSource{.solidColor = half3(0.f, 0.f, 0.f)},
772 };
Sally Qi59a9f502021-10-12 18:53:23 +0000773 auto layers = std::vector<LayerSettings>{layer};
Sally Qi4cabdd02021-08-05 16:45:57 -0700774 // call get() to make it synchronous
Alec Mourif29700f2023-08-17 21:53:31 +0000775 renderengine->drawLayers(display, layers, dstTexture, base::unique_fd()).get();
Nathaniel Nifong2d2f4322021-07-22 15:17:36 -0400776
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400777 const nsecs_t timeAfter = systemTime();
778 const float compileTimeMs = static_cast<float>(timeAfter - timeBefore) / 1.0E6;
Leon Scroggins III45be9182022-04-27 10:37:11 -0400779 const int shadersCompiled = renderengine->reportShadersCompiled() - previousCount;
Derek Sollenbergere9a51082021-05-06 14:01:38 -0400780 ALOGD("Shader cache generated %d shaders in %f ms\n", shadersCompiled, compileTimeMs);
781 }
Leon Scroggins IIIb9216dc2021-03-08 17:19:01 -0500782}
783
784} // namespace android::renderengine::skia