Implement SkColorFilter as a GrGLEffect

Adds GrEffect::willUseInputColor() which indicates whether or not the
input color affects the output of the effect. This is needed for
certain Xfermodes, such as kSrc_Mode. For these modes the color filter
will not use the input color.

An effect with GrEffect::willUseInputColor() true will cause all color
or coverage effects before it to be discarded, as their computations
cannot affect the output. In these cases program is marked as having
white input color.

This fixes an assert when Skia is compiled in a mode that prefers
using uniforms instead of attributes for constants. (Flags
GR_GL_USE_NV_PATH_RENDERING or GR_GL_NO_CONSTANT_ATTRIBUTES). Using
attributes hides the problem where the fragment shader does not need
input color for color filters that ignore DST part of the filter. The
assert would be hit when uniform manager tries to bind an uniform which
has been optimized away by the shader compiler.

Adds specific GrGLSLExpr4 and GrGLSLExpr1 classes. This way the GLSL
expressions like "(v - src.a)" can remain somewhat readable in form of
"(v - src.a())". The GrGLSLExpr<typename> template implements the
generic functionality, GrGLSLExprX is the specialization that exposes
the type-safe interface to this functionality.

Also adds operators so that GLSL binary operators of the form
"(float * vecX)" can be expressed in C++. Before only the equivalent
"(vecX * float)" was possible. This reverts the common blending
calculations to more conventional order, such as "(1-a) * c" instead of
"c * (1-a)".

Changes GrGLSLExpr1::OnesStr from 1 to 1.0 in order to preserve the
color filter blending formula string the same (with the exception of
variable name change).

Shaders change in case of input color being needed:
 -   vec4 filteredColor;
 -   filteredColor = (((1.0 - uFilterColor.a) * output_Stage0) + uFilterColor);
 -   fsColorOut = filteredColor;
 +   vec4 output_Stage1;
 +   { // Stage 1: ModeColorFilterEffect
 +   output_Stage1 = (((1.0 - uFilterColor_Stage1.a) * output_Stage0) + uFilterColor_Stage1);
 +   }
 +   fsColorOut = output_Stage1;

Shaders change in case of input color being not needed:
 -uniform vec4 uFilterColor;
 -in vec4 vColor;
 +uniform vec4 uFilterColor_Stage0;
  out vec4 fsColorOut;
  void main() {
 -   vec4 filteredColor;
 -   filteredColor = uFilterColor;
 -   fsColorOut = filteredColor;
 +   vec4 output_Stage0;
 +   { // Stage 0: ModeColorFilterEffect
 +   output_Stage0 = uFilterColor_Stage0;
 +   }
 +   fsColorOut = output_Stage0;
  }

R=bsalomon@google.com, robertphillips@google.com, jvanverth@google.com

Author: kkinnunen@nvidia.com

Review URL: https://codereview.chromium.org/25023003

git-svn-id: http://skia.googlecode.com/svn/trunk@11912 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/gl/GrGLProgramDesc.cpp b/src/gpu/gl/GrGLProgramDesc.cpp
index f9728ff..d958742 100644
--- a/src/gpu/gl/GrGLProgramDesc.cpp
+++ b/src/gpu/gl/GrGLProgramDesc.cpp
@@ -56,6 +56,27 @@
 
     bool skipColor = SkToBool(blendOpts & (GrDrawState::kEmitTransBlack_BlendOptFlag |
                                            GrDrawState::kEmitCoverage_BlendOptFlag));
+    int firstEffectiveColorStage = 0;
+    bool inputColorIsUsed = true;
+    if (!skipColor) {
+        firstEffectiveColorStage = drawState.numColorStages();
+        while (firstEffectiveColorStage > 0 && inputColorIsUsed) {
+            --firstEffectiveColorStage;
+            const GrEffect* effect = drawState.getColorStage(firstEffectiveColorStage).getEffect()->get();
+            inputColorIsUsed = effect->willUseInputColor();
+        }
+    }
+
+    int firstEffectiveCoverageStage = 0;
+    bool inputCoverageIsUsed = true;
+    if (!skipCoverage) {
+        firstEffectiveCoverageStage = drawState.numCoverageStages();
+        while (firstEffectiveCoverageStage > 0 && inputCoverageIsUsed) {
+            --firstEffectiveCoverageStage;
+            const GrEffect* effect = drawState.getCoverageStage(firstEffectiveCoverageStage).getEffect()->get();
+            inputCoverageIsUsed = effect->willUseInputColor();
+        }
+    }
 
     // The descriptor is used as a cache key. Thus when a field of the
     // descriptor will not affect program generation (because of the attribute
