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