Remove notion of default rendertarget. This doesn't map well to usage patterns outside sample app. Make binding between SkGpuDevice and a GrRenderTarget more explicit. Create method on GrContext to wrap the current target in the 3D API with a GrRenderTarget.



git-svn-id: http://skia.googlecode.com/svn/trunk@706 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gpu/src/GrContext.cpp b/gpu/src/GrContext.cpp
index 94398cf..5128602 100644
--- a/gpu/src/GrContext.cpp
+++ b/gpu/src/GrContext.cpp
@@ -30,8 +30,8 @@
 

 #if DEFER_TEXT_RENDERING

     static const uint32_t POOL_VB_SIZE = 2048 *

-            GrDrawTarget::VertexSize(
-                GrDrawTarget::kTextFormat_VertexLayoutBit |
+            GrDrawTarget::VertexSize(

+                GrDrawTarget::kTextFormat_VertexLayoutBit |

                 GrDrawTarget::StageTexCoordVertexLayoutBit(0,0));

     static const uint32_t NUM_POOL_VBS = 8;

 #else

@@ -856,10 +856,6 @@
     return fGpu->currentRenderTarget();

 }

 

-void GrContext::setDefaultRenderTargetSize(uint32_t width, uint32_t height) {

-    fGpu->setDefaultRenderTargetSize(width, height);

-}

-

 void GrContext::setSamplerState(int stage, const GrSamplerState& samplerState) {

     fGpu->setSamplerState(stage, samplerState);

 }

