Index: tools/LazyDecodeBitmap.cpp |
diff --git a/tools/LazyDecodeBitmap.cpp b/tools/LazyDecodeBitmap.cpp |
new file mode 100644 |
index 0000000000000000000000000000000000000000..f7c5a6cbc1c9709c8fff6c1fba312b60f1ac82f9 |
--- /dev/null |
+++ b/tools/LazyDecodeBitmap.cpp |
@@ -0,0 +1,64 @@ |
+#include "LazyDecodeBitmap.h" |
scroggo
2013/07/15 18:02:41
Copyright header.
|
+ |
+#include "PictureRenderingFlags.h" // --deferImageDecoding is defined here. |
+#include "SkData.h" |
+#include "SkImageDecoder.h" |
+#include "SkForceLinking.h" |
+#include "SkLruImageCache.h" |
+#include "SkPurgeableImageCache.h" |
+#include "SkCommandLineFlags.h" |
+ |
+__SK_FORCE_IMAGE_DECODER_LINKING; |
+ |
+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."); |
+ |
+SkLruImageCache gLruImageCache(1024*1024); |
+ |
+// Simple cache selector to choose between a purgeable cache for large images and the standard one |
+// for smaller images. |
+// |
+class ElasticCacheSelector : public SkBitmapFactory::CacheSelector { |
scroggo
2013/07/15 18:02:41
Naming nit: I don't see what makes this CacheSelec
sglez
2013/07/16 15:59:33
How about just CacheSelector?
scroggo
2013/07/16 16:06:18
Fine with me.
|
+ |
+public: |
+ ElasticCacheSelector() { |
+ fPurgeableImageCache = SkPurgeableImageCache::Create(); |
+ } |
+ |
+ ~ElasticCacheSelector() { |
+ 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 ElasticCacheSelector gCacheSelector; |
+static SkBitmapFactory gFactory(&SkImageDecoder::DecodeMemoryToTarget); |
+ |
+bool LazyDecodeBitmap(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); |
+} |