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/tools/bench_pictures_main.cpp b/tools/bench_pictures_main.cpp
index 579830d..60d79fc 100644
--- a/tools/bench_pictures_main.cpp
+++ b/tools/bench_pictures_main.cpp
@@ -9,6 +9,7 @@
 #include "CopyTilesRenderer.h"
 #include "PictureBenchmark.h"
 #include "SkBenchLogger.h"
+#include "SkBitmapFactory.h"
 #include "SkCanvas.h"
 #include "SkGraphics.h"
 #include "SkImageDecoder.h"
@@ -126,6 +127,7 @@
 "     [--pipe]\n"
 "     [--bbh bbhType]\n"
 "     [--multi numThreads]\n"
+"     [--enable-deferred-image-decoding]\n"
 "     [--viewport width height][--scale sf]\n"
 "     [--device bitmap"
 #if SK_SUPPORT_GPU
@@ -186,6 +188,8 @@
     SkDebugf(
 "     --multi numThreads : Set the number of threads for multi threaded drawing. Must be greater\n"
 "                          than 1. Only works with tiled rendering.\n"
+"     --enable-deferred-image-decoding : Defer decoding until drawing images. Has no effect if\n"
+"                      the provided skp does not have its images encoded.\n"
 "     --viewport width height : Set the viewport.\n"
 "     --scale sf : Scale drawing by sf.\n"
 "     --pipe: Benchmark SkGPipe rendering. Currently incompatible with \"mode\".\n");
@@ -227,6 +231,22 @@
 
 SkBenchLogger gLogger;
 
+bool lazy_decode = false;
+
+#include "SkData.h"
+#include "SkLruImageCache.h"
+
+static SkLruImageCache gLruImageCache(1024*1024);
+
+static bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap* bitmap) {
+    void* copiedBuffer = sk_malloc_throw(size);
+    memcpy(copiedBuffer, buffer, size);
+    SkAutoDataUnref data(SkData::NewFromMalloc(copiedBuffer, size));
+    SkBitmapFactory factory(&SkImageDecoder::DecodeMemoryToTarget);
+    factory.setImageCache(&gLruImageCache);
+    return factory.installPixelRef(data, bitmap);
+}
+
 static bool run_single_benchmark(const SkString& inputPath,
                                  sk_tools::PictureBenchmark& benchmark) {
     SkFILEStream inputStream;
@@ -240,7 +260,14 @@
     }
 
     bool success = false;
-    SkPicture picture(&inputStream, &success, &SkImageDecoder::DecodeStream);
+    SkPicture* picture;
+    if (lazy_decode) {
+        picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &lazy_decode_bitmap));
+    } else {
+        picture = SkNEW_ARGS(SkPicture, (&inputStream, &success, &SkImageDecoder::DecodeMemory));
+    }
+    SkAutoTDelete<SkPicture> ad(picture);
+
     if (!success) {
         SkString err;
         err.printf("Could not read an SkPicture from %s\n", inputPath.c_str());
@@ -252,11 +279,11 @@
     sk_tools::get_basename(&filename, inputPath);
 
     SkString result;
-    result.printf("running bench [%i %i] %s ", picture.width(),
-                  picture.height(), filename.c_str());
+    result.printf("running bench [%i %i] %s ", picture->width(), picture->height(),
+                  filename.c_str());
     gLogger.logProgress(result);
 
-    benchmark.run(&picture);
+    benchmark.run(picture);
     return true;
 }
 
@@ -538,6 +565,8 @@
             benchmark->setTimeIndividualTiles(true);
         } else if (0 == strcmp(*argv, "--min")) {
             benchmark->setPrintMin(true);
+        } else if (0 == strcmp(*argv, "--enable-deferred-image-decoding")) {
+            lazy_decode = true;
         } else if (0 == strcmp(*argv, "--logPerIter")) {
             ++argv;
             if (argv < stop) {