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