diff --git a/gpu/src/GrGpu.cpp b/gpu/src/GrGpu.cpp
index fe6d0c3..c48bd19 100644
--- a/gpu/src/GrGpu.cpp
+++ b/gpu/src/GrGpu.cpp
@@ -178,6 +178,14 @@
 bool GrGpu::setupClipAndFlushState(PrimitiveType type) {
     const GrIRect* r = NULL;
 
+    // we check this early because we need a valid
+    // render target to setup stencil clipping
+    // before even going into flushGraphicsState
+    if (NULL == fCurrDrawState.fRenderTarget) {
+        GrAssert(!"No render target bound.");
+        return false;
+    }
+
     if (fCurrDrawState.fFlagBits & kClip_StateBit) {
         fClipState.fClipInStencil = fClip.countRects() > 1;
 
diff --git a/gpu/src/GrGpuGL.cpp b/gpu/src/GrGpuGL.cpp
index 23636d1..084f98f 100644
--- a/gpu/src/GrGpuGL.cpp
+++ b/gpu/src/GrGpuGL.cpp
@@ -152,24 +152,7 @@
 
     resetContextHelper();
 
-    GrGLRenderTarget::GLRenderTargetIDs defaultRTIDs;
-    GR_GL_GetIntegerv(GR_FRAMEBUFFER_BINDING, (GLint*)&defaultRTIDs.fRTFBOID);
-    defaultRTIDs.fTexFBOID = defaultRTIDs.fRTFBOID;
-    defaultRTIDs.fMSColorRenderbufferID = 0;
-    defaultRTIDs.fStencilRenderbufferID = 0;
-    GLint vp[4];
-    GR_GL_GetIntegerv(GL_VIEWPORT, vp);
-    fHWBounds.fViewportRect.setLTRB(vp[0],
-                                    vp[1] + vp[3],
-                                    vp[0] + vp[2],
-                                    vp[1]);
-    defaultRTIDs.fOwnIDs = false;
-
-    fDefaultRenderTarget = new GrGLRenderTarget(defaultRTIDs,
-                                                fHWBounds.fViewportRect,
-                                                NULL,
-                                                this);
-    fHWDrawState.fRenderTarget = fDefaultRenderTarget;
+    fHWDrawState.fRenderTarget = NULL;
     fRenderTargetChanged = true;
 
     GLint maxTextureUnits;
@@ -445,17 +428,13 @@
     fMinRenderTargetWidth = GrMax<GLuint>(fMinRenderTargetWidth, 16);
     fMinRenderTargetHeight = GrMax<GLuint>(fMinRenderTargetHeight, 16);
 #endif
-    // bind back to original FBO
-    GR_GLEXT(fExts, BindFramebuffer(GR_FRAMEBUFFER, defaultRTIDs.fRTFBOID));
+
 #if GR_COLLECT_STATS
     ++fStats.fRenderTargetChngCnt;
 #endif
-    eraseStencil(0, ~0);
 }
 
 GrGpuGL::~GrGpuGL() {
-    fDefaultRenderTarget->abandon();
-    fDefaultRenderTarget->unref();
 }
 
 void GrGpuGL::resetContextHelper() {
@@ -501,6 +480,7 @@
     fHWBounds.fScissorRect.setLTRB(0,0,0,0);
     fHWBounds.fScissorEnabled = false;
     GR_GL(Disable(GL_SCISSOR_TEST));
+    fHWBounds.fViewportRect.setLTRB(-1,-1,-1,-1);
 
     // disabling the stencil test also disables
     // stencil buffer writes
@@ -546,6 +526,30 @@
     return rt;
 }
 
+GrRenderTarget* GrGpuGL::createRenderTargetFrom3DApiState() {
+    
+    GrGLRenderTarget::GLRenderTargetIDs rtIDs;
+    
+    GR_GL_GetIntegerv(GR_FRAMEBUFFER_BINDING, (GLint*)&rtIDs.fRTFBOID);
+    rtIDs.fTexFBOID = rtIDs.fRTFBOID;
+    rtIDs.fMSColorRenderbufferID = 0;
+    rtIDs.fStencilRenderbufferID = 0;
+    
+    GLint vp[4];
+    GR_GL_GetIntegerv(GL_VIEWPORT, vp);
+    GrIRect viewportRect;
+    viewportRect.setLTRB(vp[0],
+                         vp[1] + vp[3],
+                         vp[0] + vp[2],
+                         vp[1]);
+    rtIDs.fOwnIDs = false;
+
+    return new GrGLRenderTarget(rtIDs,
+                                viewportRect,
+                                NULL,
+                                this);
+}
+
 // defines stencil formats from more to less preferred
 GLenum GR_GL_STENCIL_FORMAT_ARRAY[] = {

     GR_STENCIL_INDEX8,

@@ -979,10 +983,6 @@
     return tex;
 }
 
-GrRenderTarget* GrGpuGL::defaultRenderTarget() {
-    return fDefaultRenderTarget;
-}
-
 GrVertexBuffer* GrGpuGL::createVertexBuffer(uint32_t size, bool dynamic) {
     GLuint id;
     GR_GL(GenBuffers(1, &id));
@@ -1029,16 +1029,6 @@
     return NULL;
 }
 
-void GrGpuGL::setDefaultRenderTargetSize(uint32_t width, uint32_t height) {
-    GrIRect viewport(0, height, width, 0);
-    if (viewport != fDefaultRenderTarget->viewport()) {
-        fDefaultRenderTarget->setViewport(viewport);
-        if (fHWDrawState.fRenderTarget == fDefaultRenderTarget) {
-            fHWDrawState.fRenderTarget = NULL;
-        }
-    }
-}
-
 void GrGpuGL::flushScissor(const GrIRect* rect) {
     GrAssert(NULL != fCurrDrawState.fRenderTarget);
     const GrIRect& vp =
@@ -1153,6 +1143,9 @@
 }
 
 void GrGpuGL::flushRenderTarget() {
+
+    GrAssert(NULL != fCurrDrawState.fRenderTarget);
+
     if (fHWDrawState.fRenderTarget != fCurrDrawState.fRenderTarget) {
         GrGLRenderTarget* rt = (GrGLRenderTarget*)fCurrDrawState.fRenderTarget;
         GR_GLEXT(fExts, BindFramebuffer(GR_FRAMEBUFFER, rt->renderFBOID()));
@@ -1459,7 +1452,11 @@
     }
 }
 
-void GrGpuGL::flushGLStateCommon(PrimitiveType type) {
+bool GrGpuGL::flushGLStateCommon(PrimitiveType type) {
+
+    // GrGpu::setupClipAndFlushState should have already checked this
+    // and bailed if not true.
+    GrAssert(NULL != fCurrDrawState.fRenderTarget);
 
     for (int s = 0; s < kNumStages; ++s) {
         bool usingTexture = VertexUsesStage(s, fGeometrySrc.fVertexLayout);
@@ -1521,15 +1518,7 @@
                 nextTexture->setTexParams(newTexParams);
             } else {
                 GrAssert(!"Rendering with texture vert flag set but no texture");
-                if (NULL != fHWDrawState.fTextures[s]) {
-                    setTextureUnit(s);
-                    GR_GL(BindTexture(GL_TEXTURE_2D, 0));
-                    //            GrPrintf("---- bindtexture 0\n");
-                #if GR_COLLECT_STATS
-                    ++fStats.fTextureChngCnt;
-                #endif
-                    fHWDrawState.fTextures[s] = NULL;
-                }
+                return false;
             }
         }
     }
@@ -1607,6 +1596,7 @@
     flushStencil();
 
     fHWDrawState.fFlagBits = fCurrDrawState.fFlagBits;
+    return true;
 }
 
 void GrGpuGL::notifyVertexBufferBind(const GrGLVertexBuffer* buffer) {
@@ -1645,7 +1635,7 @@
     // b) we set more state than just FBO based on the RT
     // So trash the HW state to force an RT flush next time
     if (fCurrDrawState.fRenderTarget == renderTarget) {
-        fCurrDrawState.fRenderTarget = fDefaultRenderTarget;
+        fCurrDrawState.fRenderTarget = NULL;
     }
     if (fHWDrawState.fRenderTarget == renderTarget) {
         fHWDrawState.fRenderTarget = NULL;
diff --git a/gpu/src/GrGpuGL.h b/gpu/src/GrGpuGL.h
index 8c2cd80..611485d 100644
--- a/gpu/src/GrGpuGL.h
+++ b/gpu/src/GrGpuGL.h
@@ -42,9 +42,7 @@
                                                  intptr_t platformRenderTarget,
                                                  int width, int height);
 
-    virtual GrRenderTarget* defaultRenderTarget();
-
-    virtual void setDefaultRenderTargetSize(uint32_t width, uint32_t height);
+    virtual GrRenderTarget* createRenderTargetFrom3DApiState();
 
     virtual void eraseColor(GrColor color);
 
@@ -98,7 +96,7 @@
     // sampler state (filtering, tiling)
     // FBO binding
     // line width
-    void flushGLStateCommon(PrimitiveType type);
+    bool flushGLStateCommon(PrimitiveType type);
 
     // set when this class changes the rendertarget.
     // Subclass should notice at flush time, take appropriate action,
@@ -114,8 +112,6 @@
     GrGLExts fExts;
 
 private:
-    GrGLRenderTarget* fDefaultRenderTarget;
-
     void resetContextHelper();
 
     // notify callbacks to update state tracking when related
diff --git a/gpu/src/GrGpuGLFixed.cpp b/gpu/src/GrGpuGLFixed.cpp
index d142b66..99a593d 100644
--- a/gpu/src/GrGpuGLFixed.cpp
+++ b/gpu/src/GrGpuGLFixed.cpp
@@ -140,7 +140,9 @@
         }
     }
     
