]> granicus.if.org Git - libass/blob - libass/ass_cache.c
cache: unified outline cache for glyphs/drawings
[libass] / libass / ass_cache.c
1 /*
2  * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
3  * Copyright (C) 2011 Grigori Goronzy <greg@chown.ath.cx>
4  *
5  * This file is part of libass.
6  *
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.
10  *
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.
18  */
19
20 #include "config.h"
21
22 #include <inttypes.h>
23 #include <ft2build.h>
24 #include FT_OUTLINE_H
25 #include <assert.h>
26
27 #include "ass_utils.h"
28 #include "ass_font.h"
29 #include "ass_cache.h"
30
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"
37
38 // font cache
39 static unsigned font_hash(void *buf, size_t len)
40 {
41     ASS_FontDesc *desc = buf;
42     unsigned hval;
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);
49     return hval;
50 }
51
52 static unsigned font_compare(void *key1, void *key2, size_t key_size)
53 {
54     ASS_FontDesc *a = key1;
55     ASS_FontDesc *b = key2;
56     if (strcmp(a->family, b->family) != 0)
57         return 0;
58     if (a->bold != b->bold)
59         return 0;
60     if (a->italic != b->italic)
61         return 0;
62     if (a->treat_family_as_pattern != b->treat_family_as_pattern)
63         return 0;
64     if (a->vertical != b->vertical)
65         return 0;
66     return 1;
67 }
68
69 static void font_destruct(void *key, void *value)
70 {
71     ass_font_free(value);
72     free(key);
73 }
74
75 // bitmap cache
76 static void bitmap_destruct(void *key, void *value)
77 {
78     BitmapHashValue *v = value;
79     if (v->bm)
80         ass_free_bitmap(v->bm);
81     if (v->bm_o)
82         ass_free_bitmap(v->bm_o);
83     if (v->bm_s)
84         ass_free_bitmap(v->bm_s);
85     free(key);
86     free(value);
87 }
88
89 static size_t bitmap_size(void *value, size_t value_size)
90 {
91     BitmapHashValue *val = value;
92     if (val->bm_o)
93         return val->bm_o->w * val->bm_o->h * 3;
94     else if (val->bm)
95         return val->bm->w * val->bm->h * 3;
96     return 0;
97 }
98
99 // composite cache
100 static void composite_destruct(void *key, void *value)
101 {
102     CompositeHashValue *v = value;
103     free(v->a);
104     free(v->b);
105     free(key);
106     free(value);
107 }
108
109 // outline cache
110
111 static unsigned outline_hash(void *key, size_t key_size)
112 {
113     OutlineHashKey *k = key;
114     switch (k->type) {
115         case OUTLINE_GLYPH: return glyph_hash(&k->u, key_size);
116         case OUTLINE_DRAWING: return drawing_hash(&k->u, key_size);
117         default: return 0;
118     }
119 }
120
121 static unsigned outline_compare(void *a, void *b, size_t key_size)
122 {
123     OutlineHashKey *ak = a;
124     OutlineHashKey *bk = b;
125     if (ak->type != bk->type) return 0;
126     switch (ak->type) {
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);
129         default: return 0;
130     }
131 }
132
133 static void outline_destruct(void *key, void *value)
134 {
135     OutlineHashValue *v = value;
136     OutlineHashKey *k = key;
137     if (v->outline)
138         outline_free(v->lib, v->outline);
139     if (v->border)
140         outline_free(v->lib, v->border);
141     if (k->type == OUTLINE_DRAWING)
142         free(k->u.drawing.text);
143     free(key);
144     free(value);
145 }
146
147
148
149 // Cache data
150 typedef struct cache_item {
151     void *key;
152     void *value;
153     struct cache_item *next;
154 } CacheItem;
155
156 struct cache {
157     unsigned buckets;
158     CacheItem **map;
159
160     HashFunction hash_func;
161     ItemSize size_func;
162     HashCompare compare_func;
163     CacheItemDestructor destruct_func;
164     size_t key_size;
165     size_t value_size;
166
167     size_t cache_size;
168     unsigned hits;
169     unsigned misses;
170     unsigned items;
171 };
172
173 // Hash for a simple (single value or array) type
174 static unsigned hash_simple(void *key, size_t key_size)
175 {
176     return fnv_32a_buf(key, key_size, FNV1_32A_INIT);
177 }
178
179 // Comparison of a simple type
180 static unsigned compare_simple(void *a, void *b, size_t key_size)
181 {
182     return memcmp(a, b, key_size) == 0;
183 }
184
185 // Default destructor
186 static void destruct_simple(void *key, void *value)
187 {
188     free(key);
189     free(value);
190 }
191
192
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)
197 {
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;
204     if (hash_func)
205         cache->hash_func = hash_func;
206     if (compare_func)
207         cache->compare_func = compare_func;
208     if (destruct_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 *));
213
214     return cache;
215 }
216
217 void *ass_cache_put(Cache *cache, void *key, void *value)
218 {
219     unsigned bucket = cache->hash_func(key, cache->key_size) % cache->buckets;
220     CacheItem **item = &cache->map[bucket];
221     while (*item)
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);
228
229     cache->items++;
230     if (cache->size_func)
231         cache->cache_size += cache->size_func(value, cache->value_size);
232
233     return (*item)->value;
234 }
235
236 void *ass_cache_get(Cache *cache, void *key)
237 {
238     unsigned bucket = cache->hash_func(key, cache->key_size) % cache->buckets;
239     CacheItem *item = cache->map[bucket];
240     while (item) {
241         if (cache->compare_func(key, item->key, cache->key_size)) {
242             cache->hits++;
243             return item->value;
244         }
245         item = item->next;
246     }
247     cache->misses++;
248     return NULL;
249 }
250
251 size_t ass_cache_empty(Cache *cache, size_t max_size)
252 {
253     int i;
254
255     if (cache->cache_size < max_size)
256         return cache->cache_size;
257
258     for (i = 0; i < cache->buckets; i++) {
259         CacheItem *item = cache->map[i];
260         while (item) {
261             CacheItem *next = item->next;
262             cache->destruct_func(item->key, item->value);
263             free(item);
264             item = next;
265         }
266         cache->map[i] = NULL;
267     }
268
269     cache->items = cache->hits = cache->misses = cache->cache_size = 0;
270
271     return 0;
272 }
273
274 void ass_cache_stats(Cache *cache, size_t *size, unsigned *hits,
275                      unsigned *misses, unsigned *count)
276 {
277     *size = cache->cache_size;
278     *hits = cache->hits;
279     *misses = cache->misses;
280     *count = cache->items;
281 }
282
283 void ass_cache_done(Cache *cache)
284 {
285     ass_cache_empty(cache, 0);
286     free(cache->map);
287     free(cache);
288 }
289
290 // Type-specific creation function
291 Cache *ass_font_cache_create(void)
292 {
293     return ass_cache_create(font_hash, font_compare, font_destruct,
294             (ItemSize)NULL, sizeof(ASS_FontDesc), sizeof(ASS_Font));
295 }
296
297 Cache *ass_outline_cache_create(void)
298 {
299     return ass_cache_create(outline_hash, outline_compare, outline_destruct,
300             NULL, sizeof(OutlineHashKey), sizeof(OutlineHashValue));
301 }
302
303 Cache *ass_bitmap_cache_create(void)
304 {
305     return ass_cache_create(bitmap_hash, bitmap_compare, bitmap_destruct,
306             bitmap_size, sizeof(BitmapHashKey), sizeof(BitmapHashValue));
307 }
308
309 Cache *ass_composite_cache_create(void)
310 {
311     return ass_cache_create(composite_hash, composite_compare,
312             composite_destruct, (ItemSize)NULL, sizeof(CompositeHashKey),
313             sizeof(CompositeHashValue));
314 }