Add GrGpuResource::CacheAccess

Internal only helper class for manipulating and accessing cache keys.

BUG=skia:2889

Review URL: https://codereview.chromium.org/703303003
This commit is contained in:
bsalomon 2014-11-11 14:15:57 -08:00 committed by Commit bot
parent 3c3fe7ce48
commit 453cf40ac7
10 changed files with 141 additions and 78 deletions

View File

@ -87,6 +87,7 @@
'<(skia_src_path)/gpu/GrGeometryBuffer.h', '<(skia_src_path)/gpu/GrGeometryBuffer.h',
'<(skia_src_path)/gpu/GrGpu.cpp', '<(skia_src_path)/gpu/GrGpu.cpp',
'<(skia_src_path)/gpu/GrGpu.h', '<(skia_src_path)/gpu/GrGpu.h',
'<(skia_src_path)/gpu/GrGpuResourceCacheAccess.h',
'<(skia_src_path)/gpu/GrGpuResource.cpp', '<(skia_src_path)/gpu/GrGpuResource.cpp',
'<(skia_src_path)/gpu/GrGpuFactory.cpp', '<(skia_src_path)/gpu/GrGpuFactory.cpp',
'<(skia_src_path)/gpu/GrIndexBuffer.h', '<(skia_src_path)/gpu/GrIndexBuffer.h',

View File

@ -174,24 +174,6 @@ public:
*/ */
virtual size_t gpuMemorySize() const = 0; virtual size_t gpuMemorySize() const = 0;
// TODO(bsalomon): Move this stuff to GrGpuResourcePriv.
bool setContentKey(const GrResourceKey& contentKey);
void setCacheEntry(GrResourceCacheEntry* cacheEntry);
GrResourceCacheEntry* getCacheEntry() const { return fCacheEntry; }
bool isScratch() const;
/**
* If this resource can be used as a scratch resource this returns a valid
* scratch key. Otherwise it returns a key for which isNullScratch is true.
* The resource may currently be used as content resource rather than scratch.
* Check isScratch().
*/
const GrResourceKey& getScratchKey() const { return fScratchKey; }
/**
* If this resource is currently cached by its contents then this will return
* the content key. Otherwise, NULL is returned.
*/
const GrResourceKey* getContentKey() const;
/** /**
* Gets an id that is unique for this GrGpuResource object. It is static in that it does * Gets an id that is unique for this GrGpuResource object. It is static in that it does
* not change when the content of the GrGpuResource object changes. This will never return * not change when the content of the GrGpuResource object changes. This will never return
@ -199,6 +181,13 @@ public:
*/ */
uint32_t getUniqueID() const { return fUniqueID; } uint32_t getUniqueID() const { return fUniqueID; }
/**
* Internal-only helper class used for cache manipulations of the reosurce.
*/
class CacheAccess;
inline CacheAccess cacheAccess();
inline const CacheAccess cacheAccess() const;
protected: protected:
// This must be called by every GrGpuObject. It should be called once the object is fully // This must be called by every GrGpuObject. It should be called once the object is fully
// initialized (i.e. not in a base class constructor). // initialized (i.e. not in a base class constructor).
@ -207,8 +196,6 @@ protected:
GrGpuResource(GrGpu*, bool isWrapped); GrGpuResource(GrGpu*, bool isWrapped);
virtual ~GrGpuResource(); virtual ~GrGpuResource();
bool isInCache() const { return SkToBool(fCacheEntry); }
GrGpu* getGpu() const { return fGpu; } GrGpu* getGpu() const { return fGpu; }
// Derived classes should always call their parent class' onRelease // Derived classes should always call their parent class' onRelease
@ -233,6 +220,9 @@ protected:
void setScratchKey(const GrResourceKey& scratchKey); void setScratchKey(const GrResourceKey& scratchKey);
private: private:
// See comments in CacheAccess.
bool setContentKey(const GrResourceKey& contentKey);
void notifyIsPurgable() const; void notifyIsPurgable() const;
#ifdef SK_DEBUG #ifdef SK_DEBUG

View File

