epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 1 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 2 | /* |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 3 | * Copyright 2010 Google Inc. |
| 4 | * |
| 5 | * Use of this source code is governed by a BSD-style license that can be |
| 6 | * found in the LICENSE file. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | |
epoger@google.com | ec3ed6a | 2011-07-28 14:26:00 +0000 | [diff] [blame] | 10 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 11 | #include "GrResourceCache.h" |
| 12 | #include "GrResource.h" |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 13 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 14 | GrResourceEntry::GrResourceEntry(const GrResourceKey& key, GrResource* resource) |
| 15 | : fKey(key), fResource(resource) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 16 | fLockCount = 0; |
| 17 | fPrev = fNext = NULL; |
| 18 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 19 | // we assume ownership of the resource, and will unref it when we die |
| 20 | GrAssert(resource); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 21 | } |
| 22 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 23 | GrResourceEntry::~GrResourceEntry() { |
| 24 | fResource->unref(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 25 | } |
| 26 | |
| 27 | #if GR_DEBUG |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 28 | void GrResourceEntry::validate() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 29 | GrAssert(fLockCount >= 0); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 30 | GrAssert(fResource); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 31 | GrAssert(fResource->getCacheEntry() == this); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 32 | fResource->validate(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 33 | } |
| 34 | #endif |
| 35 | |
| 36 | /////////////////////////////////////////////////////////////////////////////// |
| 37 | |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 38 | GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) : |
| 39 | fMaxCount(maxCount), |
| 40 | fMaxBytes(maxBytes) { |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame^] | 41 | #if GR_DEBUG |
| 42 | fHighWaterEntryCount = 0; |
| 43 | fHighWaterUnlockedEntryCount = 0; |
| 44 | fHighWaterEntryBytes = 0; |
| 45 | fHighWaterClientDetachedCount = 0; |
| 46 | fHighWaterClientDetachedBytes = 0; |
| 47 | #endif |
| 48 | |
| 49 | fEntryCount = 0; |
| 50 | fUnlockedEntryCount = 0; |
| 51 | fEntryBytes = 0; |
| 52 | fClientDetachedCount = 0; |
| 53 | fClientDetachedBytes = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 54 | |
| 55 | fHead = fTail = NULL; |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 56 | fPurging = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 57 | } |
| 58 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 59 | GrResourceCache::~GrResourceCache() { |
| 60 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 61 | |
bsalomon@google.com | 8fe7247 | 2011-03-30 21:26:44 +0000 | [diff] [blame] | 62 | this->removeAll(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 63 | } |
| 64 | |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 65 | void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{ |
| 66 | if (maxResources) { |
| 67 | *maxResources = fMaxCount; |
| 68 | } |
| 69 | if (maxResourceBytes) { |
| 70 | *maxResourceBytes = fMaxBytes; |
| 71 | } |
| 72 | } |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 73 | |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 74 | void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) { |
| 75 | bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes); |
| 76 | |
| 77 | fMaxCount = maxResources; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 78 | fMaxBytes = maxResourceBytes; |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 79 | |
| 80 | if (smaller) { |
| 81 | this->purgeAsNeeded(); |
| 82 | } |
| 83 | } |
| 84 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 85 | void GrResourceCache::internalDetach(GrResourceEntry* entry, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 86 | bool clientDetach) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 87 | GrResourceEntry* prev = entry->fPrev; |
| 88 | GrResourceEntry* next = entry->fNext; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 89 | |
| 90 | if (prev) { |
| 91 | prev->fNext = next; |
| 92 | } else { |
| 93 | fHead = next; |
| 94 | } |
| 95 | if (next) { |
| 96 | next->fPrev = prev; |
| 97 | } else { |
| 98 | fTail = prev; |
| 99 | } |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 100 | if (!entry->isLocked()) { |
| 101 | --fUnlockedEntryCount; |
| 102 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 103 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 104 | entry->fPrev = NULL; |
| 105 | entry->fNext = NULL; |
| 106 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 107 | // update our stats |
| 108 | if (clientDetach) { |
| 109 | fClientDetachedCount += 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 110 | fClientDetachedBytes += entry->resource()->sizeInBytes(); |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame^] | 111 | |
| 112 | #if GR_DEBUG |
| 113 | if (fHighWaterClientDetachedCount < fClientDetachedCount) { |
| 114 | fHighWaterClientDetachedCount = fClientDetachedCount; |
| 115 | } |
| 116 | if (fHighWaterClientDetachedBytes < fClientDetachedBytes) { |
| 117 | fHighWaterClientDetachedBytes = fClientDetachedBytes; |
| 118 | } |
| 119 | #endif |
| 120 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 121 | } else { |
| 122 | fEntryCount -= 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 123 | fEntryBytes -= entry->resource()->sizeInBytes(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 124 | } |
| 125 | } |
| 126 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 127 | void GrResourceCache::attachToHead(GrResourceEntry* entry, |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 128 | bool clientReattach) { |
| 129 | entry->fPrev = NULL; |
| 130 | entry->fNext = fHead; |
| 131 | if (fHead) { |
| 132 | fHead->fPrev = entry; |
| 133 | } |
| 134 | fHead = entry; |
| 135 | if (NULL == fTail) { |
| 136 | fTail = entry; |
| 137 | } |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 138 | if (!entry->isLocked()) { |
| 139 | ++fUnlockedEntryCount; |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame^] | 140 | #if GR_DEBUG |
| 141 | if (fHighWaterUnlockedEntryCount < fUnlockedEntryCount) { |
| 142 | fHighWaterUnlockedEntryCount = fUnlockedEntryCount; |
| 143 | } |
| 144 | #endif |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 145 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 146 | |
| 147 | // update our stats |
| 148 | if (clientReattach) { |
| 149 | fClientDetachedCount -= 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 150 | fClientDetachedBytes -= entry->resource()->sizeInBytes(); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 151 | } else { |
| 152 | fEntryCount += 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 153 | fEntryBytes += entry->resource()->sizeInBytes(); |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame^] | 154 | |
| 155 | #if GR_DEBUG |
| 156 | if (fHighWaterEntryCount < fEntryCount) { |
| 157 | fHighWaterEntryCount = fEntryCount; |
| 158 | } |
| 159 | if (fHighWaterEntryBytes < fEntryBytes) { |
| 160 | fHighWaterEntryBytes = fEntryBytes; |
| 161 | } |
| 162 | #endif |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 163 | } |
| 164 | } |
| 165 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 166 | class GrResourceCache::Key { |
| 167 | typedef GrResourceEntry T; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 168 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 169 | const GrResourceKey& fKey; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 170 | public: |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 171 | Key(const GrResourceKey& key) : fKey(key) {} |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 172 | |
| 173 | uint32_t getHash() const { return fKey.hashIndex(); } |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 174 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 175 | static bool LT(const T& entry, const Key& key) { |
| 176 | return entry.key() < key.fKey; |
| 177 | } |
| 178 | static bool EQ(const T& entry, const Key& key) { |
| 179 | return entry.key() == key.fKey; |
| 180 | } |
| 181 | #if GR_DEBUG |
| 182 | static uint32_t GetHash(const T& entry) { |
| 183 | return entry.key().hashIndex(); |
| 184 | } |
| 185 | static bool LT(const T& a, const T& b) { |
| 186 | return a.key() < b.key(); |
| 187 | } |
| 188 | static bool EQ(const T& a, const T& b) { |
| 189 | return a.key() == b.key(); |
| 190 | } |
| 191 | #endif |
| 192 | }; |
| 193 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 194 | GrResource* GrResourceCache::findAndLock(const GrResourceKey& key, |
| 195 | LockType type) { |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 196 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 197 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 198 | GrResourceEntry* entry = fCache.find(key); |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 199 | if (NULL == entry) { |
| 200 | return NULL; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 201 | } |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 202 | |
| 203 | this->internalDetach(entry, false); |
| 204 | // mark the entry as "busy" so it doesn't get purged |
| 205 | // do this between detach and attach for locked count tracking |
| 206 | if (kNested_LockType == type || !entry->isLocked()) { |
| 207 | entry->lock(); |
| 208 | } |
| 209 | this->attachToHead(entry, false); |
| 210 | |
| 211 | return entry->fResource; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 212 | } |
| 213 | |
bsalomon@google.com | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 214 | bool GrResourceCache::hasKey(const GrResourceKey& key) const { |
| 215 | return NULL != fCache.find(key); |
| 216 | } |
| 217 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 218 | GrResourceEntry* GrResourceCache::create(const GrResourceKey& key, |
| 219 | GrResource* resource, |
robertphillips@google.com | 386319e | 2012-06-27 14:59:18 +0000 | [diff] [blame] | 220 | bool lock, |
| 221 | bool clientReattach) { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 222 | // we don't expect to create new resources during a purge. In theory |
| 223 | // this could cause purgeAsNeeded() into an infinite loop (e.g. |
| 224 | // each resource destroyed creates and locks 2 resources and |
| 225 | // unlocks 1 thereby causing a new purge). |
| 226 | GrAssert(!fPurging); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 227 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 228 | |
tomhudson@google.com | c377baf | 2012-07-09 20:17:56 +0000 | [diff] [blame] | 229 | GrResourceEntry* entry = SkNEW_ARGS(GrResourceEntry, (key, resource)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 230 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 231 | resource->setCacheEntry(entry); |
| 232 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 233 | if (lock) { |
| 234 | // mark the entry as "busy" so it doesn't get purged |
| 235 | // do this before attach for locked count tracking |
| 236 | entry->lock(); |
| 237 | } |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 238 | |
robertphillips@google.com | 386319e | 2012-06-27 14:59:18 +0000 | [diff] [blame] | 239 | this->attachToHead(entry, clientReattach); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 240 | fCache.insert(key, entry); |
| 241 | |
| 242 | #if GR_DUMP_TEXTURE_UPLOAD |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 243 | GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n", |
| 244 | entry, fEntryCount, resource->sizeInBytes(), fEntryBytes); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 245 | #endif |
| 246 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 247 | this->purgeAsNeeded(); |
| 248 | return entry; |
| 249 | } |
| 250 | |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 251 | void GrResourceCache::createAndLock(const GrResourceKey& key, |
| 252 | GrResource* resource) { |
| 253 | GrAssert(NULL == resource->getCacheEntry()); |
| 254 | this->create(key, resource, true, false); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | void GrResourceCache::attach(const GrResourceKey& key, |
| 258 | GrResource* resource) { |
robertphillips@google.com | 1f47f4f | 2012-08-16 14:49:16 +0000 | [diff] [blame] | 259 | GrAssert(NULL == resource->getCacheEntry()); |
robertphillips@google.com | 386319e | 2012-06-27 14:59:18 +0000 | [diff] [blame] | 260 | this->create(key, resource, false, true); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 261 | } |
| 262 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 263 | void GrResourceCache::detach(GrResourceEntry* entry) { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 264 | GrAutoResourceCacheValidate atcv(this); |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 265 | this->internalDetach(entry, true); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 266 | fCache.remove(entry->fKey, entry); |
| 267 | } |
| 268 | |
robertphillips@google.com | 15c0fea | 2012-06-22 12:41:43 +0000 | [diff] [blame] | 269 | void GrResourceCache::freeEntry(GrResourceEntry* entry) { |
| 270 | GrAssert(NULL == entry->fNext && NULL == entry->fPrev); |
| 271 | delete entry; |
| 272 | } |
| 273 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 274 | void GrResourceCache::reattachAndUnlock(GrResourceEntry* entry) { |
bsalomon@google.com | 6087975 | 2011-09-15 20:43:53 +0000 | [diff] [blame] | 275 | GrAutoResourceCacheValidate atcv(this); |
| 276 | if (entry->resource()->isValid()) { |
| 277 | attachToHead(entry, true); |
| 278 | fCache.insert(entry->key(), entry); |
| 279 | } else { |
| 280 | // If the resource went invalid while it was detached then purge it |
| 281 | // This can happen when a 3D context was lost, |
| 282 | // the client called GrContext::contextDestroyed() to notify Gr, |
| 283 | // and then later an SkGpuDevice's destructor releases its backing |
| 284 | // texture (which was invalidated at contextDestroyed time). |
| 285 | fClientDetachedCount -= 1; |
| 286 | fEntryCount -= 1; |
| 287 | size_t size = entry->resource()->sizeInBytes(); |
| 288 | fClientDetachedBytes -= size; |
| 289 | fEntryBytes -= size; |
| 290 | } |
| 291 | this->unlock(entry); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 292 | } |
| 293 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 294 | void GrResourceCache::unlock(GrResourceEntry* entry) { |
| 295 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 296 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 297 | GrAssert(entry); |
| 298 | GrAssert(entry->isLocked()); |
| 299 | GrAssert(fCache.find(entry->key())); |
| 300 | |
| 301 | entry->unlock(); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 302 | if (!entry->isLocked()) { |
| 303 | ++fUnlockedEntryCount; |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame^] | 304 | #if GR_DEBUG |
| 305 | if (fHighWaterUnlockedEntryCount < fUnlockedEntryCount) { |
| 306 | fHighWaterUnlockedEntryCount = fUnlockedEntryCount; |
| 307 | } |
| 308 | #endif |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 309 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 310 | this->purgeAsNeeded(); |
| 311 | } |
| 312 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 313 | /** |
| 314 | * Destroying a resource may potentially trigger the unlock of additional |
| 315 | * resources which in turn will trigger a nested purge. We block the nested |
| 316 | * purge using the fPurging variable. However, the initial purge will keep |
| 317 | * looping until either all resources in the cache are unlocked or we've met |
| 318 | * the budget. There is an assertion in createAndLock to check against a |
| 319 | * resource's destructor inserting new resources into the cache. If these |
| 320 | * new resources were unlocked before purgeAsNeeded completed it could |
| 321 | * potentially make purgeAsNeeded loop infinitely. |
| 322 | */ |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 323 | void GrResourceCache::purgeAsNeeded() { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 324 | if (!fPurging) { |
| 325 | fPurging = true; |
| 326 | bool withinBudget = false; |
| 327 | do { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 328 | GrResourceEntry* entry = fTail; |
| 329 | while (entry && fUnlockedEntryCount) { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 330 | GrAutoResourceCacheValidate atcv(this); |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 331 | if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 332 | withinBudget = true; |
| 333 | break; |
| 334 | } |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 335 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 336 | GrResourceEntry* prev = entry->fPrev; |
| 337 | if (!entry->isLocked()) { |
| 338 | // remove from our cache |
| 339 | fCache.remove(entry->fKey, entry); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 340 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 341 | // remove from our llist |
| 342 | this->internalDetach(entry, false); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 343 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 344 | #if GR_DUMP_TEXTURE_UPLOAD |
| 345 | GrPrintf("--- ~resource from cache %p [%d %d]\n", |
| 346 | entry->resource(), |
| 347 | entry->resource()->width(), |
| 348 | entry->resource()->height()); |
| 349 | #endif |
| 350 | delete entry; |
| 351 | } |
| 352 | entry = prev; |
| 353 | } |
| 354 | } while (!withinBudget && fUnlockedEntryCount); |
| 355 | fPurging = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 356 | } |
| 357 | } |
| 358 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 359 | void GrResourceCache::removeAll() { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 360 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 361 | |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 362 | // we can have one GrResource holding a lock on another |
| 363 | // so we don't want to just do a simple loop kicking each |
| 364 | // entry out. Instead change the budget and purge. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 365 | |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 366 | int savedMaxBytes = fMaxBytes; |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 367 | int savedMaxCount = fMaxCount; |
| 368 | fMaxBytes = (size_t) -1; |
| 369 | fMaxCount = 0; |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 370 | this->purgeAsNeeded(); |
| 371 | |
twiz@google.com | 0ec107f | 2012-02-21 19:15:53 +0000 | [diff] [blame] | 372 | #if GR_DEBUG |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 373 | GrAssert(!fUnlockedEntryCount); |
twiz@google.com | 0ec107f | 2012-02-21 19:15:53 +0000 | [diff] [blame] | 374 | if (!fCache.count()) { |
| 375 | // Items may have been detached from the cache (such as the backing |
| 376 | // texture for an SkGpuDevice). The above purge would not have removed |
| 377 | // them. |
| 378 | GrAssert(fEntryCount == fClientDetachedCount); |
| 379 | GrAssert(fEntryBytes == fClientDetachedBytes); |
| 380 | GrAssert(NULL == fHead); |
| 381 | GrAssert(NULL == fTail); |
| 382 | } |
| 383 | #endif |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 384 | |
| 385 | fMaxBytes = savedMaxBytes; |
bsalomon@google.com | 07fc0d1 | 2012-06-22 15:15:59 +0000 | [diff] [blame] | 386 | fMaxCount = savedMaxCount; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /////////////////////////////////////////////////////////////////////////////// |
| 390 | |
| 391 | #if GR_DEBUG |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 392 | static int countMatches(const GrResourceEntry* head, const GrResourceEntry* target) { |
| 393 | const GrResourceEntry* entry = head; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 394 | int count = 0; |
| 395 | while (entry) { |
| 396 | if (target == entry) { |
| 397 | count += 1; |
| 398 | } |
| 399 | entry = entry->next(); |
| 400 | } |
| 401 | return count; |
| 402 | } |
| 403 | |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 404 | #if GR_DEBUG |
| 405 | static bool both_zero_or_nonzero(int count, size_t bytes) { |
| 406 | return (count == 0 && bytes == 0) || (count > 0 && bytes > 0); |
| 407 | } |
| 408 | #endif |
| 409 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 410 | void GrResourceCache::validate() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 411 | GrAssert(!fHead == !fTail); |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 412 | GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes)); |
| 413 | GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 414 | GrAssert(fClientDetachedBytes <= fEntryBytes); |
| 415 | GrAssert(fClientDetachedCount <= fEntryCount); |
| 416 | GrAssert((fEntryCount - fClientDetachedCount) == fCache.count()); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 417 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 418 | fCache.validate(); |
| 419 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 420 | GrResourceEntry* entry = fHead; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 421 | int count = 0; |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 422 | int unlockCount = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 423 | size_t bytes = 0; |
| 424 | while (entry) { |
| 425 | entry->validate(); |
| 426 | GrAssert(fCache.find(entry->key())); |
| 427 | count += 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 428 | bytes += entry->resource()->sizeInBytes(); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 429 | if (!entry->isLocked()) { |
| 430 | unlockCount += 1; |
| 431 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 432 | entry = entry->fNext; |
| 433 | } |
| 434 | GrAssert(count == fEntryCount - fClientDetachedCount); |
| 435 | GrAssert(bytes == fEntryBytes - fClientDetachedBytes); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 436 | GrAssert(unlockCount == fUnlockedEntryCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 437 | |
| 438 | count = 0; |
| 439 | for (entry = fTail; entry; entry = entry->fPrev) { |
| 440 | count += 1; |
| 441 | } |
| 442 | GrAssert(count == fEntryCount - fClientDetachedCount); |
| 443 | |
| 444 | for (int i = 0; i < count; i++) { |
| 445 | int matches = countMatches(fHead, fCache.getArray()[i]); |
| 446 | GrAssert(1 == matches); |
| 447 | } |
| 448 | } |
robertphillips@google.com | 5f9f2f5 | 2012-08-22 10:57:05 +0000 | [diff] [blame^] | 449 | |
| 450 | void GrResourceCache::printStats() const { |
| 451 | SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes); |
| 452 | SkDebugf("\t\tEntry Count: current %d high %d\n", |
| 453 | fEntryCount, fHighWaterEntryCount); |
| 454 | SkDebugf("\t\tUnlocked Entry Count: current %d high %d\n", |
| 455 | fUnlockedEntryCount, fHighWaterUnlockedEntryCount); |
| 456 | SkDebugf("\t\tEntry Bytes: current %d high %d\n", |
| 457 | fEntryBytes, fHighWaterEntryBytes); |
| 458 | SkDebugf("\t\tDetached Entry Count: current %d high %d\n", |
| 459 | fClientDetachedCount, fHighWaterClientDetachedCount); |
| 460 | SkDebugf("\t\tDetached Bytes: current %d high %d\n", |
| 461 | fClientDetachedBytes, fHighWaterClientDetachedBytes); |
| 462 | } |
| 463 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 464 | #endif |