Revert "GaussianConvolutionFragmentProcessor uses GrTextureEffect."

This reverts commit eb48024f8bc79001439071216cb106ba95240cdf.

Reason for revert: Failing additional layout test on Win10

Original change's description:
> GaussianConvolutionFragmentProcessor uses GrTextureEffect.
> 
> Also removes now unused GrShaderVar::appendArrayAccess.
> 
> Bug: skia:10139
> 
> Change-Id: Ic2583a6822e88510551b1031f3fb130266b3f395
> Reviewed-on: https://skia-review.googlesource.com/c/skia/+/283440
> Commit-Queue: Brian Salomon <bsalomon@google.com>
> Reviewed-by: Michael Ludwig <michaelludwig@google.com>

TBR=bsalomon@google.com,michaelludwig@google.com

Change-Id: I7403e97c51ba966e52679b5a048a962795c4271b
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: skia:10139
Reviewed-on: https://skia-review.googlesource.com/c/skia/+/283636
Reviewed-by: Brian Salomon <bsalomon@google.com>
Commit-Queue: Brian Salomon <bsalomon@google.com>
diff --git a/src/gpu/effects/GrGaussianConvolutionFragmentProcessor.cpp b/src/gpu/effects/GrGaussianConvolutionFragmentProcessor.cpp
index 2b32d79..118efe8 100644
--- a/src/gpu/effects/GrGaussianConvolutionFragmentProcessor.cpp
+++ b/src/gpu/effects/GrGaussianConvolutionFragmentProcessor.cpp
@@ -9,7 +9,6 @@
 
 #include "src/gpu/GrTexture.h"
 #include "src/gpu/GrTextureProxy.h"
-#include "src/gpu/effects/GrTextureEffect.h"
 #include "src/gpu/glsl/GrGLSLFragmentProcessor.h"
 #include "src/gpu/glsl/GrGLSLFragmentShaderBuilder.h"
 #include "src/gpu/glsl/GrGLSLProgramDataManager.h"
@@ -30,7 +29,8 @@
 
 private:
     UniformHandle fKernelUni;
-    UniformHandle fIncrementUni;
+    UniformHandle fImageIncrementUni;
+    UniformHandle fBoundsUni;
 
     typedef GrGLSLFragmentProcessor INHERITED;
 };
@@ -40,54 +40,136 @@
             args.fFp.cast<GrGaussianConvolutionFragmentProcessor>();
 
     GrGLSLUniformHandler* uniformHandler = args.fUniformHandler;
-
-    const char* inc;
-    fIncrementUni = uniformHandler->addUniform(&ce, kFragment_GrShaderFlag, kHalf2_GrSLType,
-                                               "Increment", &inc);
+    fImageIncrementUni = uniformHandler->addUniform(&ce, kFragment_GrShaderFlag, kHalf2_GrSLType,
+                                                    "ImageIncrement");
+    if (ce.useBounds()) {
+        fBoundsUni = uniformHandler->addUniform(&ce, kFragment_GrShaderFlag, kHalf2_GrSLType,
+                                                "Bounds");
+    }
 
     int width = ce.width();
 
     int arrayCount = (width + 3) / 4;
     SkASSERT(4 * arrayCount >= width);
 
-    const char* kernel;
     fKernelUni = uniformHandler->addUniformArray(&ce, kFragment_GrShaderFlag, kHalf4_GrSLType,
-                                                 "Kernel", arrayCount, &kernel);
+                                                 "Kernel", arrayCount);
 
     GrGLSLFPFragmentBuilder* fragBuilder = args.fFragBuilder;
-    auto coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint,
-                                                ce.sampleMatrix());
+    SkString coords2D = fragBuilder->ensureCoords2D(args.fTransformedCoords[0].fVaryingPoint,
+                                                    ce.sampleMatrix());
 
     fragBuilder->codeAppendf("%s = half4(0, 0, 0, 0);", args.fOutputColor);
 