@ -15,6 +15,8 @@
#include "GrAARectRenderer.h" #include "GrAARectRenderer.h"
#include "GrBufferAllocPool.h" #include "GrBufferAllocPool.h"
#include "GrGpu.h" #include "GrGpu.h"
#include "GrGpuResource.h"
#include "GrGpuResourceCacheAccess.h"
#include "GrDistanceFieldTextContext.h" #include "GrDistanceFieldTextContext.h"
#include "GrDrawTargetCaps.h" #include "GrDrawTargetCaps.h"
#include "GrIndexBuffer.h" #include "GrIndexBuffer.h"
@ -433,7 +435,7 @@ GrTexture* GrContext::createNewScratchTexture(const GrSurfaceDesc& desc) {
if (!texture) { if (!texture) {
return NULL; return NULL;
} }
fResourceCache->addResource(texture->getScratchKey(), texture); fResourceCache->addResource(texture->cacheAccess().getScratchKey(), texture);
return texture; return texture;
} }
@ -496,7 +498,7 @@ GrTexture* GrContext::refScratchTexture(const GrSurfaceDesc& inDesc, ScratchTexM
GrTexture* texture = this->createNewScratchTexture(*desc); GrTexture* texture = this->createNewScratchTexture(*desc);
SkASSERT(NULL == texture || SkASSERT(NULL == texture ||
texture->getScratchKey() == GrTexturePriv::ComputeScratchKey(*desc)); texture->cacheAccess().getScratchKey() == GrTexturePriv::ComputeScratchKey(*desc));
return texture; return texture;
} }

View File

