]> granicus.if.org Git - libass/commitdiff
cache: replace size_func with parameter in ass_cache_commit()
authorDr.Smile <vabnick@gmail.com>
Thu, 17 Sep 2015 22:15:16 +0000 (01:15 +0300)
committerDr.Smile <vabnick@gmail.com>
Thu, 30 Jun 2016 20:13:53 +0000 (23:13 +0300)
libass/ass_cache.c
libass/ass_cache.h
libass/ass_font.c
libass/ass_render.c
libass/ass_shaper.c

index 87e6cc29300b081a4dc58d773a61d942b8cef0dc..ce742e65d6323548de9ac1abd0e373d7b587a739 100644 (file)
@@ -130,17 +130,6 @@ static void bitmap_destruct(void *key, void *value)
     }
 }
 
-static size_t bitmap_size(void *value, size_t value_size)
-{
-    BitmapHashValue *val = value;
-    size_t size = sizeof(BitmapHashKey) + sizeof(BitmapHashValue);
-    if (val->bm)
-        size += sizeof(Bitmap) + val->bm->stride * val->bm->h;
-    if (val->bm_o)
-        size += sizeof(Bitmap) + val->bm_o->stride * val->bm_o->h;
-    return size;
-}
-
 // composite cache
 static unsigned composite_hash(void *key, size_t key_size)
 {
@@ -193,19 +182,6 @@ static void composite_destruct(void *key, void *value)
     free(k->bitmaps);
 }
 
