Make GrResourceCache use a priority queue of purgeable resources.

Review URL: https://codereview.chromium.org/921323002
This commit is contained in:
bsalomon 2015-02-17 11:47:40 -08:00 committed by Commit bot
parent 9e779d4951
commit 9f2d1571ed
6 changed files with 108 additions and 109 deletions

View File

@ -269,7 +269,7 @@ private:
virtual size_t onGpuMemorySize() const = 0; virtual size_t onGpuMemorySize() const = 0;
// See comments in CacheAccess. // See comments in CacheAccess and ResourcePriv.
bool setContentKey(const GrContentKey& contentKey); bool setContentKey(const GrContentKey& contentKey);
void removeContentKey(); void removeContentKey();
void notifyIsPurgeable() const; void notifyIsPurgeable() const;
@ -283,9 +283,15 @@ private:
static uint32_t CreateUniqueID(); static uint32_t CreateUniqueID();
// We're in an internal doubly linked list owned by GrResourceCache // We're in an internal doubly linked list owned by GrResourceCache. TODO: Replace this with an
// array of unpurgeable resources in the cache.
SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrGpuResource); SK_DECLARE_INTERNAL_LLIST_INTERFACE(GrGpuResource);
// An index into a heap when this resource is purgeable. This is maintained by the cache.
int fCacheArrayIndex;
// This value reflects how recently this resource was accessed in the cache. This is maintained
// by the cache.
uint32_t fTimestamp;
static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0); static const size_t kInvalidGpuMemorySize = ~static_cast<size_t>(0);
GrScratchKey fScratchKey; GrScratchKey fScratchKey;

View File

@ -92,6 +92,10 @@ public:
this->validate(); this->validate();
} }
#ifdef SK_DEBUG
T at(int i) const { return fArray[i]; }
#endif
private: private:
static int LeftOf(int x) { SkASSERT(x >= 0); return 2 * x + 1; } static int LeftOf(int x) { SkASSERT(x >= 0); return 2 * x + 1; }
static int ParentOf(int x) { SkASSERT(x > 0); return (x - 1) >> 1; } static int ParentOf(int x) { SkASSERT(x > 0); return (x - 1) >> 1; }

View File

@ -23,6 +23,7 @@ GrGpuResource::GrGpuResource(GrGpu* gpu, LifeCycle lifeCycle)
, fGpuMemorySize(kInvalidGpuMemorySize) , fGpuMemorySize(kInvalidGpuMemorySize)
, fLifeCycle(lifeCycle) , fLifeCycle(lifeCycle)
, fUniqueID(CreateUniqueID()) { , fUniqueID(CreateUniqueID()) {
SkDEBUGCODE(fCacheArrayIndex = -1);
} }
void GrGpuResource::registerWithCache() { void GrGpuResource::registerWithCache() {

View File

@ -55,6 +55,11 @@ private:
} }
} }
uint32_t timestamp() const { return fResource->fTimestamp; }
void setTimestamp(uint32_t ts) { fResource->fTimestamp = ts; }
int* accessCacheIndex() const { return &fResource->fCacheArrayIndex; }
CacheAccess(GrGpuResource* resource) : fResource(resource) {} CacheAccess(GrGpuResource* resource) : fResource(resource) {}
CacheAccess(const CacheAccess& that) : fResource(that.fResource) {} CacheAccess(const CacheAccess& that) : fResource(that.fResource) {}
CacheAccess& operator=(const CacheAccess&); // unimpl CacheAccess& operator=(const CacheAccess&); // unimpl

View File

