detect if the scaledimagecache returns a purged bitmap

BUG=
R=scroggo@google.com

Review URL: https://codereview.chromium.org/110383005

git-svn-id: http://skia.googlecode.com/svn/trunk@12654 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/core/SkScaledImageCache.cpp b/src/core/SkScaledImageCache.cpp
index b3956f4..7c8b664 100644
--- a/src/core/SkScaledImageCache.cpp
+++ b/src/core/SkScaledImageCache.cpp
@@ -239,9 +239,18 @@
         return fDM->data();
     }
 
-    SkASSERT(!fIsLocked);
-    fIsLocked = fDM->lock();
-    return fIsLocked ? fDM->data() : NULL;
+    // A previous call to onUnlock may have deleted our DM, so check for that
+    if (NULL == fDM) {
+        return NULL;
+    }
+
+    if (!fDM->lock()) {
+        // since it failed, we delete it now, to free-up the resource
+        delete fDM;
+        fDM = NULL;
+        return NULL;
+    }
+    return fDM->data();
 }
 
 void SkOneShotDiscardablePixelRef::onUnlockPixels() {
@@ -613,6 +622,8 @@
     this->validate();
 }
 
+///////////////////////////////////////////////////////////////////////////////
+
 #ifdef SK_DEBUG
 void SkScaledImageCache::validate() const {
     if (NULL == fHead) {
@@ -658,6 +669,21 @@
 }
 #endif
 
+void SkScaledImageCache::dump() const {
+    this->validate();
+
+    const Rec* rec = fHead;
+    int locked = 0;
+    while (rec) {
+        locked += rec->fLockCount > 0;
+        rec = rec->fNext;
+    }
+
+    SkDebugf("SkScaledImageCache: count=%d bytes=%d locked=%d %s\n",
+             fCount, fBytesUsed, locked,
+             fDiscardableFactory ? "discardable" : "malloc");
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 
 #include "SkThread.h"
@@ -730,7 +756,9 @@
 
 void SkScaledImageCache::Unlock(SkScaledImageCache::ID* id) {
     SkAutoMutexAcquire am(gMutex);
-    return get_cache()->unlock(id);
+    get_cache()->unlock(id);
+
+//    get_cache()->dump();
 }
 
 size_t SkScaledImageCache::GetBytesUsed() {
@@ -753,6 +781,11 @@
     return get_cache()->allocator();
 }
 
+void SkScaledImageCache::Dump() {
+    SkAutoMutexAcquire am(gMutex);
+    get_cache()->dump();
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 
 #include "SkGraphics.h"