Added GrContext::AutoClipStack to encapsulate setting\resetting of clip stack

http://codereview.appspot.com/6343097/



git-svn-id: http://skia.googlecode.com/svn/trunk@4558 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gm/texdata.cpp b/gm/texdata.cpp
index 316e4d6..c3c09fb 100644
--- a/gm/texdata.cpp
+++ b/gm/texdata.cpp
@@ -89,8 +89,7 @@
                 }
                 GrAutoUnref au(texture);
 
-                GrClip newClip(GrRect::MakeWH(2*S, 2*S));
-                ctx->setClip(newClip);
+                GrContext::AutoClip acs(ctx, GrRect::MakeWH(2*S, 2*S));
 
                 ctx->setRenderTarget(target);
 
diff --git a/include/gpu/GrContext.h b/include/gpu/GrContext.h
index 8f7378f..a199beb 100644
--- a/include/gpu/GrContext.h
+++ b/include/gpu/GrContext.h
@@ -338,12 +338,6 @@
      */
     void setClip(const GrClip& clip);
 
-    /**
-     * Convenience method for setting the clip to a rect.
-     * @param rect  the rect to set as the new clip.
-     */
-    void setClip(const GrIRect& rect);
-
     ///////////////////////////////////////////////////////////////////////////
     // Draws
 
@@ -701,6 +695,26 @@
         GrMatrix    fMatrix;
     };
 
+    class AutoClip : GrNoncopyable {
+    public:
+        AutoClip(GrContext* context, const GrRect& newClipRect) 
+        : fContext(context)
+        , fNewClip(newClipRect) {
+            fOldClip = fContext->getClip();
+            fContext->setClip(fNewClip);
+        }
+
+        ~AutoClip() {
+            if (NULL != fContext) {
+                fContext->setClip(fOldClip);
+            }
+        }
+    private:
+        GrContext*  fContext;
+        GrClip      fOldClip;
+        GrClip      fNewClip;
+    };
+
     ///////////////////////////////////////////////////////////////////////////
     // Functions intended for internal use only.
     GrGpu* getGpu() { return fGpu; }
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index a1e4161..149359f 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -618,12 +618,6 @@
     fDrawState->enableState(GrDrawState::kClip_StateBit);
 }
 
-void GrContext::setClip(const GrIRect& rect) {
-    GrClip clip;
-    clip.setFromIRect(rect);
-    fGpu->setClip(clip);
-}
-
 ////////////////////////////////////////////////////////////////////////////////
 
 void GrContext::clear(const GrIRect* rect, 
@@ -1822,7 +1816,6 @@
                                    float sigmaX, float sigmaY) {
     ASSERT_OWNED_RESOURCE(srcTexture);
     GrRenderTarget* oldRenderTarget = this->getRenderTarget();
-    GrClip oldClip = this->getClip();
     GrTexture* origTexture = srcTexture;
     AutoMatrix avm(this, GrMatrix::I());
     SkIRect clearRect;
@@ -1837,8 +1830,7 @@
     scale_rect(&srcRect, static_cast<float>(scaleFactorX), 
                          static_cast<float>(scaleFactorY));
 
-    GrClip newClip(srcRect);
-    this->setClip(newClip);
+    AutoClip acs(this, srcRect);
 
     GrAssert(kBGRA_8888_PM_GrPixelConfig == srcTexture->config() ||
              kRGBA_8888_PM_GrPixelConfig == srcTexture->config() ||
@@ -1938,7 +1930,6 @@
         SkTSwap(srcTexture, dstTexture);
     }
     this->setRenderTarget(oldRenderTarget);
-    this->setClip(oldClip);
     return srcTexture;
 }
 
@@ -1949,12 +1940,11 @@
                                       SkISize radius) {
     ASSERT_OWNED_RESOURCE(srcTexture);
     GrRenderTarget* oldRenderTarget = this->getRenderTarget();
-    AutoMatrix avm(this, GrMatrix::I());
-    GrClip oldClip = this->getClip();
 
-    GrClip newClip(GrRect::MakeWH(SkIntToScalar(srcTexture->width()), 
-                                  SkIntToScalar(srcTexture->height())));
-    this->setClip(newClip);
+    AutoMatrix avm(this, GrMatrix::I());
+
+    AutoClip acs(this, GrRect::MakeWH(SkIntToScalar(srcTexture->width()), 
+                                           SkIntToScalar(srcTexture->height())));
 
     if (radius.fWidth > 0) {
         this->setRenderTarget(temp1->asRenderTarget());
@@ -1975,7 +1965,6 @@
         srcTexture = temp2;
     }
     this->setRenderTarget(oldRenderTarget);
-    this->setClip(oldClip);
     return srcTexture;
 }
 
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index fc77c47..2a2bbcd 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -1445,10 +1445,7 @@
     SkASSERT(srcTexture && srcTexture->getContext() == context);
     GrContext::AutoMatrix avm(context, GrMatrix::I());
     GrContext::AutoRenderTarget art(context, dstTexture->asRenderTarget());
-    GrClip oldClip = context->getClip();
-
-    GrClip newClip(rect);
-    context->setClip(newClip);
+    GrContext::AutoClip acs(context, rect);
 
     GrMatrix sampleM;
     sampleM.setIDiv(srcTexture->width(), srcTexture->height());
@@ -1459,7 +1456,6 @@
     paint.textureSampler(0)->setCustomStage(stage);
     paint.setTexture(0, srcTexture);
     context->drawRect(paint, rect);
-    context->setClip(oldClip);
 }
 
 };