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) { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 209 | attachToHead(entry, true); |
| 210 | fCache.insert(entry->key(), entry); |
| 211 | unlock(entry); |
| 212 | } |
| 213 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 214 | void GrResourceCache::unlock(GrResourceEntry* entry) { |
| 215 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 216 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 217 | GrAssert(entry); |
| 218 | GrAssert(entry->isLocked()); |
| 219 | GrAssert(fCache.find(entry->key())); |
| 220 | |
| 221 | entry->unlock(); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 222 | if (!entry->isLocked()) { |
| 223 | ++fUnlockedEntryCount; |
| 224 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 225 | this->purgeAsNeeded(); |
| 226 | } |
| 227 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 228 | /** |
| 229 | * Destroying a resource may potentially trigger the unlock of additional |
| 230 | * resources which in turn will trigger a nested purge. We block the nested |
| 231 | * purge using the fPurging variable. However, the initial purge will keep |
| 232 | * looping until either all resources in the cache are unlocked or we've met |
| 233 | * the budget. There is an assertion in createAndLock to check against a |
| 234 | * resource's destructor inserting new resources into the cache. If these |
| 235 | * new resources were unlocked before purgeAsNeeded completed it could |
| 236 | * potentially make purgeAsNeeded loop infinitely. |
| 237 | */ |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 238 | void GrResourceCache::purgeAsNeeded() { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 239 | if (!fPurging) { |
| 240 | fPurging = true; |
| 241 | bool withinBudget = false; |
| 242 | do { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 243 | GrResourceEntry* entry = fTail; |
| 244 | while (entry && fUnlockedEntryCount) { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame^] | 245 | GrAutoResourceCacheValidate atcv(this); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 246 | if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) { |
| 247 | withinBudget = true; |
| 248 | break; |
| 249 | } |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 250 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 251 | GrResourceEntry* prev = entry->fPrev; |
| 252 | if (!entry->isLocked()) { |
| 253 | // remove from our cache |
| 254 | fCache.remove(entry->fKey, entry); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 255 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 256 | // remove from our llist |
| 257 | this->internalDetach(entry, false); |
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 | #if GR_DUMP_TEXTURE_UPLOAD |
| 260 | GrPrintf("--- ~resource from cache %p [%d %d]\n", |
| 261 | entry->resource(), |
| 262 | entry->resource()->width(), |
| 263 | entry->resource()->height()); |
| 264 | #endif |
| 265 | delete entry; |
| 266 | } |
| 267 | entry = prev; |
| 268 | } |
| 269 | } while (!withinBudget && fUnlockedEntryCount); |
| 270 | fPurging = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 271 | } |
| 272 | } |
| 273 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 274 | void GrResourceCache::removeAll() { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame^] | 275 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 276 | GrAssert(!fClientDetachedCount); |
| 277 | GrAssert(!fClientDetachedBytes); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 278 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 279 | GrResourceEntry* entry = fHead; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 280 | |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame^] | 281 | // we can have one GrResource holding a lock on another |
| 282 | // so we don't want to just do a simple loop kicking each |
| 283 | // entry out. Instead change the budget and purge. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 284 | |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame^] | 285 | int savedMaxBytes = fMaxBytes; |
| 286 | int savedMaxCount = fMaxCount; |
| 287 | fMaxBytes = -1; |
| 288 | fMaxCount = 0; |
| 289 | this->purgeAsNeeded(); |
| 290 | |
| 291 | GrAssert(!fCache.count()); |
| 292 | GrAssert(!fUnlockedEntryCount); |
| 293 | GrAssert(!fEntryCount); |
| 294 | GrAssert(!fEntryBytes); |
| 295 | GrAssert(NULL == fHead); |
| 296 | GrAssert(NULL == fTail); |
| 297 | |
| 298 | fMaxBytes = savedMaxBytes; |
| 299 | fMaxCount = savedMaxCount; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 300 | } |
| 301 | |
| 302 | /////////////////////////////////////////////////////////////////////////////// |
| 303 | |
| 304 | #if GR_DEBUG |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 305 | static int countMatches(const GrResourceEntry* head, const GrResourceEntry* target) { |
| 306 | const GrResourceEntry* entry = head; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 307 | int count = 0; |
| 308 | while (entry) { |
| 309 | if (target == entry) { |
| 310 | count += 1; |
| 311 | } |
| 312 | entry = entry->next(); |
| 313 | } |
| 314 | return count; |
| 315 | } |
| 316 | |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 317 | #if GR_DEBUG |
| 318 | static bool both_zero_or_nonzero(int count, size_t bytes) { |
| 319 | return (count == 0 && bytes == 0) || (count > 0 && bytes > 0); |
| 320 | } |
| 321 | #endif |
| 322 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 323 | void GrResourceCache::validate() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 324 | GrAssert(!fHead == !fTail); |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 325 | GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes)); |
| 326 | GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 327 | GrAssert(fClientDetachedBytes <= fEntryBytes); |
| 328 | GrAssert(fClientDetachedCount <= fEntryCount); |
| 329 | GrAssert((fEntryCount - fClientDetachedCount) == fCache.count()); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 330 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 331 | fCache.validate(); |
| 332 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 333 | GrResourceEntry* entry = fHead; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 334 | int count = 0; |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 335 | int unlockCount = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 336 | size_t bytes = 0; |
| 337 | while (entry) { |
| 338 | entry->validate(); |
| 339 | GrAssert(fCache.find(entry->key())); |
| 340 | count += 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 341 | bytes += entry->resource()->sizeInBytes(); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 342 | if (!entry->isLocked()) { |
| 343 | unlockCount += 1; |
| 344 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 345 | entry = entry->fNext; |
| 346 | } |
| 347 | GrAssert(count == fEntryCount - fClientDetachedCount); |
| 348 | GrAssert(bytes == fEntryBytes - fClientDetachedBytes); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 349 | GrAssert(unlockCount == fUnlockedEntryCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 350 | |
| 351 | count = 0; |
| 352 | for (entry = fTail; entry; entry = entry->fPrev) { |
| 353 | count += 1; |
| 354 | } |
| 355 | GrAssert(count == fEntryCount - fClientDetachedCount); |
| 356 | |
| 357 | for (int i = 0; i < count; i++) { |
| 358 | int matches = countMatches(fHead, fCache.getArray()[i]); |
| 359 | GrAssert(1 == matches); |
| 360 | } |
| 361 | } |
| 362 | #endif |
| 363 | |
| 364 | |