Refactor 'in uniform' CPP code generation

Easily supports mapping ctypes to sksl types with templates that
specify how to send data to the GPU and how to track state changes.
The template logic and type mappings are defined in
SkSLCPPUniformCTypes.* while SkSLCPPCodeGenerator is updated to
utilize it.

It also updates the supported ctypes to properly generate code for
SkPoint, SkIPoint, SkIRect, and GrColor4f. The code generated for
'in uniforms' now also correctly supports conditional uniforms.


Bug: skia:
Change-Id: Ib7c0a873bdd68a966b6a00871f33102dfa2c432d
Reviewed-on: https://skia-review.googlesource.com/150129
Commit-Queue: Michael Ludwig <michaelludwig@google.com>
Reviewed-by: Ethan Nicholas <ethannicholas@google.com>
diff --git a/src/gpu/effects/GrAARectEffect.h b/src/gpu/effects/GrAARectEffect.h
index efb7ecb..94974e2 100644
--- a/src/gpu/effects/GrAARectEffect.h
+++ b/src/gpu/effects/GrAARectEffect.h
@@ -15,8 +15,8 @@
 #include "GrCoordTransform.h"
 class GrAARectEffect : public GrFragmentProcessor {
 public:
-    GrClipEdgeType edgeType() const { return fEdgeType; }
-    SkRect rect() const { return fRect; }
+    const GrClipEdgeType& edgeType() const { return fEdgeType; }
+    const SkRect& rect() const { return fRect; }
     static std::unique_ptr<GrFragmentProcessor> Make(GrClipEdgeType edgeType, SkRect rect) {
         return std::unique_ptr<GrFragmentProcessor>(new GrAARectEffect(edgeType, rect));
     }
diff --git a/src/gpu/effects/GrAlphaThresholdFragmentProcessor.cpp b/src/gpu/effects/GrAlphaThresholdFragmentProcessor.cpp
index 45852ed..3a13123 100644
--- a/src/gpu/effects/GrAlphaThresholdFragmentProcessor.cpp
+++ b/src/gpu/effects/GrAlphaThresholdFragmentProcessor.cpp
@@ -66,8 +66,8 @@
         const GrAlphaThresholdFragmentProcessor& _outer =
                 _proc.cast<GrAlphaThresholdFragmentProcessor>();
         {
-            pdman.set1f(fInnerThresholdVar, _outer.innerThreshold());
-            pdman.set1f(fOuterThresholdVar, _outer.outerThreshold());
+            pdman.set1f(fInnerThresholdVar, (_outer.innerThreshold()));
+            pdman.set1f(fOuterThresholdVar, (_outer.outerThreshold()));
         }
     }
     UniformHandle fInnerThresholdVar;
