• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2 * Copyright 2015 Google Inc.
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/GrFragmentProcessor.h"
9 
10 #include "src/core/SkRuntimeEffectPriv.h"
11 #include "src/gpu/KeyBuilder.h"
12 #include "src/gpu/ganesh/GrPipeline.h"
13 #include "src/gpu/ganesh/GrProcessorAnalysis.h"
14 #include "src/gpu/ganesh/GrShaderCaps.h"
15 #include "src/gpu/ganesh/effects/GrBlendFragmentProcessor.h"
16 #include "src/gpu/ganesh/effects/GrSkSLFP.h"
17 #include "src/gpu/ganesh/effects/GrTextureEffect.h"
18 #include "src/gpu/ganesh/glsl/GrGLSLFragmentShaderBuilder.h"
19 #include "src/gpu/ganesh/glsl/GrGLSLProgramBuilder.h"
20 #include "src/gpu/ganesh/glsl/GrGLSLProgramDataManager.h"
21 #include "src/gpu/ganesh/glsl/GrGLSLUniformHandler.h"
22 
isEqual(const GrFragmentProcessor & that) const23 bool GrFragmentProcessor::isEqual(const GrFragmentProcessor& that) const {
24     if (this->classID() != that.classID()) {
25         return false;
26     }
27     if (this->sampleUsage() != that.sampleUsage()) {
28         return false;
29     }
30     if (!this->onIsEqual(that)) {
31         return false;
32     }
33     if (this->numChildProcessors() != that.numChildProcessors()) {
34         return false;
35     }
36     for (int i = 0; i < this->numChildProcessors(); ++i) {
37         auto thisChild = this->childProcessor(i),
38              thatChild = that .childProcessor(i);
39         if (SkToBool(thisChild) != SkToBool(thatChild)) {
40             return false;
41         }
42         if (thisChild && !thisChild->isEqual(*thatChild)) {
43             return false;
44         }
45     }
46     return true;
47 }
48 
visitProxies(const GrVisitProxyFunc & func) const49 void GrFragmentProcessor::visitProxies(const GrVisitProxyFunc& func) const {
50     this->visitTextureEffects([&func](const GrTextureEffect& te) {
51         func(te.view().proxy(), te.samplerState().mipmapped());
52     });
53 }
54 
visitTextureEffects(const std::function<void (const GrTextureEffect &)> & func) const55 void GrFragmentProcessor::visitTextureEffects(
56         const std::function<void(const GrTextureEffect&)>& func) const {
57     if (auto* te = this->asTextureEffect()) {
58         func(*te);
59     }
60     for (auto& child : fChildProcessors) {
61         if (child) {
62             child->visitTextureEffects(func);
63         }
64     }
65 }
66 
visitWithImpls(const std::function<void (const GrFragmentProcessor &,ProgramImpl &)> & f,ProgramImpl & impl) const67 void GrFragmentProcessor::visitWithImpls(
68         const std::function<void(const GrFragmentProcessor&, ProgramImpl&)>& f,
69         ProgramImpl& impl) const {
70     f(*this, impl);
71     SkASSERT(impl.numChildProcessors() == this->numChildProcessors());
72     for (int i = 0; i < this->numChildProcessors(); ++i) {
73         if (const auto* child = this->childProcessor(i)) {
74             child->visitWithImpls(f, *impl.childProcessor(i));
75         }
76     }
77 }
78 
asTextureEffect()79 GrTextureEffect* GrFragmentProcessor::asTextureEffect() {
80     if (this->classID() == kGrTextureEffect_ClassID) {
81         return static_cast<GrTextureEffect*>(this);
82     }
83     return nullptr;
84 }
85 
asTextureEffect() const86 const GrTextureEffect* GrFragmentProcessor::asTextureEffect() const {
87     if (this->classID() == kGrTextureEffect_ClassID) {
88         return static_cast<const GrTextureEffect*>(this);
89     }
90     return nullptr;
91 }
92 
93 #if GR_TEST_UTILS
recursive_dump_tree_info(const GrFragmentProcessor & fp,SkString indent,SkString * text)94 static void recursive_dump_tree_info(const GrFragmentProcessor& fp,
95                                      SkString indent,
96                                      SkString* text) {
97     for (int index = 0; index < fp.numChildProcessors(); ++index) {
98         text->appendf("\n%s(#%d) -> ", indent.c_str(), index);
99         if (const GrFragmentProcessor* childFP = fp.childProcessor(index)) {
100             text->append(childFP->dumpInfo());
101             indent.append("\t");
102             recursive_dump_tree_info(*childFP, indent, text);
103         } else {
104             text->append("null");
105         }
106     }
107 }
108 
dumpTreeInfo() const109 SkString GrFragmentProcessor::dumpTreeInfo() const {
110     SkString text = this->dumpInfo();
111     recursive_dump_tree_info(*this, SkString("\t"), &text);
112     text.append("\n");
113     return text;
114 }
115 #endif
116 
makeProgramImpl() const117 std::unique_ptr<GrFragmentProcessor::ProgramImpl> GrFragmentProcessor::makeProgramImpl() const {
118     std::unique_ptr<ProgramImpl> impl = this->onMakeProgramImpl();
119     impl->fChildProcessors.push_back_n(fChildProcessors.size());
120     for (int i = 0; i < fChildProcessors.size(); ++i) {
121         impl->fChildProcessors[i] = fChildProcessors[i] ? fChildProcessors[i]->makeProgramImpl()
122                                                         : nullptr;
123     }
124     return impl;
125 }
126 
numNonNullChildProcessors() const127 int GrFragmentProcessor::numNonNullChildProcessors() const {
128     return std::count_if(fChildProcessors.begin(), fChildProcessors.end(),
129                          [](const auto& c) { return c != nullptr; });
130 }
131 
132 #ifdef SK_DEBUG
isInstantiated() const133 bool GrFragmentProcessor::isInstantiated() const {
134     bool result = true;
135     this->visitTextureEffects([&result](const GrTextureEffect& te) {
136         if (!te.texture()) {
137             result = false;
138         }
139     });
140     return result;
141 }
142 #endif
143 
registerChild(std::unique_ptr<GrFragmentProcessor> child,SkSL::SampleUsage sampleUsage)144 void GrFragmentProcessor::registerChild(std::unique_ptr<GrFragmentProcessor> child,
145                                         SkSL::SampleUsage sampleUsage) {
146     SkASSERT(sampleUsage.isSampled());
147 
148     if (!child) {
149         fChildProcessors.push_back(nullptr);
150         return;
151     }
152 
153     // The child should not have been attached to another FP already and not had any sampling
154     // strategy set on it.
155     SkASSERT(!child->fParent && !child->sampleUsage().isSampled());
156 
157     // Configure child's sampling state first
158     child->fUsage = sampleUsage;
159 
160     // Propagate the "will read dest-color" flag up to parent FPs.
161     if (child->willReadDstColor()) {
162         this->setWillReadDstColor();
163     }
164 
165     // If this child receives passthrough or matrix transformed coords from its parent then note
166     // that the parent's coords are used indirectly to ensure that they aren't omitted.
167     if ((sampleUsage.isPassThrough() || sampleUsage.isUniformMatrix()) &&
168         child->usesSampleCoords()) {
169         fFlags |= kUsesSampleCoordsIndirectly_Flag;
170     }
171 
172     // Record that the child is attached to us; this FP is the source of any uniform data needed
173     // to evaluate the child sample matrix.
174     child->fParent = this;
175     fChildProcessors.push_back(std::move(child));
176 
177     // Validate: our sample strategy comes from a parent we shouldn't have yet.
178     SkASSERT(!fUsage.isSampled() && !fParent);
179 }
180 
cloneAndRegisterAllChildProcessors(const GrFragmentProcessor & src)181 void GrFragmentProcessor::cloneAndRegisterAllChildProcessors(const GrFragmentProcessor& src) {
182     for (int i = 0; i < src.numChildProcessors(); ++i) {
183         if (auto fp = src.childProcessor(i)) {
184             this->registerChild(fp->clone(), fp->sampleUsage());
185         } else {
186             this->registerChild(nullptr);
187         }
188     }
189 }
190 
MakeColor(SkPMColor4f color)191 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MakeColor(SkPMColor4f color) {
192     // Use ColorFilter signature/factory to get the constant output for constant input optimization
193     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter,
194         "uniform half4 color;"
195         "half4 main(half4 inColor) { return color; }"
196     );
197     SkASSERT(SkRuntimeEffectPriv::SupportsConstantOutputForConstantInput(effect));
198     return GrSkSLFP::Make(effect, "color_fp", /*inputFP=*/nullptr,
199                           color.isOpaque() ? GrSkSLFP::OptFlags::kPreservesOpaqueInput
200                                            : GrSkSLFP::OptFlags::kNone,
201                           "color", color);
202 }
203 
MulInputByChildAlpha(std::unique_ptr<GrFragmentProcessor> fp)204 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::MulInputByChildAlpha(
205         std::unique_ptr<GrFragmentProcessor> fp) {
206     if (!fp) {
207         return nullptr;
208     }
209     return GrBlendFragmentProcessor::Make<SkBlendMode::kSrcIn>(/*src=*/nullptr, std::move(fp));
210 }
211 
ApplyPaintAlpha(std::unique_ptr<GrFragmentProcessor> child)212 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ApplyPaintAlpha(
213         std::unique_ptr<GrFragmentProcessor> child) {
214     SkASSERT(child);
215     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter,
216         "uniform colorFilter fp;"
217         "half4 main(half4 inColor) {"
218             "return fp.eval(inColor.rgb1) * inColor.a;"
219         "}"
220     );
221     return GrSkSLFP::Make(effect, "ApplyPaintAlpha", /*inputFP=*/nullptr,
222                           GrSkSLFP::OptFlags::kPreservesOpaqueInput |
223                           GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha,
224                           "fp", std::move(child));
225 }
226 
ModulateRGBA(std::unique_ptr<GrFragmentProcessor> inputFP,const SkPMColor4f & color)227 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ModulateRGBA(
228         std::unique_ptr<GrFragmentProcessor> inputFP, const SkPMColor4f& color) {
229     auto colorFP = MakeColor(color);
230     return GrBlendFragmentProcessor::Make<SkBlendMode::kModulate>(std::move(colorFP),
231                                                                   std::move(inputFP));
232 }
233 
ClampOutput(std::unique_ptr<GrFragmentProcessor> fp)234 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ClampOutput(
235         std::unique_ptr<GrFragmentProcessor> fp) {
236     SkASSERT(fp);
237     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter,
238         "half4 main(half4 inColor) {"
239             "return saturate(inColor);"
240         "}"
241     );
242     SkASSERT(SkRuntimeEffectPriv::SupportsConstantOutputForConstantInput(effect));
243     return GrSkSLFP::Make(effect, "Clamp", std::move(fp),
244                           GrSkSLFP::OptFlags::kPreservesOpaqueInput);
245 }
246 
SwizzleOutput(std::unique_ptr<GrFragmentProcessor> fp,const skgpu::Swizzle & swizzle)247 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SwizzleOutput(
248         std::unique_ptr<GrFragmentProcessor> fp, const skgpu::Swizzle& swizzle) {
249     class SwizzleFragmentProcessor : public GrFragmentProcessor {
250     public:
251         static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp,
252                                                          const skgpu::Swizzle& swizzle) {
253             return std::unique_ptr<GrFragmentProcessor>(
254                     new SwizzleFragmentProcessor(std::move(fp), swizzle));
255         }
256 
257         const char* name() const override { return "Swizzle"; }
258 
259         std::unique_ptr<GrFragmentProcessor> clone() const override {
260             return Make(this->childProcessor(0)->clone(), fSwizzle);
261         }
262 
263     private:
264         SwizzleFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp,
265                                  const skgpu::Swizzle& swizzle)
266                 : INHERITED(kSwizzleFragmentProcessor_ClassID, ProcessorOptimizationFlags(fp.get()))
267                 , fSwizzle(swizzle) {
268             this->registerChild(std::move(fp));
269         }
270 
271         std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override {
272             class Impl : public ProgramImpl {
273             public:
274                 void emitCode(EmitArgs& args) override {
275                     SkString childColor = this->invokeChild(0, args);
276 
277                     const SwizzleFragmentProcessor& sfp = args.fFp.cast<SwizzleFragmentProcessor>();
278                     const skgpu::Swizzle& swizzle = sfp.fSwizzle;
279                     GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
280 
281                     fragBuilder->codeAppendf("return %s.%s;",
282                                              childColor.c_str(), swizzle.asString().c_str());
283                 }
284             };
285             return std::make_unique<Impl>();
286         }
287 
288         void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder* b) const override {
289             b->add32(fSwizzle.asKey());
290         }
291 
292         bool onIsEqual(const GrFragmentProcessor& other) const override {
293             const SwizzleFragmentProcessor& sfp = other.cast<SwizzleFragmentProcessor>();
294             return fSwizzle == sfp.fSwizzle;
295         }
296 
297         SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
298             return fSwizzle.applyTo(ConstantOutputForConstantInput(this->childProcessor(0), input));
299         }
300 
301         skgpu::Swizzle fSwizzle;
302 
303         using INHERITED = GrFragmentProcessor;
304     };
305 
306     if (!fp) {
307         return nullptr;
308     }
309     if (skgpu::Swizzle::RGBA() == swizzle) {
310         return fp;
311     }
312     return SwizzleFragmentProcessor::Make(std::move(fp), swizzle);
313 }
314 
315 //////////////////////////////////////////////////////////////////////////////
316 
OverrideInput(std::unique_ptr<GrFragmentProcessor> fp,const SkPMColor4f & color)317 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::OverrideInput(
318         std::unique_ptr<GrFragmentProcessor> fp, const SkPMColor4f& color) {
319     if (!fp) {
320         return nullptr;
321     }
322     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter,
323         "uniform colorFilter fp;"  // Declared as colorFilter so we can pass a color
324         "uniform half4 color;"
325         "half4 main(half4 inColor) {"
326             "return fp.eval(color);"
327         "}"
328     );
329     SkASSERT(SkRuntimeEffectPriv::SupportsConstantOutputForConstantInput(effect));
330     return GrSkSLFP::Make(effect, "OverrideInput", /*inputFP=*/nullptr,
331                           color.isOpaque() ? GrSkSLFP::OptFlags::kPreservesOpaqueInput
332                                            : GrSkSLFP::OptFlags::kNone,
333                           "fp", std::move(fp),
334                           "color", color);
335 }
336 
337 //////////////////////////////////////////////////////////////////////////////
338 
DisableCoverageAsAlpha(std::unique_ptr<GrFragmentProcessor> fp)339 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::DisableCoverageAsAlpha(
340         std::unique_ptr<GrFragmentProcessor> fp) {
341     if (!fp || !fp->compatibleWithCoverageAsAlpha()) {
342         return fp;
343     }
344     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter,
345         "half4 main(half4 inColor) { return inColor; }"
346     );
347     SkASSERT(SkRuntimeEffectPriv::SupportsConstantOutputForConstantInput(effect));
348     return GrSkSLFP::Make(effect, "DisableCoverageAsAlpha", std::move(fp),
349                           GrSkSLFP::OptFlags::kPreservesOpaqueInput);
350 }
351 
352 //////////////////////////////////////////////////////////////////////////////
353 
DestColor()354 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::DestColor() {
355     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForBlender,
356         "half4 main(half4 src, half4 dst) {"
357             "return dst;"
358         "}"
359     );
360     return GrSkSLFP::Make(effect, "DestColor", /*inputFP=*/nullptr, GrSkSLFP::OptFlags::kNone);
361 }
362 
363 //////////////////////////////////////////////////////////////////////////////
364 
Compose(std::unique_ptr<GrFragmentProcessor> f,std::unique_ptr<GrFragmentProcessor> g)365 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::Compose(
366         std::unique_ptr<GrFragmentProcessor> f, std::unique_ptr<GrFragmentProcessor> g) {
367     class ComposeProcessor : public GrFragmentProcessor {
368     public:
369         static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> f,
370                                                          std::unique_ptr<GrFragmentProcessor> g) {
371             return std::unique_ptr<GrFragmentProcessor>(new ComposeProcessor(std::move(f),
372                                                                              std::move(g)));
373         }
374 
375         const char* name() const override { return "Compose"; }
376 
377         std::unique_ptr<GrFragmentProcessor> clone() const override {
378             return std::unique_ptr<GrFragmentProcessor>(new ComposeProcessor(*this));
379         }
380 
381     private:
382         std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override {
383             class Impl : public ProgramImpl {
384             public:
385                 void emitCode(EmitArgs& args) override {
386                     SkString result = this->invokeChild(1, args);         // g(x)
387                     result = this->invokeChild(0, result.c_str(), args);  // f(g(x))
388                     args.fFragBuilder->codeAppendf("return %s;", result.c_str());
389                 }
390             };
391             return std::make_unique<Impl>();
392         }
393 
394         ComposeProcessor(std::unique_ptr<GrFragmentProcessor> f,
395                          std::unique_ptr<GrFragmentProcessor> g)
396                 : INHERITED(kSeriesFragmentProcessor_ClassID,
397                             f->optimizationFlags() & g->optimizationFlags()) {
398             this->registerChild(std::move(f));
399             this->registerChild(std::move(g));
400         }
401 
402         ComposeProcessor(const ComposeProcessor& that) : INHERITED(that) {}
403 
404         void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
405 
406         bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
407 
408         SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& inColor) const override {
409             SkPMColor4f color = inColor;
410             color = ConstantOutputForConstantInput(this->childProcessor(1), color);
411             color = ConstantOutputForConstantInput(this->childProcessor(0), color);
412             return color;
413         }
414 
415         using INHERITED = GrFragmentProcessor;
416     };
417 
418     // Allow either of the composed functions to be null.
419     if (f == nullptr) {
420         return g;
421     }
422     if (g == nullptr) {
423         return f;
424     }
425 
426     // Run an optimization pass on this composition.
427     GrProcessorAnalysisColor inputColor;
428     inputColor.setToUnknown();
429 
430     std::unique_ptr<GrFragmentProcessor> series[2] = {std::move(g), std::move(f)};
431     GrColorFragmentProcessorAnalysis info(inputColor, series, std::size(series));
432 
433     SkPMColor4f knownColor;
434     int leadingFPsToEliminate = info.initialProcessorsToEliminate(&knownColor);
435     switch (leadingFPsToEliminate) {
436         default:
437             // We shouldn't eliminate more than we started with.
438             SkASSERT(leadingFPsToEliminate <= 2);
439             [[fallthrough]];
440         case 0:
441             // Compose the two processors as requested.
442             return ComposeProcessor::Make(/*f=*/std::move(series[1]), /*g=*/std::move(series[0]));
443         case 1:
444             // Replace the first processor with a constant color.
445             return ComposeProcessor::Make(/*f=*/std::move(series[1]),
446                                           /*g=*/MakeColor(knownColor));
447         case 2:
448             // Replace the entire composition with a constant color.
449             return MakeColor(knownColor);
450     }
451 }
452 
453 //////////////////////////////////////////////////////////////////////////////
454 
ColorMatrix(std::unique_ptr<GrFragmentProcessor> child,const float matrix[20],bool unpremulInput,bool clampRGBOutput,bool premulOutput)455 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::ColorMatrix(
456         std::unique_ptr<GrFragmentProcessor> child,
457         const float matrix[20],
458         bool unpremulInput,
459         bool clampRGBOutput,
460         bool premulOutput) {
461     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForColorFilter,
462         "uniform half4x4 m;"
463         "uniform half4 v;"
464         "uniform int unpremulInput;"   // always specialized
465         "uniform int clampRGBOutput;"  // always specialized
466         "uniform int premulOutput;"    // always specialized
467         "half4 main(half4 color) {"
468             "if (bool(unpremulInput)) {"
469                 "color = unpremul(color);"
470             "}"
471             "color = m * color + v;"
472             "if (bool(clampRGBOutput)) {"
473                 "color = saturate(color);"
474             "} else {"
475                 "color.a = saturate(color.a);"
476             "}"
477             "if (bool(premulOutput)) {"
478                 "color.rgb *= color.a;"
479             "}"
480             "return color;"
481         "}"
482     );
483     SkASSERT(SkRuntimeEffectPriv::SupportsConstantOutputForConstantInput(effect));
484 
485     SkM44 m44(matrix[ 0], matrix[ 1], matrix[ 2], matrix[ 3],
486               matrix[ 5], matrix[ 6], matrix[ 7], matrix[ 8],
487               matrix[10], matrix[11], matrix[12], matrix[13],
488               matrix[15], matrix[16], matrix[17], matrix[18]);
489     SkV4 v4 = {matrix[4], matrix[9], matrix[14], matrix[19]};
490     return GrSkSLFP::Make(effect, "ColorMatrix", std::move(child), GrSkSLFP::OptFlags::kNone,
491                           "m", m44,
492                           "v", v4,
493                           "unpremulInput",  GrSkSLFP::Specialize(unpremulInput  ? 1 : 0),
494                           "clampRGBOutput", GrSkSLFP::Specialize(clampRGBOutput ? 1 : 0),
495                           "premulOutput",   GrSkSLFP::Specialize(premulOutput   ? 1 : 0));
496 }
497 
498 //////////////////////////////////////////////////////////////////////////////
499 
SurfaceColor()500 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::SurfaceColor() {
501     class SurfaceColorProcessor : public GrFragmentProcessor {
502     public:
503         static std::unique_ptr<GrFragmentProcessor> Make() {
504             return std::unique_ptr<GrFragmentProcessor>(new SurfaceColorProcessor());
505         }
506 
507         std::unique_ptr<GrFragmentProcessor> clone() const override { return Make(); }
508 
509         const char* name() const override { return "SurfaceColor"; }
510 
511     private:
512         std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override {
513             class Impl : public ProgramImpl {
514             public:
515                 void emitCode(EmitArgs& args) override {
516                     const char* dstColor = args.fFragBuilder->dstColor();
517                     args.fFragBuilder->codeAppendf("return %s;", dstColor);
518                 }
519             };
520             return std::make_unique<Impl>();
521         }
522 
523         SurfaceColorProcessor()
524                 : INHERITED(kSurfaceColorProcessor_ClassID, kNone_OptimizationFlags) {
525             this->setWillReadDstColor();
526         }
527 
528         void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
529 
530         bool onIsEqual(const GrFragmentProcessor&) const override { return true; }
531 
532         using INHERITED = GrFragmentProcessor;
533     };
534 
535     return SurfaceColorProcessor::Make();
536 }
537 
538 //////////////////////////////////////////////////////////////////////////////
539 
DeviceSpace(std::unique_ptr<GrFragmentProcessor> fp)540 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::DeviceSpace(
541         std::unique_ptr<GrFragmentProcessor> fp) {
542     if (!fp) {
543         return nullptr;
544     }
545 
546     class DeviceSpace : GrFragmentProcessor {
547     public:
548         static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp) {
549             return std::unique_ptr<GrFragmentProcessor>(new DeviceSpace(std::move(fp)));
550         }
551 
552     private:
553         DeviceSpace(std::unique_ptr<GrFragmentProcessor> fp)
554                 : GrFragmentProcessor(kDeviceSpace_ClassID, fp->optimizationFlags()) {
555             // Passing FragCoord here is the reason this is a subclass and not a runtime-FP.
556             this->registerChild(std::move(fp), SkSL::SampleUsage::FragCoord());
557         }
558 
559         std::unique_ptr<GrFragmentProcessor> clone() const override {
560             auto child = this->childProcessor(0)->clone();
561             return std::unique_ptr<GrFragmentProcessor>(new DeviceSpace(std::move(child)));
562         }
563 
564         SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& f) const override {
565             return this->childProcessor(0)->constantOutputForConstantInput(f);
566         }
567 
568         std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override {
569             class Impl : public ProgramImpl {
570             public:
571                 Impl() = default;
572                 void emitCode(ProgramImpl::EmitArgs& args) override {
573                     auto child = this->invokeChild(0, args.fInputColor, args, "sk_FragCoord.xy");
574                     args.fFragBuilder->codeAppendf("return %s;", child.c_str());
575                 }
576             };
577             return std::make_unique<Impl>();
578         }
579 
580         void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
581 
582         bool onIsEqual(const GrFragmentProcessor& processor) const override { return true; }
583 
584         const char* name() const override { return "DeviceSpace"; }
585     };
586 
587     return DeviceSpace::Make(std::move(fp));
588 }
589 
590 //////////////////////////////////////////////////////////////////////////////
591 
592 #define CLIP_EDGE_SKSL              \
593     "const int kFillBW = 0;"        \
594     "const int kFillAA = 1;"        \
595     "const int kInverseFillBW = 2;" \
596     "const int kInverseFillAA = 3;"
597 
598 static_assert(static_cast<int>(GrClipEdgeType::kFillBW) == 0);
599 static_assert(static_cast<int>(GrClipEdgeType::kFillAA) == 1);
600 static_assert(static_cast<int>(GrClipEdgeType::kInverseFillBW) == 2);
601 static_assert(static_cast<int>(GrClipEdgeType::kInverseFillAA) == 3);
602 
Rect(std::unique_ptr<GrFragmentProcessor> inputFP,GrClipEdgeType edgeType,SkRect rect)603 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::Rect(
604         std::unique_ptr<GrFragmentProcessor> inputFP, GrClipEdgeType edgeType, SkRect rect) {
605     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
606     CLIP_EDGE_SKSL
607         "uniform int edgeType;"  // GrClipEdgeType, specialized
608         "uniform float4 rectUniform;"
609 
610         "half4 main(float2 xy) {"
611             "half coverage;"
612             "if (edgeType == kFillBW || edgeType == kInverseFillBW) {"
613                 // non-AA
614                 "coverage = half(all(greaterThan(float4(sk_FragCoord.xy, rectUniform.zw),"
615                                                 "float4(rectUniform.xy, sk_FragCoord.xy))));"
616             "} else {"
617                 // compute coverage relative to left and right edges, add, then subtract 1 to
618                 // account for double counting. And similar for top/bottom.
619                 "half4 dists4 = saturate(half4(1, 1, -1, -1) *"
620                                         "half4(sk_FragCoord.xyxy - rectUniform));"
621                 "half2 dists2 = dists4.xy + dists4.zw - 1;"
622                 "coverage = dists2.x * dists2.y;"
623             "}"
624 
625             "if (edgeType == kInverseFillBW || edgeType == kInverseFillAA) {"
626                 "coverage = 1.0 - coverage;"
627             "}"
628 
629             "return half4(coverage);"
630         "}"
631     );
632 
633     SkASSERT(rect.isSorted());
634     // The AA math in the shader evaluates to 0 at the uploaded coordinates, so outset by 0.5
635     // to interpolate from 0 at a half pixel inset and 1 at a half pixel outset of rect.
636     SkRect rectUniform = GrClipEdgeTypeIsAA(edgeType) ? rect.makeOutset(.5f, .5f) : rect;
637 
638     auto rectFP = GrSkSLFP::Make(effect, "Rect", /*inputFP=*/nullptr,
639                                  GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha,
640                                 "edgeType", GrSkSLFP::Specialize(static_cast<int>(edgeType)),
641                                 "rectUniform", rectUniform);
642     return GrBlendFragmentProcessor::Make<SkBlendMode::kModulate>(std::move(rectFP),
643                                                                   std::move(inputFP));
644 }
645 
Circle(std::unique_ptr<GrFragmentProcessor> inputFP,GrClipEdgeType edgeType,SkPoint center,float radius)646 GrFPResult GrFragmentProcessor::Circle(std::unique_ptr<GrFragmentProcessor> inputFP,
647                                        GrClipEdgeType edgeType,
648                                        SkPoint center,
649                                        float radius) {
650     // A radius below half causes the implicit insetting done by this processor to become
651     // inverted. We could handle this case by making the processor code more complicated.
652     if (radius < .5f && GrClipEdgeTypeIsInverseFill(edgeType)) {
653         return GrFPFailure(std::move(inputFP));
654     }
655 
656     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
657     CLIP_EDGE_SKSL
658         "uniform int edgeType;"  // GrClipEdgeType, specialized
659         // The circle uniform is (center.x, center.y, radius + 0.5, 1 / (radius + 0.5)) for regular
660         // fills and (..., radius - 0.5, 1 / (radius - 0.5)) for inverse fills.
661         "uniform float4 circle;"
662 
663         "half4 main(float2 xy) {"
664             // TODO: Right now the distance to circle calculation is performed in a space normalized
665             // to the radius and then denormalized. This is to mitigate overflow on devices that
666             // don't have full float.
667             "half d;"
668             "if (edgeType == kInverseFillBW || edgeType == kInverseFillAA) {"
669                 "d = half((length((circle.xy - sk_FragCoord.xy) * circle.w) - 1.0) * circle.z);"
670             "} else {"
671                 "d = half((1.0 - length((circle.xy - sk_FragCoord.xy) * circle.w)) * circle.z);"
672             "}"
673             "return half4((edgeType == kFillAA || edgeType == kInverseFillAA)"
674                               "? saturate(d)"
675                               ": (d > 0.5 ? 1 : 0));"
676         "}"
677     );
678 
679     SkScalar effectiveRadius = radius;
680     if (GrClipEdgeTypeIsInverseFill(edgeType)) {
681         effectiveRadius -= 0.5f;
682         // When the radius is 0.5 effectiveRadius is 0 which causes an inf * 0 in the shader.
683         effectiveRadius = std::max(0.001f, effectiveRadius);
684     } else {
685         effectiveRadius += 0.5f;
686     }
687     SkV4 circle = {center.fX, center.fY, effectiveRadius, SkScalarInvert(effectiveRadius)};
688 
689     auto circleFP = GrSkSLFP::Make(effect, "Circle", /*inputFP=*/nullptr,
690                                    GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha,
691                                    "edgeType", GrSkSLFP::Specialize(static_cast<int>(edgeType)),
692                                    "circle", circle);
693     return GrFPSuccess(GrBlendFragmentProcessor::Make<SkBlendMode::kModulate>(std::move(inputFP),
694                                                                               std::move(circleFP)));
695 }
696 
Ellipse(std::unique_ptr<GrFragmentProcessor> inputFP,GrClipEdgeType edgeType,SkPoint center,SkPoint radii,const GrShaderCaps & caps)697 GrFPResult GrFragmentProcessor::Ellipse(std::unique_ptr<GrFragmentProcessor> inputFP,
698                                         GrClipEdgeType edgeType,
699                                         SkPoint center,
700                                         SkPoint radii,
701                                         const GrShaderCaps& caps) {
702     const bool medPrecision = !caps.fFloatIs32Bits;
703 
704     // Small radii produce bad results on devices without full float.
705     if (medPrecision && (radii.fX < 0.5f || radii.fY < 0.5f)) {
706         return GrFPFailure(std::move(inputFP));
707     }
708     // Very narrow ellipses produce bad results on devices without full float
709     if (medPrecision && (radii.fX > 255*radii.fY || radii.fY > 255*radii.fX)) {
710         return GrFPFailure(std::move(inputFP));
711     }
712     // Very large ellipses produce bad results on devices without full float
713     if (medPrecision && (radii.fX > 16384 || radii.fY > 16384)) {
714         return GrFPFailure(std::move(inputFP));
715     }
716 
717     static const SkRuntimeEffect* effect = SkMakeRuntimeEffect(SkRuntimeEffect::MakeForShader,
718     CLIP_EDGE_SKSL
719         "uniform int edgeType;"      // GrClipEdgeType, specialized
720         "uniform int medPrecision;"  // !sk_Caps.floatIs32Bits, specialized
721 
722         "uniform float4 ellipse;"
723         "uniform float2 scale;"    // only for medPrecision
724 
725         "half4 main(float2 xy) {"
726             // d is the offset to the ellipse center
727             "float2 d = sk_FragCoord.xy - ellipse.xy;"
728             // If we're on a device with a "real" mediump then we'll do the distance computation in
729             // a space that is normalized by the larger radius or 128, whichever is smaller. The
730             // scale uniform will be scale, 1/scale. The inverse squared radii uniform values are
731             // already in this normalized space. The center is not.
732             "if (bool(medPrecision)) {"
733                 "d *= scale.y;"
734             "}"
735             "float2 Z = d * ellipse.zw;"
736             // implicit is the evaluation of (x/rx)^2 + (y/ry)^2 - 1.
737             "float implicit = dot(Z, d) - 1;"
738             // grad_dot is the squared length of the gradient of the implicit.
739             "float grad_dot = 4 * dot(Z, Z);"
740             // Avoid calling inversesqrt on zero.
741             "if (bool(medPrecision)) {"
742                 "grad_dot = max(grad_dot, 6.1036e-5);"
743             "} else {"
744                 "grad_dot = max(grad_dot, 1.1755e-38);"
745             "}"
746             "float approx_dist = implicit * inversesqrt(grad_dot);"
747             "if (bool(medPrecision)) {"
748                 "approx_dist *= scale.x;"
749             "}"
750 
751             "half alpha;"
752             "if (edgeType == kFillBW) {"
753                 "alpha = approx_dist > 0.0 ? 0.0 : 1.0;"
754             "} else if (edgeType == kFillAA) {"
755                 "alpha = saturate(0.5 - half(approx_dist));"
756             "} else if (edgeType == kInverseFillBW) {"
757                 "alpha = approx_dist > 0.0 ? 1.0 : 0.0;"
758             "} else {"  // edgeType == kInverseFillAA
759                 "alpha = saturate(0.5 + half(approx_dist));"
760             "}"
761             "return half4(alpha);"
762         "}"
763     );
764 
765     float invRXSqd;
766     float invRYSqd;
767     SkV2 scale = {1, 1};
768     // If we're using a scale factor to work around precision issues, choose the larger radius as
769     // the scale factor. The inv radii need to be pre-adjusted by the scale factor.
770     if (medPrecision) {
771         if (radii.fX > radii.fY) {
772             invRXSqd = 1.f;
773             invRYSqd = (radii.fX * radii.fX) / (radii.fY * radii.fY);
774             scale = {radii.fX, 1.f / radii.fX};
775         } else {
776             invRXSqd = (radii.fY * radii.fY) / (radii.fX * radii.fX);
777             invRYSqd = 1.f;
778             scale = {radii.fY, 1.f / radii.fY};
779         }
780     } else {
781         invRXSqd = 1.f / (radii.fX * radii.fX);
782         invRYSqd = 1.f / (radii.fY * radii.fY);
783     }
784     SkV4 ellipse = {center.fX, center.fY, invRXSqd, invRYSqd};
785 
786     auto ellipseFP = GrSkSLFP::Make(effect, "Ellipse", /*inputFP=*/nullptr,
787                                     GrSkSLFP::OptFlags::kCompatibleWithCoverageAsAlpha,
788                                     "edgeType", GrSkSLFP::Specialize(static_cast<int>(edgeType)),
789                                     "medPrecision",  GrSkSLFP::Specialize<int>(medPrecision),
790                                     "ellipse", ellipse,
791                                     "scale", scale);
792     return GrFPSuccess(GrBlendFragmentProcessor::Make<SkBlendMode::kModulate>(std::move(ellipseFP),
793                                                                               std::move(inputFP)));
794 }
795 
796 //////////////////////////////////////////////////////////////////////////////
797 
HighPrecision(std::unique_ptr<GrFragmentProcessor> fp)798 std::unique_ptr<GrFragmentProcessor> GrFragmentProcessor::HighPrecision(
799         std::unique_ptr<GrFragmentProcessor> fp) {
800     class HighPrecisionFragmentProcessor : public GrFragmentProcessor {
801     public:
802         static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp) {
803             return std::unique_ptr<GrFragmentProcessor>(
804                     new HighPrecisionFragmentProcessor(std::move(fp)));
805         }
806 
807         const char* name() const override { return "HighPrecision"; }
808 
809         std::unique_ptr<GrFragmentProcessor> clone() const override {
810             return Make(this->childProcessor(0)->clone());
811         }
812 
813     private:
814         HighPrecisionFragmentProcessor(std::unique_ptr<GrFragmentProcessor> fp)
815                 : INHERITED(kHighPrecisionFragmentProcessor_ClassID,
816                             ProcessorOptimizationFlags(fp.get())) {
817             this->registerChild(std::move(fp));
818         }
819 
820         std::unique_ptr<ProgramImpl> onMakeProgramImpl() const override {
821             class Impl : public ProgramImpl {
822             public:
823                 void emitCode(EmitArgs& args) override {
824                     SkString childColor = this->invokeChild(0, args);
825 
826                     args.fFragBuilder->forceHighPrecision();
827                     args.fFragBuilder->codeAppendf("return %s;", childColor.c_str());
828                 }
829             };
830             return std::make_unique<Impl>();
831         }
832 
833         void onAddToKey(const GrShaderCaps&, skgpu::KeyBuilder*) const override {}
834         bool onIsEqual(const GrFragmentProcessor& other) const override { return true; }
835 
836         SkPMColor4f constantOutputForConstantInput(const SkPMColor4f& input) const override {
837             return ConstantOutputForConstantInput(this->childProcessor(0), input);
838         }
839 
840         using INHERITED = GrFragmentProcessor;
841     };
842 
843     return HighPrecisionFragmentProcessor::Make(std::move(fp));
844 }
845 
846 //////////////////////////////////////////////////////////////////////////////
847 
848 using ProgramImpl = GrFragmentProcessor::ProgramImpl;
849 
setData(const GrGLSLProgramDataManager & pdman,const GrFragmentProcessor & processor)850 void ProgramImpl::setData(const GrGLSLProgramDataManager& pdman,
851                           const GrFragmentProcessor& processor) {
852     this->onSetData(pdman, processor);
853 }
854 
invokeChild(int childIndex,const char * inputColor,const char * destColor,EmitArgs & args,std::string_view skslCoords)855 SkString ProgramImpl::invokeChild(int childIndex,
856                                   const char* inputColor,
857                                   const char* destColor,
858                                   EmitArgs& args,
859                                   std::string_view skslCoords) {
860     SkASSERT(childIndex >= 0);
861 
862     if (!inputColor) {
863         inputColor = args.fInputColor;
864     }
865 
866     const GrFragmentProcessor* childProc = args.fFp.childProcessor(childIndex);
867     if (!childProc) {
868         // If no child processor is provided, return the input color as-is.
869         return SkString(inputColor);
870     }
871 
872     auto invocation = SkStringPrintf("%s(%s", this->childProcessor(childIndex)->functionName(),
873                                      inputColor);
874 
875     if (childProc->isBlendFunction()) {
876         if (!destColor) {
877             destColor = args.fFp.isBlendFunction() ? args.fDestColor : "half4(1)";
878         }
879         invocation.appendf(", %s", destColor);
880     }
881 
882     // Assert that the child has no sample matrix. A uniform matrix sample call would go through
883     // invokeChildWithMatrix, not here.
884     SkASSERT(!childProc->sampleUsage().isUniformMatrix());
885 
886     if (args.fFragBuilder->getProgramBuilder()->fragmentProcessorHasCoordsParam(childProc)) {
887         SkASSERT(!childProc->sampleUsage().isFragCoord() || skslCoords == "sk_FragCoord.xy");
888         // The child's function takes a half4 color and a float2 coordinate
889         if (!skslCoords.empty()) {
890             invocation.appendf(", %.*s", (int)skslCoords.size(), skslCoords.data());
891         } else {
892             invocation.appendf(", %s", args.fSampleCoord);
893         }
894     }
895 
896     invocation.append(")");
897     return invocation;
898 }
899 
invokeChildWithMatrix(int childIndex,const char * inputColor,const char * destColor,EmitArgs & args)900 SkString ProgramImpl::invokeChildWithMatrix(int childIndex,
901                                             const char* inputColor,
902                                             const char* destColor,
903                                             EmitArgs& args) {
904     SkASSERT(childIndex >= 0);
905 
906     if (!inputColor) {
907         inputColor = args.fInputColor;
908     }
909 
910     const GrFragmentProcessor* childProc = args.fFp.childProcessor(childIndex);
911     if (!childProc) {
912         // If no child processor is provided, return the input color as-is.
913         return SkString(inputColor);
914     }
915 
916     SkASSERT(childProc->sampleUsage().isUniformMatrix());
917 
918     // Every uniform matrix has the same (initial) name. Resolve that into the mangled name:
919     GrShaderVar uniform = args.fUniformHandler->getUniformMapping(
920             args.fFp, SkString(SkSL::SampleUsage::MatrixUniformName()));
921     SkASSERT(uniform.getType() == SkSLType::kFloat3x3);
922     const SkString& matrixName(uniform.getName());
923 
924     auto invocation = SkStringPrintf("%s(%s", this->childProcessor(childIndex)->functionName(),
925                                      inputColor);
926 
927     if (childProc->isBlendFunction()) {
928         if (!destColor) {
929             destColor = args.fFp.isBlendFunction() ? args.fDestColor : "half4(1)";
930         }
931         invocation.appendf(", %s", destColor);
932     }
933 
934     // Produce a string containing the call to the helper function. We have a uniform variable
935     // containing our transform (matrixName). If the parent coords were produced by uniform
936     // transforms, then the entire expression (matrixName * coords) is lifted to a vertex shader
937     // and is stored in a varying. In that case, childProc will not be sampled explicitly, so its
938     // function signature will not take in coords.
939     //
940     // In all other cases, we need to insert sksl to compute matrix * parent coords and then invoke
941     // the function.
942     if (args.fFragBuilder->getProgramBuilder()->fragmentProcessorHasCoordsParam(childProc)) {
943         // Only check perspective for this specific matrix transform, not the aggregate FP property.
944         // Any parent perspective will have already been applied when evaluated in the FS.
945         if (childProc->sampleUsage().hasPerspective()) {
946             invocation.appendf(", proj((%s) * %s.xy1)", matrixName.c_str(), args.fSampleCoord);
947         } else if (args.fShaderCaps->fNonsquareMatrixSupport) {
948             invocation.appendf(", float3x2(%s) * %s.xy1", matrixName.c_str(), args.fSampleCoord);
949         } else {
950             invocation.appendf(", ((%s) * %s.xy1).xy", matrixName.c_str(), args.fSampleCoord);
951         }
952     }
953 
954     invocation.append(")");
955     return invocation;
956 }
957