-    fragBuilder->codeAppendf("float2 coord = %s - %d.0 * %s;", coords2D.c_str(), ce.radius(), inc);
+    const GrShaderVar& kernel = uniformHandler->getUniformVariable(fKernelUni);
+    const char* imgInc = uniformHandler->getUniformCStr(fImageIncrementUni);
+
+    fragBuilder->codeAppendf("float2 coord = %s - %d.0 * %s;", coords2D.c_str(), ce.radius(), imgInc);
     fragBuilder->codeAppend("float2 coordSampled = half2(0, 0);");
 
     // Manually unroll loop because some drivers don't; yields 20-30% speedup.
-    static constexpr const char* kVecSuffix[4] = {".x", ".y", ".z", ".w"};
+    const char* kVecSuffix[4] = {".x", ".y", ".z", ".w"};
     for (int i = 0; i < width; i++) {
+        SkString index;
         SkString kernelIndex;
-        kernelIndex.printf("%s[%d]", kernel, i/4);
+        index.appendS32(i / 4);
+        kernel.appendArrayAccess(index.c_str(), &kernelIndex);
         kernelIndex.append(kVecSuffix[i & 0x3]);
 
         fragBuilder->codeAppend("coordSampled = coord;");
-        auto sample = this->invokeChild(0, args, "coordSampled");
-        fragBuilder->codeAppendf("%s += %s", args.fOutputColor, sample.c_str());
-        fragBuilder->codeAppendf(" * %s;", kernelIndex.c_str());
-        fragBuilder->codeAppendf("coord += %s;", inc);
+        if (ce.useBounds()) {
+            // We used to compute a bool indicating whether we're in bounds or not, cast it to a
+            // float, and then mul weight*texture_sample by the float. However, the Adreno 430 seems
+            // to have a bug that caused corruption.
+            const char* bounds = uniformHandler->getUniformCStr(fBoundsUni);
+            const char* component = ce.direction() == Direction::kY ? "y" : "x";
+
+            switch (ce.mode()) {
+                case GrTextureDomain::kClamp_Mode: {
+                    fragBuilder->codeAppendf("coordSampled.%s = clamp(coord.%s, %s.x, %s.y);\n",
+                                             component, component, bounds, bounds);
+                    break;
+                }
+                // Deferring implementing kMirrorRepeat until we use DomainEffects as
+                // child processors. Fallback to Repeat.
+                case GrTextureDomain::kMirrorRepeat_Mode:
+                case GrTextureDomain::kRepeat_Mode: {
+                    fragBuilder->codeAppendf("coordSampled.%s = "
+                                             "mod(coord.%s - %s.x, %s.y - %s.x) + %s.x;\n",
+                                             component, component, bounds, bounds, bounds, bounds);
+                    break;
+                }
+                case GrTextureDomain::kDecal_Mode: {
+                    fragBuilder->codeAppendf("if (coord.%s >= %s.x && coord.%s <= %s.y) {",
+                                             component, bounds, component, bounds);
+                    break;
+                }
+                default: {
+                    SK_ABORT("Unsupported operation.");
+                }
+            }
+        }
+        fragBuilder->codeAppendf("%s += ", args.fOutputColor);
+        fragBuilder->appendTextureLookup(args.fTexSamplers[0], "coordSampled");
+        fragBuilder->codeAppendf(" * %s;\n", kernelIndex.c_str());
+        if (GrTextureDomain::kDecal_Mode == ce.mode()) {
+            fragBuilder->codeAppend("}");
+        }
+        fragBuilder->codeAppendf("coord += %s;\n", imgInc);
     }