diff --git a/src/gpu/effects/GrBlurredEdgeFragmentProcessor.h b/src/gpu/effects/GrBlurredEdgeFragmentProcessor.h
index 7bbdd32..71376d7 100644
--- a/src/gpu/effects/GrBlurredEdgeFragmentProcessor.h
+++ b/src/gpu/effects/GrBlurredEdgeFragmentProcessor.h
@@ -16,7 +16,7 @@
 class GrBlurredEdgeFragmentProcessor : public GrFragmentProcessor {
 public:
     enum class Mode { kGaussian = 0, kSmoothStep = 1 };
-    Mode mode() const { return fMode; }
+    const Mode& mode() const { return fMode; }
     static std::unique_ptr<GrFragmentProcessor> Make(Mode mode) {
         return std::unique_ptr<GrFragmentProcessor>(new GrBlurredEdgeFragmentProcessor(mode));
     }
diff --git a/src/gpu/effects/GrCircleBlurFragmentProcessor.h b/src/gpu/effects/GrCircleBlurFragmentProcessor.h
index 373ea42..7a53b01 100644
--- a/src/gpu/effects/GrCircleBlurFragmentProcessor.h
+++ b/src/gpu/effects/GrCircleBlurFragmentProcessor.h
@@ -15,7 +15,7 @@
 #include "GrCoordTransform.h"
 class GrCircleBlurFragmentProcessor : public GrFragmentProcessor {
 public:
-    SkRect circleRect() const { return fCircleRect; }
+    const SkRect& circleRect() const { return fCircleRect; }
     float textureRadius() const { return fTextureRadius; }
     float solidRadius() const { return fSolidRadius; }
 
diff --git a/src/gpu/effects/GrCircleEffect.h b/src/gpu/effects/GrCircleEffect.h
index cb28f60..686591a 100644
--- a/src/gpu/effects/GrCircleEffect.h
+++ b/src/gpu/effects/GrCircleEffect.h
@@ -15,8 +15,8 @@
 #include "GrCoordTransform.h"
 class GrCircleEffect : public GrFragmentProcessor {
 public:
-    GrClipEdgeType edgeType() const { return fEdgeType; }
-    SkPoint center() const { return fCenter; }
+    const GrClipEdgeType& edgeType() const { return fEdgeType; }
+    const SkPoint& center() const { return fCenter; }
     float radius() const { return fRadius; }
 
     static std::unique_ptr<GrFragmentProcessor> Make(GrClipEdgeType edgeType, SkPoint center,
diff --git a/src/gpu/effects/GrConfigConversionEffect.h b/src/gpu/effects/GrConfigConversionEffect.h
index ed362ae..4da5b0f 100644
--- a/src/gpu/effects/GrConfigConversionEffect.h
+++ b/src/gpu/effects/GrConfigConversionEffect.h
@@ -129,7 +129,7 @@
 
         return true;
     }
-    PMConversion pmConversion() const { return fPmConversion; }
+    const PMConversion& pmConversion() const { return fPmConversion; }
 
     static std::unique_ptr<GrFragmentProcessor> Make(std::unique_ptr<GrFragmentProcessor> fp,
                                                      PMConversion pmConversion) {
diff --git a/src/gpu/effects/GrConstColorProcessor.cpp b/src/gpu/effects/GrConstColorProcessor.cpp
index 43e9ffc..5b07eca 100644
--- a/src/gpu/effects/GrConstColorProcessor.cpp
+++ b/src/gpu/effects/GrConstColorProcessor.cpp
@@ -26,39 +26,33 @@
         (void)color;
         auto mode = _outer.mode();
         (void)mode;
-        fColorUniformVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
-                                                            kDefault_GrSLPrecision, "colorUniform");
+        fColorVar = args.fUniformHandler->addUniform(kFragment_GrShaderFlag, kHalf4_GrSLType,
+                                                     kDefault_GrSLPrecision, "color");
         fragBuilder->codeAppendf(
-                "half4 prevColor;\n@switch (%d) {\n    case 0:\n        %s = %s;\n        break;\n "
-                "   case 1:\n        %s = %s * %s;\n        break;\n    case 2:\n        %s = %s.w "
-                "* %s;\n        break;\n}\n",
+                "@switch (%d) {\n    case 0:\n        %s = %s;\n        break;\n    case 1:\n      "
+                "  %s = %s * %s;\n        break;\n    case 2:\n        %s = %s.w * %s;\n        "
+                "break;\n}\n",
                 (int)_outer.mode(), args.fOutputColor,
-                args.fUniformHandler->getUniformCStr(fColorUniformVar), args.fOutputColor,
-                args.fInputColor, args.fUniformHandler->getUniformCStr(fColorUniformVar),
+                args.fUniformHandler->getUniformCStr(fColorVar), args.fOutputColor,
+                args.fInputColor, args.fUniformHandler->getUniformCStr(fColorVar),
                 args.fOutputColor, args.fInputColor,
-                args.fUniformHandler->getUniformCStr(fColorUniformVar));
+                args.fUniformHandler->getUniformCStr(fColorVar));
     }
 
 private:
     void onSetData(const GrGLSLProgramDataManager& pdman,
                    const GrFragmentProcessor& _proc) override {
         const GrConstColorProcessor& _outer = _proc.cast<GrConstColorProcessor>();
-        auto color = _outer.color();
-        (void)color;
-        UniformHandle& colorUniform = fColorUniformVar;
-        (void)colorUniform;
-        auto mode = _outer.mode();
-        (void)mode;
-
-        // We use the "illegal" color value as an uninit sentinel. With GrColor4f, the "illegal"
-        // color is *really* illegal (not just unpremultiplied), so this check is simple.
-        if (prevColor != color) {
-            pdman.set4fv(colorUniform, 1, color.fRGBA);
-            prevColor = color;
+        {
+            const GrColor4f& colorValue = _outer.color();
+            if (fColorPrev != colorValue) {
+                fColorPrev = colorValue;
+                pdman.set4fv(fColorVar, 1, colorValue.fRGBA);
+            }
         }
     }
-    GrColor4f prevColor = GrColor4f::kIllegalConstructor;
-    UniformHandle fColorUniformVar;
+    GrColor4f fColorPrev = GrColor4f::kIllegalConstructor;
+    UniformHandle fColorVar;
 };
 GrGLSLFragmentProcessor* GrConstColorProcessor::onCreateGLSLInstance() const {
     return new GrGLSLConstColorProcessor();
diff --git a/src/gpu/effects/GrConstColorProcessor.fp b/src/gpu/effects/GrConstColorProcessor.fp
index dbad279..80e855b 100644
--- a/src/gpu/effects/GrConstColorProcessor.fp
+++ b/src/gpu/effects/GrConstColorProcessor.fp
@@ -13,9 +13,7 @@
     kLast = kModulateA
 };
 
-layout(ctype=GrColor4f) in half4 color;
-uniform half4 colorUniform;
-layout(ctype=GrColor4f) half4 prevColor;
+layout(ctype=GrColor4f, tracked) in uniform half4 color;
 layout(key) in InputMode mode;
 
 @optimizationFlags {
@@ -25,26 +23,17 @@
 void main() {
     @switch (mode) {
         case InputMode::kIgnore:
-            sk_OutColor = colorUniform;
+            sk_OutColor = color;
             break;
         case InputMode::kModulateRGBA:
-            sk_OutColor = sk_InColor * colorUniform;
+            sk_OutColor = sk_InColor * color;
             break;
         case InputMode::kModulateA:
-            sk_OutColor = sk_InColor.a * colorUniform;
+            sk_OutColor = sk_InColor.a * color;
             break;
     }
 }
 
-@setData(pdman) {
-    // We use the "illegal" color value as an uninit sentinel. With GrColor4f, the "illegal"
-    // color is *really* illegal (not just unpremultiplied), so this check is simple.
-    if (prevColor != color) {
-        pdman.set4fv(colorUniform, 1, color.fRGBA);
-        prevColor = color;
-    }
-}
-
 @class {
     static const int kInputModeCnt = (int) InputMode::kLast + 1;
 
@@ -95,4 +84,4 @@
     }
     InputMode mode = static_cast<InputMode>(d->fRandom->nextULessThan(kInputModeCnt));
     return GrConstColorProcessor::Make(color, mode);
-}
\ No newline at end of file
+}
diff --git a/src/gpu/effects/GrConstColorProcessor.h b/src/gpu/effects/GrConstColorProcessor.h
index e36a38f..8e94900 100644
--- a/src/gpu/effects/GrConstColorProcessor.h
+++ b/src/gpu/effects/GrConstColorProcessor.h
@@ -42,8 +42,8 @@
         SK_ABORT("Unexpected mode");
         return GrColor4f::TransparentBlack();
     }
