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 | fb30951 | 2011-11-30 14:13:48 +0000 | [diff] [blame] | 175 | bool GrResourceCache::hasKey(const GrResourceKey& key) const { |
| 176 | return NULL != fCache.find(key); |
| 177 | } |
| 178 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 179 | GrResourceEntry* GrResourceCache::createAndLock(const GrResourceKey& key, |
| 180 | GrResource* resource) { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 181 | // we don't expect to create new resources during a purge. In theory |
| 182 | // this could cause purgeAsNeeded() into an infinite loop (e.g. |
| 183 | // each resource destroyed creates and locks 2 resources and |
| 184 | // unlocks 1 thereby causing a new purge). |
| 185 | GrAssert(!fPurging); |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 186 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 187 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 188 | GrResourceEntry* entry = new GrResourceEntry(key, resource); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 189 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 190 | // mark the entry as "busy" so it doesn't get purged |
| 191 | // do this before attach for locked count tracking |
| 192 | entry->lock(); |
| 193 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 194 | this->attachToHead(entry, false); |
| 195 | fCache.insert(key, entry); |
| 196 | |
| 197 | #if GR_DUMP_TEXTURE_UPLOAD |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 198 | GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n", |
| 199 | entry, fEntryCount, resource->sizeInBytes(), fEntryBytes); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 200 | #endif |
| 201 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 202 | this->purgeAsNeeded(); |
| 203 | return entry; |
| 204 | } |
| 205 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 206 | void GrResourceCache::detach(GrResourceEntry* entry) { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 207 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 208 | internalDetach(entry, true); |
| 209 | fCache.remove(entry->fKey, entry); |
| 210 | } |
| 211 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 212 | void GrResourceCache::reattachAndUnlock(GrResourceEntry* entry) { |
bsalomon@google.com | 6087975 | 2011-09-15 20:43:53 +0000 | [diff] [blame] | 213 | GrAutoResourceCacheValidate atcv(this); |
| 214 | if (entry->resource()->isValid()) { |
| 215 | attachToHead(entry, true); |
| 216 | fCache.insert(entry->key(), entry); |
| 217 | } else { |
| 218 | // If the resource went invalid while it was detached then purge it |
| 219 | // This can happen when a 3D context was lost, |
| 220 | // the client called GrContext::contextDestroyed() to notify Gr, |
| 221 | // and then later an SkGpuDevice's destructor releases its backing |
| 222 | // texture (which was invalidated at contextDestroyed time). |
| 223 | fClientDetachedCount -= 1; |
| 224 | fEntryCount -= 1; |
| 225 | size_t size = entry->resource()->sizeInBytes(); |
| 226 | fClientDetachedBytes -= size; |
| 227 | fEntryBytes -= size; |
| 228 | } |
| 229 | this->unlock(entry); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 230 | } |
| 231 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 232 | void GrResourceCache::unlock(GrResourceEntry* entry) { |
| 233 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 234 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 235 | GrAssert(entry); |
| 236 | GrAssert(entry->isLocked()); |
| 237 | GrAssert(fCache.find(entry->key())); |
| 238 | |
| 239 | entry->unlock(); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 240 | if (!entry->isLocked()) { |
| 241 | ++fUnlockedEntryCount; |
| 242 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 243 | this->purgeAsNeeded(); |
| 244 | } |
| 245 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 246 | /** |
| 247 | * Destroying a resource may potentially trigger the unlock of additional |
| 248 | * resources which in turn will trigger a nested purge. We block the nested |
| 249 | * purge using the fPurging variable. However, the initial purge will keep |
| 250 | * looping until either all resources in the cache are unlocked or we've met |
| 251 | * the budget. There is an assertion in createAndLock to check against a |
| 252 | * resource's destructor inserting new resources into the cache. If these |
| 253 | * new resources were unlocked before purgeAsNeeded completed it could |
| 254 | * potentially make purgeAsNeeded loop infinitely. |
| 255 | */ |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 256 | void GrResourceCache::purgeAsNeeded() { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 257 | if (!fPurging) { |
| 258 | fPurging = true; |
| 259 | bool withinBudget = false; |
| 260 | do { |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 261 | GrResourceEntry* entry = fTail; |
| 262 | while (entry && fUnlockedEntryCount) { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 263 | GrAutoResourceCacheValidate atcv(this); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 264 | if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) { |
| 265 | withinBudget = true; |
| 266 | break; |
| 267 | } |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 268 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 269 | GrResourceEntry* prev = entry->fPrev; |
| 270 | if (!entry->isLocked()) { |
| 271 | // remove from our cache |
| 272 | fCache.remove(entry->fKey, entry); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 273 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 274 | // remove from our llist |
| 275 | this->internalDetach(entry, false); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 276 | |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 277 | #if GR_DUMP_TEXTURE_UPLOAD |
| 278 | GrPrintf("--- ~resource from cache %p [%d %d]\n", |
| 279 | entry->resource(), |
| 280 | entry->resource()->width(), |
| 281 | entry->resource()->height()); |
| 282 | #endif |
| 283 | delete entry; |
| 284 | } |
| 285 | entry = prev; |
| 286 | } |
| 287 | } while (!withinBudget && fUnlockedEntryCount); |
| 288 | fPurging = false; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 289 | } |
| 290 | } |
| 291 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 292 | void GrResourceCache::removeAll() { |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 293 | GrAutoResourceCacheValidate atcv(this); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 294 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 295 | GrResourceEntry* entry = fHead; |
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 | // we can have one GrResource holding a lock on another |
| 298 | // so we don't want to just do a simple loop kicking each |
| 299 | // entry out. Instead change the budget and purge. |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 300 | |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 301 | int savedMaxBytes = fMaxBytes; |
| 302 | int savedMaxCount = fMaxCount; |
| 303 | fMaxBytes = -1; |
| 304 | fMaxCount = 0; |
| 305 | this->purgeAsNeeded(); |
| 306 | |
| 307 | GrAssert(!fCache.count()); |
| 308 | GrAssert(!fUnlockedEntryCount); |
bsalomon@google.com | 6087975 | 2011-09-15 20:43:53 +0000 | [diff] [blame] | 309 | // Items may have been detached from the cache (such as the backing texture |
| 310 | // for an SkGpuDevice). The above purge would not have removed them. |
| 311 | GrAssert(fEntryCount == fClientDetachedCount); |
| 312 | GrAssert(fEntryBytes == fClientDetachedBytes); |
bsalomon@google.com | e9a9894 | 2011-08-22 17:06:16 +0000 | [diff] [blame] | 313 | GrAssert(NULL == fHead); |
| 314 | GrAssert(NULL == fTail); |
| 315 | |
| 316 | fMaxBytes = savedMaxBytes; |
| 317 | fMaxCount = savedMaxCount; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 318 | } |
| 319 | |
| 320 | /////////////////////////////////////////////////////////////////////////////// |
| 321 | |
| 322 | #if GR_DEBUG |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 323 | static int countMatches(const GrResourceEntry* head, const GrResourceEntry* target) { |
| 324 | const GrResourceEntry* entry = head; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 325 | int count = 0; |
| 326 | while (entry) { |
| 327 | if (target == entry) { |
| 328 | count += 1; |
| 329 | } |
| 330 | entry = entry->next(); |
| 331 | } |
| 332 | return count; |
| 333 | } |
| 334 | |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 335 | #if GR_DEBUG |
| 336 | static bool both_zero_or_nonzero(int count, size_t bytes) { |
| 337 | return (count == 0 && bytes == 0) || (count > 0 && bytes > 0); |
| 338 | } |
| 339 | #endif |
| 340 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 341 | void GrResourceCache::validate() const { |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 342 | GrAssert(!fHead == !fTail); |
reed@google.com | b89a643 | 2011-02-07 13:20:30 +0000 | [diff] [blame] | 343 | GrAssert(both_zero_or_nonzero(fEntryCount, fEntryBytes)); |
| 344 | GrAssert(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes)); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 345 | GrAssert(fClientDetachedBytes <= fEntryBytes); |
| 346 | GrAssert(fClientDetachedCount <= fEntryCount); |
| 347 | GrAssert((fEntryCount - fClientDetachedCount) == fCache.count()); |
reed@google.com | 01804b4 | 2011-01-18 21:50:41 +0000 | [diff] [blame] | 348 | |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 349 | fCache.validate(); |
| 350 | |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 351 | GrResourceEntry* entry = fHead; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 352 | int count = 0; |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 353 | int unlockCount = 0; |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 354 | size_t bytes = 0; |
| 355 | while (entry) { |
| 356 | entry->validate(); |
| 357 | GrAssert(fCache.find(entry->key())); |
| 358 | count += 1; |
bsalomon@google.com | 50398bf | 2011-07-26 20:45:30 +0000 | [diff] [blame] | 359 | bytes += entry->resource()->sizeInBytes(); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 360 | if (!entry->isLocked()) { |
| 361 | unlockCount += 1; |
| 362 | } |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 363 | entry = entry->fNext; |
| 364 | } |
| 365 | GrAssert(count == fEntryCount - fClientDetachedCount); |
| 366 | GrAssert(bytes == fEntryBytes - fClientDetachedBytes); |
bsalomon@google.com | a5a1da8 | 2011-08-05 14:02:41 +0000 | [diff] [blame] | 367 | GrAssert(unlockCount == fUnlockedEntryCount); |
reed@google.com | ac10a2d | 2010-12-22 21:39:39 +0000 | [diff] [blame] | 368 | |
| 369 | count = 0; |
| 370 | for (entry = fTail; entry; entry = entry->fPrev) { |
| 371 | count += 1; |
| 372 | } |
| 373 | GrAssert(count == fEntryCount - fClientDetachedCount); |
| 374 | |
| 375 | for (int i = 0; i < count; i++) { |
| 376 | int matches = countMatches(fHead, fCache.getArray()[i]); |
| 377 | GrAssert(1 == matches); |
| 378 | } |
| 379 | } |
| 380 | #endif |