add more tests for discardable caches

BUG=
R=halcanary@google.com

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

git-svn-id: http://skia.googlecode.com/svn/trunk/src@12618 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/core/SkScaledImageCache.cpp b/core/SkScaledImageCache.cpp
index 5b16688..b3956f4 100644
--- a/core/SkScaledImageCache.cpp
+++ b/core/SkScaledImageCache.cpp
@@ -209,6 +209,7 @@
     SkDiscardableMemory* fDM;
     size_t               fRB;
     bool                 fFirstTime;
+    bool                 fIsLocked;
 
     typedef SkPixelRef INHERITED;
 };
@@ -224,6 +225,7 @@
 
     SkASSERT(dm->data());
     fFirstTime = true;
+    fIsLocked = false;
 }
 
 SkOneShotDiscardablePixelRef::~SkOneShotDiscardablePixelRef() {
@@ -236,12 +238,18 @@
         fFirstTime = false;
         return fDM->data();
     }
-    return fDM->lock() ? fDM->data() : NULL;
+
+    SkASSERT(!fIsLocked);
+    fIsLocked = fDM->lock();
+    return fIsLocked ? fDM->data() : NULL;
 }
 
 void SkOneShotDiscardablePixelRef::onUnlockPixels() {
     SkASSERT(!fFirstTime);
-    fDM->unlock();
+    if (fIsLocked) {
+        fIsLocked = false;
+        fDM->unlock();
+    }
 }
 
 size_t SkOneShotDiscardablePixelRef::getAllocatedSizeInBytes() const {
diff --git a/lazy/SkDiscardableMemoryPool.h b/lazy/SkDiscardableMemoryPool.h
index e9f3b04..61bad70 100644
--- a/lazy/SkDiscardableMemoryPool.h
+++ b/lazy/SkDiscardableMemoryPool.h
@@ -12,6 +12,8 @@
 #include "SkTInternalLList.h"
 #include "SkThread.h"
 
+class SkPoolDiscardableMemory;
+
 #ifdef SK_DEBUG
     #define LAZY_CACHE_STATS 1
 #elif !defined(LAZY_CACHE_STATS)
@@ -22,20 +24,22 @@
  *  This non-global pool can be used for unit tests to verify that the
  *  pool works.
  */
-class SkPoolDiscardableMemory;
 class SkDiscardableMemoryPool : public SkDiscardableMemory::Factory {
 public:
     /**
      *  Without mutex, will be not be thread safe.
      */
     SkDiscardableMemoryPool(size_t budget, SkBaseMutex* mutex = NULL);
-    ~SkDiscardableMemoryPool();
-    SkDiscardableMemory* create(size_t bytes) SK_OVERRIDE;
+    virtual ~SkDiscardableMemoryPool();
+
+    virtual SkDiscardableMemory* create(size_t bytes) SK_OVERRIDE;
+
     size_t getRAMUsed();
     void setRAMBudget(size_t budget);
+
     /** purges all unlocked DMs */
     void dumpPool();
-    friend class SkPoolDiscardableMemory;
+
     #if LAZY_CACHE_STATS
     int          fCacheHits;
     int          fCacheMisses;
@@ -46,6 +50,7 @@
     size_t       fBudget;
     size_t       fUsed;
     SkTInternalLList<SkPoolDiscardableMemory> fList;
+
     /** Function called to free memory if needed */
     void dumpDownTo(size_t budget);
     /** called by SkDiscardableMemoryPool upon destruction */
@@ -54,6 +59,9 @@
     bool lock(SkPoolDiscardableMemory* dm);
     /** called by SkDiscardableMemoryPool::unlock() */
     void unlock(SkPoolDiscardableMemory* dm);
+
+    friend class SkPoolDiscardableMemory;
+
     typedef SkDiscardableMemory::Factory INHERITED;
 };