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/GrContext.cpp b/src/gpu/GrContext.cpp
index 3e53c19..7e0c915 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -743,7 +743,7 @@
                                            &useVertexCoverage);
 
     if (doAA) {
-        GrDrawTarget::AutoDeviceCoordDraw adcd(target);
+        GrDrawState::AutoDeviceCoordDraw adcd(target->drawState());
         if (!adcd.succeeded()) {
             return;
         }
@@ -1034,7 +1034,7 @@
         return;
     }
 
-    GrDrawTarget::AutoDeviceCoordDraw adcd(target);
+    GrDrawState::AutoDeviceCoordDraw adcd(drawState);
     if (!adcd.succeeded()) {
         return;
     }
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];
+            }
+        }
+    }
+}
diff --git a/src/gpu/GrDrawState.h b/src/gpu/GrDrawState.h
index ca3b2c1..ea8f418 100644
--- a/src/gpu/GrDrawState.h
+++ b/src/gpu/GrDrawState.h
@@ -135,6 +135,24 @@
     GrColor getColorFilterColor() const { return fColorFilterColor; }
     SkXfermode::Mode getColorFilterMode() const { return fColorFilterMode; }
 
+    /**
+     * Constructor sets the color to be 'color' which is undone by the destructor.
+     */
+    class AutoColorRestore : public ::GrNoncopyable {
+    public:
+        AutoColorRestore(GrDrawState* drawState, GrColor color) {
+            fDrawState = drawState;
+            fOldColor = fDrawState->getColor();
+            fDrawState->setColor(color);
+        }
+        ~AutoColorRestore() {
+            fDrawState->setColor(fOldColor);
+        }
+    private:
+        GrDrawState*    fDrawState;
+        GrColor         fOldColor;
+    };
+
     /// @}
 
     ///////////////////////////////////////////////////////////////////////////
@@ -446,6 +464,11 @@
         return false;
     }
 
+    ////////////////////////////////////////////////////////////////////////////
+
+    /**
+     * TODO: Automatically handle stage matrices.
+     */
     class AutoViewMatrixRestore : public ::GrNoncopyable {
     public:
         AutoViewMatrixRestore() : fDrawState(NULL) {}
@@ -485,6 +508,31 @@
         GrMatrix fSavedMatrix;
     };
 
+    ////////////////////////////////////////////////////////////////////////////
+
+    /**
+     * This sets the view matrix to identity and adjusts stage matrices to
+     * compensate. The destructor undoes the changes, restoring the view matrix
+     * that was set before the constructor.
+     */
+    class AutoDeviceCoordDraw : ::GrNoncopyable {
+    public:
+        /**
+         * If a stage's texture matrix is applied to explicit per-vertex coords,
+         * rather than to positions, then we don't want to modify its matrix.
+         * The explicitCoordStageMask is used to specify such stages.
+         */
+        AutoDeviceCoordDraw(GrDrawState* drawState,
+                            uint32_t explicitCoordStageMask = 0);
+        bool succeeded() const { return NULL != fDrawState; }
+        ~AutoDeviceCoordDraw();
+    private:
+        GrDrawState*       fDrawState;
+        GrMatrix           fViewMatrix;
+        GrMatrix           fSamplerMatrices[GrDrawState::kNumStages];
+        int                fRestoreMask;
+    };
+
     /// @}
 
     ///////////////////////////////////////////////////////////////////////////
