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 "SkDiscardableMemory.h" | 8 #include "SkDiscardableMemory.h" |
9 #include "SkDiscardableMemoryPool.h" | 9 #include "SkDiscardableMemoryPool.h" |
10 #include "SkImageGenerator.h" | 10 #include "SkImageGenerator.h" |
(...skipping 11 matching lines...) Expand all Loading... |
22 | 22 |
23 /** | 23 /** |
24 * This non-global pool can be used for unit tests to verify that the | 24 * This non-global pool can be used for unit tests to verify that the |
25 * pool works. | 25 * pool works. |
26 */ | 26 */ |
27 class DiscardableMemoryPool : public SkDiscardableMemoryPool { | 27 class DiscardableMemoryPool : public SkDiscardableMemoryPool { |
28 public: | 28 public: |
29 /** | 29 /** |
30 * Without mutex, will be not be thread safe. | 30 * Without mutex, will be not be thread safe. |
31 */ | 31 */ |
32 DiscardableMemoryPool(size_t budget, SkMutex* mutex = nullptr); | 32 DiscardableMemoryPool(size_t budget, SkBaseMutex* mutex = nullptr); |
33 virtual ~DiscardableMemoryPool(); | 33 virtual ~DiscardableMemoryPool(); |
34 | 34 |
35 SkDiscardableMemory* create(size_t bytes) override; | 35 SkDiscardableMemory* create(size_t bytes) override; |
36 | 36 |
37 size_t getRAMUsed() override; | 37 size_t getRAMUsed() override; |
38 void setRAMBudget(size_t budget) override; | 38 void setRAMBudget(size_t budget) override; |
39 size_t getRAMBudget() override { return fBudget; } | 39 size_t getRAMBudget() override { return fBudget; } |
40 | 40 |
41 /** purges all unlocked DMs */ | 41 /** purges all unlocked DMs */ |
42 void dumpPool() override; | 42 void dumpPool() override; |
43 | 43 |
44 #if SK_LAZY_CACHE_STATS // Defined in SkDiscardableMemoryPool.h | 44 #if SK_LAZY_CACHE_STATS // Defined in SkDiscardableMemoryPool.h |
45 int getCacheHits() override { return fCacheHits; } | 45 int getCacheHits() override { return fCacheHits; } |
46 int getCacheMisses() override { return fCacheMisses; } | 46 int getCacheMisses() override { return fCacheMisses; } |
47 void resetCacheHitsAndMisses() override { | 47 void resetCacheHitsAndMisses() override { |
48 fCacheHits = fCacheMisses = 0; | 48 fCacheHits = fCacheMisses = 0; |
49 } | 49 } |
50 int fCacheHits; | 50 int fCacheHits; |
51 int fCacheMisses; | 51 int fCacheMisses; |
52 #endif // SK_LAZY_CACHE_STATS | 52 #endif // SK_LAZY_CACHE_STATS |
53 | 53 |
54 private: | 54 private: |
55 SkMutex* fMutex; | 55 SkBaseMutex* fMutex; |
56 size_t fBudget; | 56 size_t fBudget; |
57 size_t fUsed; | 57 size_t fUsed; |
58 SkTInternalLList<PoolDiscardableMemory> fList; | 58 SkTInternalLList<PoolDiscardableMemory> fList; |
59 | 59 |
60 /** Function called to free memory if needed */ | 60 /** Function called to free memory if needed */ |
61 void dumpDownTo(size_t budget); | 61 void dumpDownTo(size_t budget); |
62 /** called by DiscardableMemoryPool upon destruction */ | 62 /** called by DiscardableMemoryPool upon destruction */ |
63 void free(PoolDiscardableMemory* dm); | 63 void free(PoolDiscardableMemory* dm); |
64 /** called by DiscardableMemoryPool::lock() */ | 64 /** called by DiscardableMemoryPool::lock() */ |
65 bool lock(PoolDiscardableMemory* dm); | 65 bool lock(PoolDiscardableMemory* dm); |
66 /** called by DiscardableMemoryPool::unlock() */ | 66 /** called by DiscardableMemoryPool::unlock() */ |
67 void unlock(PoolDiscardableMemory* dm); | 67 void unlock(PoolDiscardableMemory* dm); |
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 return fPointer; | 121 return fPointer; |
122 } | 122 } |
123 | 123 |
124 void PoolDiscardableMemory::unlock() { | 124 void PoolDiscardableMemory::unlock() { |
125 SkASSERT(fLocked); // contract for SkDiscardableMemory | 125 SkASSERT(fLocked); // contract for SkDiscardableMemory |
126 fPool->unlock(this); | 126 fPool->unlock(this); |
127 } | 127 } |
128 | 128 |
129 //////////////////////////////////////////////////////////////////////////////// | 129 //////////////////////////////////////////////////////////////////////////////// |
130 | 130 |
131 DiscardableMemoryPool::DiscardableMemoryPool(size_t budget, SkMutex* mutex) | 131 DiscardableMemoryPool::DiscardableMemoryPool(size_t budget, |
| 132 SkBaseMutex* mutex) |
132 : fMutex(mutex) | 133 : fMutex(mutex) |
133 , fBudget(budget) | 134 , fBudget(budget) |
134 , fUsed(0) { | 135 , fUsed(0) { |
135 #if SK_LAZY_CACHE_STATS | 136 #if SK_LAZY_CACHE_STATS |
136 fCacheHits = 0; | 137 fCacheHits = 0; |
137 fCacheMisses = 0; | 138 fCacheMisses = 0; |
138 #endif // SK_LAZY_CACHE_STATS | 139 #endif // SK_LAZY_CACHE_STATS |
139 } | 140 } |
140 DiscardableMemoryPool::~DiscardableMemoryPool() { | 141 DiscardableMemoryPool::~DiscardableMemoryPool() { |
141 // PoolDiscardableMemory objects that belong to this pool are | 142 // PoolDiscardableMemory objects that belong to this pool are |
(...skipping 91 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 fBudget = budget; | 234 fBudget = budget; |
234 this->dumpDownTo(fBudget); | 235 this->dumpDownTo(fBudget); |
235 } | 236 } |
236 void DiscardableMemoryPool::dumpPool() { | 237 void DiscardableMemoryPool::dumpPool() { |
237 SkAutoMutexAcquire autoMutexAcquire(fMutex); | 238 SkAutoMutexAcquire autoMutexAcquire(fMutex); |
238 this->dumpDownTo(0); | 239 this->dumpDownTo(0); |
239 } | 240 } |
240 | 241 |
241 } // namespace | 242 } // namespace |
242 | 243 |
243 SkDiscardableMemoryPool* SkDiscardableMemoryPool::Create(size_t size, SkMutex* m
utex) { | 244 SkDiscardableMemoryPool* SkDiscardableMemoryPool::Create(size_t size, SkBaseMute
x* mutex) { |
244 return new DiscardableMemoryPool(size, mutex); | 245 return new DiscardableMemoryPool(size, mutex); |
245 } | 246 } |
246 | 247 |
247 SK_DECLARE_STATIC_MUTEX(gMutex); | 248 SK_DECLARE_STATIC_MUTEX(gMutex); |
248 SK_DECLARE_STATIC_ONCE_PTR(SkDiscardableMemoryPool, global); | 249 SK_DECLARE_STATIC_ONCE_PTR(SkDiscardableMemoryPool, global); |
249 | 250 |
250 SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool() { | 251 SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool() { |
251 return global.get([] { | 252 return global.get([] { |
252 return SkDiscardableMemoryPool::Create(SK_DEFAULT_GLOBAL_DISCARDABLE_MEM
ORY_POOL_SIZE, | 253 return SkDiscardableMemoryPool::Create(SK_DEFAULT_GLOBAL_DISCARDABLE_MEM
ORY_POOL_SIZE, |
253 &gMutex); | 254 &gMutex); |
254 }); | 255 }); |
255 } | 256 } |
OLD | NEW |