releaseTextures portion of GrDrawState Ref textures CL (http://codereview.appspot.com/6251049/)

http://codereview.appspot.com/6299081/



git-svn-id: http://skia.googlecode.com/svn/trunk@4254 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 2e5bd7f..3cc2c89 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -744,6 +744,7 @@
     SK_TRACE_EVENT0("GrContext::drawRect");
 
     GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
+    GrDrawState::AutoTextureRelease atr(fDrawState);
     int stageMask = paint.getActiveStageMask();
 
     GrRect devRect = rect;
@@ -863,6 +864,7 @@
 
 #if GR_STATIC_RECT_VB
     GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
+    GrDrawState::AutoTextureRelease atr(fDrawState);
     GrDrawState* drawState = target->drawState();
     GrVertexLayout layout = PaintStageVertexLayoutBits(paint, NULL);
     GrDrawState::AutoViewMatrixRestore avmr(drawState);
@@ -907,6 +909,7 @@
 #else
     target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
 #endif
+    GrDrawState::AutoTextureRelease atr(fDrawState);
 
     const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
     const GrMatrix* srcMatrices[GrDrawState::kNumStages] = {NULL};
@@ -930,6 +933,7 @@
     GrDrawTarget::AutoReleaseGeometry geo;
 
     GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
+    GrDrawState::AutoTextureRelease atr(fDrawState);
 
     bool hasTexCoords[GrPaint::kTotalStages] = {
         NULL != texCoords,   // texCoordSrc provides explicit stage 0 coords
@@ -1034,6 +1038,7 @@
     DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
                                             kUnbuffered_DrawCategory;
     GrDrawTarget* target = this->prepareToDraw(paint, category);
+    GrDrawState::AutoTextureRelease atr(fDrawState);
     GrDrawState* drawState = target->drawState();
     GrMatrix vm = drawState->getViewMatrix();
 
@@ -1139,6 +1144,7 @@
     DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
                                             kUnbuffered_DrawCategory;
     GrDrawTarget* target = this->prepareToDraw(paint, category);
+    GrDrawState::AutoTextureRelease atr(fDrawState);
     GrDrawState::StageMask stageMask = paint.getActiveStageMask();
 
     bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
@@ -1813,7 +1819,9 @@
         srcRect = dstRect;
         SkTSwap(srcTexture, dstTexture);
         // If temp2 is non-NULL, don't render back to origTexture
-        if (temp2 && dstTexture == origTexture) dstTexture = temp2->texture();
+        if (temp2 && dstTexture == origTexture) {
+            dstTexture = temp2->texture();
+        }
     }
 
     SkIRect srcIRect;
diff --git a/src/gpu/GrDefaultTextContext.cpp b/src/gpu/GrDefaultTextContext.cpp
index d3d4e52..21cb990 100644
--- a/src/gpu/GrDefaultTextContext.cpp
+++ b/src/gpu/GrDefaultTextContext.cpp
@@ -61,6 +61,7 @@
                                           4, 6);
         fVertices = NULL;
         this->INHERITED::reset();
+        drawState->setTexture(kGlyphMaskStage, NULL);
     }
 }
 
diff --git a/src/gpu/GrDrawState.h b/src/gpu/GrDrawState.h
index 55bd04c..e77b63a 100644
--- a/src/gpu/GrDrawState.h
+++ b/src/gpu/GrDrawState.h
@@ -205,6 +205,27 @@
         return fTextures[stage];
     }
 
+    /**
+     * Release all the textures referred to by this draw state
+     */
+    void releaseTextures() {
+        for (int i = 0; i < kNumStages; ++i) {
+            this->setTexture(i, NULL);
+        }
+    }
+
+    class AutoTextureRelease : public ::GrNoncopyable {
+    public:
+        AutoTextureRelease(GrDrawState* ds) : fDrawState(ds) {}
+        ~AutoTextureRelease() { 
+            if (NULL != fDrawState) {
+                fDrawState->releaseTextures();
+            }
+        }
+    private:
+        GrDrawState* fDrawState;
+    };
+
     /// @}
 
     ///////////////////////////////////////////////////////////////////////////