-    GrColor4f color() const { return fColor; }
-    InputMode mode() const { return fMode; }
+    const GrColor4f& color() const { return fColor; }
+    const InputMode& mode() const { return fMode; }
     static std::unique_ptr<GrFragmentProcessor> Make(GrColor4f color, InputMode mode) {
         return std::unique_ptr<GrFragmentProcessor>(new GrConstColorProcessor(color, mode));
     }
diff --git a/src/gpu/effects/GrEllipseEffect.h b/src/gpu/effects/GrEllipseEffect.h
index 62625fc..ca1574d 100644
--- a/src/gpu/effects/GrEllipseEffect.h
+++ b/src/gpu/effects/GrEllipseEffect.h
@@ -17,9 +17,9 @@
 #include "GrCoordTransform.h"
 class GrEllipseEffect : public GrFragmentProcessor {
 public:
-    GrClipEdgeType edgeType() const { return fEdgeType; }
-    SkPoint center() const { return fCenter; }
-    SkPoint radii() const { return fRadii; }
+    const GrClipEdgeType& edgeType() const { return fEdgeType; }
+    const SkPoint& center() const { return fCenter; }
+    const SkPoint& radii() const { return fRadii; }
 
     static std::unique_ptr<GrFragmentProcessor> Make(GrClipEdgeType edgeType, SkPoint center,
                                                      SkPoint radii, const GrShaderCaps& caps) {
diff --git a/src/gpu/effects/GrMagnifierEffect.cpp b/src/gpu/effects/GrMagnifierEffect.cpp
index ae53b33..b709c48 100644
--- a/src/gpu/effects/GrMagnifierEffect.cpp
+++ b/src/gpu/effects/GrMagnifierEffect.cpp
@@ -77,10 +77,10 @@
                    const GrFragmentProcessor& _proc) override {
         const GrMagnifierEffect& _outer = _proc.cast<GrMagnifierEffect>();
         {
-            pdman.set1f(fXInvZoomVar, _outer.xInvZoom());
-            pdman.set1f(fYInvZoomVar, _outer.yInvZoom());
-            pdman.set1f(fXInvInsetVar, _outer.xInvInset());
-            pdman.set1f(fYInvInsetVar, _outer.yInvInset());
+            pdman.set1f(fXInvZoomVar, (_outer.xInvZoom()));
+            pdman.set1f(fYInvZoomVar, (_outer.yInvZoom()));
+            pdman.set1f(fXInvInsetVar, (_outer.xInvInset()));
+            pdman.set1f(fYInvInsetVar, (_outer.yInvInset()));
         }
         GrSurfaceProxy& srcProxy = *_outer.textureSampler(0).proxy();
         GrTexture& src = *srcProxy.peekTexture();
diff --git a/src/gpu/effects/GrMagnifierEffect.h b/src/gpu/effects/GrMagnifierEffect.h
index 582e0ef..c0c44d7 100644
--- a/src/gpu/effects/GrMagnifierEffect.h
+++ b/src/gpu/effects/GrMagnifierEffect.h
@@ -15,8 +15,8 @@
 #include "GrCoordTransform.h"
 class GrMagnifierEffect : public GrFragmentProcessor {
 public:
-    SkIRect bounds() const { return fBounds; }
-    SkRect srcRect() const { return fSrcRect; }
+    const SkIRect& bounds() const { return fBounds; }
+    const SkRect& srcRect() const { return fSrcRect; }
     float xInvZoom() const { return fXInvZoom; }
     float yInvZoom() const { return fYInvZoom; }
     float xInvInset() const { return fXInvInset; }
diff --git a/src/gpu/effects/GrRRectBlurEffect.cpp b/src/gpu/effects/GrRRectBlurEffect.cpp
index ac17b17..fa85ea5 100644
--- a/src/gpu/effects/GrRRectBlurEffect.cpp
+++ b/src/gpu/effects/GrRRectBlurEffect.cpp
@@ -100,7 +100,7 @@
     void onSetData(const GrGLSLProgramDataManager& pdman,
                    const GrFragmentProcessor& _proc) override {
         const GrRRectBlurEffect& _outer = _proc.cast<GrRRectBlurEffect>();
-        { pdman.set1f(fCornerRadiusVar, _outer.cornerRadius()); }
+        { pdman.set1f(fCornerRadiusVar, (_outer.cornerRadius())); }
         auto sigma = _outer.sigma();
         (void)sigma;
         auto rect = _outer.rect();
diff --git a/src/gpu/effects/GrRRectBlurEffect.h b/src/gpu/effects/GrRRectBlurEffect.h
index 3e386a6..3093b31 100644
--- a/src/gpu/effects/GrRRectBlurEffect.h
+++ b/src/gpu/effects/GrRRectBlurEffect.h
@@ -96,7 +96,7 @@
         return mask;
     }
     float sigma() const { return fSigma; }
-    SkRect rect() const { return fRect; }
+    const SkRect& rect() const { return fRect; }
     float cornerRadius() const { return fCornerRadius; }
 
     static std::unique_ptr<GrFragmentProcessor> Make(GrContext* context, float sigma,
diff --git a/src/gpu/effects/GrRectBlurEffect.cpp b/src/gpu/effects/GrRectBlurEffect.cpp
index 4c4ca86..aa74f61 100644
--- a/src/gpu/effects/GrRectBlurEffect.cpp
+++ b/src/gpu/effects/GrRectBlurEffect.cpp
@@ -104,10 +104,7 @@
     void onSetData(const GrGLSLProgramDataManager& pdman,
                    const GrFragmentProcessor& _proc) override {
         const GrRectBlurEffect& _outer = _proc.cast<GrRectBlurEffect>();
-        {
-            const SkRect rectValue = _outer.rect();
-            pdman.set4fv(fRectVar, 1, (float*)&rectValue);
-        }
+        { pdman.set4fv(fRectVar, 1, reinterpret_cast<const float*>(&(_outer.rect()))); }
         UniformHandle& rect = fRectVar;
         (void)rect;
         auto sigma = _outer.sigma();
diff --git a/src/gpu/effects/GrRectBlurEffect.h b/src/gpu/effects/GrRectBlurEffect.h
index 0d443c3..bad6be1 100644
--- a/src/gpu/effects/GrRectBlurEffect.h
+++ b/src/gpu/effects/GrRectBlurEffect.h
@@ -60,7 +60,7 @@
 
         return blurProfile;
     }
-    SkRect rect() const { return fRect; }
+    const SkRect& rect() const { return fRect; }
     float sigma() const { return fSigma; }
 
     static std::unique_ptr<GrFragmentProcessor> Make(GrProxyProvider* proxyProvider,
diff --git a/src/gpu/effects/GrSimpleTextureEffect.h b/src/gpu/effects/GrSimpleTextureEffect.h
index 8a8924b..fb4e373 100644
--- a/src/gpu/effects/GrSimpleTextureEffect.h
+++ b/src/gpu/effects/GrSimpleTextureEffect.h
@@ -15,7 +15,7 @@
 #include "GrCoordTransform.h"
 class GrSimpleTextureEffect : public GrFragmentProcessor {
 public:
-    SkMatrix44 matrix() const { return fMatrix; }
+    const SkMatrix44& matrix() const { return fMatrix; }
 
     static std::unique_ptr<GrFragmentProcessor> Make(sk_sp<GrTextureProxy> proxy,
                                                      const SkMatrix& matrix) {
diff --git a/src/gpu/effects/GrYUVtoRGBEffect.cpp b/src/gpu/effects/GrYUVtoRGBEffect.cpp
index 33dff41..c1b77b4 100644
--- a/src/gpu/effects/GrYUVtoRGBEffect.cpp
+++ b/src/gpu/effects/GrYUVtoRGBEffect.cpp
@@ -130,11 +130,7 @@
     void onSetData(const GrGLSLProgramDataManager& pdman,
                    const GrFragmentProcessor& _proc) override {
         const GrYUVtoRGBEffect& _outer = _proc.cast<GrYUVtoRGBEffect>();
-        {
-            float colorSpaceMatrixValue[16];
-            _outer.colorSpaceMatrix().asColMajorf(colorSpaceMatrixValue);
-            pdman.setMatrix4f(fColorSpaceMatrixVar, colorSpaceMatrixValue);
-        }
+        { pdman.setSkMatrix44(fColorSpaceMatrixVar, (_outer.colorSpaceMatrix())); }
     }
     UniformHandle fColorSpaceMatrixVar;
 };
diff --git a/src/gpu/effects/GrYUVtoRGBEffect.h b/src/gpu/effects/GrYUVtoRGBEffect.h
index 0b3393c..efc1944 100644
--- a/src/gpu/effects/GrYUVtoRGBEffect.h
+++ b/src/gpu/effects/GrYUVtoRGBEffect.h
@@ -20,11 +20,11 @@
                                                      sk_sp<GrTextureProxy> vProxy,
                                                      SkYUVColorSpace colorSpace, bool nv12);
     SkString dumpInfo() const override;
-    SkMatrix44 ySamplerTransform() const { return fYSamplerTransform; }
-    SkMatrix44 uSamplerTransform() const { return fUSamplerTransform; }
-    SkMatrix44 vSamplerTransform() const { return fVSamplerTransform; }
-    SkMatrix44 colorSpaceMatrix() const { return fColorSpaceMatrix; }
-    bool nv12() const { return fNv12; }
+    const SkMatrix44& ySamplerTransform() const { return fYSamplerTransform; }
+    const SkMatrix44& uSamplerTransform() const { return fUSamplerTransform; }
+    const SkMatrix44& vSamplerTransform() const { return fVSamplerTransform; }
+    const SkMatrix44& colorSpaceMatrix() const { return fColorSpaceMatrix; }
+    const bool& nv12() const { return fNv12; }
     static std::unique_ptr<GrFragmentProcessor> Make(
             sk_sp<GrTextureProxy> ySampler, SkMatrix44 ySamplerTransform,
             sk_sp<GrTextureProxy> uSampler, SkMatrix44 uSamplerTransform,