rename ScaledImageCache to ResourceCache

BUG=skia:
R=bsalomon@google.com, mtklein@google.com

Author: reed@google.com

Review URL: https://codereview.chromium.org/511283002
diff --git a/src/core/SkBitmapCache.cpp b/src/core/SkBitmapCache.cpp
index c0ce0e6..99e2f5c 100644
--- a/src/core/SkBitmapCache.cpp
+++ b/src/core/SkBitmapCache.cpp
@@ -6,7 +6,7 @@
  */
 
 #include "SkBitmapCache.h"
-#include "SkScaledImageCache.h"
+#include "SkResourceCache.h"
 #include "SkMipMap.h"
 #include "SkRect.h"
 
@@ -24,7 +24,7 @@
     return SkIRect::MakeXYWH(origin.fX, origin.fY, bm.width(), bm.height());
 }
 
-struct BitmapKey : public SkScaledImageCache::Key {
+struct BitmapKey : public SkResourceCache::Key {
 public:
     BitmapKey(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& bounds)
     : fGenID(genID)
@@ -43,7 +43,7 @@
 
 //////////////////////////////////////////////////////////////////////////////////////////
 
-struct BitmapRec : public SkScaledImageCache::Rec {
+struct BitmapRec : public SkResourceCache::Rec {
     BitmapRec(uint32_t genID, SkScalar scaleX, SkScalar scaleY, const SkIRect& bounds,
               const SkBitmap& result)
         : fKey(genID, scaleX, scaleY, bounds)
@@ -58,10 +58,10 @@
 };
 
 static bool find_and_return(const BitmapKey& key, SkBitmap* result) {
-    const BitmapRec* rec = (BitmapRec*)SkScaledImageCache::FindAndLock(key);
+    const BitmapRec* rec = (BitmapRec*)SkResourceCache::FindAndLock(key);
     if (rec) {
         *result = rec->fBitmap;
-        SkScaledImageCache::Unlock(rec);
+        SkResourceCache::Unlock(rec);
 
         result->lockPixels();
         if (result->getPixels()) {
@@ -91,9 +91,8 @@
         // degenerate, and the key we use for mipmaps
         return;
     }
-    SkScaledImageCache::Add(SkNEW_ARGS(BitmapRec,
-                                       (src.getGenerationID(), invScaleX, invScaleY,
-                                        get_bounds_from_bitmap(src), result)));
+    SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (src.getGenerationID(), invScaleX, invScaleY,
+                                                get_bounds_from_bitmap(src), result)));
 }
 
 bool SkBitmapCache::Find(uint32_t genID, int width, int height, SkBitmap* result) {
@@ -102,13 +101,13 @@
 }
 
 void SkBitmapCache::Add(uint32_t genID, int width, int height, const SkBitmap& result) {
-    SkScaledImageCache::Add(SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar1,
-                                                   SkIRect::MakeWH(width, height), result)));
+    SkResourceCache::Add(SkNEW_ARGS(BitmapRec, (genID, SK_Scalar1, SK_Scalar1,
+                                                SkIRect::MakeWH(width, height), result)));
 }
 
 //////////////////////////////////////////////////////////////////////////////////////////
 
-struct MipMapRec : public SkScaledImageCache::Rec {
+struct MipMapRec : public SkResourceCache::Rec {
     MipMapRec(const SkBitmap& src, const SkMipMap* result)
         : fKey(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src))
         , fMipMap(SkRef(result))
@@ -128,18 +127,18 @@
 
 const SkMipMap* SkMipMapCache::FindAndRef(const SkBitmap& src) {
     BitmapKey key(src.getGenerationID(), 0, 0, get_bounds_from_bitmap(src));
-    const MipMapRec* rec = (MipMapRec*)SkScaledImageCache::FindAndLock(key);
+    const MipMapRec* rec = (MipMapRec*)SkResourceCache::FindAndLock(key);
     const SkMipMap* result = NULL;
     if (rec) {
         result = SkRef(rec->fMipMap);
-        SkScaledImageCache::Unlock(rec);
+        SkResourceCache::Unlock(rec);
     }
     return result;
 }
 
 void SkMipMapCache::Add(const SkBitmap& src, const SkMipMap* result) {
     if (result) {
-        SkScaledImageCache::Add(SkNEW_ARGS(MipMapRec, (src, result)));
+        SkResourceCache::Add(SkNEW_ARGS(MipMapRec, (src, result)));
     }
 }
 
diff --git a/src/core/SkBitmapProcState.cpp b/src/core/SkBitmapProcState.cpp
index 7640573..52a2ecf 100644
--- a/src/core/SkBitmapProcState.cpp
+++ b/src/core/SkBitmapProcState.cpp
@@ -16,6 +16,7 @@
 #include "SkMipMap.h"
 #include "SkPixelRef.h"
 #include "SkImageEncoder.h"
+#include "SkResourceCache.h"
 
 #if !SK_ARM_NEON_IS_NONE
 // These are defined in src/opts/SkBitmapProcState_arm_neon.cpp
@@ -107,31 +108,10 @@
     return SkMaxScalar(v1.lengthSqd(), v2.lengthSqd());
 }
 
