Chromium Code Reviews| Index: src/gpu/GrLayerCache.cpp |
| diff --git a/src/gpu/GrLayerCache.cpp b/src/gpu/GrLayerCache.cpp |
| index ff26750df764cc1b1a33bcee0fee81096a42adc4..8df6e3d59e2e1c0819d124c072da0e5cd9c7e152 100644 |
| --- a/src/gpu/GrLayerCache.cpp |
| +++ b/src/gpu/GrLayerCache.cpp |
| @@ -9,67 +9,11 @@ |
| #include "GrGpu.h" |
| #include "GrLayerCache.h" |
| -/** |
| - * PictureLayerKey just wraps a saveLayer's id in a picture for GrTHashTable. |
| - */ |
| -class GrLayerCache::PictureLayerKey { |
| -public: |
| - PictureLayerKey(uint32_t pictureID, int layerID) |
| - : fPictureID(pictureID) |
| - , fLayerID(layerID) { |
| - } |
| - |
| - uint32_t pictureID() const { return fPictureID; } |
| - int layerID() const { return fLayerID; } |
| - |
| - uint32_t getHash() const { return (fPictureID << 16) | fLayerID; } |
| - |
| - static bool LessThan(const GrCachedLayer& layer, const PictureLayerKey& key) { |
| - if (layer.pictureID() == key.pictureID()) { |
| - return layer.layerID() < key.layerID(); |
| - } |
| - |
| - return layer.pictureID() < key.pictureID(); |
| - } |
| - |
| - static bool Equals(const GrCachedLayer& layer, const PictureLayerKey& key) { |
| - return layer.pictureID() == key.pictureID() && layer.layerID() == key.layerID(); |
| - } |
| - |
| -private: |
| - uint32_t fPictureID; |
| - int fLayerID; |
| -}; |
| - |
| -/** |
| - * PictureKey just wraps a picture's unique ID for GrTHashTable. It is used to |
| - * look up a picture's GrPictureInfo (i.e., its GrPlot usage). |
| - */ |
| -class GrLayerCache::PictureKey { |
| -public: |
| - PictureKey(uint32_t pictureID) : fPictureID(pictureID) { } |
| - |
| - uint32_t pictureID() const { return fPictureID; } |
| - |
| - uint32_t getHash() const { return fPictureID; } |
| - |
| - static bool LessThan(const GrPictureInfo& pictInfo, const PictureKey& key) { |
| - return pictInfo.fPictureID < key.pictureID(); |
| - } |
| - |
| - static bool Equals(const GrPictureInfo& pictInfo, const PictureKey& key) { |
| - return pictInfo.fPictureID == key.pictureID(); |
| - |
| - } |
| - |
| -private: |
| - uint32_t fPictureID; |
| -}; |
| - |
| #ifdef SK_DEBUG |
| void GrCachedLayer::validate(const GrTexture* backingTexture) const { |
| - SkASSERT(SK_InvalidGenID != fPictureID); |
| - SkASSERT(-1 != fLayerID); |
| + SkASSERT(SK_InvalidGenID != fKey.getPictureID()); |
| + SkASSERT(-1 != fKey.getLayerID()); |
| + |
| if (NULL != fTexture) { |
| // If the layer is in some texture then it must occupy some rectangle |
| @@ -121,13 +65,22 @@ GrLayerCache::GrLayerCache(GrContext* context) |
| this->initAtlas(); |
| } |
| -GrLayerCache::~GrLayerCache() { |
| - SkTDArray<GrCachedLayer*>& layerArray = fLayerHash.getArray(); |
| - for (int i = 0; i < fLayerHash.count(); ++i) { |
| - this->unlock(layerArray[i]); |
| +class UnlockAndDeleteFunctor { |
| +public: |
| + UnlockAndDeleteFunctor(GrLayerCache* cache) : fCache(cache) {} |
| + |
| + void operator()(GrCachedLayer& layer) { |
| + fCache->unlock(&layer); |
| + SkDELETE(&layer); |
| } |
| - fLayerHash.deleteAll(); |
| +private: |
| + GrLayerCache* fCache; |
| +}; |
| + |
| +GrLayerCache::~GrLayerCache() { |
| + |
| + fLayerHash.mutateAll(UnlockAndDeleteFunctor(this)); |
|
mtklein
2014/07/17 19:36:20
Now that I'm remembering, maybe adding mutateAll m
robertphillips
2014/07/17 20:02:46
Yeah it's not a fantastic clarity win. I think it
|
| // The atlas only lets go of its texture when the atlas is deleted. |
| fAtlas.free(); |
| @@ -147,12 +100,9 @@ void GrLayerCache::initAtlas() { |
| } |
| void GrLayerCache::freeAll() { |
| - SkTDArray<GrCachedLayer*>& layerArray = fLayerHash.getArray(); |
| - for (int i = 0; i < fLayerHash.count(); ++i) { |
| - this->unlock(layerArray[i]); |
| - } |
| - fLayerHash.deleteAll(); |
| + fLayerHash.mutateAll(UnlockAndDeleteFunctor(this)); |
| + fLayerHash.rewind(); |
| // The atlas only lets go of its texture when the atlas is deleted. |
| fAtlas.free(); |
| @@ -164,21 +114,21 @@ void GrLayerCache::freeAll() { |
| } |
| GrCachedLayer* GrLayerCache::createLayer(const SkPicture* picture, int layerID) { |
| - SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
| + SkASSERT(picture->uniqueID() != SK_InvalidGenID && layerID >= 0); |
| GrCachedLayer* layer = SkNEW_ARGS(GrCachedLayer, (picture->uniqueID(), layerID)); |
| - fLayerHash.insert(PictureLayerKey(picture->uniqueID(), layerID), layer); |
| + fLayerHash.add(layer); |
| return layer; |
| } |
| GrCachedLayer* GrLayerCache::findLayer(const SkPicture* picture, int layerID) { |
| - SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
| - return fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID)); |
| + SkASSERT(picture->uniqueID() != SK_InvalidGenID && layerID >= 0); |
| + return fLayerHash.find(GrCachedLayer::Key(picture->uniqueID(), layerID)); |
| } |
| GrCachedLayer* GrLayerCache::findLayerOrCreate(const SkPicture* picture, int layerID) { |
| - SkASSERT(picture->uniqueID() != SK_InvalidGenID); |
| - GrCachedLayer* layer = fLayerHash.find(PictureLayerKey(picture->uniqueID(), layerID)); |
| + SkASSERT(picture->uniqueID() != SK_InvalidGenID && layerID >= 0); |
| + GrCachedLayer* layer = fLayerHash.find(GrCachedLayer::Key(picture->uniqueID(), layerID)); |
| if (NULL == layer) { |
| layer = this->createLayer(picture, layerID); |
| } |
| @@ -203,10 +153,10 @@ bool GrLayerCache::lock(GrCachedLayer* layer, const GrTextureDesc& desc) { |
| #if USE_ATLAS |
| { |
| - GrPictureInfo* pictInfo = fPictureHash.find(PictureKey(layer->pictureID())); |
| + GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID()); |
| if (NULL == pictInfo) { |
| pictInfo = SkNEW_ARGS(GrPictureInfo, (layer->pictureID())); |
| - fPictureHash.insert(PictureKey(layer->pictureID()), pictInfo); |
| + fPictureHash.add(pictInfo); |
| } |
| SkIPoint16 loc; |
| @@ -243,7 +193,7 @@ void GrLayerCache::unlock(GrCachedLayer* layer) { |
| if (layer->isAtlased()) { |
| SkASSERT(layer->texture() == fAtlas->getTexture()); |
| - GrPictureInfo* pictInfo = fPictureHash.find(PictureKey(layer->pictureID())); |
| + GrPictureInfo* pictInfo = fPictureHash.find(layer->pictureID()); |
| SkASSERT(NULL != pictInfo); |
| pictInfo->fPlotUsage.isEmpty(); // just to silence compiler warnings for the time being |
| @@ -256,9 +206,9 @@ void GrLayerCache::unlock(GrCachedLayer* layer) { |
| #ifdef SK_DEBUG |
| void GrLayerCache::validate() const { |
| - const SkTDArray<GrCachedLayer*>& layerArray = fLayerHash.getArray(); |
| - for (int i = 0; i < fLayerHash.count(); ++i) { |
| - layerArray[i]->validate(fAtlas->getTexture()); |
| + SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::ConstIter iter(&fLayerHash); |
| + for (; !iter.done(); ++iter) { |
| + (*iter).validate(fAtlas->getTexture()); |
| } |
| } |
| @@ -279,31 +229,25 @@ private: |
| void GrLayerCache::purge(const SkPicture* picture) { |
| SkDEBUGCODE(GrAutoValidateCache avc(this);) |
| - // This is somewhat of an abuse of GrTHashTable. We need to find all the |
| - // layers associated with 'picture' but the usual hash calls only look for |
| - // exact key matches. This code peeks into the hash table's innards to |
| - // find all the 'picture'-related layers. |
| - // TODO: use a different data structure for the layer hash? |
| + // We need to find all the layers associated with 'picture' and remove them. |
| SkTDArray<GrCachedLayer*> toBeRemoved; |
| - const SkTDArray<GrCachedLayer*>& layerArray = fLayerHash.getArray(); |
| - for (int i = 0; i < fLayerHash.count(); ++i) { |
| - if (picture->uniqueID() == layerArray[i]->pictureID()) { |
| - *toBeRemoved.append() = layerArray[i]; |
| + SkTDynamicHash<GrCachedLayer, GrCachedLayer::Key>::Iter iter(&fLayerHash); |
| + for (; !iter.done(); ++iter) { |
| + if (picture->uniqueID() == (*iter).pictureID()) { |
| + *toBeRemoved.append() = &(*iter); |
| } |
| } |
| for (int i = 0; i < toBeRemoved.count(); ++i) { |
| this->unlock(toBeRemoved[i]); |
| - |
| - PictureLayerKey key(picture->uniqueID(), toBeRemoved[i]->layerID()); |
| - fLayerHash.remove(key, toBeRemoved[i]); |
| + fLayerHash.remove(GrCachedLayer::GetKey(*toBeRemoved[i])); |
| SkDELETE(toBeRemoved[i]); |
| } |
| - GrPictureInfo* pictInfo = fPictureHash.find(PictureKey(picture->uniqueID())); |
| + GrPictureInfo* pictInfo = fPictureHash.find(picture->uniqueID()); |
| if (NULL != pictInfo) { |
| - fPictureHash.remove(PictureKey(picture->uniqueID()), pictInfo); |
| + fPictureHash.remove(picture->uniqueID()); |
| SkDELETE(pictInfo); |
| } |
| } |