retool image cache to be generic cache, allowing the client to subclass "Rec", so they can provide a custom Key and arbitrary Value.

Follow-on CLs

- rename ScaledimageCache to something like GeneralCache
- explore if we can use call-backs or some mechanism to completely hide "lock/unlock", by forcing all clients to support "copying" their value out of the cache as the result of a Find.

R=mtklein@google.com, senorblanco@google.com, bsalomon@google.com, qiankun.miao@intel.com, senorblanco@chromium.org

Author: reed@google.com

Review URL: https://codereview.chromium.org/507483002
diff --git a/src/lazy/SkCachingPixelRef.cpp b/src/lazy/SkCachingPixelRef.cpp
index c596828..6d97aae 100644
--- a/src/lazy/SkCachingPixelRef.cpp
+++ b/src/lazy/SkCachingPixelRef.cpp
@@ -30,13 +30,11 @@
     : INHERITED(info)
     , fImageGenerator(generator)
     , fErrorInDecoding(false)
-    , fScaledCacheId(NULL)
     , fRowBytes(rowBytes) {
     SkASSERT(fImageGenerator != NULL);
 }
 SkCachingPixelRef::~SkCachingPixelRef() {
     SkDELETE(fImageGenerator);
-    SkASSERT(NULL == fScaledCacheId);
     // Assert always unlock before unref.
 }
 
@@ -46,48 +44,28 @@
     }
 
     const SkImageInfo& info = this->info();
-    SkBitmap bitmap;
-    SkASSERT(NULL == fScaledCacheId);
-    fScaledCacheId = SkBitmapCache::FindAndLock(this->getGenerationID(), info.fWidth, info.fHeight,
-                                                &bitmap);
-    if (NULL == fScaledCacheId) {
+    if (!SkBitmapCache::Find(this->getGenerationID(), info.fWidth, info.fHeight, &fLockedBitmap)) {
         // Cache has been purged, must re-decode.
-        if (!bitmap.allocPixels(info, fRowBytes)) {
+        if (!fLockedBitmap.allocPixels(info, fRowBytes)) {
             fErrorInDecoding = true;
             return false;
         }
-        SkAutoLockPixels autoLockPixels(bitmap);
-        if (!fImageGenerator->getPixels(info, bitmap.getPixels(), fRowBytes)) {
+        if (!fImageGenerator->getPixels(info, fLockedBitmap.getPixels(), fRowBytes)) {
             fErrorInDecoding = true;
             return false;
         }
-        fScaledCacheId = SkBitmapCache::AddAndLock(this->getGenerationID(),
-                                                   info.fWidth, info.fHeight, bitmap);
-        SkASSERT(fScaledCacheId != NULL);
+        SkBitmapCache::Add(this->getGenerationID(), info.fWidth, info.fHeight, fLockedBitmap);
     }
 
-    // Now bitmap should contain a concrete PixelRef of the decoded
-    // image.
-    SkAutoLockPixels autoLockPixels(bitmap);
-    void* pixels = bitmap.getPixels();
+    // Now bitmap should contain a concrete PixelRef of the decoded image.
+    void* pixels = fLockedBitmap.getPixels();
     SkASSERT(pixels != NULL);
-
-    // At this point, the autoLockPixels will unlockPixels()
-    // to remove bitmap's lock on the pixels.  We will then
-    // destroy bitmap.  The *only* guarantee that this pointer
-    // remains valid is the guarantee made by
-    // SkScaledImageCache that it will not destroy the *other*
-    // bitmap (SkScaledImageCache::Rec.fBitmap) that holds a
-    // reference to the concrete PixelRef while this record is
-    // locked.
     rec->fPixels = pixels;
     rec->fColorTable = NULL;
-    rec->fRowBytes = bitmap.rowBytes();
+    rec->fRowBytes = fLockedBitmap.rowBytes();
     return true;
 }
 
 void SkCachingPixelRef::onUnlockPixels() {
-    SkASSERT(fScaledCacheId != NULL);
-    SkScaledImageCache::Unlock( static_cast<SkScaledImageCache::ID*>(fScaledCacheId));
-    fScaledCacheId = NULL;
+    fLockedBitmap.reset();
 }