SkCachingPixelRef to use SkImageGenerator

-   Remove SkLazyCachingPixelRef class.

-   Refactor unit tests.

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

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

git-svn-id: http://skia.googlecode.com/svn/trunk/src@12505 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/lazy/SkCachingPixelRef.cpp b/lazy/SkCachingPixelRef.cpp
index d12b7cf..6c9da8f 100644
--- a/lazy/SkCachingPixelRef.cpp
+++ b/lazy/SkCachingPixelRef.cpp
@@ -8,66 +8,71 @@
 #include "SkCachingPixelRef.h"
 #include "SkScaledImageCache.h"
 
-SkCachingPixelRef::SkCachingPixelRef()
-    : fErrorInDecoding(false)
-    , fScaledCacheId(NULL) {
-    memset(&fInfo, 0xFF, sizeof(fInfo));
+
+bool SkCachingPixelRef::Install(SkImageGenerator* generator,
+                                SkBitmap* dst) {
+    SkImageInfo info;
+    SkASSERT(generator != NULL);
+    SkASSERT(dst != NULL);
+    if ((NULL == generator)
+        || !(generator->getInfo(&info))
+        || !dst->setConfig(info, 0)) {
+        SkDELETE(generator);
+        return false;
+    }
+    SkAutoTUnref<SkCachingPixelRef> ref(SkNEW_ARGS(SkCachingPixelRef,
+                                                   (generator,
+                                                    info,
+                                                    dst->rowBytes())));
+    dst->setPixelRef(ref);
+    return true;
+}
+
+SkCachingPixelRef::SkCachingPixelRef(SkImageGenerator* generator,
+                                     const SkImageInfo& info,
+                                     size_t rowBytes)
+    : fImageGenerator(generator)
+    , fErrorInDecoding(false)
+    , fScaledCacheId(NULL)
+    , fInfo(info)
+    , fRowBytes(rowBytes) {
+    SkASSERT(fImageGenerator != NULL);
 }
 SkCachingPixelRef::~SkCachingPixelRef() {
+    SkDELETE(fImageGenerator);
     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;
+    if (fErrorInDecoding) {
+        return NULL;  // don't try again.
     }
     SkBitmap bitmap;
-
+    SkASSERT(NULL == fScaledCacheId);
     fScaledCacheId = SkScaledImageCache::FindAndLock(this->getGenerationID(),
-                                                     info.fWidth,
-                                                     info.fHeight,
+                                                     fInfo.fWidth,
+                                                     fInfo.fHeight,
                                                      &bitmap);
     if (NULL == fScaledCacheId) {
         // Cache has been purged, must re-decode.
-        if (!this->onDecodeInto(0, &bitmap)) {
+        if ((!bitmap.setConfig(fInfo, fRowBytes)) || !bitmap.allocPixels()) {
+            fErrorInDecoding = true;
+            return NULL;
+        }
+        SkAutoLockPixels autoLockPixels(bitmap);
+        if (!fImageGenerator->getPixels(fInfo, bitmap.getPixels(), fRowBytes)) {
+            fErrorInDecoding = true;
             return NULL;
         }
         fScaledCacheId = SkScaledImageCache::AddAndLock(this->getGenerationID(),
-                                                        info.fWidth,
-                                                        info.fHeight,
+                                                        fInfo.fWidth,
+                                                        fInfo.fHeight,
                                                         bitmap);
         SkASSERT(fScaledCacheId != NULL);
     }
+
     // Now bitmap should contain a concrete PixelRef of the decoded
     // image.
     SkAutoLockPixels autoLockPixels(bitmap);
@@ -92,20 +97,3 @@
     }
 }
 
-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/lazy/SkCachingPixelRef.h b/lazy/SkCachingPixelRef.h
index db968df..81092dc 100644
--- a/lazy/SkCachingPixelRef.h
+++ b/lazy/SkCachingPixelRef.h
@@ -8,7 +8,8 @@
 #ifndef SkCachingPixelRef_DEFINED
 #define SkCachingPixelRef_DEFINED
 
-#include "SkImage.h"
+#include "SkImageInfo.h"
+#include "SkImageGenerator.h"
 #include "SkPixelRef.h"
 
 class SkColorTable;
@@ -20,61 +21,51 @@
  *  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.
+ *  Decoding is handled by the SkImageGenerator
  */
 class SkCachingPixelRef : public SkPixelRef {
 public:
-    SkCachingPixelRef();
-    virtual ~SkCachingPixelRef();
+    /**
+     *  Takes ownership of SkImageGenerator.  If this method fails for
+     *  whatever reason, it will return false and immediatetely delete
+     *  the generator.  If it succeeds, it will modify destination
+     *  bitmap.
+     *
+     *  If Install fails or when the SkCachingPixelRef that is
+     *  installed into destination is destroyed, it will call
+     *  SkDELETE() on the generator.  Therefore, generator should be
+     *  allocated with SkNEW() or SkNEW_ARGS().
+     */
+    static bool Install(SkImageGenerator* gen, SkBitmap* dst);
 
 protected:
+    virtual ~SkCachingPixelRef();
     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;
+    virtual SkData* onRefEncodedData() SK_OVERRIDE {
+        return fImageGenerator->refEncodedData();
+    }
+    // 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()
 
 private:
-    bool                        fErrorInDecoding;
-    void*                       fScaledCacheId;
-    SkImageInfo                 fInfo;
+    SkImageGenerator* const fImageGenerator;
+    bool                    fErrorInDecoding;
+    void*                   fScaledCacheId;
+    const SkImageInfo       fInfo;
+    const size_t            fRowBytes;
 
+    SkCachingPixelRef(SkImageGenerator* imageGenerator,
+                      const SkImageInfo& info,
+                      size_t rowBytes);
     typedef SkPixelRef INHERITED;
 };
 
 #endif  // SkCachingPixelRef_DEFINED
+
diff --git a/lazy/SkLazyCachingPixelRef.cpp b/lazy/SkLazyCachingPixelRef.cpp
deleted file mode 100644
index 730b5f7..0000000
--- a/lazy/SkLazyCachingPixelRef.cpp
+++ /dev/null
@@ -1,61 +0,0 @@
-/*
- * 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/lazy/SkLazyCachingPixelRef.h b/lazy/SkLazyCachingPixelRef.h
deleted file mode 100644
index a9d2dad..0000000
--- a/lazy/SkLazyCachingPixelRef.h
+++ /dev/null
@@ -1,98 +0,0 @@
-/*
- * 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