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 "SkDiscardablePixelRef.h" | |
14 #include "SkForceLinking.h" | 13 #include "SkForceLinking.h" |
15 #include "SkLruImageCache.h" | 14 #include "SkDiscardableMemoryPool.h" |
16 #include "SkPurgeableImageCache.h" | |
17 #include "SkCommandLineFlags.h" | |
18 | 15 |
19 __SK_FORCE_IMAGE_DECODER_LINKING; | 16 __SK_FORCE_IMAGE_DECODER_LINKING; |
20 | 17 |
21 DEFINE_bool(useVolatileCache, false, "Use a volatile cache for deferred image de coding pixels. " | 18 // Fits SkPicture::InstallPixelRefProc call signature. |
scroggo
2013/12/03 23:00:01
You'll need to modify buildbot/slave/skia_slave_sc
hal.canary
2013/12/04 00:01:36
Okay.
We may want to keep useVolatileCache for An
| |
22 "Only meaningful if --deferImageDecoding is set to true and the plat form has an " | 19 // Used in SkPicturePlayback::CreateFromStream |
23 "implementation."); | 20 bool sk_tools::LazyDecodeBitmap(const void* src, |
24 | 21 size_t length, |
25 SkLruImageCache gLruImageCache(1024 * 1024); | 22 SkBitmap* dst) { |
26 | 23 SkAutoDataUnref data(SkData::NewWithCopy(src, length)); |
27 namespace sk_tools { | 24 if (NULL == data.get()) { |
28 | 25 return false; |
29 // Simple cache selector to choose between a purgeable cache for large images an d the standard one | |
30 // for smaller images. | |
31 // | |
32 class CacheSelector : public SkBitmapFactory::CacheSelector { | |
33 | |
34 public: | |
35 CacheSelector() { | |
36 fPurgeableImageCache = SkPurgeableImageCache::Create(); | |
37 } | 26 } |
38 | 27 |
39 ~CacheSelector() { | 28 SkAutoTDelete<SkImageGenerator> gen(SkNEW_ARGS(SkDecodingImageGenerator, |
40 SkSafeUnref(fPurgeableImageCache); | 29 (data))); |
30 SkImageInfo info; | |
31 if (!gen->getInfo(&info)) { | |
32 return false; | |
41 } | 33 } |
42 | 34 SkDiscardableMemory::Factory* pool = NULL; |
43 virtual SkImageCache* selectCache(const SkImageInfo& info) SK_OVERRIDE { | 35 if (info.fWidth * info.fHeight > 32 * 1024) { |
44 if (info.fWidth * info.fHeight > 32 * 1024 && fPurgeableImageCache != NU LL) { | 36 // how to do switching with SkDiscardableMemory. |
45 return fPurgeableImageCache; | 37 pool = SkGetGlobalDiscardableMemoryPool(); |
46 } | |
47 return &gLruImageCache; | |
48 } | 38 } |
49 private: | 39 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 } | 40 } |
75 | |
76 } // namespace sk_tools | |
OLD | NEW |