| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2013 Google Inc. | 2 * Copyright 2013 Google Inc. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be | 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. | 5 * found in the LICENSE file. |
| 6 */ | 6 */ |
| 7 | 7 |
| 8 #include "LazyDecodeBitmap.h" | 8 #include "LazyDecodeBitmap.h" |
| 9 | 9 |
| 10 #include "PictureRenderingFlags.h" // --deferImageDecoding is defined here. | |
| 11 #include "SkBitmap.h" | |
| 12 #include "SkData.h" | 10 #include "SkData.h" |
| 13 #include "SkImageDecoder.h" | 11 #include "SkDecodingImageGenerator.h" |
| 12 #include "SkDiscardableMemoryPool.h" |
| 13 #include "SkDiscardablePixelRef.h" |
| 14 #include "SkForceLinking.h" | 14 #include "SkForceLinking.h" |
| 15 #include "SkLruImageCache.h" | 15 |
| 16 #include "SkPurgeableImageCache.h" | |
| 17 #include "SkCommandLineFlags.h" | 16 #include "SkCommandLineFlags.h" |
| 18 | 17 |
| 19 __SK_FORCE_IMAGE_DECODER_LINKING; | 18 __SK_FORCE_IMAGE_DECODER_LINKING; |
| 20 | 19 |
| 20 // TODO(halcanary) Use this flag when ashmem-backed discardable memory lands. |
| 21 DEFINE_bool(useVolatileCache, false, "Use a volatile cache for deferred image de
coding pixels. " | 21 DEFINE_bool(useVolatileCache, false, "Use a volatile cache for deferred image de
coding pixels. " |
| 22 "Only meaningful if --deferImageDecoding is set to true and the plat
form has an " | 22 "Only meaningful if --deferImageDecoding is set to true and the plat
form has an " |
| 23 "implementation."); | 23 "implementation."); |
| 24 | 24 |
| 25 SkLruImageCache gLruImageCache(1024 * 1024); | 25 // Fits SkPicture::InstallPixelRefProc call signature. |
| 26 | 26 // Used in SkPicturePlayback::CreateFromStream |
| 27 namespace sk_tools { | 27 bool sk_tools::LazyDecodeBitmap(const void* src, |
| 28 | 28 size_t length, |
| 29 // Simple cache selector to choose between a purgeable cache for large images an
d the standard one | 29 SkBitmap* dst) { |
| 30 // for smaller images. | 30 SkAutoDataUnref data(SkData::NewWithCopy(src, length)); |
| 31 // | 31 if (NULL == data.get()) { |
| 32 class CacheSelector : public SkBitmapFactory::CacheSelector { | 32 return false; |
| 33 | |
| 34 public: | |
| 35 CacheSelector() { | |
| 36 fPurgeableImageCache = SkPurgeableImageCache::Create(); | |
| 37 } | 33 } |
| 38 | 34 |
| 39 ~CacheSelector() { | 35 SkAutoTDelete<SkImageGenerator> gen(SkNEW_ARGS(SkDecodingImageGenerator, |
| 40 SkSafeUnref(fPurgeableImageCache); | 36 (data))); |
| 37 SkImageInfo info; |
| 38 if (!gen->getInfo(&info)) { |
| 39 return false; |
| 41 } | 40 } |
| 42 | 41 SkDiscardableMemory::Factory* pool = NULL; |
| 43 virtual SkImageCache* selectCache(const SkImageInfo& info) SK_OVERRIDE { | 42 if (info.fWidth * info.fHeight > 32 * 1024) { |
| 44 if (info.fWidth * info.fHeight > 32 * 1024 && fPurgeableImageCache != NU
LL) { | 43 // how to do switching with SkDiscardableMemory. |
| 45 return fPurgeableImageCache; | 44 pool = SkGetGlobalDiscardableMemoryPool(); |
| 46 } | |
| 47 return &gLruImageCache; | |
| 48 } | 45 } |
| 49 private: | 46 return SkDiscardablePixelRef::Install(gen.detach(), dst, pool); |
| 50 SkImageCache* fPurgeableImageCache; | |
| 51 }; | |
| 52 | |
| 53 static CacheSelector gCacheSelector; | |
| 54 static SkBitmapFactory gFactory(&SkImageDecoder::DecodeMemoryToTarget); | |
| 55 | |
| 56 bool LazyDecodeBitmap(const void* buffer, size_t size, SkBitmap* bitmap) { | |
| 57 void* copiedBuffer = sk_malloc_throw(size); | |
| 58 memcpy(copiedBuffer, buffer, size); | |
| 59 SkAutoDataUnref data(SkData::NewFromMalloc(copiedBuffer, size)); | |
| 60 | |
| 61 static bool gOnce; | |
| 62 if (!gOnce) { | |
| 63 // Only use the cache selector if there is a purgeable image cache to us
e for large | |
| 64 // images. | |
| 65 if (FLAGS_useVolatileCache && SkAutoTUnref<SkImageCache>( | |
| 66 SkPurgeableImageCache::Create()).get() != NULL) { | |
| 67 gFactory.setCacheSelector(&gCacheSelector); | |
| 68 } else { | |
| 69 gFactory.setImageCache(&gLruImageCache); | |
| 70 } | |
| 71 gOnce = true; | |
| 72 } | |
| 73 return gFactory.installPixelRef(data, bitmap); | |
| 74 } | 47 } |
| 75 | |
| 76 } // namespace sk_tools | |
| OLD | NEW |