diff --git a/include/config/SkUserConfig.h b/include/config/SkUserConfig.h index 270c51c400..439cbea030 100644 --- a/include/config/SkUserConfig.h +++ b/include/config/SkUserConfig.h @@ -105,6 +105,12 @@ */ //#define SkDebugf(...) MyFunction(__VA_ARGS__) +/* + * To specify a different default font cache limit, define this. If this is + * undefined, skia will use a built-in value. + */ +//#define SK_DEFAULT_FONT_CACHE_LIMIT (1024 * 1024) + /* If defined, use CoreText instead of ATSUI on OS X. */ //#define SK_USE_MAC_CORE_TEXT diff --git a/include/core/SkFontHost.h b/include/core/SkFontHost.h index 8858ecb817..c32c3d5ae0 100644 --- a/include/core/SkFontHost.h +++ b/include/core/SkFontHost.h @@ -232,14 +232,6 @@ public: /////////////////////////////////////////////////////////////////////////// - /** Return the number of bytes (approx) that should be purged from the font - cache. The input parameter is the cache's estimate of how much as been - allocated by the cache so far. - To purge (basically) everything, return the input parameter. - To purge nothing, return 0 - */ - static size_t ShouldPurgeFontCache(size_t sizeAllocatedSoFar); - /** Return SkScalerContext gamma flag, or 0, based on the paint that will be used to draw something with antialiasing. */ diff --git a/include/core/SkGraphics.h b/include/core/SkGraphics.h index 114364f274..d6af865931 100644 --- a/include/core/SkGraphics.h +++ b/include/core/SkGraphics.h @@ -17,21 +17,42 @@ public: static void Init(); static void Term(); - /** Return the (approximate) number of bytes used by the font cache. - */ - static size_t GetFontCacheUsed(); - - /** Attempt to purge the font cache until <= the specified amount remains - in the cache. Specifying 0 will attempt to purge the entire cache. - Returns true if some amount was purged from the font cache. - */ - static bool SetFontCacheUsed(size_t usageInBytes); - - /** Return the version numbers for the library. If the parameter is not - null, it is set to the version number. + /** + * Return the version numbers for the library. If the parameter is not + * null, it is set to the version number. */ static void GetVersion(int32_t* major, int32_t* minor, int32_t* patch); + // Font Cache routines + + /** + * Return the (approximate) number of bytes used by the font cache. + */ + static size_t GetFontCacheUsed(); + + /** + * Attempt to purge the font cache until <= the specified amount remains + * in the cache. Specifying 0 will attempt to purge the entire cache. + * Returns true if some amount was purged from the font cache. + */ + static bool SetFontCacheUsed(size_t usageInBytes); + + /** + * Return the max number of bytes that should be used by the font cache. + * If the cache needs to allocate more, it will purge previous entries. + * This max can be changed by calling SetFontCacheLimit(). + */ + static size_t GetFontCacheLimit(); + + /** + * Specify the max number of bytes that should be used by the font cache. + * If the cache needs to allocate more, it will purge previous entries. + * + * This function returns the previous setting, as if GetFontCacheLimit() + * had be called before the new limit was set. + */ + static size_t SetFontCacheLimit(size_t bytes); + private: /** This is automatically called by SkGraphics::Init(), and must be implemented by the host OS. This allows the host OS to register a callback diff --git a/src/core/SkGlyphCache.cpp b/src/core/SkGlyphCache.cpp index 6cdc993dd8..937ac225b2 100644 --- a/src/core/SkGlyphCache.cpp +++ b/src/core/SkGlyphCache.cpp @@ -8,7 +8,7 @@ #include "SkGlyphCache.h" -#include "SkFontHost.h" +#include "SkGraphics.h" #include "SkPaint.h" #include "SkTemplates.h" @@ -545,9 +545,10 @@ void SkGlyphCache::AttachCache(SkGlyphCache* cache) { // if we have a fixed budget for our cache, do a purge here { size_t allocated = globals.fTotalMemoryUsed + cache->fMemoryUsed; - size_t amountToFree = SkFontHost::ShouldPurgeFontCache(allocated); - if (amountToFree) - (void)InternalFreeCache(&globals, amountToFree); + size_t budgeted = SkGraphics::GetFontCacheLimit(); + if (allocated > budgeted) { + (void)InternalFreeCache(&globals, allocated - budgeted); + } } cache->attachToHead(&globals.fHead); diff --git a/src/core/SkGraphics.cpp b/src/core/SkGraphics.cpp index 2da0856d86..5e8e549624 100644 --- a/src/core/SkGraphics.cpp +++ b/src/core/SkGraphics.cpp @@ -29,6 +29,18 @@ #include "SkUtils.h" #include "SkXfermode.h" +void SkGraphics::GetVersion(int32_t* major, int32_t* minor, int32_t* patch) { + if (major) { + *major = SKIA_VERSION_MAJOR; + } + if (minor) { + *minor = SKIA_VERSION_MINOR; + } + if (patch) { + *patch = SKIA_VERSION_PATCH; + } +} + #define typesizeline(type) { #type , sizeof(type) } #ifdef BUILD_EMBOSS_TABLE @@ -124,15 +136,27 @@ bool SkGraphics::SetFontCacheUsed(size_t usageInBytes) { return SkGlyphCache::SetCacheUsed(usageInBytes); } -void SkGraphics::GetVersion(int32_t* major, int32_t* minor, int32_t* patch) { - if (major) { - *major = SKIA_VERSION_MAJOR; +#ifndef SK_DEFAULT_FONT_CACHE_LIMIT + #define SK_DEFAULT_FONT_CACHE_LIMIT (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) { + if (bytes < SK_MIN_FONT_CACHE_LIMIT) { + bytes = SK_MIN_FONT_CACHE_LIMIT; } - if (minor) { - *minor = SKIA_VERSION_MINOR; - } - if (patch) { - *patch = SKIA_VERSION_PATCH; + gFontCacheLimit = bytes; + + // trigger a purge if the new size is smaller that our currently used amount + if (bytes < GetFontCacheUsed()) { + SetFontCacheUsed(bytes); } } diff --git a/src/ports/SkFontHost_FONTPATH.cpp b/src/ports/SkFontHost_FONTPATH.cpp index b4a9e50104..f0438f429c 100644 --- a/src/ports/SkFontHost_FONTPATH.cpp +++ b/src/ports/SkFontHost_FONTPATH.cpp @@ -324,8 +324,3 @@ SkScalerContext* SkFontHost::CreateFallbackScalerContext( return SkFontHost::CreateScalerContext(desc); } -size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) -{ - return 0; // nothing to do (change me if you want to limit the font cache) -} - diff --git a/src/ports/SkFontHost_android.cpp b/src/ports/SkFontHost_android.cpp index 7f1517485e..dce3b3c9d8 100644 --- a/src/ports/SkFontHost_android.cpp +++ b/src/ports/SkFontHost_android.cpp @@ -26,8 +26,6 @@ #include "FontHostConfiguration_android.h" #include -#define FONT_CACHE_MEMORY_BUDGET (768 * 1024) - #ifndef SK_FONT_FILE_PREFIX #define SK_FONT_FILE_PREFIX "/fonts/" #endif @@ -763,12 +761,3 @@ SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { return face; } -/////////////////////////////////////////////////////////////////////////////// - -size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) { - if (sizeAllocatedSoFar > FONT_CACHE_MEMORY_BUDGET) - return sizeAllocatedSoFar - FONT_CACHE_MEMORY_BUDGET; - else - return 0; // nothing to do -} - diff --git a/src/ports/SkFontHost_fontconfig.cpp b/src/ports/SkFontHost_fontconfig.cpp index 4100572a4c..1ae971d742 100644 --- a/src/ports/SkFontHost_fontconfig.cpp +++ b/src/ports/SkFontHost_fontconfig.cpp @@ -44,9 +44,6 @@ static std::map global_fc_map_inverted; static std::map global_fc_typefaces; static unsigned global_fc_map_next_id = 0; -// This is the maximum size of the font cache. -static const unsigned kFontCacheMemoryBudget = 2 * 1024 * 1024; // 2MB - static unsigned UniqueIdToFileId(unsigned uniqueid) { return uniqueid >> 8; @@ -361,12 +358,3 @@ SkFontID SkFontHost::NextLogicalFont(SkFontID currFontID, SkFontID origFontID) { return 0; } -/////////////////////////////////////////////////////////////////////////////// - -size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) -{ - if (sizeAllocatedSoFar > kFontCacheMemoryBudget) - return sizeAllocatedSoFar - kFontCacheMemoryBudget; - else - return 0; // nothing to do -} diff --git a/src/ports/SkFontHost_linux.cpp b/src/ports/SkFontHost_linux.cpp index b60db0764b..f39270c6bd 100644 --- a/src/ports/SkFontHost_linux.cpp +++ b/src/ports/SkFontHost_linux.cpp @@ -18,8 +18,6 @@ #include "SkTSearch.h" #include -#define FONT_CACHE_MEMORY_BUDGET (1 * 1024 * 1024) - #ifndef SK_FONT_FILE_PREFIX #define SK_FONT_FILE_PREFIX "/usr/share/fonts/truetype/msttcorefonts/" #endif @@ -600,12 +598,3 @@ SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { return face; } -/////////////////////////////////////////////////////////////////////////////// - -size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) { - if (sizeAllocatedSoFar > FONT_CACHE_MEMORY_BUDGET) - return sizeAllocatedSoFar - FONT_CACHE_MEMORY_BUDGET; - else - return 0; // nothing to do -} - diff --git a/src/ports/SkFontHost_mac_atsui.cpp b/src/ports/SkFontHost_mac_atsui.cpp index d5c50fbf86..3c3abb4647 100644 --- a/src/ports/SkFontHost_mac_atsui.cpp +++ b/src/ports/SkFontHost_mac_atsui.cpp @@ -15,9 +15,6 @@ #include "SkPaint.h" #include "SkPoint.h" -// Give 1MB font cache budget -#define FONT_CACHE_MEMORY_BUDGET (1024 * 1024) - const char* gDefaultfont = "Arial"; // hard code for now static SkMutex gFTMutex; @@ -508,13 +505,6 @@ SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace, } } -size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) { - if (sizeAllocatedSoFar > FONT_CACHE_MEMORY_BUDGET) - return sizeAllocatedSoFar - FONT_CACHE_MEMORY_BUDGET; - else - return 0; // nothing to do -} - int SkFontHost::ComputeGammaFlag(const SkPaint& paint) { return 0; } diff --git a/src/ports/SkFontHost_mac_coretext.cpp b/src/ports/SkFontHost_mac_coretext.cpp index abea9d5806..f7ef0a8461 100644 --- a/src/ports/SkFontHost_mac_coretext.cpp +++ b/src/ports/SkFontHost_mac_coretext.cpp @@ -160,7 +160,6 @@ static unsigned CGRGBPixel_getAlpha(CGRGBPixel pixel) { using namespace skia_advanced_typeface_metrics_utils; -static const size_t FONT_CACHE_MEMORY_BUDGET = 1024 * 1024; static const char FONT_DEFAULT_NAME[] = "Lucida Sans"; // see Source/WebKit/chromium/base/mac/mac_util.mm DarwinMajorVersionInternal @@ -1612,13 +1611,6 @@ void SkFontHost::FilterRec(SkScalerContext::Rec* rec) { /////////////////////////////////////////////////////////////////////////// -size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) { - if (sizeAllocatedSoFar > FONT_CACHE_MEMORY_BUDGET) { - return sizeAllocatedSoFar - FONT_CACHE_MEMORY_BUDGET; - } - return 0; -} - #include "SkColorFilter.h" static bool justAColor(const SkPaint& paint, SkColor* color) { diff --git a/src/ports/SkFontHost_none.cpp b/src/ports/SkFontHost_none.cpp index 9c61b84f21..94bfe0b3ae 100644 --- a/src/ports/SkFontHost_none.cpp +++ b/src/ports/SkFontHost_none.cpp @@ -81,10 +81,6 @@ SkFontID SkFontHost::NextLogicalFont(SkFontID currFontID, SkFontID origFontID) { /////////////////////////////////////////////////////////////////////////////// -size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) { - return 0; // nothing to do (change me if you want to limit the font cache) -} - int SkFontHost::ComputeGammaFlag(const SkPaint& paint) { return 0; } diff --git a/src/ports/SkFontHost_simple.cpp b/src/ports/SkFontHost_simple.cpp index d94edfca7c..44642937dc 100644 --- a/src/ports/SkFontHost_simple.cpp +++ b/src/ports/SkFontHost_simple.cpp @@ -17,8 +17,6 @@ #include "SkTSearch.h" #include -#define FONT_CACHE_MEMORY_BUDGET (768 * 1024) - #ifdef SK_BUILD_FOR_MAC #define SK_FONT_FILE_PREFIX "/Library/Fonts/" #else @@ -653,12 +651,3 @@ SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { return face; } -/////////////////////////////////////////////////////////////////////////////// - -size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) { - if (sizeAllocatedSoFar > FONT_CACHE_MEMORY_BUDGET) - return sizeAllocatedSoFar - FONT_CACHE_MEMORY_BUDGET; - else - return 0; // nothing to do -} - diff --git a/src/ports/SkFontHost_win.cpp b/src/ports/SkFontHost_win.cpp index dd9c5dc7de..847b814826 100755 --- a/src/ports/SkFontHost_win.cpp +++ b/src/ports/SkFontHost_win.cpp @@ -75,9 +75,6 @@ using namespace skia_advanced_typeface_metrics_utils; static const uint16_t BUFFERSIZE = (16384 - 32); static uint8_t glyphbuf[BUFFERSIZE]; -// Give 1MB font cache budget -#define FONT_CACHE_MEMORY_BUDGET (1024 * 1024) - /** * Since LOGFONT wants its textsize as an int, and we support fractional sizes, * and since we have a cache of LOGFONTs for our tyepfaces, we always set the @@ -1246,13 +1243,6 @@ SkTypeface* SkFontHost::CreateTypeface(const SkTypeface* familyFace, return SkCreateTypefaceFromLOGFONT(lf); } -size_t SkFontHost::ShouldPurgeFontCache(size_t sizeAllocatedSoFar) { - if (sizeAllocatedSoFar > FONT_CACHE_MEMORY_BUDGET) - return sizeAllocatedSoFar - FONT_CACHE_MEMORY_BUDGET; - else - return 0; // nothing to do -} - SkTypeface* SkFontHost::CreateTypefaceFromFile(const char path[]) { printf("SkFontHost::CreateTypefaceFromFile unimplemented"); return NULL;