Reland of 8525 with fix for case when GrRT outlives GrTexture.
Review URL: https://codereview.chromium.org/13814015

git-svn-id: http://skia.googlecode.com/svn/trunk@8573 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/gpu/GrContext.cpp b/src/gpu/GrContext.cpp
index 9755ecf..5ebe5b9 100644
--- a/src/gpu/GrContext.cpp
+++ b/src/gpu/GrContext.cpp
@@ -373,7 +373,6 @@
             verts[1].setIRectFan(0, 0, 1, 1, 2 * sizeof(GrPoint));
             fGpu->drawNonIndexed(kTriangleFan_GrPrimitiveType, 0, 4);
         }
-        texture->releaseRenderTarget();
     } else {
         // TODO: Our CPU stretch doesn't filter. But we create separate
         // stretched textures when the texture params is either filtered or
diff --git a/src/gpu/GrGpu.h b/src/gpu/GrGpu.h
index d4e4e6b..f55c6ad 100644
--- a/src/gpu/GrGpu.h
+++ b/src/gpu/GrGpu.h
@@ -78,8 +78,7 @@
      *
      * If kRenderTarget_TextureFlag is specified the GrRenderTarget is
      * accessible via GrTexture::asRenderTarget(). The texture will hold a ref
-     * on the render target until its releaseRenderTarget() is called or it is
-     * destroyed.
+     * on the render target until the texture is destroyed.
      *
      * @param desc        describes the texture to be created.
      * @param srcData     texel data to load texture. Begins with full-size
diff --git a/src/gpu/GrTexture.cpp b/src/gpu/GrTexture.cpp
index 44a1442..6452aae 100644
--- a/src/gpu/GrTexture.cpp
+++ b/src/gpu/GrTexture.cpp
@@ -17,6 +17,12 @@
 
 SK_DEFINE_INST_COUNT(GrTexture)
 
+GrTexture::~GrTexture() {
+    if (NULL != fRenderTarget.get()) {
+        fRenderTarget.get()->owningTextureDestroyed();
+    }
+}
+
 /**
  * This method allows us to interrupt the normal deletion process and place
  * textures back in the texture cache when their ref count goes to zero.
@@ -67,33 +73,15 @@
                                 pixelOpsFlags);
 }
 
-void GrTexture::releaseRenderTarget() {
-    if (NULL != fRenderTarget) {
-        GrAssert(fRenderTarget->asTexture() == this);
-        GrAssert(fDesc.fFlags & kRenderTarget_GrTextureFlagBit);
-
-        fRenderTarget->onTextureReleaseRenderTarget();
-        fRenderTarget->unref();
-        fRenderTarget = NULL;
-
-        fDesc.fFlags = fDesc.fFlags &
-            ~(kRenderTarget_GrTextureFlagBit|kNoStencil_GrTextureFlagBit);
-        fDesc.fSampleCnt = 0;
-    }
-}
-
 void GrTexture::onRelease() {
     GrAssert(!this->isSetFlag((GrTextureFlags) kReturnToCache_FlagBit));
-    this->releaseRenderTarget();
-
     INHERITED::onRelease();
 }
 
 void GrTexture::onAbandon() {
-    if (NULL != fRenderTarget) {
+    if (NULL != fRenderTarget.get()) {
         fRenderTarget->abandon();
     }
-
     INHERITED::onAbandon();
 }
 
diff --git a/src/gpu/gl/GrGLTexture.cpp b/src/gpu/gl/GrGLTexture.cpp
index 66d6371..6314b0c 100644
--- a/src/gpu/gl/GrGLTexture.cpp
+++ b/src/gpu/gl/GrGLTexture.cpp
@@ -34,8 +34,7 @@
         vp.fBottom = 0;
         vp.fHeight = textureDesc.fHeight;
 
-        fRenderTarget = SkNEW_ARGS(GrGLRenderTarget,
-                                   (gpu, *rtDesc, vp, fTexIDObj, this));
+        fRenderTarget.reset(SkNEW_ARGS(GrGLRenderTarget, (gpu, *rtDesc, vp, fTexIDObj, this)));
     }
 }