Make flush discardable and lazily reset context

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



git-svn-id: http://skia.googlecode.com/svn/trunk@914 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gpu/src/GrContext.cpp b/gpu/src/GrContext.cpp
index a047895..c6fa618 100644
--- a/gpu/src/GrContext.cpp
+++ b/gpu/src/GrContext.cpp
@@ -560,9 +560,14 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
-void GrContext::flush(bool flushRenderTarget) {
-    flushDrawBuffer();
-    if (flushRenderTarget) {
+void GrContext::flush(int flagsBitfield) {
+    if (kDiscard_FlushBit & flagsBitfield) {
+        fDrawBuffer->reset();
+    } else {
+        flushDrawBuffer();
+    }
+
+    if (kForceCurrentRenderTarget_FlushBit & flagsBitfield) {
         fGpu->forceRenderTargetFlush();
     }
 }
@@ -683,7 +688,7 @@
 ////////////////////////////////////////////////////////////////////////////////
 
 void GrContext::resetContext() {
-    fGpu->resetContext();
+    fGpu->markContextDirty();
 }
 
 void GrContext::setRenderTarget(GrRenderTarget* target) {
diff --git a/gpu/src/GrGpu.cpp b/gpu/src/GrGpu.cpp
index 045dcd0..d41005b 100644
--- a/gpu/src/GrGpu.cpp
+++ b/gpu/src/GrGpu.cpp
@@ -71,6 +71,7 @@
                  fQuadIndexBuffer(NULL),
                  fUnitSquareVertexBuffer(NULL),
                  fPathRenderer(NULL),
+                 fContextIsDirty(true),
                  fVertexPoolInUse(false),
                  fIndexPoolInUse(false) {
 #if GR_DEBUG
@@ -98,6 +99,54 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
+GrTexture* GrGpu::createTexture(const TextureDesc& desc,
+                                const void* srcData, size_t rowBytes) {
+    this->handleDirtyContext();
+    return this->createTextureHelper(desc, srcData, rowBytes);
+}
+
+GrRenderTarget* GrGpu::createPlatformRenderTarget(intptr_t platformRenderTarget,
+                                                  int stencilBits,
+                                                  int width, int height) {
+    this->handleDirtyContext();
+    return this->createPlatformRenderTargetHelper(platformRenderTarget,
+                                                  stencilBits,
+                                                  width, height);
+}
+
+GrRenderTarget* GrGpu::createRenderTargetFrom3DApiState() {
+    this->handleDirtyContext();
+    return this->createRenderTargetFrom3DApiStateHelper();
+}
+
+GrVertexBuffer* GrGpu::createVertexBuffer(uint32_t size, bool dynamic) {
+    this->handleDirtyContext();
+    return this->createVertexBufferHelper(size, dynamic);
+}
+
+GrIndexBuffer* GrGpu::createIndexBuffer(uint32_t size, bool dynamic) {
+    this->handleDirtyContext();
+    return this->createIndexBufferHelper(size, dynamic);
+}
+
+void GrGpu::eraseColor(GrColor color) {
+    this->handleDirtyContext();
+    this->eraseColorHelper(color);
+}
+
+void GrGpu::forceRenderTargetFlush() {
+    this->handleDirtyContext();
+    this->forceRenderTargetFlushHelper();
+}
+
+bool GrGpu::readPixels(int left, int top, int width, int height,
+                       GrTexture::PixelConfig config, void* buffer) {
+    this->handleDirtyContext();
+    return this->readPixelsHelper(left, top, width, height, config, buffer);
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
 static const int MAX_QUADS = 1 << 12; // max possible: (1 << 14) - 1;
 
 GR_STATIC_ASSERT(4 * MAX_QUADS <= 65535);
@@ -184,7 +233,8 @@
     0,                           0
 };
 
-// converts special stencil func to
+// mapping of clip-respecting stencil funcs to normal stencil funcs
+// mapping depends on whether stencil-clipping is in effect.
 static const GrStencilFunc gGrClipToNormalStencilFunc[2][kClipStencilFuncCount] = {
     {// Stencil-Clipping is DISABLED, effectively always inside the clip
         // In the Clip Funcs
@@ -445,7 +495,9 @@
     GrAssert(kReserved_GeometrySrcType != fGeometrySrc.fIndexSrc ||
              fReservedGeometry.fLocked);
 
-    if (!setupClipAndFlushState(type)) {
+    this->handleDirtyContext();
+
+    if (!this->setupClipAndFlushState(type)) {
         return;
     }
 
@@ -469,7 +521,9 @@
     GrAssert(kReserved_GeometrySrcType != fGeometrySrc.fVertexSrc ||
              fReservedGeometry.fLocked);
 
-    if (!setupClipAndFlushState(type)) {
+    this->handleDirtyContext();
+
+    if (!this->setupClipAndFlushState(type)) {
         return;
     }
 #if GR_COLLECT_STATS
diff --git a/gpu/src/GrGpuGL.cpp b/gpu/src/GrGpuGL.cpp
index 3ee524b..b0d5e73 100644
--- a/gpu/src/GrGpuGL.cpp
+++ b/gpu/src/GrGpuGL.cpp
@@ -152,8 +152,6 @@
 
     GrGLInitExtensions(&fExts);
 
-    resetContextHelper();
-
     resetDirtyFlags();
 
     GLint maxTextureUnits;
@@ -427,8 +425,8 @@
 GrGpuGL::~GrGpuGL() {
 }
 
-void GrGpuGL::resetContextHelper() {
-// We detect cases when blending is effectively off
+void GrGpuGL::resetContext() {
+    // We detect cases when blending is effectively off
     fHWBlendDisabled = false;
     GR_GL(Enable(GL_BLEND));
 
@@ -491,12 +489,7 @@
     fHWDrawState.fRenderTarget = NULL;
 }
 
-void GrGpuGL::resetContext() {
-    INHERITED::resetContext();
-    resetContextHelper();
-}
-
-GrRenderTarget* GrGpuGL::createPlatformRenderTarget(
+GrRenderTarget* GrGpuGL::createPlatformRenderTargetHelper(
                                                 intptr_t platformRenderTarget,
                                                 int stencilBits,
                                                 int width,
@@ -520,7 +513,7 @@
     return new GrGLRenderTarget(rtIDs, stencilBits, viewport, NULL, this);
 }
 
-GrRenderTarget* GrGpuGL::createRenderTargetFrom3DApiState() {
+GrRenderTarget* GrGpuGL::createRenderTargetFrom3DApiStateHelper() {
 
     GrGLRenderTarget::GLRenderTargetIDs rtIDs;
 
@@ -575,8 +568,9 @@
 }
 #endif
 
-GrTexture* GrGpuGL::createTexture(const TextureDesc& desc,
-                                  const void* srcData, size_t rowBytes) {
+GrTexture* GrGpuGL::createTextureHelper(const TextureDesc& desc,
+                                        const void* srcData,
+                                        size_t rowBytes) {
 
 #if GR_COLLECT_STATS
     ++fStats.fTextureCreateCnt;
@@ -988,7 +982,7 @@
     return tex;
 }
 
-GrVertexBuffer* GrGpuGL::createVertexBuffer(uint32_t size, bool dynamic) {
+GrVertexBuffer* GrGpuGL::createVertexBufferHelper(uint32_t size, bool dynamic) {
     GLuint id;
     GR_GL(GenBuffers(1, &id));
     if (id) {
@@ -1012,7 +1006,7 @@
     return NULL;
 }
 
-GrIndexBuffer* GrGpuGL::createIndexBuffer(uint32_t size, bool dynamic) {
+GrIndexBuffer* GrGpuGL::createIndexBufferHelper(uint32_t size, bool dynamic) {
     GLuint id;
     GR_GL(GenBuffers(1, &id));
     if (id) {
@@ -1066,7 +1060,7 @@
     }
 }
 
-void GrGpuGL::eraseColor(GrColor color) {
+void GrGpuGL::eraseColorHelper(GrColor color) {
     if (NULL == fCurrDrawState.fRenderTarget) {
         return;
     }
@@ -1121,12 +1115,12 @@
     fHWDrawState.fStencilSettings.invalidate();
 }
 
-void GrGpuGL::forceRenderTargetFlush() {
+void GrGpuGL::forceRenderTargetFlushHelper() {
     flushRenderTarget();
 }
 
-bool GrGpuGL::readPixels(int left, int top, int width, int height,
-                         GrTexture::PixelConfig config, void* buffer) {
+bool GrGpuGL::readPixelsHelper(int left, int top, int width, int height,
+                               GrTexture::PixelConfig config, void* buffer) {
     GLenum internalFormat;  // we don't use this for glReadPixels
     GLenum format;
     GLenum type;
@@ -1207,7 +1201,7 @@
 
 #define SWAP_PER_DRAW 0
 
-#if SWAP_PER_DRAW 
+#if SWAP_PER_DRAW
     #if GR_MAC_BUILD
         #include <AGL/agl.h>
     #elif GR_WIN32_BUILD
diff --git a/gpu/src/GrGpuGL.h b/gpu/src/GrGpuGL.h
index a2905c5..ab504ad 100644
--- a/gpu/src/GrGpuGL.h
+++ b/gpu/src/GrGpuGL.h
@@ -31,28 +31,6 @@
             GrGpuGL();
     virtual ~GrGpuGL();
 
-    // overrides from GrGpu
-    virtual void resetContext();
-
-    virtual GrTexture* createTexture(const TextureDesc& desc,
-                                     const void* srcData, size_t rowBytes);
-    virtual GrVertexBuffer* createVertexBuffer(uint32_t size, bool dynamic);
-    virtual GrIndexBuffer* createIndexBuffer(uint32_t size, bool dynamic);
-
-    virtual GrRenderTarget* createPlatformRenderTarget(
-                                                 intptr_t platformRenderTarget,
-                                                 int stencilBits,
-                                                 int width, int height);
-
-    virtual GrRenderTarget* createRenderTargetFrom3DApiState();
-
-    virtual void eraseColor(GrColor color);
-
-    virtual void forceRenderTargetFlush();
-
-    virtual bool readPixels(int left, int top, int width, int height,
-                            GrTexture::PixelConfig, void* buffer);
-
     /**
      * Gets the struct containing the GL extensions for the context
      * underlying the GrGpuGL
@@ -97,6 +75,31 @@
     GrGLExts fExts;
 
     // GrGpu overrides
+    // overrides from GrGpu
+    virtual void resetContext();
+
+    virtual GrTexture* createTextureHelper(const TextureDesc& desc,
+                                           const void* srcData,
+                                           size_t rowBytes);
+    virtual GrVertexBuffer* createVertexBufferHelper(uint32_t size,
+                                                     bool dynamic);
+    virtual GrIndexBuffer* createIndexBufferHelper(uint32_t size,
+                                                   bool dynamic);
+
+    virtual GrRenderTarget* createPlatformRenderTargetHelper(
+                                                 intptr_t platformRenderTarget,
+                                                 int stencilBits,
+                                                 int width, int height);
+
+    virtual GrRenderTarget* createRenderTargetFrom3DApiStateHelper();
+
+    virtual void eraseColorHelper(GrColor color);
+
+    virtual void forceRenderTargetFlushHelper();
+
+    virtual bool readPixelsHelper(int left, int top, int width, int height,
+                                  GrTexture::PixelConfig, void* buffer);
+
     virtual void drawIndexedHelper(GrPrimitiveType type,
                                    uint32_t startVertex,
                                    uint32_t startIndex,
@@ -140,8 +143,6 @@
                                         const GrSamplerState& sampler);
 
 private:
-    void resetContextHelper();
-
     // notify callbacks to update state tracking when related
     // objects are bound to GL or deleted outside of the class
     void notifyVertexBufferBind(const GrGLVertexBuffer* buffer);
@@ -190,4 +191,4 @@
     typedef GrGpu INHERITED;
 };
 
-#endif
\ No newline at end of file
+#endif
diff --git a/gpu/src/GrGpuGLFixed.cpp b/gpu/src/GrGpuGLFixed.cpp
index 516381e..695b22c 100644
--- a/gpu/src/GrGpuGLFixed.cpp
+++ b/gpu/src/GrGpuGLFixed.cpp
@@ -58,7 +58,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 GrGpuGLFixed::GrGpuGLFixed() {
-    resetContextHelper();
 }
 
 GrGpuGLFixed::~GrGpuGLFixed() {
@@ -66,10 +65,7 @@
 
 void GrGpuGLFixed::resetContext() {
     INHERITED::resetContext();
-    resetContextHelper();
-}
 
-void GrGpuGLFixed::resetContextHelper() {
     GR_GL(Disable(GL_TEXTURE_2D));
 
     for (int s = 0; s < kNumStages; ++s) {
@@ -204,11 +200,11 @@
                 }
 
                 if (((1 << s) & fDirtyFlags.fTextureChangedMask) ||
-                    (fHWDrawState.fSamplerStates[s].getMatrix() != 
+                    (fHWDrawState.fSamplerStates[s].getMatrix() !=
                      getSamplerMatrix(s))) {
 
                     GrMatrix texMat = getSamplerMatrix(s);
-                    AdjustTextureMatrix(texture, 
+                    AdjustTextureMatrix(texture,
                                         GrSamplerState::kNormal_SampleMode,
                                         &texMat);
                     GrGpuMatrix glm;
diff --git a/gpu/src/GrGpuGLFixed.h b/gpu/src/GrGpuGLFixed.h
index 5b81ea6..077b6e2 100644
--- a/gpu/src/GrGpuGLFixed.h
+++ b/gpu/src/GrGpuGLFixed.h
@@ -26,8 +26,6 @@
              GrGpuGLFixed();
     virtual ~GrGpuGLFixed();
 
-    virtual void resetContext();
-
 protected:
     // overrides from GrGpu
     virtual bool flushGraphicsState(GrPrimitiveType type);
@@ -37,7 +35,7 @@
                                int indexCount);
 
 private:
-    void resetContextHelper();
+    virtual void resetContext();
 
     // Helpers to make code more readable
     const GrMatrix& getHWSamplerMatrix(int stage) const {
diff --git a/gpu/src/GrGpuGLShaders2.cpp b/gpu/src/GrGpuGLShaders2.cpp
index 77847e9..f79e9c8 100644
--- a/gpu/src/GrGpuGLShaders2.cpp
+++ b/gpu/src/GrGpuGLShaders2.cpp
@@ -1088,8 +1088,6 @@
 
 GrGpuGLShaders2::GrGpuGLShaders2() {
 
-    resetContextHelper();
-
     fProgram = NULL;
     fProgramCache = new ProgramCache();
 
@@ -1119,11 +1117,9 @@
 }
 
 void GrGpuGLShaders2::resetContext() {
-    INHERITED::resetContext();
-    resetContextHelper();
-}
 
-void GrGpuGLShaders2::resetContextHelper() {
+    INHERITED::resetContext();
+
     fHWGeometryState.fVertexLayout = 0;
     fHWGeometryState.fVertexOffset  = ~0;
     GR_GL(DisableVertexAttribArray(COL_ATTR_LOCATION));
diff --git a/gpu/src/GrGpuGLShaders2.h b/gpu/src/GrGpuGLShaders2.h
index e8785c9..4c501be 100644
--- a/gpu/src/GrGpuGLShaders2.h
+++ b/gpu/src/GrGpuGLShaders2.h
@@ -26,8 +26,6 @@
              GrGpuGLShaders2();
     virtual ~GrGpuGLShaders2();
 
-    virtual void resetContext();
-
 protected:
     // overrides from GrGpu
     virtual bool flushGraphicsState(GrPrimitiveType type);
@@ -38,7 +36,7 @@
 
 private:
 
-    void resetContextHelper();
+    virtual void resetContext();
 
     // Helpers to make code more readable
     const GrMatrix& getHWSamplerMatrix(int stage);