Break up SkLazyPixelRef functionally into class hierarchy.

The reason for this CL is to allow greater decoder flexibility.
Chrome currently uses its own decoding functions.  These allow for
greater flexibility in dealing with images with multiple frames or
partial data.  The DecodeProc function was not flexible enough to
handle these.  Instead of asking the decoder to squeeze everything
into the DecodeProc, we now ask the downstream library to inherit from
SkCachingPixelRef.  If WebKit's LazyDecodingPixelRef is re-tooled to
inherit from SkCachingPixelRef, then it can make use of Skia's caching
ability while still allowing it to deal with multiple frames, scaling,
subsetting, and partial data.

-   The abstract SkCachingPixelRef class handles caching the decoded
    data in a SkScaledImageCache.  This class relies on the virtual
    functions onDecodeInfo() and onDecode() to do the actual decoding
    of data.

-   The SkLazyCachingPixelRef class is derived from SkCachingPixelRef.
    It provides an implementation of onDecodeInfo() and onDecode() in
    terms of calls to a SkBitmapFactory::DecodeProc function.  It also
    provides an Install() static method which installs a new
    SkLazyCachingPixelRef into a SkBitmap.

SkLazyCachingPixelRef exists for two reasons: to test
SkCachingPixelRef within Skia and as an example for downstream
developers to make their own classes that inherit from
SkCachingPixelRef.

-   The CachedDecodingPixelRefTest was updated to test the
    SkLazyCachingPixelRef class and indirectly the SkCachingPixelRef
    class.

BUG=
R=reed@google.com, scroggo@google.com

Author: halcanary@google.com

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

git-svn-id: http://skia.googlecode.com/svn/trunk@12149 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/gyp/core.gypi b/gyp/core.gypi
index b96a2ec..3000c70 100644
--- a/gyp/core.gypi
+++ b/gyp/core.gypi
@@ -322,6 +322,11 @@
         '<(skia_src_path)/lazy/SkPurgeableMemoryBlock_common.cpp',
         '<(skia_src_path)/lazy/SkPurgeableImageCache.cpp',
 
+        '<(skia_src_path)/lazy/SkCachingPixelRef.cpp',
+        '<(skia_src_path)/lazy/SkCachingPixelRef.h',
+        '<(skia_src_path)/lazy/SkLazyCachingPixelRef.cpp',
+        '<(skia_src_path)/lazy/SkLazyCachingPixelRef.h',
+
         # Path ops
         '<(skia_include_path)/pathops/SkPathOps.h',
 
diff --git a/include/core/SkBitmap.h b/include/core/SkBitmap.h
index 59b6482..64f2e24 100644
--- a/include/core/SkBitmap.h
+++ b/include/core/SkBitmap.h
@@ -17,6 +17,7 @@
 #include "SkPoint.h"
 #include "SkRefCnt.h"
 
+struct SkImageInfo;
 struct SkIRect;
 struct SkRect;
 class SkPaint;
@@ -259,6 +260,7 @@
                                kPremul_SkAlphaType);
     }
 
