Revert "Remove texture sampling from GrConfigConversionEffect"

This reverts commit 3e7cddaf32e280fe9f32eec5bfdd8168ca4941b6.

Reason for revert: Precision issue on some mobile GPUs.

Original change's description:
> Remove texture sampling from GrConfigConversionEffect
> 
> Re-land fixed version of https://skia-review.googlesource.com/c/10026/
> 
> BUG=skia:
> 
> Change-Id: I0754ffb72da2966eb57e5cd7ec818b1cdce84a74
> Reviewed-on: https://skia-review.googlesource.com/10056
> Reviewed-by: Brian Salomon <bsalomon@google.com>
> Commit-Queue: Brian Osman <brianosman@google.com>
> 

TBR=bsalomon@google.com,robertphillips@google.com,brianosman@google.com,reviews@skia.org
NOPRESUBMIT=true
NOTREECHECKS=true
NOTRY=true
BUG=skia:

Change-Id: I66f52efe191b170612775e26d84a2af3d3f856af
Reviewed-on: https://skia-review.googlesource.com/10118
Reviewed-by: Brian Osman <brianosman@google.com>
Commit-Queue: Brian Osman <brianosman@google.com>
diff --git a/src/gpu/effects/GrConfigConversionEffect.cpp b/src/gpu/effects/GrConfigConversionEffect.cpp
index b2c9e3e..2c6589d 100644
--- a/src/gpu/effects/GrConfigConversionEffect.cpp
+++ b/src/gpu/effects/GrConfigConversionEffect.cpp
@@ -10,6 +10,7 @@
 #include "GrClip.h"
 #include "GrContext.h"
 #include "GrRenderTargetContext.h"
+#include "GrSimpleTextureEffect.h"
 #include "SkMatrix.h"
 #include "glsl/GrGLSLFragmentProcessor.h"
 #include "glsl/GrGLSLFragmentShaderBuilder.h"
@@ -29,11 +30,10 @@
 
         fragBuilder->codeAppendf("%s;", tmpDecl.c_str());
 
-        if (nullptr == args.fInputColor) {
-            // could optimize this case, but we aren't for now.
-            args.fInputColor = "vec4(1)";
-        }
-        fragBuilder->codeAppendf("%s = %s;", tmpVar.c_str(), args.fInputColor);
+        fragBuilder->codeAppendf("%s = ", tmpVar.c_str());
+        fragBuilder->appendTextureLookup(args.fTexSamplers[0], args.fTransformedCoords[0].c_str(),
+                                         args.fTransformedCoords[0].getType());
+        fragBuilder->codeAppend(";");
 
         switch (pmConversion) {
             case GrConfigConversionEffect::kMulByAlpha_RoundUp_PMConversion:
@@ -68,6 +68,10 @@
                 break;
         }
         fragBuilder->codeAppendf("%s = %s;", args.fOutputColor, tmpVar.c_str());
+
+        SkString modulate;
+        GrGLSLMulVarBy4f(&modulate, args.fOutputColor, args.fInputColor);
+        fragBuilder->codeAppend(modulate.c_str());
     }
 
     static inline void GenKey(const GrProcessor& processor, const GrShaderCaps&,
@@ -83,11 +87,29 @@
 };
 
 ///////////////////////////////////////////////////////////////////////////////
