Make GrContext track the current matrix, render target, and clip directly rather than using GrDrawState.

R=robertphillips@google.com, jvanverth@google.com

Author: bsalomon@google.com

Review URL: https://chromiumcodereview.appspot.com/15821008

git-svn-id: http://skia.googlecode.com/svn/trunk@9297 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/include/gpu/GrContext.h b/include/gpu/GrContext.h
index 560d440..cca476b 100644
--- a/include/gpu/GrContext.h
+++ b/include/gpu/GrContext.h
@@ -257,16 +257,18 @@
 
     /**
      * Sets the render target.
-     * @param target    the render target to set. (should not be NULL.)
+     * @param target    the render target to set.
      */
-    void setRenderTarget(GrRenderTarget* target);
+    void setRenderTarget(GrRenderTarget* target) {
+        fRenderTarget.reset(SkSafeRef(target));
+    }
 
     /**
      * Gets the current render target.
-     * @return the currently bound render target. Should never be NULL.
+     * @return the currently bound render target.
      */
-    const GrRenderTarget* getRenderTarget() const;
-    GrRenderTarget* getRenderTarget();
+    const GrRenderTarget* getRenderTarget() const { return fRenderTarget.get(); }
+    GrRenderTarget* getRenderTarget() { return fRenderTarget.get(); }
 
     GrAARectRenderer* getAARectRenderer() { return fAARectRenderer; }
 
@@ -321,25 +323,25 @@
      * Gets the current transformation matrix.
      * @return the current matrix.
      */
-    const SkMatrix& getMatrix() const;
+    const SkMatrix& getMatrix() const { return fViewMatrix; }
 
     /**
      * Sets the transformation matrix.
      * @param m the matrix to set.
      */
-    void setMatrix(const SkMatrix& m);
+    void setMatrix(const SkMatrix& m) { fViewMatrix = m; }
 
     /**
      * Sets the current transformation matrix to identity.
      */
-    void setIdentityMatrix();
+    void setIdentityMatrix() { fViewMatrix.reset(); }
 
     /**
      * Concats the current matrix. The passed matrix is applied before the
      * current matrix.
      * @param m the matrix to concat.
      */
-    void concatMatrix(const SkMatrix& m) const;
+    void concatMatrix(const SkMatrix& m) { fViewMatrix.preConcat(m); }
 
 
     ///////////////////////////////////////////////////////////////////////////
@@ -348,13 +350,13 @@
      * Gets the current clip.
      * @return the current clip.
      */
-    const GrClipData* getClip() const;
+    const GrClipData* getClip() const { return fClip; }
 
     /**
      * Sets the clip.
      * @param clipData  the clip to set.
      */
-    void setClip(const GrClipData* clipData);
+    void setClip(const GrClipData* clipData) { fClip = clipData; }
 
     ///////////////////////////////////////////////////////////////////////////
     // Draws
@@ -864,25 +866,28 @@
     };
     BufferedDraw fLastDrawWasBuffered;
 
-    GrGpu*              fGpu;
-    GrDrawState*        fDrawState;
+    GrGpu*                          fGpu;
+    SkMatrix                        fViewMatrix;
+    SkAutoTUnref<GrRenderTarget>    fRenderTarget;
+    const GrClipData*               fClip;  // TODO: make this ref counted
+    GrDrawState*                    fDrawState;
 
-    GrResourceCache*    fTextureCache;
-    GrFontCache*        fFontCache;
+    GrResourceCache*                fTextureCache;
+    GrFontCache*                    fFontCache;
 
-    GrPathRendererChain*        fPathRendererChain;
-    GrSoftwarePathRenderer*     fSoftwarePathRenderer;
+    GrPathRendererChain*            fPathRendererChain;
+    GrSoftwarePathRenderer*         fSoftwarePathRenderer;
 
-    GrVertexBufferAllocPool*    fDrawBufferVBAllocPool;
-    GrIndexBufferAllocPool*     fDrawBufferIBAllocPool;
-    GrInOrderDrawBuffer*        fDrawBuffer;
+    GrVertexBufferAllocPool*        fDrawBufferVBAllocPool;
+    GrIndexBufferAllocPool*         fDrawBufferIBAllocPool;
+    GrInOrderDrawBuffer*            fDrawBuffer;
 
-    GrAARectRenderer*           fAARectRenderer;
-    GrOvalRenderer*             fOvalRenderer;
+    GrAARectRenderer*               fAARectRenderer;
+    GrOvalRenderer*                 fOvalRenderer;
 
-    bool                        fDidTestPMConversions;
-    int                         fPMToUPMConversion;
-    int                         fUPMToPMConversion;
+    bool                            fDidTestPMConversions;
+    int                             fPMToUPMConversion;
+    int                             fUPMToPMConversion;
 
     struct CleanUpData {
         PFCleanUpFunc fFunc;
diff --git a/include/gpu/GrEffectStage.h b/include/gpu/GrEffectStage.h
index 07294e1..7b8852b 100644
--- a/include/gpu/GrEffectStage.h
+++ b/include/gpu/GrEffectStage.h
@@ -53,9 +53,9 @@
         return fCoordChangeMatrix == other.fCoordChangeMatrix;
     }
 
