GrResourceCache::~GrResourceCache() { GrAutoResourceCacheValidate atcv(this); EntryList::Iter iter; // Unlike the removeAll, here we really remove everything, including locked resources. while (GrResourceCacheEntry* entry = fList.head()) { GrAutoResourceCacheValidate atcv(this); // remove from our llist this->internalDetach(entry); delete entry; } }
void GrResourceCache::purgeAllUnlocked() { GrAutoResourceCacheValidate atcv(this); // we can have one GrResource holding a lock on another // so we don't want to just do a simple loop kicking each // entry out. Instead change the budget and purge. int savedMaxBytes = fMaxBytes; int savedMaxCount = fMaxCount; fMaxBytes = (size_t) -1; fMaxCount = 0; this->purgeAsNeeded(); #if GR_DEBUG GrAssert(fExclusiveList.countEntries() == fClientDetachedCount); GrAssert(countBytes(fExclusiveList) == fClientDetachedBytes); if (!fCache.count()) { // Items may have been detached from the cache (such as the backing // texture for an SkGpuDevice). The above purge would not have removed // them. GrAssert(fEntryCount == fClientDetachedCount); GrAssert(fEntryBytes == fClientDetachedBytes); GrAssert(fList.isEmpty()); } #endif fMaxBytes = savedMaxBytes; fMaxCount = savedMaxCount; }
void GrResourceCache::addResource(const GrResourceKey& key, GrResource* resource, uint32_t ownershipFlags) { GrAssert(NULL == resource->getCacheEntry()); // we don't expect to create new resources during a purge. In theory // this could cause purgeAsNeeded() into an infinite loop (e.g. // each resource destroyed creates and locks 2 resources and // unlocks 1 thereby causing a new purge). GrAssert(!fPurging); GrAutoResourceCacheValidate atcv(this); GrResourceEntry* entry = SkNEW_ARGS(GrResourceEntry, (key, resource)); resource->setCacheEntry(entry); this->attachToHead(entry); fCache.insert(key, entry); #if GR_DUMP_TEXTURE_UPLOAD GrPrintf("--- add resource to cache %p, count=%d bytes= %d %d\n", entry, fEntryCount, resource->sizeInBytes(), fEntryBytes); #endif if (ownershipFlags & kHide_OwnershipFlag) { this->makeExclusive(entry); } }
GrResource* GrResourceCache::find(const GrResourceKey& key, uint32_t ownershipFlags) { GrAutoResourceCacheValidate atcv(this); GrResourceEntry* entry = NULL; if (ownershipFlags & kNoOtherOwners_OwnershipFlag) { GrTFindUnreffedFunctor functor; entry = fCache.find<GrTFindUnreffedFunctor>(key, functor); } else { entry = fCache.find(key); } if (NULL == entry) { return NULL; } if (ownershipFlags & kHide_OwnershipFlag) { this->makeExclusive(entry); } else { // Make this resource MRU this->internalDetach(entry); this->attachToHead(entry); } return entry->fResource; }
bool GrResourceCache::addResource(const GrResourceKey& key, GrGpuResource* resource) { if (NULL != resource->cacheAccess().getCacheEntry()) { return false; } if (key.isScratch()) { SkASSERT(resource->cacheAccess().isScratch()); SkASSERT(key == resource->cacheAccess().getScratchKey()); } else { if (!resource->cacheAccess().setContentKey(key)) { return false; } } // we don't expect to create new resources during a purge. In theory // this could cause purgeAsNeeded() into an infinite loop (e.g. // each resource destroyed creates and locks 2 resources and // unlocks 1 thereby causing a new purge). SkASSERT(!fPurging); GrAutoResourceCacheValidate atcv(this); GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, resource)); resource->cacheAccess().setCacheEntry(entry); this->attachToHead(entry); this->purgeAsNeeded(); return true; }
void GrResourceCache::makeExclusive(GrResourceEntry* entry) { GrAutoResourceCacheValidate atcv(this); this->internalDetach(entry, true); fCache.remove(entry->key(), entry); #if GR_DEBUG fExclusiveList.addToHead(entry); #endif }
void GrResourceCache::makeExclusive(GrResourceEntry* entry) { GrAutoResourceCacheValidate atcv(this); // When scratch textures are detached (to hide them from future finds) they // still count against the resource budget this->internalDetach(entry, kIgnore_BudgetBehavior); fCache.remove(entry->key(), entry); #if GR_DEBUG fExclusiveList.addToHead(entry); #endif }
/** * Destroying a resource may potentially trigger the unlock of additional * resources which in turn will trigger a nested purge. We block the nested * purge using the fPurging variable. However, the initial purge will keep * looping until either all resources in the cache are unlocked or we've met * the budget. There is an assertion in createAndLock to check against a * resource's destructor inserting new resources into the cache. If these * new resources were unlocked before purgeAsNeeded completed it could * potentially make purgeAsNeeded loop infinitely. */ void GrResourceCache::purgeAsNeeded() { if (!fPurging) { fPurging = true; bool withinBudget = false; bool changed = false; // The purging process is repeated several times since one pass // may free up other resources do { EntryList::Iter iter; changed = false; // Note: the following code relies on the fact that the // doubly linked list doesn't invalidate its data/pointers // outside of the specific area where a deletion occurs (e.g., // in internalDetach) GrResourceEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart); while (NULL != entry) { GrAutoResourceCacheValidate atcv(this); if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) { withinBudget = true; break; } GrResourceEntry* prev = iter.prev(); if (1 == entry->fResource->getRefCnt()) { changed = true; // remove from our cache fCache.remove(entry->key(), entry); // remove from our llist this->internalDetach(entry); #if GR_DUMP_TEXTURE_UPLOAD GrPrintf("--- ~resource from cache %p [%d %d]\n", entry->resource(), entry->resource()->width(), entry->resource()->height()); #endif delete entry; } entry = prev; } } while (!withinBudget && changed); fPurging = false; } }
GrResource* GrResourceCache::find(const GrResourceKey& key) { GrAutoResourceCacheValidate atcv(this); GrResourceEntry* entry = fCache.find(key); if (NULL == entry) { return NULL; } this->internalDetach(entry, false); this->attachToHead(entry, false); return entry->fResource; }
void GrResourceCache::makeNonExclusive(GrResourceEntry* entry) { GrAutoResourceCacheValidate atcv(this); #if GR_DEBUG fExclusiveList.remove(entry); #endif if (entry->resource()->isValid()) { attachToHead(entry, true); fCache.insert(entry->key(), entry); } else { this->removeInvalidResource(entry); } }
void GrResourceCache::purgeAllUnlocked() { GrAutoResourceCacheValidate atcv(this); // we can have one GrCacheable holding a lock on another // so we don't want to just do a simple loop kicking each // entry out. Instead change the budget and purge. size_t savedMaxBytes = fMaxBytes; int savedMaxCount = fMaxCount; fMaxBytes = (size_t) -1; fMaxCount = 0; this->purgeAsNeeded(); fMaxBytes = savedMaxBytes; fMaxCount = savedMaxCount; }
void GrResourceCache::makeNonExclusive(GrResourceEntry* entry) { GrAutoResourceCacheValidate atcv(this); #if GR_DEBUG fExclusiveList.remove(entry); #endif if (entry->resource()->isValid()) { // Since scratch textures still count against the cache budget even // when they have been removed from the cache, re-adding them doesn't // alter the budget information. attachToHead(entry, kIgnore_BudgetBehavior); fCache.insert(entry->key(), entry); } else { this->removeInvalidResource(entry); } }
void GrResourceCache::addResource(const GrResourceKey& key, GrGpuResource* resource, uint32_t ownershipFlags) { SkASSERT(NULL == resource->getCacheEntry()); // we don't expect to create new resources during a purge. In theory // this could cause purgeAsNeeded() into an infinite loop (e.g. // each resource destroyed creates and locks 2 resources and // unlocks 1 thereby causing a new purge). SkASSERT(!fPurging); GrAutoResourceCacheValidate atcv(this); GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, key, resource)); resource->setCacheEntry(entry); this->attachToHead(entry); fCache.insert(key, entry); if (ownershipFlags & kHide_OwnershipFlag) { this->makeExclusive(entry); } }
void GrResourceCache::internalPurge(int extraCount, size_t extraBytes) { SkASSERT(fPurging); bool withinBudget = false; bool changed = false; // The purging process is repeated several times since one pass // may free up other resources do { EntryList::Iter iter; changed = false; // Note: the following code relies on the fact that the // doubly linked list doesn't invalidate its data/pointers // outside of the specific area where a deletion occurs (e.g., // in internalDetach) GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart); while (entry) { GrAutoResourceCacheValidate atcv(this); if ((fEntryCount+extraCount) <= fMaxCount && (fEntryBytes+extraBytes) <= fMaxBytes) { withinBudget = true; break; } GrResourceCacheEntry* prev = iter.prev(); if (entry->fResource->isPurgable()) { changed = true; this->deleteResource(entry); } entry = prev; } } while (!withinBudget && changed); }