Remove gpu shader optimatization for solid white or trans black colors

Running test on the added bench which draws a grid of all white paths, all blue paths, or alternating checkered white/blue paths.

With optimization in (ms):
         White       Blue        Checkered
Linux    ~80         ~80         ~160
N7       ~800        ~1100       ~1500
Moto-e   ~830        ~1100       ~2500

Without optimization in (ms):
         White       Blue        Checkered
Linux    ~80         ~80         ~80
N7       ~1100       ~1100       ~1100
Moto-e   ~1100       ~1100       ~1500

BUG=skia:

Committed: https://skia.googlesource.com/skia/+/5f78d2251a440443c9eaa321dad058d7a32bfef7

R=bsalomon@google.com

Author: egdaniel@google.com

Review URL: https://codereview.chromium.org/375823005
diff --git a/bench/AlternatingColorPatternBench.cpp b/bench/AlternatingColorPatternBench.cpp
new file mode 100644
index 0000000..b497979
--- /dev/null
+++ b/bench/AlternatingColorPatternBench.cpp
@@ -0,0 +1,200 @@
+/*
+ * Copyright 2014 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "Benchmark.h"
+#include "SkCanvas.h"
+#include "SkGradientShader.h"
+#include "SkPaint.h"
+#include "SkString.h"
+
+enum ColorPattern {
+    kWhite_ColorPattern,
+    kBlue_ColorPattern,
+    kOpaqueBitmap_ColorPattern,
+    kAlphaBitmap_ColorPattern,
+};
+
+static const struct ColorPatternData{
+    SkColor         fColor;
+    bool            fIsBitmap;
+    const char*     fName;
+} gColorPatterns[] = {
+    // Keep this in same order as ColorPattern enum
+    { SK_ColorWHITE, false,  "white"        }, // kWhite_ColorPattern
+    { SK_ColorBLUE,  false,  "blue"         }, // kBlue_ColorPattern
+    { SK_ColorWHITE, true,   "obaqueBitMap" }, // kOpaqueBitmap_ColorPattern
+    { 0x10000000,    true,   "alphaBitmap"  }, // kAlphaBitmap_ColorPattern
+};
+
+enum DrawType {
+    kRect_DrawType,
+    kPath_DrawType,
+};
+
+static void makebm(SkBitmap* bm, int w, int h) {
+    bm->allocN32Pixels(w, h);
+    bm->eraseColor(SK_ColorTRANSPARENT);
+
+    SkCanvas    canvas(*bm);
+    SkScalar    s = SkIntToScalar(SkMin32(w, h));
+    static const SkPoint     kPts0[] = { { 0, 0 }, { s, s } };
+    static const SkPoint     kPts1[] = { { s/2, 0 }, { s/2, s } };
+    static const SkScalar    kPos[] = { 0, SK_Scalar1/2, SK_Scalar1 };
+    static const SkColor kColors0[] = {0x80F00080, 0xF0F08000, 0x800080F0 };
+    static const SkColor kColors1[] = {0xF08000F0, 0x8080F000, 0xF000F080 };
+
+
+    SkPaint     paint;
+
+    paint.setShader(SkGradientShader::CreateLinear(kPts0, kColors0, kPos,
+                    SK_ARRAY_COUNT(kColors0), SkShader::kClamp_TileMode))->unref();
+    canvas.drawPaint(paint);
+    paint.setShader(SkGradientShader::CreateLinear(kPts1, kColors1, kPos,
+                    SK_ARRAY_COUNT(kColors1), SkShader::kClamp_TileMode))->unref();
+    canvas.drawPaint(paint);
+}
+
+/**
+ * This bench draws a grid of either rects or filled paths, with two alternating color patterns.
+ * This color patterns are passed in as enums to the class. The options are:
+ *   1) solid white color
+ *   2) solid blue color
+ *   3) opaque bitmap
+ *   4) partial alpha bitmap
+ * The same color pattern can be set for both arguments to create a uniform pattern on all draws.
+ *
+ * The bench is used to test a few things. First it can test any optimizations made for a specific
+ * color pattern (for example drawing an opaque bitmap versus one with partial alpha). Also it can
+ * be used to test the cost of program switching and/or batching when alternating between different
+ * patterns when on the gpu.
+ */
+class AlternatingColorPatternBench : public Benchmark {
+public:
+    enum {
+        NX = 5,
+        NY = 5,
+        NUM_DRAWS = NX * NY,
+    };
+    SkShader* fBmShader;
+
+    SkPath  fPaths[NUM_DRAWS];
+    SkRect  fRects[NUM_DRAWS];
+    SkColor fColors[NUM_DRAWS];
+    SkShader* fShaders[NUM_DRAWS];
+
+    SkString        fName;
+    ColorPatternData    fPattern1;
+    ColorPatternData    fPattern2;
+    DrawType fDrawType;
+    SkBitmap fBmp;
+
+
+    AlternatingColorPatternBench(ColorPattern pattern1, ColorPattern pattern2, DrawType drawType)
+        : fBmShader(NULL) {
+        fPattern1 = gColorPatterns[pattern1];
+        fPattern2 = gColorPatterns[pattern2];
+        fName.printf("colorPattern_%s_%s_%s",
+                     fPattern1.fName, fPattern2.fName,
+                     kRect_DrawType == drawType ? "rect" : "path");
+        fDrawType = drawType;
+    }
+
+    virtual ~AlternatingColorPatternBench() {
+        SkSafeUnref(fBmShader);
+    }
+
+protected:
+    virtual const char* onGetName() SK_OVERRIDE {
+        return fName.c_str();
+    }
+
+    virtual void onPreDraw() {
+        int w = 40;
+        int h = 40;
+        makebm(&fBmp, w, h);
+        fBmShader = SkShader::CreateBitmapShader(fBmp,
+                                                 SkShader::kRepeat_TileMode,
+                                                 SkShader::kRepeat_TileMode);
+        int offset = 2;
+        int count = 0;
+        for (int j = 0; j < NY; ++j) {
+            for (int i = 0; i < NX; ++i) {
+                int x = (w + offset) * i;
+                int y = (h * offset) * j;
+                if (kRect_DrawType == fDrawType) {
+                    fRects[count].set(SkIntToScalar(x), SkIntToScalar(y),
+                                      SkIntToScalar(x + w), SkIntToScalar(y + h));
+                } else {
+                    fPaths[count].moveTo(SkIntToScalar(x), SkIntToScalar(y));
+                    fPaths[count].rLineTo(SkIntToScalar(w), 0);
+                    fPaths[count].rLineTo(0, SkIntToScalar(h));
+                    fPaths[count].rLineTo(SkIntToScalar(-w + 1), 0);
+                }
+                if (0 == count % 2) {
+                    fColors[count]  = fPattern1.fColor;
+                    fShaders[count] = fPattern1.fIsBitmap ? fBmShader : NULL;
+                } else {
+                    fColors[count]  = fPattern2.fColor;
+                    fShaders[count] = fPattern2.fIsBitmap ? fBmShader : NULL;
+                }
+                ++count;
+            }
+        }
+    }
+
+    virtual void onDraw(const int loops, SkCanvas* canvas) SK_OVERRIDE {
+        SkPaint paint;
+        paint.setAntiAlias(false);
+        paint.setFilterLevel(SkPaint::kLow_FilterLevel);
+
+        for (int i = 0; i < loops; ++i) {
+            for (int j = 0; j < NUM_DRAWS; ++j) {
+                paint.setColor(fColors[j]);
+                paint.setShader(fShaders[j]);
+                if (kRect_DrawType == fDrawType) {
+                    canvas->drawRect(fRects[j], paint);
+                } else {
+                    canvas->drawPath(fPaths[j], paint);
+                }
+            }
+        }
+    }
+
+private:
+    typedef Benchmark INHERITED;
+};
+
+DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench,
+                             (kWhite_ColorPattern, kWhite_ColorPattern,
+                              kPath_DrawType)); )
+DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench,
+                             (kBlue_ColorPattern, kBlue_ColorPattern,
+                              kPath_DrawType)); )
+DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench,
+                             (kWhite_ColorPattern, kBlue_ColorPattern,
+                              kPath_DrawType)); )
+
+DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench,
+                             (kOpaqueBitmap_ColorPattern, kOpaqueBitmap_ColorPattern,
+                              kPath_DrawType)); )
+DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench,
+                             (kAlphaBitmap_ColorPattern, kAlphaBitmap_ColorPattern,
+                              kPath_DrawType)); )
+DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench,
+                             (kOpaqueBitmap_ColorPattern, kAlphaBitmap_ColorPattern,
+                              kPath_DrawType)); )
+
+DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench,
+                             (kOpaqueBitmap_ColorPattern, kOpaqueBitmap_ColorPattern,
+                              kRect_DrawType)); )
+DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench,
+                             (kAlphaBitmap_ColorPattern, kAlphaBitmap_ColorPattern,
+                              kRect_DrawType)); )
+DEF_BENCH( return SkNEW_ARGS(AlternatingColorPatternBench,
+                             (kOpaqueBitmap_ColorPattern, kAlphaBitmap_ColorPattern,
+                              kRect_DrawType)); )
+
diff --git a/expectations/gm/ignored-tests.txt b/expectations/gm/ignored-tests.txt
index c97a0b9..d8c0476 100644
--- a/expectations/gm/ignored-tests.txt
+++ b/expectations/gm/ignored-tests.txt
@@ -33,6 +33,18 @@
 ## epoger will rebaseline by 25 Dec 2013
 #gradtext
 
