SSSA for drawVerts, cleanup determination of when stage is enabled


Review URL: http://codereview.appspot.com/4430066/



git-svn-id: http://skia.googlecode.com/svn/trunk@1195 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gpu/include/GrContext.h b/gpu/include/GrContext.h
index 495eff4..3112112 100644
--- a/gpu/include/GrContext.h
+++ b/gpu/include/GrContext.h
@@ -619,6 +619,8 @@
 
     // sets up target to sample coverage of supersampled render target back
     // to the main render target using stage kOffscreenStage.
+    // caller should set view matrix to matrix used for this pass prior to 
+    // calling.
     void setupOffscreenAAPass2(GrDrawTarget* target,
                                const GrPaint& paint,
                                OffscreenRecord* record);
diff --git a/gpu/include/GrContext_impl.h b/gpu/include/GrContext_impl.h
index fdcb2b1..b0faa2c 100644
--- a/gpu/include/GrContext_impl.h
+++ b/gpu/include/GrContext_impl.h
@@ -17,6 +17,14 @@
 #ifndef GrContext_impl_DEFINED
 #define GrContext_impl_DEFINED
 
+struct GrContext::OffscreenRecord {
+    OffscreenRecord() { fEntry = NULL; }
+    ~OffscreenRecord() { GrAssert(NULL == fEntry); }
+
+    GrTextureEntry*                fEntry;
+    GrDrawTarget::SavedDrawState   fSavedState;
+};
+
 template <typename POS_SRC, typename TEX_SRC,
           typename COL_SRC, typename IDX_SRC>
 inline void GrContext::drawCustomVertices(const GrPaint& paint,
@@ -44,6 +52,16 @@
         layout |= GrDrawTarget::kColor_VertexLayoutBit;
     }
 
+    bool doOffscreenAA = false;
+    OffscreenRecord record;
+    if (paint.fAntiAlias &&
+        !this->getRenderTarget()->isMultisampled() &&
+        !(GrIsPrimTypeLines(primitiveType) && fGpu->supportsAALines()) &&
+        this->setupOffscreenAAPass1(target, false, &record)) {
+        doOffscreenAA = true;
+        layout |= GrDrawTarget::StagePosAsTexCoordVertexLayoutBit(kOffscreenStage);
+    }
+
     int vertexCount = posSrc.count();
     int indexCount = (NULL != idxSrc) ? idxSrc->count() : 0;
 
@@ -81,6 +99,47 @@
     } else {
         target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
     }
+
+    if (doOffscreenAA) {
+        // draw to the offscreen
+        if (NULL != indices) {
+            target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
+        } else {
+            target->drawNonIndexed(primitiveType, 0, vertexCount);
+        }
+        // When there are custom texture coordinates we can't just draw
+        // a quad to sample the offscreen. Instead we redraw the geometry to
+        // specify the texture coords. This isn't quite right either, primitives
+        // will only be eroded at the edges, not expanded into partial pixels.
+        bool useRect = 0 == (layout & GrDrawTarget::StageTexCoordVertexLayoutBit(0,0));
+        if (useRect) {
+            target->setViewMatrix(GrMatrix::I());
+        }
+        this->setupOffscreenAAPass2(target, paint, &record);
+        if (useRect) {
+            geo.set(NULL, 0, 0, 0);
+            int stages = (NULL != paint.getTexture()) ? 0x1 : 0x0;
+            stages |= (1 << kOffscreenStage);
+            GrRect dstRect(0, 0, 
+                        target->getRenderTarget()->width(),
+                        target->getRenderTarget()->height());
+                        target->drawSimpleRect(dstRect, NULL, stages);
+            target->drawSimpleRect(dstRect, NULL, stages);
+        } else {
+            if (NULL != indices) {
+                target->drawIndexed (primitiveType, 0, 0, vertexCount, indexCount);
+            } else {
+                target->drawNonIndexed(primitiveType, 0, vertexCount);
+            }
+        }
+        this->endOffscreenAA(target, &record);
+    } else {
+        if (NULL != indices) {
+            target->drawIndexed(primitiveType, 0, 0, vertexCount, indexCount);
+        } else {
+            target->drawNonIndexed(primitiveType, 0, vertexCount);
+        }
+    }
 }
 
 class GrNullTexCoordSource {
diff --git a/gpu/include/GrDrawTarget.h b/gpu/include/GrDrawTarget.h
index e021a93..cb36d3c 100644
--- a/gpu/include/GrDrawTarget.h
+++ b/gpu/include/GrDrawTarget.h
@@ -797,20 +797,16 @@
                             GrVertexLayout vertexLayout,
                             uint32_t       vertexCount,
                             uint32_t       indexCount) {
-            fTarget = target;
-            fSuccess = fTarget->reserveAndLockGeometry(vertexLayout,
-                                                       vertexCount,
-                                                       indexCount,
-                                                       &fVertices,
-                                                       &fIndices);
+            fTarget = NULL;
+            this->set(target, vertexLayout, vertexCount, indexCount);
         }
 
         AutoReleaseGeometry() {
-            fSuccess = false;
+            fTarget = NULL;
         }
 
         ~AutoReleaseGeometry() {
-            if (fSuccess) {
+            if (NULL != fTarget) {
                 fTarget->releaseReservedGeometry();
             }
         }
@@ -819,19 +815,23 @@
                  GrVertexLayout vertexLayout,
                  uint32_t       vertexCount,
                  uint32_t       indexCount) {
-            if (fSuccess) {
+            if (NULL != fTarget) {
                 fTarget->releaseReservedGeometry();
             }
             fTarget = target;
-            fSuccess = fTarget->reserveAndLockGeometry(vertexLayout,
-                                                       vertexCount,
-                                                       indexCount,
-                                                       &fVertices,
-                                                       &fIndices);
-            return fSuccess;
+            if (NULL != fTarget) {
+                if (!fTarget->reserveAndLockGeometry(vertexLayout,
+                                                     vertexCount,
+                                                     indexCount,
+                                                     &fVertices,
+                                                     &fIndices)) {
+                    fTarget = NULL;
+                }
+            }
+            return NULL != fTarget;
         }
 
-        bool succeeded() const { return fSuccess; }
+        bool succeeded() const { return NULL != fTarget; }
         void* vertices() const { return fVertices; }
         void* indices() const { return fIndices; }
 
@@ -841,7 +841,6 @@
 
     private:
         GrDrawTarget* fTarget;
-        bool          fSuccess;
         void*         fVertices;
         void*         fIndices;
     };
@@ -1020,6 +1019,15 @@
     static void VertexLayoutUnitTest();
 
 protected:
+    // given a vertex layout and a draw state, will a stage be used?
+    static bool StageWillBeUsed(int stage, GrVertexLayout layout, 
+                         const DrState& state) {
+        return NULL != state.fTextures[stage] && VertexUsesStage(stage, layout);
+    }
+
+    bool isStageEnabled(int stage) const {
+        return StageWillBeUsed(stage, fGeometrySrc.fVertexLayout, fCurrDrawState);
+    }
 
     // Helpers for GrDrawTarget subclasses that won't have private access to
     // SavedDrawState but need to peek at the state values.