-    flushGLStateCommon(type);
+    if (!flushGLStateCommon(type)) {
+        return false;
+    }
 
     if (fRenderTargetChanged) {    
         flushProjectionMatrix();
diff --git a/gpu/src/GrGpuGLShaders.cpp b/gpu/src/GrGpuGLShaders.cpp
index 7cd1891..03d4674 100644
--- a/gpu/src/GrGpuGLShaders.cpp
+++ b/gpu/src/GrGpuGLShaders.cpp
@@ -741,7 +741,9 @@
         }
     }
 
-    flushGLStateCommon(type);
+    if (!flushGLStateCommon(type)) {
+        return false;
+    }
 
     if (fRenderTargetChanged) {
         // our coords are in pixel space and the GL matrices map to NDC
diff --git a/gpu/src/GrGpuGLShaders2.cpp b/gpu/src/GrGpuGLShaders2.cpp
index e47fe5d..1218a36 100644
--- a/gpu/src/GrGpuGLShaders2.cpp
+++ b/gpu/src/GrGpuGLShaders2.cpp
@@ -1233,7 +1233,9 @@
 
 bool GrGpuGLShaders2::flushGraphicsState(PrimitiveType type) {
 
-    flushGLStateCommon(type);
+    if (!flushGLStateCommon(type)) {
+        return false;
+    }
 
     if (fRenderTargetChanged) {
         // our coords are in pixel space and the GL matrices map to NDC