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