diff --git a/src/gpu/GrDrawTarget.cpp b/src/gpu/GrDrawTarget.cpp
index ab345bc..4c028c3 100644
--- a/src/gpu/GrDrawTarget.cpp
+++ b/src/gpu/GrDrawTarget.cpp
@@ -1160,47 +1160,6 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
-GrDrawTarget::AutoDeviceCoordDraw::AutoDeviceCoordDraw(GrDrawTarget* target,
-                                                       uint32_t explicitCoordStageMask) {
-    GrAssert(NULL != target);
-    GrDrawState* drawState = target->drawState();
-
-    fDrawTarget = target;
-    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
-                fDrawTarget = NULL;
-                return;
-            } else {
-                inverted = true;
-            }
-            fRestoreMask |= (1 << s);
-            GrSamplerState* sampler = drawState->sampler(s);
-            fSamplerMatrices[s] = sampler->getMatrix();
-            sampler->preConcatMatrix(invVM);
-        }
-    }
-    drawState->viewMatrix()->reset();
-}
-
-GrDrawTarget::AutoDeviceCoordDraw::~AutoDeviceCoordDraw() {
-    GrDrawState* drawState = fDrawTarget->drawState();
-    drawState->setViewMatrix(fViewMatrix);
-    for (int s = 0; s < GrDrawState::kNumStages; ++s) {
-        if (fRestoreMask & (1 << s)) {
-            *drawState->sampler(s)->matrix() = fSamplerMatrices[s];
-        }
-    }
-}
-
-////////////////////////////////////////////////////////////////////////////////
-
 GrDrawTarget::AutoReleaseGeometry::AutoReleaseGeometry(
                                          GrDrawTarget*  target,
                                          GrVertexLayout vertexLayout,
diff --git a/src/gpu/GrDrawTarget.h b/src/gpu/GrDrawTarget.h
index b975f69..134cccd 100644
--- a/src/gpu/GrDrawTarget.h
+++ b/src/gpu/GrDrawTarget.h
@@ -599,54 +599,6 @@
 
     ////////////////////////////////////////////////////////////////////////////
 
-    /**
-     * Sets the view matrix to I and preconcats all stage matrices enabled in
-     * mask by the view inverse. Destructor undoes these changes.
-     */
-    class AutoDeviceCoordDraw : ::GrNoncopyable {
-    public:
-        /**
-         * If a stage's texture matrix is applied to explicit per-vertex coords,
-         * rather than to positions, then we don't want to modify its matrix.
-         * The explicitCoordStageMask is used to specify such stages.
-         *
-         * TODO: Remove this when custom stage's control their own texture
-         * matrix and there is a "view matrix has changed" notification to the
-         * custom stages.
-         */
-        AutoDeviceCoordDraw(GrDrawTarget* target,
-                            uint32_t explicitCoordStageMask = 0);
-        bool succeeded() const { return NULL != fDrawTarget; }
-        ~AutoDeviceCoordDraw();
-    private:
-        GrDrawTarget*       fDrawTarget;
-        GrMatrix            fViewMatrix;
-        GrMatrix            fSamplerMatrices[GrDrawState::kNumStages];
-        int                 fRestoreMask;
-    };
-
-    ////////////////////////////////////////////////////////////////////////////
-
-    /**
-     * Constructor sets the color to be 'color' which is undone by the destructor.
-     */
-    class AutoColorRestore : public ::GrNoncopyable {
-    public:
-        AutoColorRestore(GrDrawTarget* target, GrColor color) {
-            fDrawTarget = target;
-            fOldColor = target->drawState()->getColor();
-            target->drawState()->setColor(color);
-        }
-        ~AutoColorRestore() {
-            fDrawTarget->drawState()->setColor(fOldColor);
-        }
-    private:
-        GrDrawTarget*   fDrawTarget;
-        GrColor         fOldColor;
-    };
-
-    ////////////////////////////////////////////////////////////////////////////
-
     class AutoReleaseGeometry : ::GrNoncopyable {
     public:
         AutoReleaseGeometry(GrDrawTarget*  target,
diff --git a/src/gpu/GrInOrderDrawBuffer.cpp b/src/gpu/GrInOrderDrawBuffer.cpp
index c3c1e89..eaee5c9 100644
--- a/src/gpu/GrInOrderDrawBuffer.cpp
+++ b/src/gpu/GrInOrderDrawBuffer.cpp
@@ -115,7 +115,7 @@
                 }
             }
         }
-        GrDrawTarget::AutoDeviceCoordDraw adcd(this, explicitCoordMask);
+        GrDrawState::AutoDeviceCoordDraw adcd(this->drawState(), explicitCoordMask);
         if (!adcd.succeeded()) {
             return;
         }
@@ -129,7 +129,7 @@
         // Now that the paint's color is stored in the vertices set it to
         // white so that the following code can batch all the rects regardless
         // of paint color
-        AutoColorRestore acr(this, SK_ColorWHITE);
+        GrDrawState::AutoColorRestore acr(this->drawState(), SK_ColorWHITE);
 
         // we don't want to miss an opportunity to batch rects together
         // simply because the clip has changed if the clip doesn't affect
diff --git a/src/gpu/GrSWMaskHelper.cpp b/src/gpu/GrSWMaskHelper.cpp
index 9e7012b..ec0dbf1 100644
--- a/src/gpu/GrSWMaskHelper.cpp
+++ b/src/gpu/GrSWMaskHelper.cpp
@@ -202,7 +202,7 @@
                                               const GrIRect& rect) {
     GrDrawState* drawState = target->drawState();
 
-    GrDrawTarget::AutoDeviceCoordDraw adcd(target);
+    GrDrawState::AutoDeviceCoordDraw adcd(drawState);
     if (!adcd.succeeded()) {
         return;
     }
diff --git a/src/gpu/GrSoftwarePathRenderer.cpp b/src/gpu/GrSoftwarePathRenderer.cpp
index edac161..a2c5867 100644
--- a/src/gpu/GrSoftwarePathRenderer.cpp
+++ b/src/gpu/GrSoftwarePathRenderer.cpp
@@ -75,7 +75,7 @@
 void draw_around_inv_path(GrDrawTarget* target,
                           const GrIRect& devClipBounds,
                           const GrIRect& devPathBounds) {
-    GrDrawTarget::AutoDeviceCoordDraw adcd(target);
+    GrDrawState::AutoDeviceCoordDraw adcd(target->drawState());
     if (!adcd.succeeded()) {
         return;
     }