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/src/gpu/SkGpuCanvas.cpp b/src/gpu/SkGpuCanvas.cpp
index 5ca1736..9513bbd 100644
--- a/src/gpu/SkGpuCanvas.cpp
+++ b/src/gpu/SkGpuCanvas.cpp
@@ -23,11 +23,14 @@
 
 ///////////////////////////////////////////////////////////////////////////////
 
-static SkDeviceFactory* make_df(GrContext* context) {
-    return SkNEW_ARGS(SkGpuDeviceFactory, (context));
+static SkDeviceFactory* make_df(GrContext* context, 
+                                GrRenderTarget* renderTarget) {
+    return SkNEW_ARGS(SkGpuDeviceFactory, (context, renderTarget));
 }
 
-SkGpuCanvas::SkGpuCanvas(GrContext* context) : SkCanvas(make_df(context)) {
+SkGpuCanvas::SkGpuCanvas(GrContext* context,
+                         GrRenderTarget* renderTarget) 
+      : SkCanvas(make_df(context, renderTarget)) {
     SkASSERT(context);
     fContext = context;
     fContext->ref();
diff --git a/src/gpu/SkGpuDevice.cpp b/src/gpu/SkGpuDevice.cpp
index d55f6f2..f14de35 100644
--- a/src/gpu/SkGpuDevice.cpp
+++ b/src/gpu/SkGpuDevice.cpp
@@ -109,8 +109,14 @@
 
 ///////////////////////////////////////////////////////////////////////////////
 
-SkGpuDevice::SkGpuDevice(GrContext* context, const SkBitmap& bitmap, bool isLayer)
-        : SkDevice(NULL, bitmap, false) {
+GrRenderTarget* SkGpuDevice::Current3DApiRenderTarget() {
+    return (GrRenderTarget*) -1;
+}
+
+SkGpuDevice::SkGpuDevice(GrContext* context,
+                         const SkBitmap& bitmap,
+                         GrRenderTarget* renderTargetOrNull)
+        : SkDevice(NULL, bitmap, (NULL == renderTargetOrNull)) {
 
     fNeedPrepareRenderTarget = false;
     fDrawProcs = NULL;
@@ -123,7 +129,7 @@
     fRenderTarget = NULL;
     fNeedClear = false;
 
-    if (isLayer) {
+    if (NULL == renderTargetOrNull) {
         SkBitmap::Config c = bitmap.config();
         if (c != SkBitmap::kRGB_565_Config) {
             c = SkBitmap::kARGB_8888_Config;
@@ -164,16 +170,13 @@
         } else {
             GrPrintf("--- failed to create gpu-offscreen [%d %d]\n",
                      this->width(), this->height());
+            GrAssert(false);
         }
-    }
-
-    if (NULL == fRenderTarget) {
-        GrAssert(NULL == fCache);
-        GrAssert(NULL == fTexture);
-
-        fRenderTarget = fContext->currentRenderTarget();
+    } else if (Current3DApiRenderTarget() == renderTargetOrNull) {
+        fRenderTarget = fContext->createRenderTargetFrom3DApiState();
+    } else {
+        fRenderTarget = renderTargetOrNull;
         fRenderTarget->ref();
-        fContext->setDefaultRenderTargetSize(this->width(), this->height());
     }
 }
 
@@ -196,26 +199,6 @@
     }
 }
 
-void SkGpuDevice::bindDeviceToTargetHandle(intptr_t handle) {
-    if (fCache) {
-        GrAssert(NULL != fTexture);
-        GrAssert(fRenderTarget == fTexture->asRenderTarget());
-        // IMPORTANT: reattach the rendertarget/tex back to the cache.
-        fContext->reattachAndUnlockCachedTexture((GrTextureEntry*)fCache);
-    } else if (NULL != fTexture) {
-        GrAssert(!CACHE_LAYER_TEXTURES);
-        fTexture->unref();
-    } else if (NULL != fRenderTarget) {
-        fRenderTarget->unref();
-    }
-
-    fCache  = NULL;
-    fTexture = NULL;
-    fRenderTarget = fContext->createPlatformRenderTarget(handle,
-                                                         this->width(),
-                                                         this->height());
-}
-
 intptr_t SkGpuDevice::getLayerTextureHandle() const {
     if (fTexture) {
         return fTexture->getTextureHandle();
@@ -1050,12 +1033,29 @@
 
 ///////////////////////////////////////////////////////////////////////////////
 
-SkGpuDeviceFactory::SkGpuDeviceFactory(GrContext* context) : fContext(context) {
+SkGpuDeviceFactory::SkGpuDeviceFactory(GrContext* context,
+                                       GrRenderTarget* rootRenderTarget)
+        : fContext(context) {
+
+    GrAssert(NULL != context);
+    GrAssert(NULL != rootRenderTarget);
+
+    // check this now rather than passing this value to SkGpuDevice cons.
+    // we want the rt that is bound *now* in the 3D API, not the one
+    // at the time of newDevice.
+    if (SkGpuDevice::Current3DApiRenderTarget() == rootRenderTarget) {
+        fRootRenderTarget = context->createRenderTargetFrom3DApiState();
+    } else {
+        fRootRenderTarget = rootRenderTarget;
+        rootRenderTarget->ref();
+    }
     context->ref();
+
 }
 
 SkGpuDeviceFactory::~SkGpuDeviceFactory() {
     fContext->unref();
+    fRootRenderTarget->unref();
 }
 
 SkDevice* SkGpuDeviceFactory::newDevice(SkCanvas*, SkBitmap::Config config,
@@ -1064,6 +1064,6 @@
     SkBitmap bm;
     bm.setConfig(config, width, height);
     bm.setIsOpaque(isOpaque);
-    return new SkGpuDevice(fContext, bm, isLayer);
+    return new SkGpuDevice(fContext, bm, isLayer ?  NULL : fRootRenderTarget);
 }
 
diff --git a/src/utils/mac/SkOSWindow_Mac.cpp b/src/utils/mac/SkOSWindow_Mac.cpp
index 6828a95..d4ef7e9 100644
--- a/src/utils/mac/SkOSWindow_Mac.cpp
+++ b/src/utils/mac/SkOSWindow_Mac.cpp
@@ -512,6 +512,7 @@
     
     if (success) {
         glClearColor(0, 0, 0, 0);
+        glClearStencil(0);
         glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
     }
     return success;
diff --git a/src/utils/win/SkOSWindow_Win.cpp b/src/utils/win/SkOSWindow_Win.cpp
index 40455d2..53449b1 100644
--- a/src/utils/win/SkOSWindow_Win.cpp
+++ b/src/utils/win/SkOSWindow_Win.cpp
@@ -439,7 +439,8 @@
     }
     if (wglMakeCurrent(GetDC((HWND)fHWND), (HGLRC)fHGLRC)) {
         glClearColor(0, 0, 0, 0);
-        glClear(GL_COLOR_BUFFER_BIT);
+        glClearStencil(0);
+        glClear(GL_COLOR_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);
         fGLAttached = true;
         return true;
     }