+    bool setConfig(const SkImageInfo& info, size_t rowBytes = 0);
 
     /** Use this to assign a new pixel address for an existing bitmap. This
         will automatically release any pixelref previously installed. Only call
diff --git a/include/core/SkImage.h b/include/core/SkImage.h
index 97dc799..ae8bbf5 100644
--- a/include/core/SkImage.h
+++ b/include/core/SkImage.h
@@ -47,6 +47,9 @@
     SkAlphaType fAlphaType;
 };
 
+bool operator==(const SkImageInfo& lhs, const SkImageInfo& rhs);
+bool operator!=(const SkImageInfo& lhs, const SkImageInfo& rhs);
+
 /**
  *  SkImage is an abstraction for drawing a rectagle of pixels, though the
  *  particular type of image could be actually storing its data on the GPU, or
diff --git a/src/core/SkBitmap.cpp b/src/core/SkBitmap.cpp
index bff0880..2fae75a 100644
--- a/src/core/SkBitmap.cpp
+++ b/src/core/SkBitmap.cpp
@@ -11,6 +11,7 @@
 #include "SkColorPriv.h"
 #include "SkDither.h"
 #include "SkFlattenable.h"
+#include "SkImagePriv.h"
 #include "SkMallocPixelRef.h"
 #include "SkMask.h"
 #include "SkOrderedReadBuffer.h"
@@ -332,6 +333,11 @@
     return false;
 }
 
+bool SkBitmap::setConfig(const SkImageInfo& info, size_t rowBytes) {
+    return this->setConfig(SkImageInfoToBitmapConfig(info), info.fWidth,
+                           info.fHeight, rowBytes, info.fAlphaType);
+}
+
 bool SkBitmap::setAlphaType(SkAlphaType alphaType) {
     if (!validate_alphaType(this->config(), alphaType, &alphaType)) {
         return false;
diff --git a/src/image/SkImage.cpp b/src/image/SkImage.cpp
index 39fd93a..c8559e6 100644
--- a/src/image/SkImage.cpp
+++ b/src/image/SkImage.cpp
@@ -12,6 +12,14 @@
 
 SK_DEFINE_INST_COUNT(SkImage)
 
+bool operator==(const SkImageInfo& lhs, const SkImageInfo& rhs) {
+    return 0 == memcmp(&lhs, &rhs, sizeof(SkImageInfo));
+}
+bool operator!=(const SkImageInfo& lhs, const SkImageInfo& rhs) {
+    return 0 != memcmp(&lhs, &rhs, sizeof(SkImageInfo));
+}
+
+
 static SkImage_Base* as_IB(SkImage* image) {
     return static_cast<SkImage_Base*>(image);
 }
diff --git a/src/lazy/SkCachingPixelRef.cpp b/src/lazy/SkCachingPixelRef.cpp
new file mode 100644
index 0000000..2761f1e
--- /dev/null
+++ b/src/lazy/SkCachingPixelRef.cpp
@@ -0,0 +1,112 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "SkCachingPixelRef.h"
+#include "SkScaledImageCache.h"
+
+SkCachingPixelRef::SkCachingPixelRef()
+    : fErrorInDecoding(false)
+    , fScaledCacheId(NULL) {
+    memset(&fInfo, 0xFF, sizeof(fInfo));
+}
+SkCachingPixelRef::~SkCachingPixelRef() {
+    SkASSERT(NULL == fScaledCacheId);
+    // Assert always unlock before unref.
+}
+
+bool SkCachingPixelRef::getInfo(SkImageInfo* info) {
+    SkASSERT(info != NULL);
+    if (fErrorInDecoding) {
+        return false;  // Don't try again.
+    }
+    if (fInfo.fWidth < 0) {
+        SkImageInfo tmp;
+        if (!this->onDecodeInfo(&tmp)) {
+            fErrorInDecoding = true;
+            return false;
+        }
+        SkASSERT(tmp.fWidth >= 0);
+        fInfo = tmp;
+    }
+    *info = fInfo;
+    return true;
+}
+
+bool SkCachingPixelRef::configure(SkBitmap* bitmap) {
+    SkASSERT(bitmap != NULL);
+    SkImageInfo info;
+    if (!this->getInfo(&info)) {
+        return false;
+    }
+    return bitmap->setConfig(info, 0);
+}
+
+void* SkCachingPixelRef::onLockPixels(SkColorTable** colorTable) {
+    (void)colorTable;
+    SkImageInfo info;
+    if (!this->getInfo(&info)) {
+        return NULL;
+    }
+    SkBitmap bitmap;
+
+    fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(),
+                                                     info.fWidth,
+                                                     info.fHeight,
+                                                     &bitmap);
+    if (NULL == fScaledCacheId) {
+        // Cache has been purged, must re-decode.
+        if (!this->onDecodeInto(0, &bitmap)) {
+            return NULL;
+        }
+        fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(),
+                                                        info.fWidth,
+                                                        info.fHeight,
+                                                        bitmap);
+        SkASSERT(fScaledCacheId != NULL);
+    }
+    // Now bitmap should contain a concrete PixelRef of the decoded
+    // image.
+    SkAutoLockPixels autoLockPixels(bitmap);
+    void* pixels = bitmap.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.
+    return pixels;
+}
+
+void SkCachingPixelRef::onUnlockPixels() {
+    if (fScaledCacheId != NULL) {
+        SkScaledImageCache::Unlock(
+            static_cast<SkScaledImageCache::ID*>(fScaledCacheId));
+        fScaledCacheId = NULL;
+    }
+}
+
+bool SkCachingPixelRef::onDecodeInto(int pow2, SkBitmap* bitmap) {
+    SkASSERT(bitmap != NULL);
+    SkBitmap tmp;
+    SkImageInfo info;
+    // TODO(halcanary) - Enable SkCachingPixelRef to use a custom
+    // allocator. `tmp.allocPixels(fAllocator, NULL)`
+    if (!(this->configure(&tmp) && tmp.allocPixels())) {
+        return false;
+    }
+    SkAssertResult(this->getInfo(&info));  // since configure() succeeded.
+    if (!this->onDecodePixels(info, tmp.getPixels(), tmp.rowBytes())) {
+        fErrorInDecoding = true;
+        return false;
+    }
+    *bitmap = tmp;
+    return true;
+}
+
diff --git a/src/lazy/SkCachingPixelRef.h b/src/lazy/SkCachingPixelRef.h
new file mode 100644
index 0000000..7caba45
--- /dev/null
+++ b/src/lazy/SkCachingPixelRef.h
@@ -0,0 +1,81 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkCachingPixelRef_DEFINED
+#define SkCachingPixelRef_DEFINED
+
+#include "SkImage.h"
+#include "SkPixelRef.h"
+
+class SkColorTable;
+
+/**
+ *  PixelRef which defers decoding until SkBitmap::lockPixels() is
+ *  called.  Caches the decoded images in the global
+ *  SkScaledImageCache.  When the pixels are unlocked, this cache may
+ *  or be destroyed before the next lock.  If so, onLockPixels will
+ *  attempt to re-decode.
+ *
+ *  Decoding is handled by the pure-virtual functions onDecodeInfo()
+ *  and onDecodePixels().  Subclasses of this class need only provide
+ *  those two functions.
+ */
+class SkCachingPixelRef : public SkPixelRef {
+public:
+    SkCachingPixelRef();
+    virtual ~SkCachingPixelRef();
+
+protected:
+    virtual void* onLockPixels(SkColorTable** colorTable) SK_OVERRIDE;
+    virtual void onUnlockPixels() SK_OVERRIDE;
+    virtual bool onLockPixelsAreWritable() const SK_OVERRIDE { return false; }
+    virtual bool onImplementsDecodeInto() SK_OVERRIDE  { return true; }
+    virtual bool onDecodeInto(int pow2, SkBitmap*) SK_OVERRIDE;
+
+    /**
+     *  Configure the supplied bitmap for this pixelRef, based on
+     *  information provided by onDecodeInfo().  Does not set the
+     *  bitmap's pixelRef. */
+    bool configure(SkBitmap* bitmap);
+
+    /**
+     *  Cache info from onDecodeInfo(). Returns false on failure.
+     */
+    bool getInfo(SkImageInfo* info);
+
+    /**
+     *  Return some information about the pixels, allowing this class
+     *  to allocate pixels.  @return false if anything goes wrong.
+     */
+    virtual bool onDecodeInfo(SkImageInfo* info) = 0;
+    /**
+     *  Decode into the given pixels, a block of memory of size
+     *  (info.fHeight - 1) * rowBytes + (info.fWidth * bytesPerPixel)
+     *
+     *  @param info Should be identical to the info returned by
+     *         onDecodeInfo so that the implementation can confirm
+     *         that the caller knows what it is asking for (config,
+     *         size).  Thiscontract also allows the caller to specify
+     *         different output-configs, which the implementation can
+     *         decide to support or not.
+     *
+     *  @return false if anything goes wrong.
+     */
+    virtual bool onDecodePixels(const SkImageInfo& info,
+                                void* pixels,
+                                size_t rowBytes) = 0;
+
+private:
+    bool                        fErrorInDecoding;
+    void*                       fScaledCacheId;
+    SkImageInfo                 fInfo;
+
+    typedef SkPixelRef INHERITED;
+};
+
+#endif  // SkCachingPixelRef_DEFINED
+
diff --git a/src/lazy/SkLazyCachingPixelRef.cpp b/src/lazy/SkLazyCachingPixelRef.cpp
new file mode 100644
index 0000000..59ac9c3
--- /dev/null
+++ b/src/lazy/SkLazyCachingPixelRef.cpp
@@ -0,0 +1,62 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "Sk64.h"
+#include "SkColorTable.h"
+#include "SkData.h"
+#include "SkImageDecoder.h"
+#include "SkImagePriv.h"
+#include "SkLazyCachingPixelRef.h"
+#include "SkPostConfig.h"
+
+SkLazyCachingPixelRef::SkLazyCachingPixelRef(SkData* data,
+                                             SkBitmapFactory::DecodeProc proc)
+    : fDecodeProc(proc) {
+    if (NULL == data) {
+        fData = SkData::NewEmpty();
+    } else {
+        fData = data;
+        fData->ref();
+    }
+    if (NULL == fDecodeProc) {  // use a reasonable default.
+        fDecodeProc = SkImageDecoder::DecodeMemoryToTarget;
+    }
+    this->setImmutable();
+}
+
+SkLazyCachingPixelRef::~SkLazyCachingPixelRef() {
+    SkASSERT(fData != NULL);
+    fData->unref();
+}
+
+bool SkLazyCachingPixelRef::onDecodeInfo(SkImageInfo* info) {
+    SkASSERT(info);
+    return fDecodeProc(fData->data(), fData->size(), info, NULL);
+}
+
+bool SkLazyCachingPixelRef::onDecodePixels(const SkImageInfo& passedInfo,
+                                           void* pixels, size_t rowBytes) {
+    SkASSERT(pixels);
+    SkImageInfo info;
+    if (!this->getInfo(&info)) {
+        return false;
+    }
+    if (passedInfo != info) {
+        return false;  // This implementation can not handle this case.
+    }
+    SkBitmapFactory::Target target = {pixels, rowBytes};
+    return fDecodeProc(fData->data(), fData->size(), &info, &target);
+}
+
+bool SkLazyCachingPixelRef::Install(SkBitmapFactory::DecodeProc proc,
+                                    SkData* data,
+                                    SkBitmap* destination) {
+    SkAutoTUnref<SkLazyCachingPixelRef> ref(
+        SkNEW_ARGS(SkLazyCachingPixelRef, (data, proc)));
+    return ref->configure(destination) && destination->setPixelRef(ref);
+}
+
diff --git a/src/lazy/SkLazyCachingPixelRef.h b/src/lazy/SkLazyCachingPixelRef.h
new file mode 100644
index 0000000..c13a2cd
--- /dev/null
+++ b/src/lazy/SkLazyCachingPixelRef.h
@@ -0,0 +1,99 @@
+/*
+ * Copyright 2013 Google Inc.
+ *
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef SkLazyCachingPixelRef_DEFINED
+#define SkLazyCachingPixelRef_DEFINED
+
+#include "SkBitmapFactory.h"
+#include "SkCachingPixelRef.h"
+
+class SkData;
+
+/**
+ *  PixelRef which defers decoding until SkBitmap::lockPixels() is
+ *  called.  Makes use of a supplied decode procedure.  Will decode at
+ *  the procedure's preferred size.
+ */
+class SkLazyCachingPixelRef : public SkCachingPixelRef {
+public:
+    /**
+     *  @param data Encoded data representing the pixels.  NULL is
+     *         equivalent to an empty data, and will be passed to
+     *         DecodeProc with length zero.
+     *
+     *  @param procedure Called to decode the pixels when
+     *         needed. If NULL, use SkImageDecoder::DecodeMemoryToTarget.
+     */
+    SkLazyCachingPixelRef(SkData* data,
+                          SkBitmapFactory::DecodeProc procedure);
+
+    virtual ~SkLazyCachingPixelRef();
+
+    virtual SkData* onRefEncodedData() SK_OVERRIDE { return SkSafeRef(fData); }
+
+    /**
+     *  A simplified version of SkBitmapFactory.  Installs a new
+     *  SkLazyCachingPixelRef into the provided bitmap.  Will
+     *  immediately call onDecodeInfo() to configure the bitmap, but
+     *  will defer decoding until the first time the bitmap's pixels
+     *  are locked.
+     *
+     *  @param data Encoded data representing the pixels.  NULL is
+     *         equivalent to an empty data, and will be passed to
+     *         DecodeProc with length zero.
+     *
+     *  @param procedure Called to decode the pixels when
+     *         needed. If NULL, use SkImageDecoder::DecodeMemoryToTarget.
+     *
+     *  @param destination Bitmap that will be modified on success.
+     *
+     *  @returns true on success.
+     */
+    static bool Install(SkBitmapFactory::DecodeProc procedure,
+                        SkData* data,
+                        SkBitmap* destination);
+
+    // No need to flatten this object. When flattening an SkBitmap,
+    // SkOrderedWriteBuffer will check the encoded data and write that
+    // instead.
+    // Future implementations of SkFlattenableWriteBuffer will need to
+    // special case for onRefEncodedData as well.
+    SK_DECLARE_UNFLATTENABLE_OBJECT()
+
+protected:
+    /**
+     *  Return some information about the pixels, allowing this class
+     *  to allocate pixels.  @return false if anything goes wrong.
+     *
+     *  This implementation calls SkBitmapFactory::DecodeProc with a
+     *  NULL target.
+     */
+    virtual bool onDecodeInfo(SkImageInfo* info) SK_OVERRIDE;
+    /**
+     *  Decode into the given pixels, a block of memory of size
+     *  (info.fHeight * rowBytes) bytes.
+     *
+     *  @param info Should be identical to the info returned by
+     *         onDecodeInfo so that the implementation can confirm
+     *         that the caller knows what its asking for (config,
+     *         size).
+     *
+     *  @return false if anything goes wrong.
+     */
+    virtual bool onDecodePixels(const SkImageInfo& info,
+                                void* pixels,
+                                size_t rowBytes) SK_OVERRIDE;
+
+private:
+    SkData*                     fData;
+    SkBitmapFactory::DecodeProc fDecodeProc;
+
+    typedef SkCachingPixelRef INHERITED;
+};
+
+#endif  // SkLazyCachingPixelRef_DEFINED
+
diff --git a/tests/CachedDecodingPixelRefTest.cpp b/tests/CachedDecodingPixelRefTest.cpp
index 08d492c..9a00546 100644
--- a/tests/CachedDecodingPixelRefTest.cpp
+++ b/tests/CachedDecodingPixelRefTest.cpp
@@ -11,10 +11,13 @@
 #include "SkForceLinking.h"
 #include "SkImageDecoder.h"
 #include "SkImagePriv.h"
