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