halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 1 | /* |
| 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.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 8 | #include "SkDiscardableMemory.h" |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 9 | #include "SkDiscardableMemoryPool.h" |
halcanary | fa37a21 | 2014-07-02 10:43:50 -0700 | [diff] [blame] | 10 | #include "SkImageGenerator.h" |
mtklein | 78358bf | 2014-06-02 08:44:27 -0700 | [diff] [blame] | 11 | #include "SkLazyPtr.h" |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 12 | #include "SkTInternalLList.h" |
| 13 | #include "SkThread.h" |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 14 | |
| 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.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 19 | namespace { |
| 20 | |
| 21 | class PoolDiscardableMemory; |
| 22 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 23 | /** |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 24 | * This non-global pool can be used for unit tests to verify that the |
| 25 | * pool works. |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 26 | */ |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 27 | class DiscardableMemoryPool : public SkDiscardableMemoryPool { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 28 | public: |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 29 | /** |
| 30 | * Without mutex, will be not be thread safe. |
| 31 | */ |
| 32 | DiscardableMemoryPool(size_t budget, SkBaseMutex* mutex = NULL); |
| 33 | virtual ~DiscardableMemoryPool(); |
| 34 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 35 | SkDiscardableMemory* create(size_t bytes) SK_OVERRIDE; |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 36 | |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 37 | size_t getRAMUsed() SK_OVERRIDE; |
| 38 | void setRAMBudget(size_t budget) SK_OVERRIDE; |
| 39 | size_t getRAMBudget() SK_OVERRIDE { return fBudget; } |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 40 | |
| 41 | /** purges all unlocked DMs */ |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 42 | void dumpPool() SK_OVERRIDE; |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 43 | |
| 44 | #if SK_LAZY_CACHE_STATS // Defined in SkDiscardableMemoryPool.h |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 45 | int getCacheHits() SK_OVERRIDE { return fCacheHits; } |
| 46 | int getCacheMisses() SK_OVERRIDE { return fCacheMisses; } |
| 47 | void resetCacheHitsAndMisses() SK_OVERRIDE { |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 48 | fCacheHits = fCacheMisses = 0; |
| 49 | } |
| 50 | int fCacheHits; |
| 51 | int fCacheMisses; |
| 52 | #endif // SK_LAZY_CACHE_STATS |
| 53 | |
| 54 | private: |
| 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 | */ |
| 78 | class PoolDiscardableMemory : public SkDiscardableMemory { |
| 79 | public: |
| 80 | PoolDiscardableMemory(DiscardableMemoryPool* pool, |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 81 | void* pointer, size_t bytes); |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 82 | virtual ~PoolDiscardableMemory(); |
mtklein | 72c9faa | 2015-01-09 10:06:39 -0800 | [diff] [blame] | 83 | bool lock() SK_OVERRIDE; |
| 84 | void* data() SK_OVERRIDE; |
| 85 | void unlock() SK_OVERRIDE; |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 86 | friend class DiscardableMemoryPool; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 87 | private: |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 88 | SK_DECLARE_INTERNAL_LLIST_INTERFACE(PoolDiscardableMemory); |
| 89 | DiscardableMemoryPool* const fPool; |
| 90 | bool fLocked; |
| 91 | void* fPointer; |
| 92 | const size_t fBytes; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 95 | PoolDiscardableMemory::PoolDiscardableMemory(DiscardableMemoryPool* pool, |
| 96 | void* pointer, |
| 97 | size_t bytes) |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 98 | : 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.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 108 | PoolDiscardableMemory::~PoolDiscardableMemory() { |
reed@google.com | 1d0654f | 2013-12-12 22:37:32 +0000 | [diff] [blame] | 109 | SkASSERT(!fLocked); // contract for SkDiscardableMemory |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 110 | fPool->free(this); |
| 111 | fPool->unref(); |
| 112 | } |
| 113 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 114 | bool PoolDiscardableMemory::lock() { |
reed@google.com | 1d0654f | 2013-12-12 22:37:32 +0000 | [diff] [blame] | 115 | SkASSERT(!fLocked); // contract for SkDiscardableMemory |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 116 | return fPool->lock(this); |
| 117 | } |
| 118 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 119 | void* PoolDiscardableMemory::data() { |
reed@google.com | 1d0654f | 2013-12-12 22:37:32 +0000 | [diff] [blame] | 120 | SkASSERT(fLocked); // contract for SkDiscardableMemory |
| 121 | return fPointer; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 122 | } |
| 123 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 124 | void PoolDiscardableMemory::unlock() { |
reed@google.com | 1d0654f | 2013-12-12 22:37:32 +0000 | [diff] [blame] | 125 | SkASSERT(fLocked); // contract for SkDiscardableMemory |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 126 | fPool->unlock(this); |
| 127 | } |
| 128 | |
| 129 | //////////////////////////////////////////////////////////////////////////////// |
| 130 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 131 | DiscardableMemoryPool::DiscardableMemoryPool(size_t budget, |
| 132 | SkBaseMutex* mutex) |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 133 | : fMutex(mutex) |
| 134 | , fBudget(budget) |
| 135 | , fUsed(0) { |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 136 | #if SK_LAZY_CACHE_STATS |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 137 | fCacheHits = 0; |
| 138 | fCacheMisses = 0; |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 139 | #endif // SK_LAZY_CACHE_STATS |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 140 | } |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 141 | DiscardableMemoryPool::~DiscardableMemoryPool() { |
| 142 | // PoolDiscardableMemory objects that belong to this pool are |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 143 | // 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.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 148 | void DiscardableMemoryPool::dumpDownTo(size_t budget) { |
halcanary | 9b79066 | 2014-06-11 09:40:23 -0700 | [diff] [blame] | 149 | if (fMutex != NULL) { |
| 150 | fMutex->assertHeld(); |
| 151 | } |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 152 | if (fUsed <= budget) { |
| 153 | return; |
| 154 | } |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 155 | typedef SkTInternalLList<PoolDiscardableMemory>::Iter Iter; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 156 | Iter iter; |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 157 | PoolDiscardableMemory* cur = iter.init(fList, Iter::kTail_IterStart); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 158 | while ((fUsed > budget) && (cur)) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 159 | if (!cur->fLocked) { |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 160 | PoolDiscardableMemory* dm = cur; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 161 | 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.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 176 | SkDiscardableMemory* DiscardableMemoryPool::create(size_t bytes) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 177 | void* addr = sk_malloc_flags(bytes, 0); |
| 178 | if (NULL == addr) { |
| 179 | return NULL; |
| 180 | } |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 181 | PoolDiscardableMemory* dm = SkNEW_ARGS(PoolDiscardableMemory, |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 182 | (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.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 190 | void DiscardableMemoryPool::free(PoolDiscardableMemory* dm) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 191 | // 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.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 204 | bool DiscardableMemoryPool::lock(PoolDiscardableMemory* dm) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 205 | SkASSERT(dm != NULL); |
| 206 | if (NULL == dm->fPointer) { |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 207 | #if SK_LAZY_CACHE_STATS |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 208 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
| 209 | ++fCacheMisses; |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 210 | #endif // SK_LAZY_CACHE_STATS |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 211 | return false; |
| 212 | } |
| 213 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
| 214 | if (NULL == dm->fPointer) { |
| 215 | // May have been purged while waiting for lock. |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 216 | #if SK_LAZY_CACHE_STATS |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 217 | ++fCacheMisses; |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 218 | #endif // SK_LAZY_CACHE_STATS |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 219 | return false; |
| 220 | } |
| 221 | dm->fLocked = true; |
| 222 | fList.remove(dm); |
| 223 | fList.addToHead(dm); |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 224 | #if SK_LAZY_CACHE_STATS |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 225 | ++fCacheHits; |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 226 | #endif // SK_LAZY_CACHE_STATS |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 227 | return true; |
| 228 | } |
| 229 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 230 | void DiscardableMemoryPool::unlock(PoolDiscardableMemory* dm) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 231 | SkASSERT(dm != NULL); |
| 232 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
| 233 | dm->fLocked = false; |
| 234 | this->dumpDownTo(fBudget); |
| 235 | } |
| 236 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 237 | size_t DiscardableMemoryPool::getRAMUsed() { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 238 | return fUsed; |
| 239 | } |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 240 | void DiscardableMemoryPool::setRAMBudget(size_t budget) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 241 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
| 242 | fBudget = budget; |
| 243 | this->dumpDownTo(fBudget); |
| 244 | } |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 245 | void DiscardableMemoryPool::dumpPool() { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 246 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
| 247 | this->dumpDownTo(0); |
| 248 | } |
| 249 | |
| 250 | //////////////////////////////////////////////////////////////////////////////// |
| 251 | SK_DECLARE_STATIC_MUTEX(gMutex); |
mtklein | 78358bf | 2014-06-02 08:44:27 -0700 | [diff] [blame] | 252 | SkDiscardableMemoryPool* create_global_pool() { |
| 253 | return SkDiscardableMemoryPool::Create(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE, |
| 254 | &gMutex); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 255 | } |
mtklein | 78358bf | 2014-06-02 08:44:27 -0700 | [diff] [blame] | 256 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 257 | } // namespace |
| 258 | |
mtklein | 78358bf | 2014-06-02 08:44:27 -0700 | [diff] [blame] | 259 | SkDiscardableMemoryPool* SkDiscardableMemoryPool::Create(size_t size, SkBaseMutex* mutex) { |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 260 | return SkNEW_ARGS(DiscardableMemoryPool, (size, mutex)); |
| 261 | } |
| 262 | |
mtklein | 148ec59 | 2014-10-13 13:17:56 -0700 | [diff] [blame] | 263 | SK_DECLARE_STATIC_LAZY_PTR(SkDiscardableMemoryPool, global, create_global_pool); |
| 264 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 265 | SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool() { |
mtklein | 78358bf | 2014-06-02 08:44:27 -0700 | [diff] [blame] | 266 | return global.get(); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 267 | } |
| 268 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 269 | //////////////////////////////////////////////////////////////////////////////// |