Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 #include "LazyDecodeBitmap.h" | |
|
scroggo
2013/07/15 18:02:41
Copyright header.
| |
| 2 | |
| 3 #include "PictureRenderingFlags.h" // --deferImageDecoding is defined here. | |
| 4 #include "SkData.h" | |
| 5 #include "SkImageDecoder.h" | |
| 6 #include "SkForceLinking.h" | |
| 7 #include "SkLruImageCache.h" | |
| 8 #include "SkPurgeableImageCache.h" | |
| 9 #include "SkCommandLineFlags.h" | |
| 10 | |
| 11 __SK_FORCE_IMAGE_DECODER_LINKING; | |
| 12 | |
| 13 DEFINE_bool(useVolatileCache, false, "Use a volatile cache for deferred image de coding pixels. " | |
| 14 "Only meaningful if --deferImageDecoding is set to true and the plat form has an " | |
| 15 "implementation."); | |
| 16 | |
| 17 SkLruImageCache gLruImageCache(1024*1024); | |
| 18 | |
| 19 // Simple cache selector to choose between a purgeable cache for large images an d the standard one | |
| 20 // for smaller images. | |
| 21 // | |
| 22 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.
| |
| 23 | |
| 24 public: | |
| 25 ElasticCacheSelector() { | |
| 26 fPurgeableImageCache = SkPurgeableImageCache::Create(); | |
| 27 } | |
| 28 | |
| 29 ~ElasticCacheSelector() { | |
| 30 SkSafeUnref(fPurgeableImageCache); | |
| 31 } | |
| 32 | |
| 33 virtual SkImageCache* selectCache(const SkImage::Info& info) SK_OVERRIDE { | |
| 34 if (info.fWidth * info.fHeight > 32 * 1024 && fPurgeableImageCache != NU LL) { | |
| 35 return fPurgeableImageCache; | |
| 36 } | |
| 37 return &gLruImageCache; | |
| 38 } | |
| 39 private: | |
| 40 SkImageCache* fPurgeableImageCache; | |
| 41 }; | |
| 42 | |
| 43 static ElasticCacheSelector gCacheSelector; | |
| 44 static SkBitmapFactory gFactory(&SkImageDecoder::DecodeMemoryToTarget); | |
| 45 | |
| 46 bool LazyDecodeBitmap(const void* buffer, size_t size, SkBitmap* bitmap) { | |
| 47 void* copiedBuffer = sk_malloc_throw(size); | |
| 48 memcpy(copiedBuffer, buffer, size); | |
| 49 SkAutoDataUnref data(SkData::NewFromMalloc(copiedBuffer, size)); | |
| 50 | |
| 51 static bool gOnce; | |
| 52 if (!gOnce) { | |
| 53 // Only use the cache selector if there is a purgeable image cache to us e for large | |
| 54 // images. | |
| 55 if (FLAGS_useVolatileCache && SkAutoTUnref<SkImageCache>( | |
| 56 SkPurgeableImageCache::Create()).get() != NULL) { | |
| 57 gFactory.setCacheSelector(&gCacheSelector); | |
| 58 } else { | |
| 59 gFactory.setImageCache(&gLruImageCache); | |
| 60 } | |
| 61 gOnce = true; | |
| 62 } | |
| 63 return gFactory.installPixelRef(data, bitmap); | |
| 64 } | |
| OLD | NEW |