Start removing calls to GrDrawState::setTexture() (and GrPaint::setTexture()?) when there's
a GrSingleTextureEffect involved holding the texture.

http://codereview.appspot.com/6353094/



git-svn-id: http://skia.googlecode.com/svn/trunk@4608 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/gpu/GrPaint.h b/include/gpu/GrPaint.h
index 3c8c47a..8b1bf2c 100644
--- a/include/gpu/GrPaint.h
+++ b/include/gpu/GrPaint.h
@@ -149,19 +149,15 @@
         }
         
         for (int i = 0; i < kMaxTextures; ++i) {
-            GrSafeUnref(fTextures[i]);
-            fTextures[i] = paint.fTextures[i];
-            if (NULL != fTextures[i]) {
+            GrSafeAssign(fTextures[i], paint.fTextures[i]);
+            if (paint.isTextureStageEnabled(i)) {
                 fTextureSamplers[i] = paint.fTextureSamplers[i];
-                fTextures[i]->ref();
             }
         }
         for (int i = 0; i < kMaxMasks; ++i) {
-            GrSafeUnref(fMaskTextures[i]);
-            fMaskTextures[i] = paint.fMaskTextures[i];
-            if (NULL != fMaskTextures[i]) {
+            GrSafeAssign(fMaskTextures[i], paint.fMaskTextures[i]);
+            if (paint.isMaskStageEnabled(i)) {
                 fMaskSamplers[i] = paint.fMaskSamplers[i];
-                fMaskTextures[i]->ref();
             }
         }
         return *this;
@@ -209,7 +205,8 @@
     int getActiveTextureStageMask() const {
         int mask = 0;
         for (int i = 0; i < kMaxTextures; ++i) {
-            if (NULL != fTextures[i]) {
+            if ((NULL != fTextures[i]) ||
+                (NULL != fTextureSamplers[i].getCustomStage())) {
                 mask |= 1 << (i + kFirstTextureStage);
             }
         }
@@ -219,7 +216,8 @@
     int getActiveMaskStageMask() const {
         int mask = 0;
         for (int i = 0; i < kMaxMasks; ++i) {
-            if (NULL != fMaskTextures[i]) {
+            if ((NULL != fMaskTextures[i]) ||
+                (NULL != fMaskSamplers[i].getCustomStage())) {
                 mask |= 1 << (i + kFirstMaskStage);
             }
         }
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 95e0eca..e70ac5c 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -246,7 +246,6 @@
     SkAutoTUnref<GrCustomStage> morph(
         SkNEW_ARGS(GrMorphologyEffect, (texture, direction, radius, morphType)));
     drawState->sampler(0)->setCustomStage(morph);
-    drawState->setTexture(0, texture);
     gpu->drawSimpleRect(rect, NULL, 1 << 0);
 }
 
@@ -267,7 +266,6 @@
                                                       (texture, direction, radius)));
     conv->setGaussianKernel(sigma);
     drawState->sampler(0)->setCustomStage(conv);
-    drawState->setTexture(0, texture);
     gpu->drawSimpleRect(rect, NULL, 1 << 0);
 }
 
diff --git a/src/gpu/GrDrawState.h b/src/gpu/GrDrawState.h
index c07001c..3de33d8 100644
--- a/src/gpu/GrDrawState.h
+++ b/src/gpu/GrDrawState.h
@@ -209,10 +209,22 @@
      */
     const GrTexture* getTexture(int stage) const {
         GrAssert((unsigned)stage < kNumStages);
+        GrAssert(!this->getSampler(stage).getCustomStage() ||
+                 !fTextures[stage] ||
+                 fTextures[stage] == this->getSampler(stage).getCustomStage()->texture(0));
+        if (this->getSampler(stage).getCustomStage()) {
+            return this->getSampler(stage).getCustomStage()->texture(0);
+        }
         return fTextures[stage];
     }
     GrTexture* getTexture(int stage) {
         GrAssert((unsigned)stage < kNumStages);
+        GrAssert(!this->getSampler(stage).getCustomStage() ||
+                 !fTextures[stage] ||
+                 fTextures[stage] == this->getSampler(stage).getCustomStage()->texture(0));
+        if (this->getSampler(stage).getCustomStage()) {
+            return this->getSampler(stage).getCustomStage()->texture(0);
+        }
         return fTextures[stage];
     }
 
@@ -811,7 +823,7 @@
 
         for (int i = 0; i < kNumStages; i++) {
             SkSafeRef(fTextures[i]);            // already copied by memcpy
-            if (s.fTextures[i]) {
+            if (s.isStageEnabled(i)) {
                 this->fSamplerStates[i] = s.fSamplerStates[i];
             }
         }
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index 6443fa2..dfded26 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -562,7 +562,6 @@
         SkDebugf("Couldn't convert bitmap to texture.\n");
         return false;
     }
-    grPaint->setTexture(kShaderTextureIdx, texture);
 
     switch (bmptype) {
         case SkShader::kRadial_BitmapType:
@@ -595,6 +594,9 @@
             } else {
                 sampler->setFilter(GrSamplerState::kNearest_Filter);
             }
+            // TODO - once we have a trivial GrCustomStage for texture drawing,
+            // create that here & get rid of the paint's texture
+            grPaint->setTexture(kShaderTextureIdx, texture);
             break;
     }
     sampler->setWrapX(sk_tile_mode_to_grwrap(tileModes[0]));
@@ -1476,7 +1478,6 @@
     paint.textureSampler(0)->setFilter(GrSamplerState::kBilinear_Filter);
     paint.textureSampler(0)->reset(sampleM);
     paint.textureSampler(0)->setCustomStage(stage);
-    paint.setTexture(0, srcTexture);
     context->drawRect(paint, rect);
 }
 
diff --git a/src/gpu/gl/GrGpuGL.cpp b/src/gpu/gl/GrGpuGL.cpp
index edabb9c..4a63b33 100644
--- a/src/gpu/gl/GrGpuGL.cpp
+++ b/src/gpu/gl/GrGpuGL.cpp
@@ -2140,15 +2140,6 @@
     GrGLTexture* nextTexture = 
         static_cast<GrGLTexture*>(drawState->getTexture(stage));
 
-    // HACK - if we're using a new SingleTextureEffect, override
-    // the old texture pointer
-    const GrSamplerState& sampler = drawState->getSampler(stage);
-    GrCustomStage* customStage = sampler.getCustomStage();
-    if (customStage && customStage->numTextures()) {
-        nextTexture = 
-            static_cast<GrGLTexture*>(customStage->texture(0));
-    }
-
     flushBoundTextureAndParams(stage, nextTexture);
 }