Add GrPaint::*StageEnabled() and GrDrawState::stageEnabled() functions.
These wrap the question of "is this stage of the shader enabled?" so that
we can change the semantics - previously iff there was a texture, now
if there is a texture OR a GrCustomStage, soon (post-cl 6306097) iff there
is a GrCustomStage, which at that point will hold whatever texture is
necessary.

http://codereview.appspot.com/6306104/



git-svn-id: http://skia.googlecode.com/svn/trunk@4325 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/gpu/GrPaint.h b/include/gpu/GrPaint.h
index 729f6f0..3c8c47a 100644
--- a/include/gpu/GrPaint.h
+++ b/include/gpu/GrPaint.h
@@ -64,6 +64,12 @@
         return fTextureSamplers[i];
     }
 
+    bool isTextureStageEnabled(int i) const {
+        GrAssert((unsigned)i < kMaxTextures);
+        return (NULL != fTextures[i]) ||
+               (NULL != fTextureSamplers[i].getCustomStage());
+    }
+
     // The mask can be alpha-only or per channel. It is applied
     // after the colorfilter
     void setMask(int i, GrTexture* mask) {
@@ -90,6 +96,12 @@
         return fMaskSamplers[i];
     }
 
+    bool isMaskStageEnabled(int i) const {
+        GrAssert((unsigned)i < kMaxTextures);
+        return (NULL != fMaskTextures[i]) ||
+               (NULL != fMaskSamplers[i].getCustomStage());
+    }
+
     // pre-concats sampler matrices for non-NULL textures and masks
     void preConcatActiveSamplerMatrices(const GrMatrix& matrix) {
         for (int i = 0; i < kMaxTextures; ++i) {
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index a49bf99..e157a8a 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -893,7 +893,7 @@
     SK_TRACE_EVENT0("GrContext::drawRectToRect");
 
     // srcRect refers to paint's first texture
-    if (NULL == paint.getTexture(0)) {
+    if (!paint.isTextureStageEnabled(0)) {
         drawRect(paint, dstRect, -1, dstMatrix);
         return;
     }
@@ -1590,9 +1590,9 @@
 
     for (int i = 0; i < GrPaint::kMaxTextures; ++i) {
         int s = i + GrPaint::kFirstTextureStage;
-        fDrawState->setTexture(s, paint.getTexture(i));
         ASSERT_OWNED_RESOURCE(paint.getTexture(i));
-        if (paint.getTexture(i)) {
+        if (paint.isTextureStageEnabled(i)) {
+            fDrawState->setTexture(s, paint.getTexture(i));
             *fDrawState->sampler(s) = paint.getTextureSampler(i);
         }
     }
@@ -1601,9 +1601,9 @@
 
     for (int i = 0; i < GrPaint::kMaxMasks; ++i) {
         int s = i + GrPaint::kFirstMaskStage;
-        fDrawState->setTexture(s, paint.getMask(i));
         ASSERT_OWNED_RESOURCE(paint.getMask(i));
-        if (paint.getMask(i)) {
+        if (paint.isMaskStageEnabled(i)) {
+            fDrawState->setTexture(s, paint.getMask(i));
             *fDrawState->sampler(s) = paint.getMaskSampler(i);
         }
     }
diff --git a/src/gpu/GrDefaultTextContext.cpp b/src/gpu/GrDefaultTextContext.cpp
index 9530f07..07a0162 100644
--- a/src/gpu/GrDefaultTextContext.cpp
+++ b/src/gpu/GrDefaultTextContext.cpp
@@ -115,7 +115,7 @@
     bool invVMComputed = false;
     GrMatrix invVM;
     for (int t = 0; t < GrPaint::kMaxTextures; ++t) {
-        if (NULL != fGrPaint.getTexture(t)) {
+        if (fGrPaint.isTextureStageEnabled(t)) {
             if (invVMComputed || fOrigViewMatrix.invert(&invVM)) {
                 invVMComputed = true;
                 fGrPaint.textureSampler(t)->preConcatMatrix(invVM);
@@ -123,7 +123,7 @@
         }
     }
     for (int m = 0; m < GrPaint::kMaxMasks; ++m) {
-        if (NULL != fGrPaint.getMask(m)) {
+        if (fGrPaint.isMaskStageEnabled(m)) {
             if (invVMComputed || fOrigViewMatrix.invert(&invVM)) {
                 invVMComputed = true;
                 fGrPaint.maskSampler(m)->preConcatMatrix(invVM);
diff --git a/src/gpu/GrDrawState.h b/src/gpu/GrDrawState.h
index a56baec..61925cd 100644
--- a/src/gpu/GrDrawState.h
+++ b/src/gpu/GrDrawState.h
@@ -760,6 +760,12 @@
 
     ///////////////////////////////////////////////////////////////////////////
 
+    bool isStageEnabled(int s) const {
+        GrAssert((unsigned)s < kNumStages);
+        return (NULL != fTextures[s]) ||
+               (NULL != fSamplerStates[s].getCustomStage());
+    }
+
     // Most stages are usually not used, so conditionals here
     // reduce the expected number of bytes touched by 50%.
     bool operator ==(const GrDrawState& s) const {
@@ -772,7 +778,7 @@
         }
 
         for (int i = 0; i < kNumStages; i++) {
-            if (fTextures[i] &&
+            if (this->isStageEnabled(i) &&
                 this->fSamplerStates[i] != s.fSamplerStates[i]) {
                 return false;
             }
diff --git a/src/gpu/GrDrawTarget.cpp b/src/gpu/GrDrawTarget.cpp
index a051716..39ea5bf 100644
--- a/src/gpu/GrDrawTarget.cpp
+++ b/src/gpu/GrDrawTarget.cpp
@@ -862,7 +862,7 @@
     }
     // Check if a color stage could create a partial alpha
     for (int s = 0; s < drawState.getFirstCoverageStage(); ++s) {
-        if (StageWillBeUsed(s, this->getDrawState())) {
+        if (this->isStageEnabled(s)) {
             GrAssert(NULL != drawState.getTexture(s));
             GrPixelConfig config = drawState.getTexture(s)->config();
             if (!GrPixelConfigIsOpaque(config)) {
@@ -951,7 +951,7 @@
     for (int s = drawState.getFirstCoverageStage();
          !hasCoverage && s < GrDrawState::kNumStages;
          ++s) {
-        if (StageWillBeUsed(s, this->getDrawState())) {
+        if (this->isStageEnabled(s)) {
             hasCoverage = true;
         }
     }
diff --git a/src/gpu/GrDrawTarget.h b/src/gpu/GrDrawTarget.h
index 73de1c5..f619469 100644
--- a/src/gpu/GrDrawTarget.h
+++ b/src/gpu/GrDrawTarget.h
@@ -952,15 +952,9 @@
                 return 0;
         }
     }
-    // given (a vertex layout and) a draw state, will a stage be used?
-    static bool StageWillBeUsed(int stage,
-                                const GrDrawState& state) {
-        return NULL != state.getTexture(stage);
-    }
 
     bool isStageEnabled(int stage) const {
-        return StageWillBeUsed(stage,
-                               this->getDrawState());
+        return this->getDrawState().isStageEnabled(stage);
     }
 
     StageMask enabledStages() const {
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index 8c34c54..d3ca0b5 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -883,7 +883,7 @@
 
     static const int MASK_IDX = GrPaint::kMaxMasks - 1;
     // we assume the last mask index is available for use
-    GrAssert(NULL == grp->getMask(MASK_IDX));
+    GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
     grp->setMask(MASK_IDX, blurTexture);
     grp->maskSampler(MASK_IDX)->reset();
 
@@ -949,7 +949,7 @@
 
     static const int MASK_IDX = GrPaint::kMaxMasks - 1;
     // we assume the last mask index is available for use
-    GrAssert(NULL == grp->getMask(MASK_IDX));
+    GrAssert(!grp->isMaskStageEnabled(MASK_IDX));
     grp->setMask(MASK_IDX, texture);
     grp->maskSampler(MASK_IDX)->reset();