@ -98,12 +98,6 @@ bool GrGpuResource::setContentKey(const GrResourceKey& contentKey) {
return true; return true;
} }
void GrGpuResource::setCacheEntry(GrResourceCacheEntry* cacheEntry) {
// GrResourceCache never changes the cacheEntry once one has been added.
SkASSERT(NULL == cacheEntry || NULL == fCacheEntry);
fCacheEntry = cacheEntry;
}
void GrGpuResource::notifyIsPurgable() const { void GrGpuResource::notifyIsPurgable() const {
if (fCacheEntry && !this->wasDestroyed()) { if (fCacheEntry && !this->wasDestroyed()) {
get_resource_cache(fGpu)->notifyPurgable(this); get_resource_cache(fGpu)->notifyPurgable(this);
@ -117,18 +111,6 @@ void GrGpuResource::setScratchKey(const GrResourceKey& scratchKey) {
fScratchKey = scratchKey; fScratchKey = scratchKey;
} }
const GrResourceKey* GrGpuResource::getContentKey() const {
if (fContentKeySet) {
return &fContentKey;
}
return NULL;
}
bool GrGpuResource::isScratch() const {
SkASSERT(fScratchKey.isScratch());
return NULL == this->getContentKey() && !fScratchKey.isNullScratch();
}
uint32_t GrGpuResource::CreateUniqueID() { uint32_t GrGpuResource::CreateUniqueID() {
static int32_t gUniqueID = SK_InvalidUniqueID; static int32_t gUniqueID = SK_InvalidUniqueID;
uint32_t id; uint32_t id;

View File

@ -0,0 +1,96 @@
/*
* Copyright 2014 Google Inc.
*
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#ifndef GrGpuResourceCacheAccess_DEFINED
#define GrGpuResourceCacheAccess_DEFINED
#include "GrGpuResource.h"
/**
* This class allows code internal to Skia privileged access to manage the cache keys of a
* GrGpuResource object.
*/
class GrGpuResource::CacheAccess {
public:
/**
* Sets a content key for the resource. If the resource was previously cached as scratch it will
* be converted to a content resource. Currently this may only be called once per resource. It
* fails if there is already a resource with the same content key. TODO: make this supplant the
* resource that currently is using the content key, allow resources' content keys to change,
* and allow removal of a content key to convert a resource back to scratch.
*/
bool setContentKey(const GrResourceKey& contentKey) {
return fResource->setContentKey(contentKey);
}
/**
* Used by legacy cache to attach a cache entry. This is to be removed soon.
*/
void setCacheEntry(GrResourceCacheEntry* cacheEntry) {
// GrResourceCache never changes the cacheEntry once one has been added.
SkASSERT(NULL == cacheEntry || NULL == fResource->fCacheEntry);
fResource->fCacheEntry = cacheEntry;
}
/**
* Is the resource in the legacy cache? This is to be removed soon.
*/
bool isInCache() const { return SkToBool(fResource->fCacheEntry); }
/**
* Returns the cache entry for the legacy cache. This is to be removed soon.
*/
GrResourceCacheEntry* getCacheEntry() const { return fResource->fCacheEntry; }
/**
* Is the resource currently cached as scratch? This means it has a valid scratch key and does
* not have a content key.
*/
bool isScratch() const {
SkASSERT(fResource->fScratchKey.isScratch());
return NULL == this->getContentKey() && !fResource->fScratchKey.isNullScratch();
}
/**
* If this resource can be used as a scratch resource this returns a valid scratch key.
* Otherwise it returns a key for which isNullScratch is true. The resource may currently be
* used as content resource rather than scratch. Check isScratch().
*/
const GrResourceKey& getScratchKey() const { return fResource->fScratchKey; }
/**
* If the resource is currently cached by a content key, the key is returned, otherwise NULL.
*/
const GrResourceKey* getContentKey() const {
if (fResource->fContentKeySet) {
return &fResource->fContentKey;
}
return NULL;
}
private:
CacheAccess(GrGpuResource* resource) : fResource(resource) { }
CacheAccess(const CacheAccess& that) : fResource(that.fResource) { }
CacheAccess& operator=(const CacheAccess&); // unimpl
// No taking addresses of this type.
const CacheAccess* operator&() const;
CacheAccess* operator&();
GrGpuResource* fResource;
friend class GrGpuResource; // to construct/copy this type.
};
inline GrGpuResource::CacheAccess GrGpuResource::cacheAccess() { return CacheAccess(this); }
inline const GrGpuResource::CacheAccess GrGpuResource::cacheAccess() const {
return CacheAccess(const_cast<GrGpuResource*>(this));
}
#endif

View File

@ -8,6 +8,7 @@
#include "GrResourceCache.h" #include "GrResourceCache.h"
#include "GrGpuResource.h" #include "GrGpuResource.h"
#include "GrGpuResourceCacheAccess.h"
#include "GrTexturePriv.h" #include "GrTexturePriv.h"
DECLARE_SKMESSAGEBUS_MESSAGE(GrResourceInvalidatedMessage); DECLARE_SKMESSAGEBUS_MESSAGE(GrResourceInvalidatedMessage);
@ -15,7 +16,7 @@ DECLARE_SKMESSAGEBUS_MESSAGE(GrResourceInvalidatedMessage);
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
void GrGpuResource::didChangeGpuMemorySize() const { void GrGpuResource::didChangeGpuMemorySize() const {
if (this->isInCache()) { if (this->cacheAccess().isInCache()) {
fCacheEntry->didChangeResourceSize(); fCacheEntry->didChangeResourceSize();
} }
} }
@ -54,7 +55,7 @@ GrResourceCacheEntry::~GrResourceCacheEntry() {
void GrResourceCacheEntry::validate() const { void GrResourceCacheEntry::validate() const {
SkASSERT(fResourceCache); SkASSERT(fResourceCache);
SkASSERT(fResource); SkASSERT(fResource);
SkASSERT(fResource->getCacheEntry() == this); SkASSERT(fResource->cacheAccess().getCacheEntry() == this);
SkASSERT(fResource->gpuMemorySize() == fCachedSize); SkASSERT(fResource->gpuMemorySize() == fCachedSize);
fResource->validate(); fResource->validate();
} }
@ -150,7 +151,7 @@ void GrResourceCache::attachToHead(GrResourceCacheEntry* entry) {
void GrResourceCache::makeResourceMRU(GrGpuResource* resource) { void GrResourceCache::makeResourceMRU(GrGpuResource* resource) {
GrResourceCacheEntry* entry = resource->getCacheEntry(); GrResourceCacheEntry* entry = resource->cacheAccess().getCacheEntry();
if (entry) { if (entry) {
this->internalDetach(entry); this->internalDetach(entry);
this->attachToHead(entry); this->attachToHead(entry);
@ -160,26 +161,27 @@ void GrResourceCache::makeResourceMRU(GrGpuResource* resource) {
void GrResourceCache::notifyPurgable(const GrGpuResource* resource) { void GrResourceCache::notifyPurgable(const GrGpuResource* resource) {
// Remove scratch textures from the cache the moment they become purgeable if // Remove scratch textures from the cache the moment they become purgeable if
// scratch texture reuse is turned off. // scratch texture reuse is turned off.
SkASSERT(resource->getCacheEntry()); SkASSERT(resource->cacheAccess().getCacheEntry());
if (resource->isScratch()) { if (resource->cacheAccess().isScratch()) {
const GrResourceKey& key = resource->getScratchKey(); const GrResourceKey& key = resource->cacheAccess().getScratchKey();
if (key.getResourceType() == GrTexturePriv::ResourceType() && if (key.getResourceType() == GrTexturePriv::ResourceType() &&
!fCaps->reuseScratchTextures() && !fCaps->reuseScratchTextures() &&
!(static_cast<const GrSurface*>(resource)->desc().fFlags & kRenderTarget_GrSurfaceFlag)) { !(static_cast<const GrSurface*>(resource)->desc().fFlags & kRenderTarget_GrSurfaceFlag)) {
this->deleteResource(resource->getCacheEntry()); this->deleteResource(resource->cacheAccess().getCacheEntry());
} }
} }
} }
bool GrResourceCache::addResource(const GrResourceKey& key, GrGpuResource* resource) { bool GrResourceCache::addResource(const GrResourceKey& key, GrGpuResource* resource) {
if (NULL != resource->getCacheEntry()) { if (NULL != resource->cacheAccess().getCacheEntry()) {
return false; return false;
} }
if (key.isScratch()) { if (key.isScratch()) {
SkASSERT(resource->isScratch() && key == resource->getScratchKey()); SkASSERT(resource->cacheAccess().isScratch());
SkASSERT(key == resource->cacheAccess().getScratchKey());
} else { } else {
if (!resource->setContentKey(key)) { if (!resource->cacheAccess().setContentKey(key)) {
return false; return false;
} }
} }
@ -192,7 +194,7 @@ bool GrResourceCache::addResource(const GrResourceKey& key, GrGpuResource* resou
GrAutoResourceCacheValidate atcv(this); GrAutoResourceCacheValidate atcv(this);
GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, resource)); GrResourceCacheEntry* entry = SkNEW_ARGS(GrResourceCacheEntry, (this, resource));
resource->setCacheEntry(entry); resource->cacheAccess().setCacheEntry(entry);
this->attachToHead(entry); this->attachToHead(entry);
this->purgeAsNeeded(); this->purgeAsNeeded();
@ -370,7 +372,7 @@ void GrResourceCache::printStats() {
if (!entry->fResource->isPurgable()) { if (!entry->fResource->isPurgable()) {
++locked; ++locked;
} }
if (entry->fResource->isScratch()) { if (entry->fResource->cacheAccess().isScratch()) {
++scratch; ++scratch;
} }
} }

View File

@ -39,20 +39,20 @@ void GrResourceCache2::insertResource(GrGpuResource* resource) {
SkASSERT(!this->isInCache(resource)); SkASSERT(!this->isInCache(resource));
fResources.addToHead(resource); fResources.addToHead(resource);
++fCount; ++fCount;
if (!resource->getScratchKey().isNullScratch()) { if (!resource->cacheAccess().getScratchKey().isNullScratch()) {
// TODO(bsalomon): Make this assertion possible. // TODO(bsalomon): Make this assertion possible.
// SkASSERT(!resource->isWrapped()); // SkASSERT(!resource->isWrapped());
fScratchMap.insert(resource->getScratchKey(), resource); fScratchMap.insert(resource->cacheAccess().getScratchKey(), resource);
} }
} }
void GrResourceCache2::removeResource(GrGpuResource* resource) { void GrResourceCache2::removeResource(GrGpuResource* resource) {
SkASSERT(this->isInCache(resource)); SkASSERT(this->isInCache(resource));
fResources.remove(resource); fResources.remove(resource);
if (!resource->getScratchKey().isNullScratch()) { if (!resource->cacheAccess().getScratchKey().isNullScratch()) {
fScratchMap.remove(resource->getScratchKey(), resource); fScratchMap.remove(resource->cacheAccess().getScratchKey(), resource);
} }
if (const GrResourceKey* contentKey = resource->getContentKey()) { if (const GrResourceKey* contentKey = resource->cacheAccess().getContentKey()) {
fContentHash.remove(*contentKey); fContentHash.remove(*contentKey);
} }
--fCount; --fCount;
@ -86,7 +86,7 @@ public:
AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { } AvailableForScratchUse(bool rejectPendingIO) : fRejectPendingIO(rejectPendingIO) { }
bool operator()(const GrGpuResource* resource) const { bool operator()(const GrGpuResource* resource) const {
if (!resource->reffedOnlyByCache() || !resource->isScratch()) { if (!resource->reffedOnlyByCache() || !resource->cacheAccess().isScratch()) {
return false; return false;
} }
@ -114,23 +114,12 @@ GrGpuResource* GrResourceCache2::findAndRefScratchResource(const GrResourceKey&
return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(false))); return SkSafeRef(fScratchMap.find(scratchKey, AvailableForScratchUse(false)));
} }
#if 0
void GrResourceCache2::willRemoveContentKey(const GrGpuResource* resource) {
SkASSERT(resource);
SkASSERT(resource->getContentKey());
SkDEBUGCODE(GrGpuResource* res = fContentHash.find(*resource->getContentKey()));
SkASSERT(res == resource);
fContentHash.remove(*resource->getContentKey());
}
#endif
bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) { bool GrResourceCache2::didSetContentKey(GrGpuResource* resource) {
SkASSERT(resource); SkASSERT(resource);
SkASSERT(resource->getContentKey()); SkASSERT(resource->cacheAccess().getContentKey());
SkASSERT(!resource->getContentKey()->isScratch()); SkASSERT(!resource->cacheAccess().getContentKey()->isScratch());
GrGpuResource* res = fContentHash.find(*resource->getContentKey()); GrGpuResource* res = fContentHash.find(*resource->cacheAccess().getContentKey());
if (NULL != res) { if (NULL != res) {
return false; return false;
} }

View File

@ -10,6 +10,7 @@
#define GrResourceCache2_DEFINED #define GrResourceCache2_DEFINED
#include "GrGpuResource.h" #include "GrGpuResource.h"
#include "GrGpuResourceCacheAccess.h"
#include "GrResourceKey.h" #include "GrResourceKey.h"
#include "SkRefCnt.h" #include "SkRefCnt.h"
#include "SkTInternalLList.h" #include "SkTInternalLList.h"
@ -74,7 +75,7 @@ private:
struct ScratchMapTraits { struct ScratchMapTraits {
static const GrResourceKey& GetKey(const GrGpuResource& r) { static const GrResourceKey& GetKey(const GrGpuResource& r) {
return r.getScratchKey(); return r.cacheAccess().getScratchKey();
} }
static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); } static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); }
@ -83,7 +84,7 @@ private:
struct ContentHashTraits { struct ContentHashTraits {
static const GrResourceKey& GetKey(const GrGpuResource& r) { static const GrResourceKey& GetKey(const GrGpuResource& r) {
return *r.getContentKey(); return *r.cacheAccess().getContentKey();
} }
static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); } static uint32_t Hash(const GrResourceKey& key) { return key.getHash(); }

View File

@ -10,10 +10,10 @@
#include "GrContext.h" #include "GrContext.h"
#include "GrGpu.h" #include "GrGpu.h"
#include "GrResourceCache.h" #include "GrResourceCache2.h"
void GrStencilBuffer::transferToCache() { void GrStencilBuffer::transferToCache() {
SkASSERT(NULL == this->getCacheEntry()); SkASSERT(!this->cacheAccess().isInCache());
this->getGpu()->getContext()->addStencilBuffer(this); this->getGpu()->getContext()->addStencilBuffer(this);
} }

View File

@ -89,7 +89,7 @@ public:
if (fToDelete) { if (fToDelete) {
// Breaks our little 2-element cycle below. // Breaks our little 2-element cycle below.
fToDelete->setDeleteWhenDestroyed(NULL, NULL); fToDelete->setDeleteWhenDestroyed(NULL, NULL);
fCache->deleteResource(fToDelete->getCacheEntry()); fCache->deleteResource(fToDelete->cacheAccess().getCacheEntry());
} }
this->release(); this->release();
} }
@ -345,7 +345,7 @@ static void test_cache_delete_on_destruction(skiatest::Reporter* reporter) {
a->unref(); a->unref();
b->unref(); b->unref();
cache->deleteResource(a->getCacheEntry()); cache->deleteResource(a->cacheAccess().getCacheEntry());
REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive()); REPORTER_ASSERT(reporter, 0 == TestResource::NumAlive());
} }
} }