+# egdaniel: removing a gpu shader optimization causes some single pixel changes in gm
+# cl: https://codereview.chromium.org/375823005/#ps210001
+coloremoji
+complexclip2
+complexclip2_path_bw
+complexclip2_rrect_bw
+gradients_view_perspective
+imagefiltersbase
+inverse_paths
+xfermodes3
+verylargebitmap
+
 # reed: bitmapfilters changed (labels) with hide_config CL, just need rebaselines
 bitmapfilters
 
diff --git a/gyp/bench.gypi b/gyp/bench.gypi
index c8d2061..b8406e2 100644
--- a/gyp/bench.gypi
+++ b/gyp/bench.gypi
@@ -23,6 +23,7 @@
     '../bench/Benchmark.h',
 
     '../bench/AAClipBench.cpp',
+    '../bench/AlternatingColorPatternBench.cpp',
     '../bench/BitmapBench.cpp',
     '../bench/BitmapRectBench.cpp',
     '../bench/BitmapScaleBench.cpp',
diff --git a/src/gpu/GrDrawState.cpp b/src/gpu/GrDrawState.cpp
index 14ba6fa..02c7920 100644
--- a/src/gpu/GrDrawState.cpp
+++ b/src/gpu/GrDrawState.cpp
@@ -409,6 +409,15 @@
     return kNone_BlendOpt;
 }
 
