Make all GrContext draws go through the draw buffer.
authorbsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 16 Aug 2012 18:36:06 +0000 (18:36 +0000)
committerbsalomon@google.com <bsalomon@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81>
Thu, 16 Aug 2012 18:36:06 +0000 (18:36 +0000)
Review URL: http://codereview.appspot.com/6462069/

git-svn-id: http://skia.googlecode.com/svn/trunk@5136 2bbb7eff-a529-9590-31e7-b0007b416f81

include/gpu/GrConfig.h
include/gpu/GrContext.h
include/gpu/GrUserConfig.h
src/gpu/GrContext.cpp

index eecce79..ffab237 100644 (file)
@@ -337,6 +337,14 @@ inline void GrCrash(const char* msg) { GrPrintf(msg); GrAlwaysAssert(false); }
 #endif
 
 /**
+ *  GR_DISABLE_DRAW_BUFFERING prevents GrContext from queueing draws in a
+ *  GrInOrderDrawBuffer.
+ */
+#if !defined(GR_DISABLE_DRAW_BUFFERING)
+    #define GR_DISABLE_DRAW_BUFFERING 0
+#endif
+
+/**
  *  GR_AGGRESSIVE_SHADER_OPTS controls how aggressively shaders are optimized
  *  for special cases. On systems where program changes are expensive this
  *  may not be advantageous. Consecutive draws may no longer use the same
index 0c4d73c..c32b908 100644 (file)
@@ -748,12 +748,12 @@ public:
                                     bool allowSW);
 
 private:
-    // used to keep track of when we need to flush the draw buffer
-    enum DrawCategory {
-        kBuffered_DrawCategory,      // last draw was inserted in draw buffer
-        kUnbuffered_DrawCategory,    // last draw was not inserted in the draw buffer
+    // Used to indicate whether a draw should be performed immediately or queued in fDrawBuffer.
+    enum BufferedDraw {
+        kYes_BufferedDraw,
+        kNo_BufferedDraw,
     };
-    DrawCategory fLastDrawCategory;
+    BufferedDraw fLastDrawWasBuffered;
 
     GrGpu*              fGpu;
     GrDrawState*        fDrawState;
@@ -778,7 +778,7 @@ private:
 
     void setPaint(const GrPaint& paint);
 
-    GrDrawTarget* prepareToDraw(const GrPaint& paint, DrawCategory drawType);
+    GrDrawTarget* prepareToDraw(const GrPaint&, BufferedDraw);
 
     void internalDrawPath(const GrPaint& paint, const SkPath& path,
                           GrPathFill fill, const GrPoint* translate);
index 657f74f..d514486 100644 (file)
@@ -22,8 +22,8 @@
 #endif
 
 /*
- *  To diagnose texture cache performance, define this to 1 if you want to see
- *  a log statement everytime we upload an image to create a texture.
+ * To diagnose texture cache performance, define this to 1 if you want to see
+ * a log statement everytime we upload an image to create a texture.
  */
 //#define GR_DUMP_TEXTURE_UPLOAD    1
 
 //#define GR_STATIC_RECT_VB 1
 
 /*
+ * This causes the GrContext to execute all draws immediately in the 3D API
+ * rather than internally queuing draws.
+ */
+//#define GR_DISABLE_DRAW_BUFFERING 1
+
+/*
  * This causes more aggressive shader optimization. May hurt performance if
  * switching shaders is expensive.
  */
index a8b6808..895040d 100644 (file)
 SK_DEFINE_INST_COUNT(GrContext)
 SK_DEFINE_INST_COUNT(GrDrawState)
 
-#define DEFER_TEXT_RENDERING 1
-
-#define DEFER_PATHS 1
-
-#define BATCH_RECT_TO_RECT (1 && !GR_STATIC_RECT_VB)
+// It can be useful to set this to kNo_BufferedDraw to test whether a bug is caused by using the
+// InOrderDrawBuffer, to compare performance of using/not using InOrderDrawBuffer, or to make
+// debugging easier.
+#define DEFAULT_BUFFERING (GR_DISABLE_DRAW_BUFFERING ? kNo_BufferedDraw : kYes_BufferedDraw)
 
 #define MAX_BLUR_SIGMA 4.0f
 
