Don't reuse scratch textures patch

https://codereview.chromium.org/24222004/



git-svn-id: http://skia.googlecode.com/svn/trunk/include@11997 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gpu/GrResource.h b/gpu/GrResource.h
index a3291e0..3fb2e77 100644
--- a/gpu/GrResource.h
+++ b/gpu/GrResource.h
@@ -66,10 +66,24 @@
     void setCacheEntry(GrResourceEntry* cacheEntry) { fCacheEntry = cacheEntry; }
     GrResourceEntry* getCacheEntry() { return fCacheEntry; }
 
-    void incDeferredRefCount() const { SkASSERT(fDeferredRefCount >= 0); ++fDeferredRefCount; }
-    void decDeferredRefCount() const { SkASSERT(fDeferredRefCount > 0); --fDeferredRefCount; }
+    void incDeferredRefCount() const { 
+        SkASSERT(fDeferredRefCount >= 0); 
+        ++fDeferredRefCount; 
+    }
+
+    void decDeferredRefCount() const { 
+        SkASSERT(fDeferredRefCount > 0); 
+        --fDeferredRefCount; 
+        if (0 == fDeferredRefCount && this->needsDeferredUnref()) {
+            SkASSERT(this->getRefCnt() > 1);
+            this->unref();
+        }
+    }
+
     int getDeferredRefCount() const { return fDeferredRefCount; }
 
+    void setNeedsDeferredUnref() { fFlags |= kDeferredUnref_FlagBit; }
+
 protected:
     /**
      * isWrapped indicates we have wrapped a client-created backend resource in a GrResource. If it
@@ -87,7 +101,8 @@
     virtual void onAbandon() {};
 
     bool isInCache() const { return NULL != fCacheEntry; }
-    bool isWrapped() const { return kWrapped_Flag & fFlags; }
+    bool isWrapped() const { return kWrapped_FlagBit & fFlags; }
+    bool needsDeferredUnref() const { return SkToBool(kDeferredUnref_FlagBit & fFlags); }
 
 private:
 #ifdef SK_DEBUG
@@ -105,7 +120,20 @@
     mutable int         fDeferredRefCount;  // How many references in deferred drawing buffers.
 
     enum Flags {
-        kWrapped_Flag = 0x1,
+        /**
+         * This resource wraps a GPU resource given to us by the user.
+         * Lifetime management is left up to the user (i.e., we will not 
+         * free it).
+         */
+        kWrapped_FlagBit         = 0x1,
+
+        /**
+         * This texture should be de-refed when the deferred ref count goes
+         * to zero. A resource gets into this state when the resource cache
+         * is holding a ref-of-obligation (i.e., someone needs to own it but
+         * no one else wants to) but doesn't really want to keep it around.
+         */
+        kDeferredUnref_FlagBit  = 0x2,
     };
     uint32_t         fFlags;