]> granicus.if.org Git - libass/commitdiff
Limit (bitmap) glyph cache size
authorGrigori Goronzy <greg@blackbox>
Sun, 1 Aug 2010 01:30:59 +0000 (03:30 +0200)
committerGrigori Goronzy <greg@blackbox>
Sun, 1 Aug 2010 01:35:19 +0000 (03:35 +0200)
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
libass/ass_render.c

index 025273529bcf6ebc2dfcc5f646aab39572ed6388..3ebf0cc97fa7f2b37cec6b1fb0c730fc8e01b04b 100644 (file)
@@ -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);
 }
 
index cf11f92c1857f82e1f42882880152963ee8670a3..1fdbfe443c0f11c0a6481a77e72d25525eddf3a7 100644 (file)
@@ -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);
     }