-    fragBuilder->codeAppendf("%s *= %s;", args.fOutputColor, args.fInputColor);
+    fragBuilder->codeAppendf("%s *= %s;\n", args.fOutputColor, args.fInputColor);
 }
 
 void GrGLConvolutionEffect::onSetData(const GrGLSLProgramDataManager& pdman,
                                       const GrFragmentProcessor& processor) {
-    const auto& conv = processor.cast<GrGaussianConvolutionFragmentProcessor>();
+    const GrGaussianConvolutionFragmentProcessor& conv =
+            processor.cast<GrGaussianConvolutionFragmentProcessor>();
+    const auto& view = conv.textureSampler(0).view();
+    GrSurfaceProxy* proxy = view.proxy();
+    GrTexture& texture = *proxy->peekTexture();
 
-    float increment[2] = {};
-    increment[static_cast<int>(conv.direction())] = 1;
-    pdman.set2fv(fIncrementUni, 1, increment);
+    float imageIncrement[2] = {0};
+    float ySign = view.origin() != kTopLeft_GrSurfaceOrigin ? 1.0f : -1.0f;
+    switch (conv.direction()) {
+        case Direction::kX:
+            imageIncrement[0] = 1.0f / texture.width();
+            break;
+        case Direction::kY:
+            imageIncrement[1] = ySign / texture.height();
+            break;
+        default:
+            SK_ABORT("Unknown filter direction.");
+    }
+    pdman.set2fv(fImageIncrementUni, 1, imageIncrement);
+    if (conv.useBounds()) {
+        float bounds[2] = {0};
+        bounds[0] = conv.bounds()[0];
+        bounds[1] = conv.bounds()[1];
+        if (GrTextureDomain::kClamp_Mode == conv.mode()) {
+            bounds[0] += SK_ScalarHalf;
+            bounds[1] -= SK_ScalarHalf;
+        }
+        if (Direction::kX == conv.direction()) {
+            SkScalar inv = SkScalarInvert(SkIntToScalar(texture.width()));
+            bounds[0] *= inv;
+            bounds[1] *= inv;
+        } else {
+            SkScalar inv = SkScalarInvert(SkIntToScalar(texture.height()));
+            if (view.origin() != kTopLeft_GrSurfaceOrigin) {
+                float tmp = bounds[0];
+                bounds[0] = 1.0f - (inv * bounds[1]);
+                bounds[1] = 1.0f - (inv * tmp);
+            } else {
+                bounds[0] *= inv;
+                bounds[1] *= inv;
+            }
+        }
 
+        SkASSERT(bounds[0] <= bounds[1]);
+        pdman.set2f(fBoundsUni, bounds[0], bounds[1]);
+    }
     int width = conv.width();
+
     int arrayCount = (width + 3) / 4;
     SkASSERT(4 * arrayCount >= width);
     pdman.set4fv(fKernelUni, arrayCount, conv.kernel());
@@ -95,8 +177,15 @@
 
 void GrGLConvolutionEffect::GenKey(const GrProcessor& processor, const GrShaderCaps&,
                                    GrProcessorKeyBuilder* b) {
-    const auto& conv = processor.cast<GrGaussianConvolutionFragmentProcessor>();
-    b->add32(conv.radius());
+    const GrGaussianConvolutionFragmentProcessor& conv =
+            processor.cast<GrGaussianConvolutionFragmentProcessor>();
+    uint32_t key = conv.radius();
+    key <<= 3;
+    if (conv.useBounds()) {
+        key |= Direction::kY == conv.direction() ? 0x4 : 0x0;
+    }
+    key |= static_cast<uint32_t>(conv.mode());
+    b->add32(key);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -126,67 +215,79 @@
     }
 }
 
