Remove GrArithmeticProcessor, use GrRuntimeFPBuilder instead

This also converts "enforcePMColor" to a uniform (rather than baking it
in). It's easier to write this way, and I can't imagine it made that
much difference to performance.

Change-Id: Idd202678740077412a2fb4e5122ebb9118f1d7bf
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/410096
Commit-Queue: Brian Osman <brianosman@google.com>
Reviewed-by: Brian Salomon <bsalomon@google.com>
Reviewed-by: John Stiles <johnstiles@google.com>
diff --git a/gn/gpu.gni b/gn/gpu.gni
index c57b853..5c18668 100644
--- a/gn/gpu.gni
+++ b/gn/gpu.gni
@@ -323,8 +323,6 @@
   "$_src/gpu/effects/generated/GrAARectEffect.h",
   "$_src/gpu/effects/generated/GrAlphaThresholdFragmentProcessor.cpp",
   "$_src/gpu/effects/generated/GrAlphaThresholdFragmentProcessor.h",
-  "$_src/gpu/effects/generated/GrArithmeticProcessor.cpp",
-  "$_src/gpu/effects/generated/GrArithmeticProcessor.h",
   "$_src/gpu/effects/generated/GrBlurredEdgeFragmentProcessor.cpp",
   "$_src/gpu/effects/generated/GrBlurredEdgeFragmentProcessor.h",
   "$_src/gpu/effects/generated/GrCircleBlurFragmentProcessor.cpp",
diff --git a/gn/sksl.gni b/gn/sksl.gni
index edd2401..94361fc 100644
--- a/gn/sksl.gni
+++ b/gn/sksl.gni
@@ -207,7 +207,6 @@
 skia_gpu_processor_sources = [
   "$_src/gpu/effects/GrAARectEffect.fp",
   "$_src/gpu/effects/GrAlphaThresholdFragmentProcessor.fp",
-  "$_src/gpu/effects/GrArithmeticProcessor.fp",
   "$_src/gpu/effects/GrBlurredEdgeFragmentProcessor.fp",
   "$_src/gpu/effects/GrCircleBlurFragmentProcessor.fp",
   "$_src/gpu/effects/GrCircleEffect.fp",
diff --git a/src/effects/imagefilters/SkArithmeticImageFilter.cpp b/src/effects/imagefilters/SkArithmeticImageFilter.cpp
index 6782328..0a46995 100644
--- a/src/effects/imagefilters/SkArithmeticImageFilter.cpp
+++ b/src/effects/imagefilters/SkArithmeticImageFilter.cpp
@@ -23,11 +23,7 @@
 #include "src/gpu/GrSurfaceDrawContext.h"
 #include "src/gpu/GrTextureProxy.h"
 #include "src/gpu/SkGr.h"
-#include "src/gpu/effects/generated/GrArithmeticProcessor.h"
-#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
-#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
-#include "src/gpu/glsl/GrGLSLProgramDataManager.h"
-#include "src/gpu/glsl/GrGLSLUniformHandler.h"
+#include "src/gpu/effects/GrSkSLFP.h"
 #endif
 
 namespace {
@@ -307,6 +303,35 @@
 
 #if SK_SUPPORT_GPU
 
+std::unique_ptr<GrFragmentProcessor> make_arithmetic_fp(
+        std::unique_ptr<GrFragmentProcessor> srcFP,
+        std::unique_ptr<GrFragmentProcessor> dstFP,
+        const SkV4& k,
+        bool enforcePMColor) {
+    static constexpr char kCode[] = R"(
+        uniform shader srcFP;
+        uniform shader dstFP;
+        uniform half4 k;
+        uniform half pmClamp;
+        half4 main(float2 xy) {
+            half4 src = sample(srcFP, xy);
+            half4 dst = sample(dstFP, xy);
+            half4 color = saturate(k.x * src * dst +
+                                   k.y * src +
+                                   k.z * dst +
+                                   k.w);
+            color.rgb = min(color.rgb, max(color.a, pmClamp));
+            return color;
+        }
+    )";
+    auto builder = GrRuntimeFPBuilder::Make<kCode, SkRuntimeEffect::MakeForShader>();
+    builder.child("srcFP") = std::move(srcFP);
+    builder.child("dstFP") = std::move(dstFP);
+    builder.uniform("k") = k;
+    builder.uniform("pmClamp") = enforcePMColor ? 0.0f : 1.0f;
+    return builder.makeFP();
+}
+
 sk_sp<SkSpecialImage> SkArithmeticImageFilter::filterImageGPU(
         const Context& ctx,
         sk_sp<SkSpecialImage> background,
@@ -374,7 +399,7 @@
                                              foreground->alphaType(),
                                              ctx.colorSpace(),
                                              kPremul_SkAlphaType);
-        fp = GrArithmeticProcessor::Make(std::move(fgFP), std::move(fp), fK, fEnforcePMColor);
+        fp = make_arithmetic_fp(std::move(fgFP), std::move(fp), fK, fEnforcePMColor);
     }
 
     GrImageInfo info(ctx.grColorType(), kPremul_SkAlphaType, ctx.refColorSpace(), bounds.size());
