Add gpu implementation of SkAvoidXfermode
GOLD_TRYBOT_URL= https://gold.skia.org/search2?unt=true&query=source_type%3Dgm&master=false&issue=1658623002

TBR=bsalomon@google.com

Committed: https://skia.googlesource.com/skia/+/15691a055db9b68c9b48f589e48d8a85888cf83f

Review URL: https://codereview.chromium.org/1658623002
diff --git a/gm/avoidxfermode.cpp b/gm/avoidxfermode.cpp
index 0d681cb..a015b6b 100644
--- a/gm/avoidxfermode.cpp
+++ b/gm/avoidxfermode.cpp
@@ -67,9 +67,9 @@
 
         r.offsetTo(0.0f, 64);
 
-        // LL corner: replace red with blue with a loose tolerance
+        // LL corner: replace red with transparent blue with a loose tolerance
         SkPaint p3;
-        p3.setColor(SK_ColorBLUE);
+        p3.setColor(0x7F0000FF);
         p3.setXfermode(SkAvoidXfermode::Create(SK_ColorRED,
                                                250,
                                                SkAvoidXfermode::kTargetColor_Mode))->unref();
diff --git a/gm/avoidxfermode2.cpp b/gm/avoidxfermode2.cpp
new file mode 100644
index 0000000..039ed40
--- /dev/null
+++ b/gm/avoidxfermode2.cpp
@@ -0,0 +1,84 @@
+/*
+ * Copyright 2016 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "gm.h"
+#include "SkAvoidXfermode.h"
+
+class AvoidXfermode2GM : public skiagm::GM {
+public:
+    AvoidXfermode2GM() { }
+
+protected:
+    SkString onShortName() override {
+        return SkString("avoidxfermode2");
+    }
+
+    SkISize onISize() override { return SkISize::Make(128, 128); }
+
+    void onDraw(SkCanvas* canvas) override {
+        canvas->drawARGB(255, 0, 0, 0);
+
+        SkRect r = SkRect::MakeXYWH(32, 32, 64, 64);
+
+        SkPaint p0;
+        p0.setColor(SK_ColorGREEN);
+        p0.setAntiAlias(false);
+
+        canvas->drawRect(r, p0);
+
+        r = SkRect::MakeIWH(64, 64);
+
+        // UL corner: replace the green with a tight tolerance
+        SkPaint p1;
+        p1.setColor(SK_ColorRED);
+        p1.setXfermode(SkAvoidXfermode::Create(SK_ColorGREEN,
+                                               55,
+                                               SkAvoidXfermode::kTargetColor_Mode))->unref();
+
+        canvas->drawRect(r, p1);
+
+        r.offsetTo(64, 0.0f);
+
+        // UR corner: avoid the green with a tight tolerance
+        SkPaint p2;
+        p2.setColor(SK_ColorRED);
+        p2.setXfermode(SkAvoidXfermode::Create(SK_ColorGREEN,
+                                               200,
+                                               SkAvoidXfermode::kAvoidColor_Mode))->unref();
+
+        canvas->drawRect(r, p2);
+
+        r.offsetTo(0.0f, 64);
+
+        // LL corner: replace the green with a loose tolerance
+        SkPaint p3;
+        p3.setColor(SK_ColorRED);
+        p3.setXfermode(SkAvoidXfermode::Create(SK_ColorGREEN,
+                                               200,
+                                               SkAvoidXfermode::kTargetColor_Mode))->unref();
+
+        canvas->drawRect(r, p3);
+
+        r.offsetTo(64, 64);
+
+        // LR corner: avoid the green with a loose tolerance
+        SkPaint p4;
+        p4.setColor(SK_ColorRED);
+        p4.setXfermode(SkAvoidXfermode::Create(SK_ColorGREEN,
+                                               55,
+                                               SkAvoidXfermode::kAvoidColor_Mode))->unref();
+
+        canvas->drawRect(r, p4);
+    }
+
+private:
+    typedef GM INHERITED;
+};
+
+//////////////////////////////////////////////////////////////////////////////
+
+DEF_GM(return new AvoidXfermode2GM;)
diff --git a/include/client/android/SkAvoidXfermode.h b/include/client/android/SkAvoidXfermode.h
index bcd42a5..8d08827 100644
--- a/include/client/android/SkAvoidXfermode.h
+++ b/include/client/android/SkAvoidXfermode.h
@@ -51,6 +51,12 @@
     void xferA8(SkAlpha dst[], const SkPMColor src[], int count,
                 const SkAlpha aa[]) const override;
 
+#if SK_SUPPORT_GPU
+    bool asFragmentProcessor(const GrFragmentProcessor** output,
+                             const GrFragmentProcessor* dst) const override;
+    bool asXPFactory(GrXPFactory** xpf) const override;
+#endif
+
     SK_TO_STRING_OVERRIDE()
     SK_DECLARE_PUBLIC_FLATTENABLE_DESERIALIZATION_PROCS(AvoidXfermode)
 
diff --git a/src/effects/SkAvoidXfermode.cpp b/src/effects/SkAvoidXfermode.cpp
index d0aa44f..30da0ad 100644
--- a/src/effects/SkAvoidXfermode.cpp
+++ b/src/effects/SkAvoidXfermode.cpp
@@ -164,11 +164,384 @@
                              const SkAlpha aa[]) const {
 }
 
+
+#if SK_SUPPORT_GPU
+
+#include "GrFragmentProcessor.h"
+#include "GrInvariantOutput.h"
+#include "GrXferProcessor.h"
+#include "glsl/GrGLSLFragmentProcessor.h"
+#include "glsl/GrGLSLFragmentShaderBuilder.h"
+#include "glsl/GrGLSLUniformHandler.h"
+#include "glsl/GrGLSLXferProcessor.h"
+
+///////////////////////////////////////////////////////////////////////////////
+// Fragment Processor
+///////////////////////////////////////////////////////////////////////////////
+
+class GLAvoidFP;
+
+class AvoidFP : public GrFragmentProcessor {
+public:
+    static const GrFragmentProcessor* Create(SkColor opColor, uint8_t tolerance,
+                                             SkAvoidXfermode::Mode mode,
+                                             const GrFragmentProcessor* dst) {
+        return new AvoidFP(opColor, tolerance, mode, dst);
+    }
+
+    ~AvoidFP() override { }
+
+    const char* name() const override { return "Avoid"; }
+
+    SkString dumpInfo() const override {
+        SkString str;
+        str.appendf("Color: 0x%08x Tol: %d Mode: %s",
+                    fOpColor, fTolerance,
+                    fMode == SkAvoidXfermode::kAvoidColor_Mode ? "Avoid" : "Target");
+        return str;
+    }
+
+    SkColor opColor() const { return fOpColor; }
+    uint8_t tol() const { return fTolerance; }
+    SkAvoidXfermode::Mode mode() const { return fMode; }
+
+private:
+    GrGLSLFragmentProcessor* onCreateGLSLInstance() const override;
+
+    void onGetGLSLProcessorKey(const GrGLSLCaps&, GrProcessorKeyBuilder*) const override;
+
+    bool onIsEqual(const GrFragmentProcessor& fpBase) const override {
+        const AvoidFP& fp = fpBase.cast<AvoidFP>();
+
+        return fOpColor == fp.fOpColor &&
+               fTolerance == fp.fTolerance &&
+               fMode == fp.fMode;
+    }
+
+    void onComputeInvariantOutput(GrInvariantOutput* inout) const override {
+        inout->setToUnknown(GrInvariantOutput::kWill_ReadInput);
+    }
+
+    AvoidFP(SkColor opColor, uint8_t tolerance,
+            SkAvoidXfermode::Mode mode, const GrFragmentProcessor* dst) 
+        : fOpColor(opColor), fTolerance(tolerance), fMode(mode) {
+        this->initClassID<AvoidFP>();
+
+        SkASSERT(dst);
+        SkDEBUGCODE(int dstIndex = )this->registerChildProcessor(dst);
+        SkASSERT(0 == dstIndex);
+    }
+
+    SkColor               fOpColor;
+    uint8_t               fTolerance;
+    SkAvoidXfermode::Mode fMode;
+
+    GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
+    typedef GrFragmentProcessor INHERITED;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+// Add common code for calculating avoid's distance value
+static void add_avoid_code(GrGLSLFragmentBuilder* fragBuilder,
+                           const char* dstColor,
+                           const char* srcCoverage,
+                           const char* kColorAndTolUni,
+                           const char* kCoverageName,
+                           SkAvoidXfermode::Mode mode) {
+
+    fragBuilder->codeAppendf("vec3 temp = %s.rgb - %s.rgb;", dstColor, kColorAndTolUni);
+    fragBuilder->codeAppendf("float dist = max(max(abs(temp.r), abs(temp.g)), abs(temp.b));");
+
+    if (SkAvoidXfermode::kTargetColor_Mode == mode) {
+        fragBuilder->codeAppendf("dist = 1.0 - dist;");
+    }
+
+    // the 'a' portion of the uniform is the scaled and inverted tolerance
+    fragBuilder->codeAppendf("dist = dist * %s.a - (%s.a - 1.0);",
+                             kColorAndTolUni, kColorAndTolUni);
+
+    fragBuilder->codeAppendf("vec4 %s = vec4(dist);", kCoverageName);
+    if (srcCoverage) {
+        fragBuilder->codeAppendf("%s *= %s;", kCoverageName, srcCoverage);
+    }
+}
+
+class GLAvoidFP : public GrGLSLFragmentProcessor {
+public:
+    void emitCode(EmitArgs& args) override {
+        const AvoidFP& avoid = args.fFp.cast<AvoidFP>();
+
+        GrGLSLFragmentBuilder* fragBuilder = args.fFragBuilder;
+        SkString dstColor("dstColor");
+        this->emitChild(0, nullptr, &dstColor, args);
+
+        fColorAndTolUni = args.fUniformHandler->addUniform(
+                                                 GrGLSLUniformHandler::kFragment_Visibility,
+                                                 kVec4f_GrSLType, kDefault_GrSLPrecision,
+                                                 "colorAndTol");
+        const char* kColorAndTolUni = args.fUniformHandler->getUniformCStr(fColorAndTolUni);
+
+        const char* kCoverageName = "newCoverage";
+
+        // add_avoid_code emits the code needed to compute the new coverage
+        add_avoid_code(fragBuilder,
+                       dstColor.c_str(), nullptr,
+                       kColorAndTolUni, kCoverageName, avoid.mode());
+
+        // The raster implementation's quantization and behavior yield a very noticeable
+        // effect near zero (0.0039 = 1/256).
+        fragBuilder->codeAppendf("if (%s.r < 0.0039) { %s = %s; } else {",
+                                 kCoverageName, args.fOutputColor, dstColor.c_str());
+        fragBuilder->codeAppendf("%s = %s * %s + (vec4(1.0)-%s) * %s;",
+                                 args.fOutputColor,
+                                 kCoverageName, args.fInputColor ? args.fInputColor : "vec4(1.0)",
+                                 kCoverageName, dstColor.c_str());
+        fragBuilder->codeAppend("}");
+    }
+
+    static void GenKey(const GrProcessor& proc, const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
+        const AvoidFP& avoid = proc.cast<AvoidFP>();
+        uint32_t key = avoid.mode() == SkAvoidXfermode::kTargetColor_Mode ? 1 : 0;
+        b->add32(key);
+    }
+
+protected:
+    void onSetData(const GrGLSLProgramDataManager& pdman, const GrProcessor& proc) override {
+        const AvoidFP& avoid = proc.cast<AvoidFP>();
+        pdman.set4f(fColorAndTolUni,
+                    SkColorGetR(avoid.opColor())/255.0f,
+                    SkColorGetG(avoid.opColor())/255.0f,
+                    SkColorGetB(avoid.opColor())/255.0f,
+                    256.0f/(avoid.tol()+1.0f));
+    }
+
+private:
+    GrGLSLProgramDataManager::UniformHandle fColorAndTolUni;
+
+    typedef GrGLSLFragmentProcessor INHERITED;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+GrGLSLFragmentProcessor* AvoidFP::onCreateGLSLInstance() const {
+    return new GLAvoidFP;
+}
+
+void AvoidFP::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
+    GLAvoidFP::GenKey(*this, caps, b);
+}
+
+const GrFragmentProcessor* AvoidFP::TestCreate(GrProcessorTestData* d) {
+    SkColor opColor = d->fRandom->nextU();
+    uint8_t tolerance = d->fRandom->nextBits(8);
+    SkAvoidXfermode::Mode mode = d->fRandom->nextBool() ? SkAvoidXfermode::kAvoidColor_Mode
+                                                        : SkAvoidXfermode::kTargetColor_Mode;
+
+    SkAutoTUnref<const GrFragmentProcessor> dst(GrProcessorUnitTest::CreateChildFP(d));
+    return new AvoidFP(opColor, tolerance, mode, dst);
+}
+
+GR_DEFINE_FRAGMENT_PROCESSOR_TEST(AvoidFP);
+
+///////////////////////////////////////////////////////////////////////////////
+// Xfer Processor
+///////////////////////////////////////////////////////////////////////////////
+
+class AvoidXP : public GrXferProcessor {
+public:
+    AvoidXP(const DstTexture* dstTexture, bool hasMixedSamples,
+            SkColor opColor, uint8_t tolerance, SkAvoidXfermode::Mode mode)
+        : INHERITED(dstTexture, true, hasMixedSamples)
+        , fOpColor(opColor)
+        , fTolerance(tolerance)
+        , fMode(mode) {
+        this->initClassID<AvoidXP>();
+    }
+
+    const char* name() const override { return "Avoid"; }
+
+    GrGLSLXferProcessor* createGLSLInstance() const override;
+
+    SkColor opColor() const { return fOpColor; }
+    uint8_t tol() const { return fTolerance; }
+    SkAvoidXfermode::Mode mode() const { return fMode; }
+
+private:
+    GrXferProcessor::OptFlags onGetOptimizations(const GrPipelineOptimizations& optimizations,
+                                                 bool doesStencilWrite,
+                                                 GrColor* overrideColor,
+                                                 const GrCaps& caps) const override {
+        return GrXferProcessor::kNone_OptFlags;
+    }
+
+    void onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const override;
+
+    bool onIsEqual(const GrXferProcessor& xpBase) const override { 
+        const AvoidXP& xp = xpBase.cast<AvoidXP>();
+
+        return fOpColor == xp.fOpColor &&
+               fTolerance == xp.fTolerance &&
+               fMode == xp.fMode;
+    }
+
+    SkColor               fOpColor;
+    uint8_t               fTolerance;
+    SkAvoidXfermode::Mode fMode;
+
+    typedef GrXferProcessor INHERITED;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+class GLAvoidXP : public GrGLSLXferProcessor {
+public:
+    static void GenKey(const GrProcessor& processor, const GrGLSLCaps&, GrProcessorKeyBuilder* b) {
+        const AvoidXP& avoid = processor.cast<AvoidXP>();
+        uint32_t key = SkAvoidXfermode::kTargetColor_Mode == avoid.mode() ? 1 : 0;
+        b->add32(key);
+    }
+
+private:
+    void emitBlendCodeForDstRead(GrGLSLXPFragmentBuilder* fragBuilder,
+                                 GrGLSLUniformHandler* uniformHandler,
+                                 const char* srcColor,
+                                 const char* srcCoverage,
+                                 const char* dstColor,
+                                 const char* outColor,
+                                 const char* outColorSecondary,
+                                 const GrXferProcessor& proc) override {
+        const AvoidXP& avoid = proc.cast<AvoidXP>();
+
+        fColorAndTolUni = uniformHandler->addUniform(GrGLSLUniformHandler::kFragment_Visibility,
+                                                     kVec4f_GrSLType, kDefault_GrSLPrecision,
+                                                     "colorAndTol");
+        const char* kColorandTolUni = uniformHandler->getUniformCStr(fColorAndTolUni);
+
+        const char* kCoverageName = "newCoverage";
+
+        // add_avoid_code emits the code needed to compute the new coverage
+        add_avoid_code(fragBuilder,
+                       dstColor, srcCoverage,
+                       kColorandTolUni, kCoverageName, avoid.mode());
+
+        // The raster implementation's quantization and behavior yield a very noticeable
+        // effect near zero (0.0039 = 1/256).
+        fragBuilder->codeAppendf("if (%s.r < 0.0039) { %s = %s; } else {",
+                                 kCoverageName, outColor, dstColor);
+        fragBuilder->codeAppendf("%s = %s;", outColor, srcColor ? srcColor : "vec4(1.0)");
+        INHERITED::DefaultCoverageModulation(fragBuilder, kCoverageName, dstColor, outColor,
+                                             outColorSecondary, proc);
+        fragBuilder->codeAppend("}");
+    }
+
+    void onSetData(const GrGLSLProgramDataManager& pdman,
+                   const GrXferProcessor& processor) override {
+        const AvoidXP& avoid = processor.cast<AvoidXP>();
+        pdman.set4f(fColorAndTolUni,
+                    SkColorGetR(avoid.opColor())/255.0f,
+                    SkColorGetG(avoid.opColor())/255.0f,
+                    SkColorGetB(avoid.opColor())/255.0f,
+                    256.0f/(avoid.tol()+1.0f));
+    };
+
+    GrGLSLProgramDataManager::UniformHandle fColorAndTolUni;
+
+    typedef GrGLSLXferProcessor INHERITED;
+};
+
+///////////////////////////////////////////////////////////////////////////////
+
+void AvoidXP::onGetGLSLProcessorKey(const GrGLSLCaps& caps, GrProcessorKeyBuilder* b) const {
+    GLAvoidXP::GenKey(*this, caps, b);
+}
+
+GrGLSLXferProcessor* AvoidXP::createGLSLInstance() const { return new GLAvoidXP; }
+
+///////////////////////////////////////////////////////////////////////////////
+class GrAvoidXPFactory : public GrXPFactory {
+public:
+    static GrXPFactory* Create(SkColor opColor, uint8_t tolerance,
+                               SkAvoidXfermode::Mode mode) {
+        return new GrAvoidXPFactory(opColor, tolerance, mode);
+    }
+
+    void getInvariantBlendedColor(const GrProcOptInfo& colorPOI,
+                                  GrXPFactory::InvariantBlendedColor* blendedColor) const override {
+        blendedColor->fWillBlendWithDst = true;
+        blendedColor->fKnownColorFlags = kNone_GrColorComponentFlags;
+    }
+
+private:
+    GrAvoidXPFactory(SkColor opColor, uint8_t tolerance, SkAvoidXfermode::Mode mode)
+        : fOpColor(opColor)
+        , fTolerance(tolerance)
+        , fMode(mode) {
+        this->initClassID<GrAvoidXPFactory>();
+    }
+
+    GrXferProcessor* onCreateXferProcessor(const GrCaps& caps,
+                                           const GrPipelineOptimizations& optimizations,
+                                           bool hasMixedSamples,
+                                           const DstTexture* dstTexture) const override {
+        return new AvoidXP(dstTexture, hasMixedSamples, fOpColor, fTolerance, fMode);
+    }
+
+    bool onWillReadDstColor(const GrCaps& caps,
+                            const GrPipelineOptimizations& optimizations,
+                            bool hasMixedSamples) const override {
+        return true;
+    }
+
+    bool onIsEqual(const GrXPFactory& xpfBase) const override {
+        const GrAvoidXPFactory& xpf = xpfBase.cast<GrAvoidXPFactory>();
+        return fOpColor == xpf.fOpColor &&
+               fTolerance == xpf.fTolerance &&
+               fMode == xpf.fMode;
+    }
+
+    GR_DECLARE_XP_FACTORY_TEST;
+
+    SkColor               fOpColor;
+    uint8_t               fTolerance;
+    SkAvoidXfermode::Mode fMode;
+
+    typedef GrXPFactory INHERITED;
+};
+
+GR_DEFINE_XP_FACTORY_TEST(GrAvoidXPFactory);
+
+const GrXPFactory* GrAvoidXPFactory::TestCreate(GrProcessorTestData* d) {
+    SkColor opColor = d->fRandom->nextU();
+    uint8_t tolerance = d->fRandom->nextBits(8);
+    SkAvoidXfermode::Mode mode = d->fRandom->nextBool() ? SkAvoidXfermode::kAvoidColor_Mode
+                                                        : SkAvoidXfermode::kTargetColor_Mode;
+    return GrAvoidXPFactory::Create(opColor, tolerance, mode);
+}
+
+///////////////////////////////////////////////////////////////////////////////
+
+bool SkAvoidXfermode::asFragmentProcessor(const GrFragmentProcessor** output,
+                                          const GrFragmentProcessor* dst) const {
+    if (output) {
+        *output = AvoidFP::Create(fOpColor, fTolerance, fMode, dst);
+    }
+    return true;
+}
+
+bool SkAvoidXfermode::asXPFactory(GrXPFactory** xpf) const {
+    if (xpf) {
+        *xpf = GrAvoidXPFactory::Create(fOpColor, fTolerance, fMode);
+    }
+    return true;
+}
+#endif
+
 #ifndef SK_IGNORE_TO_STRING
 void SkAvoidXfermode::toString(SkString* str) const {
     str->append("AvoidXfermode: opColor: ");
     str->appendHex(fOpColor);
-    str->appendf("distMul: %d ", fDistMul);
+    str->appendf("tolerance: %d ", fTolerance);
 
     static const char* gModeStrings[] = { "Avoid", "Target" };
 
diff --git a/src/effects/SkPixelXorXfermode.cpp b/src/effects/SkPixelXorXfermode.cpp
index 94fb8df..2eb4294 100644
--- a/src/effects/SkPixelXorXfermode.cpp
+++ b/src/effects/SkPixelXorXfermode.cpp
@@ -345,8 +345,8 @@
     }
 
     bool onWillReadDstColor(const GrCaps& caps,
-                          const GrPipelineOptimizations& optimizations,
-                          bool hasMixedSamples) const override {
+                            const GrPipelineOptimizations& optimizations,
+                            bool hasMixedSamples) const override {
         return true;
     }
 
diff --git a/src/gpu/GrProcessor.cpp b/src/gpu/GrProcessor.cpp
index 52f1de7..15206c2 100644
--- a/src/gpu/GrProcessor.cpp
+++ b/src/gpu/GrProcessor.cpp
@@ -48,9 +48,9 @@
  * we verify the count is as expected.  If a new factory is added, then these numbers must be
  * manually adjusted.
  */
-static const int kFPFactoryCount = 40;
+static const int kFPFactoryCount = 41;
 static const int kGPFactoryCount = 14;
-static const int kXPFactoryCount = 7;
+static const int kXPFactoryCount = 8;
 
 template<>
 void GrProcessorTestFactory<GrFragmentProcessor>::VerifyFactoryCount() {