Use GrDrawTarget::AutoClipRestore to set temporary irect clips.

R=robertphillips@google.com
Review URL: https://codereview.appspot.com/6937048

git-svn-id: http://skia.googlecode.com/svn/trunk@6793 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/GrClipMaskManager.cpp b/src/gpu/GrClipMaskManager.cpp
index 0a64dd1..311a405 100644
--- a/src/gpu/GrClipMaskManager.cpp
+++ b/src/gpu/GrClipMaskManager.cpp
@@ -542,19 +542,10 @@
 
         stencilBuffer->setLastClip(genID, clipSpaceIBounds, clipSpaceToStencilOffset);
 
-        // we set the current clip to the bounds so that our recursive draws are scissored to them.
-        // We use the copy of the GrClipData we just stashed on the SB to render from. We set it
-        // back after we finish drawing it into the stencil.
-        const GrClipData* oldClipData = fGpu->getClip();
-
+        // We set the current clip to the bounds so that our recursive draws are scissored to them.
         SkIRect stencilSpaceIBounds(clipSpaceIBounds);
         stencilSpaceIBounds.offset(clipSpaceToStencilOffset);
-
-        SkClipStack newClipStack(stencilSpaceIBounds);
-        GrClipData newClipData; // origin defaults to (0,0)
-        newClipData.fClipStack = &newClipStack;
-
-        fGpu->setClip(&newClipData);
+        GrDrawTarget::AutoClipRestore(fGpu, stencilSpaceIBounds);
 
         GrDrawTarget::AutoStateRestore asr(fGpu, GrDrawTarget::kReset_ASRInit);
         drawState = fGpu->drawState();
@@ -624,7 +615,6 @@
                                                          GrPathRendererChain::kStencilOnly_DrawType,
                                                          &stencilSupport);
                 if (NULL == pr) {
-                    fGpu->setClip(oldClipData);
                     return false;
                 }
             }
@@ -691,8 +681,6 @@
                 }
             }
         }
-        // restore clip
-        fGpu->setClip(oldClipData);
     }
     // set this last because recursive draws may overwrite it back to kNone.
     GrAssert(kNone_ClipMaskType == fCurrClipMaskType);
diff --git a/src/gpu/GrDrawTarget.cpp b/src/gpu/GrDrawTarget.cpp
index 92b8a03..9ede743 100644
--- a/src/gpu/GrDrawTarget.cpp
+++ b/src/gpu/GrDrawTarget.cpp
@@ -1218,6 +1218,15 @@
     fIndices = NULL;
 }
 
+GrDrawTarget::AutoClipRestore::AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip) {
+    fTarget = target;
+    fClip = fTarget->getClip();
+    fStack.init();
+    fStack.get()->clipDevRect(newClip, SkRegion::kReplace_Op);
+    fReplacementClip.fClipStack = fStack.get();
+    target->setClip(&fReplacementClip);
+}
+
 void GrDrawTarget::Caps::print() const {
     static const char* gNY[] = {"NO", "YES"};
     GrPrintf("8 Bit Palette Support       : %s\n", gNY[fInternals.f8BitPaletteSupport]);
diff --git a/src/gpu/GrDrawTarget.h b/src/gpu/GrDrawTarget.h
index fb83c1e..6b756b8 100644
--- a/src/gpu/GrDrawTarget.h
+++ b/src/gpu/GrDrawTarget.h
@@ -11,16 +11,18 @@
 #ifndef GrDrawTarget_DEFINED
 #define GrDrawTarget_DEFINED
 
+#include "GrClipData.h"
 #include "GrDrawState.h"
 #include "GrIndexBuffer.h"
 #include "SkMatrix.h"
 #include "GrRefCnt.h"
 #include "GrTemplates.h"
 
+#include "SkClipStack.h"
 #include "SkPath.h"
-#include "SkXfermode.h"
 #include "SkTLazy.h"
 #include "SkTArray.h"
+#include "SkXfermode.h"
 
 class GrClipData;
 class GrPath;
@@ -641,12 +643,16 @@
             fClip = fTarget->getClip();
         }
 
+        AutoClipRestore(GrDrawTarget* target, const SkIRect& newClip);
+
         ~AutoClipRestore() {
             fTarget->setClip(fClip);
         }
     private:
-        GrDrawTarget*      fTarget;
-        const GrClipData*  fClip;
+        GrDrawTarget*           fTarget;
+        const GrClipData*       fClip;
+        SkTLazy<SkClipStack>    fStack;
+        GrClipData              fReplacementClip;
     };
 
     ////////////////////////////////////////////////////////////////////////////