diff --git a/src/gpu/GrProcessor.h b/src/gpu/GrProcessor.h
index 5e1f1f3..0ec13ba 100644
--- a/src/gpu/GrProcessor.h
+++ b/src/gpu/GrProcessor.h
@@ -53,7 +53,6 @@
         kEllipticalRRectEffect_ClassID,
         kGP_ClassID,
         kVertexColorSpaceBenchGP_ClassID,
-        kGrArithmeticProcessor_ClassID,
         kGrAARectEffect_ClassID,
         kGrAlphaThresholdFragmentProcessor_ClassID,
         kGrBicubicEffect_ClassID,
diff --git a/src/gpu/GrProcessorUnitTest.cpp b/src/gpu/GrProcessorUnitTest.cpp
index ffcc3be..03190ab 100644
--- a/src/gpu/GrProcessorUnitTest.cpp
+++ b/src/gpu/GrProcessorUnitTest.cpp
@@ -146,7 +146,7 @@
  * we verify the count is as expected.  If a new factory is added, then these numbers must be
  * manually adjusted.
  */
-static constexpr int kFPFactoryCount = 34;
+static constexpr int kFPFactoryCount = 33;
 static constexpr int kGPFactoryCount = 14;
 static constexpr int kXPFactoryCount = 4;
 