-class AutoScaledCacheUnlocker {
-public:
-    AutoScaledCacheUnlocker(SkScaledImageCache::ID* idPtr) : fIDPtr(idPtr) {}
-    ~AutoScaledCacheUnlocker() {
-        if (fIDPtr && *fIDPtr) {
-            SkScaledImageCache::Unlock(*fIDPtr);
-            *fIDPtr = NULL;
-        }
-    }
-
-    // forgets the ID, so it won't call Unlock
-    void release() {
-        fIDPtr = NULL;
-    }
-
-private:
-    SkScaledImageCache::ID* fIDPtr;
-};
-#define AutoScaledCacheUnlocker(...) SK_REQUIRE_LOCAL_VAR(AutoScaledCacheUnlocker)
-
 // Check to see that the size of the bitmap that would be produced by
 // scaling by the given inverted matrix is less than the maximum allowed.
 static inline bool cache_size_okay(const SkBitmap& bm, const SkMatrix& invMat) {
-    size_t maximumAllocation
-        = SkScaledImageCache::GetSingleAllocationByteLimit();
+    size_t maximumAllocation = SkResourceCache::GetSingleAllocationByteLimit();
     if (0 == maximumAllocation) {
         return true;
     }
@@ -191,7 +171,7 @@
                                         SkBitmapScaler::RESIZE_BEST,
                                         dest_width,
                                         dest_height,
-                                        SkScaledImageCache::GetAllocator())) {
+                                        SkResourceCache::GetAllocator())) {
                 // we failed to create fScaledBitmap, so just return and let
                 // the scanline proc handle it.
                 return false;
diff --git a/src/core/SkBitmapProcState.h b/src/core/SkBitmapProcState.h
index 64dda2e..289e308 100644
--- a/src/core/SkBitmapProcState.h
+++ b/src/core/SkBitmapProcState.h
@@ -1,4 +1,3 @@
-
 /*
  * Copyright 2007 The Android Open Source Project
  *
@@ -6,7 +5,6 @@
  * found in the LICENSE file.
  */
 
-
 #ifndef SkBitmapProcState_DEFINED
 #define SkBitmapProcState_DEFINED
 
@@ -15,7 +13,6 @@
 #include "SkMatrix.h"
 #include "SkMipMap.h"
 #include "SkPaint.h"
-#include "SkScaledImageCache.h"
 
 #define FractionalInt_IS_64BIT
 
diff --git a/src/core/SkScaledImageCache.cpp b/src/core/SkResourceCache.cpp
similarity index 76%
rename from src/core/SkScaledImageCache.cpp
rename to src/core/SkResourceCache.cpp
index 9d15422..7ffdfff 100644
--- a/src/core/SkScaledImageCache.cpp
+++ b/src/core/SkResourceCache.cpp
@@ -6,7 +6,7 @@
  */
 
 #include "SkChecksum.h"
-#include "SkScaledImageCache.h"
+#include "SkResourceCache.h"
 #include "SkMipMap.h"
 #include "SkPixelRef.h"
 
@@ -21,7 +21,7 @@
     #define SK_DEFAULT_IMAGE_CACHE_LIMIT     (2 * 1024 * 1024)
 #endif
 
-void SkScaledImageCache::Key::init(size_t length) {
+void SkResourceCache::Key::init(size_t length) {
     SkASSERT(SkAlign4(length) == length);
     // 2 is fCount32 and fHash
     fCount32 = SkToS32(2 + (length >> 2));
@@ -31,8 +31,8 @@
 
 #include "SkTDynamicHash.h"
 
-class SkScaledImageCache::Hash :
-    public SkTDynamicHash<SkScaledImageCache::Rec, SkScaledImageCache::Key> {};
+class SkResourceCache::Hash :
+    public SkTDynamicHash<SkResourceCache::Rec, SkResourceCache::Key> {};
 
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -41,9 +41,9 @@
 #define USE_HASH
 
 #if !defined(USE_HASH)
-static inline SkScaledImageCache::Rec* find_rec_in_list(
-        SkScaledImageCache::Rec* head, const Key & key) {
-    SkScaledImageCache::Rec* rec = head;
+static inline SkResourceCache::Rec* find_rec_in_list(
+        SkResourceCache::Rec* head, const Key & key) {
+    SkResourceCache::Rec* rec = head;
     while ((rec != NULL) && (rec->fKey != key)) {
         rec = rec->fNext;
     }
@@ -51,7 +51,7 @@
 }
 #endif
 
-void SkScaledImageCache::init() {
+void SkResourceCache::init() {
     fHead = NULL;
     fTail = NULL;
 #ifdef USE_HASH
@@ -142,10 +142,9 @@
     return this->info().getSafeSize(fRB);
 }
 
-class SkScaledImageCacheDiscardableAllocator : public SkBitmap::Allocator {
+class SkResourceCacheDiscardableAllocator : public SkBitmap::Allocator {
 public:
-    SkScaledImageCacheDiscardableAllocator(
-                            SkScaledImageCache::DiscardableFactory factory) {
+    SkResourceCacheDiscardableAllocator(SkResourceCache::DiscardableFactory factory) {
         SkASSERT(factory);
         fFactory = factory;
     }
@@ -153,11 +152,10 @@
     virtual bool allocPixelRef(SkBitmap*, SkColorTable*) SK_OVERRIDE;
 
 private:
-    SkScaledImageCache::DiscardableFactory fFactory;
+    SkResourceCache::DiscardableFactory fFactory;
 };
 
-bool SkScaledImageCacheDiscardableAllocator::allocPixelRef(SkBitmap* bitmap,
-                                                       SkColorTable* ctable) {
+bool SkResourceCacheDiscardableAllocator::allocPixelRef(SkBitmap* bitmap, SkColorTable* ctable) {
     size_t size = bitmap->getSize();
     uint64_t size64 = bitmap->computeSize64();
     if (0 == size || size64 > (uint64_t)size) {
@@ -181,19 +179,19 @@
     return bitmap->readyToDraw();
 }
 
-SkScaledImageCache::SkScaledImageCache(DiscardableFactory factory) {
+SkResourceCache::SkResourceCache(DiscardableFactory factory) {
     this->init();
     fDiscardableFactory = factory;
 
-    fAllocator = SkNEW_ARGS(SkScaledImageCacheDiscardableAllocator, (factory));
+    fAllocator = SkNEW_ARGS(SkResourceCacheDiscardableAllocator, (factory));
 }
 
-SkScaledImageCache::SkScaledImageCache(size_t byteLimit) {
+SkResourceCache::SkResourceCache(size_t byteLimit) {
     this->init();
     fTotalByteLimit = byteLimit;
 }
 
-SkScaledImageCache::~SkScaledImageCache() {
+SkResourceCache::~SkResourceCache() {
     SkSafeUnref(fAllocator);
 
     Rec* rec = fHead;
@@ -207,7 +205,7 @@
 
 ////////////////////////////////////////////////////////////////////////////////
 
-const SkScaledImageCache::Rec* SkScaledImageCache::findAndLock(const Key& key) {
+const SkResourceCache::Rec* SkResourceCache::findAndLock(const Key& key) {
 #ifdef USE_HASH
     Rec* rec = fHash->find(key);
 #else
@@ -220,7 +218,7 @@
     return rec;
 }
 
-const SkScaledImageCache::Rec* SkScaledImageCache::addAndLock(Rec* rec) {
+const SkResourceCache::Rec* SkResourceCache::addAndLock(Rec* rec) {
     SkASSERT(rec);
     // See if we already have this key (racy inserts, etc.)
     const Rec* existing = this->findAndLock(rec->getKey());
@@ -240,7 +238,7 @@
     return rec;
 }
 
-void SkScaledImageCache::add(Rec* rec) {
+void SkResourceCache::add(Rec* rec) {
     SkASSERT(rec);
     // See if we already have this key (racy inserts, etc.)
     const Rec* existing = this->findAndLock(rec->getKey());
@@ -259,7 +257,7 @@
     this->unlock(rec);
 }
 
-void SkScaledImageCache::unlock(SkScaledImageCache::ID id) {
+void SkResourceCache::unlock(SkResourceCache::ID id) {
     SkASSERT(id);
 
 #ifdef SK_DEBUG
@@ -288,7 +286,7 @@
     }
 }
 
-void SkScaledImageCache::purgeAsNeeded() {
+void SkResourceCache::purgeAsNeeded() {
     size_t byteLimit;
     int    countLimit;
 
@@ -330,7 +328,7 @@
     fCount = countUsed;
 }
 
-size_t SkScaledImageCache::setTotalByteLimit(size_t newLimit) {
+size_t SkResourceCache::setTotalByteLimit(size_t newLimit) {
     size_t prevLimit = fTotalByteLimit;
     fTotalByteLimit = newLimit;
     if (newLimit < prevLimit) {
@@ -341,7 +339,7 @@
 
 ///////////////////////////////////////////////////////////////////////////////
 
-void SkScaledImageCache::detach(Rec* rec) {
+void SkResourceCache::detach(Rec* rec) {
     Rec* prev = rec->fPrev;
     Rec* next = rec->fNext;
 
@@ -361,7 +359,7 @@
     rec->fNext = rec->fPrev = NULL;
 }
 
-void SkScaledImageCache::moveToHead(Rec* rec) {
+void SkResourceCache::moveToHead(Rec* rec) {
     if (fHead == rec) {
         return;
     }
@@ -380,7 +378,7 @@
     this->validate();
 }
 
-void SkScaledImageCache::addToHead(Rec* rec) {
+void SkResourceCache::addToHead(Rec* rec) {
     this->validate();
 
     rec->fPrev = NULL;
@@ -401,7 +399,7 @@
 ///////////////////////////////////////////////////////////////////////////////
 
 #ifdef SK_DEBUG
-void SkScaledImageCache::validate() const {
+void SkResourceCache::validate() const {
     if (NULL == fHead) {
         SkASSERT(NULL == fTail);
         SkASSERT(0 == fTotalBytesUsed);
@@ -445,7 +443,7 @@
 }
 #endif
 
-void SkScaledImageCache::dump() const {
+void SkResourceCache::dump() const {
     this->validate();
 
     const Rec* rec = fHead;
@@ -455,18 +453,18 @@
         rec = rec->fNext;
     }
 
-    SkDebugf("SkScaledImageCache: count=%d bytes=%d locked=%d %s\n",
+    SkDebugf("SkResourceCache: count=%d bytes=%d locked=%d %s\n",
              fCount, fTotalBytesUsed, locked,
              fDiscardableFactory ? "discardable" : "malloc");
 }
 
-size_t SkScaledImageCache::setSingleAllocationByteLimit(size_t newLimit) {
+size_t SkResourceCache::setSingleAllocationByteLimit(size_t newLimit) {
     size_t oldLimit = fSingleAllocationByteLimit;
     fSingleAllocationByteLimit = newLimit;
     return oldLimit;
 }
 
-size_t SkScaledImageCache::getSingleAllocationByteLimit() const {
+size_t SkResourceCache::getSingleAllocationByteLimit() const {
     return fSingleAllocationByteLimit;
 }
 
@@ -475,85 +473,85 @@
 #include "SkThread.h"
 
 SK_DECLARE_STATIC_MUTEX(gMutex);
-static SkScaledImageCache* gScaledImageCache = NULL;
-static void cleanup_gScaledImageCache() {
+static SkResourceCache* gResourceCache = NULL;
+static void cleanup_gResourceCache() {
     // We'll clean this up in our own tests, but disable for clients.
     // Chrome seems to have funky multi-process things going on in unit tests that
     // makes this unsafe to delete when the main process atexit()s.
     // SkLazyPtr does the same sort of thing.
 #if SK_DEVELOPER
-    SkDELETE(gScaledImageCache);
+    SkDELETE(gResourceCache);
 #endif
 }
 
 /** Must hold gMutex when calling. */
-static SkScaledImageCache* get_cache() {
+static SkResourceCache* get_cache() {
     // gMutex is always held when this is called, so we don't need to be fancy in here.
     gMutex.assertHeld();
-    if (NULL == gScaledImageCache) {
+    if (NULL == gResourceCache) {
 #ifdef SK_USE_DISCARDABLE_SCALEDIMAGECACHE
-        gScaledImageCache = SkNEW_ARGS(SkScaledImageCache, (SkDiscardableMemory::Create));
+        gResourceCache = SkNEW_ARGS(SkResourceCache, (SkDiscardableMemory::Create));
 #else
-        gScaledImageCache = SkNEW_ARGS(SkScaledImageCache, (SK_DEFAULT_IMAGE_CACHE_LIMIT));
+        gResourceCache = SkNEW_ARGS(SkResourceCache, (SK_DEFAULT_IMAGE_CACHE_LIMIT));
 #endif
-        atexit(cleanup_gScaledImageCache);
+        atexit(cleanup_gResourceCache);
     }
-    return gScaledImageCache;
+    return gResourceCache;
 }
 
-void SkScaledImageCache::Unlock(SkScaledImageCache::ID id) {
+void SkResourceCache::Unlock(SkResourceCache::ID id) {
     SkAutoMutexAcquire am(gMutex);
     get_cache()->unlock(id);
 
 //    get_cache()->dump();
 }
 
-size_t SkScaledImageCache::GetTotalBytesUsed() {
+size_t SkResourceCache::GetTotalBytesUsed() {
     SkAutoMutexAcquire am(gMutex);
     return get_cache()->getTotalBytesUsed();
 }
 
-size_t SkScaledImageCache::GetTotalByteLimit() {
+size_t SkResourceCache::GetTotalByteLimit() {
     SkAutoMutexAcquire am(gMutex);
     return get_cache()->getTotalByteLimit();
 }
 
-size_t SkScaledImageCache::SetTotalByteLimit(size_t newLimit) {
+size_t SkResourceCache::SetTotalByteLimit(size_t newLimit) {
     SkAutoMutexAcquire am(gMutex);
     return get_cache()->setTotalByteLimit(newLimit);
 }
 
-SkBitmap::Allocator* SkScaledImageCache::GetAllocator() {
+SkBitmap::Allocator* SkResourceCache::GetAllocator() {
     SkAutoMutexAcquire am(gMutex);
     return get_cache()->allocator();
 }
 
-void SkScaledImageCache::Dump() {
+void SkResourceCache::Dump() {
     SkAutoMutexAcquire am(gMutex);
     get_cache()->dump();
 }
 
-size_t SkScaledImageCache::SetSingleAllocationByteLimit(size_t size) {
+size_t SkResourceCache::SetSingleAllocationByteLimit(size_t size) {
     SkAutoMutexAcquire am(gMutex);
     return get_cache()->setSingleAllocationByteLimit(size);
 }
 
-size_t SkScaledImageCache::GetSingleAllocationByteLimit() {
+size_t SkResourceCache::GetSingleAllocationByteLimit() {
     SkAutoMutexAcquire am(gMutex);
     return get_cache()->getSingleAllocationByteLimit();
 }
 
-const SkScaledImageCache::Rec* SkScaledImageCache::FindAndLock(const Key& key) {
+const SkResourceCache::Rec* SkResourceCache::FindAndLock(const Key& key) {
     SkAutoMutexAcquire am(gMutex);
     return get_cache()->findAndLock(key);
 }
 
-const SkScaledImageCache::Rec* SkScaledImageCache::AddAndLock(Rec* rec) {
+const SkResourceCache::Rec* SkResourceCache::AddAndLock(Rec* rec) {
     SkAutoMutexAcquire am(gMutex);
     return get_cache()->addAndLock(rec);
 }
 
-void SkScaledImageCache::Add(Rec* rec) {
+void SkResourceCache::Add(Rec* rec) {
     SkAutoMutexAcquire am(gMutex);
     get_cache()->add(rec);
 }
@@ -562,23 +560,23 @@
 
 #include "SkGraphics.h"
 
-size_t SkGraphics::GetImageCacheTotalBytesUsed() {
-    return SkScaledImageCache::GetTotalBytesUsed();
+size_t SkGraphics::GetResourceCacheTotalBytesUsed() {
+    return SkResourceCache::GetTotalBytesUsed();
 }
 
-size_t SkGraphics::GetImageCacheTotalByteLimit() {
-    return SkScaledImageCache::GetTotalByteLimit();
+size_t SkGraphics::GetResourceCacheTotalByteLimit() {
+    return SkResourceCache::GetTotalByteLimit();
 }
 
-size_t SkGraphics::SetImageCacheTotalByteLimit(size_t newLimit) {
-    return SkScaledImageCache::SetTotalByteLimit(newLimit);
+size_t SkGraphics::SetResourceCacheTotalByteLimit(size_t newLimit) {
+    return SkResourceCache::SetTotalByteLimit(newLimit);
 }
 
-size_t SkGraphics::GetImageCacheSingleAllocationByteLimit() {
-    return SkScaledImageCache::GetSingleAllocationByteLimit();
+size_t SkGraphics::GetResourceCacheSingleAllocationByteLimit() {
+    return SkResourceCache::GetSingleAllocationByteLimit();
 }
 
-size_t SkGraphics::SetImageCacheSingleAllocationByteLimit(size_t newLimit) {
-    return SkScaledImageCache::SetSingleAllocationByteLimit(newLimit);
+size_t SkGraphics::SetResourceCacheSingleAllocationByteLimit(size_t newLimit) {
+    return SkResourceCache::SetSingleAllocationByteLimit(newLimit);
 }
 
diff --git a/src/core/SkScaledImageCache.h b/src/core/SkResourceCache.h
similarity index 94%
rename from src/core/SkScaledImageCache.h
rename to src/core/SkResourceCache.h
index c31f8ff..0873bd4 100644
--- a/src/core/SkScaledImageCache.h
+++ b/src/core/SkResourceCache.h
@@ -5,8 +5,8 @@
  * found in the LICENSE file.
  */
 
-#ifndef SkScaledImageCache_DEFINED
-#define SkScaledImageCache_DEFINED
+#ifndef SkResourceCache_DEFINED
+#define SkResourceCache_DEFINED
 
 #include "SkBitmap.h"
 
@@ -23,7 +23,7 @@
  *  As a convenience, a global instance is also defined, which can be safely
  *  access across threads via the static methods (e.g. FindAndLock, etc.).
  */
-class SkScaledImageCache {
+class SkResourceCache {
 public:
     struct Key {
         // Call this to access your private contents. Must not use the address after calling init()
@@ -58,7 +58,7 @@
     };
 
     struct Rec {
-        typedef SkScaledImageCache::Key Key;
+        typedef SkResourceCache::Key Key;
 
         Rec() : fLockCount(1) {}
         virtual ~Rec() {}
@@ -78,7 +78,7 @@
         int32_t fLockCount;
         int32_t fPad;
         
-        friend class SkScaledImageCache;
+        friend class SkResourceCache;
     };
 
     typedef const Rec* ID;
@@ -122,7 +122,7 @@
      *  and getTotalByteLimit() will return 0, and setTotalByteLimit
      *  will ignore its argument and return 0.
      */
-    SkScaledImageCache(DiscardableFactory);
+    SkResourceCache(DiscardableFactory);
 
     /**
      *  Construct the cache, allocating memory with malloc, and respect the
@@ -130,8 +130,8 @@
      *  that pushes the total bytesUsed over the limit. Note: The limit can be
      *  changed at runtime with setTotalByteLimit.
      */
-    explicit SkScaledImageCache(size_t byteLimit);
-    ~SkScaledImageCache();
+    explicit SkResourceCache(size_t byteLimit);
+    ~SkResourceCache();
 
     const Rec* findAndLock(const Key& key);
     const Rec* addAndLock(Rec*);