2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
3 * Copyright (C) 2011 Grigori Goronzy <greg@chown.ath.cx>
5 * This file is part of libass.
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
27 #include "ass_utils.h"
29 #include "ass_cache.h"
31 // type-specific functions
32 // create hash/compare functions for bitmap, outline and composite cache
33 #define CREATE_HASH_FUNCTIONS
34 #include "ass_cache_template.h"
35 #define CREATE_COMPARISON_FUNCTIONS
36 #include "ass_cache_template.h"
39 static unsigned font_hash(void *buf, size_t len)
41 ASS_FontDesc *desc = buf;
43 hval = fnv_32a_str(desc->family, FNV1_32A_INIT);
44 hval = fnv_32a_buf(&desc->bold, sizeof(desc->bold), hval);
45 hval = fnv_32a_buf(&desc->italic, sizeof(desc->italic), hval);
46 hval = fnv_32a_buf(&desc->treat_family_as_pattern,
47 sizeof(desc->treat_family_as_pattern), hval);
48 hval = fnv_32a_buf(&desc->vertical, sizeof(desc->vertical), hval);
52 static unsigned font_compare(void *key1, void *key2, size_t key_size)
54 ASS_FontDesc *a = key1;
55 ASS_FontDesc *b = key2;
56 if (strcmp(a->family, b->family) != 0)
58 if (a->bold != b->bold)
60 if (a->italic != b->italic)
62 if (a->treat_family_as_pattern != b->treat_family_as_pattern)
64 if (a->vertical != b->vertical)
69 static void font_destruct(void *key, void *value)
76 static void bitmap_destruct(void *key, void *value)
78 BitmapHashValue *v = value;
80 ass_free_bitmap(v->bm);
82 ass_free_bitmap(v->bm_o);
84 ass_free_bitmap(v->bm_s);
89 static size_t bitmap_size(void *value, size_t value_size)
91 BitmapHashValue *val = value;
93 return val->bm_o->w * val->bm_o->h * 3;
95 return val->bm->w * val->bm->h * 3;
100 static void composite_destruct(void *key, void *value)
102 CompositeHashValue *v = value;
111 static unsigned outline_hash(void *key, size_t key_size)
113 OutlineHashKey *k = key;
115 case OUTLINE_GLYPH: return glyph_hash(&k->u, key_size);
116 case OUTLINE_DRAWING: return drawing_hash(&k->u, key_size);
121 static unsigned outline_compare(void *a, void *b, size_t key_size)
123 OutlineHashKey *ak = a;
124 OutlineHashKey *bk = b;
125 if (ak->type != bk->type) return 0;
127 case OUTLINE_GLYPH: return glyph_compare(&ak->u, &bk->u, key_size);
128 case OUTLINE_DRAWING: return drawing_compare(&ak->u, &bk->u, key_size);
133 static void outline_destruct(void *key, void *value)
135 OutlineHashValue *v = value;
136 OutlineHashKey *k = key;
138 outline_free(v->lib, v->outline);
140 outline_free(v->lib, v->border);
141 if (k->type == OUTLINE_DRAWING)
142 free(k->u.drawing.text);
150 typedef struct cache_item {
153 struct cache_item *next;
160 HashFunction hash_func;
162 HashCompare compare_func;
163 CacheItemDestructor destruct_func;
173 // Hash for a simple (single value or array) type
174 static unsigned hash_simple(void *key, size_t key_size)
176 return fnv_32a_buf(key, key_size, FNV1_32A_INIT);
179 // Comparison of a simple type
180 static unsigned compare_simple(void *a, void *b, size_t key_size)
182 return memcmp(a, b, key_size) == 0;
185 // Default destructor
186 static void destruct_simple(void *key, void *value)
193 // Create a cache with type-specific hash/compare/destruct/size functions
194 Cache *ass_cache_create(HashFunction hash_func, HashCompare compare_func,
195 CacheItemDestructor destruct_func, ItemSize size_func,
196 size_t key_size, size_t value_size)
198 Cache *cache = calloc(1, sizeof(*cache));
199 cache->buckets = 0xFFFF;
200 cache->hash_func = hash_simple;
201 cache->compare_func = compare_simple;
202 cache->destruct_func = destruct_simple;
203 cache->size_func = size_func;
205 cache->hash_func = hash_func;
207 cache->compare_func = compare_func;
209 cache->destruct_func = destruct_func;
210 cache->key_size = key_size;
211 cache->value_size = value_size;
212 cache->map = calloc(cache->buckets, sizeof(CacheItem *));
217 void *ass_cache_put(Cache *cache, void *key, void *value)
219 unsigned bucket = cache->hash_func(key, cache->key_size) % cache->buckets;
220 CacheItem **item = &cache->map[bucket];
222 *item = (*item)->next;
223 (*item) = calloc(1, sizeof(CacheItem));
224 (*item)->key = malloc(cache->key_size);
225 (*item)->value = malloc(cache->value_size);
226 memcpy((*item)->key, key, cache->key_size);
227 memcpy((*item)->value, value, cache->value_size);
230 if (cache->size_func)
231 cache->cache_size += cache->size_func(value, cache->value_size);
233 return (*item)->value;
236 void *ass_cache_get(Cache *cache, void *key)
238 unsigned bucket = cache->hash_func(key, cache->key_size) % cache->buckets;
239 CacheItem *item = cache->map[bucket];
241 if (cache->compare_func(key, item->key, cache->key_size)) {
251 size_t ass_cache_empty(Cache *cache, size_t max_size)
255 if (cache->cache_size < max_size)
256 return cache->cache_size;
258 for (i = 0; i < cache->buckets; i++) {
259 CacheItem *item = cache->map[i];
261 CacheItem *next = item->next;
262 cache->destruct_func(item->key, item->value);
266 cache->map[i] = NULL;
269 cache->items = cache->hits = cache->misses = cache->cache_size = 0;
274 void ass_cache_stats(Cache *cache, size_t *size, unsigned *hits,
275 unsigned *misses, unsigned *count)
277 *size = cache->cache_size;
279 *misses = cache->misses;
280 *count = cache->items;
283 void ass_cache_done(Cache *cache)
285 ass_cache_empty(cache, 0);
290 // Type-specific creation function
291 Cache *ass_font_cache_create(void)
293 return ass_cache_create(font_hash, font_compare, font_destruct,
294 (ItemSize)NULL, sizeof(ASS_FontDesc), sizeof(ASS_Font));
297 Cache *ass_outline_cache_create(void)
299 return ass_cache_create(outline_hash, outline_compare, outline_destruct,
300 NULL, sizeof(OutlineHashKey), sizeof(OutlineHashValue));
303 Cache *ass_bitmap_cache_create(void)
305 return ass_cache_create(bitmap_hash, bitmap_compare, bitmap_destruct,
306 bitmap_size, sizeof(BitmapHashKey), sizeof(BitmapHashValue));
309 Cache *ass_composite_cache_create(void)
311 return ass_cache_create(composite_hash, composite_compare,
312 composite_destruct, (ItemSize)NULL, sizeof(CompositeHashKey),
313 sizeof(CompositeHashValue));