From ec0d975158e5f3b1c6a60c44fb10171f1b3c1b32 Mon Sep 17 00:00:00 2001 From: Grigori Goronzy Date: Sun, 1 Aug 2010 03:30:59 +0200 Subject: [PATCH] Limit (bitmap) glyph cache size Now that bitmap glyphs (for vector clip masks) are stored in the glyph cache, make sure it cannot grow indefinitely easily. Similar to the bitmap cache, track approximate size of the cached elements and reset if the cache exceeds a certain limit. Also, reduce default bitmap cache size to 30 MB as we have essentially two bitmap caches now. That's still plenty in all use cases where caching matters. --- libass/ass_cache.c | 5 +++++ libass/ass_render.c | 10 ++++++---- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/libass/ass_cache.c b/libass/ass_cache.c index 0252735..3ebf0cc 100644 --- a/libass/ass_cache.c +++ b/libass/ass_cache.c @@ -288,6 +288,11 @@ static void glyph_hash_dtor(void *key, size_t key_size, void *value, void *cache_add_glyph(Hashmap *glyph_cache, GlyphHashKey *key, GlyphHashValue *val) { + if (val->glyph && val->glyph->format == FT_GLYPH_FORMAT_BITMAP) { + FT_Bitmap *bitmap = &((FT_BitmapGlyph) val->glyph)->bitmap; + glyph_cache->cache_size += bitmap->rows * bitmap->pitch; + } + return hashmap_insert(glyph_cache, key, val); } diff --git a/libass/ass_render.c b/libass/ass_render.c index cf11f92..1fdbfe4 100644 --- a/libass/ass_render.c +++ b/libass/ass_render.c @@ -45,7 +45,7 @@ #define SUBPIXEL_MASK 63 #define SUBPIXEL_ACCURACY 7 // d6 mask for subpixel accuracy adjustment #define GLYPH_CACHE_MAX 1000 -#define BITMAP_CACHE_MAX_SIZE 50 * 1048576 +#define BITMAP_CACHE_MAX_SIZE 30 * 1048576 static void ass_lazy_track_init(ASS_Renderer *render_priv) { @@ -2358,10 +2358,12 @@ ass_start_frame(ASS_Renderer *render_priv, ASS_Track *track, render_priv->prev_images_root = 0; } - if (cache->glyph_cache->count > cache->glyph_max) { + if (cache->glyph_cache->count > cache->glyph_max + || cache->glyph_cache->cache_size > cache->bitmap_max_size) { ass_msg(render_priv->library, MSGL_V, - "Hitting hard glyph cache limit (was: %ld glyphs), resetting.", - (long) cache->glyph_cache->count); + "Hitting hard glyph cache limit (was: %d glyphs, %ld bytes), " + "resetting.", + cache->glyph_cache->count, (long) cache->glyph_cache->cache_size); cache->glyph_cache = ass_glyph_cache_reset(cache->glyph_cache); } -- 2.40.0