@ -58,7 +58,8 @@ static const int kDefaultMaxCount = 2 * (1 << 10);
static const size_t kDefaultMaxSize = 96 * (1 << 20); static const size_t kDefaultMaxSize = 96 * (1 << 20);
GrResourceCache::GrResourceCache() GrResourceCache::GrResourceCache()
: fMaxCount(kDefaultMaxCount) : fTimestamp(0)
, fMaxCount(kDefaultMaxCount)
, fMaxBytes(kDefaultMaxSize) , fMaxBytes(kDefaultMaxSize)
#if GR_CACHE_STATS #if GR_CACHE_STATS
, fHighWaterCount(0) , fHighWaterCount(0)
@ -70,8 +71,6 @@ GrResourceCache::GrResourceCache()
, fBytes(0) , fBytes(0)
, fBudgetedCount(0) , fBudgetedCount(0)
, fBudgetedBytes(0) , fBudgetedBytes(0)
, fPurging(false)
, fNewlyPurgeableResourceWhilePurging(false)
, fOverBudgetCB(NULL) , fOverBudgetCB(NULL)
, fOverBudgetData(NULL) { , fOverBudgetData(NULL) {
} }
@ -90,7 +89,6 @@ void GrResourceCache::insertResource(GrGpuResource* resource) {
SkASSERT(resource); SkASSERT(resource);
SkASSERT(!resource->wasDestroyed()); SkASSERT(!resource->wasDestroyed());
SkASSERT(!this->isInCache(resource)); SkASSERT(!this->isInCache(resource));
SkASSERT(!fPurging);
fResources.addToHead(resource); fResources.addToHead(resource);
size_t size = resource->gpuMemorySize(); size_t size = resource->gpuMemorySize();
@ -113,12 +111,19 @@ void GrResourceCache::insertResource(GrGpuResource* resource) {
fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource); fScratchMap.insert(resource->resourcePriv().getScratchKey(), resource);
} }
resource->cacheAccess().setTimestamp(fTimestamp++);
this->purgeAsNeeded(); this->purgeAsNeeded();
} }
void GrResourceCache::removeResource(GrGpuResource* resource) { void GrResourceCache::removeResource(GrGpuResource* resource) {
this->validate();
SkASSERT(this->isInCache(resource)); SkASSERT(this->isInCache(resource));
if (resource->isPurgeable()) {
fPurgeableQueue.remove(resource);
}
size_t size = resource->gpuMemorySize(); size_t size = resource->gpuMemorySize();
--fCount; --fCount;
fBytes -= size; fBytes -= size;
@ -140,7 +145,6 @@ void GrResourceCache::removeResource(GrGpuResource* resource) {
void GrResourceCache::abandonAll() { void GrResourceCache::abandonAll() {
AutoValidate av(this); AutoValidate av(this);
SkASSERT(!fPurging);
while (GrGpuResource* head = fResources.head()) { while (GrGpuResource* head = fResources.head()) {
SkASSERT(!head->wasDestroyed()); SkASSERT(!head->wasDestroyed());
head->cacheAccess().abandon(); head->cacheAccess().abandon();
@ -158,7 +162,6 @@ void GrResourceCache::abandonAll() {
void GrResourceCache::releaseAll() { void GrResourceCache::releaseAll() {
AutoValidate av(this); AutoValidate av(this);
SkASSERT(!fPurging);
while (GrGpuResource* head = fResources.head()) { while (GrGpuResource* head = fResources.head()) {
SkASSERT(!head->wasDestroyed()); SkASSERT(!head->wasDestroyed());
head->cacheAccess().release(); head->cacheAccess().release();
@ -189,15 +192,13 @@ private:
GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey, GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& scratchKey,
uint32_t flags) { uint32_t flags) {
SkASSERT(!fPurging);
SkASSERT(scratchKey.isValid()); SkASSERT(scratchKey.isValid());
GrGpuResource* resource; GrGpuResource* resource;
if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) { if (flags & (kPreferNoPendingIO_ScratchFlag | kRequireNoPendingIO_ScratchFlag)) {
resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true)); resource = fScratchMap.find(scratchKey, AvailableForScratchUse(true));
if (resource) { if (resource) {
resource->ref(); this->refAndMakeResourceMRU(resource);
this->makeResourceMRU(resource);
this->validate(); this->validate();
return resource; return resource;
} else if (flags & kRequireNoPendingIO_ScratchFlag) { } else if (flags & kRequireNoPendingIO_ScratchFlag) {
@ -208,8 +209,7 @@ GrGpuResource* GrResourceCache::findAndRefScratchResource(const GrScratchKey& sc
} }
resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false)); resource = fScratchMap.find(scratchKey, AvailableForScratchUse(false));
if (resource) { if (resource) {
resource->ref(); this->refAndMakeResourceMRU(resource);
this->makeResourceMRU(resource);
this->validate(); this->validate();
} }
return resource; return resource;
@ -228,7 +228,6 @@ void GrResourceCache::willRemoveContentKey(const GrGpuResource* resource) {
} }
bool GrResourceCache::didSetContentKey(GrGpuResource* resource) { bool GrResourceCache::didSetContentKey(GrGpuResource* resource) {
SkASSERT(!fPurging);
SkASSERT(resource); SkASSERT(resource);
SkASSERT(this->isInCache(resource)); SkASSERT(this->isInCache(resource));
SkASSERT(resource->getContentKey().isValid()); SkASSERT(resource->getContentKey().isValid());
@ -243,12 +242,16 @@ bool GrResourceCache::didSetContentKey(GrGpuResource* resource) {
return true; return true;
} }
void GrResourceCache::makeResourceMRU(GrGpuResource* resource) { void GrResourceCache::refAndMakeResourceMRU(GrGpuResource* resource) {
SkASSERT(!fPurging);
SkASSERT(resource); SkASSERT(resource);
SkASSERT(this->isInCache(resource)); SkASSERT(this->isInCache(resource));
fResources.remove(resource); if (resource->isPurgeable()) {
fResources.addToHead(resource); // It's about to become unpurgeable.
fPurgeableQueue.remove(resource);
}
resource->ref();
resource->cacheAccess().setTimestamp(fTimestamp++);
SkASSERT(!resource->isPurgeable());
} }
void GrResourceCache::notifyPurgeable(GrGpuResource* resource) { void GrResourceCache::notifyPurgeable(GrGpuResource* resource) {
@ -256,49 +259,37 @@ void GrResourceCache::notifyPurgeable(GrGpuResource* resource) {
SkASSERT(this->isInCache(resource)); SkASSERT(this->isInCache(resource));
SkASSERT(resource->isPurgeable()); SkASSERT(resource->isPurgeable());
// We can't purge if in the middle of purging because purge is iterating. Instead record SkASSERT(-1 == *resource->cacheAccess().accessCacheIndex());
// that additional resources became purgeable. fPurgeableQueue.insert(resource);
if (fPurging) {
fNewlyPurgeableResourceWhilePurging = true;
return;
}
bool release = false; if (!resource->resourcePriv().isBudgeted()) {
if (resource->cacheAccess().isWrapped()) {
release = true;
} else if (!resource->resourcePriv().isBudgeted()) {
// Check whether this resource could still be used as a scratch resource. // Check whether this resource could still be used as a scratch resource.
if (resource->resourcePriv().getScratchKey().isValid()) { if (!resource->cacheAccess().isWrapped() &&
resource->resourcePriv().getScratchKey().isValid()) {
// We won't purge an existing resource to make room for this one. // We won't purge an existing resource to make room for this one.
bool underBudget = fBudgetedCount < fMaxCount && bool underBudget = fBudgetedCount < fMaxCount &&
fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes; fBudgetedBytes + resource->gpuMemorySize() <= fMaxBytes;
if (underBudget) { if (underBudget) {
resource->resourcePriv().makeBudgeted(); resource->resourcePriv().makeBudgeted();
} else { return;
release = true; }
} }
} else { } else {
release = true; // Purge the resource immediately if we're over budget
}
} else {
// Purge the resource if we're over budget
bool overBudget = fBudgetedCount > fMaxCount || fBudgetedBytes > fMaxBytes; bool overBudget = fBudgetedCount > fMaxCount || fBudgetedBytes > fMaxBytes;
// Also purge if the resource has neither a valid scratch key nor a content key. // Also purge if the resource has neither a valid scratch key nor a content key.
bool noKey = !resource->resourcePriv().getScratchKey().isValid() && bool noKey = !resource->resourcePriv().getScratchKey().isValid() &&
!resource->getContentKey().isValid(); !resource->getContentKey().isValid();
if (overBudget || noKey) { if (!overBudget && !noKey) {
release = true; return;
} }
} }
if (release) {
SkDEBUGCODE(int beforeCount = fCount;) SkDEBUGCODE(int beforeCount = fCount;)
resource->cacheAccess().release(); resource->cacheAccess().release();
// We should at least free this resource, perhaps dependent resources as well. // We should at least free this resource, perhaps dependent resources as well.
SkASSERT(fCount < beforeCount); SkASSERT(fCount < beforeCount);
}
this->validate(); this->validate();
} }
@ -325,7 +316,6 @@ void GrResourceCache::didChangeGpuMemorySize(const GrGpuResource* resource, size
} }
void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) { void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
SkASSERT(!fPurging);
SkASSERT(resource); SkASSERT(resource);
SkASSERT(this->isInCache(resource)); SkASSERT(this->isInCache(resource));
@ -348,67 +338,38 @@ void GrResourceCache::didChangeBudgetStatus(GrGpuResource* resource) {
} }
void GrResourceCache::internalPurgeAsNeeded() { void GrResourceCache::internalPurgeAsNeeded() {
SkASSERT(!fPurging);
SkASSERT(!fNewlyPurgeableResourceWhilePurging);
SkASSERT(fBudgetedCount > fMaxCount || fBudgetedBytes > fMaxBytes); SkASSERT(fBudgetedCount > fMaxCount || fBudgetedBytes > fMaxBytes);
fPurging = true; bool stillOverbudget = true;
while (fPurgeableQueue.count()) {
bool overBudget = true; GrGpuResource* resource = fPurgeableQueue.peek();
do { SkASSERT(resource->isPurgeable());
fNewlyPurgeableResourceWhilePurging = false;
ResourceList::Iter resourceIter;
GrGpuResource* resource = resourceIter.init(fResources,
ResourceList::Iter::kTail_IterStart);
while (resource) {
GrGpuResource* prev = resourceIter.prev();
if (resource->isPurgeable()) {
resource->cacheAccess().release(); resource->cacheAccess().release();
}
resource = prev;
if (fBudgetedCount <= fMaxCount && fBudgetedBytes <= fMaxBytes) { if (fBudgetedCount <= fMaxCount && fBudgetedBytes <= fMaxBytes) {
overBudget = false; stillOverbudget = false;
resource = NULL; break;
} }
} }
if (!fNewlyPurgeableResourceWhilePurging && overBudget && fOverBudgetCB) {
// Despite the purge we're still over budget. Call our over budget callback.
(*fOverBudgetCB)(fOverBudgetData);
}
} while (overBudget && fNewlyPurgeableResourceWhilePurging);
fNewlyPurgeableResourceWhilePurging = false;
fPurging = false;
this->validate(); this->validate();
if (stillOverbudget) {
// Despite the purge we're still over budget. Call our over budget callback. If this frees
// any resources then we'll get notifyPurgeable() calls and take appropriate action.
(*fOverBudgetCB)(fOverBudgetData);
this->validate();
}
} }
void GrResourceCache::purgeAllUnlocked() { void GrResourceCache::purgeAllUnlocked() {
SkASSERT(!fPurging); // We could disable maintaining the heap property here, but it would add a lot of complexity.
SkASSERT(!fNewlyPurgeableResourceWhilePurging); // Moreover, this is rarely called.
while (fPurgeableQueue.count()) {
fPurging = true; GrGpuResource* resource = fPurgeableQueue.peek();
SkASSERT(resource->isPurgeable());
do {
fNewlyPurgeableResourceWhilePurging = false;
ResourceList::Iter resourceIter;
GrGpuResource* resource =
resourceIter.init(fResources, ResourceList::Iter::kTail_IterStart);
while (resource) {
GrGpuResource* prev = resourceIter.prev();
if (resource->isPurgeable()) {
resource->cacheAccess().release(); resource->cacheAccess().release();
} }
resource = prev;
}
if (!fNewlyPurgeableResourceWhilePurging && fCount && fOverBudgetCB) {
(*fOverBudgetCB)(fOverBudgetData);
}
} while (fNewlyPurgeableResourceWhilePurging);
fPurging = false;
this->validate(); this->validate();
} }
@ -475,8 +436,17 @@ void GrResourceCache::validate() const {
++budgetedCount; ++budgetedCount;
budgetedBytes += resource->gpuMemorySize(); budgetedBytes += resource->gpuMemorySize();
} }
if (!resource->isPurgeable()) {
SkASSERT(-1 == *resource->cacheAccess().accessCacheIndex());
}
} }
for (int i = 0; i < fPurgeableQueue.count(); ++i) {
SkASSERT(fPurgeableQueue.at(i)->isPurgeable());
}
SkASSERT(fCount - locked == fPurgeableQueue.count());
SkASSERT(fBudgetedCount <= fCount); SkASSERT(fBudgetedCount <= fCount);
SkASSERT(fBudgetedBytes <= fBudgetedBytes); SkASSERT(fBudgetedBytes <= fBudgetedBytes);
SkASSERT(bytes == fBytes); SkASSERT(bytes == fBytes);

View File

@ -10,11 +10,13 @@
#define GrResourceCache_DEFINED #define GrResourceCache_DEFINED
#include "GrGpuResource.h" #include "GrGpuResource.h"
#include "GrGpuResourceCacheAccess.h"
#include "GrGpuResourcePriv.h" #include "GrGpuResourcePriv.h"
#include "GrResourceKey.h" #include "GrResourceKey.h"
#include "SkMessageBus.h" #include "SkMessageBus.h"
#include "SkRefCnt.h" #include "SkRefCnt.h"
#include "SkTArray.h" #include "SkTArray.h"
#include "SkTDPQueue.h"
#include "SkTInternalLList.h" #include "SkTInternalLList.h"
#include "SkTMultiMap.h" #include "SkTMultiMap.h"
@ -117,8 +119,7 @@ public:
GrGpuResource* findAndRefContentResource(const GrContentKey& contentKey) { GrGpuResource* findAndRefContentResource(const GrContentKey& contentKey) {
GrGpuResource* resource = fContentHash.find(contentKey); GrGpuResource* resource = fContentHash.find(contentKey);
if (resource) { if (resource) {
resource->ref(); this->refAndMakeResourceMRU(resource);
this->makeResourceMRU(resource);
} }
return resource; return resource;
} }
@ -138,7 +139,7 @@ public:
if (invalidKeyMsgs.count()) { if (invalidKeyMsgs.count()) {
this->processInvalidContentKeys(invalidKeyMsgs); this->processInvalidContentKeys(invalidKeyMsgs);
} }
if (fPurging || (fBudgetedCount <= fMaxCount && fBudgetedBytes <= fMaxBytes)) { if (fBudgetedCount <= fMaxCount && fBudgetedBytes <= fMaxBytes) {
return; return;
} }
this->internalPurgeAsNeeded(); this->internalPurgeAsNeeded();
@ -179,7 +180,7 @@ private:
void willRemoveScratchKey(const GrGpuResource*); void willRemoveScratchKey(const GrGpuResource*);
void willRemoveContentKey(const GrGpuResource*); void willRemoveContentKey(const GrGpuResource*);
void didChangeBudgetStatus(GrGpuResource*); void didChangeBudgetStatus(GrGpuResource*);
void makeResourceMRU(GrGpuResource*); void refAndMakeResourceMRU(GrGpuResource*);
/// @} /// @}
void internalPurgeAsNeeded(); void internalPurgeAsNeeded();
@ -216,9 +217,26 @@ private:
typedef SkTInternalLList<GrGpuResource> ResourceList; typedef SkTInternalLList<GrGpuResource> ResourceList;
typedef SkMessageBus<GrContentKeyInvalidatedMessage>::Inbox InvalidContentKeyInbox; static bool CompareTimestamp(GrGpuResource* const& a, GrGpuResource* const& b) {
return a->cacheAccess().timestamp() < b->cacheAccess().timestamp();
}
static int* AccessResourceIndex(GrGpuResource* const& res) {
return res->cacheAccess().accessCacheIndex();
}
typedef SkMessageBus<GrContentKeyInvalidatedMessage>::Inbox InvalidContentKeyInbox;
typedef SkTDPQueue<GrGpuResource*, CompareTimestamp, AccessResourceIndex> PurgeableQueue;
// Whenever a resource is added to the cache or the result of a cache lookup, fTimestamp is
// assigned as the resource's timestamp and then incremented. fPurgeableQueue orders the
// purgeable resources by this value, and thus is used to purge resources in LRU order.
uint32_t fTimestamp;
PurgeableQueue fPurgeableQueue;
// TODO: Replace this with an array of nonpurgeable resources
ResourceList fResources; ResourceList fResources;
// This map holds all resources that can be used as scratch resources. // This map holds all resources that can be used as scratch resources.
ScratchMap fScratchMap; ScratchMap fScratchMap;
// This holds all resources that have content keys. // This holds all resources that have content keys.
@ -243,15 +261,10 @@ private:
int fBudgetedCount; int fBudgetedCount;
size_t fBudgetedBytes; size_t fBudgetedBytes;
// prevents recursive purging
bool fPurging;
bool fNewlyPurgeableResourceWhilePurging;
PFOverBudgetCB fOverBudgetCB; PFOverBudgetCB fOverBudgetCB;
void* fOverBudgetData; void* fOverBudgetData;
InvalidContentKeyInbox fInvalidContentKeyInbox; InvalidContentKeyInbox fInvalidContentKeyInbox;
}; };
class GrResourceCache::ResourceAccess { class GrResourceCache::ResourceAccess {