-
-GrConfigConversionEffect::GrConfigConversionEffect(PMConversion pmConversion)
-        : INHERITED(kNone_OptimizationFlags)
+GrConfigConversionEffect::GrConfigConversionEffect(GrTexture* texture,
+                                                   PMConversion pmConversion,
+                                                   const SkMatrix& matrix)
+        : INHERITED(texture, nullptr, matrix, kNone_OptimizationFlags)
         , fPMConversion(pmConversion) {
     this->initClassID<GrConfigConversionEffect>();
+    // We expect to get here with non-BGRA/RGBA only if we're doing not doing a premul/unpremul
+    // conversion.
+    SkASSERT(kRGBA_8888_GrPixelConfig == texture->config() ||
+             kBGRA_8888_GrPixelConfig == texture->config());
+}
+
+GrConfigConversionEffect::GrConfigConversionEffect(GrResourceProvider* resourceProvider,
+                                                   sk_sp<GrTextureProxy> proxy,
+                                                   PMConversion pmConversion,
+                                                   const SkMatrix& matrix)
+        : INHERITED(resourceProvider, kNone_OptimizationFlags, proxy, nullptr, matrix)
+        , fPMConversion(pmConversion) {
+    this->initClassID<GrConfigConversionEffect>();
+    // We expect to get here with non-BGRA/RGBA only if we're doing not doing a premul/unpremul
+    // conversion.
+    SkASSERT(kRGBA_8888_GrPixelConfig == proxy->config() ||
+             kBGRA_8888_GrPixelConfig == proxy->config());
 }
 
 bool GrConfigConversionEffect::onIsEqual(const GrFragmentProcessor& s) const {
@@ -108,7 +130,10 @@
 #if GR_TEST_UTILS
 sk_sp<GrFragmentProcessor> GrConfigConversionEffect::TestCreate(GrProcessorTestData* d) {
     PMConversion pmConv = static_cast<PMConversion>(d->fRandom->nextULessThan(kPMConversionCnt));
-    return sk_sp<GrFragmentProcessor>(new GrConfigConversionEffect(pmConv));
+    return sk_sp<GrFragmentProcessor>(new GrConfigConversionEffect(
+                            d->resourceProvider(),
+                            d->textureProxy(GrProcessorUnitTest::kSkiaPMTextureIdx),
+                            pmConv, GrTest::TestMatrix(d->fRandom)));
 }
 #endif
 
@@ -201,11 +226,14 @@
         GrPaint paint1;
         GrPaint paint2;
         GrPaint paint3;
-        sk_sp<GrFragmentProcessor> pmToUPM(new GrConfigConversionEffect(*pmToUPMRule));
-        sk_sp<GrFragmentProcessor> upmToPM(new GrConfigConversionEffect(*upmToPMRule));
+        sk_sp<GrFragmentProcessor> pmToUPM1(new GrConfigConversionEffect(
+                resourceProvider, dataProxy, *pmToUPMRule, SkMatrix::I()));
+        sk_sp<GrFragmentProcessor> upmToPM(new GrConfigConversionEffect(
+                resourceProvider, readRTC->asTextureProxyRef(), *upmToPMRule, SkMatrix::I()));
+        sk_sp<GrFragmentProcessor> pmToUPM2(new GrConfigConversionEffect(
+                resourceProvider, tempRTC->asTextureProxyRef(), *pmToUPMRule, SkMatrix::I()));
 
-        paint1.addColorTextureProcessor(resourceProvider, dataProxy, nullptr, SkMatrix::I());
-        paint1.addColorFragmentProcessor(pmToUPM);
+        paint1.addColorFragmentProcessor(std::move(pmToUPM1));
         paint1.setPorterDuffXPFactory(SkBlendMode::kSrc);
 
         readRTC->fillRectToRect(GrNoClip(), std::move(paint1), GrAA::kNo, SkMatrix::I(), kDstRect,
@@ -215,17 +243,13 @@
             continue;
         }
 
-        paint2.addColorTextureProcessor(resourceProvider, readRTC->asTextureProxyRef(), nullptr,
-                                        SkMatrix::I());
         paint2.addColorFragmentProcessor(std::move(upmToPM));
         paint2.setPorterDuffXPFactory(SkBlendMode::kSrc);
 
         tempRTC->fillRectToRect(GrNoClip(), std::move(paint2), GrAA::kNo, SkMatrix::I(), kDstRect,
                                 kSrcRect);
 
-        paint3.addColorTextureProcessor(resourceProvider, tempRTC->asTextureProxyRef(), nullptr,
-                                        SkMatrix::I());
-        paint3.addColorFragmentProcessor(std::move(pmToUPM));
+        paint3.addColorFragmentProcessor(std::move(pmToUPM2));
         paint3.setPorterDuffXPFactory(SkBlendMode::kSrc);
 
         readRTC->fillRectToRect(GrNoClip(), std::move(paint3), GrAA::kNo, SkMatrix::I(), kDstRect,
@@ -251,12 +275,28 @@
     }
 }
 
-sk_sp<GrFragmentProcessor> GrConfigConversionEffect::Make(sk_sp<GrFragmentProcessor> fp,
-                                                          PMConversion pmConversion) {
-    if (!fp) {
+sk_sp<GrFragmentProcessor> GrConfigConversionEffect::Make(GrTexture* texture,
+                                                          PMConversion pmConversion,
+                                                          const SkMatrix& matrix) {
+    if (kRGBA_8888_GrPixelConfig != texture->config() &&
+        kBGRA_8888_GrPixelConfig != texture->config()) {
+        // The PM conversions assume colors are 0..255
         return nullptr;
     }
-    sk_sp<GrFragmentProcessor> ccFP(new GrConfigConversionEffect(pmConversion));
-    sk_sp<GrFragmentProcessor> fpPipeline[] = { fp, ccFP };
-    return GrFragmentProcessor::RunInSeries(fpPipeline, 2);
+    return sk_sp<GrFragmentProcessor>(
+        new GrConfigConversionEffect(texture, pmConversion, matrix));
+}
+
+sk_sp<GrFragmentProcessor> GrConfigConversionEffect::Make(GrResourceProvider* resourceProvider,
+                                                          sk_sp<GrTextureProxy> proxy,
+                                                          PMConversion pmConversion,
+                                                          const SkMatrix& matrix) {
+    if (kRGBA_8888_GrPixelConfig != proxy->config() &&
+        kBGRA_8888_GrPixelConfig != proxy->config()) {
+        // The PM conversions assume colors are 0..255
+        return nullptr;
+    }
+    return sk_sp<GrFragmentProcessor>(new GrConfigConversionEffect(resourceProvider,
+                                                                   std::move(proxy),
+                                                                   pmConversion, matrix));
 }