blob: 96d6046f3fb6c4f6e80eff1d8cf91a22b06dd946 [file] [log] [blame]
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +00001/*
2 * Copyright 2013 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
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +00008#include "SkDiscardableMemory.h"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +00009#include "SkDiscardableMemoryPool.h"
halcanaryfa37a212014-07-02 10:43:50 -070010#include "SkImageGenerator.h"
mtklein78358bf2014-06-02 08:44:27 -070011#include "SkLazyPtr.h"
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000012#include "SkTInternalLList.h"
13#include "SkThread.h"
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000014
15// Note:
16// A PoolDiscardableMemory is memory that is counted in a pool.
17// A DiscardableMemoryPool is a pool of PoolDiscardableMemorys.
18
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000019namespace {
20
21class PoolDiscardableMemory;
22
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000023/**
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000024 * This non-global pool can be used for unit tests to verify that the
25 * pool works.
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000026 */
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000027class DiscardableMemoryPool : public SkDiscardableMemoryPool {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000028public:
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000029 /**
30 * Without mutex, will be not be thread safe.
31 */
32 DiscardableMemoryPool(size_t budget, SkBaseMutex* mutex = NULL);
33 virtual ~DiscardableMemoryPool();
34
35 virtual SkDiscardableMemory* create(size_t bytes) SK_OVERRIDE;
36
37 virtual size_t getRAMUsed() SK_OVERRIDE;
38 virtual void setRAMBudget(size_t budget) SK_OVERRIDE;
39 virtual size_t getRAMBudget() SK_OVERRIDE { return fBudget; }
40
41 /** purges all unlocked DMs */
42 virtual void dumpPool() SK_OVERRIDE;
43
44 #if SK_LAZY_CACHE_STATS // Defined in SkDiscardableMemoryPool.h
45 virtual int getCacheHits() SK_OVERRIDE { return fCacheHits; }
46 virtual int getCacheMisses() SK_OVERRIDE { return fCacheMisses; }
47 virtual void resetCacheHitsAndMisses() SK_OVERRIDE {
48 fCacheHits = fCacheMisses = 0;
49 }
50 int fCacheHits;
51 int fCacheMisses;
52 #endif // SK_LAZY_CACHE_STATS
53
54private:
55 SkBaseMutex* fMutex;
56 size_t fBudget;
57 size_t fUsed;
58 SkTInternalLList<PoolDiscardableMemory> fList;
59
60 /** Function called to free memory if needed */
61 void dumpDownTo(size_t budget);
62 /** called by DiscardableMemoryPool upon destruction */
63 void free(PoolDiscardableMemory* dm);
64 /** called by DiscardableMemoryPool::lock() */
65 bool lock(PoolDiscardableMemory* dm);
66 /** called by DiscardableMemoryPool::unlock() */
67 void unlock(PoolDiscardableMemory* dm);
68
69 friend class PoolDiscardableMemory;
70
71 typedef SkDiscardableMemory::Factory INHERITED;
72};
73
74/**
75 * A PoolDiscardableMemory is a SkDiscardableMemory that relies on
76 * a DiscardableMemoryPool object to manage the memory.
77 */
78class PoolDiscardableMemory : public SkDiscardableMemory {
79public:
80 PoolDiscardableMemory(DiscardableMemoryPool* pool,
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000081 void* pointer, size_t bytes);
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000082 virtual ~PoolDiscardableMemory();
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000083 virtual bool lock() SK_OVERRIDE;
84 virtual void* data() SK_OVERRIDE;
85 virtual void unlock() SK_OVERRIDE;
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000086 friend class DiscardableMemoryPool;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000087private:
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000088 SK_DECLARE_INTERNAL_LLIST_INTERFACE(PoolDiscardableMemory);
89 DiscardableMemoryPool* const fPool;
90 bool fLocked;
91 void* fPointer;
92 const size_t fBytes;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000093};
94
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +000095PoolDiscardableMemory::PoolDiscardableMemory(DiscardableMemoryPool* pool,
96 void* pointer,
97 size_t bytes)
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +000098 : fPool(pool)
99 , fLocked(true)
100 , fPointer(pointer)
101 , fBytes(bytes) {
102 SkASSERT(fPool != NULL);
103 SkASSERT(fPointer != NULL);
104 SkASSERT(fBytes > 0);
105 fPool->ref();
106}
107
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000108PoolDiscardableMemory::~PoolDiscardableMemory() {
reed@google.com1d0654f2013-12-12 22:37:32 +0000109 SkASSERT(!fLocked); // contract for SkDiscardableMemory
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000110 fPool->free(this);
111 fPool->unref();
112}
113
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000114bool PoolDiscardableMemory::lock() {
reed@google.com1d0654f2013-12-12 22:37:32 +0000115 SkASSERT(!fLocked); // contract for SkDiscardableMemory
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000116 return fPool->lock(this);
117}
118
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000119void* PoolDiscardableMemory::data() {
reed@google.com1d0654f2013-12-12 22:37:32 +0000120 SkASSERT(fLocked); // contract for SkDiscardableMemory
121 return fPointer;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000122}
123
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000124void PoolDiscardableMemory::unlock() {
reed@google.com1d0654f2013-12-12 22:37:32 +0000125 SkASSERT(fLocked); // contract for SkDiscardableMemory
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000126 fPool->unlock(this);
127}
128
129////////////////////////////////////////////////////////////////////////////////
130
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000131DiscardableMemoryPool::DiscardableMemoryPool(size_t budget,
132 SkBaseMutex* mutex)
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000133 : fMutex(mutex)
134 , fBudget(budget)
135 , fUsed(0) {
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000136 #if SK_LAZY_CACHE_STATS
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000137 fCacheHits = 0;
138 fCacheMisses = 0;
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000139 #endif // SK_LAZY_CACHE_STATS
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000140}
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000141DiscardableMemoryPool::~DiscardableMemoryPool() {
142 // PoolDiscardableMemory objects that belong to this pool are
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000143 // always deleted before deleting this pool since each one has a
144 // ref to the pool.
145 SkASSERT(fList.isEmpty());
146}
147
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000148void DiscardableMemoryPool::dumpDownTo(size_t budget) {
halcanary9b790662014-06-11 09:40:23 -0700149 if (fMutex != NULL) {
150 fMutex->assertHeld();
151 }
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000152 if (fUsed <= budget) {
153 return;
154 }
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000155 typedef SkTInternalLList<PoolDiscardableMemory>::Iter Iter;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000156 Iter iter;
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000157 PoolDiscardableMemory* cur = iter.init(fList, Iter::kTail_IterStart);
bsalomon49f085d2014-09-05 13:34:00 -0700158 while ((fUsed > budget) && (cur)) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000159 if (!cur->fLocked) {
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000160 PoolDiscardableMemory* dm = cur;
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000161 SkASSERT(dm->fPointer != NULL);
162 sk_free(dm->fPointer);
163 dm->fPointer = NULL;
164 SkASSERT(fUsed >= dm->fBytes);
165 fUsed -= dm->fBytes;
166 cur = iter.prev();
167 // Purged DMs are taken out of the list. This saves times
168 // looking them up. Purged DMs are NOT deleted.
169 fList.remove(dm);
170 } else {
171 cur = iter.prev();
172 }
173 }
174}
175
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000176SkDiscardableMemory* DiscardableMemoryPool::create(size_t bytes) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000177 void* addr = sk_malloc_flags(bytes, 0);
178 if (NULL == addr) {
179 return NULL;
180 }
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000181 PoolDiscardableMemory* dm = SkNEW_ARGS(PoolDiscardableMemory,
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000182 (this, addr, bytes));
183 SkAutoMutexAcquire autoMutexAcquire(fMutex);
184 fList.addToHead(dm);
185 fUsed += bytes;
186 this->dumpDownTo(fBudget);
187 return dm;
188}
189
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000190void DiscardableMemoryPool::free(PoolDiscardableMemory* dm) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000191 // This is called by dm's destructor.
192 if (dm->fPointer != NULL) {
193 SkAutoMutexAcquire autoMutexAcquire(fMutex);
194 sk_free(dm->fPointer);
195 dm->fPointer = NULL;
196 SkASSERT(fUsed >= dm->fBytes);
197 fUsed -= dm->fBytes;
198 fList.remove(dm);
199 } else {
200 SkASSERT(!fList.isInList(dm));
201 }
202}
203
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000204bool DiscardableMemoryPool::lock(PoolDiscardableMemory* dm) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000205 SkASSERT(dm != NULL);
206 if (NULL == dm->fPointer) {
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000207 #if SK_LAZY_CACHE_STATS
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000208 SkAutoMutexAcquire autoMutexAcquire(fMutex);
209 ++fCacheMisses;
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000210 #endif // SK_LAZY_CACHE_STATS
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000211 return false;
212 }
213 SkAutoMutexAcquire autoMutexAcquire(fMutex);
214 if (NULL == dm->fPointer) {
215 // May have been purged while waiting for lock.
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000216 #if SK_LAZY_CACHE_STATS
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000217 ++fCacheMisses;
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000218 #endif // SK_LAZY_CACHE_STATS
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000219 return false;
220 }
221 dm->fLocked = true;
222 fList.remove(dm);
223 fList.addToHead(dm);
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000224 #if SK_LAZY_CACHE_STATS
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000225 ++fCacheHits;
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000226 #endif // SK_LAZY_CACHE_STATS
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000227 return true;
228}
229
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000230void DiscardableMemoryPool::unlock(PoolDiscardableMemory* dm) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000231 SkASSERT(dm != NULL);
232 SkAutoMutexAcquire autoMutexAcquire(fMutex);
233 dm->fLocked = false;
234 this->dumpDownTo(fBudget);
235}
236
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000237size_t DiscardableMemoryPool::getRAMUsed() {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000238 return fUsed;
239}
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000240void DiscardableMemoryPool::setRAMBudget(size_t budget) {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000241 SkAutoMutexAcquire autoMutexAcquire(fMutex);
242 fBudget = budget;
243 this->dumpDownTo(fBudget);
244}
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000245void DiscardableMemoryPool::dumpPool() {
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000246 SkAutoMutexAcquire autoMutexAcquire(fMutex);
247 this->dumpDownTo(0);
248}
249
250////////////////////////////////////////////////////////////////////////////////
251SK_DECLARE_STATIC_MUTEX(gMutex);
mtklein78358bf2014-06-02 08:44:27 -0700252SkDiscardableMemoryPool* create_global_pool() {
253 return SkDiscardableMemoryPool::Create(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE,
254 &gMutex);
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000255}
mtklein78358bf2014-06-02 08:44:27 -0700256
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000257} // namespace
258
mtklein78358bf2014-06-02 08:44:27 -0700259SkDiscardableMemoryPool* SkDiscardableMemoryPool::Create(size_t size, SkBaseMutex* mutex) {
commit-bot@chromium.orgcf2f0082014-04-04 16:43:38 +0000260 return SkNEW_ARGS(DiscardableMemoryPool, (size, mutex));
261}
262
mtklein148ec592014-10-13 13:17:56 -0700263SK_DECLARE_STATIC_LAZY_PTR(SkDiscardableMemoryPool, global, create_global_pool);
264
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000265SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool() {
mtklein78358bf2014-06-02 08:44:27 -0700266 return global.get();
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000267}
268
halcanary@google.com2c7c7ee2013-12-05 18:31:42 +0000269////////////////////////////////////////////////////////////////////////////////