diff options
author | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-04-23 15:04:44 +0000 |
---|---|---|
committer | reed@google.com <reed@google.com@2bbb7eff-a529-9590-31e7-b0007b416f81> | 2012-04-23 15:04:44 +0000 |
commit | 098370137fc9c13acf1631bcc16d9ffbb0abf45f (patch) | |
tree | 2a40da6c22aceec2db9b06b97b52464f6432df1b /src/core | |
parent | 331560e461a5ea12ebf2f0fba2371d0944b3a3a3 (diff) |
move FontCacheLimit implementations into SkGlyphCache.cpp, in prep for TLS
Review URL: https://codereview.appspot.com/6110043
git-svn-id: http://skia.googlecode.com/svn/trunk@3751 2bbb7eff-a529-9590-31e7-b0007b416f81
Diffstat (limited to 'src/core')
-rw-r--r-- | src/core/SkGlyphCache.cpp | 77 | ||||
-rw-r--r-- | src/core/SkGlyphCache.h | 12 | ||||
-rw-r--r-- | src/core/SkGraphics.cpp | 39 |
3 files changed, 59 insertions, 69 deletions
diff --git a/src/core/SkGlyphCache.cpp b/src/core/SkGlyphCache.cpp index f6fa3a00ce..b020f04411 100644 --- a/src/core/SkGlyphCache.cpp +++ b/src/core/SkGlyphCache.cpp @@ -387,6 +387,10 @@ void SkGlyphCache::invokeAndRemoveAuxProcs() { /////////////////////////////////////////////////////////////////////////////// /////////////////////////////////////////////////////////////////////////////// +#ifndef SK_DEFAULT_FONT_CACHE_LIMIT + #define SK_DEFAULT_FONT_CACHE_LIMIT (2 * 1024 * 1024) +#endif + #ifdef USE_CACHE_HASH #define HASH_BITCOUNT 6 #define HASH_COUNT (1 << HASH_BITCOUNT) @@ -413,6 +417,8 @@ public: SkGlyphCache_Globals() { fHead = NULL; fTotalMemoryUsed = 0; + fFontCacheLimit = SK_DEFAULT_FONT_CACHE_LIMIT; + #ifdef USE_CACHE_HASH sk_bzero(fHash, sizeof(fHash)); #endif @@ -430,14 +436,45 @@ public: #else void validate() const {} #endif + + size_t getFontCacheLimit() const { return fFontCacheLimit; } + size_t setFontCacheLimit(size_t limit); + +private: + size_t fFontCacheLimit; }; -static SkGlyphCache_Globals& getGlobals() { +size_t SkGlyphCache_Globals::setFontCacheLimit(size_t newLimit) { + static const size_t minLimit = 256 * 1024; + if (newLimit < minLimit) { + newLimit = minLimit; + } + + size_t prevLimit = fFontCacheLimit; + fFontCacheLimit = newLimit; + + size_t currUsed = fTotalMemoryUsed; + if (currUsed > newLimit) { + fMutex.acquire(); + SkGlyphCache::InternalFreeCache(this, currUsed - newLimit); + fMutex.release(); + } + return prevLimit; +} + +// Returns the shared globals +static SkGlyphCache_Globals& getSharedGlobals() { // we leak this, so we don't incur any shutdown cost of the destructor static SkGlyphCache_Globals* gGlobals = new SkGlyphCache_Globals; return *gGlobals; } +// Returns the TLS globals (if set), or the shared globals +static SkGlyphCache_Globals& getGlobals() { + // TODO: Check TLS for local globals... + return getSharedGlobals(); +} + void SkGlyphCache::VisitAllCaches(bool (*proc)(SkGlyphCache*, void*), void* context) { SkGlyphCache_Globals& globals = getGlobals(); @@ -555,22 +592,6 @@ void SkGlyphCache::AttachCache(SkGlyphCache* cache) { globals.validate(); } -size_t SkGlyphCache::GetCacheUsed() { - return getGlobals().fTotalMemoryUsed; -} - -bool SkGlyphCache::SetCacheUsed(size_t bytesUsed) { - size_t curr = SkGlyphCache::GetCacheUsed(); - - if (curr > bytesUsed) { - SkGlyphCache_Globals& globals = getGlobals(); - SkAutoMutexAcquire ac(globals.fMutex); - - return InternalFreeCache(&globals, curr - bytesUsed) > 0; - } - return false; -} - /////////////////////////////////////////////////////////////////////////////// SkGlyphCache* SkGlyphCache::FindTail(SkGlyphCache* cache) { @@ -644,8 +665,8 @@ size_t SkGlyphCache::InternalFreeCache(SkGlyphCache_Globals* globals, } /////////////////////////////////////////////////////////////////////////////// -#ifdef SK_DEBUG +#ifdef SK_DEBUG void SkGlyphCache::validate() const { int count = fGlyphArray.count(); for (int i = 0; i < count; i++) { @@ -657,5 +678,23 @@ void SkGlyphCache::validate() const { } } } - #endif + +/////////////////////////////////////////////////////////////////////////////// +/////////////////////////////////////////////////////////////////////////////// + +#include "SkTypefaceCache.h" + +size_t SkGraphics::GetFontCacheLimit() { + return getSharedGlobals().getFontCacheLimit(); +} + +size_t SkGraphics::SetFontCacheLimit(size_t bytes) { + return getSharedGlobals().setFontCacheLimit(bytes); +} + +void SkGraphics::PurgeFontCache() { + getSharedGlobals().setFontCacheLimit(0); + SkTypefaceCache::PurgeAll(); +} + diff --git a/src/core/SkGlyphCache.h b/src/core/SkGlyphCache.h index e1ab63530c..dfd2df8a96 100644 --- a/src/core/SkGlyphCache.h +++ b/src/core/SkGlyphCache.h @@ -156,18 +156,6 @@ public: return VisitCache(desc, DetachProc, NULL); } - /** Return the approximate number of bytes used by the font cache - */ - static size_t GetCacheUsed(); - - /** This can be called to purge old font data, in an attempt to free - enough bytes such that the font cache is not using more than the - specified number of bytes. It is thread-safe, and may be called at - any time. - Return true if some amount of the cache was purged. - */ - static bool SetCacheUsed(size_t bytesUsed); - #ifdef SK_DEBUG void validate() const; #else diff --git a/src/core/SkGraphics.cpp b/src/core/SkGraphics.cpp index db1c9be311..5334c85016 100644 --- a/src/core/SkGraphics.cpp +++ b/src/core/SkGraphics.cpp @@ -120,47 +120,10 @@ void SkGraphics::Init() { #endif } -/////////////////////////////////////////////////////////////////////////////// - -#include "SkGlyphCache.h" -#include "SkTypefaceCache.h" - void SkGraphics::Term() { PurgeFontCache(); } -#ifndef SK_DEFAULT_FONT_CACHE_LIMIT - #define SK_DEFAULT_FONT_CACHE_LIMIT (2 * 1024 * 1024) -#endif - -#define SK_MIN_FONT_CACHE_LIMIT (256 * 1024) - -static size_t gFontCacheLimit = SK_DEFAULT_FONT_CACHE_LIMIT; - -size_t SkGraphics::GetFontCacheLimit() { - return gFontCacheLimit; -} - -size_t SkGraphics::SetFontCacheLimit(size_t bytes) { - size_t prev = gFontCacheLimit; - - if (bytes < SK_MIN_FONT_CACHE_LIMIT) { - bytes = SK_MIN_FONT_CACHE_LIMIT; - } - gFontCacheLimit = bytes; - - // trigger a purge if the new size is smaller that our currently used amount - if (bytes < SkGlyphCache::GetCacheUsed()) { - SkGlyphCache::SetCacheUsed(bytes); - } - return prev; -} - -void SkGraphics::PurgeFontCache() { - SkGlyphCache::SetCacheUsed(0); - SkTypefaceCache::PurgeAll(); -} - /////////////////////////////////////////////////////////////////////////////// static const char kFontCacheLimitStr[] = "font-cache-limit"; @@ -171,7 +134,7 @@ static const struct { size_t fLen; size_t (*fFunc)(size_t); } gFlags[] = { - {kFontCacheLimitStr, kFontCacheLimitLen, SkGraphics::SetFontCacheLimit} + { kFontCacheLimitStr, kFontCacheLimitLen, SkGraphics::SetFontCacheLimit } }; /* flags are of the form param; or param=value; */ |