@@ -70,10 +91,11 @@
 
     bool colorIsTransBlack = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag);
     bool colorIsSolidWhite = (blendOpts & GrDrawState::kEmitCoverage_BlendOptFlag) ||
-                             (!requiresColorAttrib && 0xffffffff == drawState.getColor());
+                             (!requiresColorAttrib && 0xffffffff == drawState.getColor()) ||
+                             (!inputColorIsUsed);
 
-    int numEffects = (skipColor ? 0 : drawState.numColorStages()) +
-                     (skipCoverage ? 0 : drawState.numCoverageStages());
+    int numEffects = (skipColor ? 0 : (drawState.numColorStages() - firstEffectiveColorStage)) +
+                     (skipCoverage ? 0 : (drawState.numCoverageStages() - firstEffectiveCoverageStage));
 
     size_t newKeyLength = KeyLength(numEffects);
     bool allocChanged;
@@ -93,7 +115,7 @@
     bool readFragPosition = false;
     bool hasVertexCode = false;
     if (!skipColor) {
-        for (int s = 0; s < drawState.numColorStages(); ++s) {
+        for (int s = firstEffectiveColorStage; s < drawState.numColorStages(); ++s) {
             effectKeys[currEffectKey++] =
                 get_key_and_update_stats(drawState.getColorStage(s), gpu->glCaps(),
                                          requiresLocalCoordAttrib, &readsDst, &readFragPosition,
@@ -101,7 +123,7 @@
         }
     }
     if (!skipCoverage) {
-        for (int s = 0; s < drawState.numCoverageStages(); ++s) {
+        for (int s = firstEffectiveCoverageStage; s < drawState.numCoverageStages(); ++s) {
             effectKeys[currEffectKey++] =
                 get_key_and_update_stats(drawState.getCoverageStage(s), gpu->glCaps(),
                                          requiresLocalCoordAttrib, &readsDst, &readFragPosition,
@@ -111,7 +133,6 @@
 
     header->fHasVertexCode = hasVertexCode || requiresLocalCoordAttrib;
     header->fEmitsPointSize = isPoints;
-    header->fColorFilterXfermode = skipColor ? SkXfermode::kDst_Mode : drawState.getColorFilterMode();
 
     // Currently the experimental GS will only work with triangle prims (and it doesn't do anything
     // other than pass through values from the VS to the FS anyway).
@@ -139,7 +160,7 @@
 
     if (skipCoverage) {
         header->fCoverageInput = kTransBlack_ColorInput;
-    } else if (covIsSolidWhite) {
+    } else if (covIsSolidWhite || !inputCoverageIsUsed) {
         header->fCoverageInput = kSolidWhite_ColorInput;
     } else if (defaultToUniformInputs && !requiresCoverageAttrib) {
         header->fCoverageInput = kUniform_ColorInput;
@@ -202,10 +223,6 @@
     bool separateCoverageFromColor = false;
     if (!drawState.isCoverageDrawing() && !skipCoverage &&
         (drawState.numCoverageStages() > 0 || requiresCoverageAttrib)) {
-        // color filter is applied between color/coverage computation
-        if (SkXfermode::kDst_Mode != header->fColorFilterXfermode) {
-            separateCoverageFromColor = true;
-        }
 
         // If we're stenciling then we want to discard samples that have zero coverage
         if (drawState.getStencil().doesWrite()) {
@@ -237,24 +254,23 @@
         }
     }
     if (!skipColor) {
-        for (int s = 0; s < drawState.numColorStages(); ++s) {
+        for (int s = firstEffectiveColorStage; s < drawState.numColorStages(); ++s) {
             colorStages->push_back(&drawState.getColorStage(s));
         }
-        header->fColorEffectCnt = drawState.numColorStages();
     }
     if (!skipCoverage) {
         SkTArray<const GrEffectStage*, true>* array;
         if (separateCoverageFromColor) {
             array = coverageStages;
-            header->fCoverageEffectCnt = drawState.numCoverageStages();
         } else {
             array = colorStages;
-            header->fColorEffectCnt += drawState.numCoverageStages();
         }
-        for (int s = 0; s < drawState.numCoverageStages(); ++s) {
+        for (int s = firstEffectiveCoverageStage; s < drawState.numCoverageStages(); ++s) {
             array->push_back(&drawState.getCoverageStage(s));
         }
     }
+    header->fColorEffectCnt = colorStages->count();
+    header->fCoverageEffectCnt = coverageStages->count();
 
     *desc->checksum() = 0;
     *desc->checksum() = SkChecksum::Compute(reinterpret_cast<uint32_t*>(desc->fKey.get()),