@@ -55,9 +54,8 @@ static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
 static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
 static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
 
-// path rendering is the only thing we defer today that uses non-static indices
-static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = DEFER_PATHS ? 1 << 11 : 0;
-static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = DEFER_PATHS ? 4 : 0;
+static const size_t DRAW_BUFFER_IBPOOL_BUFFER_SIZE = 1 << 11;
+static const int DRAW_BUFFER_IBPOOL_PREALLOC_BUFFERS = 4;
 
 #define ASSERT_OWNED_RESOURCE(R) GrAssert(!(R) || (R)->getContext() == this)
 
@@ -740,7 +738,7 @@ void GrContext::drawRect(const GrPaint& paint,
                          const GrMatrix* matrix) {
     SK_TRACE_EVENT0("GrContext::drawRect");
 
-    GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
+    GrDrawTarget* target = this->prepareToDraw(paint, DEFAULT_BUFFERING);
     GrDrawState::AutoStageDisable atr(fDrawState);
 
     GrRect devRect = rect;
@@ -857,10 +855,9 @@ void GrContext::drawRectToRect(const GrPaint& paint,
         return;
     }
 
-    GR_STATIC_ASSERT(!BATCH_RECT_TO_RECT || !GR_STATIC_RECT_VB);
+    GrDrawTarget* target = this->prepareToDraw(paint, DEFAULT_BUFFERING);
 
 #if GR_STATIC_RECT_VB
-    GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
     GrDrawState::AutoStageDisable atr(fDrawState);
     GrDrawState* drawState = target->drawState();
     GrDrawState::AutoViewMatrixRestore avmr(drawState);
@@ -899,13 +896,6 @@ void GrContext::drawRectToRect(const GrPaint& paint,
     target->setVertexSourceToBuffer(0, sqVB);
     target->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
 #else
-
-    GrDrawTarget* target;
-#if BATCH_RECT_TO_RECT
-    target = this->prepareToDraw(paint, kBuffered_DrawCategory);
-#else
-    target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
-#endif
     GrDrawState::AutoStageDisable atr(fDrawState);
 
     const GrRect* srcRects[GrDrawState::kNumStages] = {NULL};
@@ -929,7 +919,7 @@ void GrContext::drawVertices(const GrPaint& paint,
 
     GrDrawTarget::AutoReleaseGeometry geo;
 
-    GrDrawTarget* target = this->prepareToDraw(paint, kUnbuffered_DrawCategory);
+    GrDrawTarget* target = this->prepareToDraw(paint, DEFAULT_BUFFERING);
     GrDrawState::AutoStageDisable atr(fDrawState);
 
     GrVertexLayout layout = 0;
@@ -1041,9 +1031,7 @@ void GrContext::drawOval(const GrPaint& paint,
         return;
     }
 
-    DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
-                                            kUnbuffered_DrawCategory;
-    GrDrawTarget* target = this->prepareToDraw(paint, category);
+    GrDrawTarget* target = this->prepareToDraw(paint, DEFAULT_BUFFERING);
     GrDrawState* drawState = target->drawState();
     GrDrawState::AutoStageDisable atr(fDrawState);
     const GrMatrix vm = drawState->getViewMatrix();
@@ -1141,9 +1129,7 @@ void GrContext::internalDrawPath(const GrPaint& paint, const SkPath& path,
     // cache. This presents a potential hazard for buffered drawing. However,
     // the writePixels that uploads to the scratch will perform a flush so we're
     // OK.
-    DrawCategory category = (DEFER_PATHS) ? kBuffered_DrawCategory :
-                                            kUnbuffered_DrawCategory;
-    GrDrawTarget* target = this->prepareToDraw(paint, category);
+    GrDrawTarget* target = this->prepareToDraw(paint, DEFAULT_BUFFERING);
     GrDrawState::AutoStageDisable atr(fDrawState);
 
     bool prAA = paint.fAntiAlias && !this->getRenderTarget()->isMultisampled();
@@ -1594,27 +1580,20 @@ void GrContext::setPaint(const GrPaint& paint) {
 #endif
 }
 
-GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint,
-                                       DrawCategory category) {
-    if (category != fLastDrawCategory) {
+GrDrawTarget* GrContext::prepareToDraw(const GrPaint& paint, BufferedDraw buffered) {
+    if (kNo_BufferedDraw == buffered && kYes_BufferedDraw == fLastDrawWasBuffered) {
         this->flushDrawBuffer();
-        fLastDrawCategory = category;
+        fLastDrawWasBuffered = kNo_BufferedDraw;
     }
     this->setPaint(paint);
-    GrDrawTarget* target = fGpu;
-    switch (category) {
-        case kUnbuffered_DrawCategory:
-            target = fGpu;
-            break;
-        case kBuffered_DrawCategory:
-            target = fDrawBuffer;
-            fDrawBuffer->setClip(fGpu->getClip());
-            break;
-        default:
-            GrCrash("Unexpected DrawCategory.");
-            break;
+    if (kYes_BufferedDraw == buffered) {
+        fDrawBuffer->setClip(fGpu->getClip());
+        fLastDrawWasBuffered = kYes_BufferedDraw;
+        return fDrawBuffer;
+    } else {
+        GrAssert(kNo_BufferedDraw == buffered);
+        return fGpu;
     }
-    return target;
 }
 
 /*
@@ -1711,7 +1690,7 @@ GrContext::GrContext(GrGpu* gpu) {
                                 MAX_TEXTURE_CACHE_BYTES));
     fFontCache = SkNEW_ARGS(GrFontCache, (fGpu));
 
-    fLastDrawCategory = kUnbuffered_DrawCategory;
+    fLastDrawWasBuffered = kNo_BufferedDraw;
 
     fDrawBuffer = NULL;
     fDrawBufferVBAllocPool = NULL;
@@ -1728,7 +1707,6 @@ void GrContext::setupDrawBuffer() {
     GrAssert(NULL == fDrawBufferVBAllocPool);
     GrAssert(NULL == fDrawBufferIBAllocPool);
 
-#if DEFER_TEXT_RENDERING || BATCH_RECT_TO_RECT || DEFER_PATHS
     fDrawBufferVBAllocPool =
         SkNEW_ARGS(GrVertexBufferAllocPool, (fGpu, false,
                                     DRAW_BUFFER_VBPOOL_BUFFER_SIZE,
@@ -1741,11 +1719,8 @@ void GrContext::setupDrawBuffer() {
     fDrawBuffer = SkNEW_ARGS(GrInOrderDrawBuffer, (fGpu,
                                           fDrawBufferVBAllocPool,
                                           fDrawBufferIBAllocPool));
-#endif
 
-#if BATCH_RECT_TO_RECT
     fDrawBuffer->setQuadIndexBuffer(this->getQuadIndexBuffer());
-#endif
     if (fDrawBuffer) {
         fDrawBuffer->setAutoFlushTarget(fGpu);
         fDrawBuffer->setDrawState(fDrawState);
@@ -1753,11 +1728,7 @@ void GrContext::setupDrawBuffer() {
 }
 
 GrDrawTarget* GrContext::getTextTarget(const GrPaint& paint) {
-#if DEFER_TEXT_RENDERING
-    return prepareToDraw(paint, kBuffered_DrawCategory);
-#else
-    return prepareToDraw(paint, kUnbuffered_DrawCategory);
-#endif
+    return prepareToDraw(paint, DEFAULT_BUFFERING);
 }
 
 const GrIndexBuffer* GrContext::getQuadIndexBuffer() const {