Move willUseInputColor check to computeInvariantOutput

BUG=skia:

Review URL: https://codereview.chromium.org/656503002
diff --git a/include/gpu/GrPaint.h b/include/gpu/GrPaint.h
index ff37fcf..c5563f0 100644
--- a/include/gpu/GrPaint.h
+++ b/include/gpu/GrPaint.h
@@ -89,9 +89,6 @@
      */
     const GrFragmentProcessor* addColorProcessor(const GrFragmentProcessor* fp) {
         SkASSERT(fp);
-        if (!fp->willUseInputColor()) {
-            fColorStages.reset();
-        }
         SkNEW_APPEND_TO_TARRAY(&fColorStages, GrFragmentStage, (fp));
         return fp;
     }
@@ -101,9 +98,6 @@
      */
     const GrFragmentProcessor* addCoverageProcessor(const GrFragmentProcessor* fp) {
         SkASSERT(fp);
-        if (!fp->willUseInputColor()) {
-            fCoverageStages.reset();
-        }
         SkNEW_APPEND_TO_TARRAY(&fCoverageStages, GrFragmentStage, (fp));
         return fp;
     }
diff --git a/include/gpu/GrProcessor.h b/include/gpu/GrProcessor.h
index c860b32..ee12663 100644
--- a/include/gpu/GrProcessor.h
+++ b/include/gpu/GrProcessor.h
@@ -13,6 +13,7 @@
 #include "GrProcessorUnitTest.h"
 #include "GrProgramElement.h"
 #include "GrTextureAccess.h"
+#include "SkMath.h"
 
 class GrContext;
 class GrCoordTransform;
