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