3 * Copyright 2010 Google Inc.
5 * Use of this source code is governed by a BSD-style license that can be
6 * found in the LICENSE file.
11 #include "GrResourceCache.h"
12 #include "GrGpuResource.h"
14 DECLARE_SKMESSAGEBUS_MESSAGE(GrResourceInvalidatedMessage);
16 ///////////////////////////////////////////////////////////////////////////////
18 void GrGpuResource::didChangeGpuMemorySize() const {
19 if (this->isInCache()) {
20 fCacheEntry->didChangeResourceSize();
24 ///////////////////////////////////////////////////////////////////////////////
26 GrResourceKey::ResourceType GrResourceKey::GenerateResourceType() {
27 static int32_t gNextType = 0;
29 int32_t type = sk_atomic_inc(&gNextType);
30 if (type >= (1 << 8 * sizeof(ResourceType))) {
31 SkFAIL("Too many Resource Types");
34 return static_cast<ResourceType>(type);
37 ///////////////////////////////////////////////////////////////////////////////
39 GrResourceCacheEntry::GrResourceCacheEntry(GrResourceCache* resourceCache,
40 const GrResourceKey& key,
41 GrGpuResource* resource)
42 : fResourceCache(resourceCache),
45 fCachedSize(resource->gpuMemorySize()),
47 // we assume ownership of the resource, and will unref it when we die
52 GrResourceCacheEntry::~GrResourceCacheEntry() {
53 fResource->setCacheEntry(NULL);
58 void GrResourceCacheEntry::validate() const {
59 SkASSERT(fResourceCache);
61 SkASSERT(fResource->getCacheEntry() == this);
62 SkASSERT(fResource->gpuMemorySize() == fCachedSize);
63 fResource->validate();
67 void GrResourceCacheEntry::didChangeResourceSize() {
68 size_t oldSize = fCachedSize;
69 fCachedSize = fResource->gpuMemorySize();
70 if (fCachedSize > oldSize) {
71 fResourceCache->didIncreaseResourceSize(this, fCachedSize - oldSize);
72 } else if (fCachedSize < oldSize) {
73 fResourceCache->didDecreaseResourceSize(this, oldSize - fCachedSize);
77 ///////////////////////////////////////////////////////////////////////////////
79 GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) :
83 fHighWaterEntryCount = 0;
84 fHighWaterEntryBytes = 0;
85 fHighWaterClientDetachedCount = 0;
86 fHighWaterClientDetachedBytes = 0;
91 fClientDetachedCount = 0;
92 fClientDetachedBytes = 0;
97 fOverbudgetData = NULL;
100 GrResourceCache::~GrResourceCache() {
101 GrAutoResourceCacheValidate atcv(this);
103 EntryList::Iter iter;
105 // Unlike the removeAll, here we really remove everything, including locked resources.
106 while (GrResourceCacheEntry* entry = fList.head()) {
107 GrAutoResourceCacheValidate atcv(this);
109 // remove from our cache
110 fCache.remove(entry->fKey, entry);
112 // remove from our llist
113 this->internalDetach(entry);
119 void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{
120 if (NULL != maxResources) {
121 *maxResources = fMaxCount;
123 if (NULL != maxResourceBytes) {
124 *maxResourceBytes = fMaxBytes;
128 void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
129 bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
131 fMaxCount = maxResources;
132 fMaxBytes = maxResourceBytes;
135 this->purgeAsNeeded();
139 void GrResourceCache::internalDetach(GrResourceCacheEntry* entry,
140 BudgetBehaviors behavior) {
144 if (kIgnore_BudgetBehavior == behavior) {
145 fClientDetachedCount += 1;
146 fClientDetachedBytes += entry->fCachedSize;
149 if (fHighWaterClientDetachedCount < fClientDetachedCount) {
150 fHighWaterClientDetachedCount = fClientDetachedCount;
152 if (fHighWaterClientDetachedBytes < fClientDetachedBytes) {
153 fHighWaterClientDetachedBytes = fClientDetachedBytes;
158 SkASSERT(kAccountFor_BudgetBehavior == behavior);
161 fEntryBytes -= entry->fCachedSize;
165 void GrResourceCache::attachToHead(GrResourceCacheEntry* entry,
166 BudgetBehaviors behavior) {
167 fList.addToHead(entry);
170 if (kIgnore_BudgetBehavior == behavior) {
171 fClientDetachedCount -= 1;
172 fClientDetachedBytes -= entry->fCachedSize;
174 SkASSERT(kAccountFor_BudgetBehavior == behavior);
177 fEntryBytes += entry->fCachedSize;
180 if (fHighWaterEntryCount < fEntryCount) {
181 fHighWaterEntryCount = fEntryCount;
183 if (fHighWaterEntryBytes < fEntryBytes) {
184 fHighWaterEntryBytes = fEntryBytes;
190 // This functor just searches for an entry with only a single ref (from
191 // the texture cache itself). Presumably in this situation no one else
192 // is relying on the texture.
193 class GrTFindUnreffedFunctor {
195 bool operator()(const GrResourceCacheEntry* entry) const {
196 return entry->resource()->unique();
200 GrGpuResource* GrResourceCache::find(const GrResourceKey& key, uint32_t ownershipFlags) {
201 GrAutoResourceCacheValidate atcv(this);
203 GrResourceCacheEntry* entry = NULL;
205 if (ownershipFlags & kNoOtherOwners_OwnershipFlag) {
206 GrTFindUnreffedFunctor functor;
208 entry = fCache.find<GrTFindUnreffedFunctor>(key, functor);
210 entry = fCache.find(key);
217 if (ownershipFlags & kHide_OwnershipFlag) {
218 this->makeExclusive(entry);
220 // Make this resource MRU
221 this->internalDetach(entry);
222 this->attachToHead(entry);
225 return entry->fResource;
228 void GrResourceCache::addResource(const GrResourceKey& key,
229 GrGpuResource* resource,
230 uint32_t ownershipFlags) {
231 SkASSERT(NULL == resource->getCacheEntry());
232 // we don't expect to create new resources during a purge. In theory
233 // this could cause purgeAsNeeded() into an infinite loop (e.g.
234 // each resource destroyed creates and locks 2 resources and
235 // unlocks 1 thereby causing a new purge).
237 GrAutoResourceCacheValidate atcv(this);
239 GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, key, resource));
240 resource->setCacheEntry(entry);
242 this->attachToHead(entry);
243 fCache.insert(key, entry);
245 if (ownershipFlags & kHide_OwnershipFlag) {
246 this->makeExclusive(entry);
251 void GrResourceCache::makeExclusive(GrResourceCacheEntry* entry) {
252 GrAutoResourceCacheValidate atcv(this);
254 SkASSERT(!entry->fIsExclusive);
255 entry->fIsExclusive = true;
257 // When scratch textures are detached (to hide them from future finds) they
258 // still count against the resource budget
259 this->internalDetach(entry, kIgnore_BudgetBehavior);
260 fCache.remove(entry->key(), entry);
263 fExclusiveList.addToHead(entry);
267 void GrResourceCache::removeInvalidResource(GrResourceCacheEntry* entry) {
268 // If the resource went invalid while it was detached then purge it
269 // This can happen when a 3D context was lost,
270 // the client called GrContext::contextDestroyed() to notify Gr,
271 // and then later an SkGpuDevice's destructor releases its backing
272 // texture (which was invalidated at contextDestroyed time).
273 // TODO: Safely delete the GrResourceCacheEntry as well.
274 fClientDetachedCount -= 1;
276 fClientDetachedBytes -= entry->fCachedSize;
277 fEntryBytes -= entry->fCachedSize;
278 entry->fCachedSize = 0;
281 void GrResourceCache::makeNonExclusive(GrResourceCacheEntry* entry) {
282 GrAutoResourceCacheValidate atcv(this);
285 fExclusiveList.remove(entry);
288 if (!entry->resource()->wasDestroyed()) {
289 // Since scratch textures still count against the cache budget even
290 // when they have been removed from the cache, re-adding them doesn't
291 // alter the budget information.
292 attachToHead(entry, kIgnore_BudgetBehavior);
293 fCache.insert(entry->key(), entry);
295 SkASSERT(entry->fIsExclusive);
296 entry->fIsExclusive = false;
298 this->removeInvalidResource(entry);
302 void GrResourceCache::didIncreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountInc) {
303 fEntryBytes += amountInc;
304 if (entry->fIsExclusive) {
305 fClientDetachedBytes += amountInc;
307 this->purgeAsNeeded();
310 void GrResourceCache::didDecreaseResourceSize(const GrResourceCacheEntry* entry, size_t amountDec) {
311 fEntryBytes -= amountDec;
312 if (entry->fIsExclusive) {
313 fClientDetachedBytes -= amountDec;
321 * Destroying a resource may potentially trigger the unlock of additional
322 * resources which in turn will trigger a nested purge. We block the nested
323 * purge using the fPurging variable. However, the initial purge will keep
324 * looping until either all resources in the cache are unlocked or we've met
325 * the budget. There is an assertion in createAndLock to check against a
326 * resource's destructor inserting new resources into the cache. If these
327 * new resources were unlocked before purgeAsNeeded completed it could
328 * potentially make purgeAsNeeded loop infinitely.
330 * extraCount and extraBytes are added to the current resource totals to account
331 * for incoming resources (e.g., GrContext is about to add 10MB split between
334 void GrResourceCache::purgeAsNeeded(int extraCount, size_t extraBytes) {
341 this->purgeInvalidated();
343 this->internalPurge(extraCount, extraBytes);
344 if (((fEntryCount+extraCount) > fMaxCount ||
345 (fEntryBytes+extraBytes) > fMaxBytes) &&
346 NULL != fOverbudgetCB) {
347 // Despite the purge we're still over budget. See if Ganesh can
348 // release some resources and purge again.
349 if ((*fOverbudgetCB)(fOverbudgetData)) {
350 this->internalPurge(extraCount, extraBytes);
357 void GrResourceCache::purgeInvalidated() {
358 SkTDArray<GrResourceInvalidatedMessage> invalidated;
359 fInvalidationInbox.poll(&invalidated);
361 for (int i = 0; i < invalidated.count(); i++) {
362 // We're somewhat missing an opportunity here. We could use the
363 // default find functor that gives us back resources whether we own
364 // them exclusively or not, and when they're not exclusively owned mark
365 // them for purging later when they do become exclusively owned.
367 // This is complicated and confusing. May try this in the future. For
368 // now, these resources are just LRU'd as if we never got the message.
369 while (GrResourceCacheEntry* entry = fCache.find(invalidated[i].key, GrTFindUnreffedFunctor())) {
370 this->deleteResource(entry);
375 void GrResourceCache::deleteResource(GrResourceCacheEntry* entry) {
376 SkASSERT(entry->fResource->unique());
378 // remove from our cache
379 fCache.remove(entry->key(), entry);
381 // remove from our llist
382 this->internalDetach(entry);
386 void GrResourceCache::internalPurge(int extraCount, size_t extraBytes) {
389 bool withinBudget = false;
390 bool changed = false;
392 // The purging process is repeated several times since one pass
393 // may free up other resources
395 EntryList::Iter iter;
399 // Note: the following code relies on the fact that the
400 // doubly linked list doesn't invalidate its data/pointers
401 // outside of the specific area where a deletion occurs (e.g.,
402 // in internalDetach)
403 GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
405 while (NULL != entry) {
406 GrAutoResourceCacheValidate atcv(this);
408 if ((fEntryCount+extraCount) <= fMaxCount &&
409 (fEntryBytes+extraBytes) <= fMaxBytes) {
414 GrResourceCacheEntry* prev = iter.prev();
415 if (entry->fResource->unique()) {
417 this->deleteResource(entry);
421 } while (!withinBudget && changed);
424 void GrResourceCache::purgeAllUnlocked() {
425 GrAutoResourceCacheValidate atcv(this);
427 // we can have one GrCacheable holding a lock on another
428 // so we don't want to just do a simple loop kicking each
429 // entry out. Instead change the budget and purge.
431 size_t savedMaxBytes = fMaxBytes;
432 int savedMaxCount = fMaxCount;
433 fMaxBytes = (size_t) -1;
435 this->purgeAsNeeded();
438 SkASSERT(fExclusiveList.countEntries() == fClientDetachedCount);
439 SkASSERT(countBytes(fExclusiveList) == fClientDetachedBytes);
440 if (!fCache.count()) {
441 // Items may have been detached from the cache (such as the backing
442 // texture for an SkGpuDevice). The above purge would not have removed
444 SkASSERT(fEntryCount == fClientDetachedCount);
445 SkASSERT(fEntryBytes == fClientDetachedBytes);
446 SkASSERT(fList.isEmpty());
450 fMaxBytes = savedMaxBytes;
451 fMaxCount = savedMaxCount;
454 ///////////////////////////////////////////////////////////////////////////////
457 size_t GrResourceCache::countBytes(const EntryList& list) {
460 EntryList::Iter iter;
462 const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(list),
463 EntryList::Iter::kTail_IterStart);
465 for ( ; NULL != entry; entry = iter.prev()) {
466 bytes += entry->resource()->gpuMemorySize();
471 static bool both_zero_or_nonzero(int count, size_t bytes) {
472 return (count == 0 && bytes == 0) || (count > 0 && bytes > 0);
475 void GrResourceCache::validate() const {
477 fExclusiveList.validate();
478 SkASSERT(both_zero_or_nonzero(fEntryCount, fEntryBytes));
479 SkASSERT(both_zero_or_nonzero(fClientDetachedCount, fClientDetachedBytes));
480 SkASSERT(fClientDetachedBytes <= fEntryBytes);
481 SkASSERT(fClientDetachedCount <= fEntryCount);
482 SkASSERT((fEntryCount - fClientDetachedCount) == fCache.count());
484 EntryList::Iter iter;
486 // check that the exclusively held entries are okay
487 const GrResourceCacheEntry* entry = iter.init(const_cast<EntryList&>(fExclusiveList),
488 EntryList::Iter::kHead_IterStart);
490 for ( ; NULL != entry; entry = iter.next()) {
494 // check that the shareable entries are okay
495 entry = iter.init(const_cast<EntryList&>(fList), EntryList::Iter::kHead_IterStart);
498 for ( ; NULL != entry; entry = iter.next()) {
500 SkASSERT(fCache.find(entry->key()));
503 SkASSERT(count == fEntryCount - fClientDetachedCount);
505 size_t bytes = countBytes(fList);
506 SkASSERT(bytes == fEntryBytes - fClientDetachedBytes);
508 bytes = countBytes(fExclusiveList);
509 SkASSERT(bytes == fClientDetachedBytes);
511 SkASSERT(fList.countEntries() == fEntryCount - fClientDetachedCount);
513 SkASSERT(fExclusiveList.countEntries() == fClientDetachedCount);
519 void GrResourceCache::printStats() {
522 EntryList::Iter iter;
524 GrResourceCacheEntry* entry = iter.init(fList, EntryList::Iter::kTail_IterStart);
526 for ( ; NULL != entry; entry = iter.prev()) {
527 if (entry->fResource->getRefCnt() > 1) {
532 SkDebugf("Budget: %d items %d bytes\n", fMaxCount, fMaxBytes);
533 SkDebugf("\t\tEntry Count: current %d (%d locked) high %d\n",
534 fEntryCount, locked, fHighWaterEntryCount);
535 SkDebugf("\t\tEntry Bytes: current %d high %d\n",
536 fEntryBytes, fHighWaterEntryBytes);
537 SkDebugf("\t\tDetached Entry Count: current %d high %d\n",
538 fClientDetachedCount, fHighWaterClientDetachedCount);
539 SkDebugf("\t\tDetached Bytes: current %d high %d\n",
540 fClientDetachedBytes, fHighWaterClientDetachedBytes);
545 ///////////////////////////////////////////////////////////////////////////////