Chromium Code Reviews| Index: src/core/SkGlyphCache.cpp |
| diff --git a/src/core/SkGlyphCache.cpp b/src/core/SkGlyphCache.cpp |
| index 6ae17b10741bc00204000daf1974137e3e5db967..f935dffbbbe79767bef07146dbbe5b709546ad75 100755 |
| --- a/src/core/SkGlyphCache.cpp |
| +++ b/src/core/SkGlyphCache.cpp |
| @@ -21,7 +21,7 @@ |
| namespace { |
| SkGlyphCache_Globals* create_globals() { |
| - return SkNEW_ARGS(SkGlyphCache_Globals, (SkGlyphCache_Globals::kYes_UseMutex)); |
| + return SkNEW(SkGlyphCache_Globals); |
| } |
| } // namespace |
| @@ -35,8 +35,7 @@ static SkGlyphCache_Globals& getSharedGlobals() { |
| // Returns the TLS globals (if set), or the shared globals |
| static SkGlyphCache_Globals& getGlobals() { |
|
mtklein_C
2015/07/09 16:46:22
seems like we can merge getGlobals() and getShared
herb_g
2015/07/09 18:21:15
Done.
|
| - SkGlyphCache_Globals* tls = SkGlyphCache_Globals::FindTLS(); |
| - return tls ? *tls : getSharedGlobals(); |
| + return getSharedGlobals(); |
| } |
| /////////////////////////////////////////////////////////////////////////////// |
| @@ -418,13 +417,22 @@ void SkGlyphCache::invokeAndRemoveAuxProcs() { |
| /////////////////////////////////////////////////////////////////////////////// |
| /////////////////////////////////////////////////////////////////////////////// |
| + |
| +class AutoAcquire { |
| +public: |
| + AutoAcquire(SkSpinlock& lock) : fLock(lock) { fLock.acquire(); } |
| + ~AutoAcquire() { fLock.release(); } |
| +private: |
| + SkSpinlock& fLock; |
| +}; |
| + |
| size_t SkGlyphCache_Globals::setCacheSizeLimit(size_t newLimit) { |
| static const size_t minLimit = 256 * 1024; |
| if (newLimit < minLimit) { |
| newLimit = minLimit; |
| } |
| - SkAutoMutexAcquire ac(fMutex); |
| + AutoAcquire ac(fLock); |
| size_t prevLimit = fCacheSizeLimit; |
| fCacheSizeLimit = newLimit; |
| @@ -437,7 +445,7 @@ int SkGlyphCache_Globals::setCacheCountLimit(int newCount) { |
| newCount = 0; |
| } |
| - SkAutoMutexAcquire ac(fMutex); |
| + AutoAcquire ac(fLock); |
| int prevCount = fCacheCountLimit; |
| fCacheCountLimit = newCount; |
| @@ -446,7 +454,7 @@ int SkGlyphCache_Globals::setCacheCountLimit(int newCount) { |
| } |
| void SkGlyphCache_Globals::purgeAll() { |
| - SkAutoMutexAcquire ac(fMutex); |
| + AutoAcquire ac(fLock); |
| this->internalPurge(fTotalMemoryUsed); |
| } |
| @@ -466,25 +474,25 @@ SkGlyphCache* SkGlyphCache::VisitCache(SkTypeface* typeface, |
| SkASSERT(desc); |
| SkGlyphCache_Globals& globals = getGlobals(); |
| - SkAutoMutexAcquire ac(globals.fMutex); |
| SkGlyphCache* cache; |
| - bool insideMutex = true; |
| - |
| - globals.validate(); |
| - for (cache = globals.internalGetHead(); cache != NULL; cache = cache->fNext) { |
| - if (cache->fDesc->equals(*desc)) { |
| - globals.internalDetachCache(cache); |
| - goto FOUND_IT; |
| + { |
| + AutoAcquire ac(globals.fLock); |
| + |
| + globals.validate(); |
| + |
| + for (cache = globals.internalGetHead(); cache != NULL; cache = cache->fNext) { |
| + if (cache->fDesc->equals(*desc)) { |
| + globals.internalDetachCache(cache); |
| + if (!proc(cache, context)) { |
| + globals.internalAttachCacheToHead(cache); |
|
mtklein_C
2015/07/09 16:46:22
Thanks, I find this code a lot easier to follow th
herb_g
2015/07/09 18:21:15
Acknowledged.
|
| + cache = NULL; |
| + } |
| + return cache; |
| + } |
| } |
| } |
| - /* Release the mutex now, before we create a new entry (which might have |
| - side-effects like trying to access the cache/mutex (yikes!) |
| - */ |
| - ac.release(); // release the mutex now |
| - insideMutex = false; // can't use globals anymore |
| - |
| // Check if we can create a scaler-context before creating the glyphcache. |
| // If not, we may have exhausted OS/font resources, so try purging the |
| // cache once and try again. |
| @@ -500,16 +508,10 @@ SkGlyphCache* SkGlyphCache::VisitCache(SkTypeface* typeface, |
| cache = SkNEW_ARGS(SkGlyphCache, (typeface, desc, ctx)); |
| } |
| -FOUND_IT: |
| - |
| AutoValidate av(cache); |
| if (!proc(cache, context)) { // need to reattach |
| - if (insideMutex) { |
| - globals.internalAttachCacheToHead(cache); |
| - } else { |
| - globals.attachCacheToHead(cache); |
| - } |
| + globals.attachCacheToHead(cache); |
| cache = NULL; |
| } |
| return cache; |
| @@ -524,7 +526,7 @@ void SkGlyphCache::AttachCache(SkGlyphCache* cache) { |
| void SkGlyphCache::Dump() { |
| SkGlyphCache_Globals& globals = getGlobals(); |
| - SkAutoMutexAcquire ac(globals.fMutex); |
| + AutoAcquire ac(globals.fLock); |
| SkGlyphCache* cache; |
| globals.validate(); |
| @@ -552,7 +554,7 @@ void SkGlyphCache::Dump() { |
| /////////////////////////////////////////////////////////////////////////////// |
| void SkGlyphCache_Globals::attachCacheToHead(SkGlyphCache* cache) { |
| - SkAutoMutexAcquire ac(fMutex); |
| + AutoAcquire ac(fLock); |
| this->validate(); |
| cache->validate(); |
| @@ -681,8 +683,10 @@ void SkGlyphCache_Globals::validate() const { |
| head = head->fNext; |
| } |
| - SkASSERT(fTotalMemoryUsed == computedBytes); |
| - SkASSERT(fCacheCount == computedCount); |
| + SkASSERTF(fCacheCount == computedCount, "fCacheCount: %d, computedCount: %d", fCacheCount, |
| + computedCount); |
| + SkASSERTF(fTotalMemoryUsed == computedBytes, "fTotalMemoryUsed: %d, computedBytes: %d", |
| + fTotalMemoryUsed, computedBytes); |
| } |
| #endif |
| @@ -722,14 +726,8 @@ void SkGraphics::PurgeFontCache() { |
| } |
| size_t SkGraphics::GetTLSFontCacheLimit() { |
|
mtklein_C
2015/07/09 16:46:22
TODO(mtklein): clean up TLS apis ?
herb_g
2015/07/09 18:21:15
Done.
|
| - const SkGlyphCache_Globals* tls = SkGlyphCache_Globals::FindTLS(); |
| - return tls ? tls->getCacheSizeLimit() : 0; |
| + return 0; |
| } |
| void SkGraphics::SetTLSFontCacheLimit(size_t bytes) { |
| - if (0 == bytes) { |
| - SkGlyphCache_Globals::DeleteTLS(); |
| - } else { |
| - SkGlyphCache_Globals::GetTLS().setCacheSizeLimit(bytes); |
| - } |
| } |