-static size_t composite_size(void *value, size_t value_size)
-{
-    CompositeHashValue *val = value;
-    size_t size = sizeof(CompositeHashKey) + sizeof(CompositeHashValue);
-    if (val->bm)
-        size += sizeof(Bitmap) + val->bm->stride * val->bm->h;
-    if (val->bm_o)
-        size += sizeof(Bitmap) + val->bm_o->stride * val->bm_o->h;
-    if (val->bm_s)
-        size += sizeof(Bitmap) + val->bm_s->stride * val->bm_s->h;
-    return size;
-}
-
 // outline cache
 static unsigned outline_hash(void *key, size_t key_size)
 {
@@ -292,7 +268,6 @@ struct cache {
     CacheItem *queue_first, **queue_last;
 
     HashFunction hash_func;
-    ItemSize size_func;
     HashCompare compare_func;
     CacheKeyCopy copy_func;
     CacheItemDestructor destruct_func;
@@ -346,7 +321,7 @@ static void destruct_simple(void *key, void *value)
 // Create a cache with type-specific hash/compare/destruct/size functions
 Cache *ass_cache_create(HashFunction hash_func, HashCompare compare_func,
                         CacheKeyCopy copy_func, CacheItemDestructor destruct_func,
-                        ItemSize size_func, size_t key_size, size_t value_size)
+                        size_t key_size, size_t value_size)
 {
     Cache *cache = calloc(1, sizeof(*cache));
     if (!cache)
@@ -357,7 +332,6 @@ Cache *ass_cache_create(HashFunction hash_func, HashCompare compare_func,
     cache->compare_func = compare_func ? compare_func : compare_simple;
     cache->copy_func = copy_func ? copy_func : copy_simple;
     cache->destruct_func = destruct_func ? destruct_func : destruct_simple;
-    cache->size_func = size_func;
     cache->key_size = key_size;
     cache->value_size = value_size;
     cache->map = calloc(cache->buckets, sizeof(CacheItem *));
@@ -432,18 +406,14 @@ void *ass_cache_get_key(void *value)
     return (char *) value + align_cache(item->cache->value_size);
 }
 
-void ass_cache_commit(void *value)
+void ass_cache_commit(void *value, size_t item_size)
 {
     CacheItem *item = value_to_item(value);
-    assert(!item->size);
+    assert(!item->size && item_size);
+    item->size = item_size;
     Cache *cache = item->cache;
-
+    cache->cache_size += item_size;
     cache->items++;
-    if (cache->size_func)
-        item->size = cache->size_func(value, cache->value_size);
-    else
-        item->size = 1;
-    cache->cache_size += item->size;
 }
 
 static inline void destroy_item(Cache *cache, CacheItem *item)
@@ -552,34 +522,34 @@ void ass_cache_done(Cache *cache)
 Cache *ass_font_cache_create(void)
 {
     return ass_cache_create(font_hash, font_compare,
-                            font_key_copy, font_destruct, NULL,
+                            font_key_copy, font_destruct,
                             sizeof(ASS_FontDesc), sizeof(ASS_Font));
 }
 
 Cache *ass_outline_cache_create(void)
 {
     return ass_cache_create(outline_hash, outline_compare,
-                            outline_key_copy, outline_destruct, NULL,
+                            outline_key_copy, outline_destruct,
                             sizeof(OutlineHashKey), sizeof(OutlineHashValue));
 }
 
 Cache *ass_glyph_metrics_cache_create(void)
 {
     return ass_cache_create(glyph_metrics_hash, glyph_metrics_compare,
-                            glyph_metric_key_copy, glyph_metric_destruct, NULL,
+                            glyph_metric_key_copy, glyph_metric_destruct,
                             sizeof(GlyphMetricsHashKey), sizeof(GlyphMetricsHashValue));
 }
 
 Cache *ass_bitmap_cache_create(void)
 {
     return ass_cache_create(bitmap_hash, bitmap_compare,
-                            bitmap_key_copy, bitmap_destruct, bitmap_size,
+                            bitmap_key_copy, bitmap_destruct,
                             sizeof(BitmapHashKey), sizeof(BitmapHashValue));
 }
 
 Cache *ass_composite_cache_create(void)
 {
     return ass_cache_create(composite_hash, composite_compare,
-                            composite_key_copy, composite_destruct, composite_size,
+                            composite_key_copy, composite_destruct,
                             sizeof(CompositeHashKey), sizeof(CompositeHashValue));
 }
index c18f9d81fe2fc47c4424a688dbba5ac16e0681a1..6cea269493f67c371c0713d5006fa42008f76cba 100644 (file)
@@ -57,7 +57,6 @@ typedef struct {
 
 // Type-specific function pointers
 typedef unsigned(*HashFunction)(void *key, size_t key_size);
-typedef size_t(*ItemSize)(void *value, size_t value_size);
 typedef unsigned(*HashCompare)(void *a, void *b, size_t key_size);
 typedef bool(*CacheKeyCopy)(void *dst, void *src, size_t key_size);
 typedef void(*CacheItemDestructor)(void *key, void *value);
@@ -106,10 +105,10 @@ typedef struct {
 
 Cache *ass_cache_create(HashFunction hash_func, HashCompare compare_func,
                         CacheKeyCopy copy_func, CacheItemDestructor destruct_func,
-                        ItemSize size_func, size_t key_size, size_t value_size);
+                        size_t key_size, size_t value_size);
 bool ass_cache_get(Cache *cache, void *key, void *value_ptr);
 void *ass_cache_get_key(void *value);
-void ass_cache_commit(void *value);
+void ass_cache_commit(void *value, size_t item_size);
 void ass_cache_inc_ref(void *value);
 void ass_cache_dec_ref(void *value);
 void ass_cache_cut(Cache *cache, size_t max_size);
index c40ff1ef0d2988202d129d41aa8281e10513b92e..d9dc7babf91472dad3eca6ffcbe875b0b9890fc1 100644 (file)
@@ -251,10 +251,10 @@ ASS_Font *ass_font_new(Cache *font_cache, ASS_Library *library,
     int error = add_face(fontsel, font, 0);
     if (error == -1) {
         font->desc.family = NULL;
-        ass_cache_commit(font);
+        ass_cache_commit(font, 1);
         return NULL;
     }
-    ass_cache_commit(font);
+    ass_cache_commit(font, 1);
     return font;
 }
 
index 7a1dd47ae0fcecd8af4fb7d813b06a581b1e929c..53d2681a58d979107c5832a0c1beeee573832f56 100644 (file)
@@ -479,6 +479,12 @@ static bool free_list_add(ASS_Renderer *render_priv, void *object)
     return true;
 }
 
+// Calculate bitmap memory footprint
+static inline size_t bitmap_size(Bitmap *bm)
+{
+    return bm ? sizeof(Bitmap) + bm->stride * bm->h : 0;
+}
+
 /**
  * Iterate through a list of bitmaps and blend with clip vector, if
  * applicable. The blended bitmaps are added to a free list which is freed
@@ -508,7 +514,7 @@ static void blend_vector_clip(ASS_Renderer *render_priv,
         if (!outline) {
             ass_msg(render_priv->library, MSGL_WARN,
                     "Clip vector parsing failed. Skipping.");
-            ass_cache_commit(val);
+            ass_cache_commit(val, sizeof(BitmapHashKey) + sizeof(BitmapHashValue));
             return;
         }
 
@@ -523,7 +529,8 @@ static void blend_vector_clip(ASS_Renderer *render_priv,
         }
 
         val->bm = outline_to_bitmap(render_priv, outline, 0);
-        ass_cache_commit(val);
+        ass_cache_commit(val, bitmap_size(val->bm) +
+                         sizeof(BitmapHashKey) + sizeof(BitmapHashValue));
     }
 
     Bitmap *clip_bm = val->bm;
@@ -1160,7 +1167,7 @@ get_outline_glyph(ASS_Renderer *priv, GlyphInfo *info)
             ASS_Drawing *drawing = info->drawing;
             ass_drawing_hash(drawing);
             if(!ass_drawing_parse(drawing, 0)) {
-                ass_cache_commit(val);
+                ass_cache_commit(val, 1);
                 return;
             }
             val->outline = outline_copy(&drawing->outline);
@@ -1192,7 +1199,7 @@ get_outline_glyph(ASS_Renderer *priv, GlyphInfo *info)
         }
 
         if (!val->outline) {
-            ass_cache_commit(val);
+            ass_cache_commit(val, 1);
             return;
         }
 
@@ -1204,7 +1211,7 @@ get_outline_glyph(ASS_Renderer *priv, GlyphInfo *info)
                 outline_free(val->outline);
                 free(val->outline);
                 val->outline = NULL;
-                ass_cache_commit(val);
+                ass_cache_commit(val, 1);
                 return;
             }
 
@@ -1228,7 +1235,7 @@ get_outline_glyph(ASS_Renderer *priv, GlyphInfo *info)
                     double_to_d6(info->border_y * priv->border_scale));
         }
 
-        ass_cache_commit(val);
+        ass_cache_commit(val, 1);
     }
 
     if (!val->outline)
@@ -1381,7 +1388,8 @@ get_bitmap_glyph(ASS_Renderer *render_priv, GlyphInfo *info)
     if (error)
         info->symbol = 0;
 
-    ass_cache_commit(val);
+    ass_cache_commit(val, bitmap_size(val->bm) + bitmap_size(val->bm_o) +
+                     sizeof(BitmapHashKey) + sizeof(BitmapHashValue));
     info->image = val;
 
     outline_free(outline);
@@ -2349,7 +2357,9 @@ static void render_and_combine_glyphs(ASS_Renderer *render_priv,
         hv->bm = info->bm;
         hv->bm_o = info->bm_o;
         hv->bm_s = info->bm_s;
-        ass_cache_commit(hv);
+        ass_cache_commit(hv, bitmap_size(hv->bm) +
+                         bitmap_size(hv->bm_o) + bitmap_size(hv->bm_s) +
+                         sizeof(CompositeHashKey) + sizeof(CompositeHashValue));
     }
 
     text_info->n_bitmaps = nb_bitmaps;
index 6f872e328959548f6bbd90a58e8715147d14638c..8cb865403afa0ae946178fe9a54482c5147cf455 100644 (file)
@@ -218,7 +218,7 @@ get_cached_metrics(struct ass_shaper_metrics_data *metrics, FT_Face face,
 
     if (FT_Load_Glyph(face, glyph, load_flags)) {
         val->metrics.width = -1;
-        ass_cache_commit(val);
+        ass_cache_commit(val, 1);
         return NULL;
     }
 
@@ -229,7 +229,7 @@ get_cached_metrics(struct ass_shaper_metrics_data *metrics, FT_Face face,
     if (metrics->vertical && unicode >= VERTICAL_LOWER_BOUND)
         val->metrics.horiAdvance = val->metrics.vertAdvance;
 
-    ass_cache_commit(val);
+    ass_cache_commit(val, 1);
     return val;
 }