diff --git a/src/gpu/effects/GrArithmeticProcessor.fp b/src/gpu/effects/GrArithmeticProcessor.fp
deleted file mode 100644
index 548f38a..0000000
--- a/src/gpu/effects/GrArithmeticProcessor.fp
+++ /dev/null
@@ -1,49 +0,0 @@
-/*
- * Copyright 2020 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-in fragmentProcessor srcFP;
-in fragmentProcessor dstFP;
-layout(ctype=SkV4) in uniform float4 k;
-layout(key) in bool enforcePMColor;
-
-half4 main() {
-    half4 src = sample(srcFP);
-    half4 dst = sample(dstFP);
-    half4 color = saturate(half(k.x) * src * dst +
-                           half(k.y) * src +
-                           half(k.z) * dst +
-                           half(k.w));
-    @if (enforcePMColor) {
-        color.rgb = min(color.rgb, color.a);
-    }
-    return color;
-}
-
-@optimizationFlags {
-    kNone_OptimizationFlags
-}
-
-@make{
-    static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> srcFP,
-                                                     std::unique_ptr<GrFragmentProcessor> dstFP,
-                                                     const SkV4& k, bool enforcePMColor) {
-        return std::unique_ptr<GrFragmentProcessor>(new GrArithmeticProcessor(
-                std::move(srcFP), std::move(dstFP), k, enforcePMColor));
-    }
-}
-
-@test(d) {
-    SkV4 k;
-    k.x = d->fRandom->nextF();
-    k.y = d->fRandom->nextF();
-    k.z = d->fRandom->nextF();
-    k.w = d->fRandom->nextF();
-    bool enforcePMColor = d->fRandom->nextBool();
-    return GrArithmeticProcessor::Make(
-            GrProcessorUnitTest::MakeChildFP(d), GrProcessorUnitTest::MakeChildFP(d),
-            k, enforcePMColor);
-}
diff --git a/src/gpu/effects/generated/GrArithmeticProcessor.cpp b/src/gpu/effects/generated/GrArithmeticProcessor.cpp
deleted file mode 100644
index 9f5d8bc..0000000
--- a/src/gpu/effects/generated/GrArithmeticProcessor.cpp
+++ /dev/null
@@ -1,109 +0,0 @@
-/*
- * Copyright 2020 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-/**************************************************************************************************
- *** This file was autogenerated from GrArithmeticProcessor.fp; do not modify.
- **************************************************************************************************/
-#include "GrArithmeticProcessor.h"
-
-#include "src/core/SkUtils.h"
-#include "src/gpu/GrTexture.h"
-#include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
-#include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
-#include "src/gpu/glsl/GrGLSLProgramBuilder.h"
-#include "src/sksl/SkSLCPP.h"
-#include "src/sksl/SkSLUtil.h"
-class GrGLSLArithmeticProcessor : public GrGLSLFragmentProcessor {
-public:
-    GrGLSLArithmeticProcessor() {}
-    void emitCode(EmitArgs& args) override {
-        GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
-        const GrArithmeticProcessor& _outer = args.fFp.cast<GrArithmeticProcessor>();
-        (void)_outer;
-        auto k = _outer.k;
-        (void)k;
-        auto enforcePMColor = _outer.enforcePMColor;
-        (void)enforcePMColor;
-        kVar = args.fUniformHandler->addUniform(
-                &_outer, kFragment_GrShaderFlag, kFloat4_GrSLType, "k");
-        SkString _sample0 = this->invokeChild(0, args);
-        fragBuilder->codeAppendf(
-                R"SkSL(half4 src = %s;)SkSL", _sample0.c_str());
-        SkString _sample1 = this->invokeChild(1, args);
-        fragBuilder->codeAppendf(
-                R"SkSL(
-half4 dst = %s;
-half4 color = clamp((((half(%s.x) * src) * dst + half(%s.y) * src) + half(%s.z) * dst) + half(%s.w), 0.0, 1.0);
-@if (%s) {
-    color.xyz = min(color.xyz, color.w);
-}
-return color;
-)SkSL",
-                _sample1.c_str(),
-                args.fUniformHandler->getUniformCStr(kVar),
-                args.fUniformHandler->getUniformCStr(kVar),
-                args.fUniformHandler->getUniformCStr(kVar),
-                args.fUniformHandler->getUniformCStr(kVar),
-                (_outer.enforcePMColor ? "true" : "false"));
-    }
-
-private:
-    void onSetData(const GrGLSLProgramDataManager& pdman,
-                   const GrFragmentProcessor& _proc) override {
-        const GrArithmeticProcessor& _outer = _proc.cast<GrArithmeticProcessor>();
-        { pdman.set4fv(kVar, 1, _outer.k.ptr()); }
-    }
-    UniformHandle kVar;
-};
-std::unique_ptr<GrGLSLFragmentProcessor> GrArithmeticProcessor::onMakeProgramImpl() const {
-    return std::make_unique<GrGLSLArithmeticProcessor>();
-}
-void GrArithmeticProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
-                                                  GrProcessorKeyBuilder* b) const {
-    b->addBool(enforcePMColor, "enforcePMColor");
-}
-bool GrArithmeticProcessor::onIsEqual(const GrFragmentProcessor& other) const {
-    const GrArithmeticProcessor& that = other.cast<GrArithmeticProcessor>();
-    (void)that;
-    if (k != that.k) return false;
-    if (enforcePMColor != that.enforcePMColor) return false;
-    return true;
-}
-GrArithmeticProcessor::GrArithmeticProcessor(const GrArithmeticProcessor& src)
-        : INHERITED(kGrArithmeticProcessor_ClassID, src.optimizationFlags())
-        , k(src.k)
-        , enforcePMColor(src.enforcePMColor) {
-    this->cloneAndRegisterAllChildProcessors(src);
-}
-std::unique_ptr<GrFragmentProcessor> GrArithmeticProcessor::clone() const {
-    return std::make_unique<GrArithmeticProcessor>(*this);
-}
-#if GR_TEST_UTILS
-SkString GrArithmeticProcessor::onDumpInfo() const {
-    return SkStringPrintf("(k=float4(%f, %f, %f, %f), enforcePMColor=%s)",
-                          k.x,
-                          k.y,
-                          k.z,
-                          k.w,
-                          (enforcePMColor ? "true" : "false"));
-}
-#endif
-GR_DEFINE_FRAGMENT_PROCESSOR_TEST(GrArithmeticProcessor);
-#if GR_TEST_UTILS
-std::unique_ptr<GrFragmentProcessor> GrArithmeticProcessor::TestCreate(GrProcessorTestData* d) {
-    SkV4 k;
-    k.x = d->fRandom->nextF();
-    k.y = d->fRandom->nextF();
-    k.z = d->fRandom->nextF();
-    k.w = d->fRandom->nextF();
-    bool enforcePMColor = d->fRandom->nextBool();
-    return GrArithmeticProcessor::Make(GrProcessorUnitTest::MakeChildFP(d),
-                                       GrProcessorUnitTest::MakeChildFP(d),
-                                       k,
-                                       enforcePMColor);
-}
-#endif
diff --git a/src/gpu/effects/generated/GrArithmeticProcessor.h b/src/gpu/effects/generated/GrArithmeticProcessor.h
deleted file mode 100644
index 2404e60..0000000
--- a/src/gpu/effects/generated/GrArithmeticProcessor.h
+++ /dev/null
@@ -1,54 +0,0 @@
-/*
- * Copyright 2020 Google Inc.
- *
- * Use of this source code is governed by a BSD-style license that can be
- * found in the LICENSE file.
- */
-
-/**************************************************************************************************
- *** This file was autogenerated from GrArithmeticProcessor.fp; do not modify.
- **************************************************************************************************/
-#ifndef GrArithmeticProcessor_DEFINED
-#define GrArithmeticProcessor_DEFINED
-
-#include "include/core/SkM44.h"
-#include "include/core/SkTypes.h"
-
-#include "src/gpu/GrFragmentProcessor.h"
-
-class GrArithmeticProcessor : public GrFragmentProcessor {
-public:
-    static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> srcFP,
-                                                     std::unique_ptr<GrFragmentProcessor> dstFP,
-                                                     const SkV4& k,
-                                                     bool enforcePMColor) {
-        return std::unique_ptr<GrFragmentProcessor>(
-                new GrArithmeticProcessor(std::move(srcFP), std::move(dstFP), k, enforcePMColor));
-    }
-    GrArithmeticProcessor(const GrArithmeticProcessor& src);
-    std::unique_ptr<GrFragmentProcessor> clone() const override;
-    const char* name() const override { return "ArithmeticProcessor"; }
-    SkV4 k;
-    bool enforcePMColor;
-
-private:
-    GrArithmeticProcessor(std::unique_ptr<GrFragmentProcessor> srcFP,
-                          std::unique_ptr<GrFragmentProcessor> dstFP,
-                          SkV4 k,
-                          bool enforcePMColor)
-            : INHERITED(kGrArithmeticProcessor_ClassID, (OptimizationFlags)kNone_OptimizationFlags)
-            , k(k)
-            , enforcePMColor(enforcePMColor) {
-        this->registerChild(std::move(srcFP), SkSL::SampleUsage::PassThrough());
-        this->registerChild(std::move(dstFP), SkSL::SampleUsage::PassThrough());
-    }
-    std::unique_ptr<GrGLSLFragmentProcessor> onMakeProgramImpl() const override;
-    void onGetGLSLProcessorKey(const GrShaderCaps&, GrProcessorKeyBuilder*) const override;
-    bool onIsEqual(const GrFragmentProcessor&) const override;
-#if GR_TEST_UTILS
-    SkString onDumpInfo() const override;
-#endif
-    GR_DECLARE_FRAGMENT_PROCESSOR_TEST
-    using INHERITED = GrFragmentProcessor;
-};
-#endif