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 | 9bd3fc7 | 2016-05-04 14:40:18 -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 | */ |
mtklein | 427c281 | 2016-05-04 11:31:29 -0700 | [diff] [blame] | 32 | DiscardableMemoryPool(size_t budget, SkMutex* 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: |
mtklein | 427c281 | 2016-05-04 11:31:29 -0700 | [diff] [blame] | 55 | SkMutex* 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 | |
mtklein | 427c281 | 2016-05-04 11:31:29 -0700 | [diff] [blame] | 131 | DiscardableMemoryPool::DiscardableMemoryPool(size_t budget, SkMutex* mutex) |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 132 | : fMutex(mutex) |
| 133 | , fBudget(budget) |
| 134 | , fUsed(0) { |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 135 | #if SK_LAZY_CACHE_STATS |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 136 | fCacheHits = 0; |
| 137 | fCacheMisses = 0; |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 138 | #endif // SK_LAZY_CACHE_STATS |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 139 | } |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 140 | DiscardableMemoryPool::~DiscardableMemoryPool() { |
| 141 | // PoolDiscardableMemory objects that belong to this pool are |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 142 | // always deleted before deleting this pool since each one has a |
| 143 | // ref to the pool. |
| 144 | SkASSERT(fList.isEmpty()); |
| 145 | } |
| 146 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 147 | void DiscardableMemoryPool::dumpDownTo(size_t budget) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 148 | if (fMutex != nullptr) { |
halcanary | 9b79066 | 2014-06-11 09:40:23 -0700 | [diff] [blame] | 149 | fMutex->assertHeld(); |
| 150 | } |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 151 | if (fUsed <= budget) { |
| 152 | return; |
| 153 | } |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 154 | typedef SkTInternalLList<PoolDiscardableMemory>::Iter Iter; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 155 | Iter iter; |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 156 | PoolDiscardableMemory* cur = iter.init(fList, Iter::kTail_IterStart); |
bsalomon | 49f085d | 2014-09-05 13:34:00 -0700 | [diff] [blame] | 157 | while ((fUsed > budget) && (cur)) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 158 | if (!cur->fLocked) { |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 159 | PoolDiscardableMemory* dm = cur; |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 160 | SkASSERT(dm->fPointer != nullptr); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 161 | sk_free(dm->fPointer); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 162 | dm->fPointer = nullptr; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 163 | SkASSERT(fUsed >= dm->fBytes); |
| 164 | fUsed -= dm->fBytes; |
| 165 | cur = iter.prev(); |
| 166 | // Purged DMs are taken out of the list. This saves times |
| 167 | // looking them up. Purged DMs are NOT deleted. |
| 168 | fList.remove(dm); |
| 169 | } else { |
| 170 | cur = iter.prev(); |
| 171 | } |
| 172 | } |
| 173 | } |
| 174 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 175 | SkDiscardableMemory* DiscardableMemoryPool::create(size_t bytes) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 176 | void* addr = sk_malloc_flags(bytes, 0); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 177 | if (nullptr == addr) { |
| 178 | return nullptr; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 179 | } |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 180 | PoolDiscardableMemory* dm = new PoolDiscardableMemory(this, addr, bytes); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 181 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
| 182 | fList.addToHead(dm); |
| 183 | fUsed += bytes; |
| 184 | this->dumpDownTo(fBudget); |
| 185 | return dm; |
| 186 | } |
| 187 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 188 | void DiscardableMemoryPool::free(PoolDiscardableMemory* dm) { |
halcanary | 2edf599 | 2015-03-26 14:08:56 -0700 | [diff] [blame] | 189 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 190 | // This is called by dm's destructor. |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 191 | if (dm->fPointer != nullptr) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 192 | sk_free(dm->fPointer); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 193 | dm->fPointer = nullptr; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 194 | SkASSERT(fUsed >= dm->fBytes); |
| 195 | fUsed -= dm->fBytes; |
| 196 | fList.remove(dm); |
| 197 | } else { |
| 198 | SkASSERT(!fList.isInList(dm)); |
| 199 | } |
| 200 | } |
| 201 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 202 | bool DiscardableMemoryPool::lock(PoolDiscardableMemory* dm) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 203 | SkASSERT(dm != nullptr); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 204 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 205 | if (nullptr == dm->fPointer) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 206 | // May have been purged while waiting for lock. |
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 | ++fCacheMisses; |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 209 | #endif // SK_LAZY_CACHE_STATS |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 210 | return false; |
| 211 | } |
| 212 | dm->fLocked = true; |
| 213 | fList.remove(dm); |
| 214 | fList.addToHead(dm); |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 215 | #if SK_LAZY_CACHE_STATS |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 216 | ++fCacheHits; |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 217 | #endif // SK_LAZY_CACHE_STATS |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 218 | return true; |
| 219 | } |
| 220 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 221 | void DiscardableMemoryPool::unlock(PoolDiscardableMemory* dm) { |
halcanary | 96fcdcc | 2015-08-27 07:41:13 -0700 | [diff] [blame] | 222 | SkASSERT(dm != nullptr); |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 223 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
| 224 | dm->fLocked = false; |
| 225 | this->dumpDownTo(fBudget); |
| 226 | } |
| 227 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 228 | size_t DiscardableMemoryPool::getRAMUsed() { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 229 | return fUsed; |
| 230 | } |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 231 | void DiscardableMemoryPool::setRAMBudget(size_t budget) { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 232 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
| 233 | fBudget = budget; |
| 234 | this->dumpDownTo(fBudget); |
| 235 | } |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 236 | void DiscardableMemoryPool::dumpPool() { |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 237 | SkAutoMutexAcquire autoMutexAcquire(fMutex); |
| 238 | this->dumpDownTo(0); |
| 239 | } |
| 240 | |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 241 | } // namespace |
| 242 | |
mtklein | 427c281 | 2016-05-04 11:31:29 -0700 | [diff] [blame] | 243 | SkDiscardableMemoryPool* SkDiscardableMemoryPool::Create(size_t size, SkMutex* mutex) { |
halcanary | 385fe4d | 2015-08-26 13:07:48 -0700 | [diff] [blame] | 244 | return new DiscardableMemoryPool(size, mutex); |
commit-bot@chromium.org | cf2f008 | 2014-04-04 16:43:38 +0000 | [diff] [blame] | 245 | } |
| 246 | |
mtklein | 5e56cfd | 2016-05-04 15:21:12 -0700 | [diff] [blame^] | 247 | static SkMutex gMutex; |
mtklein | 148ec59 | 2014-10-13 13:17:56 -0700 | [diff] [blame] | 248 | |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 249 | SkDiscardableMemoryPool* SkGetGlobalDiscardableMemoryPool() { |
mtklein | 9bd3fc7 | 2016-05-04 14:40:18 -0700 | [diff] [blame] | 250 | static SkOnce once; |
| 251 | static SkDiscardableMemoryPool* global; |
| 252 | once([]{ |
| 253 | global = SkDiscardableMemoryPool::Create(SK_DEFAULT_GLOBAL_DISCARDABLE_MEMORY_POOL_SIZE, |
| 254 | &gMutex); |
mtklein | 6c59d80 | 2015-09-09 09:09:53 -0700 | [diff] [blame] | 255 | }); |
mtklein | 9bd3fc7 | 2016-05-04 14:40:18 -0700 | [diff] [blame] | 256 | return global; |
halcanary@google.com | 2c7c7ee | 2013-12-05 18:31:42 +0000 | [diff] [blame] | 257 | } |