Remove count budget from resource cache

Review URL: http://codereview.appspot.com/6312052/



git-svn-id: http://skia.googlecode.com/svn/trunk@4287 2bbb7eff-a529-9590-31e7-b0007b416f81
This commit is contained in:
bsalomon@google.com 2012-06-21 19:48:32 +00:00
parent c778cda14c
commit 8f7e1dac5c
5 changed files with 39 additions and 66 deletions

View File

@ -83,11 +83,6 @@ public:
*/ */
void freeGpuResources(); void freeGpuResources();
/**
* Returns the number of bytes of GPU memory hosted by the texture cache.
*/
size_t getGpuTextureCacheBytes() const;
/////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////
// Textures // Textures
@ -212,25 +207,28 @@ public:
int height) const; int height) const;
/** /**
* Return the current texture cache limits. * Return the current texture cache budget in bytes.
*
* @param maxTextures If non-null, returns maximum number of textures that
* can be held in the cache.
* @param maxTextureBytes If non-null, returns maximum number of bytes of
* texture memory that can be held in the cache.
*/ */
void getTextureCacheLimits(int* maxTextures, size_t* maxTextureBytes) const; size_t getTextureCacheBudget() const;
/** /**
* Specify the texture cache limits. If the current cache exceeds either * Specify the texture cache budget. If the current cache size exceeds the
* of these, it will be purged (LRU) to keep the cache within these limits. * budget it will immediately be purged to be within the budget.
* *
* @param maxTextures The maximum number of textures that can be held in
* the cache.
* @param maxTextureBytes The maximum number of bytes of texture memory * @param maxTextureBytes The maximum number of bytes of texture memory
* that can be held in the cache. * that can be held in the cache.
*/ */
void setTextureCacheLimits(int maxTextures, size_t maxTextureBytes); void setTextureCacheBudget(size_t maxTextureBytes);
// DEPRECATED, this will be deleted soon.
void setTextureCacheLimits(int ignored, size_t maxTextureBytes) {
this->setTextureCacheBudget(maxTextureBytes);
}
/**
* Returns the current number of bytes of GPU memory hosted by the texture
* cache.
*/
size_t getGpuTextureCacheBytes() const;
/** /**
* Return the max width or height of a texture supported by the current gpu * Return the max width or height of a texture supported by the current gpu

View File

@ -46,8 +46,7 @@
#define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0 #define GR_DEBUG_PARTIAL_COVERAGE_CHECK 0
#endif #endif
static const size_t MAX_TEXTURE_CACHE_COUNT = 256; static const size_t kDefaultTextureCacheBudget = 16 * 1024 * 1024;
static const size_t MAX_TEXTURE_CACHE_BYTES = 16 * 1024 * 1024;
static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15; static const size_t DRAW_BUFFER_VBPOOL_BUFFER_SIZE = 1 << 15;
static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4; static const int DRAW_BUFFER_VBPOOL_PREALLOC_BUFFERS = 4;
@ -541,13 +540,12 @@ GrTexture* GrContext::createUncachedTexture(const GrTextureDesc& descIn,
return fGpu->createTexture(descCopy, srcData, rowBytes); return fGpu->createTexture(descCopy, srcData, rowBytes);
} }
void GrContext::getTextureCacheLimits(int* maxTextures, size_t GrContext::getTextureCacheBudget() const {
size_t* maxTextureBytes) const { return fTextureCache->getBudget();
fTextureCache->getLimits(maxTextures, maxTextureBytes);
} }
void GrContext::setTextureCacheLimits(int maxTextures, size_t maxTextureBytes) { void GrContext::setTextureCacheBudget(size_t maxTextureBytes) {
fTextureCache->setLimits(maxTextures, maxTextureBytes); fTextureCache->setBudget(maxTextureBytes);
} }
int GrContext::getMaxTextureSize() const { int GrContext::getMaxTextureSize() const {
@ -1731,8 +1729,7 @@ GrContext::GrContext(GrGpu* gpu) {
fPathRendererChain = NULL; fPathRendererChain = NULL;
fSoftwarePathRenderer = NULL; fSoftwarePathRenderer = NULL;
fTextureCache = new GrResourceCache(MAX_TEXTURE_CACHE_COUNT, fTextureCache = new GrResourceCache(kDefaultTextureCacheBudget);
MAX_TEXTURE_CACHE_BYTES);
fFontCache = new GrFontCache(fGpu); fFontCache = new GrFontCache(fGpu);
fLastDrawCategory = kUnbuffered_DrawCategory; fLastDrawCategory = kUnbuffered_DrawCategory;

View File

@ -34,9 +34,8 @@ void GrResourceEntry::validate() const {
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
GrResourceCache::GrResourceCache(int maxCount, size_t maxBytes) : GrResourceCache::GrResourceCache(size_t maxBytes) {
fMaxCount(maxCount), fMaxBytes = maxBytes;
fMaxBytes(maxBytes) {
fEntryCount = 0; fEntryCount = 0;
fUnlockedEntryCount = 0; fUnlockedEntryCount = 0;
fEntryBytes = 0; fEntryBytes = 0;
@ -53,19 +52,9 @@ GrResourceCache::~GrResourceCache() {
this->removeAll(); this->removeAll();
} }
void GrResourceCache::getLimits(int* maxResources, size_t* maxResourceBytes) const{ void GrResourceCache::setBudget(size_t maxResourceBytes) {
if (maxResources) { bool smaller = maxResourceBytes < fMaxBytes;
*maxResources = fMaxCount;
}
if (maxResourceBytes) {
*maxResourceBytes = fMaxBytes;
}
}
void GrResourceCache::setLimits(int maxResources, size_t maxResourceBytes) {
bool smaller = (maxResources < fMaxCount) || (maxResourceBytes < fMaxBytes);
fMaxCount = maxResources;
fMaxBytes = maxResourceBytes; fMaxBytes = maxResourceBytes;
if (smaller) { if (smaller) {
@ -223,6 +212,7 @@ void GrResourceCache::reattachAndUnlock(GrResourceEntry* entry) {
fClientDetachedCount -= 1; fClientDetachedCount -= 1;
fEntryCount -= 1; fEntryCount -= 1;
size_t size = entry->resource()->sizeInBytes(); size_t size = entry->resource()->sizeInBytes();
GrAssert(size > 0);
fClientDetachedBytes -= size; fClientDetachedBytes -= size;
fEntryBytes -= size; fEntryBytes -= size;
} }
@ -261,7 +251,7 @@ void GrResourceCache::purgeAsNeeded() {
GrResourceEntry* entry = fTail; GrResourceEntry* entry = fTail;
while (entry && fUnlockedEntryCount) { while (entry && fUnlockedEntryCount) {
GrAutoResourceCacheValidate atcv(this); GrAutoResourceCacheValidate atcv(this);
if (fEntryCount <= fMaxCount && fEntryBytes <= fMaxBytes) { if (fEntryBytes <= fMaxBytes) {
withinBudget = true; withinBudget = true;
break; break;
} }
@ -297,9 +287,7 @@ void GrResourceCache::removeAll() {
// entry out. Instead change the budget and purge. // entry out. Instead change the budget and purge.
int savedMaxBytes = fMaxBytes; int savedMaxBytes = fMaxBytes;
int savedMaxCount = fMaxCount; fMaxBytes = 0;
fMaxBytes = (size_t) -1;
fMaxCount = 0;
this->purgeAsNeeded(); this->purgeAsNeeded();
#if GR_DEBUG #if GR_DEBUG
@ -316,7 +304,6 @@ void GrResourceCache::removeAll() {
#endif #endif
fMaxBytes = savedMaxBytes; fMaxBytes = savedMaxBytes;
fMaxCount = savedMaxCount;
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -194,29 +194,22 @@ private:
*/ */
class GrResourceCache { class GrResourceCache {
public: public:
GrResourceCache(int maxCount, size_t maxBytes); GrResourceCache(size_t maxResourceBytes);
~GrResourceCache(); ~GrResourceCache();
/** /**
* Return the current resource cache limits. * Returns the cache budget in bytes.
*
* @param maxResource If non-null, returns maximum number of resources
* that can be held in the cache.
* @param maxBytes If non-null, returns maximum number of bytes of
* gpu memory that can be held in the cache.
*/ */
void getLimits(int* maxResources, size_t* maxBytes) const; size_t getBudget() const { return fMaxBytes; }
/** /**
* Specify the resource cache limits. If the current cache exceeds either * Specify the resource cache budget in bytes of GPU memory. If the current
* of these, it will be purged (LRU) to keep the cache within these limits. * cache exceeds the budget it will be purged to be within the budget.
* *
* @param maxResources The maximum number of resources that can be held in * @param maxResourceBytes The maximum number of bytes of GPU memory that
* the cache. * can be held in the cache.
* @param maxBytes The maximum number of bytes of resource memory that
* can be held in the cache.
*/ */
void setLimits(int maxResource, size_t maxResourceBytes); void setBudget(size_t maxResourceBytes);
/** /**
* Returns the number of bytes consumed by cached resources. * Returns the number of bytes consumed by cached resources.
@ -241,8 +234,8 @@ public:
* Create a new entry, based on the specified key and resource, and return * Create a new entry, based on the specified key and resource, and return
* its "locked" entry. * its "locked" entry.
* *
* Ownership of the resource is transferred to the Entry, which will unref() * Ownership of the resource is transferred to the Entry, which will
* it when we are purged or deleted. * unref() it when we are purged or deleted.
*/ */
GrResourceEntry* createAndLock(const GrResourceKey&, GrResource*); GrResourceEntry* createAndLock(const GrResourceKey&, GrResource*);
@ -294,7 +287,6 @@ private:
GrResourceEntry* fTail; GrResourceEntry* fTail;
// our budget, used in purgeAsNeeded() // our budget, used in purgeAsNeeded()
int fMaxCount;
size_t fMaxBytes; size_t fMaxBytes;
// our current stats, related to our budget // our current stats, related to our budget

View File

@ -1152,8 +1152,7 @@ bool SkGpuDevice::shouldTileBitmap(const SkBitmap& bitmap,
// assumption here is that sw bitmap size is a good proxy for its size as // assumption here is that sw bitmap size is a good proxy for its size as
// a texture // a texture
size_t bmpSize = bitmap.getSize(); size_t bmpSize = bitmap.getSize();
size_t cacheSize; size_t cacheSize = fContext->getTextureCacheBudget();
fContext->getTextureCacheLimits(NULL, &cacheSize);
if (bmpSize < cacheSize / 2) { if (bmpSize < cacheSize / 2) {
return false; return false;
} }