+#include "SkLazyCachingPixelRef.h"
 #include "SkLazyPixelRef.h"
 #include "SkScaledImageCache.h"
 #include "SkStream.h"
+
 #include "Test.h"
+#include "TestClassDef.h"
 
 __SK_FORCE_IMAGE_DECODER_LINKING;
 
@@ -92,6 +95,7 @@
     if (!pixelPerfect) {
         return;
     }
+
     int pixelErrors = 0;
     for (int y = 0; y < b2.height(); ++y) {
         for (int x = 0; x < b2.width(); ++x) {
@@ -103,101 +107,140 @@
     REPORTER_ASSERT(reporter, 0 == pixelErrors);
 }
 
+
+typedef void(*CompareEncodedToOriginal)(skiatest::Reporter* reporter,
+                                        SkData* encoded,
+                                        const SkBitmap& original,
+                                        bool pixelPerfect);
 /**
- *  This checks to see that a SkLazyPixelRef works as advertized.
- */
-#include "TestClassDef.h"
-DEF_TEST(CachedDecodingPixelRefTest, reporter) {
+   this function tests three differently encoded images against the
+   original bitmap  */
+static void test_three_encodings(skiatest::Reporter* reporter,
+                                 CompareEncodedToOriginal comp) {
     SkBitmap original;
     make_test_image(&original);
-    const size_t bitmapSize = original.getSize();
-    const size_t oldByteLimit = SkScaledImageCache::GetByteLimit();
-    REPORTER_ASSERT(reporter, (!(original.empty())) && (!(original.isNull())));
-
+    REPORTER_ASSERT(reporter, !original.empty());
+    REPORTER_ASSERT(reporter, !original.isNull());
+    if (original.empty() || original.isNull()) {
+        return;
+    }
     static const SkImageEncoder::Type types[] = {
         SkImageEncoder::kPNG_Type,
         SkImageEncoder::kJPEG_Type,
         SkImageEncoder::kWEBP_Type
     };
-
     for (size_t i = 0; i < SK_ARRAY_COUNT(types); i++) {
         SkImageEncoder::Type type = types[i];
         SkAutoDataUnref encoded(create_data_from_bitmap(original, type));
         REPORTER_ASSERT(reporter, encoded.get() != NULL);
-        if (NULL == encoded.get()) {
-            continue;
+        if (NULL != encoded.get()) {
+            bool comparePixels = (SkImageEncoder::kPNG_Type == type);
+            comp(reporter, encoded, original, comparePixels);
         }
-        SkBitmap lazy;
-        static const SkBitmapFactory::DecodeProc decoder =
-            &(SkImageDecoder::DecodeMemoryToTarget);
-        bool success = simple_bitmap_factory(decoder, encoded.get(), &lazy);
-
-        REPORTER_ASSERT(reporter, success);
-
-        size_t bytesUsed = SkScaledImageCache::GetBytesUsed();
-
-        if (oldByteLimit < bitmapSize) {
-            SkScaledImageCache::SetByteLimit(bitmapSize + oldByteLimit);
-        }
-        void* lazyPixels = NULL;
-
-        // Since this is lazy, it shouldn't have fPixels yet!
-        REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
-        {
-            SkAutoLockPixels autoLockPixels(lazy);  // now pixels are good.
-            lazyPixels = lazy.getPixels();
-            REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
-            // first time we lock pixels, we should get bump in the size
-            // of the cache by exactly bitmapSize.
-            REPORTER_ASSERT(reporter, bytesUsed + bitmapSize
-                            == SkScaledImageCache::GetBytesUsed());
-            bytesUsed = SkScaledImageCache::GetBytesUsed();
-        }
-        // pixels should be gone!
-        REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
-        {
-            SkAutoLockPixels autoLockPixels(lazy);  // now pixels are good.
-            REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
-
-            // verify that the same pixels are used this time.
-            REPORTER_ASSERT(reporter, lazy.getPixels() == lazyPixels);
-        }
-
-        bool comparePixels = (SkImageEncoder::kPNG_Type == type);
-        // Only PNG is pixel-perfect.
-        compare_bitmaps(reporter, original, lazy, comparePixels);
-
-        // force the cache to clear by making it too small.
-        SkScaledImageCache::SetByteLimit(bitmapSize / 2);
-        compare_bitmaps(reporter, original, lazy, comparePixels);
-
-        // I'm pretty sure that the logic of the cache should mean
-        // that it will clear to zero, regardless of where it started.
-        REPORTER_ASSERT(reporter, SkScaledImageCache::GetBytesUsed() == 0);
-        // TODO(someone) - write a custom allocator that can verify
-        // that the memory where those pixels were cached really did
-        // get freed.
-
-        ////////////////////////////////////////////////////////////////////////
-        //  The following commented-out code happens to work on my
-        //  machine, and indicates to me that the SkLazyPixelRef is
-        //  behaving as designed.  But I don't know an easy way to
-        //  guarantee that a second allocation of the same size will
-        //  give a different address.
-        ////////////////////////////////////////////////////////////////////////
-        // {
-        //     // confuse the heap allocation system
-        //     SkAutoMalloc autoMalloc(bitmapSize);
-        //     REPORTER_ASSERT(reporter, autoMalloc.get() == lazyPixels);
-        //     {
-        //         SkAutoLockPixels autoLockPixels(lazy);
-        //         // verify that *different* pixels are used this time.
-        //         REPORTER_ASSERT(reporter, lazy.getPixels() != lazyPixels);
-        //         compare_bitmaps(reporter, original, lazy, comparePixels);
-        //     }
-        // }
-
-        // restore cache size
-        SkScaledImageCache::SetByteLimit(oldByteLimit);
     }
 }
+
+/**
+ *  This checks to see that a SkLazyPixelRef works as advertised.
+ */
+static void compare_with_skLazyPixelRef(skiatest::Reporter* reporter,
+                                        SkData* encoded,
+                                        const SkBitmap& original,
+                                        bool comparePixels) {
+    SkBitmap lazy;
+    static const SkBitmapFactory::DecodeProc decoder =
+        &(SkImageDecoder::DecodeMemoryToTarget);
+    bool success = simple_bitmap_factory(decoder, encoded, &lazy);
+    REPORTER_ASSERT(reporter, success);
+
+    REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
+    {
+        SkAutoLockPixels autoLockPixels(lazy);  // now pixels are good.
+        REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
+    }
+    // pixels should be gone!
+    REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
+    {
+        SkAutoLockPixels autoLockPixels(lazy);  // now pixels are good.
+        REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
+    }
+    compare_bitmaps(reporter, original, lazy, comparePixels);
+}
+DEF_TEST(LazyPixelRef, reporter) {
+    test_three_encodings(reporter, compare_with_skLazyPixelRef);
+}
+
+
+
+/**
+ *  This checks to see that a SkLazyCachedPixelRef works as advertised.
+ */
+
+static void compare_with_skLazyCachedPixelRef(skiatest::Reporter* reporter,
+                                              SkData* encoded,
+                                              const SkBitmap& original,
+                                              bool comparePixels) {
+    SkBitmap lazy;
+    static const SkBitmapFactory::DecodeProc decoder =
+        &(SkImageDecoder::DecodeMemoryToTarget);
+    bool success = SkLazyCachingPixelRef::Install(decoder, encoded, &lazy);
+    REPORTER_ASSERT(reporter, success);
+
+    REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
+    {
+        SkAutoLockPixels autoLockPixels(lazy);  // now pixels are good.
+        REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
+    }
+    // pixels should be gone!
+    REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
+    {
+        SkAutoLockPixels autoLockPixels(lazy);  // now pixels are good.
+        REPORTER_ASSERT(reporter, NULL != lazy.getPixels());
+    }
+    compare_bitmaps(reporter, original, lazy, comparePixels);
+}
+DEF_TEST(LazyCachedPixelRef, reporter) {
+    test_three_encodings(reporter, compare_with_skLazyCachedPixelRef);
+}
+
+class TestPixelRef : public SkCachingPixelRef {
+public:
+    TestPixelRef(int x) : fX(x) { }
+    virtual ~TestPixelRef() { }
+    static bool Install(SkBitmap* destination, int x) {
+        SkAutoTUnref<TestPixelRef> ref(SkNEW_ARGS(TestPixelRef, (x)));
+        return ref->configure(destination) && destination->setPixelRef(ref);
+    }
+    SK_DECLARE_UNFLATTENABLE_OBJECT()
+protected:
+    virtual bool onDecodeInfo(SkImageInfo* info) SK_OVERRIDE {
+        if (fX == 0) {
+            return false;
+        }
+        SkASSERT(info);
+        info->fWidth = 10;
+        info->fHeight = 10;
+        info->fColorType = kRGBA_8888_SkColorType;
+        info->fAlphaType = kOpaque_SkAlphaType;
+        return true;
+    }
+    virtual bool onDecodePixels(const SkImageInfo& info,
+                                void* pixels,
+                                size_t rowBytes) SK_OVERRIDE {
+        return false;
+    }
+private:
+    int fX;  // controls where the failure happens
+    typedef SkCachingPixelRef INHERITED;
+};
+
+DEF_TEST(CachingPixelRef, reporter) {
+    SkBitmap lazy;
+    // test the error handling
+    REPORTER_ASSERT(reporter, !TestPixelRef::Install(&lazy, 0));
+    // onDecodeInfo should succeed, allowing installation
+    REPORTER_ASSERT(reporter, TestPixelRef::Install(&lazy, 1));
+    SkAutoLockPixels autoLockPixels(lazy);  // now pixels are good.
+    // onDecodePixels should fail, so getting pixels will fail.
+    REPORTER_ASSERT(reporter, NULL == lazy.getPixels());
+}