+bool GrDrawState::canIgnoreColorAttribute() const {
+    if (fBlendOptFlags & kInvalid_BlendOptFlag) {
+        this->getBlendOpts();
+    }
+    return SkToBool(fBlendOptFlags & (GrDrawState::kEmitTransBlack_BlendOptFlag |
+                                      GrDrawState::kEmitCoverage_BlendOptFlag));
+}
+
+
 ////////////////////////////////////////////////////////////////////////////////
 
 void GrDrawState::AutoViewMatrixRestore::restore() {
diff --git a/src/gpu/GrDrawState.h b/src/gpu/GrDrawState.h
index cef21af..c1dd9dc 100644
--- a/src/gpu/GrDrawState.h
+++ b/src/gpu/GrDrawState.h
@@ -551,6 +551,12 @@
     }
 
     /**
+     * We don't use suplied vertex color attributes if our blend mode is EmitCoverage or
+     * EmitTransBlack
+     */
+    bool canIgnoreColorAttribute() const;
+
+    /**
      * Determines what optimizations can be applied based on the blend. The coefficients may have
      * to be tweaked in order for the optimization to work. srcCoeff and dstCoeff are optional
      * params that receive the tweaked coefficients. Normally the function looks at the current
diff --git a/src/gpu/gl/GrGLProgram.cpp b/src/gpu/gl/GrGLProgram.cpp
index 435d0cd..5bc7d0b 100644
--- a/src/gpu/gl/GrGLProgram.cpp
+++ b/src/gpu/gl/GrGLProgram.cpp
@@ -154,7 +154,7 @@
                            GrColor color,
                            SharedGLState* sharedState) {
     const GrGLProgramDesc::KeyHeader& header = fDesc.getHeader();
-    if (!drawState.hasColorVertexAttribute()) {
+    if (!drawState.hasColorVertexAttribute() || drawState.canIgnoreColorAttribute()) {
         switch (header.fColorInput) {
             case GrGLProgramDesc::kAttribute_ColorInput:
                 SkASSERT(-1 != header.fColorAttributeIndex);
@@ -178,12 +178,8 @@
                 }
                 sharedState->fConstAttribColorIndex = -1;
                 break;
-            case GrGLProgramDesc::kSolidWhite_ColorInput:
-            case GrGLProgramDesc::kTransBlack_ColorInput:
-                sharedState->fConstAttribColorIndex = -1;
-                break;
             default:
-                SkFAIL("Unknown color type.");
+                SkFAIL("Unexpected color type.");
         }
     } else {
         sharedState->fConstAttribColorIndex = -1;
@@ -218,11 +214,10 @@
                 sharedState->fConstAttribCoverageIndex = -1;
                 break;
             case GrGLProgramDesc::kSolidWhite_ColorInput:
-            case GrGLProgramDesc::kTransBlack_ColorInput:
                 sharedState->fConstAttribCoverageIndex = -1;
                 break;
             default:
-                SkFAIL("Unknown coverage type.");
+                SkFAIL("Unexpected coverage type.");
         }
     } else {
         sharedState->fConstAttribCoverageIndex = -1;
diff --git a/src/gpu/gl/GrGLProgramDesc.cpp b/src/gpu/gl/GrGLProgramDesc.cpp
index 27a1fab..e1a3191 100644
--- a/src/gpu/gl/GrGLProgramDesc.cpp
+++ b/src/gpu/gl/GrGLProgramDesc.cpp
@@ -66,6 +66,7 @@
 
     bool skipColor = SkToBool(blendOpts & (GrDrawState::kEmitTransBlack_BlendOptFlag |
                                            GrDrawState::kEmitCoverage_BlendOptFlag));
+
     int firstEffectiveColorStage = 0;
     bool inputColorIsUsed = true;
     if (!skipColor) {
@@ -99,11 +100,6 @@
     bool requiresLocalCoordAttrib = !(skipCoverage  && skipColor) &&
                                     drawState.hasLocalCoordAttribute();
 
-    bool colorIsTransBlack = SkToBool(blendOpts & GrDrawState::kEmitTransBlack_BlendOptFlag);
-    bool colorIsSolidWhite = (blendOpts & GrDrawState::kEmitCoverage_BlendOptFlag) ||
-                             (!requiresColorAttrib && 0xffffffff == drawState.getColor()) ||
-                             (!inputColorIsUsed);
-
     bool readsDst = false;
     bool readFragPosition = false;
     // We use vertexshader-less shader programs only when drawing paths.
@@ -192,11 +188,7 @@
 #endif
     bool defaultToUniformInputs = GR_GL_NO_CONSTANT_ATTRIBUTES || gpu->caps()->pathRenderingSupport();
 
-    if (colorIsTransBlack) {
-        header->fColorInput = kTransBlack_ColorInput;
-    } else if (colorIsSolidWhite) {
-        header->fColorInput = kSolidWhite_ColorInput;
-    } else if (defaultToUniformInputs && !requiresColorAttrib) {
+    if (defaultToUniformInputs && !requiresColorAttrib && inputColorIsUsed) {
         header->fColorInput = kUniform_ColorInput;
     } else {
         header->fColorInput = kAttribute_ColorInput;
@@ -205,11 +197,9 @@
 
     bool covIsSolidWhite = !requiresCoverageAttrib && 0xffffffff == drawState.getCoverageColor();
 
-    if (skipCoverage) {
-        header->fCoverageInput = kTransBlack_ColorInput;
-    } else if (covIsSolidWhite || !inputCoverageIsUsed) {
+    if ((covIsSolidWhite || !inputCoverageIsUsed) && !skipCoverage) {
         header->fCoverageInput = kSolidWhite_ColorInput;
-    } else if (defaultToUniformInputs && !requiresCoverageAttrib) {
+    } else if (defaultToUniformInputs && !requiresCoverageAttrib && inputCoverageIsUsed) {
         header->fCoverageInput = kUniform_ColorInput;
     } else {
         header->fCoverageInput = kAttribute_ColorInput;
diff --git a/src/gpu/gl/GrGLProgramDesc.h b/src/gpu/gl/GrGLProgramDesc.h
index a8d27ab..cccdee9 100644
--- a/src/gpu/gl/GrGLProgramDesc.h
+++ b/src/gpu/gl/GrGLProgramDesc.h
@@ -100,7 +100,6 @@
     // Specifies where the initial color comes from before the stages are applied.
     enum ColorInput {
         kSolidWhite_ColorInput,
-        kTransBlack_ColorInput,
         kAttribute_ColorInput,
         kUniform_ColorInput,
 
diff --git a/src/gpu/gl/GrGLShaderBuilder.cpp b/src/gpu/gl/GrGLShaderBuilder.cpp
index 7a57d8e..8d1e66b 100644
--- a/src/gpu/gl/GrGLShaderBuilder.cpp
+++ b/src/gpu/gl/GrGLShaderBuilder.cpp
@@ -164,10 +164,6 @@
             this->addUniform(GrGLShaderBuilder::kFragment_Visibility, kVec4f_GrSLType, "Color",
                              &name);
         inputColor = GrGLSLExpr4(name);
-    } else if (GrGLProgramDesc::kSolidWhite_ColorInput == header.fColorInput) {
-        inputColor = GrGLSLExpr4(1);
-    } else if (GrGLProgramDesc::kTransBlack_ColorInput == header.fColorInput) {
-        inputColor = GrGLSLExpr4(0);
     }
 
     if (GrGLProgramDesc::kUniform_ColorInput == header.fCoverageInput) {
@@ -178,8 +174,6 @@
         inputCoverage = GrGLSLExpr4(name);
     } else if (GrGLProgramDesc::kSolidWhite_ColorInput == header.fCoverageInput) {
         inputCoverage = GrGLSLExpr4(1);
-    } else if (GrGLProgramDesc::kTransBlack_ColorInput == header.fCoverageInput) {
-        inputCoverage = GrGLSLExpr4(0);
     }
 
     if (k110_GrGLSLGeneration != fGpu->glslGeneration()) {
diff --git a/src/gpu/gl/GrGpuGL_program.cpp b/src/gpu/gl/GrGpuGL_program.cpp
index 9e1b6f5..45181c2 100644
--- a/src/gpu/gl/GrGpuGL_program.cpp
+++ b/src/gpu/gl/GrGpuGL_program.cpp
@@ -353,9 +353,12 @@
         uint32_t usedAttribArraysMask = 0;
         const GrVertexAttrib* vertexAttrib = this->getDrawState().getVertexAttribs();
 
+        bool canIgnoreColorAttrib = this->getDrawState().canIgnoreColorAttribute();
+
         for (int vertexAttribIndex = 0; vertexAttribIndex < vertexAttribCount;
              ++vertexAttribIndex, ++vertexAttrib) {
 
+            if (kColor_GrVertexAttribBinding != vertexAttrib->fBinding || !canIgnoreColorAttrib) {
             usedAttribArraysMask |= (1 << vertexAttribIndex);
             GrVertexAttribType attribType = vertexAttrib->fType;
             attribState->set(this,
@@ -367,6 +370,7 @@
                              stride,
                              reinterpret_cast<GrGLvoid*>(
                                  vertexOffsetInBytes + vertexAttrib->fOffset));
+            }
         }
         attribState->disableUnusedArrays(this, usedAttribArraysMask);
     }
diff --git a/tests/GLProgramsTest.cpp b/tests/GLProgramsTest.cpp
index 899b42f..9da1ecc 100644
--- a/tests/GLProgramsTest.cpp
+++ b/tests/GLProgramsTest.cpp
@@ -73,8 +73,9 @@
     // if the effects have used up all off the available attributes,
     // don't try to use color or coverage attributes as input
     do {
-        header->fColorInput = static_cast<GrGLProgramDesc::ColorInput>(
-                                  random->nextULessThan(kColorInputCnt));
+        uint32_t colorRand = random->nextULessThan(2);
+        header->fColorInput = (0 == colorRand) ? GrGLProgramDesc::kAttribute_ColorInput :
+                                                 GrGLProgramDesc::kUniform_ColorInput;
     } while (GrDrawState::kMaxVertexAttribCnt <= currAttribIndex &&
              kAttribute_ColorInput == header->fColorInput);