-    bool operator !=(const GrEffectStage& s) const { return !(*this == s); }
+    bool operator!= (const GrEffectStage& s) const { return !(*this == s); }
 
-    GrEffectStage& operator =(const GrEffectStage& other) {
+    GrEffectStage& operator= (const GrEffectStage& other) {
         GrSafeAssign(fEffectRef, other.fEffectRef);
         fCoordChangeMatrixSet = other.fCoordChangeMatrixSet;
         if (NULL != fEffectRef && fCoordChangeMatrixSet) {
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 751d869..5f255a6 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -91,6 +91,7 @@
     ++THREAD_INSTANCE_COUNT;
     fDrawState = NULL;
     fGpu = NULL;
+    fClip = NULL;
     fPathRendererChain = NULL;
     fSoftwarePathRenderer = NULL;
     fTextureCache = NULL;
@@ -100,6 +101,7 @@
     fDrawBufferIBAllocPool = NULL;
     fAARectRenderer = NULL;
     fOvalRenderer = NULL;
+    fViewMatrix.reset();
 }
 
 bool GrContext::init(GrBackend backend, GrBackendContext backendContext) {
@@ -594,18 +596,6 @@
     return true;
 }
 
-////////////////////////////////////////////////////////////////////////////////
-
-const GrClipData* GrContext::getClip() const {
-    return fGpu->getClip();
-}
-
-void GrContext::setClip(const GrClipData* clipData) {
-    fGpu->setClip(clipData);
-
-    fDrawState->setState(GrDrawState::kClip_StateBit,
-                         clipData && clipData->fClipStack && !clipData->fClipStack->isWideOpen());
-}
 
 ////////////////////////////////////////////////////////////////////////////////
 
@@ -630,7 +620,7 @@
     // map the four corners and bound them with a new rect. This will not
     // produce a correct result for some perspective matrices.
     if (!this->getMatrix().hasPerspective()) {
-        if (!fDrawState->getViewInverse(&inverse)) {
+        if (!fViewMatrix.invert(&inverse)) {
             GrPrintf("Could not invert matrix\n");
             return;
         }
@@ -782,7 +772,7 @@
     SkMatrix combinedMatrix;
     bool useVertexCoverage;
     bool needAA = paint.isAntiAlias() &&
-                  !this->getRenderTarget()->isMultisampled();
+                  !target->getDrawState().getRenderTarget()->isMultisampled();
     bool doAA = needAA && apply_aa_to_rect(target, rect, width, matrix,
                                            &combinedMatrix, &devRect,
                                            &useVertexCoverage);
@@ -966,7 +956,7 @@
     GrDrawState::AutoStageDisable atr(fDrawState);
 
     bool useAA = paint.isAntiAlias() &&
-                 !this->getRenderTarget()->isMultisampled() &&
+                 !target->getDrawState().getRenderTarget()->isMultisampled() &&
                  !disable_coverage_aa_for_blend(target);
 
     if (!fOvalRenderer->drawSimpleRRect(target, this, useAA, rect, stroke)) {
@@ -986,7 +976,7 @@
     GrDrawState::AutoStageDisable atr(fDrawState);
 
     bool useAA = paint.isAntiAlias() &&
-                 !this->getRenderTarget()->isMultisampled() &&
+                 !target->getDrawState().getRenderTarget()->isMultisampled() &&
                  !disable_coverage_aa_for_blend(target);
 
     if (!fOvalRenderer->drawOval(target, this, useAA, oval, stroke)) {
@@ -1142,7 +1132,9 @@
     } else {
         this->flushDrawBuffer();
     }
+    // TODO: Remove this flag
     if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
+        fGpu->drawState()->setRenderTarget(this->getRenderTarget());
         fGpu->forceRenderTargetFlush();
     }
 }
@@ -1256,7 +1248,7 @@
     ASSERT_OWNED_RESOURCE(target);
 
     if (NULL == target) {
-        target = fDrawState->getRenderTarget();
+        target = fRenderTarget.get();
         if (NULL == target) {
             return false;
         }
@@ -1444,7 +1436,7 @@
     ASSERT_OWNED_RESOURCE(target);
 
     if (NULL == target) {
-        target = fDrawState->getRenderTarget();
+        target = fRenderTarget.get();
         if (NULL == target) {
             return false;
         }
@@ -1562,24 +1554,35 @@
         this->flushDrawBuffer();
         fLastDrawWasBuffered = kNo_BufferedDraw;
     }
+    ASSERT_OWNED_RESOURCE(fRenderTarget.get());
     if (NULL != paint) {
         GrAssert(fDrawState->stagesDisabled());
-        fDrawState->setFromPaint(*paint);
+        fDrawState->setFromPaint(*paint, fViewMatrix, fRenderTarget.get());
 #if GR_DEBUG_PARTIAL_COVERAGE_CHECK
         if ((paint->hasMask() || 0xff != paint->fCoverage) &&
             !fGpu->canApplyCoverage()) {
             GrPrintf("Partial pixel coverage will be incorrectly blended.\n");
         }
 #endif
+    } else {
+        fDrawState->reset();
+        *fDrawState->viewMatrix() = fViewMatrix;
+        fDrawState->setRenderTarget(fRenderTarget.get());
     }
+    GrDrawTarget* target;
     if (kYes_BufferedDraw == buffered) {
-        fDrawBuffer->setClip(fGpu->getClip());
         fLastDrawWasBuffered = kYes_BufferedDraw;
-        return fDrawBuffer;
+        target = fDrawBuffer;
     } else {
         GrAssert(kNo_BufferedDraw == buffered);
-        return fGpu;
+        fLastDrawWasBuffered = kNo_BufferedDraw;
+        target = fGpu;
     }
+    fDrawState->setState(GrDrawState::kClip_StateBit, NULL != fClip &&
+                                                     !fClip->fClipStack->isWideOpen());
+    target->setClip(fClip);
+    GrAssert(fDrawState == target->drawState());
+    return target;
 }
 
 /*
@@ -1617,39 +1620,10 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
-void GrContext::setRenderTarget(GrRenderTarget* target) {
-    ASSERT_OWNED_RESOURCE(target);
-    fDrawState->setRenderTarget(target);
-}
-
-GrRenderTarget* GrContext::getRenderTarget() {
-    return fDrawState->getRenderTarget();
-}
-
-const GrRenderTarget* GrContext::getRenderTarget() const {
-    return fDrawState->getRenderTarget();
-}
-
 bool GrContext::isConfigRenderable(GrPixelConfig config) const {
     return fGpu->isConfigRenderable(config);
 }
 
-const SkMatrix& GrContext::getMatrix() const {
-    return fDrawState->getViewMatrix();
-}
-
-void GrContext::setMatrix(const SkMatrix& m) {
-    fDrawState->setViewMatrix(m);
-}
-
-void GrContext::setIdentityMatrix() {
-    fDrawState->viewMatrix()->reset();
-}
-
-void GrContext::concatMatrix(const SkMatrix& m) const {
-    fDrawState->preConcatViewMatrix(m);
-}
-
 static inline intptr_t setOrClear(intptr_t bits, int shift, intptr_t pred) {
     intptr_t mask = 1 << shift;
     if (pred) {
diff --git a/src/gpu/GrDrawState.cpp b/src/gpu/GrDrawState.cpp
index a3850b0..2cc50f7 100644
--- a/src/gpu/GrDrawState.cpp
+++ b/src/gpu/GrDrawState.cpp
@@ -8,7 +8,7 @@
 #include "GrDrawState.h"
 #include "GrPaint.h"
 
-void GrDrawState::setFromPaint(const GrPaint& paint) {
+void GrDrawState::setFromPaint(const GrPaint& paint, const SkMatrix& vm, GrRenderTarget* rt) {
     for (int i = 0; i < GrPaint::kMaxColorStages; ++i) {
         int s = i + GrPaint::kFirstColorStage;
         if (paint.isColorStageEnabled(i)) {
@@ -34,8 +34,18 @@
         this->disableStage(s);
     }
 
-    this->setColor(paint.getColor());
+    this->setRenderTarget(rt);
 
+    fCommon.fViewMatrix = vm;
+
+    // These have no equivalent in GrPaint, set them to defaults
+    fCommon.fBlendConstant = 0x0;
+    fCommon.fCoverage = 0xffffffff;
+    fCommon.fDrawFace = kBoth_DrawFace;
+    fCommon.fStencilSettings.setDisabled();
+    this->resetStateFlags();
+
+    this->setColor(paint.getColor());
     this->setState(GrDrawState::kDither_StateBit, paint.isDither());
     this->setState(GrDrawState::kHWAntialias_StateBit, paint.isAntiAlias());
 
diff --git a/src/gpu/GrDrawState.h b/src/gpu/GrDrawState.h
index 80ad28e..bf3ef74 100644
--- a/src/gpu/GrDrawState.h
+++ b/src/gpu/GrDrawState.h
@@ -95,12 +95,12 @@
     }
 
     /**
-     * Initializes the GrDrawState based on a GrPaint. Note that GrDrawState
-     * encompasses more than GrPaint. Aspects of GrDrawState that have no
-     * GrPaint equivalents are not modified. GrPaint has fewer stages than
-     * GrDrawState. The extra GrDrawState stages are disabled.
+     * Initializes the GrDrawState based on a GrPaint, view matrix and render target. Note that
+     * GrDrawState encompasses more than GrPaint. Aspects of GrDrawState that have no GrPaint
+     * equivalents are set to default values. GrPaint has fewer stages than GrDrawState. The extra
+     * GrDrawState stages are disabled.
      */
-    void setFromPaint(const GrPaint& paint);
+    void setFromPaint(const GrPaint& , const SkMatrix& viewMatrix, GrRenderTarget*);
 
     ///////////////////////////////////////////////////////////////////////////
     /// @name Vertex Attributes