@@ -32,7 +33,12 @@
 
     struct InvariantOutput{
         InvariantOutput() : fColor(0), fValidFlags(0), fIsSingleComponent(false),
-                            fNonMulStageFound(false) {}
+                            fNonMulStageFound(false), fWillUseInputColor(true) {}
+
+        enum ReadInput {
+            kWill_ReadInput,
+            kWillNot_ReadInput,
+        };
 
         void mulByUnknownOpaqueColor() {
             if (this->isOpaque()) {
@@ -62,26 +68,44 @@
             }
         }
 
-        void invalidateComponents(uint8_t invalidateFlags) {
+        void mulByKnownAlpha(uint8_t alpha) {
+            if (this->hasZeroAlpha() || 0 == alpha) {
+                this->internalSetToTransparentBlack();
+            } else {
+                if (alpha != 255) {
+                    // Multiply color by alpha
+                    fColor = GrColorPackRGBA(SkMulDiv255Round(GrColorUnpackR(fColor), alpha),
+                                             SkMulDiv255Round(GrColorUnpackG(fColor), alpha),
+                                             SkMulDiv255Round(GrColorUnpackB(fColor), alpha),
+                                             SkMulDiv255Round(GrColorUnpackA(fColor), alpha));
+                }
+            }
+        }
+
+        void invalidateComponents(uint8_t invalidateFlags, ReadInput readsInput) {
             fValidFlags &= ~invalidateFlags;
             fIsSingleComponent = false;
+            if (kWillNot_ReadInput == readsInput) {
+                fWillUseInputColor = false;
+            }
         }
 
-        void setToTransparentBlack() {
-            this->internalSetToTransparentBlack();
-            fNonMulStageFound = true;
-        }
-
-        void setToOther(uint8_t validFlags, GrColor color) {
+        void setToOther(uint8_t validFlags, GrColor color, ReadInput readsInput) {
             fValidFlags = validFlags;
             fColor = color;
             fIsSingleComponent = false;
             fNonMulStageFound = true;
+            if (kWillNot_ReadInput == readsInput) {
+                fWillUseInputColor = false;
+            }
         }
 
-        void setToUnknown() {
+        void setToUnknown(ReadInput readsInput) {
             this->internalSetToUnknown();
             fNonMulStageFound= true;
+            if (kWillNot_ReadInput == readsInput) {
+                fWillUseInputColor = false;
+            }
         }
 
         bool isOpaque() const {
@@ -129,11 +153,13 @@
         friend class GrDrawState;
         friend class GrOptDrawState;
         friend class GrPaint;
+        friend class GrProcessor;
 
         GrColor fColor;
         uint32_t fValidFlags;
         bool fIsSingleComponent;
         bool fNonMulStageFound;
+        bool fWillUseInputColor;
     };
 
     /**
@@ -145,6 +171,7 @@
      * meaning if the corresponding bit in validFlags is set.
      */
     void computeInvariantOutput(InvariantOutput* inout) const {
+        inout->fWillUseInputColor = true;
         this->onComputeInvariantOutput(inout);
 #ifdef SK_DEBUG
         inout->validate();
diff --git a/src/core/SkXfermode.cpp b/src/core/SkXfermode.cpp
index 8a8c622..616a361 100644
--- a/src/core/SkXfermode.cpp
+++ b/src/core/SkXfermode.cpp
@@ -1213,7 +1213,7 @@
     }
 
     virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE {
-        inout->setToUnknown();
+        inout->setToUnknown(InvariantOutput::kWill_ReadInput);
     }
 
     SkXfermode::Mode fMode;
diff --git a/src/effects/SkArithmeticMode.cpp b/src/effects/SkArithmeticMode.cpp
index d337603..06a602a 100644
--- a/src/effects/SkArithmeticMode.cpp
+++ b/src/effects/SkArithmeticMode.cpp
@@ -346,7 +346,7 @@
 
 void GrArithmeticEffect::onComputeInvariantOutput(InvariantOutput* inout) const {
     // TODO: optimize this
-    inout->setToUnknown();
+    inout->setToUnknown(InvariantOutput::kWill_ReadInput);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/src/effects/SkColorCubeFilter.cpp b/src/effects/SkColorCubeFilter.cpp
index 2908d08..325d7de 100644
--- a/src/effects/SkColorCubeFilter.cpp
+++ b/src/effects/SkColorCubeFilter.cpp
@@ -264,7 +264,7 @@
 }
 
 void GrColorCubeEffect::onComputeInvariantOutput(InvariantOutput* inout) const {
-    inout->setToUnknown();
+    inout->setToUnknown(InvariantOutput::kWill_ReadInput);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/src/effects/SkColorFilters.cpp b/src/effects/SkColorFilters.cpp
index d96aadf..4c6474a 100644
--- a/src/effects/SkColorFilters.cpp
+++ b/src/effects/SkColorFilters.cpp
@@ -274,17 +274,7 @@
 private:
     ModeColorFilterEffect(GrColor color, SkXfermode::Mode mode)
         : fMode(mode),
-          fColor(color) {
-
-        SkXfermode::Coeff dstCoeff;
-        SkXfermode::Coeff srcCoeff;
-        SkAssertResult(SkXfermode::ModeAsCoeff(fMode, &srcCoeff, &dstCoeff));
-        // These could be calculated from the blend equation with template trickery..
-        if (SkXfermode::kZero_Coeff == dstCoeff &&
-                !GrBlendCoeffRefsDst(sk_blend_to_grblend(srcCoeff))) {
-            this->setWillNotUseInputColor();
-        }
-    }
+          fColor(color) {}
 
     virtual bool onIsEqual(const GrFragmentProcessor& other) const SK_OVERRIDE {
         const ModeColorFilterEffect& s = other.cast<ModeColorFilterEffect>();
@@ -392,7 +382,17 @@
                                 MaskedColorExpr(filterColor, kRGBA_GrColorComponentFlags),
                                 MaskedColorExpr(inputColor, inout->validFlags()));
 
-    inout->setToOther(result.getValidComponents(), result.getColor());
+    // Check if we will use the input color
+    SkXfermode::Coeff dstCoeff;
+    SkXfermode::Coeff srcCoeff;
+    SkAssertResult(SkXfermode::ModeAsCoeff(fMode, &srcCoeff, &dstCoeff));
+    InvariantOutput::ReadInput readInput = InvariantOutput::kWill_ReadInput;
+    // These could be calculated from the blend equation with template trickery..
+    if (SkXfermode::kZero_Coeff == dstCoeff &&
+        !GrBlendCoeffRefsDst(sk_blend_to_grblend(srcCoeff))) {
+        readInput = InvariantOutput::kWillNot_ReadInput;
+    }
+    inout->setToOther(result.getValidComponents(), result.getColor(), readInput);
 }
 
 GR_DEFINE_FRAGMENT_PROCESSOR_TEST(ModeColorFilterEffect);
diff --git a/src/effects/SkColorMatrixFilter.cpp b/src/effects/SkColorMatrixFilter.cpp
index 4aee11a..844a376 100644
--- a/src/effects/SkColorMatrixFilter.cpp
+++ b/src/effects/SkColorMatrixFilter.cpp
@@ -453,7 +453,7 @@
             // then we can't know the final result.
             if (0 != fMatrix.fMat[kAlphaRowStartIdx + i]) {
                 if (!(inout->validFlags() & kRGBAFlags[i])) {
-                    inout->setToUnknown();
+                    inout->setToUnknown(InvariantOutput::kWill_ReadInput);
                     return;
                 } else {
                     uint32_t component = (inout->color() >> kShifts[i]) & 0xFF;
@@ -467,7 +467,8 @@
         // underflow this may deviate from the actual result. Maybe the effect should pin its
         // result if the matrix could over/underflow for any component?
         inout->setToOther(kA_GrColorComponentFlag,
-                          static_cast<uint8_t>(SkScalarPin(outputA, 0, 255)) << GrColor_SHIFT_A);
+                          static_cast<uint8_t>(SkScalarPin(outputA, 0, 255)) << GrColor_SHIFT_A,
+                          InvariantOutput::kWill_ReadInput);
     }
 
     SkColorMatrix fMatrix;
diff --git a/src/effects/SkDisplacementMapEffect.cpp b/src/effects/SkDisplacementMapEffect.cpp
index 2e6a2c8..bc35752 100644
--- a/src/effects/SkDisplacementMapEffect.cpp
+++ b/src/effects/SkDisplacementMapEffect.cpp
@@ -473,7 +473,6 @@
     this->addTextureAccess(&fDisplacementAccess);
     this->addCoordTransform(&fColorTransform);
     this->addTextureAccess(&fColorAccess);
-    this->setWillNotUseInputColor();
 }
 
 GrDisplacementMapEffect::~GrDisplacementMapEffect() {
@@ -498,7 +497,7 @@
     // and no displacement offset push any texture coordinates out of bounds OR if the constant
     // alpha is 0. Since this isn't trivial to compute at this point, let's assume the output is
     // not of constant color when a displacement effect is applied.
-    inout->setToUnknown();
+    inout->setToUnknown(InvariantOutput::kWillNot_ReadInput);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/src/effects/SkLumaColorFilter.cpp b/src/effects/SkLumaColorFilter.cpp
index d06a213..7750f04 100644
--- a/src/effects/SkLumaColorFilter.cpp
+++ b/src/effects/SkLumaColorFilter.cpp
@@ -115,7 +115,8 @@
 
     virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE {
         // The output is always black. The alpha value for the color passed in is arbitrary.
-        inout->setToOther(kRGB_GrColorComponentFlags, GrColorPackRGBA(0, 0, 0, 0));
+        inout->setToOther(kRGB_GrColorComponentFlags, GrColorPackRGBA(0, 0, 0, 0),
+                          InvariantOutput::kWill_ReadInput);
     }
 };
 
diff --git a/src/effects/SkPerlinNoiseShader.cpp b/src/effects/SkPerlinNoiseShader.cpp
index 3dd161c..de17806 100644
--- a/src/effects/SkPerlinNoiseShader.cpp
+++ b/src/effects/SkPerlinNoiseShader.cpp
@@ -587,7 +587,7 @@
     }
 
     void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE {
-        inout->setToUnknown();
+        inout->setToUnknown(InvariantOutput::kWillNot_ReadInput);
     }
 
     GrPerlinNoiseEffect(SkPerlinNoiseShader::Type type,
@@ -606,7 +606,6 @@
         this->addTextureAccess(&fNoiseAccess);
         fCoordTransform.reset(kLocal_GrCoordSet, matrix);
         this->addCoordTransform(&fCoordTransform);
-        this->setWillNotUseInputColor();
     }
 
     GR_DECLARE_FRAGMENT_PROCESSOR_TEST;
diff --git a/src/effects/SkTableColorFilter.cpp b/src/effects/SkTableColorFilter.cpp
index 1da6c55..fb177b2 100644
--- a/src/effects/SkTableColorFilter.cpp
+++ b/src/effects/SkTableColorFilter.cpp
@@ -417,7 +417,7 @@
     if (fFlags & SkTable_ColorFilter::kA_Flag) {
         invalidateFlags |= kA_GrColorComponentFlag;
     }
-    inout->invalidateComponents(invalidateFlags);
+    inout->invalidateComponents(invalidateFlags, InvariantOutput::kWill_ReadInput);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
diff --git a/src/gpu/GrOptDrawState.cpp b/src/gpu/GrOptDrawState.cpp
index 7c8710f..40e0c6c 100644
--- a/src/gpu/GrOptDrawState.cpp
+++ b/src/gpu/GrOptDrawState.cpp
@@ -244,16 +244,19 @@
 
     for (int i = 0; i < ds.numColorStages(); ++i) {
         const GrFragmentProcessor* fp = ds.getColorStage(i).getProcessor();
-        if (!fp->willUseInputColor()) {
+        fp->computeInvariantOutput(&inout);
+        if (!inout.fWillUseInputColor) {
             *firstColorStageIdx = i;
             fInputColorIsUsed = false;
         }
-        fp->computeInvariantOutput(&inout);
         if (kRGBA_GrColorComponentFlags == inout.fValidFlags) {
             *firstColorStageIdx = i + 1;
             fColor = inout.fColor;
             fInputColorIsUsed = true;
             *fixedFunctionVAToRemove |= 0x1 << kColor_GrVertexAttribBinding;
+            // Since we are clearing all previous color stages we are in a state where we have found
+            // zero stages that don't multiply the inputColor.
+            inout.fNonMulStageFound = false;
         }
     }
 }
@@ -267,9 +270,11 @@
     // Don't do any optimizations on coverage stages. It should not be the case where we do not use
     // input coverage in an effect
 #ifdef OptCoverageStages
+    GrProcessor::InvariantOutput inout;
     for (int i = 0; i < ds.numCoverageStages(); ++i) {
-        const GrProcessor* processor = ds.getCoverageStage(i).getProcessor();
-        if (!processor->willUseInputColor()) {
+        const GrFragmentProcessor* fp = ds.getCoverageStage(i).getProcessor();
+        fp->computeInvariantOutput(&inout);
+        if (!inout.fWillUseInputColor) {
             *firstCoverageStageIdx = i;
             fInputCoverageIsUsed = false;
         }
diff --git a/src/gpu/GrProcessor.cpp b/src/gpu/GrProcessor.cpp
index bf65cb0..04401c8 100644
--- a/src/gpu/GrProcessor.cpp
+++ b/src/gpu/GrProcessor.cpp
@@ -133,6 +133,9 @@
     }
 
     SkASSERT(this->validPreMulColor());
+
+    // If we claim that we are not using the input color we must not be modulating the input.
+    SkASSERT(fNonMulStageFound || fWillUseInputColor);
 }
 
 bool GrProcessor::InvariantOutput::colorComponentsAllEqual() const {
diff --git a/src/gpu/effects/GrConvexPolyEffect.cpp b/src/gpu/effects/GrConvexPolyEffect.cpp
index bafa64d..c84b540 100644
--- a/src/gpu/effects/GrConvexPolyEffect.cpp
+++ b/src/gpu/effects/GrConvexPolyEffect.cpp
@@ -46,7 +46,7 @@
     virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE {
         if (fRect.isEmpty()) {
             // An empty rect will have no coverage anywhere.
-            inout->setToTransparentBlack();
+            inout->mulByKnownAlpha(0);
         } else {
             inout->mulByUnknownAlpha();
         }
diff --git a/src/gpu/effects/GrDitherEffect.cpp b/src/gpu/effects/GrDitherEffect.cpp
index 068a23e..643829b 100644
--- a/src/gpu/effects/GrDitherEffect.cpp
+++ b/src/gpu/effects/GrDitherEffect.cpp
@@ -50,7 +50,7 @@
 };
 
 void DitherEffect::onComputeInvariantOutput(InvariantOutput* inout) const {
-    inout->setToUnknown();
+    inout->setToUnknown(InvariantOutput::kWill_ReadInput);
 }
 
 //////////////////////////////////////////////////////////////////////////////
diff --git a/src/gpu/effects/GrYUVtoRGBEffect.cpp b/src/gpu/effects/GrYUVtoRGBEffect.cpp
index f280d1b..b18bd7f 100644
--- a/src/gpu/effects/GrYUVtoRGBEffect.cpp
+++ b/src/gpu/effects/GrYUVtoRGBEffect.cpp
@@ -99,7 +99,6 @@
         this->addTextureAccess(&fYAccess);
         this->addTextureAccess(&fUAccess);
         this->addTextureAccess(&fVAccess);
-        this->setWillNotUseInputColor();
     }
 
     virtual bool onIsEqual(const GrFragmentProcessor& sBase) const {
@@ -112,7 +111,8 @@
 
     virtual void onComputeInvariantOutput(InvariantOutput* inout) const SK_OVERRIDE {
         // YUV is opaque
-        inout->setToOther(kA_GrColorComponentFlag, 0xFF << GrColor_SHIFT_A);
+        inout->setToOther(kA_GrColorComponentFlag, 0xFF << GrColor_SHIFT_A,
+                          InvariantOutput::kWill_ReadInput);
     }
 
     GrCoordTransform fCoordTransform;
diff --git a/tests/GpuColorFilterTest.cpp b/tests/GpuColorFilterTest.cpp
index c8f5630..82f5f31 100644
--- a/tests/GpuColorFilterTest.cpp
+++ b/tests/GpuColorFilterTest.cpp
@@ -100,7 +100,8 @@
         SkAutoTUnref<SkColorFilter> cf(SkColorFilter::CreateModeFilter(test.filterColor, test.filterMode));
         SkAutoTUnref<GrFragmentProcessor> effect(cf->asFragmentProcessor(grContext));
         GrProcessor::InvariantOutput inout;
-        inout.setToOther(test.inputComponents, test.inputColor);
+        inout.setToOther(test.inputComponents, test.inputColor,
+                         GrProcessor::InvariantOutput::kWill_ReadInput);
         effect->computeInvariantOutput(&inout);
 
         REPORTER_ASSERT(reporter, filterColor(inout.color(), inout.validFlags()) == test.outputColor);