Create SkLazyPixelRef which performs lazy decoding.

The new pixel ref behaves similarly to SkImageRef, with some key differences:
It does not depend on the images project.
It requires an SkImageCache, which handles allocation and caching of the pixel
memory.
It takes a function signature for decoding which decodes into already allocated
pixel memory rather than into an SkBitmap.

Add two implementations of SkImageCache: SkLruImageCache and SkAshmemImageCache.

Replace SkSerializationHelpers::DecodeBitmap with SkPicture::InstallPixelRefProc,
and update sites that referenced it.

SkBitmapFactory now sets the pixel ref to a new object of the new
class SkLazyPixelRef, provided it has an SkImageCache for caching.

Provide an option to do lazy decodes in render_pictures and bench_pictures.

SkPicture:
Eliminate the default parameters in the constructor.
If a proc for decoding bitmaps is installed, use it to decode any encoded
data in subpictures.
When parsing deserializing subpictures, check for success.
When serializing subpictures, pass the picture's bitmap encoder to the
subpicture's call to serialize.

Update BitmapFactoryTest to test its new behavior.

BUG=https://code.google.com/p/skia/issues/detail?id=1008
BUG=https://code.google.com/p/skia/issues/detail?id=1009

Review URL: https://codereview.appspot.com/7060052

git-svn-id: http://skia.googlecode.com/svn/trunk@7835 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/src/lazy/SkLazyPixelRef.cpp b/src/lazy/SkLazyPixelRef.cpp
new file mode 100644
index 0000000..c0d5d90
--- /dev/null
+++ b/src/lazy/SkLazyPixelRef.cpp
@@ -0,0 +1,107 @@
+/*
+ * Copyright 2012 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 "SkLazyPixelRef.h"
+#include "SkColorTable.h"
+#include "SkData.h"
+#include "SkImageCache.h"
+#include "SkImagePriv.h"
+
+SkLazyPixelRef::SkLazyPixelRef(SkData* data, SkBitmapFactory::DecodeProc proc, SkImageCache* cache)
+    // Pass NULL for the Mutex so that the default (ring buffer) will be used.
+    : INHERITED(NULL)
+    , fDecodeProc(proc)
+    , fImageCache(cache)
+    , fCacheId(SkImageCache::UNINITIALIZED_ID) {
+    SkASSERT(fDecodeProc != NULL);
+    if (NULL == data) {
+        fData = SkData::NewEmpty();
+        fErrorInDecoding = true;
+    } else {
+        fData = data;
+        fData->ref();
+        fErrorInDecoding = data->size() == 0;
+    }
+    SkASSERT(cache != NULL);
+    cache->ref();
+    // Since this pixel ref bases its data on encoded data, it should never change.
+    this->setImmutable();
+}
+
+SkLazyPixelRef::~SkLazyPixelRef() {
+    SkASSERT(fData != NULL);
+    fData->unref();
+    SkASSERT(fImageCache);
+    if (fCacheId != SkImageCache::UNINITIALIZED_ID) {
+        fImageCache->throwAwayCache(fCacheId);
+    }
+    fImageCache->unref();
+}
+
+static size_t ComputeMinRowBytesAndSize(const SkImage::Info& info, size_t* rowBytes) {
+    *rowBytes = SkImageMinRowBytes(info);
+
+    Sk64 safeSize;
+    safeSize.setZero();
+    if (info.fHeight > 0) {
+        safeSize.setMul(info.fHeight, *rowBytes);
+    }
+    SkASSERT(!safeSize.isNeg());
+    return safeSize.is32() ? safeSize.get32() : 0;
+}
+
+void* SkLazyPixelRef::onLockPixels(SkColorTable**) {
+    if (fErrorInDecoding) {
+        return NULL;
+    }
+    SkBitmapFactory::Target target;
+    // Check to see if the pixels still exist in the cache.
+    target.fAddr = SkImageCache::UNINITIALIZED_ID == fCacheId ?
+                   NULL : fImageCache->pinCache(fCacheId);
+    if (NULL == target.fAddr) {
+        SkImage::Info info;
+        SkASSERT(fData != NULL && fData->size() > 0);
+        // FIXME: As an optimization, only do this part once.
+        fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, NULL);
+        if (fErrorInDecoding) {
+            fCacheId = SkImageCache::UNINITIALIZED_ID;
+            return NULL;
+        }
+        // Allocate the memory.
+        size_t bytes = ComputeMinRowBytesAndSize(info, &target.fRowBytes);
+
+        target.fAddr = fImageCache->allocAndPinCache(bytes, &fCacheId);
+        if (NULL == target.fAddr) {
+            // Space could not be allocated.
+            fCacheId = SkImageCache::UNINITIALIZED_ID;
+            return NULL;
+        }
+        SkASSERT(SkImageCache::UNINITIALIZED_ID != fCacheId);
+        fErrorInDecoding = !fDecodeProc(fData->data(), fData->size(), &info, &target);
+        if (fErrorInDecoding) {
+            fImageCache->throwAwayCache(fCacheId);
+            fCacheId = SkImageCache::UNINITIALIZED_ID;
+            return NULL;
+        }
+    }
+    return target.fAddr;
+}
+
+void SkLazyPixelRef::onUnlockPixels() {
+    if (fErrorInDecoding) {
+        return;
+    }
+    if (fCacheId != SkImageCache::UNINITIALIZED_ID) {
+        fImageCache->releaseCache(fCacheId);
+    }
+}
+
+SkData* SkLazyPixelRef::onRefEncodedData() {
+    fData->ref();
+    return fData;
+}