OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright 2016 Google Inc. |
| 3 * |
| 4 * Use of this source code is governed by a BSD-style license that can be |
| 5 * found in the LICENSE file. |
| 6 */ |
| 7 |
| 8 #include "SkImageFilterCache.h" |
| 9 |
| 10 #include "SkChecksum.h" |
| 11 #include "SkMutex.h" |
| 12 #include "SkOncePtr.h" |
| 13 #include "SkRefCnt.h" |
| 14 #include "SkSpecialImage.h" |
| 15 #include "SkTDynamicHash.h" |
| 16 #include "SkTInternalLList.h" |
| 17 |
| 18 #ifdef SK_BUILD_FOR_IOS |
| 19 enum { kDefaultCacheSize = 2 * 1024 * 1024 }; |
| 20 #else |
| 21 enum { kDefaultCacheSize = 128 * 1024 * 1024 }; |
| 22 #endif |
| 23 |
| 24 namespace { |
| 25 |
| 26 class CacheImpl : public SkImageFilterCache { |
| 27 public: |
| 28 typedef SkImageFilterCacheKey Key; |
| 29 CacheImpl(size_t maxBytes) : fMaxBytes(maxBytes), fCurrentBytes(0) { } |
| 30 ~CacheImpl() override { |
| 31 SkTDynamicHash<Value, Key>::Iter iter(&fLookup); |
| 32 |
| 33 while (!iter.done()) { |
| 34 Value* v = &*iter; |
| 35 ++iter; |
| 36 delete v; |
| 37 } |
| 38 } |
| 39 struct Value { |
| 40 Value(const Key& key, SkSpecialImage* image, const SkIPoint& offset) |
| 41 : fKey(key), fImage(SkRef(image)), fOffset(offset) {} |
| 42 |
| 43 Key fKey; |
| 44 SkAutoTUnref<SkSpecialImage> fImage; |
| 45 SkIPoint fOffset; |
| 46 static const Key& GetKey(const Value& v) { |
| 47 return v.fKey; |
| 48 } |
| 49 static uint32_t Hash(const Key& key) { |
| 50 return SkChecksum::Murmur3(reinterpret_cast<const uint32_t*>(&key),
sizeof(Key)); |
| 51 } |
| 52 SK_DECLARE_INTERNAL_LLIST_INTERFACE(Value); |
| 53 }; |
| 54 |
| 55 SkSpecialImage* get(const Key& key, SkIPoint* offset) const override { |
| 56 SkAutoMutexAcquire mutex(fMutex); |
| 57 if (Value* v = fLookup.find(key)) { |
| 58 *offset = v->fOffset; |
| 59 if (v != fLRU.head()) { |
| 60 fLRU.remove(v); |
| 61 fLRU.addToHead(v); |
| 62 } |
| 63 return v->fImage; |
| 64 } |
| 65 return nullptr; |
| 66 } |
| 67 |
| 68 void set(const Key& key, SkSpecialImage* image, const SkIPoint& offset) over
ride { |
| 69 SkAutoMutexAcquire mutex(fMutex); |
| 70 if (Value* v = fLookup.find(key)) { |
| 71 this->removeInternal(v); |
| 72 } |
| 73 Value* v = new Value(key, image, offset); |
| 74 fLookup.add(v); |
| 75 fLRU.addToHead(v); |
| 76 fCurrentBytes += image->getSize(); |
| 77 while (fCurrentBytes > fMaxBytes) { |
| 78 Value* tail = fLRU.tail(); |
| 79 SkASSERT(tail); |
| 80 if (tail == v) { |
| 81 break; |
| 82 } |
| 83 this->removeInternal(tail); |
| 84 } |
| 85 } |
| 86 |
| 87 void purge() override { |
| 88 SkAutoMutexAcquire mutex(fMutex); |
| 89 while (fCurrentBytes > 0) { |
| 90 Value* tail = fLRU.tail(); |
| 91 SkASSERT(tail); |
| 92 this->removeInternal(tail); |
| 93 } |
| 94 } |
| 95 |
| 96 void purgeByKeys(const Key keys[], int count) override { |
| 97 SkAutoMutexAcquire mutex(fMutex); |
| 98 for (int i = 0; i < count; i++) { |
| 99 if (Value* v = fLookup.find(keys[i])) { |
| 100 this->removeInternal(v); |
| 101 } |
| 102 } |
| 103 } |
| 104 |
| 105 SkDEBUGCODE(int count() const override { return fLookup.count(); }) |
| 106 private: |
| 107 void removeInternal(Value* v) { |
| 108 SkASSERT(v->fImage); |
| 109 fCurrentBytes -= v->fImage->getSize(); |
| 110 fLRU.remove(v); |
| 111 fLookup.remove(v->fKey); |
| 112 delete v; |
| 113 } |
| 114 private: |
| 115 SkTDynamicHash<Value, Key> fLookup; |
| 116 mutable SkTInternalLList<Value> fLRU; |
| 117 size_t fMaxBytes; |
| 118 size_t fCurrentBytes; |
| 119 mutable SkMutex fMutex; |
| 120 }; |
| 121 |
| 122 } // namespace |
| 123 |
| 124 SkImageFilterCache* SkImageFilterCache::Create(size_t maxBytes) { |
| 125 return new CacheImpl(maxBytes); |
| 126 } |
| 127 |
| 128 SK_DECLARE_STATIC_ONCE_PTR(SkImageFilterCache, cache); |
| 129 SkImageFilterCache* SkImageFilterCache::Get() { |
| 130 return cache.get([]{ return SkImageFilterCache::Create(kDefaultCacheSize); }
); |
| 131 } |
OLD | NEW |