Debian Codegraph @

src/gpu/ganesh/GrFragmentProcessors.cpp

raw ◀ ▶
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 isProtected =
759 args.fSurfaceDrawContext->asSurfaceProxy()->isProtected() ==
760 GrProtected::kYes;
761 auto image = info.makeImage(SkSurfaces::RenderTarget(ctx,
762 skgpu::Budgeted::kYes,
763 info.imageInfo,
764 msaaSampleCount,
765 kTopLeft_GrSurfaceOrigin,
766 &info.props,
767 createWithMips,
•768 isProtected),
769 shader->picture().get());
770 if (!image) {
771 return nullptr;
772 }
773 // Images from a picture shader shouldn't ever be drawn into a surface backed by the same
774 // texture so there will never be need to make a copy of the image. So just pass in nullptr
775 // for targetSurface of the view.
776 constexpr GrRenderTargetProxy* targetSurface = nullptr;
777 auto [v, ct] = skgpu::ganesh::AsView(ctx, image, skgpu::Mipmapped::kNo, targetSurface);
778 view = std::move(v);
779 provider->assignUniqueKeyToProxy(key, view.asTextureProxy());
780 }
781
782 const GrSamplerState sampler(static_cast<GrSamplerState::WrapMode>(shader->tileModeX()),
783 static_cast<GrSamplerState::WrapMode>(shader->tileModeY()),
784 shader->filter());
785 auto fp = GrTextureEffect::Make(
786 std::move(view), kPremul_SkAlphaType, SkMatrix::I(), sampler, *ctx->priv().caps());
787 SkMatrix scale = SkMatrix::Scale(info.tileScale.width(), info.tileScale.height());
788 auto [total, ok] = mRec.applyForFragmentProcessor(scale);
789 if (!ok) {
790 return nullptr;
791 }
792 return GrMatrixEffect::Make(total, std::move(fp));
793 }
794
795 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkRuntimeShader* shader,
796 const GrFPArgs& args,
797 const SkShaders::MatrixRec& mRec) {
798 if (!SkRuntimeEffectPriv::CanDraw(args.fSurfaceDrawContext->caps(),
799 shader->asRuntimeEffect())) {
800 return nullptr;
801 }
802
803 sk_sp<const SkData> uniforms = SkRuntimeEffectPriv::TransformUniforms(
804 shader->asRuntimeEffect()->uniforms(),
805 shader->uniformData(args.fDstColorInfo->colorSpace()),
806 args.fDstColorInfo->colorSpace());
807 SkASSERT(uniforms);
808
809 bool success;
810 std::unique_ptr<GrFragmentProcessor> fp;
811 GrFPArgs childArgs(args.fSurfaceDrawContext, args.fDstColorInfo, args.fSurfaceProps,
812 GrFPArgs::Scope::kRuntimeEffect);
813 std::tie(success, fp) = make_effect_fp(shader->effect(),
814 "runtime_shader",
815 std::move(uniforms),
816 /*inputFP=*/nullptr,
817 /*destColorFP=*/nullptr,
818 shader->children(),
819 childArgs);
820 if (!success) {
821 return nullptr;
822 }
823
824 auto [total, ok] = mRec.applyForFragmentProcessor({});
825 if (!ok) {
826 return nullptr;
827 }
828 return GrMatrixEffect::Make(total, std::move(fp));
829 }
830
831 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkTransformShader* shader,
832 const GrFPArgs&,
833 const SkShaders::MatrixRec&) {
834 return nullptr;
835 }
836
837 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkTriColorShader* shader,
838 const GrFPArgs&,
839 const SkShaders::MatrixRec&) {
840 return nullptr;
841 }
842
843 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkWorkingColorSpaceShader* shader,
844 const GrFPArgs& args,
845 const SkShaders::MatrixRec& mRec) {
846 const GrColorInfo* dstInfo = args.fDstColorInfo;
847 SkAlphaType dstAT = dstInfo->alphaType();
848 sk_sp<SkColorSpace> dstCS = dstInfo->refColorSpace();
849 if (!dstCS) {
850 dstCS = SkColorSpace::MakeSRGB();
851 }
852
853
854 auto [inputCS, outputCS, workingAT] = shader->workingSpace(dstCS, dstAT);
855
856 GrColorInfo dst = {dstInfo->colorType(), dstAT, dstCS},
857 input = {dstInfo->colorType(), workingAT, inputCS},
858 output = {dstInfo->colorType(), workingAT, outputCS};
859
860 GrFPArgs workingArgs(args.fSurfaceDrawContext, &input, args.fSurfaceProps, args.fScope);
861
862 auto childFP = Make(shader->shader().get(), workingArgs, mRec);
863 if (!childFP) {
864 return nullptr;
865 }
866
867 auto childWithWorkingInput = GrFragmentProcessor::Compose(
868 std::move(childFP), GrColorSpaceXformEffect::Make(nullptr, dst, input));
869 // Assuming that childFP operates on inputCS/workingAT values and returns outputCS/workingAT
870 // values, then we only need to transform from output to dst.
871 return GrColorSpaceXformEffect::Make(std::move(childWithWorkingInput), output, dst);
872 }
873
874 //////////////////////////////////////////////////////////////////////////////////////////////
875
876 static std::unique_ptr<GrFragmentProcessor> make_gradient_fp(const SkConicalGradient* shader,
877 const GrFPArgs& args,
878 const SkShaders::MatrixRec& mRec) {
879 // The 2 point conical gradient can reject a pixel so it does change opacity even if the input
880 // was opaque. Thus, all of these layout FPs disable that optimization.
881 std::unique_ptr<GrFragmentProcessor> fp;
882 std::optional<SkMatrix> matrix;
883 switch (shader->getType()) {
884 case SkConicalGradient::Type::kStrip: {
885 static const SkRuntimeEffect* kEffect =
886 SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
887 "uniform half r0_2;"
888 "half4 main(float2 p) {"
889 // validation flag, set to negative to discard fragment later.
890 "half v = 1;"
891 "float t = r0_2 - p.y * p.y;"
892 "if (t >= 0) {"
893 "t = p.x + sqrt(t);"
894 "} else {"
895 "v = -1;"
896 "}"
897 "return half4(half(t), v, 0, 0);"
898 "}"
899 );
900 float r0 = shader->getStartRadius() / shader->getCenterX1();
901 fp = GrSkSLFP::Make(kEffect,
902 "TwoPointConicalStripLayout",
903 /*inputFP=*/nullptr,
904 GrSkSLFP::OptFlags::kNone,
905 "r0_2",
906 r0 * r0);
907 } break;
908
909 case SkConicalGradient::Type::kRadial: {
910 static const SkRuntimeEffect* kEffect =
911 SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
912 "uniform half r0;"
913 "uniform half lengthScale;"
914 "half4 main(float2 p) {"
915 // validation flag, set to negative to discard fragment later
916 "half v = 1;"
917 "float t = length(p) * lengthScale - r0;"
918 "return half4(half(t), v, 0, 0);"
919 "}"
920 );
921 float dr = shader->getDiffRadius();
922 float r0 = shader->getStartRadius() / dr;
923 bool isRadiusIncreasing = dr >= 0;
924 fp = GrSkSLFP::Make(kEffect,
925 "TwoPointConicalRadialLayout",
926 /*inputFP=*/nullptr,
927 GrSkSLFP::OptFlags::kNone,
928 "r0",
929 r0,
930 "lengthScale",
931 isRadiusIncreasing ? 1.0f : -1.0f);
932
933 // GPU radial matrix is different from the original matrix, since we map the diff radius
934 // to have |dr| = 1, so manually compute the final gradient matrix here.
935
936 // Map center to (0, 0)
937 matrix = SkMatrix::Translate(-shader->getStartCenter().fX,
938 -shader->getStartCenter().fY);
939 // scale |diffRadius| to 1
940 matrix->postScale(1 / dr, 1 / dr);
941 } break;
942
943 case SkConicalGradient::Type::kFocal: {
944 static const SkRuntimeEffect* kEffect =
945 SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
946 // Optimization flags, all specialized:
947 "uniform int isRadiusIncreasing;"
948 "uniform int isFocalOnCircle;"
949 "uniform int isWellBehaved;"
950 "uniform int isSwapped;"
951 "uniform int isNativelyFocal;"
952
953 "uniform half invR1;" // 1/r1
954 "uniform half fx;" // focalX = r0/(r0-r1)
955
956 "half4 main(float2 p) {"
957 "float t = -1;"
958 "half v = 1;" // validation flag,set to negative to discard fragment later
959
960 "float x_t = -1;"
961 "if (bool(isFocalOnCircle)) {"
962 "x_t = dot(p, p) / p.x;"
963 "} else if (bool(isWellBehaved)) {"
964 "x_t = length(p) - p.x * invR1;"
965 "} else {"
966 "float temp = p.x * p.x - p.y * p.y;"
967
968 // Only do sqrt if temp >= 0; this is significantly slower than
969 // checking temp >= 0 in the if statement that checks r(t) >= 0.
970 // But GPU may break if we sqrt a negative float. (Although I
971 // haven't observed that on any devices so far, and the old
972 // approach also does sqrt negative value without a check.) If
973 // the performance is really critical, maybe we should just
974 // compute the area where temp and x_t are always valid and drop
975 // all these ifs.
976 "if (temp >= 0) {"
977 "if (bool(isSwapped) || !bool(isRadiusIncreasing)) {"
978 "x_t = -sqrt(temp) - p.x * invR1;"
979 "} else {"
980 "x_t = sqrt(temp) - p.x * invR1;"
981 "}"
982 "}"
983 "}"
984
985 // The final calculation of t from x_t has lots of static
986 // optimizations but only do them when x_t is positive (which
987 // can be assumed true if isWellBehaved is true)
988 "if (!bool(isWellBehaved)) {"
989 // This will still calculate t even though it will be ignored
990 // later in the pipeline to avoid a branch
991 "if (x_t <= 0.0) {"
992 "v = -1;"
993 "}"
994 "}"
995 "if (bool(isRadiusIncreasing)) {"
996 "if (bool(isNativelyFocal)) {"
997 "t = x_t;"
998 "} else {"
999 "t = x_t + fx;"
1000 "}"
1001 "} else {"
1002 "if (bool(isNativelyFocal)) {"
1003 "t = -x_t;"
1004 "} else {"
1005 "t = -x_t + fx;"
1006 "}"
1007 "}"
1008
1009 "if (bool(isSwapped)) {"
1010 "t = 1 - t;"
1011 "}"
1012
1013 "return half4(half(t), v, 0, 0);"
1014 "}"
1015 );
1016
1017 const SkConicalGradient::FocalData& focalData = shader->getFocalData();
1018 bool isRadiusIncreasing = (1 - focalData.fFocalX) > 0,
1019 isFocalOnCircle = focalData.isFocalOnCircle(),
1020 isWellBehaved = focalData.isWellBehaved(), isSwapped = focalData.isSwapped(),
1021 isNativelyFocal = focalData.isNativelyFocal();
1022
1023 fp = GrSkSLFP::Make(kEffect, "TwoPointConicalFocalLayout", /*inputFP=*/nullptr,
1024 GrSkSLFP::OptFlags::kNone,
1025 "isRadiusIncreasing", GrSkSLFP::Specialize<int>(isRadiusIncreasing),
1026 "isFocalOnCircle", GrSkSLFP::Specialize<int>(isFocalOnCircle),
1027 "isWellBehaved", GrSkSLFP::Specialize<int>(isWellBehaved),
1028 "isSwapped", GrSkSLFP::Specialize<int>(isSwapped),
1029 "isNativelyFocal", GrSkSLFP::Specialize<int>(isNativelyFocal),
1030 "invR1", 1.0f / focalData.fR1,
1031 "fx", focalData.fFocalX);
1032 } break;
1033 }
1034 return GrGradientShader::MakeGradientFP(
1035 *shader, args, mRec, std::move(fp), SkOptAddressOrNull(matrix));
1036 }
1037
1038 static std::unique_ptr<GrFragmentProcessor> make_gradient_fp(const SkLinearGradient* shader,
1039 const GrFPArgs& args,
1040 const SkShaders::MatrixRec& mRec) {
1041 return GrGradientShader::MakeLinear(*shader, args, mRec);
1042 }
1043
1044 static std::unique_ptr<GrFragmentProcessor> make_gradient_fp(const SkRadialGradient* shader,
1045 const GrFPArgs& args,
1046 const SkShaders::MatrixRec& mRec) {
1047 static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(
1048 SkRuntimeEffect::MakeForShader,
1049 "float4 main(float2 coord) {"
1050 "return float4(length(coord), 1, 0, 0);" // y = 1 for always valid
1051 "}");
1052 // The radial gradient never rejects a pixel so it doesn't change opacity
1053 auto fp = GrSkSLFP::Make(
1054 effect, "RadialLayout", /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kPreservesOpaqueInput);
1055 return GrGradientShader::MakeGradientFP(*shader, args, mRec, std::move(fp));
1056 }
1057
1058 static std::unique_ptr<GrFragmentProcessor> make_gradient_fp(const SkSweepGradient* shader,
1059 const GrFPArgs& args,
1060 const SkShaders::MatrixRec& mRec) {
1061 // On some devices they incorrectly implement atan2(y,x) as atan(y/x). In actuality it is
1062 // atan2(y,x) = 2 * atan(y / (sqrt(x^2 + y^2) + x)). So to work around this we pass in (sqrt(x^2
1063 // + y^2) + x) as the second parameter to atan2 in these cases. We let the device handle the
1064 // undefined behavior of the second paramenter being 0 instead of doing the divide ourselves and
1065 // using atan instead.
1066 int useAtanWorkaround =
1067 args.fSurfaceDrawContext->caps()->shaderCaps()->fAtan2ImplementedAsAtanYOverX;
1068 static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
1069 "uniform half bias;"
1070 "uniform half scale;"
1071 "uniform int useAtanWorkaround;" // specialized
1072
1073 "half4 main(float2 coord) {"
1074 "half angle;"
1075 "if (bool(useAtanWorkaround)) {"
1076 "angle = half(2 * atan(-coord.y, length(coord) - coord.x));"
1077 "} else {"
1078 // Hardcode pi/2 for the angle when x == 0, to avoid undefined behavior in this
1079 // case. This hasn't proven to be necessary in the atan workaround case.
1080 "angle = (coord.x != 0) ? half(atan(-coord.y, -coord.x)) :"
1081 " sign(coord.y) * -1.5707963267949;"
1082 "}"
1083
1084 // 0.1591549430918 is 1/(2*pi), used since atan returns values [-pi, pi]
1085 "half t = (angle * 0.1591549430918 + 0.5 + bias) * scale;"
1086 "return half4(t, 1, 0, 0);" // y = 1 for always valid
1087 "}"
1088 );
1089
1090 // The sweep gradient never rejects a pixel so it doesn't change opacity
1091 auto fp = GrSkSLFP::Make(effect, "SweepLayout", /*inputFP=*/nullptr,
1092 GrSkSLFP::OptFlags::kPreservesOpaqueInput,
1093 "bias", shader->tBias(),
1094 "scale", shader->tScale(),
1095 "useAtanWorkaround", GrSkSLFP::Specialize(useAtanWorkaround));
1096 return GrGradientShader::MakeGradientFP(*shader, args, mRec, std::move(fp));
1097 }
1098
1099 static std::unique_ptr<GrFragmentProcessor> make_shader_fp(const SkGradientBaseShader* shader,
1100 const GrFPArgs& args,
1101 const SkShaders::MatrixRec& mRec) {
1102 SkASSERT(shader);
1103
1104 switch (shader->asGradient()) {
1105 #define M(type) \
1106 case SkShaderBase::GradientType::k##type: \
1107 return make_gradient_fp(static_cast<const Sk##type##Gradient*>(shader), args, mRec);
1108 SK_ALL_GRADIENTS(M)
1109 #undef M
1110 case SkShaderBase::GradientType::kNone:
1111 SkDEBUGFAIL("Gradient shader says its type is none");
1112 return nullptr;
1113 }
1114 SkUNREACHABLE;
1115 }
1116
1117 std::unique_ptr<GrFragmentProcessor> Make(const SkShader* shader,
1118 const GrFPArgs& args,
1119 const SkMatrix& ctm) {
1120 return Make(shader, args, SkShaders::MatrixRec(ctm));
1121 }
1122
1123 std::unique_ptr<GrFragmentProcessor> Make(const SkShader* shader,
1124 const GrFPArgs& args,
1125 const SkShaders::MatrixRec& mRec) {
1126 if (!shader) {
1127 return nullptr;
1128 }
1129 auto base = as_SB(shader);
1130 switch (base->type()) {
1131 #define M(type) \
1132 case SkShaderBase::ShaderType::k##type: \
1133 return make_shader_fp(static_cast<const Sk##type##Shader*>(base), args, mRec);
1134 SK_ALL_SHADERS(M)
1135 #undef M
1136 }
1137 SkUNREACHABLE;
1138 }
1139
1140 } // namespace GrFragmentProcessors