Improvements/additions to SkImageCache/SkLazyPixelRef.

SkPurgeableImageCache:
New image cache that uses virtual memory to store the pixels. Combines
features of SkAshmemImageCache (which has been removed) with SkPurgeableMemoryBlock, which has android and Mac versions.

SkImageCache:
Modified the API. pinCache now returns a status out parameter which
states whether the pinned memory retained the old data. This allows
allocAndPinCache to only be used for allocations.
Add a new debug only interface to purge unpinned data.
Updates to documentation, clarifying behavior.
Changed CachedStatus to MemoryStatus

SkLruImageCache:
Implement the new function purgeAllUnpinnedCaches and change implementation
of pinCache for the new behavior.

SkLazyPixelRef:
Rewrite onLockPixels to account for the new behavior of pinCache.

BitmapFactoryTest:
Test the new SkPurgeableImageCache.
Write tests which directly test the SkImageCaches.
Create a larger bitmap, since some of the SkImageCaches are designed
to handle large bitmaps.

bench_ and render_pictures:
Consolidate lazy_decode_bitmap into one function.
Allow using a flag to specify using the purgeable image cache.
Clean up some #includes.

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

git-svn-id: http://skia.googlecode.com/svn/trunk@8207 2bbb7eff-a529-9590-31e7-b0007b416f81
diff --git a/tools/PictureRenderingFlags.cpp b/tools/PictureRenderingFlags.cpp
index 3cb1e96..29046ae 100644
--- a/tools/PictureRenderingFlags.cpp
+++ b/tools/PictureRenderingFlags.cpp
@@ -11,7 +11,14 @@
 #include "PictureRenderer.h"
 #include "picture_utils.h"
 
+#include "SkBitmapFactory.h"
+#include "SkData.h"
 #include "SkFlags.h"
+#include "SkImage.h"
+#include "SkImageDecoder.h"
+#include "SkLruImageCache.h"
+#include "SkPurgeableImageCache.h"
+#include "SkString.h"
 
 // Alphabetized list of flags used by this file or bench_ and render_pictures.
 DEFINE_string(bbh, "none", "bbhType [width height]: Set the bounding box hierarchy type to "
@@ -57,6 +64,9 @@
 DEFINE_double(scale, 1, "Set the scale factor.");
 DEFINE_string(tiles, "", "Used with --mode copyTile to specify number of tiles per larger tile "
               "in the x and y directions.");
+DEFINE_bool(useVolatileCache, false, "Use a volatile cache for deferred image decoding pixels. "
+            "Only meaningful if --deferImageDecoding is set to true and the platform has an "
+            "implementation.");
 DEFINE_string(viewport, "", "width height: Set the viewport.");
 
 sk_tools::PictureRenderer* parseRenderer(SkString& error, PictureTool tool) {
@@ -308,3 +318,52 @@
 
     return renderer.detach();
 }
+
+SkLruImageCache gLruImageCache(1024*1024);
+
+// Simple cache selector to choose between a purgeable cache for large images and the standard one
+// for smaller images.
+class MyCacheSelector : public SkBitmapFactory::CacheSelector {
+
+public:
+    MyCacheSelector() {
+        fPurgeableImageCache = SkPurgeableImageCache::Create();
+    }
+
+    ~MyCacheSelector() {
+        SkSafeUnref(fPurgeableImageCache);
+    }
+
+    virtual SkImageCache* selectCache(const SkImage::Info& info) SK_OVERRIDE {
+        if (info.fWidth * info.fHeight > 32 * 1024 && fPurgeableImageCache != NULL) {
+            return fPurgeableImageCache;
+        }
+        return &gLruImageCache;
+    }
+private:
+    SkImageCache* fPurgeableImageCache;
+};
+
+static MyCacheSelector gCacheSelector;
+static SkBitmapFactory gFactory(&SkImageDecoder::DecodeMemoryToTarget);
+
+bool lazy_decode_bitmap(const void* buffer, size_t size, SkBitmap* bitmap);
+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));
+
+    static bool gOnce;
+    if (!gOnce) {
+        // Only use the cache selector if there is a purgeable image cache to use for large
+        // images.
+        if (FLAGS_useVolatileCache && SkAutoTUnref<SkImageCache>(
+                SkPurgeableImageCache::Create()).get() != NULL) {
+            gFactory.setCacheSelector(&gCacheSelector);
+        } else {
+            gFactory.setImageCache(&gLruImageCache);
+        }
+        gOnce = true;
+    }
+    return gFactory.installPixelRef(data, bitmap);
+}