Debian Codegraph @

src/gpu/ganesh/GrFragmentProcessors.cpp

raw ◀ ▶
Source reconstructed at patch state 0 of 1 — not the installed (fully patched) source.
1 Debian patch touch this file
1 /*
2 * Copyright 2023 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 "src/gpu/ganesh/GrFragmentProcessors.h"
9
10 #include "include/core/SkAlphaType.h"
11 #include "include/core/SkBlendMode.h"
12 #include "include/core/SkColor.h"
13 #include "include/core/SkColorSpace.h"
14 #include "include/core/SkColorType.h"
15 #include "include/core/SkData.h"
16 #include "include/core/SkImage.h"
17 #include "include/core/SkImageInfo.h"
18 #include "include/core/SkMatrix.h"
19 #include "include/core/SkPicture.h"
20 #include "include/core/SkPoint.h"
21 #include "include/core/SkRect.h"
22 #include "include/core/SkRefCnt.h"
23 #include "include/core/SkSize.h"
24 #include "include/core/SkSpan.h"
25 #include "include/effects/SkRuntimeEffect.h"
26 #include "include/gpu/GpuTypes.h"
27 #include "include/gpu/ganesh/GrRecordingContext.h"
28 #include "include/gpu/ganesh/GrTypes.h"
29 #include "include/gpu/ganesh/SkSurfaceGanesh.h"
30 #include "include/private/base/SkAssert.h"
31 #include "include/private/base/SkDebug.h"
32 #include "include/private/base/SkTArray.h"
33 #include "include/private/gpu/ganesh/GrTypesPriv.h"
34 #include "src/base/SkTLazy.h"
35 #include "src/core/SkBlendModeBlender.h"
36 #include "src/core/SkBlenderBase.h"
37 #include "src/core/SkColorData.h"
38 #include "src/core/SkColorSpacePriv.h"
39 #include "src/core/SkColorSpaceXformSteps.h"
40 #include "src/core/SkMaskFilterBase.h"
41 #include "src/core/SkRuntimeBlender.h"
42 #include "src/core/SkRuntimeEffectPriv.h"
43 #include "src/effects/SkShaderMaskFilterImpl.h"
44 #include "src/effects/colorfilters/SkBlendModeColorFilter.h"
45 #include "src/effects/colorfilters/SkColorFilterBase.h"
46 #include "src/effects/colorfilters/SkColorSpaceXformColorFilter.h"
47 #include "src/effects/colorfilters/SkComposeColorFilter.h"
48 #include "src/effects/colorfilters/SkGaussianColorFilter.h"
49 #include "src/effects/colorfilters/SkMatrixColorFilter.h"
50 #include "src/effects/colorfilters/SkRuntimeColorFilter.h"
51 #include "src/effects/colorfilters/SkTableColorFilter.h"
52 #include "src/effects/colorfilters/SkWorkingFormatColorFilter.h"
53 #include "src/gpu/ResourceKey.h"
54 #include "src/gpu/Swizzle.h"
55 #include "src/gpu/ganesh/GrCaps.h"
56 #include "src/gpu/ganesh/GrColorInfo.h"
57 #include "src/gpu/ganesh/GrColorSpaceXform.h"
58 #include "src/gpu/ganesh/GrFPArgs.h"
59 #include "src/gpu/ganesh/GrFragmentProcessor.h"
60 #include "src/gpu/ganesh/GrProxyProvider.h"
61 #include "src/gpu/ganesh/GrRecordingContextPriv.h"
62 #include "src/gpu/ganesh/GrSamplerState.h"
63 #include "src/gpu/ganesh/GrShaderCaps.h"
64 #include "src/gpu/ganesh/GrSurfaceProxy.h"
65 #include "src/gpu/ganesh/GrSurfaceProxyView.h"
66 #include "src/gpu/ganesh/GrTextureProxy.h"
67 #include "src/gpu/ganesh/SkGr.h"
68 #include "src/gpu/ganesh/SurfaceDrawContext.h"
69 #include "src/gpu/ganesh/effects/GrBlendFragmentProcessor.h"
70 #include "src/gpu/ganesh/effects/GrColorTableEffect.h"
71 #include "src/gpu/ganesh/effects/GrMatrixEffect.h"
72 #include "src/gpu/ganesh/effects/GrPerlinNoise2Effect.h"
73 #include "src/gpu/ganesh/effects/GrSkSLFP.h"
74 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
75 #include "src/gpu/ganesh/gradients/GrGradientShader.h"
76 #include "src/gpu/ganesh/image/GrImageUtils.h"
77 #include "src/gpu/ganesh/image/GrMippedBitmap.h"
78 #include "src/shaders/SkBlendShader.h"
79 #include "src/shaders/SkColorFilterShader.h"
80 #include "src/shaders/SkColorShader.h"
81 #include "src/shaders/SkCoordClampShader.h"
82 #include "src/shaders/SkEmptyShader.h"
83 #include "src/shaders/SkImageShader.h"
84 #include "src/shaders/SkLocalMatrixShader.h"
85 #include "src/shaders/SkPerlinNoiseShaderImpl.h"
86 #include "src/shaders/SkPictureShader.h"
87 #include "src/shaders/SkRuntimeShader.h"
88 #include "src/shaders/SkShaderBase.h"
89 #include "src/shaders/SkTransformShader.h"
90 #include "src/shaders/SkTriColorShader.h"
91 #include "src/shaders/SkWorkingColorSpaceShader.h"
92 #include "src/shaders/gradients/SkConicalGradient.h"
93 #include "src/shaders/gradients/SkGradientBaseShader.h"
94 #include "src/shaders/gradients/SkLinearGradient.h"
95 #include "src/shaders/gradients/SkRadialGradient.h"
96 #include "src/shaders/gradients/SkSweepGradient.h"
97
98 #include <cstdint>
99 #include <cstring>
100 #include <memory>
101 #include <optional>
102 #include <utility>
103
104 class GrRenderTargetProxy;
105 class SkBitmap;
106 enum class SkTileMode;
107
108 namespace GrFragmentProcessors {
109 static std::unique_ptr<GrFragmentProcessor>
110 make_fp_from_shader_mask_filter(const SkMaskFilterBase* maskfilter,
111 const GrFPArgs& args,
112 const SkMatrix& ctm) {
113 SkASSERT(maskfilter);
114 auto shaderMF = static_cast<const SkShaderMaskFilterImpl*>(maskfilter);
115 auto fp = Make(shaderMF->shader().get(), args, ctm);
116 return GrFragmentProcessor::MulInputByChildAlpha(std::move(fp));
117 }
118
119 std::unique_ptr<GrFragmentProcessor> Make(const SkMaskFilter* maskfilter,
120 const GrFPArgs& args,
121 const SkMatrix& ctm) {
122 if (!maskfilter) {
123 return nullptr;
124 }
125 auto mfb = as_MFB(maskfilter);
126 switch (mfb->type()) {
127 case SkMaskFilterBase::Type::kShader:
128 return make_fp_from_shader_mask_filter(mfb, args, ctm);
129 case SkMaskFilterBase::Type::kBlur:
130 case SkMaskFilterBase::Type::kEmboss:
131 case SkMaskFilterBase::Type::kSDF:
132 case SkMaskFilterBase::Type::kTable:
133 return nullptr;
134 }
135 SkUNREACHABLE;
136 }
137
138 bool IsSupported(const SkMaskFilter* maskfilter) {
139 if (!maskfilter) {
140 return false;
141 }
142 auto mfb = as_MFB(maskfilter);
143 switch (mfb->type()) {
144 case SkMaskFilterBase::Type::kShader:
145 return true;
146 case SkMaskFilterBase::Type::kBlur:
147 case SkMaskFilterBase::Type::kEmboss:
148 case SkMaskFilterBase::Type::kSDF:
149 case SkMaskFilterBase::Type::kTable:
150 return false;
151 }
152 SkUNREACHABLE;
153 }
154
155 using ChildType = SkRuntimeEffect::ChildType;
156
157 GrFPResult MakeChildFP(const SkRuntimeEffect::ChildPtr& child, const GrFPArgs& childArgs) {
158 std::optional<ChildType> type = child.type();
159 if (!type.has_value()) {
160 // We have a null child effect.
161 return GrFPNullableSuccess(nullptr);
162 }
163
164 switch (*type) {
165 case ChildType::kShader: {
166 // Convert a SkShader into a child FP.
167 SkShaders::MatrixRec mRec(SkMatrix::I());
168 mRec.markTotalMatrixInvalid();
169 auto childFP = GrFragmentProcessors::Make(child.shader(), childArgs, mRec);
170 return childFP ? GrFPSuccess(std::move(childFP))
171 : GrFPFailure(nullptr);
172 }
173 case ChildType::kColorFilter: {
174 // Convert a SkColorFilter into a child FP.
175 auto [success, childFP] = GrFragmentProcessors::Make(childArgs.fSurfaceDrawContext,
176 child.colorFilter(),
177 /*inputFP=*/nullptr,
178 *childArgs.fDstColorInfo,
179 childArgs.fSurfaceProps);
180 return success ? GrFPSuccess(std::move(childFP))
181 : GrFPFailure(nullptr);
182 }
183 case ChildType::kBlender: {
184 // Convert a SkBlender into a child FP.
185 auto childFP = GrFragmentProcessors::Make(as_BB(child.blender()),
186 /*srcFP=*/nullptr,
187 GrFragmentProcessor::DestColor(),
188 childArgs);
189 return childFP ? GrFPSuccess(std::move(childFP))
190 : GrFPFailure(nullptr);
191 }
192 }
193
194 SkUNREACHABLE;
195 }
196
197 static GrFPResult make_effect_fp(sk_sp<SkRuntimeEffect> effect,
198 const char* name,
199 sk_sp<const SkData> uniforms,
200 std::unique_ptr<GrFragmentProcessor> inputFP,
201 std::unique_ptr<GrFragmentProcessor> destColorFP,
202 SkSpan<const SkRuntimeEffect::ChildPtr> children,
203 const GrFPArgs& childArgs) {
204 skia_private::STArray<8, std::unique_ptr<GrFragmentProcessor>> childFPs;
205 for (const auto& child : children) {
206 auto [success, childFP] = MakeChildFP(child, childArgs);
207 if (!success) {
208 return GrFPFailure(std::move(inputFP));
209 }
210 childFPs.push_back(std::move(childFP));
211 }
212 auto fp = GrSkSLFP::MakeWithData(std::move(effect),
213 name,
214 childArgs.fDstColorInfo->refColorSpace(),
215 std::move(inputFP),
216 std::move(destColorFP),
217 std::move(uniforms),
218 SkSpan(childFPs));
219 SkASSERT(fp);
220 return GrFPSuccess(std::move(fp));
221 }
222
223 static std::unique_ptr<GrFragmentProcessor> make_blender_fp(
224 const SkRuntimeBlender* rtb,
225 std::unique_ptr<GrFragmentProcessor> srcFP,
226 std::unique_ptr<GrFragmentProcessor> dstFP,
227 const GrFPArgs& fpArgs) {
228 SkASSERT(rtb);
229 if (!SkRuntimeEffectPriv::CanDraw(fpArgs.fSurfaceDrawContext->caps(), rtb->effect().get())) {
230 return nullptr;
231 }
232
233 sk_sp<const SkData> uniforms = SkRuntimeEffectPriv::TransformUniforms(
234 rtb->effect()->uniforms(),
235 rtb->uniforms(),
236 fpArgs.fDstColorInfo->colorSpace());
237 SkASSERT(uniforms);
238 GrFPArgs childArgs(fpArgs.fSurfaceDrawContext,
239 fpArgs.fDstColorInfo,
240 fpArgs.fSurfaceProps,
241 GrFPArgs::Scope::kRuntimeEffect);
242 auto [success, fp] = make_effect_fp(rtb->effect(),
243 "runtime_blender",
244 std::move(uniforms),
245 std::move(srcFP),
246 std::move(dstFP),
247 rtb->children(),
248 childArgs);
249
250 return success ? std::move(fp) : nullptr;
251 }
252
253 static std::unique_ptr<GrFragmentProcessor> make_blender_fp(
254 const SkBlendModeBlender* blender,
255 std::unique_ptr<GrFragmentProcessor> srcFP,
256 std::unique_ptr<GrFragmentProcessor> dstFP,
257 const GrFPArgs& fpArgs) {
258 SkASSERT(blender);
259 return GrBlendFragmentProcessor::Make(std::move(srcFP), std::move(dstFP), blender->mode());
260 }
261
262 std::unique_ptr<GrFragmentProcessor> Make(const SkBlenderBase* blender,
263 std::unique_ptr<GrFragmentProcessor> srcFP,
264 std::unique_ptr<GrFragmentProcessor> dstFP,
265 const GrFPArgs& fpArgs) {
266 if (!blender) {
267 return nullptr;
268 }
269 switch (blender->type()) {
270 #define M(type) \
271 case SkBlenderBase::BlenderType::k##type: \
272 return make_blender_fp(static_cast<const Sk##type##Blender*>(blender), \
273 std::move(srcFP), \
274 std::move(dstFP), \
275 fpArgs);
276 SK_ALL_BLENDERS(M)
277 #undef M
278 }
279 SkUNREACHABLE;
280 }
281
282 static SkPMColor4f map_color(const SkColor4f& c, SkColorSpace* src, SkColorSpace* dst) {
283 SkPMColor4f color = {c.fR, c.fG, c.fB, c.fA};
284 SkColorSpaceXformSteps(src, kUnpremul_SkAlphaType, dst, kPremul_SkAlphaType).apply(color.vec());
285 return color;
286 }
287 static GrFPResult make_colorfilter_fp(skgpu::ganesh::SurfaceDrawContext*,
288 const SkBlendModeColorFilter* filter,
289 std::unique_ptr<GrFragmentProcessor> inputFP,
290 const GrColorInfo& dstColorInfo,
291 const SkSurfaceProps& props) {
292 if (filter->mode() == SkBlendMode::kDst) {
293 // If the blend mode is "dest," the blend color won't factor into it at all.
294 // We can return the input FP as-is.
295 return GrFPSuccess(std::move(inputFP));
296 }
297
298 SkDEBUGCODE(const bool fpHasConstIO = !inputFP || inputFP->hasConstantOutputForConstantInput();)
299
300 SkPMColor4f color = map_color(filter->color(), sk_srgb_singleton(), dstColorInfo.colorSpace());
301
302 auto colorFP = GrFragmentProcessor::MakeColor(color);
303 auto xferFP =
304 GrBlendFragmentProcessor::Make(std::move(colorFP), std::move(inputFP), filter->mode());
305
306 if (xferFP == nullptr) {
307 // This is only expected to happen if the blend mode is "dest" and the input FP is null.
308 // Since we already did an early-out in the "dest" blend mode case, we shouldn't get here.
309 SkDEBUGFAIL("GrBlendFragmentProcessor::Make returned null unexpectedly");
310 return GrFPFailure(nullptr);
311 }
312
313 // With a solid color input this should always be able to compute the blended color
314 // (at least for coeff modes).
315 // Occasionally, we even do better than we started; specifically, in "src" blend mode, we end up
316 // ditching the input FP entirely, which turns a non-constant operation into a constant one.
317 SkASSERT(filter->mode() > SkBlendMode::kLastCoeffMode ||
318 xferFP->hasConstantOutputForConstantInput() >= fpHasConstIO);
319
320 return GrFPSuccess(std::move(xferFP));
321 }
322
323 static GrFPResult make_colorfilter_fp(skgpu::ganesh::SurfaceDrawContext* sdc,
324 const SkComposeColorFilter* filter,
325 std::unique_ptr<GrFragmentProcessor> inputFP,
326 const GrColorInfo& dstColorInfo,
327 const SkSurfaceProps& props) {
328 // Unfortunately, we need to clone the input before we know we need it. This lets us return
329 // the original FP if either internal color filter fails.
330 auto inputClone = inputFP ? inputFP->clone() : nullptr;
331
332 auto [innerSuccess, innerFP] =
333 Make(sdc, filter->inner().get(), std::move(inputFP), dstColorInfo, props);
334 if (!innerSuccess) {
335 return GrFPFailure(std::move(inputClone));
336 }
337
338 auto [outerSuccess, outerFP] =
339 Make(sdc, filter->outer().get(), std::move(innerFP), dstColorInfo, props);
340 if (!outerSuccess) {
341 return GrFPFailure(std::move(inputClone));
342 }
343
344 return GrFPSuccess(std::move(outerFP));
345 }
346
347 static GrFPResult make_colorfilter_fp(skgpu::ganesh::SurfaceDrawContext* sdc,
348 const SkColorSpaceXformColorFilter* filter,
349 std::unique_ptr<GrFragmentProcessor> inputFP,
350 const GrColorInfo&,
351 const SkSurfaceProps&) {
352 // wish our caller would let us know if our input was opaque...
353 constexpr SkAlphaType alphaType = kPremul_SkAlphaType;
354 return GrFPSuccess(GrColorSpaceXformEffect::Make(
355 std::move(inputFP), filter->src().get(), alphaType, filter->dst().get(), alphaType));
356 }
357
358 static GrFPResult make_colorfilter_fp(skgpu::ganesh::SurfaceDrawContext*,
359 const SkGaussianColorFilter*,
360 std::unique_ptr<GrFragmentProcessor> inputFP,
361 const GrColorInfo&,
362 const SkSurfaceProps&) {
363 static const SkRuntimeEffect* effect =
364 SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter,
365 "half4 main(half4 inColor) {"
366 "half factor = 1 - inColor.a;"
367 "factor = exp(-factor * factor * 4) - 0.018;"
368 "return half4(factor);"
369 "}");
370 SkASSERT(SkRuntimeEffectPriv::SupportsConstantOutputForConstantInput(effect));
371 return GrFPSuccess(
372 GrSkSLFP::Make(effect, "gaussian_fp", std::move(inputFP), GrSkSLFP::OptFlags::kNone));
373 }
374
375 static std::unique_ptr<GrFragmentProcessor> rgb_to_hsl(std::unique_ptr<GrFragmentProcessor> child) {
376 static const SkRuntimeEffect* effect =
377 SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter,
378 "half4 main(half4 color) {"
379 "return $rgb_to_hsl(color.rgb, color.a);"
380 "}");
381 SkASSERT(SkRuntimeEffectPriv::SupportsConstantOutputForConstantInput(effect));
382 return GrSkSLFP::Make(
383 effect, "RgbToHsl", std::move(child), GrSkSLFP::OptFlags::kPreservesOpaqueInput);
384 }
385
386 static std::unique_ptr<GrFragmentProcessor> hsl_to_rgb(std::unique_ptr<GrFragmentProcessor> child) {
387 static const SkRuntimeEffect* effect =
388 SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter,
389 "half4 main(half4 color) {"
390 "return $hsl_to_rgb(color.rgb, color.a);"
391 "}");
392 SkASSERT(SkRuntimeEffectPriv::SupportsConstantOutputForConstantInput(effect));
393 return GrSkSLFP::Make(
394 effect, "HslToRgb", std::move(child), GrSkSLFP::OptFlags::kPreservesOpaqueInput);
395 }
396
397 static GrFPResult make_colorfilter_fp(skgpu::ganesh::SurfaceDrawContext* sdc,
398 const SkMatrixColorFilter* filter,
399 std::unique_ptr<GrFragmentProcessor> inputFP,
400 const GrColorInfo&,
401 const SkSurfaceProps&) {
402 switch (filter->domain()) {
403 case SkMatrixColorFilter::Domain::kRGBA:
404 return GrFPSuccess(GrFragmentProcessor::ColorMatrix(
405 std::move(inputFP),
406 filter->matrix(),
407 /* unpremulInput = */ true,
408 /* clampRGBOutput = */ filter->clamp() == SkMatrixColorFilter::Clamp::kYes,
409 /* premulOutput = */ true));
410
411 case SkMatrixColorFilter::Domain::kHSLA: {
412 auto fp = rgb_to_hsl(std::move(inputFP));
413 fp = GrFragmentProcessor::ColorMatrix(std::move(fp),
414 filter->matrix(),
415 /* unpremulInput = */ false,
416 /* clampRGBOutput = */ false,
417 /* premulOutput = */ false);
418 return GrFPSuccess(hsl_to_rgb(std::move(fp)));
419 }
420 }
421 SkUNREACHABLE;
422 }
423
424 static GrFPResult make_colorfilter_fp(skgpu::ganesh::SurfaceDrawContext* sdc,
425 const SkRuntimeColorFilter* filter,
426 std::unique_ptr<GrFragmentProcessor> inputFP,
427 const GrColorInfo& colorInfo,
428 const SkSurfaceProps& props) {
429 sk_sp<const SkData> uniforms = SkRuntimeEffectPriv::TransformUniforms(
430 filter->effect()->uniforms(), filter->uniforms(), colorInfo.colorSpace());
431 SkASSERT(uniforms);
432
433 GrFPArgs childArgs(sdc, &colorInfo, props, GrFPArgs::Scope::kRuntimeEffect);
434 return make_effect_fp(filter->effect(),
435 "runtime_color_filter",
436 std::move(uniforms),
437 std::move(inputFP),
438 /*destColorFP=*/nullptr,
439 filter->children(),
440 childArgs);
441 }
442
443 static GrFPResult make_colorfilter_fp(skgpu::ganesh::SurfaceDrawContext* sdc,
444 const SkTableColorFilter* filter,
445 std::unique_ptr<GrFragmentProcessor> inputFP,
446 const GrColorInfo&,
447 const SkSurfaceProps&) {
448 SkASSERT(filter->bitmap().isImmutable());
449 auto cte = ColorTableEffect::Make(
450 std::move(inputFP), sdc->recordingContext(), GrMippedBitmap(filter->bitmap()));
451 return cte ? GrFPSuccess(std::move(cte)) : GrFPFailure(nullptr);
452 }
453
454 static GrFPResult make_colorfilter_fp(skgpu::ganesh::SurfaceDrawContext* sdc,
455 const SkWorkingFormatColorFilter* filter,
456 std::unique_ptr<GrFragmentProcessor> inputFP,
457 const GrColorInfo& dstColorInfo,
458 const SkSurfaceProps& props) {
459 sk_sp<SkColorSpace> dstCS = dstColorInfo.refColorSpace();
460 if (!dstCS) {
461 dstCS = SkColorSpace::MakeSRGB();
462 }
463
464 SkAlphaType workingAT;
465 sk_sp<SkColorSpace> workingCS = filter->workingFormat(dstCS, &workingAT);
466
467 GrColorInfo dst = {dstColorInfo.colorType(), dstColorInfo.alphaType(), dstCS},
468 working = {dstColorInfo.colorType(), workingAT, workingCS};
469
470 auto [ok, fp] = Make(sdc,
471 filter->child().get(),
472 GrColorSpaceXformEffect::Make(std::move(inputFP), dst, working),
473 working,
474 props);
475
476 return ok ? GrFPSuccess(GrColorSpaceXformEffect::Make(std::move(fp), working, dst))
477 : GrFPFailure(std::move(fp));
478 }
479
480 GrFPResult Make(skgpu::ganesh::SurfaceDrawContext* sdc,
481 const SkColorFilter* cf,
482 std::unique_ptr<GrFragmentProcessor> inputFP,
483 const GrColorInfo& dstColorInfo,
484 const SkSurfaceProps& props) {
485 if (!cf) {
486 return GrFPFailure(nullptr);
487 }
488 auto cfb = as_CFB(cf);
489 switch (cfb->type()) {
490 case SkColorFilterBase::Type::kNoop:
491 return GrFPFailure(nullptr);
492 #define M(type) \
493 case SkColorFilterBase::Type::k##type: \
494 return make_colorfilter_fp(sdc, \
495 static_cast<const Sk##type##ColorFilter*>(cf), \
496 std::move(inputFP), \
497 dstColorInfo, \
498 props);
499 SK_ALL_COLOR_FILTERS(M)
500 #undef M
501 }
502 SkUNREACHABLE;
503 }
504
505 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkBlendShader* shader,
506 const GrFPArgs& args,
507 const SkShaders::MatrixRec& mRec) {
508 auto fpA = Make(shader->dst().get(), args, mRec);
509 auto fpB = Make(shader->src().get(), args, mRec);
510 if (!fpA || !fpB) {
511 // This is unexpected. Both src and dst shaders should be valid. Just fail.
512 return nullptr;
513 }
514 return GrBlendFragmentProcessor::Make(std::move(fpB), std::move(fpA), shader->mode());
515 }
516
517 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkColorFilterShader* shader,
518 const GrFPArgs& args,
519 const SkShaders::MatrixRec& mRec) {
520 auto shaderFP = Make(shader->shader().get(), args, mRec);
521 if (!shaderFP) {
522 return nullptr;
523 }
524
525 // TODO I guess, but it shouldn't come up as used today.
526 SkASSERT(shader->alpha() == 1.0f);
527
528 auto [success, fp] = Make(args.fSurfaceDrawContext,
529 shader->filter().get(),
530 std::move(shaderFP),
531 *args.fDstColorInfo,
532 args.fSurfaceProps);
533 // If the filter FP could not be created, we still want to return the shader FP, so checking
534 // success can be omitted here.
535 return std::move(fp);
536 }
537
538 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkColorShader* shader,
539 const GrFPArgs& args,
540 const SkShaders::MatrixRec& mRec) {
541 SkColorSpaceXformSteps steps{sk_srgb_singleton(),
542 kUnpremul_SkAlphaType,
543 args.fDstColorInfo->colorSpace(),
544 kUnpremul_SkAlphaType};
545 SkColor4f color = shader->color();
546 steps.apply(color.vec());
547 return GrFragmentProcessor::MakeColor(color.premul());
548 }
549
550 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkCoordClampShader* shader,
551 const GrFPArgs& args,
552 const SkShaders::MatrixRec& mRec) {
553 static const SkRuntimeEffect* effect =
554 SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
555 "uniform shader c;"
556 "uniform float4 s;"
557 "half4 main(float2 p) {"
558 "return c.eval(clamp(p, s.LT, s.RB));"
559 "}");
560
561 auto fp = Make(shader->shader().get(), args, mRec.applied());
562 if (!fp) {
563 return nullptr;
564 }
565
566 GrSkSLFP::OptFlags flags = GrSkSLFP::OptFlags::kNone;
567 if (fp->compatibleWithCoverageAsAlpha()) {
568 flags |= GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha;
569 }
570 if (fp->preservesOpaqueInput()) {
571 flags |= GrSkSLFP::OptFlags::kPreservesOpaqueInput;
572 }
573 fp = GrSkSLFP::Make(effect,
574 "clamp_fp",
575 /*inputFP=*/nullptr,
576 flags,
577 "c",
578 std::move(fp),
579 "s",
580 shader->subset());
581
582 auto [total, ok] = mRec.applyForFragmentProcessor({});
583 if (!ok) {
584 return nullptr;
585 }
586 return GrMatrixEffect::Make(total, std::move(fp));
587 }
588
589 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkCTMShader* shader,
590 const GrFPArgs& args,
591 const SkShaders::MatrixRec& mRec) {
592 auto ctmInv = shader->ctm().invert();
593 if (!ctmInv) {
594 return nullptr;
595 }
596
597 auto base = Make(shader->proxyShader().get(), args, shader->ctm());
598 if (!base) {
599 return nullptr;
600 }
601
602 // In order for the shader to be evaluated with the original CTM, we explicitly evaluate it
603 // at sk_FragCoord, and pass that through the inverse of the original CTM. This avoids requiring
604 // local coords for the shader and mapping from the draw's local to device and then back.
605 return GrFragmentProcessor::DeviceSpace(GrMatrixEffect::Make(*ctmInv, std::move(base)));
606 }
607
608 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkEmptyShader* shader,
609 const GrFPArgs&,
610 const SkShaders::MatrixRec&) {
611 return nullptr;
612 }
613
614 static bool needs_subset(sk_sp<const SkImage> img, const SkRect& subset) {
615 return subset != SkRect::Make(img->dimensions());
616 }
617
618 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkImageShader* shader,
619 const GrFPArgs& args,
620 const SkShaders::MatrixRec& mRec) {
621 SkTileMode tileModes[2] = {shader->tileModeX(), shader->tileModeY()};
622 const SkRect shaderSubset = shader->subset();
623 const SkRect* subset = needs_subset(shader->image(), shaderSubset) ? &shaderSubset : nullptr;
624 auto fp = skgpu::ganesh::AsFragmentProcessor(
625 args.fSurfaceDrawContext, shader->image(), shader->sampling(), tileModes, SkMatrix::I(),
626 subset);
627 if (!fp) {
628 return nullptr;
629 }
630
631 auto [total, ok] = mRec.applyForFragmentProcessor({});
632 if (!ok) {
633 return nullptr;
634 }
635 fp = GrMatrixEffect::Make(total, std::move(fp));
636
637 if (!shader->isRaw()) {
638 fp = GrColorSpaceXformEffect::Make(std::move(fp),
639 shader->image()->colorSpace(),
640 shader->image()->alphaType(),
641 args.fDstColorInfo->colorSpace(),
642 kPremul_SkAlphaType);
643
644 // Alpha-only image shaders are tinted by the input color (typically the paint color).
645 // We suppress that behavior when sampled from a runtime effect.
646 if (shader->image()->isAlphaOnly() && args.fScope != GrFPArgs::Scope::kRuntimeEffect) {
647 fp = GrBlendFragmentProcessor::Make<SkBlendMode::kDstIn>(std::move(fp), nullptr);
648 }
649 }
650
651 return fp;
652 }
653
654 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkLocalMatrixShader* shader,
655 const GrFPArgs& args,
656 const SkShaders::MatrixRec& mRec) {
657 return Make(shader->wrappedShader().get(), args, mRec.concat(shader->localMatrix()));
658 }
659
660 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkPerlinNoiseShader* shader,
661 const GrFPArgs& args,
662 const SkShaders::MatrixRec& mRec) {
663 SkASSERT(args.fSurfaceDrawContext);
664 SkASSERT(shader->numOctaves());
665
666 // Either we don't stitch tiles, or we have a valid tile size
667 SkASSERT(!shader->stitchTiles() || !shader->tileSize().isEmpty());
668
669 auto paintingData = shader->getPaintingData();
670 paintingData->generateBitmaps();
671
672 GrRecordingContext* context = args.fSurfaceDrawContext->recordingContext();
673
674 const SkBitmap& permutationsBitmap = paintingData->getPermutationsBitmap();
675 const SkBitmap& noiseBitmap = paintingData->getNoiseBitmap();
676
677 auto permutationsView = std::get<0>(GrMakeCachedBitmapProxyView(
678 context,
679 GrMippedBitmap(permutationsBitmap),
680 /*label=*/"PerlinNoiseShader_FragmentProcessor_PermutationsView"));
681
682 auto noiseView = std::get<0>(
683 GrMakeCachedBitmapProxyView(context,
684 GrMippedBitmap(noiseBitmap),
685 /*label=*/"PerlinNoiseShader_FragmentProcessor_NoiseView"));
686
687 if (!permutationsView || !noiseView) {
688 return nullptr;
689 }
690
691 std::unique_ptr<GrFragmentProcessor> fp =
692 GrPerlinNoise2Effect::Make(shader->noiseType(),
693 shader->numOctaves(),
694 shader->stitchTiles(),
695 std::move(paintingData),
696 std::move(permutationsView),
697 std::move(noiseView),
698 *context->priv().caps());
699 if (!fp) {
700 return nullptr;
701 }
702 auto [total, ok] = mRec.applyForFragmentProcessor({});
703 if (!ok) {
704 return nullptr;
705 }
706 return GrMatrixEffect::Make(total, std::move(fp));
707 }
708
709 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkPictureShader* shader,
710 const GrFPArgs& args,
711 const SkShaders::MatrixRec& mRec) {
712 auto ctx = args.fSurfaceDrawContext->recordingContext();
713 SkColorType dstColorType = GrColorTypeToSkColorType(args.fDstColorInfo->colorType());
714 if (dstColorType == kUnknown_SkColorType) {
715 dstColorType = kRGBA_8888_SkColorType;
716 }
717 sk_sp<SkColorSpace> dstCS = SkColorSpace::MakeSRGB();
718 if (args.fDstColorInfo->colorSpace()) {
719 dstCS = sk_ref_sp(args.fDstColorInfo->colorSpace());
720 }
721
722 auto info = SkPictureShader::CachedImageInfo::Make(shader->tile(),
723 mRec.totalMatrix(),
724 dstColorType,
725 dstCS.get(),
726 ctx->priv().caps()->maxTextureSize(),
727 args.fSurfaceProps);
728 if (!info.success) {
729 return nullptr;
730 }
731
732 // Gotta be sure the GPU can support our requested colortype (might be FP16)
733 if (!ctx->colorTypeSupportedAsSurface(info.imageInfo.colorType())) {
734 info.imageInfo = info.imageInfo.makeColorType(kRGBA_8888_SkColorType);
735 }
736
737 static const skgpu::UniqueKey::Domain kDomain = skgpu::UniqueKey::GenerateDomain();
738 skgpu::UniqueKey key;
739 std::tuple keyData = {dstCS->toXYZD50Hash(),
740 dstCS->transferFnHash(),
741 static_cast<uint32_t>(dstColorType),
742 shader->picture()->uniqueID(),
743 shader->tile(),
744 info.tileScale,
745 info.props};
746 skgpu::UniqueKey::Builder builder(
747 &key, kDomain, sizeof(keyData) / sizeof(uint32_t), "Picture Shader Image");
748 memcpy(&builder[0], &keyData, sizeof(keyData));
749 builder.finish();
750
751 GrProxyProvider* provider = ctx->priv().proxyProvider();
752 GrSurfaceProxyView view;
753 if (auto proxy = provider->findOrCreateProxyByUniqueKey(key)) {
754 view = GrSurfaceProxyView(proxy, kTopLeft_GrSurfaceOrigin, skgpu::Swizzle());
755 } else {
756 const int msaaSampleCount = 0;
757 const bool createWithMips = false;
758 const bool kUnprotected = false;
759 auto image = info.makeImage(SkSurfaces::RenderTarget(ctx,
760 skgpu::Budgeted::kYes,
761 info.imageInfo,
762 msaaSampleCount,
763 kTopLeft_GrSurfaceOrigin,
764 &info.props,
765 createWithMips,
766 kUnprotected),
767 shader->picture().get());
768 if (!image) {
769 return nullptr;
770 }
771 // Images from a picture shader shouldn't ever be drawn into a surface backed by the same
772 // texture so there will never be need to make a copy of the image. So just pass in nullptr
773 // for targetSurface of the view.
774 constexpr GrRenderTargetProxy* targetSurface = nullptr;
775 auto [v, ct] = skgpu::ganesh::AsView(ctx, image, skgpu::Mipmapped::kNo, targetSurface);
776 view = std::move(v);
777 provider->assignUniqueKeyToProxy(key, view.asTextureProxy());
778 }
779
780 const GrSamplerState sampler(static_cast<GrSamplerState::WrapMode>(shader->tileModeX()),
781 static_cast<GrSamplerState::WrapMode>(shader->tileModeY()),
782 shader->filter());
783 auto fp = GrTextureEffect::Make(
784 std::move(view), kPremul_SkAlphaType, SkMatrix::I(), sampler, *ctx->priv().caps());
785 SkMatrix scale = SkMatrix::Scale(info.tileScale.width(), info.tileScale.height());
786 auto [total, ok] = mRec.applyForFragmentProcessor(scale);
787 if (!ok) {
788 return nullptr;
789 }
790 return GrMatrixEffect::Make(total, std::move(fp));
791 }
792
793 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkRuntimeShader* shader,
794 const GrFPArgs& args,
795 const SkShaders::MatrixRec& mRec) {
796 if (!SkRuntimeEffectPriv::CanDraw(args.fSurfaceDrawContext->caps(),
797 shader->asRuntimeEffect())) {
798 return nullptr;
799 }
800
801 sk_sp<const SkData> uniforms = SkRuntimeEffectPriv::TransformUniforms(
802 shader->asRuntimeEffect()->uniforms(),
803 shader->uniformData(args.fDstColorInfo->colorSpace()),
804 args.fDstColorInfo->colorSpace());
805 SkASSERT(uniforms);
806
807 bool success;
808 std::unique_ptr<GrFragmentProcessor> fp;
809 GrFPArgs childArgs(args.fSurfaceDrawContext, args.fDstColorInfo, args.fSurfaceProps,
810 GrFPArgs::Scope::kRuntimeEffect);
811 std::tie(success, fp) = make_effect_fp(shader->effect(),
812 "runtime_shader",
813 std::move(uniforms),
814 /*inputFP=*/nullptr,
815 /*destColorFP=*/nullptr,
816 shader->children(),
817 childArgs);
818 if (!success) {
819 return nullptr;
820 }
821
822 auto [total, ok] = mRec.applyForFragmentProcessor({});
823 if (!ok) {
824 return nullptr;
825 }
826 return GrMatrixEffect::Make(total, std::move(fp));
827 }
828
829 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkTransformShader* shader,
830 const GrFPArgs&,
831 const SkShaders::MatrixRec&) {
832 return nullptr;
833 }
834
835 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkTriColorShader* shader,
836 const GrFPArgs&,
837 const SkShaders::MatrixRec&) {
838 return nullptr;
839 }
840
841 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkWorkingColorSpaceShader* shader,
842 const GrFPArgs& args,
843 const SkShaders::MatrixRec& mRec) {
844 const GrColorInfo* dstInfo = args.fDstColorInfo;
845 SkAlphaType dstAT = dstInfo->alphaType();
846 sk_sp<SkColorSpace> dstCS = dstInfo->refColorSpace();
847 if (!dstCS) {
848 dstCS = SkColorSpace::MakeSRGB();
849 }
850
851
852 auto [inputCS, outputCS, workingAT] = shader->workingSpace(dstCS, dstAT);
853
854 GrColorInfo dst = {dstInfo->colorType(), dstAT, dstCS},
855 input = {dstInfo->colorType(), workingAT, inputCS},
856 output = {dstInfo->colorType(), workingAT, outputCS};
857
858 GrFPArgs workingArgs(args.fSurfaceDrawContext, &input, args.fSurfaceProps, args.fScope);
859
860 auto childFP = Make(shader->shader().get(), workingArgs, mRec);
861 if (!childFP) {
862 return nullptr;
863 }
864
865 auto childWithWorkingInput = GrFragmentProcessor::Compose(
866 std::move(childFP), GrColorSpaceXformEffect::Make(nullptr, dst, input));
867 // Assuming that childFP operates on inputCS/workingAT values and returns outputCS/workingAT
868 // values, then we only need to transform from output to dst.
869 return GrColorSpaceXformEffect::Make(std::move(childWithWorkingInput), output, dst);
870 }
871
872 //////////////////////////////////////////////////////////////////////////////////////////////
873
874 static std::unique_ptr<GrFragmentProcessor> make_gradient_fp(const SkConicalGradient* shader,
875 const GrFPArgs& args,
876 const SkShaders::MatrixRec& mRec) {
877 // The 2 point conical gradient can reject a pixel so it does change opacity even if the input
878 // was opaque. Thus, all of these layout FPs disable that optimization.
879 std::unique_ptr<GrFragmentProcessor> fp;
880 std::optional<SkMatrix> matrix;
881 switch (shader->getType()) {
882 case SkConicalGradient::Type::kStrip: {
883 static const SkRuntimeEffect* kEffect =
884 SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
885 "uniform half r0_2;"
886 "half4 main(float2 p) {"
887 // validation flag, set to negative to discard fragment later.
888 "half v = 1;"
889 "float t = r0_2 - p.y * p.y;"
890 "if (t >= 0) {"
891 "t = p.x + sqrt(t);"
892 "} else {"
893 "v = -1;"
894 "}"
895 "return half4(half(t), v, 0, 0);"
896 "}"
897 );
898 float r0 = shader->getStartRadius() / shader->getCenterX1();
899 fp = GrSkSLFP::Make(kEffect,
900 "TwoPointConicalStripLayout",
901 /*inputFP=*/nullptr,
902 GrSkSLFP::OptFlags::kNone,
903 "r0_2",
904 r0 * r0);
905 } break;
906
907 case SkConicalGradient::Type::kRadial: {
908 static const SkRuntimeEffect* kEffect =
909 SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
910 "uniform half r0;"
911 "uniform half lengthScale;"
912 "half4 main(float2 p) {"
913 // validation flag, set to negative to discard fragment later
914 "half v = 1;"
915 "float t = length(p) * lengthScale - r0;"
916 "return half4(half(t), v, 0, 0);"
917 "}"
918 );
919 float dr = shader->getDiffRadius();
920 float r0 = shader->getStartRadius() / dr;
921 bool isRadiusIncreasing = dr >= 0;
922 fp = GrSkSLFP::Make(kEffect,
923 "TwoPointConicalRadialLayout",
924 /*inputFP=*/nullptr,
925 GrSkSLFP::OptFlags::kNone,
926 "r0",
927 r0,
928 "lengthScale",
929 isRadiusIncreasing ? 1.0f : -1.0f);
930
931 // GPU radial matrix is different from the original matrix, since we map the diff radius
932 // to have |dr| = 1, so manually compute the final gradient matrix here.
933
934 // Map center to (0, 0)
935 matrix = SkMatrix::Translate(-shader->getStartCenter().fX,
936 -shader->getStartCenter().fY);
937 // scale |diffRadius| to 1
938 matrix->postScale(1 / dr, 1 / dr);
939 } break;
940
941 case SkConicalGradient::Type::kFocal: {
942 static const SkRuntimeEffect* kEffect =
943 SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
944 // Optimization flags, all specialized:
945 "uniform int isRadiusIncreasing;"
946 "uniform int isFocalOnCircle;"
947 "uniform int isWellBehaved;"
948 "uniform int isSwapped;"
949 "uniform int isNativelyFocal;"
950
951 "uniform half invR1;" // 1/r1
952 "uniform half fx;" // focalX = r0/(r0-r1)
953
954 "half4 main(float2 p) {"
955 "float t = -1;"
956 "half v = 1;" // validation flag,set to negative to discard fragment later
957
958 "float x_t = -1;"
959 "if (bool(isFocalOnCircle)) {"
960 "x_t = dot(p, p) / p.x;"
961 "} else if (bool(isWellBehaved)) {"
962 "x_t = length(p) - p.x * invR1;"
963 "} else {"
964 "float temp = p.x * p.x - p.y * p.y;"
965
966 // Only do sqrt if temp >= 0; this is significantly slower than
967 // checking temp >= 0 in the if statement that checks r(t) >= 0.
968 // But GPU may break if we sqrt a negative float. (Although I
969 // haven't observed that on any devices so far, and the old
970 // approach also does sqrt negative value without a check.) If
971 // the performance is really critical, maybe we should just
972 // compute the area where temp and x_t are always valid and drop
973 // all these ifs.
974 "if (temp >= 0) {"
975 "if (bool(isSwapped) || !bool(isRadiusIncreasing)) {"
976 "x_t = -sqrt(temp) - p.x * invR1;"
977 "} else {"
978 "x_t = sqrt(temp) - p.x * invR1;"
979 "}"
980 "}"
981 "}"
982
983 // The final calculation of t from x_t has lots of static
984 // optimizations but only do them when x_t is positive (which
985 // can be assumed true if isWellBehaved is true)
986 "if (!bool(isWellBehaved)) {"
987 // This will still calculate t even though it will be ignored
988 // later in the pipeline to avoid a branch
989 "if (x_t <= 0.0) {"
990 "v = -1;"
991 "}"
992 "}"
993 "if (bool(isRadiusIncreasing)) {"
994 "if (bool(isNativelyFocal)) {"
995 "t = x_t;"
996 "} else {"
997 "t = x_t + fx;"
998 "}"
999 "} else {"
1000 "if (bool(isNativelyFocal)) {"
1001 "t = -x_t;"
1002 "} else {"
1003 "t = -x_t + fx;"
1004 "}"
1005 "}"
1006
1007 "if (bool(isSwapped)) {"
1008 "t = 1 - t;"
1009 "}"
1010
1011 "return half4(half(t), v, 0, 0);"
1012 "}"
1013 );
1014
1015 const SkConicalGradient::FocalData& focalData = shader->getFocalData();
1016 bool isRadiusIncreasing = (1 - focalData.fFocalX) > 0,
1017 isFocalOnCircle = focalData.isFocalOnCircle(),
1018 isWellBehaved = focalData.isWellBehaved(), isSwapped = focalData.isSwapped(),
1019 isNativelyFocal = focalData.isNativelyFocal();
1020
1021 fp = GrSkSLFP::Make(kEffect, "TwoPointConicalFocalLayout", /*inputFP=*/nullptr,
1022 GrSkSLFP::OptFlags::kNone,
1023 "isRadiusIncreasing", GrSkSLFP::Specialize<int>(isRadiusIncreasing),
1024 "isFocalOnCircle", GrSkSLFP::Specialize<int>(isFocalOnCircle),
1025 "isWellBehaved", GrSkSLFP::Specialize<int>(isWellBehaved),
1026 "isSwapped", GrSkSLFP::Specialize<int>(isSwapped),
1027 "isNativelyFocal", GrSkSLFP::Specialize<int>(isNativelyFocal),
1028 "invR1", 1.0f / focalData.fR1,
1029 "fx", focalData.fFocalX);
1030 } break;
1031 }
1032 return GrGradientShader::MakeGradientFP(
1033 *shader, args, mRec, std::move(fp), SkOptAddressOrNull(matrix));
1034 }
1035
1036 static std::unique_ptr<GrFragmentProcessor> make_gradient_fp(const SkLinearGradient* shader,
1037 const GrFPArgs& args,
1038 const SkShaders::MatrixRec& mRec) {
1039 return GrGradientShader::MakeLinear(*shader, args, mRec);
1040 }
1041
1042 static std::unique_ptr<GrFragmentProcessor> make_gradient_fp(const SkRadialGradient* shader,
1043 const GrFPArgs& args,
1044 const SkShaders::MatrixRec& mRec) {
1045 static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(
1046 SkRuntimeEffect::MakeForShader,
1047 "float4 main(float2 coord) {"
1048 "return float4(length(coord), 1, 0, 0);" // y = 1 for always valid
1049 "}");
1050 // The radial gradient never rejects a pixel so it doesn't change opacity
1051 auto fp = GrSkSLFP::Make(
1052 effect, "RadialLayout", /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kPreservesOpaqueInput);
1053 return GrGradientShader::MakeGradientFP(*shader, args, mRec, std::move(fp));
1054 }
1055
1056 static std::unique_ptr<GrFragmentProcessor> make_gradient_fp(const SkSweepGradient* shader,
1057 const GrFPArgs& args,
1058 const SkShaders::MatrixRec& mRec) {
1059 // On some devices they incorrectly implement atan2(y,x) as atan(y/x). In actuality it is
1060 // atan2(y,x) = 2 * atan(y / (sqrt(x^2 + y^2) + x)). So to work around this we pass in (sqrt(x^2
1061 // + y^2) + x) as the second parameter to atan2 in these cases. We let the device handle the
1062 // undefined behavior of the second paramenter being 0 instead of doing the divide ourselves and
1063 // using atan instead.
1064 int useAtanWorkaround =
1065 args.fSurfaceDrawContext->caps()->shaderCaps()->fAtan2ImplementedAsAtanYOverX;
1066 static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
1067 "uniform half bias;"
1068 "uniform half scale;"
1069 "uniform int useAtanWorkaround;" // specialized
1070
1071 "half4 main(float2 coord) {"
1072 "half angle;"
1073 "if (bool(useAtanWorkaround)) {"
1074 "angle = half(2 * atan(-coord.y, length(coord) - coord.x));"
1075 "} else {"
1076 // Hardcode pi/2 for the angle when x == 0, to avoid undefined behavior in this
1077 // case. This hasn't proven to be necessary in the atan workaround case.
1078 "angle = (coord.x != 0) ? half(atan(-coord.y, -coord.x)) :"
1079 " sign(coord.y) * -1.5707963267949;"
1080 "}"
1081
1082 // 0.1591549430918 is 1/(2*pi), used since atan returns values [-pi, pi]
1083 "half t = (angle * 0.1591549430918 + 0.5 + bias) * scale;"
1084 "return half4(t, 1, 0, 0);" // y = 1 for always valid
1085 "}"
1086 );
1087
1088 // The sweep gradient never rejects a pixel so it doesn't change opacity
1089 auto fp = GrSkSLFP::Make(effect, "SweepLayout", /*inputFP=*/nullptr,
1090 GrSkSLFP::OptFlags::kPreservesOpaqueInput,
1091 "bias", shader->tBias(),
1092 "scale", shader->tScale(),
1093 "useAtanWorkaround", GrSkSLFP::Specialize(useAtanWorkaround));
1094 return GrGradientShader::MakeGradientFP(*shader, args, mRec, std::move(fp));
1095 }
1096
1097 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkGradientBaseShader* shader,
1098 const GrFPArgs& args,
1099 const SkShaders::MatrixRec& mRec) {
1100 SkASSERT(shader);
1101
1102 switch (shader->asGradient()) {
1103 #define M(type) \
1104 case SkShaderBase::GradientType::k##type: \
1105 return make_gradient_fp(static_cast<const Sk##type##Gradient*>(shader), args, mRec);
1106 SK_ALL_GRADIENTS(M)
1107 #undef M
1108 case SkShaderBase::GradientType::kNone:
1109 SkDEBUGFAIL("Gradient shader says its type is none");
1110 return nullptr;
1111 }
1112 SkUNREACHABLE;
1113 }
1114
1115 std::unique_ptr<GrFragmentProcessor> Make(const SkShader* shader,
1116 const GrFPArgs& args,
1117 const SkMatrix& ctm) {
1118 return Make(shader, args, SkShaders::MatrixRec(ctm));
1119 }
1120
1121 std::unique_ptr<GrFragmentProcessor> Make(const SkShader* shader,
1122 const GrFPArgs& args,
1123 const SkShaders::MatrixRec& mRec) {
1124 if (!shader) {
1125 return nullptr;
1126 }
1127 auto base = as_SB(shader);
1128 switch (base->type()) {
1129 #define M(type) \
1130 case SkShaderBase::ShaderType::k##type: \
1131 return make_shader_fp(static_cast<const Sk##type##Shader*>(base), args, mRec);
1132 SK_ALL_SHADERS(M)
1133 #undef M
1134 }
1135 SkUNREACHABLE;
1136 }
1137
1138 } // namespace GrFragmentProcessors