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