Mercurial > mplayer.hg
diff libass/ass_cache.c @ 34295:6e7f60f6f9d4
Update libass to 0.10 release.
Patch by [subjunk gmail com], see bug #2008.
| author | reimar |
|---|---|
| date | Sat, 03 Dec 2011 21:35:56 +0000 |
| parents | 88eebbbbd6a0 |
| children |
line wrap: on
line diff
--- a/libass/ass_cache.c Sat Dec 03 21:33:28 2011 +0000 +++ b/libass/ass_cache.c Sat Dec 03 21:35:56 2011 +0000 @@ -1,5 +1,6 @@ /* * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com> + * Copyright (C) 2011 Grigori Goronzy <greg@chown.ath.cx> * * This file is part of libass. * @@ -20,129 +21,35 @@ #include <inttypes.h> #include <ft2build.h> -#include FT_FREETYPE_H -#include FT_GLYPH_H - +#include FT_OUTLINE_H #include <assert.h> #include "ass_utils.h" -#include "ass.h" -#include "ass_fontconfig.h" #include "ass_font.h" -#include "ass_bitmap.h" #include "ass_cache.h" -static unsigned hashmap_hash(void *buf, size_t len) -{ - return fnv_32a_buf(buf, len, FNV1_32A_INIT); -} - -static int hashmap_key_compare(void *a, void *b, size_t size) -{ - return memcmp(a, b, size) == 0; -} - -static void hashmap_item_dtor(void *key, size_t key_size, void *value, - size_t value_size) -{ - free(key); - free(value); -} - -Hashmap *hashmap_init(ASS_Library *library, size_t key_size, - size_t value_size, int nbuckets, - HashmapItemDtor item_dtor, - HashmapKeyCompare key_compare, - HashmapHash hash) -{ - Hashmap *map = calloc(1, sizeof(Hashmap)); - map->library = library; - map->nbuckets = nbuckets; - map->key_size = key_size; - map->value_size = value_size; - map->root = calloc(nbuckets, sizeof(hashmap_item_p)); - map->item_dtor = item_dtor ? item_dtor : hashmap_item_dtor; - map->key_compare = key_compare ? key_compare : hashmap_key_compare; - map->hash = hash ? hash : hashmap_hash; - return map; -} - -void hashmap_done(Hashmap *map) -{ - int i; - // print stats - if (map->count > 0 || map->hit_count + map->miss_count > 0) - ass_msg(map->library, MSGL_V, - "cache statistics: \n total accesses: %d\n hits: %d\n " - "misses: %d\n object count: %d", - map->hit_count + map->miss_count, map->hit_count, - map->miss_count, map->count); +// type-specific functions +// create hash/compare functions for bitmap, outline and composite cache +#define CREATE_HASH_FUNCTIONS +#include "ass_cache_template.h" +#define CREATE_COMPARISON_FUNCTIONS +#include "ass_cache_template.h" - for (i = 0; i < map->nbuckets; ++i) { - HashmapItem *item = map->root[i]; - while (item) { - HashmapItem *next = item->next; - map->item_dtor(item->key, map->key_size, item->value, - map->value_size); - free(item); - item = next; - } - } - free(map->root); - free(map); -} - -// does nothing if key already exists -void *hashmap_insert(Hashmap *map, void *key, void *value) -{ - unsigned hash = map->hash(key, map->key_size); - HashmapItem **next = map->root + (hash % map->nbuckets); - while (*next) { - if (map->key_compare(key, (*next)->key, map->key_size)) - return (*next)->value; - next = &((*next)->next); - assert(next); - } - (*next) = malloc(sizeof(HashmapItem)); - (*next)->key = malloc(map->key_size); - (*next)->value = malloc(map->value_size); - memcpy((*next)->key, key, map->key_size); - memcpy((*next)->value, value, map->value_size); - (*next)->next = 0; - - map->count++; - return (*next)->value; -} - -void *hashmap_find(Hashmap *map, void *key) -{ - unsigned hash = map->hash(key, map->key_size); - HashmapItem *item = map->root[hash % map->nbuckets]; - while (item) { - if (map->key_compare(key, item->key, map->key_size)) { - map->hit_count++; - return item->value; - } - item = item->next; - } - map->miss_count++; - return 0; -} - -//--------------------------------- // font cache - -static unsigned font_desc_hash(void *buf, size_t len) +static unsigned font_hash(void *buf, size_t len) { ASS_FontDesc *desc = buf; unsigned hval; hval = fnv_32a_str(desc->family, FNV1_32A_INIT); hval = fnv_32a_buf(&desc->bold, sizeof(desc->bold), hval); hval = fnv_32a_buf(&desc->italic, sizeof(desc->italic), hval); + hval = fnv_32a_buf(&desc->treat_family_as_pattern, + sizeof(desc->treat_family_as_pattern), hval); + hval = fnv_32a_buf(&desc->vertical, sizeof(desc->vertical), hval); return hval; } -static int font_compare(void *key1, void *key2, size_t key_size) +static unsigned font_compare(void *key1, void *key2, size_t key_size) { ASS_FontDesc *a = key1; ASS_FontDesc *b = key2; @@ -159,181 +66,63 @@ return 1; } -static void font_hash_dtor(void *key, size_t key_size, void *value, - size_t value_size) +static void font_destruct(void *key, void *value) { ass_font_free(value); free(key); } -ASS_Font *ass_font_cache_find(Hashmap *font_cache, - ASS_FontDesc *desc) -{ - return hashmap_find(font_cache, desc); -} - -/** - * \brief Add a face struct to cache. - * \param font font struct -*/ -void *ass_font_cache_add(Hashmap *font_cache, ASS_Font *font) -{ - return hashmap_insert(font_cache, &(font->desc), font); -} - -Hashmap *ass_font_cache_init(ASS_Library *library) -{ - Hashmap *font_cache; - font_cache = hashmap_init(library, sizeof(ASS_FontDesc), - sizeof(ASS_Font), - 1000, - font_hash_dtor, font_compare, font_desc_hash); - return font_cache; -} - -void ass_font_cache_done(Hashmap *font_cache) -{ - hashmap_done(font_cache); -} - - -// Create hash/compare functions for bitmap and glyph -#define CREATE_HASH_FUNCTIONS -#include "ass_cache_template.h" -#define CREATE_COMPARISON_FUNCTIONS -#include "ass_cache_template.h" - -//--------------------------------- // bitmap cache - -static void bitmap_hash_dtor(void *key, size_t key_size, void *value, - size_t value_size) +static void bitmap_destruct(void *key, void *value) { BitmapHashValue *v = value; + BitmapHashKey *k = key; if (v->bm) ass_free_bitmap(v->bm); if (v->bm_o) ass_free_bitmap(v->bm_o); if (v->bm_s) ass_free_bitmap(v->bm_s); + if (k->type == BITMAP_CLIP) + free(k->u.clip.text); free(key); free(value); } -void *cache_add_bitmap(Hashmap *bitmap_cache, BitmapHashKey *key, - BitmapHashValue *val) -{ - // Note: this is only an approximation - if (val->bm_o) - bitmap_cache->cache_size += val->bm_o->w * val->bm_o->h * 3; - else if (val->bm) - bitmap_cache->cache_size += val->bm->w * val->bm->h * 3; - - return hashmap_insert(bitmap_cache, key, val); -} - -/** - * \brief Get a bitmap from bitmap cache. - * \param key hash key - * \return requested hash val or 0 if not found -*/ -BitmapHashValue *cache_find_bitmap(Hashmap *bitmap_cache, - BitmapHashKey *key) +static size_t bitmap_size(void *value, size_t value_size) { - return hashmap_find(bitmap_cache, key); -} - -Hashmap *ass_bitmap_cache_init(ASS_Library *library) -{ - Hashmap *bitmap_cache; - bitmap_cache = hashmap_init(library, - sizeof(BitmapHashKey), - sizeof(BitmapHashValue), - 0xFFFF + 13, - bitmap_hash_dtor, bitmap_compare, - bitmap_hash); - return bitmap_cache; -} - -void ass_bitmap_cache_done(Hashmap *bitmap_cache) -{ - hashmap_done(bitmap_cache); -} - -Hashmap *ass_bitmap_cache_reset(Hashmap *bitmap_cache) -{ - ASS_Library *lib = bitmap_cache->library; - - ass_bitmap_cache_done(bitmap_cache); - return ass_bitmap_cache_init(lib); + BitmapHashValue *val = value; + if (val->bm_o) + return val->bm_o->w * val->bm_o->h * 3; + else if (val->bm) + return val->bm->w * val->bm->h * 3; + return 0; } -//--------------------------------- -// glyph cache - -static void glyph_hash_dtor(void *key, size_t key_size, void *value, - size_t value_size) +static unsigned bitmap_hash(void *key, size_t key_size) { - GlyphHashValue *v = value; - if (v->glyph) - FT_Done_Glyph(v->glyph); - if (v->outline_glyph) - FT_Done_Glyph(v->outline_glyph); - free(key); - free(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); + BitmapHashKey *k = key; + switch (k->type) { + case BITMAP_OUTLINE: return outline_bitmap_hash(&k->u, key_size); + case BITMAP_CLIP: return clip_bitmap_hash(&k->u, key_size); + default: return 0; + } } -/** - * \brief Get a glyph from glyph cache. - * \param key hash key - * \return requested hash val or 0 if not found -*/ -GlyphHashValue *cache_find_glyph(Hashmap *glyph_cache, - GlyphHashKey *key) +static unsigned bitmap_compare (void *a, void *b, size_t key_size) { - return hashmap_find(glyph_cache, key); + BitmapHashKey *ak = a; + BitmapHashKey *bk = b; + if (ak->type != bk->type) return 0; + switch (ak->type) { + case BITMAP_OUTLINE: return outline_bitmap_compare(&ak->u, &bk->u, key_size); + case BITMAP_CLIP: return clip_bitmap_compare(&ak->u, &bk->u, key_size); + default: return 0; + } } -Hashmap *ass_glyph_cache_init(ASS_Library *library) -{ - Hashmap *glyph_cache; - glyph_cache = hashmap_init(library, sizeof(GlyphHashKey), - sizeof(GlyphHashValue), - 0xFFFF + 13, - glyph_hash_dtor, glyph_compare, glyph_hash); - return glyph_cache; -} - -void ass_glyph_cache_done(Hashmap *glyph_cache) -{ - hashmap_done(glyph_cache); -} - -Hashmap *ass_glyph_cache_reset(Hashmap *glyph_cache) -{ - ASS_Library *lib = glyph_cache->library; - - ass_glyph_cache_done(glyph_cache); - return ass_glyph_cache_init(lib); -} - - -//--------------------------------- // composite cache - -static void composite_hash_dtor(void *key, size_t key_size, void *value, - size_t value_size) +static void composite_destruct(void *key, void *value) { CompositeHashValue *v = value; free(v->a); @@ -342,44 +131,222 @@ free(value); } -void *cache_add_composite(Hashmap *composite_cache, - CompositeHashKey *key, - CompositeHashValue *val) +// outline cache + +static unsigned outline_hash(void *key, size_t key_size) +{ + OutlineHashKey *k = key; + switch (k->type) { + case OUTLINE_GLYPH: return glyph_hash(&k->u, key_size); + case OUTLINE_DRAWING: return drawing_hash(&k->u, key_size); + default: return 0; + } +} + +static unsigned outline_compare(void *a, void *b, size_t key_size) { - return hashmap_insert(composite_cache, key, val); + OutlineHashKey *ak = a; + OutlineHashKey *bk = b; + if (ak->type != bk->type) return 0; + switch (ak->type) { + case OUTLINE_GLYPH: return glyph_compare(&ak->u, &bk->u, key_size); + case OUTLINE_DRAWING: return drawing_compare(&ak->u, &bk->u, key_size); + default: return 0; + } +} + +static void outline_destruct(void *key, void *value) +{ + OutlineHashValue *v = value; + OutlineHashKey *k = key; + if (v->outline) + outline_free(v->lib, v->outline); + if (v->border) + outline_free(v->lib, v->border); + if (k->type == OUTLINE_DRAWING) + free(k->u.drawing.text); + free(key); + free(value); } -/** - * \brief Get a composite bitmap from composite cache. - * \param key hash key - * \return requested hash val or 0 if not found -*/ -CompositeHashValue *cache_find_composite(Hashmap *composite_cache, - CompositeHashKey *key) + + +// Cache data +typedef struct cache_item { + void *key; + void *value; + struct cache_item *next; +} CacheItem; + +struct cache { + unsigned buckets; + CacheItem **map; + + HashFunction hash_func; + ItemSize size_func; + HashCompare compare_func; + CacheItemDestructor destruct_func; + size_t key_size; + size_t value_size; + + size_t cache_size; + unsigned hits; + unsigned misses; + unsigned items; +}; + +// Hash for a simple (single value or array) type +static unsigned hash_simple(void *key, size_t key_size) { - return hashmap_find(composite_cache, key); + return fnv_32a_buf(key, key_size, FNV1_32A_INIT); +} + +// Comparison of a simple type +static unsigned compare_simple(void *a, void *b, size_t key_size) +{ + return memcmp(a, b, key_size) == 0; +} + +// Default destructor +static void destruct_simple(void *key, void *value) +{ + free(key); + free(value); +} + + +// Create a cache with type-specific hash/compare/destruct/size functions +Cache *ass_cache_create(HashFunction hash_func, HashCompare compare_func, + CacheItemDestructor destruct_func, ItemSize size_func, + size_t key_size, size_t value_size) +{ + Cache *cache = calloc(1, sizeof(*cache)); + cache->buckets = 0xFFFF; + cache->hash_func = hash_simple; + cache->compare_func = compare_simple; + cache->destruct_func = destruct_simple; + cache->size_func = size_func; + if (hash_func) + cache->hash_func = hash_func; + if (compare_func) + cache->compare_func = compare_func; + if (destruct_func) + cache->destruct_func = destruct_func; + cache->key_size = key_size; + cache->value_size = value_size; + cache->map = calloc(cache->buckets, sizeof(CacheItem *)); + + return cache; } -Hashmap *ass_composite_cache_init(ASS_Library *library) +void *ass_cache_put(Cache *cache, void *key, void *value) { - Hashmap *composite_cache; - composite_cache = hashmap_init(library, sizeof(CompositeHashKey), - sizeof(CompositeHashValue), - 0xFFFF + 13, - composite_hash_dtor, composite_compare, - composite_hash); - return composite_cache; + unsigned bucket = cache->hash_func(key, cache->key_size) % cache->buckets; + CacheItem **item = &cache->map[bucket]; + while (*item) + item = &(*item)->next; + (*item) = calloc(1, sizeof(CacheItem)); + (*item)->key = malloc(cache->key_size); + (*item)->value = malloc(cache->value_size); + memcpy((*item)->key, key, cache->key_size); + memcpy((*item)->value, value, cache->value_size); + + cache->items++; + if (cache->size_func) + cache->cache_size += cache->size_func(value, cache->value_size); + else + cache->cache_size++; + + return (*item)->value; +} + +void *ass_cache_get(Cache *cache, void *key) +{ + unsigned bucket = cache->hash_func(key, cache->key_size) % cache->buckets; + CacheItem *item = cache->map[bucket]; + while (item) { + if (cache->compare_func(key, item->key, cache->key_size)) { + cache->hits++; + return item->value; + } + item = item->next; + } + cache->misses++; + return NULL; } -void ass_composite_cache_done(Hashmap *composite_cache) +int ass_cache_empty(Cache *cache, size_t max_size) { - hashmap_done(composite_cache); + int i; + + if (cache->cache_size < max_size) + return 0; + + for (i = 0; i < cache->buckets; i++) { + CacheItem *item = cache->map[i]; + while (item) { + CacheItem *next = item->next; + cache->destruct_func(item->key, item->value); + free(item); + item = next; + } + cache->map[i] = NULL; + } + + cache->items = cache->hits = cache->misses = cache->cache_size = 0; + + return 1; +} + +void ass_cache_stats(Cache *cache, size_t *size, unsigned *hits, + unsigned *misses, unsigned *count) +{ + if (size) + *size = cache->cache_size; + if (hits) + *hits = cache->hits; + if (misses) + *misses = cache->misses; + if (count) + *count = cache->items; } -Hashmap *ass_composite_cache_reset(Hashmap *composite_cache) +void ass_cache_done(Cache *cache) +{ + ass_cache_empty(cache, 0); + free(cache->map); + free(cache); +} + +// Type-specific creation function +Cache *ass_font_cache_create(void) { - ASS_Library *lib = composite_cache->library; + return ass_cache_create(font_hash, font_compare, font_destruct, + (ItemSize)NULL, sizeof(ASS_FontDesc), sizeof(ASS_Font)); +} + +Cache *ass_outline_cache_create(void) +{ + return ass_cache_create(outline_hash, outline_compare, outline_destruct, + NULL, sizeof(OutlineHashKey), sizeof(OutlineHashValue)); +} - ass_composite_cache_done(composite_cache); - return ass_composite_cache_init(lib); +Cache *ass_glyph_metrics_cache_create(void) +{ + return ass_cache_create(glyph_metrics_hash, glyph_metrics_compare, NULL, + (ItemSize) NULL, sizeof(GlyphMetricsHashKey), + sizeof(GlyphMetricsHashValue)); } + +Cache *ass_bitmap_cache_create(void) +{ + return ass_cache_create(bitmap_hash, bitmap_compare, bitmap_destruct, + bitmap_size, sizeof(BitmapHashKey), sizeof(BitmapHashValue)); +} + +Cache *ass_composite_cache_create(void) +{ + return ass_cache_create(composite_hash, composite_compare, + composite_destruct, (ItemSize)NULL, sizeof(CompositeHashKey), + sizeof(CompositeHashValue)); +}
