Delete GL tex ID when last of GrGLTexture or GrGLRenderTarget that reference it is destroyed

git-svn-id: http://skia.googlecode.com/svn/trunk@915 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gpu/src/GrContext.cpp b/gpu/src/GrContext.cpp
index c6fa618..cf51cc9e 100644
--- a/gpu/src/GrContext.cpp
+++ b/gpu/src/GrContext.cpp
@@ -1,5 +1,5 @@
 /*
-    Copyright 2010 Google Inc.
+    Copyright 2011 Google Inc.
 
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
@@ -172,7 +172,7 @@
                                      0, 4);
                 entry = fTextureCache->createAndLock(*key, texture);
             }
-            texture->removeRenderTarget();
+            texture->releaseRenderTarget();
         } else {
             // TODO: Our CPU stretch doesn't filter. But we create separate
             // stretched textures when the sampler state is either filtered or
diff --git a/gpu/src/GrGLTexture.cpp b/gpu/src/GrGLTexture.cpp
index ae991e8..1acb871 100644
--- a/gpu/src/GrGLTexture.cpp
+++ b/gpu/src/GrGLTexture.cpp
@@ -1,5 +1,5 @@
 /*
-    Copyright 2010 Google Inc.
+    Copyright 2011 Google Inc.
 
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
 #include "GrGpuGL.h"
 
 GrGLRenderTarget::GrGLRenderTarget(const GLRenderTargetIDs& ids,
+                                   GrGLTexID* texID,
                                    GLuint stencilBits,
                                    const GrGLIRect& viewport,
                                    GrGLTexture* texture,
@@ -34,6 +35,8 @@
     fNeedsResolve           = false;
     fViewport               = viewport;
     fOwnIDs                 = ids.fOwnIDs;
+    fTexIDObj               = texID;
+    GrSafeRef(fTexIDObj);
 }
 
 GrGLRenderTarget::~GrGLRenderTarget() {
@@ -52,6 +55,7 @@
             GR_GLEXT(fGL->extensions(), DeleteRenderbuffers(1, &fMSColorRenderbufferID));
         }
     }
+    GrSafeUnref(fTexIDObj);
 }
 
 void GrGLRenderTarget::abandon() {
@@ -59,6 +63,9 @@
     fTexFBOID               = 0;
     fStencilRenderbufferID  = 0;
     fMSColorRenderbufferID  = 0;
+    if (NULL != fTexIDObj) {
+        fTexIDObj->abandon();
+    }
 }
 
 
@@ -84,7 +91,7 @@
                     textureDesc.fFormat) {
 
     fTexParams          = initialTexParams;
-    fTextureID          = textureDesc.fTextureID;
+    fTexIDObj           = new GrGLTexID(textureDesc.fTextureID);
     fUploadFormat       = textureDesc.fUploadFormat;
     fUploadByteCount    = textureDesc.fUploadByteCount;
     fUploadType         = textureDesc.fUploadType;
@@ -108,43 +115,32 @@
         vp.fHeight = textureDesc.fContentHeight;
         vp.fBottom = textureDesc.fAllocHeight - textureDesc.fContentHeight;
 
-        fRenderTarget = new GrGLRenderTarget(rtIDs, textureDesc.fStencilBits, 
+        fRenderTarget = new GrGLRenderTarget(rtIDs, fTexIDObj,
+                                             textureDesc.fStencilBits,
                                              vp, this, gl);
     }
 }
 
 GrGLTexture::~GrGLTexture() {
-    // make sure we haven't been abandoned
-    if (fTextureID) {
-        fGpuGL->notifyTextureDelete(this);
-        GR_GL(DeleteTextures(1, &fTextureID));
-    }
-    delete fRenderTarget;
+    fGpuGL->notifyTextureDelete(this);
+    fTexIDObj->unref();
+    GrSafeUnref(fRenderTarget);
 }
 
 void GrGLTexture::abandon() {
-    fTextureID = 0;
+    fTexIDObj->abandon();
     if (NULL != fRenderTarget) {
         fRenderTarget->abandon();
     }
 }
 
-bool GrGLTexture::isRenderTarget() const {
-    return NULL != fRenderTarget;
-}
-
 GrRenderTarget* GrGLTexture::asRenderTarget() {
     return (GrRenderTarget*)fRenderTarget;
 }
 
-void GrGLTexture::removeRenderTarget() {
-    GrAssert(NULL != fRenderTarget);
-    if (NULL != fRenderTarget) {
-        // must do this notify before the delete
-        fGpuGL->notifyTextureRemoveRenderTarget(this);
-        delete fRenderTarget;
-        fRenderTarget = NULL;
-    }
+void GrGLTexture::releaseRenderTarget() {
+    GrSafeUnref(fRenderTarget);
+    fRenderTarget = NULL;
 }
 
 void GrGLTexture::uploadTextureData(uint32_t x,
@@ -162,7 +158,7 @@
     // If we need to update textures that are created upside down
     // then we have to modify this code to flip the srcData
     GrAssert(kTopDown_Orientation == fOrientation);
-    GR_GL(BindTexture(GL_TEXTURE_2D, fTextureID));
+    GR_GL(BindTexture(GL_TEXTURE_2D, fTexIDObj->id()));
     GR_GL(PixelStorei(GL_UNPACK_ALIGNMENT, fUploadByteCount));
     GR_GL(TexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, 
                         fUploadFormat, fUploadType, srcData));
@@ -170,7 +166,7 @@
 }
 
 intptr_t GrGLTexture::getTextureHandle() {
-    return fTextureID;
+    return fTexIDObj->id();
 }
 
 
diff --git a/gpu/src/GrGpuGL.cpp b/gpu/src/GrGpuGL.cpp
index b0d5e73..1c3de1a 100644
--- a/gpu/src/GrGpuGL.cpp
+++ b/gpu/src/GrGpuGL.cpp
@@ -1,5 +1,5 @@
 /*
-    Copyright 2010 Google Inc.
+    Copyright 2011 Google Inc.
 
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
@@ -510,7 +510,7 @@
     rtIDs.fRTFBOID  = (GLuint)platformRenderTarget;
     rtIDs.fTexFBOID = (GLuint)platformRenderTarget;
 
-    return new GrGLRenderTarget(rtIDs, stencilBits, viewport, NULL, this);
+    return new GrGLRenderTarget(rtIDs, NULL, stencilBits, viewport, NULL, this);
 }
 
 GrRenderTarget* GrGpuGL::createRenderTargetFrom3DApiStateHelper() {
@@ -529,7 +529,7 @@
 
     rtIDs.fOwnIDs = false;
 
-    return new GrGLRenderTarget(rtIDs, stencilBits, viewport, NULL, this);
+    return new GrGLRenderTarget(rtIDs, NULL, stencilBits, viewport, NULL, this);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -1694,11 +1694,6 @@
 
 void GrGpuGL::notifyRenderTargetDelete(GrRenderTarget* renderTarget) {
     GrAssert(NULL != renderTarget);
-
-    // if the bound FBO is destroyed we can't rely on the implicit bind to 0
-    // a) we want the default RT which may not be FBO 0
-    // 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 = NULL;
     }
@@ -1719,13 +1714,6 @@
     }
 }
 
-void GrGpuGL::notifyTextureRemoveRenderTarget(GrGLTexture* texture) {
-    GrAssert(NULL != texture->asRenderTarget());
-
-    // if there is a pending resolve, perform it.
-    resolveTextureRenderTarget(texture);
-}
-
 bool GrGpuGL::canBeTexture(GrTexture::PixelConfig config,
                            GLenum* internalFormat,
                            GLenum* format,
diff --git a/gpu/src/GrGpuGL.h b/gpu/src/GrGpuGL.h
index ab504ad..f9a6987 100644
--- a/gpu/src/GrGpuGL.h
+++ b/gpu/src/GrGpuGL.h
@@ -1,5 +1,5 @@
 /*
-    Copyright 2010 Google Inc.
+    Copyright 2011 Google Inc.
 
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
@@ -151,7 +151,6 @@
     void notifyIndexBufferDelete(const GrGLIndexBuffer* buffer);
     void notifyTextureDelete(GrGLTexture* texture);
     void notifyRenderTargetDelete(GrRenderTarget* renderTarget);
-    void notifyTextureRemoveRenderTarget(GrGLTexture* texture);
 
     void setSpareTextureUnit();
 
diff --git a/gpu/src/GrInOrderDrawBuffer.cpp b/gpu/src/GrInOrderDrawBuffer.cpp
index b2fe581..8225425 100644
--- a/gpu/src/GrInOrderDrawBuffer.cpp
+++ b/gpu/src/GrInOrderDrawBuffer.cpp
@@ -1,5 +1,5 @@
 /*
-    Copyright 2010 Google Inc.
+    Copyright 2011 Google Inc.
 
     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
@@ -291,12 +291,11 @@
     GrAssert(!fReservedGeometry.fLocked);
     uint32_t numStates = fStates.count();
     for (uint32_t i = 0; i < numStates; ++i) {
+        const DrState& dstate = this->accessSavedDrawState(fStates[i]);
         for (int s = 0; s < kNumStages; ++s) {
-            GrTexture* tex = this->accessSavedDrawState(fStates[i]).fTextures[s];
-            if (NULL != tex) {
-                tex->unref();
-            }
+            GrSafeUnref(dstate.fTextures[s]);
         }
+        GrSafeUnref(dstate.fRenderTarget);
     }
     int numDraws = fDraws.count();
     for (int d = 0; d < numDraws; ++d) {
@@ -499,6 +498,7 @@
     for (int s = 0; s < kNumStages; ++s) {
         GrSafeRef(fCurrDrawState.fTextures[s]);
     }
+    GrSafeRef(fCurrDrawState.fRenderTarget);
     this->saveCurrentDrawState(&fStates.push_back());
  }