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 | 1b24933 | 2015-07-07 12:21:21 -0700 | [diff] [blame] | 11 | #include "SkMutex.h" |
mtklein | ffa4a92 | 2016-05-05 16:05:56 -0700 | [diff] [blame] | 12 | #include "SkOnce.h" |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 13 | #include "SkTInternalLList.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 | */ |
sclittle | d9f5d20 | 2016-05-04 18:23:30 -0700 | [diff] [blame] | 32 | DiscardableMemoryPool(size_t budget, SkBaseMutex* mutex = nullptr); |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 33 | virtual ~DiscardableMemoryPool(); |
| 34 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 35 | SkDiscardableMemory* create(size_t bytes) override; |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 36 | |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 37 | size_t getRAMUsed() override; |
| 38 | void setRAMBudget(size_t budget) override; |
| 39 | size_t getRAMBudget() override { return fBudget; } |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 40 | |
| 41 | /** purges all unlocked DMs */ |
mtklein | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 42 | void dumpPool() 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 | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 45 | int getCacheHits() override { return fCacheHits; } |
| 46 | int getCacheMisses() override { return fCacheMisses; } |
| 47 | void resetCacheHitsAndMisses() 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: |
sclittle | d9f5d20 | 2016-05-04 18:23:30 -0700 | [diff] [blame] | 55 | SkBaseMutex* fMutex; |
| 56 | size_t fBudget; |
| 57 | size_t fUsed; |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 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 | 36352bf | 2015-03-25 18:17:31 -0700 | [diff] [blame] | 83 | bool lock() override; |
| 84 | void* data() override; |
| 85 | void unlock() 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) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 102 | SkASSERT(fPool != nullptr); |
| 103 | SkASSERT(fPointer != nullptr); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 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 | |
sclittle | d9f5d20 | 2016-05-04 18:23:30 -0700 | [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 | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 149 | if (fMutex != nullptr) { |
halcanary | 9b79066 | 2014-06-11 09:40:23 -0700 | [diff] [blame] | 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 | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 161 | SkASSERT(dm->fPointer != nullptr); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 162 | sk_free(dm->fPointer); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 163 | dm->fPointer = nullptr; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 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); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 178 | if (nullptr == addr) { |
| 179 | return nullptr; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 180 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 181 | PoolDiscardableMemory* dm = new PoolDiscardableMemory(this, addr, bytes); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 182 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
| 183 | fList.addToHead(dm); |
| 184 | fUsed += bytes; |
| 185 | this->dumpDownTo(fBudget); |
| 186 | return dm; |
| 187 | } |
| 188 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 189 | void DiscardableMemoryPool::free(PoolDiscardableMemory* dm) { |
halcanary | 2edf599 | 2015-03-26 14:08:56 -0700 | [diff] [blame] | 190 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 191 | // This is called by dm's destructor. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 192 | if (dm->fPointer != nullptr) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 193 | sk_free(dm->fPointer); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 194 | dm->fPointer = nullptr; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 195 | SkASSERT(fUsed >= dm->fBytes); |
| 196 | fUsed -= dm->fBytes; |
| 197 | fList.remove(dm); |
| 198 | } else { |
| 199 | SkASSERT(!fList.isInList(dm)); |
| 200 | } |
| 201 | } |
| 202 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 203 | bool DiscardableMemoryPool::lock(PoolDiscardableMemory* dm) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 204 | SkASSERT(dm != nullptr); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 205 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 206 | if (nullptr == dm->fPointer) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 207 | // May have been purged while waiting for lock. |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 208 | #if SK_LAZY_CACHE_STATS |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 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 | dm->fLocked = true; |
| 214 | fList.remove(dm); |
| 215 | fList.addToHead(dm); |
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 | ++fCacheHits; |
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 true; |
| 220 | } |
| 221 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 222 | void DiscardableMemoryPool::unlock(PoolDiscardableMemory* dm) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 223 | SkASSERT(dm != nullptr); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 224 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
| 225 | dm->fLocked = false; |
| 226 | this->dumpDownTo(fBudget); |
| 227 | } |
| 228 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 229 | size_t DiscardableMemoryPool::getRAMUsed() { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 230 | return fUsed; |
| 231 | } |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 232 | void DiscardableMemoryPool::setRAMBudget(size_t budget) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 233 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
| 234 | fBudget = budget; |
| 235 | this->dumpDownTo(fBudget); |
| 236 | } |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 237 | void DiscardableMemoryPool::dumpPool() { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 238 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
| 239 | this->dumpDownTo(0); |
| 240 | } |
| 241 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 242 | } // namespace |
| 243 | |
sclittle | d9f5d20 | 2016-05-04 18:23:30 -0700 | [diff] [blame] | 244 | SkDiscardableMemoryPool* SkDiscardableMemoryPool::Create(size_t size, SkBaseMutex* mutex) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 245 | return new DiscardableMemoryPool(size, mutex); |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 246 | } |
| 247 | |
reed | 086eea9 | 2016-05-04 17:12:46 -0700 | [diff] [blame] | 248 | SK_DECLARE_STATIC_MUTEX(gMutex); |
mtklein | 148ec59 | 2014-10-13 13:17:56 -0700 | [diff] [blame] | 249 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 250 | SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool() { |
mtklein | ffa4a92 | 2016-05-05 16:05:56 -0700 | [diff] [blame] | 251 | static SkOnce once; |
| 252 | static SkDiscardableMemoryPool* global; |
| 253 | once([]{ |
| 254 | global = SkDiscardableMemoryPool::Create(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE, |
| 255 | &gMutex); |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 256 | }); |
mtklein | ffa4a92 | 2016-05-05 16:05:56 -0700 | [diff] [blame] | 257 | return global; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 258 | } |