Debian Codegraph @

src/gpu/graphite/precompile/PrecompileShader.cpp

raw
1 /*
2 * Copyright 2024 Google LLC
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "include/gpu/graphite/precompile/PrecompileShader.h"
9
10 #include "include/core/SkColorSpace.h"
11 #include "include/effects/SkRuntimeEffect.h"
12 #include "include/gpu/graphite/precompile/PrecompileBlender.h"
13 #include "include/gpu/graphite/precompile/PrecompileColorFilter.h"
14 #include "src/core/SkColorSpacePriv.h"
15 #include "src/core/SkImageInfoPriv.h"
16 #include "src/core/SkKnownRuntimeEffects.h"
17 #include "src/gpu/Blend.h"
18 #include "src/gpu/graphite/BuiltInCodeSnippetID.h"
19 #include "src/gpu/graphite/KeyContext.h"
20 #include "src/gpu/graphite/KeyHelpers.h"
21 #include "src/gpu/graphite/PaintParams.h"
22 #include "src/gpu/graphite/PaintParamsKey.h"
23 #include "src/gpu/graphite/PrecompileInternal.h"
24 #include "src/gpu/graphite/ReadSwizzle.h"
25 #include "src/gpu/graphite/RecorderPriv.h"
26 #include "src/gpu/graphite/precompile/PrecompileBaseComplete.h"
27 #include "src/gpu/graphite/precompile/PrecompileBasePriv.h"
28 #include "src/gpu/graphite/precompile/PrecompileBlenderPriv.h"
29 #include "src/gpu/graphite/precompile/PrecompileImageShader.h"
30 #include "src/gpu/graphite/precompile/PrecompileShaderPriv.h"
31 #include "src/gpu/graphite/precompile/PrecompileShadersPriv.h"
32 #include "src/shaders/gradients/SkLinearGradient.h"
33
34 #if defined(SK_DEBUG)
35 #include "src/base/SkMathPriv.h"
36 #endif
37
38 namespace skgpu::graphite {
39
40 SK_MAKE_BITMASK_OPS(PrecompileShaders::ImageShaderFlags)
41 SK_MAKE_BITMASK_OPS(PrecompileShaders::YUVImageShaderFlags)
42
43 using PrecompileShaders::GradientShaderFlags;
44 using PrecompileShaders::ImageShaderFlags;
45 using PrecompileShaders::YUVImageShaderFlags;
46
47 PrecompileShader::~PrecompileShader() = default;
48
49 sk_sp<PrecompileShader> PrecompileShader::makeWithColorFilter(
50 sk_sp<PrecompileColorFilter> cf) const {
51 if (!cf) {
52 return sk_ref_sp(this);
53 }
54
55 return PrecompileShaders::ColorFilter({{ sk_ref_sp(this) }}, {{ std::move(cf) }});
56 }
57
58 sk_sp<PrecompileShader> PrecompileShader::makeWithWorkingColorSpace(
59 sk_sp<SkColorSpace> inputCS, sk_sp<SkColorSpace> outputCS) const {
60 if (!inputCS && !outputCS) {
61 return sk_ref_sp(this);
62 }
63
64 return PrecompileShaders::WorkingColorSpaceExplicit(
65 {{ sk_ref_sp(this) }},
66 {{ { std::move(inputCS), std::move(outputCS) } }});
67 }
68
69 //--------------------------------------------------------------------------------------------------
70 class PrecompileEmptyShader final : public PrecompileShader {
71 private:
72 void addToKey(const KeyContext& keyContext, int desiredCombination) const override {
73 SkASSERT(desiredCombination == 0); // The empty shader only ever has one combination
74 keyContext.paintParamsKeyBuilder()->addBlock(BuiltInCodeSnippetID::kPriorOutput);
75 }
76 };
77
78 sk_sp<PrecompileShader> PrecompileShaders::Empty() {
79 return sk_make_sp<PrecompileEmptyShader>();
80 }
81
82 //--------------------------------------------------------------------------------------------------
83 class PrecompileColorShader final : public PrecompileShader {
84 private:
85 bool isConstant(int desiredCombination) const override {
86 SkASSERT(desiredCombination == 0); // The color shader only ever has one combination
87 return true;
88 }
89
90 void addToKey(const KeyContext& keyContext, int desiredCombination) const override {
91 SkASSERT(desiredCombination == 0); // The color shader only ever has one combination
92 // The white PMColor is just a placeholder for the actual paint params color
93 SolidColorShaderBlock::AddBlock(keyContext, SK_PMColor4fWHITE);
94 }
95 };
96
97 sk_sp<PrecompileShader> PrecompileShaders::Color() {
98 return sk_make_sp<PrecompileColorShader>();
99 }
100
101 // The colorSpace is safe to ignore - it is just applied to the color and doesn't modify the
102 // generated program.
103 sk_sp<PrecompileShader> PrecompileShaders::Color(sk_sp<SkColorSpace>) {
104 return sk_make_sp<PrecompileColorShader>();
105 }
106
107 //--------------------------------------------------------------------------------------------------
108 class PrecompileBlendShader final : public PrecompileShader {
109 public:
110 PrecompileBlendShader(PrecompileBlenderList&& blenders,
111 SkSpan<const sk_sp<PrecompileShader>> dsts,
112 SkSpan<const sk_sp<PrecompileShader>> srcs)
113 : fBlenderOptions(std::move(blenders))
114 , fDstOptions(dsts.begin(), dsts.end())
115 , fSrcOptions(srcs.begin(), srcs.end()) {
116 fNumDstCombos = 0;
117 for (const auto& d : fDstOptions) {
118 fNumDstCombos += d->priv().numCombinations();
119 }
120
121 fNumSrcCombos = 0;
122 for (const auto& s : fSrcOptions) {
123 fNumSrcCombos += s->priv().numCombinations();
124 }
125 }
126
127 private:
128 int numChildCombinations() const override {
129 return fBlenderOptions.numCombinations() * fNumDstCombos * fNumSrcCombos;
130 }
131
132 void addToKey(const KeyContext& keyContext, int desiredCombination) const override {
133 SkASSERT(desiredCombination < this->numCombinations());
134
135 const int desiredDstCombination = desiredCombination % fNumDstCombos;
136 int remainingCombinations = desiredCombination / fNumDstCombos;
137
138 const int desiredSrcCombination = remainingCombinations % fNumSrcCombos;
139 remainingCombinations /= fNumSrcCombos;
140
141 int desiredBlendCombination = remainingCombinations;
142 SkASSERT(desiredBlendCombination < fBlenderOptions.numCombinations());
143
144 auto [blender, blenderCombination] = fBlenderOptions.selectOption(desiredBlendCombination);
145 if (blender->priv().asBlendMode()) {
146 // Coefficient and HSLC blends, and other fixed SkBlendMode blenders use the
147 // BlendCompose block to organize the children.
148 BlendComposeBlock::BeginBlock(keyContext);
149 } else {
150 // Runtime blenders are wrapped in the kBlend runtime shader, although functionally
151 // it is identical to the BlendCompose snippet.
152 const SkRuntimeEffect* blendEffect =
153 GetKnownRuntimeEffect(SkKnownRuntimeEffects::StableKey::kBlend);
154
155 RuntimeEffectBlock::BeginBlock(keyContext, { sk_ref_sp(blendEffect) });
156 }
157
158 AddToKey<PrecompileShader>(keyContext, fSrcOptions, desiredSrcCombination);
159 AddToKey<PrecompileShader>(keyContext, fDstOptions, desiredDstCombination);
160
161 if (blender->priv().asBlendMode()) {
162 SkASSERT(blenderCombination == 0);
163 AddBlendMode(keyContext, *blender->priv().asBlendMode());
164 } else {
165 blender->priv().addToKey(keyContext, blenderCombination);
166 }
167
168 keyContext.paintParamsKeyBuilder()->endBlock(); // BlendComposeBlock or RuntimeEffectBlock
169 }
170
171 PrecompileBlenderList fBlenderOptions;
172 std::vector<sk_sp<PrecompileShader>> fDstOptions;
173 std::vector<sk_sp<PrecompileShader>> fSrcOptions;
174
175 int fNumDstCombos;
176 int fNumSrcCombos;
177 };
178
179 sk_sp<PrecompileShader> PrecompileShaders::Blend(
180 SkSpan<const sk_sp<PrecompileBlender>> blenders,
181 SkSpan<const sk_sp<PrecompileShader>> dsts,
182 SkSpan<const sk_sp<PrecompileShader>> srcs) {
183 return sk_make_sp<PrecompileBlendShader>(PrecompileBlenderList(blenders), dsts, srcs);
184 }
185
186 sk_sp<PrecompileShader> PrecompileShaders::Blend(
187 SkSpan<const SkBlendMode> blendModes,
188 SkSpan<const sk_sp<PrecompileShader>> dsts,
189 SkSpan<const sk_sp<PrecompileShader>> srcs) {
190 return sk_make_sp<PrecompileBlendShader>(PrecompileBlenderList(blendModes), dsts, srcs);
191 }
192
193 //--------------------------------------------------------------------------------------------------
194 class PrecompileCoordClampShader final : public PrecompileShader {
195 public:
196 PrecompileCoordClampShader(SkSpan<const sk_sp<PrecompileShader>> shaders)
197 : fShaders(shaders.begin(), shaders.end()) {
198 fNumShaderCombos = 0;
199 for (const auto& s : fShaders) {
200 fNumShaderCombos += s->priv().numCombinations();
201 }
202 }
203
204 private:
205 int numChildCombinations() const override {
206 return fNumShaderCombos;
207 }
208
209 void addToKey(const KeyContext& keyContext, int desiredCombination) const override {
210 SkASSERT(desiredCombination < fNumShaderCombos);
211
212 constexpr SkRect kIgnored { 0, 0, 256, 256 }; // ignored bc we're precompiling
213
214 // TODO: update CoordClampShaderBlock so this is optional
215 CoordClampShaderBlock::CoordClampData data(kIgnored);
216
217 CoordClampShaderBlock::BeginBlock(keyContext, data);
218 AddToKey<PrecompileShader>(keyContext, fShaders, desiredCombination);
219 keyContext.paintParamsKeyBuilder()->endBlock();
220 }
221
222 std::vector<sk_sp<PrecompileShader>> fShaders;
223 int fNumShaderCombos;
224 };
225
226 sk_sp<PrecompileShader> PrecompileShaders::CoordClamp(SkSpan<const sk_sp<PrecompileShader>> input) {
227 return sk_make_sp<PrecompileCoordClampShader>(input);
228 }
229
230 //--------------------------------------------------------------------------------------------------
231 PrecompileImageShader::PrecompileImageShader(SkEnumBitMask<ImageShaderFlags> flags,
232 SkSpan<const SkColorInfo> colorInfos,
233 SkSpan<const SkTileMode> tileModes,
234 bool raw)
235 : fNumExtraSamplingTilingCombos((flags & ImageShaderFlags::kCubicSampling)
236 ? kExtraNumSamplingTilingCombos
237 : 1) // Just kHWTiled
238 , fColorInfos(!colorInfos.empty()
239 ? std::vector<SkColorInfo>(colorInfos.begin(), colorInfos.end())
240 : raw ? RawImageDefaultColorInfos()
241 : (flags & ImageShaderFlags::kIncludeAlphaOnly)
242 ? DefaultColorInfos()
243 : NonAlphaOnlyDefaultColorInfos())
244 , fTileModes(std::vector<SkTileMode>(tileModes.begin(), tileModes.end()))
245 , fUseDstColorInfo(!colorInfos.empty())
246 , fRaw(raw) {}
247
248 void PrecompileImageShader::setImmutableSamplerInfo(const ImmutableSamplerInfo& samplerInfo) {
249 fImmutableSamplerInfo = samplerInfo;
250 }
251
252 int PrecompileImageShader::numIntrinsicCombinations() const {
253 // TODO(b/400682634) If color infos were provided by the client, and we're using the
254 // destination color space to determine what color space transform shaders to use, we can
255 // end up generating duplicate shaders, and the actual number of unique shaders generated
256 // will be less than the number calculated here.
257 return fColorInfos.size() * (fTileModes.size() + fNumExtraSamplingTilingCombos);
258 }
259
260 void PrecompileImageShader::addToKey(const KeyContext& keyContext, int desiredCombination) const {
261 SkASSERT(this->numChildCombinations() == 1);
262 SkASSERT(desiredCombination < this->numIntrinsicCombinations());
263
264 const int numSamplingTilingCombos = fTileModes.size() + fNumExtraSamplingTilingCombos;
265 const int desiredSamplingTilingCombo = desiredCombination % numSamplingTilingCombos;
266 desiredCombination /= numSamplingTilingCombos;
267
268 const int desiredColorInfo = desiredCombination;
269 SkASSERT(desiredColorInfo < static_cast<int>(fColorInfos.size()));
270
271 static constexpr SkSamplingOptions kDefaultCubicSampling(SkCubicResampler::Mitchell());
272 // This is kLinear to work around b/417429187
273 static constexpr SkSamplingOptions kDefaultSampling(SkFilterMode::kLinear);
274
275 // ImageShaderBlock will use hardware tiling when the subset covers the entire image, so we
276 // create subset + image size combinations where subset == imgSize (for a shader that uses
277 // hardware tiling) and subset < imgSize (for a shader that does shader-based tiling).
278 static constexpr SkRect kSubset = SkRect::MakeWH(1.0f, 1.0f);
279 static constexpr SkISize kHWTileableSize = SkISize::Make(1, 1);
280 static constexpr SkISize kShaderTileableSize = SkISize::Make(2, 2);
281
282 const int numTileModes = fTileModes.size();
283 const SkTileMode tileMode = (desiredSamplingTilingCombo < numTileModes)
284 ? fTileModes[desiredSamplingTilingCombo]
285 : SkTileMode::kClamp;
286 const SkISize imgSize = (desiredSamplingTilingCombo >= numTileModes &&
287 desiredSamplingTilingCombo - numTileModes == kHWTiled)
288 ? kHWTileableSize
289 : kShaderTileableSize;
290 const SkSamplingOptions sampling =
291 (desiredSamplingTilingCombo >= numTileModes &&
292 desiredSamplingTilingCombo - numTileModes == kCubicSampled)
293 ? kDefaultCubicSampling
294 : kDefaultSampling;
295
296 const ImageShaderBlock::ImageData imgData(sampling, tileMode, tileMode, imgSize, kSubset,
297 fImmutableSamplerInfo);
298
299 const SkColorInfo& colorInfo = fColorInfos[desiredColorInfo];
300 const bool alphaOnly = SkColorTypeIsAlphaOnly(colorInfo.colorType());
301
302 const Caps* caps = keyContext.caps();
303 Swizzle readSwizzle = caps->getReadSwizzle(
304 colorInfo.colorType(),
305 caps->getDefaultSampledTextureInfo(
306 colorInfo.colorType(), Mipmapped::kNo, Protected::kNo, Renderable::kNo));
307 if (alphaOnly) {
308 readSwizzle = Swizzle::Concat(readSwizzle, Swizzle("000a"));
309 }
310
311 ColorSpaceTransformBlock::ColorSpaceTransformData colorXformData(
312 SwizzleClassToReadEnum(readSwizzle));
313
314 if (!fRaw) {
315 const SkColorSpace* dstColorSpace = sk_srgb_singleton();
316 SkAlphaType dstAT = colorInfo.alphaType();
317 if (fUseDstColorInfo) {
318 dstColorSpace = keyContext.dstColorInfo().colorSpace();
319 dstAT = keyContext.dstColorInfo().alphaType();
320 }
321 colorXformData.fSteps = SkColorSpaceXformSteps(
322 colorInfo.colorSpace(), colorInfo.alphaType(),
323 dstColorSpace, dstAT);
324
325 if (alphaOnly) {
326 Blend(keyContext,
327 /* addBlendToKey= */ [&] () -> void {
328 AddFixedBlendMode(keyContext, SkBlendMode::kDstIn);
329 },
330 /* addSrcToKey= */ [&] () -> void {
331 Compose(keyContext,
332 /* addInnerToKey= */ [&]() -> void {
333 ImageShaderBlock::AddBlock(keyContext, imgData);
334 },
335 /* addOuterToKey= */ [&]() -> void {
336 ColorSpaceTransformBlock::AddBlock(keyContext, colorXformData);
337 });
338 },
339 /* addDstToKey= */ [&]() -> void {
340 RGBPaintColorBlock::AddBlock(keyContext);
341 });
342 return;
343 }
344 }
345
346 Compose(keyContext,
347 /* addInnerToKey= */ [&]() -> void {
348 ImageShaderBlock::AddBlock(keyContext, imgData);
349 },
350 /* addOuterToKey= */ [&]() -> void {
351 ColorSpaceTransformBlock::AddBlock(keyContext, colorXformData);
352 });
353 }
354
355 sk_sp<PrecompileShader> PrecompileShaders::Image(ImageShaderFlags shaderFlags,
356 SkSpan<const SkColorInfo> colorInfos,
357 SkSpan<const SkTileMode> tileModes) {
358 return PrecompileShaders::LocalMatrix(
359 {{ sk_make_sp<PrecompileImageShader>(shaderFlags,
360 colorInfos, tileModes,
361 /* raw= */false) }});
362 }
363
364 sk_sp<PrecompileShader> PrecompileShaders::Image(SkSpan<const SkColorInfo> colorInfos,
365 SkSpan<const SkTileMode> tileModes) {
366 return Image(ImageShaderFlags::kAll, colorInfos, tileModes);
367 }
368
369 sk_sp<PrecompileShader> PrecompileShaders::RawImage(ImageShaderFlags shaderFlags,
370 SkSpan<const SkColorInfo> colorInfos,
371 SkSpan<const SkTileMode> tileModes) {
372 SkEnumBitMask<ImageShaderFlags> newFlags = ~ImageShaderFlags::kCubicSampling & shaderFlags;
373 return PrecompileShaders::LocalMatrix(
374 {{ sk_make_sp<PrecompileImageShader>(newFlags,
375 colorInfos, tileModes,
376 /* raw= */true) }});
377 }
378
379 //--------------------------------------------------------------------------------------------------
380 class PrecompileYUVImageShader : public PrecompileShader {
381 public:
382 PrecompileYUVImageShader(SkEnumBitMask<YUVImageShaderFlags> shaderFlags,
383 SkSpan<const SkColorInfo> colorInfos)
384 : fColorInfos(!colorInfos.empty()
385 ? std::vector<SkColorInfo>(colorInfos.begin(), colorInfos.end())
386 : PrecompileImageShader::NonAlphaOnlyDefaultColorInfos())
387 , fUseDstColorSpace(!colorInfos.empty()) {
388 this->setupTilingModes(shaderFlags);
389 }
390
391 private:
392 // There are 4 possible tiling modes:
393 // non-cubic shader tiling
394 // HW tiling w/o swizzle
395 // HW tiling w/ swizzle
396 // cubic shader tiling -- can be omitted
397 inline static constexpr int kMaxTilingModes = 4;
398
399 inline static constexpr int kShaderTiled = 0;
400 inline static constexpr int kHWTiledNoSwizzle = 1;
401 inline static constexpr int kHWTiledWithSwizzle = 2;
402 inline static constexpr int kCubicShaderTiled = 3;
403
404 void setupTilingModes(SkEnumBitMask<YUVImageShaderFlags> flags) {
405 fNumTilingModes = 0;
406
407 if (flags & YUVImageShaderFlags::kHardwareSamplingNoSwizzle) {
408 fTilingModes[fNumTilingModes++] = kHWTiledNoSwizzle;
409 }
410 if (flags & YUVImageShaderFlags::kHardwareSampling) {
411 fTilingModes[fNumTilingModes++] = kHWTiledWithSwizzle;
412 }
413 if (flags & YUVImageShaderFlags::kShaderBasedSampling) {
414 fTilingModes[fNumTilingModes++] = kShaderTiled;
415 }
416 if (flags & YUVImageShaderFlags::kCubicSampling) {
417 fTilingModes[fNumTilingModes++] = kCubicShaderTiled;
418 }
419
420 SkASSERT(fNumTilingModes == SkPopCount(flags.value()));
421 SkASSERT(fNumTilingModes <= kMaxTilingModes);
422 }
423
424 int numIntrinsicCombinations() const override {
425 return fNumTilingModes * fColorInfos.size();
426 }
427
428 void addToKey(const KeyContext& keyContext, int desiredCombination) const override {
429 SkASSERT(desiredCombination < this->numIntrinsicCombinations());
430
431 int desiredTiling = desiredCombination % fNumTilingModes;
432 desiredCombination /= fNumTilingModes;
433
434 int desiredColorInfo = desiredCombination;
435 SkASSERT(desiredColorInfo < static_cast<int>(fColorInfos.size()));
436
437 static constexpr SkSamplingOptions kDefaultCubicSampling(SkCubicResampler::Mitchell());
438 static constexpr SkSamplingOptions kDefaultSampling;
439
440 YUVImageShaderBlock::ImageData imgData(
441 fTilingModes[desiredTiling] == kCubicShaderTiled
442 ? kDefaultCubicSampling
443 : kDefaultSampling,
444 SkTileMode::kClamp,
445 fTilingModes[desiredTiling] == kShaderTiled ? SkTileMode::kRepeat
446 : SkTileMode::kClamp,
447 /* imgSize= */ { 1, 1 },
448 /* subset= */ fTilingModes[desiredTiling] == kShaderTiled
449 ? SkRect::MakeEmpty()
450 : SkRect::MakeWH(1, 1));
451
452 static constexpr SkV4 kRedChannel{ 1.f, 0.f, 0.f, 0.f };
453 imgData.fChannelSelect[0] = kRedChannel;
454 imgData.fChannelSelect[1] = kRedChannel;
455 if (fTilingModes[desiredTiling] == kHWTiledNoSwizzle) {
456 imgData.fChannelSelect[2] = kRedChannel;
457 } else {
458 // Having a non-red channel selector forces a swizzle
459 imgData.fChannelSelect[2] = { 0.f, 1.f, 0.f, 0.f};
460 }
461 imgData.fChannelSelect[3] = kRedChannel;
462
463 imgData.fYUVtoRGBMatrix.setAll(1, 0, 0, 0, 1, 0, 0, 0, 0);
464 imgData.fYUVtoRGBTranslate = { 0, 0, 0 };
465
466 const SkColorInfo& colorInfo = fColorInfos[desiredColorInfo];
467
468 const Caps* caps = keyContext.caps();
469 Swizzle readSwizzle = caps->getReadSwizzle(
470 colorInfo.colorType(),
471 caps->getDefaultSampledTextureInfo(
472 colorInfo.colorType(), Mipmapped::kNo, Protected::kNo, Renderable::kNo));
473 ColorSpaceTransformBlock::ColorSpaceTransformData colorXformData(
474 SwizzleClassToReadEnum(readSwizzle));
475
476 const SkColorSpace* dstColorSpace = fUseDstColorSpace
477 ? keyContext.dstColorInfo().colorSpace()
478 : sk_srgb_singleton();
479 colorXformData.fSteps = SkColorSpaceXformSteps(
480 colorInfo.colorSpace(), colorInfo.alphaType(),
481 dstColorSpace, colorInfo.alphaType());
482
483 Compose(keyContext,
484 /* addInnerToKey= */ [&]() -> void {
485 YUVImageShaderBlock::AddBlock(keyContext, imgData);
486 },
487 /* addOuterToKey= */ [&]() -> void {
488 ColorSpaceTransformBlock::AddBlock(keyContext, colorXformData);
489 });
490 }
491
492 const std::vector<SkColorInfo> fColorInfos;
493
494 // If true, use the destination color space from the KeyContext provided to addToKey.
495 // This is true if and only if the client has provided a list of color infos. Otherwise, we
496 // always use an sRGB destination per the default SkColorInfo lists defined in
497 // PrecompileImageShader::DefaultColorInfos.
498 const bool fUseDstColorSpace;
499 int fNumTilingModes;
500 int fTilingModes[kMaxTilingModes];
501 };
502
503 sk_sp<PrecompileShader> PrecompileShaders::YUVImage(YUVImageShaderFlags shaderFlags,
504 SkSpan<const SkColorInfo> colorInfos) {
505 return PrecompileShaders::LocalMatrix(
506 {{ sk_make_sp<PrecompileYUVImageShader>(shaderFlags, colorInfos) }});
507 }
508
509 //--------------------------------------------------------------------------------------------------
510 class PrecompilePerlinNoiseShader final : public PrecompileShader {
511 public:
512 PrecompilePerlinNoiseShader() {}
513
514 private:
515 void addToKey(const KeyContext& keyContext, int desiredCombination) const override {
516
517 SkASSERT(desiredCombination == 0); // The Perlin noise shader only ever has one combination
518
519 // TODO: update PerlinNoiseShaderBlock so the NoiseData is optional
520 static const PerlinNoiseShaderBlock::PerlinNoiseData kIgnoredNoiseData(
521 PerlinNoiseShaderBlock::Type::kFractalNoise, { 0.0f, 0.0f }, 2, {1, 1});
522
523 PerlinNoiseShaderBlock::AddBlock(keyContext, kIgnoredNoiseData);
524 }
525
526 };
527
528 sk_sp<PrecompileShader> PrecompileShaders::MakeFractalNoise() {
529 return sk_make_sp<PrecompilePerlinNoiseShader>();
530 }
531
532 sk_sp<PrecompileShader> PrecompileShaders::MakeTurbulence() {
533 return sk_make_sp<PrecompilePerlinNoiseShader>();
534 }
535
536 namespace {
537
538 sk_sp<SkColorSpace> get_gradient_intermediate_cs(SkColorSpace* dstColorSpace,
539 SkGradient::Interpolation interpolation) {
540 // Any gradient shader will do, as long as it has the correct interpolation settings.
541 constexpr SkPoint pts[2] = {{0.f, 0.f}, {1.f, 0.f}};
542 constexpr SkColor4f colors[2] = {SkColors::kBlack, SkColors::kWhite};
543 constexpr float pos[2] = {0.f, 1.f};
544 SkLinearGradient shader(pts, {{colors, pos, SkTileMode::kClamp, nullptr}, interpolation});
545
546 SkColor4fXformer xformedColors(&shader, dstColorSpace);
547 return xformedColors.fIntermediateColorSpace;
548 }
549
550 } // anonymous namespace
551
552 //--------------------------------------------------------------------------------------------------
553 class PrecompileGradientShader final : public PrecompileShader {
554 public:
555 PrecompileGradientShader(SkShaderBase::GradientType type,
556 SkEnumBitMask<GradientShaderFlags> flags,
557 const SkGradient::Interpolation& interpolation)
558 : fType(type)
559 , fInterpolation(interpolation) {
560 this->setupStopVariants(flags);
561 }
562
563 private:
564 /*
565 * The gradients can have up to three specializations based on the number of stops.
566 */
567 inline static constexpr int kMaxStopVariants = 3;
568
569 void setupStopVariants(SkEnumBitMask<GradientShaderFlags> flags) {
570 fNumStopVariants = 0;
571
572 if (flags & GradientShaderFlags::kSmall) {
573 fStopVariants[fNumStopVariants++] = 4;
574 }
575 if (flags & GradientShaderFlags::kMedium) {
576 fStopVariants[fNumStopVariants++] = 8;
577 }
578 if (flags & GradientShaderFlags::kLarge) {
579 fStopVariants[fNumStopVariants++] =
580 GradientShaderBlocks::GradientData::kNumInternalStorageStops+1;
581 }
582
583 SkASSERT(fNumStopVariants == SkPopCount(flags.value()));
584 SkASSERT(fNumStopVariants <= kMaxStopVariants);
585 }
586
587 int numIntrinsicCombinations() const override { return fNumStopVariants; }
588
589 void addToKey(const KeyContext& keyContext, int desiredCombination) const override {
590 SkASSERT(this->numChildCombinations() == 1);
591 SkASSERT(desiredCombination < fNumStopVariants);
592
593 bool useStorageBuffer = keyContext.caps()->gradientBufferSupport();
594
595 GradientShaderBlocks::GradientData gradData(fType,
596 fStopVariants[desiredCombination],
597 useStorageBuffer);
598
599 // The logic for setting up color spaces here should match that in the "add_gradient_to_key"
600 // functions from src/gpu/graphite/KeyHelpers.cpp.
601 sk_sp<SkColorSpace> intermediateCS = get_gradient_intermediate_cs(
602 keyContext.dstColorInfo().colorSpace(), fInterpolation);
603 const SkColorSpace* dstCS = keyContext.dstColorInfo().colorSpace()
604 ? keyContext.dstColorInfo().colorSpace()
605 : sk_srgb_singleton();
606
607 ColorSpaceTransformBlock::ColorSpaceTransformData csData(
608 intermediateCS.get(), kPremul_SkAlphaType,
609 dstCS, kPremul_SkAlphaType);
610
611 Compose(keyContext,
612 /* addInnerToKey= */ [&]() -> void {
613 GradientShaderBlocks::AddBlock(keyContext, gradData);
614 },
615 /* addOuterToKey= */ [&]() -> void {
616 ColorSpaceTransformBlock::AddBlock(keyContext, csData);
617 });
618 }
619
620 const SkShaderBase::GradientType fType;
621 const SkGradient::Interpolation fInterpolation;
622
623 int fNumStopVariants = 0;
624 int fStopVariants[kMaxStopVariants];
625 };
626
627 sk_sp<PrecompileShader> PrecompileShaders::LinearGradient(
628 GradientShaderFlags flags,
629 SkGradient::Interpolation interpolation) {
630 sk_sp<PrecompileShader> s = sk_make_sp<PrecompileGradientShader>(
631 SkShaderBase::GradientType::kLinear, flags, interpolation);
632 return PrecompileShaders::LocalMatrix({{ std::move(s) }});
633 }
634
635 sk_sp<PrecompileShader> PrecompileShaders::RadialGradient(
636 GradientShaderFlags flags,
637 SkGradient::Interpolation interpolation) {
638 sk_sp<PrecompileShader> s = sk_make_sp<PrecompileGradientShader>(
639 SkShaderBase::GradientType::kRadial, flags, interpolation);
640 return PrecompileShaders::LocalMatrix({{ std::move(s) }});
641 }
642
643 sk_sp<PrecompileShader> PrecompileShaders::SweepGradient(
644 GradientShaderFlags flags,
645 SkGradient::Interpolation interpolation) {
646 sk_sp<PrecompileShader> s = sk_make_sp<PrecompileGradientShader>(
647 SkShaderBase::GradientType::kSweep, flags, interpolation);
648 return PrecompileShaders::LocalMatrix({{ std::move(s) }});
649 }
650
651 sk_sp<PrecompileShader> PrecompileShaders::TwoPointConicalGradient(
652 GradientShaderFlags flags,
653 SkGradient::Interpolation interpolation) {
654 sk_sp<PrecompileShader> s = sk_make_sp<PrecompileGradientShader>(
655 SkShaderBase::GradientType::kConical, flags, interpolation);
656 return PrecompileShaders::LocalMatrix({{ std::move(s) }});
657 }
658
659 //--------------------------------------------------------------------------------------------------
660 // The PictureShader ultimately turns into an SkImageShader optionally wrapped in a
661 // LocalMatrixShader.
662 // Note that this means each precompile PictureShader will add 24 combinations:
663 // 2 (pictureshader LM) x 12 (imageShader variations)
664 sk_sp<PrecompileShader> PrecompileShaders::Picture() {
665 // Note: We don't need to consider the PrecompileYUVImageShader since the image
666 // being drawn was created internally by Skia (as non-YUV).
667 return PrecompileShadersPriv::LocalMatrixBothVariants({{ PrecompileShaders::Image() }});
668 }
669
670 sk_sp<PrecompileShader> PrecompileShadersPriv::Picture(bool withLM) {
671 sk_sp<PrecompileShader> s = PrecompileShaders::Image();
672 if (withLM) {
673 return PrecompileShaders::LocalMatrix({{ std::move(s) }});
674 }
675 return s;
676 }
677
678 //--------------------------------------------------------------------------------------------------
679 // In the main Skia API the SkLocalMatrixShader is optimized away when the LM is the identity
680 // or omitted. The PrecompileLocalMatrixShader captures this by adding two intrinsic options.
681 // One with the LMShader wrapping the child and one without the LMShader.
682 class PrecompileLocalMatrixShader final : public PrecompileShader {
683 public:
684 enum class Flags {
685 kNone = 0b00,
686 kIsPerspective = 0b01,
687 kIncludeWithOutVariant = 0b10,
688 };
689
690 PrecompileLocalMatrixShader(SkSpan<const sk_sp<PrecompileShader>> wrapped,
691 SkEnumBitMask<Flags> flags = Flags::kNone)
692 : fWrapped(wrapped.begin(), wrapped.end())
693 , fFlags(flags) {
694 fNumWrappedCombos = 0;
695 for (const auto& s : fWrapped) {
696 fNumWrappedCombos += s->priv().numCombinations();
697 }
698 }
699
700 bool isConstant(int desiredCombination) const override {
701 SkASSERT(desiredCombination < this->numCombinations());
702
703 /*
704 * Regardless of whether the LocalMatrixShader elides itself or not, we always want
705 * the Constant-ness of the wrapped shader.
706 */
707 int desiredWrappedCombination = desiredCombination / kNumIntrinsicCombinations;
708 SkASSERT(desiredWrappedCombination < fNumWrappedCombos);
709
710 std::pair<sk_sp<PrecompileShader>, int> wrapped =
711 PrecompileBase::SelectOption(SkSpan(fWrapped), desiredWrappedCombination);
712 if (wrapped.first) {
713 return wrapped.first->priv().isConstant(wrapped.second);
714 }
715
716 return false;
717 }
718
719 SkSpan<const sk_sp<PrecompileShader>> getWrapped() const {
720 return fWrapped;
721 }
722
723 SkEnumBitMask<Flags> getFlags() const { return fFlags; }
724
725 private:
726 // The LocalMatrixShader has two potential variants: with and without the LocalMatrixShader
727 // In the "with" variant, the kIsPerspective flag will determine if the shader performs
728 // the perspective division or not.
729 inline static constexpr int kNumIntrinsicCombinations = 2;
730 inline static constexpr int kWithLocalMatrix = 1;
731 inline static constexpr int kWithoutLocalMatrix = 0;
732
733 bool isALocalMatrixShader() const override { return true; }
734
735 int numIntrinsicCombinations() const override {
736 if (!(fFlags & Flags::kIncludeWithOutVariant)) {
737 return 1; // just kWithLocalMatrix
738 }
739 return kNumIntrinsicCombinations;
740 }
741
742 int numChildCombinations() const override { return fNumWrappedCombos; }
743
744 void addToKey(const KeyContext& keyContext, int desiredCombination) const override {
745 SkASSERT(desiredCombination < this->numCombinations());
746
747 int desiredLMCombination, desiredWrappedCombination;
748
749 if (!(fFlags & Flags::kIncludeWithOutVariant)) {
750 desiredLMCombination = kWithLocalMatrix;
751 desiredWrappedCombination = desiredCombination;
752 } else {
753 desiredLMCombination = desiredCombination % kNumIntrinsicCombinations;
754 desiredWrappedCombination = desiredCombination / kNumIntrinsicCombinations;
755 }
756 SkASSERT(desiredWrappedCombination < fNumWrappedCombos);
757
758 if (desiredLMCombination == kWithLocalMatrix) {
759 SkMatrix matrix = SkMatrix::I();
760 if (fFlags & Flags::kIsPerspective) {
761 matrix.setPerspX(0.1f);
762 }
763 LocalMatrixShaderBlock::LMShaderData lmShaderData(matrix);
764
765 LocalMatrixShaderBlock::BeginBlock(keyContext, matrix);
766 }
767
768 AddToKey<PrecompileShader>(keyContext, fWrapped, desiredWrappedCombination);
769
770 if (desiredLMCombination == kWithLocalMatrix) {
771 keyContext.paintParamsKeyBuilder()->endBlock();
772 }
773 }
774
775 std::vector<sk_sp<PrecompileShader>> fWrapped;
776 int fNumWrappedCombos;
777 SkEnumBitMask<Flags> fFlags;
778 };
779
780 sk_sp<PrecompileShader> PrecompileShaders::LocalMatrix(
781 SkSpan<const sk_sp<PrecompileShader>> wrapped,
782 bool isPerspective) {
783 return sk_make_sp<PrecompileLocalMatrixShader>(
784 std::move(wrapped),
785 isPerspective ? PrecompileLocalMatrixShader::Flags::kIsPerspective
786 : PrecompileLocalMatrixShader::Flags::kNone);
787 }
788
789 sk_sp<PrecompileShader> PrecompileShadersPriv::LocalMatrixBothVariants(
790 SkSpan<const sk_sp<PrecompileShader>> wrapped) {
791 return sk_make_sp<PrecompileLocalMatrixShader>(
792 std::move(wrapped),
793 PrecompileLocalMatrixShader::Flags::kIncludeWithOutVariant);
794 }
795
796 sk_sp<PrecompileShader> PrecompileShader::makeWithLocalMatrix(bool isPerspective) const {
797 if (this->priv().isALocalMatrixShader()) {
798 // SkShader::makeWithLocalMatrix collapses chains of localMatrix shaders so we need to
799 // follow suit here, folding in any new perspective flag if needed.
800 auto thisAsLMShader = static_cast<const PrecompileLocalMatrixShader*>(this);
801 if (isPerspective && !(thisAsLMShader->getFlags() &
802 PrecompileLocalMatrixShader::Flags::kIsPerspective)) {
803 return sk_make_sp<PrecompileLocalMatrixShader>(
804 thisAsLMShader->getWrapped(),
805 thisAsLMShader->getFlags() | PrecompileLocalMatrixShader::Flags::kIsPerspective);
806 }
807
808 return sk_ref_sp(this);
809 }
810
811 return PrecompileShaders::LocalMatrix({{ sk_ref_sp(this) }}, isPerspective);
812 }
813
814 //--------------------------------------------------------------------------------------------------
815 class PrecompileColorFilterShader final : public PrecompileShader {
816 public:
817 PrecompileColorFilterShader(SkSpan<const sk_sp<PrecompileShader>> shaders,
818 SkSpan<const sk_sp<PrecompileColorFilter>> colorFilters)
819 : fShaders(shaders.begin(), shaders.end())
820 , fColorFilters(colorFilters.begin(), colorFilters.end()) {
821 fNumShaderCombos = 0;
822 for (const auto& s : fShaders) {
823 fNumShaderCombos += s->priv().numCombinations();
824 }
825 fNumColorFilterCombos = 0;
826 for (const auto& cf : fColorFilters) {
827 fNumColorFilterCombos += cf->priv().numCombinations();
828 }
829 }
830
831 private:
832 int numChildCombinations() const override { return fNumShaderCombos * fNumColorFilterCombos; }
833
834 void addToKey(const KeyContext& keyContext, int desiredCombination) const override {
835 SkASSERT(desiredCombination < this->numCombinations());
836
837 int desiredShaderCombination = desiredCombination % fNumShaderCombos;
838 int desiredColorFilterCombination = desiredCombination / fNumShaderCombos;
839 SkASSERT(desiredColorFilterCombination < fNumColorFilterCombos);
840
841 Compose(keyContext,
842 /* addInnerToKey= */ [&]() -> void {
843 AddToKey<PrecompileShader>(keyContext, fShaders, desiredShaderCombination);
844 },
845 /* addOuterToKey= */ [&]() -> void {
846 AddToKey<PrecompileColorFilter>(keyContext, fColorFilters,
847 desiredColorFilterCombination);
848 });
849 }
850
851 std::vector<sk_sp<PrecompileShader>> fShaders;
852 std::vector<sk_sp<PrecompileColorFilter>> fColorFilters;
853 int fNumShaderCombos;
854 int fNumColorFilterCombos;
855 };
856
857 sk_sp<PrecompileShader> PrecompileShaders::ColorFilter(
858 SkSpan<const sk_sp<PrecompileShader>> shaders,
859 SkSpan<const sk_sp<PrecompileColorFilter>> colorFilters) {
860 return sk_make_sp<PrecompileColorFilterShader>(std::move(shaders), std::move(colorFilters));
861 }
862
863 //--------------------------------------------------------------------------------------------------
864 class PrecompileWorkingColorSpaceShader final : public PrecompileShader {
865 public:
866 PrecompileWorkingColorSpaceShader(SkSpan<const sk_sp<PrecompileShader>> shaders,
867 SkSpan<const std::pair<sk_sp<SkColorSpace>,
868 sk_sp<SkColorSpace>>> colorSpaces)
869 : fShaders(shaders.begin(), shaders.end())
870 , fColorSpaces(colorSpaces.begin(), colorSpaces.end()) {
871 if (colorSpaces.empty()) {
872 fColorSpaces.push_back({nullptr, nullptr}); // encode identity
873 }
874 this->updateNumShaderCombos();
875 }
876
877 PrecompileWorkingColorSpaceShader(SkSpan<const sk_sp<PrecompileShader>> shaders,
878 SkSpan<const sk_sp<SkColorSpace>> inputSpaces,
879 SkSpan<const sk_sp<SkColorSpace>> outputSpaces)
880 : fShaders(shaders.begin(), shaders.end()) {
881 static const sk_sp<SkColorSpace> kNullCS;
882 SkSpan<const sk_sp<SkColorSpace>> nullSpan{&kNullCS, 1};
883 if (inputSpaces.empty()) { inputSpaces = nullSpan; }
884 if (outputSpaces.empty()) { outputSpaces = nullSpan; }
885
886 fColorSpaces.reserve(inputSpaces.size() * outputSpaces.size());
887 for (const sk_sp<SkColorSpace>& iCS : inputSpaces) {
888 for (const sk_sp<SkColorSpace>& oCS : outputSpaces) {
889 fColorSpaces.push_back({iCS, oCS});
890 }
891 }
892
893 this->updateNumShaderCombos();
894 }
895
896 private:
897 int numChildCombinations() const override { return fNumShaderCombos * fColorSpaces.size(); }
898
899 void addToKey(const KeyContext& keyContext, int desiredCombination) const override {
900 SkASSERT(desiredCombination < this->numCombinations());
901
902 int desiredShaderCombination = desiredCombination % fNumShaderCombos;
903 int desiredColorSpaceCombination = desiredCombination / fNumShaderCombos;
904 SkASSERT(desiredColorSpaceCombination < (int) fColorSpaces.size());
905
906 // Check for an identity working colorspace (that is detected up front with
907 // makeWithColorSpace, but due to return type mismatches, can't be handled as easily with
908 // the WorkingColorSpace() factory).
909 if (!fColorSpaces[desiredColorSpaceCombination].first &&
910 !fColorSpaces[desiredColorSpaceCombination].second) {
911 // So just add the desired shader direction
912 AddToKey<PrecompileShader>(keyContext, fShaders, desiredShaderCombination);
913 return;
914 }
915
916 const SkColorInfo& dstInfo = keyContext.dstColorInfo();
917 const SkAlphaType dstAT = dstInfo.alphaType();
918 sk_sp<SkColorSpace> dstCS = dstInfo.refColorSpace();
919 if (!dstCS) {
920 dstCS = SkColorSpace::MakeSRGB();
921 }
922
923 sk_sp<SkColorSpace> inputCS = fColorSpaces[desiredColorSpaceCombination].first;
924 if (!inputCS) {
925 inputCS = dstCS;
926 }
927 sk_sp<SkColorSpace> outputCS = fColorSpaces[desiredColorSpaceCombination].second;
928 if (!outputCS) {
929 outputCS = inputCS;
930 }
931
932 // SkWorkingColorSpaceShader's workInUnpremul is not exposed yet in the public API so
933 // precompile can assume that it'll always use dstAT.
934 const SkAlphaType workingAT = dstAT;
935 KeyContext workingContext =
936 keyContext.withColorInfo({dstInfo.colorType(), workingAT, inputCS});
937
938 Compose(keyContext,
939 /* addInnerToKey= */ [&]() -> void {
940 AddToKey<PrecompileShader>(workingContext, fShaders, desiredShaderCombination);
941 },
942 /* addOuterToKey= */ [&]() -> void {
943 ColorSpaceTransformBlock::ColorSpaceTransformData data(
944 outputCS.get(), workingAT, dstCS.get(), dstAT);
945 ColorSpaceTransformBlock::AddBlock(keyContext, data);
946 });
947 }
948
949 void updateNumShaderCombos() {
950 fNumShaderCombos = 0;
951 for (const auto& s : fShaders) {
952 fNumShaderCombos += s->priv().numCombinations();
953 }
954 }
955
956 std::vector<sk_sp<PrecompileShader>> fShaders;
957 std::vector<std::pair</*input =*/sk_sp<SkColorSpace>,
958 /*output=*/sk_sp<SkColorSpace>>> fColorSpaces;
959 int fNumShaderCombos;
960 };
961
962 sk_sp<PrecompileShader> PrecompileShaders::WorkingColorSpace(
963 SkSpan<const sk_sp<PrecompileShader>> shaders,
964 SkSpan<const sk_sp<SkColorSpace>> inputSpaces,
965 SkSpan<const sk_sp<SkColorSpace>> outputSpaces) {
966 return sk_make_sp<PrecompileWorkingColorSpaceShader>(std::move(shaders),
967 std::move(inputSpaces),
968 std::move(outputSpaces));
969 }
970
971 sk_sp<PrecompileShader> PrecompileShaders::WorkingColorSpaceExplicit(
972 SkSpan<const sk_sp<PrecompileShader>> shaders,
973 SkSpan<const std::pair</*input =*/sk_sp<SkColorSpace>,
974 /*output=*/sk_sp<SkColorSpace>>> inputAndOutputSpaces) {
975 return sk_make_sp<PrecompileWorkingColorSpaceShader>(std::move(shaders),
976 std::move(inputAndOutputSpaces));
977 }
978
979 //--------------------------------------------------------------------------------------------------
980 // In Graphite this acts as a non-elidable LocalMatrixShader
981 class PrecompileCTMShader final : public PrecompileShader {
982 public:
983 PrecompileCTMShader(SkSpan<const sk_sp<PrecompileShader>> wrapped)
984 : fWrapped(wrapped.begin(), wrapped.end()) {
985 fNumWrappedCombos = 0;
986 for (const auto& s : fWrapped) {
987 fNumWrappedCombos += s->priv().numCombinations();
988 }
989 }
990
991 bool isConstant(int desiredCombination) const override {
992 SkASSERT(desiredCombination < fNumWrappedCombos);
993
994 std::pair<sk_sp<PrecompileShader>, int> wrapped =
995 PrecompileBase::SelectOption(SkSpan(fWrapped), desiredCombination);
996 if (wrapped.first) {
997 return wrapped.first->priv().isConstant(wrapped.second);
998 }
999
1000 return false;
1001 }
1002
1003 private:
1004 int numChildCombinations() const override { return fNumWrappedCombos; }
1005
1006 void addToKey(const KeyContext& keyContext, int desiredCombination) const override {
1007 SkASSERT(desiredCombination < fNumWrappedCombos);
1008
1009 LocalMatrixShaderBlock::LMShaderData kIgnoredLMShaderData(SkMatrix::I());
1010
1011 LocalMatrixShaderBlock::BeginBlock(keyContext, kIgnoredLMShaderData);
1012
1013 AddToKey<PrecompileShader>(keyContext, fWrapped, desiredCombination);
1014
1015 keyContext.paintParamsKeyBuilder()->endBlock();
1016 }
1017
1018 std::vector<sk_sp<PrecompileShader>> fWrapped;
1019 int fNumWrappedCombos;
1020 };
1021
1022 sk_sp<PrecompileShader> PrecompileShadersPriv::CTM(SkSpan<const sk_sp<PrecompileShader>> wrapped) {
1023 return sk_make_sp<PrecompileCTMShader>(std::move(wrapped));
1024 }
1025
1026 //--------------------------------------------------------------------------------------------------
1027 class PrecompileBlurShader final : public PrecompileShader {
1028 public:
1029 PrecompileBlurShader(sk_sp<PrecompileShader> wrapped)
1030 : fWrapped(std::move(wrapped)) {
1031 fNumWrappedCombos = fWrapped->priv().numCombinations();
1032 }
1033
1034 private:
1035 // 6 known 1D blur effects + 6 known 2D blur effects
1036 inline static constexpr int kNumIntrinsicCombinations = 12;
1037
1038 int numIntrinsicCombinations() const override { return kNumIntrinsicCombinations; }
1039
1040 int numChildCombinations() const override { return fNumWrappedCombos; }
1041
1042 void addToKey(const KeyContext& keyContext, int desiredCombination) const override {
1043 SkASSERT(desiredCombination < this->numCombinations());
1044
1045 using namespace SkKnownRuntimeEffects;
1046
1047 int desiredBlurCombination = desiredCombination % kNumIntrinsicCombinations;
1048 int desiredWrappedCombination = desiredCombination / kNumIntrinsicCombinations;
1049 SkASSERT(desiredWrappedCombination < fNumWrappedCombos);
1050
1051 static const StableKey kIDs[kNumIntrinsicCombinations] = {
1052 StableKey::k1DBlur4, StableKey::k1DBlur8, StableKey::k1DBlur12,
1053 StableKey::k1DBlur16, StableKey::k1DBlur20, StableKey::k1DBlur28,
1054
1055 StableKey::k2DBlur4, StableKey::k2DBlur8, StableKey::k2DBlur12,
1056 StableKey::k2DBlur16, StableKey::k2DBlur20, StableKey::k2DBlur28,
1057 };
1058
1059 const SkRuntimeEffect* effect = GetKnownRuntimeEffect(kIDs[desiredBlurCombination]);
1060 SkASSERT(effect->children().size() == 1);
1061
1062 RuntimeEffectBlock::BeginBlock(keyContext, { sk_ref_sp(effect) });
1063 fWrapped->priv().addToKey(keyContext.forRuntimeEffect(effect, /*child=*/0),
1064 desiredWrappedCombination);
1065 keyContext.paintParamsKeyBuilder()->endBlock();
1066 }
1067
1068 sk_sp<PrecompileShader> fWrapped;
1069 int fNumWrappedCombos;
1070 };
1071
1072 sk_sp<PrecompileShader> PrecompileShadersPriv::Blur(sk_sp<PrecompileShader> wrapped) {
1073 return sk_make_sp<PrecompileBlurShader>(std::move(wrapped));
1074 }
1075
1076 //--------------------------------------------------------------------------------------------------
1077 class PrecompileMatrixConvolutionShader final : public PrecompileShader {
1078 public:
1079 PrecompileMatrixConvolutionShader(sk_sp<PrecompileShader> wrapped)
1080 : fWrapped(std::move(wrapped)) {
1081 fNumWrappedCombos = fWrapped->priv().numCombinations();
1082
1083 // When the matrix convolution ImageFilter uses a texture we know it will only ever
1084 // be SkFilterMode::kNearest and SkTileMode::kClamp.
1085 // TODO: add a PrecompileImageShaderFlags to further limit the raw image shader
1086 // combinations. Right now we're getting two combinations for the raw shader
1087 // (sk_image_shader and sk_hw_image_shader).
1088 fRawImageShader = PrecompileShaders::RawImage();
1089 fNumRawImageShaderCombos = fRawImageShader->priv().numCombinations();
1090 }
1091
1092 private:
1093 int numIntrinsicCombinations() const override {
1094 // The uniform version only has one option but the two texture-based versions will
1095 // have as many combinations as the raw image shader.
1096 return 1 + 2 * fNumRawImageShaderCombos;
1097 }
1098
1099 int numChildCombinations() const override { return fNumWrappedCombos; }
1100
1101 void addToKey(const KeyContext& keyContext, int desiredCombination) const override {
1102
1103 int desiredTextureCombination = 0;
1104
1105 const int desiredWrappedCombination = desiredCombination % fNumWrappedCombos;
1106 int remainingCombinations = desiredCombination / fNumWrappedCombos;
1107
1108 SkKnownRuntimeEffects::StableKey stableKey = SkKnownRuntimeEffects::StableKey::kInvalid;
1109 if (remainingCombinations == 0) {
1110 stableKey = SkKnownRuntimeEffects::StableKey::kMatrixConvUniforms;
1111 } else {
1112 static constexpr SkKnownRuntimeEffects::StableKey kTextureBasedStableKeys[] = {
1113 SkKnownRuntimeEffects::StableKey::kMatrixConvTexSm,
1114 SkKnownRuntimeEffects::StableKey::kMatrixConvTexLg,
1115 };
1116
1117 --remainingCombinations;
1118 stableKey = kTextureBasedStableKeys[remainingCombinations % 2];
1119 desiredTextureCombination = remainingCombinations / 2;
1120 SkASSERT(desiredTextureCombination < fNumRawImageShaderCombos);
1121 }
1122
1123 const SkRuntimeEffect* effect = GetKnownRuntimeEffect(stableKey);
1124
1125 RuntimeEffectBlock::BeginBlock(keyContext, { sk_ref_sp(effect) });
1126 fWrapped->priv().addToKey(keyContext.forRuntimeEffect(effect, /*child=*/0),
1127 desiredWrappedCombination);
1128 if (stableKey != SkKnownRuntimeEffects::StableKey::kMatrixConvUniforms) {
1129 SkASSERT(effect->children().size() == 2);
1130 fRawImageShader->priv().addToKey(keyContext.forRuntimeEffect(effect, /*child=*/1),
1131 desiredTextureCombination);
1132 } else {
1133 SkASSERT(effect->children().size() == 1);
1134 }
1135 keyContext.paintParamsKeyBuilder()->endBlock();
1136 }
1137
1138 sk_sp<PrecompileShader> fWrapped;
1139 int fNumWrappedCombos;
1140 sk_sp<PrecompileShader> fRawImageShader;
1141 int fNumRawImageShaderCombos;
1142 };
1143
1144 sk_sp<PrecompileShader> PrecompileShadersPriv::MatrixConvolution(
1145 sk_sp<skgpu::graphite::PrecompileShader> wrapped) {
1146 return sk_make_sp<PrecompileMatrixConvolutionShader>(std::move(wrapped));
1147 }
1148
1149 //--------------------------------------------------------------------------------------------------
1150 class PrecompileMorphologyShader final : public PrecompileShader {
1151 public:
1152 PrecompileMorphologyShader(sk_sp<PrecompileShader> wrapped,
1153 SkKnownRuntimeEffects::StableKey stableKey)
1154 : fWrapped(std::move(wrapped))
1155 , fStableKey(stableKey) {
1156 fNumWrappedCombos = fWrapped->priv().numCombinations();
1157 SkASSERT(stableKey == SkKnownRuntimeEffects::StableKey::kLinearMorphology ||
1158 stableKey == SkKnownRuntimeEffects::StableKey::kSparseMorphology);
1159 }
1160
1161 private:
1162 int numChildCombinations() const override { return fNumWrappedCombos; }
1163
1164 void addToKey(const KeyContext& keyContext, int desiredCombination) const override {
1165 SkASSERT(desiredCombination < fNumWrappedCombos);
1166
1167 const SkRuntimeEffect* effect = GetKnownRuntimeEffect(fStableKey);
1168 SkASSERT(effect->children().size() == 1);
1169
1170 RuntimeEffectBlock::BeginBlock(keyContext, { sk_ref_sp(effect) });
1171 fWrapped->priv().addToKey(keyContext.forRuntimeEffect(effect, /*child=*/0),
1172 desiredCombination);
1173 keyContext.paintParamsKeyBuilder()->endBlock();
1174 }
1175
1176 sk_sp<PrecompileShader> fWrapped;
1177 int fNumWrappedCombos;
1178 SkKnownRuntimeEffects::StableKey fStableKey;
1179 };
1180
1181 sk_sp<PrecompileShader> PrecompileShadersPriv::LinearMorphology(sk_sp<PrecompileShader> wrapped) {
1182 return sk_make_sp<PrecompileMorphologyShader>(
1183 std::move(wrapped),
1184 SkKnownRuntimeEffects::StableKey::kLinearMorphology);
1185 }
1186
1187 sk_sp<PrecompileShader> PrecompileShadersPriv::SparseMorphology(sk_sp<PrecompileShader> wrapped) {
1188 return sk_make_sp<PrecompileMorphologyShader>(
1189 std::move(wrapped),
1190 SkKnownRuntimeEffects::StableKey::kSparseMorphology);
1191 }
1192
1193 //--------------------------------------------------------------------------------------------------
1194 class PrecompileDisplacementShader final : public PrecompileShader {
1195 public:
1196 PrecompileDisplacementShader(sk_sp<PrecompileShader> displacement,
1197 sk_sp<PrecompileShader> color)
1198 : fDisplacement(std::move(displacement))
1199 , fColor(std::move(color)) {
1200 fNumDisplacementCombos = fDisplacement->priv().numCombinations();
1201 fNumColorCombos = fColor->priv().numCombinations();
1202 }
1203
1204 private:
1205 int numChildCombinations() const override { return fNumDisplacementCombos * fNumColorCombos; }
1206
1207 void addToKey(const KeyContext& keyContext, int desiredCombination) const override {
1208 SkASSERT(desiredCombination < this->numChildCombinations());
1209
1210 const int desiredDisplacementCombination = desiredCombination % fNumDisplacementCombos;
1211 const int desiredColorCombination = desiredCombination / fNumDisplacementCombos;
1212 SkASSERT(desiredColorCombination < fNumColorCombos);
1213
1214 const SkRuntimeEffect* effect =
1215 GetKnownRuntimeEffect(SkKnownRuntimeEffects::StableKey::kDisplacement);
1216 SkASSERT(effect->children().size() == 2);
1217
1218 RuntimeEffectBlock::BeginBlock(keyContext, { sk_ref_sp(effect) });
1219 fDisplacement->priv().addToKey(keyContext.forRuntimeEffect(effect, /*child=*/0),
1220 desiredDisplacementCombination);
1221 fColor->priv().addToKey(keyContext.forRuntimeEffect(effect, /*child=*/1),
1222 desiredColorCombination);
1223 keyContext.paintParamsKeyBuilder()->endBlock();
1224 }
1225
1226 sk_sp<PrecompileShader> fDisplacement;
1227 int fNumDisplacementCombos;
1228 sk_sp<PrecompileShader> fColor;
1229 int fNumColorCombos;
1230 };
1231
1232 //--------------------------------------------------------------------------------------------------
1233 sk_sp<PrecompileShader> PrecompileShadersPriv::Displacement(sk_sp<PrecompileShader> displacement,
1234 sk_sp<PrecompileShader> color) {
1235 return sk_make_sp<PrecompileDisplacementShader>(std::move(displacement), std::move(color));
1236 }
1237
1238 //--------------------------------------------------------------------------------------------------
1239 class PrecompileLightingShader final : public PrecompileShader {
1240 public:
1241 PrecompileLightingShader(sk_sp<PrecompileShader> wrapped)
1242 : fWrapped(std::move(wrapped)) {
1243 fNumWrappedCombos = fWrapped->priv().numCombinations();
1244 }
1245
1246 private:
1247 int numChildCombinations() const override { return fNumWrappedCombos; }
1248
1249 void addToKey(const KeyContext& keyContext, int desiredCombination) const override {
1250 SkASSERT(desiredCombination < fNumWrappedCombos);
1251
1252 const SkRuntimeEffect* normalEffect =
1253 GetKnownRuntimeEffect(SkKnownRuntimeEffects::StableKey::kNormal);
1254 const SkRuntimeEffect* lightingEffect =
1255 GetKnownRuntimeEffect(SkKnownRuntimeEffects::StableKey::kLighting);
1256 SkASSERT(normalEffect->children().size() == 1 &&
1257 lightingEffect->children().size() == 1);
1258
1259 KeyContext lightingContext = keyContext.forRuntimeEffect(lightingEffect, /*child=*/0);
1260 KeyContext normalContext = lightingContext.forRuntimeEffect(normalEffect, /*child=*/0);
1261
1262 RuntimeEffectBlock::BeginBlock(keyContext, { sk_ref_sp(lightingEffect) });
1263 RuntimeEffectBlock::BeginBlock(lightingContext, { sk_ref_sp(normalEffect) });
1264 fWrapped->priv().addToKey(normalContext, desiredCombination);
1265 keyContext.paintParamsKeyBuilder()->endBlock();
1266 keyContext.paintParamsKeyBuilder()->endBlock();
1267 }
1268
1269 sk_sp<PrecompileShader> fWrapped;
1270 int fNumWrappedCombos;
1271 };
1272
1273 sk_sp<PrecompileShader> PrecompileShadersPriv::Lighting(sk_sp<PrecompileShader> wrapped) {
1274 return sk_make_sp<PrecompileLightingShader>(std::move(wrapped));
1275 }
1276
1277 //--------------------------------------------------------------------------------------------------
1278
1279 } // namespace skgpu::graphite