Move some auto restore helpers from GrDrawTarget to GrDrawState.

R=robertphillips@google.com

git-svn-id: http://skia.googlecode.com/svn/trunk@5846 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/GrDrawState.cpp b/src/gpu/GrDrawState.cpp
index f798cda..b9b708e 100644
--- a/src/gpu/GrDrawState.cpp
+++ b/src/gpu/GrDrawState.cpp
@@ -46,3 +46,44 @@
     this->setColorFilter(paint.getColorFilterColor(), paint.getColorFilterMode());
     this->setCoverage(paint.getCoverage());
 }
+
+////////////////////////////////////////////////////////////////////////////////
+
+GrDrawState::AutoDeviceCoordDraw::AutoDeviceCoordDraw(GrDrawState* drawState,
+                                                      uint32_t explicitCoordStageMask) {
+    GrAssert(NULL != drawState);
+
+    fDrawState = drawState;
+    fViewMatrix = drawState->getViewMatrix();
+    fRestoreMask = 0;
+    GrMatrix invVM;
+    bool inverted = false;
+
+    for (int s = 0; s < GrDrawState::kNumStages; ++s) {
+        if (!(explicitCoordStageMask & (1 << s)) && drawState->isStageEnabled(s)) {
+            if (!inverted && !fViewMatrix.invert(&invVM)) {
+                // sad trombone sound
+                fDrawState = NULL;
+                return;
+            } else {
+                inverted = true;
+            }
+            fRestoreMask |= (1 << s);
+            GrSamplerState* sampler = drawState->sampler(s);
+            fSamplerMatrices[s] = sampler->getMatrix();
+            sampler->preConcatMatrix(invVM);
+        }
+    }
+    drawState->viewMatrix()->reset();
+}
+
+GrDrawState::AutoDeviceCoordDraw::~AutoDeviceCoordDraw() {
+    if (NULL != fDrawState) {
+        fDrawState->setViewMatrix(fViewMatrix);
+        for (int s = 0; s < GrDrawState::kNumStages; ++s) {
+            if (fRestoreMask & (1 << s)) {
+                *fDrawState->sampler(s)->matrix() = fSamplerMatrices[s];
+            }
+        }
+    }
+}