Resource cache now explicitly takes ref of managed resources

https://codereview.appspot.com/6489085/



git-svn-id: http://skia.googlecode.com/svn/trunk@5407 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index a2158e2..edf45b8 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -381,13 +381,13 @@
 
     GrResourceKey resourceKey = GrTexture::ComputeKey(fGpu, params, desc, cacheData, false);
 
-    GrTexture* texture = NULL;
+    SkAutoTUnref<GrTexture> texture;
     if (GrTexture::NeedsResizing(resourceKey)) {
-        texture = this->createResizedTexture(desc, cacheData,
+        texture.reset(this->createResizedTexture(desc, cacheData,
                                              srcData, rowBytes,
-                                             GrTexture::NeedsFiltering(resourceKey));
+                                             GrTexture::NeedsFiltering(resourceKey)));
     } else {
-        texture = fGpu->createTexture(desc, srcData, rowBytes);
+        texture.reset(fGpu->createTexture(desc, srcData, rowBytes));
     }
 
     if (NULL != texture) {
@@ -450,7 +450,7 @@
         desc.fFlags = inDesc.fFlags;
         desc.fWidth = origWidth;
         desc.fHeight = origHeight;
-        GrTexture* texture = fGpu->createTexture(desc, NULL, 0);
+        SkAutoTUnref<GrTexture> texture(fGpu->createTexture(desc, NULL, 0));
         if (NULL != texture) {
             GrResourceKey key = GrTexture::ComputeKey(fGpu, NULL,
                                                       texture->desc(),
diff --git a/src/gpu/GrResourceCache.cpp b/src/gpu/GrResourceCache.cpp
index 4561aea..4b262d4 100644
--- a/src/gpu/GrResourceCache.cpp
+++ b/src/gpu/GrResourceCache.cpp
@@ -17,6 +17,7 @@
 
     // we assume ownership of the resource, and will unref it when we die
     GrAssert(resource);
+    resource->ref();    
 }
 
 GrResourceEntry::~GrResourceEntry() {
diff --git a/src/gpu/GrStencilBuffer.h b/src/gpu/GrStencilBuffer.h
index 27c0a0c..690fa32 100644
--- a/src/gpu/GrStencilBuffer.h
+++ b/src/gpu/GrStencilBuffer.h
@@ -59,8 +59,8 @@
         return fLastClipData;
     }
 
-    // places the sb in the cache and locks it. Caller transfers
-    // a ref to the the cache which will unref when purged.
+    // Places the sb in the cache and locks it. The cache takes a ref
+    // of the stencil buffer.
     void transferToCacheAndLock();
 
     static GrResourceKey ComputeKey(int width, int height, int sampleCnt);
diff --git a/src/gpu/gl/GrGpuGL.cpp b/src/gpu/gl/GrGpuGL.cpp
index 8d55abb..74f9953 100644
--- a/src/gpu/gl/GrGpuGL.cpp
+++ b/src/gpu/gl/GrGpuGL.cpp
@@ -1105,19 +1105,16 @@
             // whatever sizes GL gives us. In that case we query for the size.
             GrGLStencilBuffer::Format format = sFmt;
             get_stencil_rb_sizes(this->glInterface(), sbID, &format);
-            sb = SkNEW_ARGS(GrGLStencilBuffer,
-                            (this, sbID, width, height,
-                             samples, format));
+            SkAutoTUnref<GrStencilBuffer> sb(SkNEW_ARGS(GrGLStencilBuffer,
+                                                  (this, sbID, width, height,
+                                                  samples, format)));
             if (this->attachStencilBufferToRenderTarget(sb, rt)) {
                 fLastSuccessfulStencilFmtIdx = sIdx;
-                // This code transfers the creation ref to the
-                // cache and then adds a ref for the render target
                 sb->transferToCacheAndLock();
                 rt->setStencilBuffer(sb);
                 return true;
            }
            sb->abandon(); // otherwise we lose sbID
-           sb->unref();
         }
     }
     GL_CALL(DeleteRenderbuffers(1, &sbID));