-std::unique_ptr<GrFragmentProcessor> GrGaussianConvolutionFragmentProcessor::Make(
+GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
         GrSurfaceProxyView view,
         SkAlphaType alphaType,
-        Direction dir,
-        int halfWidth,
-        float gaussianSigma,
-        GrSamplerState::WrapMode wm,
-        const int bounds[2],
-        const GrCaps& caps) {
-    std::unique_ptr<GrFragmentProcessor> child;
-    GrSamplerState sampler;
-    switch (dir) {
-        case Direction::kX: sampler.setWrapModeX(wm); break;
-        case Direction::kY: sampler.setWrapModeY(wm); break;
-    }
-    if (bounds) {
-        SkASSERT(bounds[0] < bounds[1]);
-        SkRect subset;
-        switch (dir) {
-            case Direction::kX:
-                subset = SkRect::MakeLTRB(bounds[0], 0, bounds[1], view.height());
-                break;
-            case Direction::kY:
-                subset = SkRect::MakeLTRB(0, bounds[0], view.width(), bounds[1]);
-                break;
-        }
-        child = GrTextureEffect::MakeSubset(std::move(view), alphaType, SkMatrix::I(), sampler,
-                                            subset, caps);
-    } else {
-        child = GrTextureEffect::Make(std::move(view), alphaType, SkMatrix::I(), sampler, caps);
-    }
-    return std::unique_ptr<GrFragmentProcessor>(new GrGaussianConvolutionFragmentProcessor(
-            std::move(child), dir, halfWidth, gaussianSigma));
-}
-
-GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
-        std::unique_ptr<GrFragmentProcessor> child,
         Direction direction,
         int radius,
-        float gaussianSigma)
+        float gaussianSigma,
+        GrTextureDomain::Mode mode,
+        int bounds[2])
         : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID,
-                    ProcessorOptimizationFlags(child.get()))
+                    ModulateForSamplerOptFlags(alphaType, mode == GrTextureDomain::kDecal_Mode))
+        , fCoordTransform(view.proxy(), view.origin())
+        , fTextureSampler(std::move(view))
         , fRadius(radius)
