Some fixes around GrContext::abandonContext:

Fix debug crash when GrResourceCache is destroyed after GrContext is abandoned while GrTextures are in the exlusive list.

Notify debug GL context that GL resources are expected to remain undeleted when context is destroyed after being abandoned.

Stop leaking program cache entries when context is abandoned.

R=robertphillips@google.com

Author: bsalomon@google.com

Review URL: https://codereview.chromium.org/422323002
diff --git a/src/gpu/GrTexture.cpp b/src/gpu/GrTexture.cpp
index 63069a4..5944447 100644
--- a/src/gpu/GrTexture.cpp
+++ b/src/gpu/GrTexture.cpp
@@ -102,12 +102,27 @@
                                 pixelOpsFlags);
 }
 
+void GrTexture::abandonReleaseCommon() {
+    // In debug builds the resource cache tracks removed/exclusive textures and has an unref'ed ptr.
+    // After abandon() or release() the resource cache will be unreachable (getContext() == NULL).
+    // So we readd the texture to the cache here so that it is removed from the exclusive list and
+    // there is no longer an unref'ed ptr to the texture in the cache.
+    if (this->impl()->isSetFlag((GrTextureFlags)GrTextureImpl::kReturnToCache_FlagBit)) {
+        SkASSERT(!this->wasDestroyed());
+        this->ref();  // restores the ref the resource cache gave up when it marked this exclusive.
+        this->impl()->resetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit);
+        this->getContext()->addExistingTextureToCache(this);
+    }
+}
+
 void GrTexture::onRelease() {
+    this->abandonReleaseCommon();
     SkASSERT(!this->impl()->isSetFlag((GrTextureFlags) GrTextureImpl::kReturnToCache_FlagBit));
     INHERITED::onRelease();
 }
 
 void GrTexture::onAbandon() {
+    this->abandonReleaseCommon();
     if (NULL != fRenderTarget.get()) {
         fRenderTarget->abandon();
     }