From 954c0163a7c7669c0c54527d4fe66745a0f572ef Mon Sep 17 00:00:00 2001 From: "Dr.Smile" Date: Fri, 18 Sep 2015 01:15:16 +0300 Subject: cache: replace size_func with parameter in ass_cache_commit() --- libass/ass_cache.c | 50 ++++++++++---------------------------------------- libass/ass_cache.h | 5 ++--- libass/ass_font.c | 4 ++-- libass/ass_render.c | 26 ++++++++++++++++++-------- libass/ass_shaper.c | 4 ++-- 5 files changed, 34 insertions(+), 55 deletions(-) (limited to 'libass') diff --git a/libass/ass_cache.c b/libass/ass_cache.c index 87e6cc2..ce742e6 100644 --- a/libass/ass_cache.c +++ b/libass/ass_cache.c @@ -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)); } diff --git a/libass/ass_cache.h b/libass/ass_cache.h index c18f9d8..6cea269 100644 --- a/libass/ass_cache.h +++ b/libass/ass_cache.h @@ -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); diff --git a/libass/ass_font.c b/libass/ass_font.c index c40ff1e..d9dc7ba 100644 --- a/libass/ass_font.c +++ b/libass/ass_font.c @@ -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; } diff --git a/libass/ass_render.c b/libass/ass_render.c index 7a1dd47..53d2681 100644 --- a/libass/ass_render.c +++ b/libass/ass_render.c @@ -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; diff --git a/libass/ass_shaper.c b/libass/ass_shaper.c index 6f872e3..8cb8654 100644 --- a/libass/ass_shaper.c +++ b/libass/ass_shaper.c @@ -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; } -- cgit v1.2.3