-        , fDirection(direction) {
-    child->setSampledWithExplicitCoords();
-    this->registerChildProcessor(std::move(child));
-    SkASSERT(radius <= kMaxKernelRadius);
-    fill_in_1D_gaussian_kernel(fKernel, this->width(), gaussianSigma, this->radius());
+        , fDirection(direction)
+        , fMode(mode) {
     this->addCoordTransform(&fCoordTransform);
+    this->setTextureSamplerCnt(1);
+    SkASSERT(radius <= kMaxKernelRadius);
+
+    fill_in_1D_gaussian_kernel(fKernel, this->width(), gaussianSigma, this->radius());
+    // SkGpuBlurUtils is not as aggressive as it once was about avoiding domains. So we check
+    // here if we can omit the domain. TODO: remove this when this effect uses a child to
+    // sample the texture.
+    auto samplerProxy = fTextureSampler.proxy();
+    if (!samplerProxy->isFullyLazy()) {
+        int wh = (fDirection == Direction::kX) ? samplerProxy->backingStoreDimensions().width()
+                                               : samplerProxy->backingStoreDimensions().height();
+        if (bounds[0] == 0 && bounds[1] == wh) {
+            bool useSampler = false;
+            GrSamplerState::WrapMode samplerMode = GrSamplerState::WrapMode::kClamp;
+            switch (fMode) {
+                case GrTextureDomain::kClamp_Mode:
+                case GrTextureDomain::kIgnore_Mode:
+                    useSampler = true;
+                    break;
+                case GrTextureDomain::kRepeat_Mode:
+                    useSampler = true;
+                    samplerMode = GrSamplerState::WrapMode::kRepeat;
+                    break;
+                case GrTextureDomain::kMirrorRepeat_Mode:
+                    useSampler = true;
+                    samplerMode = GrSamplerState::WrapMode::kMirrorRepeat;
+                    break;
+                case GrTextureDomain::kDecal_Mode:
+                    // Not sure if we support this in HW without having GrCaps here.
+                    // Just wait until we replace this with GrTextureEffect.
+                    break;
+            }
+            if (useSampler) {
+                fMode = GrTextureDomain::kIgnore_Mode;
+                if (fDirection == Direction::kX) {
+                    fTextureSampler.samplerState().setWrapModeX(samplerMode);
+                } else {
+                    fTextureSampler.samplerState().setWrapModeY(samplerMode);
+                }
+            }
+        }
+    }
+    memcpy(fBounds, bounds, sizeof(fBounds));
 }
 
 GrGaussianConvolutionFragmentProcessor::GrGaussianConvolutionFragmentProcessor(
         const GrGaussianConvolutionFragmentProcessor& that)
         : INHERITED(kGrGaussianConvolutionFragmentProcessor_ClassID, that.optimizationFlags())
+        , fCoordTransform(that.fCoordTransform)
+        , fTextureSampler(that.fTextureSampler)
         , fRadius(that.fRadius)
-        , fDirection(that.fDirection) {
-    auto child = that.childProcessor(0).clone();
-    child->setSampledWithExplicitCoords();
-    this->registerChildProcessor(std::move(child));
-    memcpy(fKernel, that.fKernel, that.width() * sizeof(float));
+        , fDirection(that.fDirection)
+        , fMode(that.fMode) {
     this->addCoordTransform(&fCoordTransform);
+    this->setTextureSamplerCnt(1);
+    memcpy(fKernel, that.fKernel, that.width() * sizeof(float));
+    memcpy(fBounds, that.fBounds, sizeof(fBounds));
 }
 
 void GrGaussianConvolutionFragmentProcessor::onGetGLSLProcessorKey(const GrShaderCaps& caps,
@@ -199,9 +300,12 @@
 }
 
 bool GrGaussianConvolutionFragmentProcessor::onIsEqual(const GrFragmentProcessor& sBase) const {
-    const auto& that = sBase.cast<GrGaussianConvolutionFragmentProcessor>();
-    return this->radius() == that.radius() && this->direction() == that.direction() &&
-           std::equal(fKernel, fKernel + this->width(), that.fKernel);
+    const GrGaussianConvolutionFragmentProcessor& s =
+            sBase.cast<GrGaussianConvolutionFragmentProcessor>();
+    return (this->radius() == s.radius() && this->direction() == s.direction() &&
+            this->mode() == s.mode() &&
+            0 == memcmp(fBounds, s.fBounds, sizeof(fBounds)) &&
+            0 == memcmp(fKernel, s.fKernel, this->width() * sizeof(float)));
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -213,27 +317,25 @@
         GrProcessorTestData* d) {
     auto [view, ct, at] = d->randomView();
 
-    Direction dir;
     int bounds[2];
-    do {
-        if (d->fRandom->nextBool()) {
-            dir = Direction::kX;
-            bounds[0] = d->fRandom->nextRangeU(0, view.width() - 1);
-            bounds[1] = d->fRandom->nextRangeU(0, view.width() - 1);
-        } else {
-            dir = Direction::kY;
-            bounds[0] = d->fRandom->nextRangeU(0, view.height() - 1);
-            bounds[1] = d->fRandom->nextRangeU(0, view.height() - 1);
-        }
-    } while (bounds[0] == bounds[1]);
-    std::sort(bounds, bounds + 2);
+    int modeIdx = d->fRandom->nextRangeU(0, GrTextureDomain::kModeCount-1);
 
-    auto wm = static_cast<GrSamplerState::WrapMode>(
-            d->fRandom->nextULessThan(GrSamplerState::kWrapModeCount));
+    Direction dir;
+    if (d->fRandom->nextBool()) {
+        dir = Direction::kX;
+        bounds[0] = d->fRandom->nextRangeU(0, view.width()-2);
+        bounds[1] = d->fRandom->nextRangeU(bounds[0]+1, view.width()-1);
+    } else {
+        dir = Direction::kY;
+        bounds[0] = d->fRandom->nextRangeU(0, view.height()-2);
+        bounds[1] = d->fRandom->nextRangeU(bounds[0]+1, view.height()-1);
+    }
+
     int radius = d->fRandom->nextRangeU(1, kMaxKernelRadius);
     float sigma = radius / 3.f;
 
-    return GrGaussianConvolutionFragmentProcessor::Make(std::move(view), at, dir, radius, sigma, wm,
-                                                        bounds, *d->caps());
+    return GrGaussianConvolutionFragmentProcessor::Make(std::move(view), at, dir, radius, sigma,
+                                                        static_cast<GrTextureDomain::Mode>(modeIdx),
+                                                        bounds);
 }
 #endif