Mercurial > mplayer.hg
annotate libass/ass_cache.c @ 23017:f3b04984b0da
Rename glyph cache to bitmap cache.
author | eugeni |
---|---|
date | Fri, 20 Apr 2007 23:02:20 +0000 |
parents | a1ced37f2ee5 |
children | a4517aa83565 |
rev | line source |
---|---|
20008
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
1 // -*- c-basic-offset: 8; indent-tabs-mode: t -*- |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
2 // vim:ts=8:sw=8:noet:ai: |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
3 /* |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
4 Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com> |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
5 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
6 This program is free software; you can redistribute it and/or modify |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
7 it under the terms of the GNU General Public License as published by |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
8 the Free Software Foundation; either version 2 of the License, or |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
9 (at your option) any later version. |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
10 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
11 This program is distributed in the hope that it will be useful, |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
14 GNU General Public License for more details. |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
15 |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
16 You should have received a copy of the GNU General Public License |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
17 along with this program; if not, write to the Free Software |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
18 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
19 */ |
fa122b7c71c6
Add copyright notice and vim/emacs comments to libass and vf_ass.c.
eugeni
parents:
19965
diff
changeset
|
20 |
18937 | 21 #include "config.h" |
22 | |
22292 | 23 #include <inttypes.h> |
18937 | 24 #include <ft2build.h> |
25 #include FT_FREETYPE_H | |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19545
diff
changeset
|
26 #include FT_GLYPH_H |
18937 | 27 |
28 #include <assert.h> | |
29 | |
21026
d138463e820b
Collect all includes of mplayer headers in libass in a single file (mputils.h).
eugeni
parents:
20637
diff
changeset
|
30 #include "mputils.h" |
21458
7af6c25a0cfc
Keep embedded fonts in ass_library_t and perform actual disk write
eugeni
parents:
21348
diff
changeset
|
31 #include "ass.h" |
18937 | 32 #include "ass_fontconfig.h" |
21322 | 33 #include "ass_font.h" |
19846
bcc792bfa431
Store bitmap glyphs in a separate struct, instead of FreeType's internal buffer.
eugeni
parents:
19545
diff
changeset
|
34 #include "ass_bitmap.h" |
18937 | 35 #include "ass_cache.h" |
36 | |
23016 | 37 |
38 typedef struct hashmap_item_s { | |
39 void* key; | |
40 void* value; | |
41 struct hashmap_item_s* next; | |
42 } hashmap_item_t; | |
43 typedef hashmap_item_t* hashmap_item_p; | |
44 | |
45 struct hashmap_s { | |
46 int nbuckets; | |
47 size_t key_size, value_size; | |
48 hashmap_item_p* root; | |
49 int count; | |
50 hashmap_item_dtor_t item_dtor; // a destructor for hashmap key/value pairs | |
51 hashmap_key_compare_t key_compare; | |
52 hashmap_hash_t hash; | |
53 }; | |
54 | |
55 #define FNV1_32A_INIT (unsigned)0x811c9dc5 | |
56 | |
57 static inline unsigned fnv_32a_buf(void* buf, size_t len, unsigned hval) | |
58 { | |
59 unsigned char *bp = buf; | |
60 unsigned char *be = bp + len; | |
61 while (bp < be) { | |
62 hval ^= (unsigned)*bp++; | |
63 hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24); | |
64 } | |
65 return hval; | |
66 } | |
67 static inline unsigned fnv_32a_str(char* str, unsigned hval) | |
68 { | |
69 unsigned char* s = (unsigned char*)str; | |
70 while (*s) { | |
71 hval ^= (unsigned)*s++; | |
72 hval += (hval<<1) + (hval<<4) + (hval<<7) + (hval<<8) + (hval<<24); | |
73 } | |
74 return hval; | |
75 } | |
76 | |
77 static unsigned hashmap_hash(void* buf, size_t len) | |
78 { | |
79 return fnv_32a_buf(buf, len, FNV1_32A_INIT); | |
80 } | |
81 | |
82 static int hashmap_key_compare(void* a, void* b, size_t size) | |
83 { | |
84 return (memcmp(a, b, size) == 0); | |
85 } | |
86 | |
87 static void hashmap_item_dtor(void* key, size_t key_size, void* value, size_t value_size) | |
88 { | |
89 free(key); | |
90 free(value); | |
91 } | |
18937 | 92 |
23016 | 93 hashmap_t* hashmap_init(size_t key_size, size_t value_size, int nbuckets, |
94 hashmap_item_dtor_t item_dtor, hashmap_key_compare_t key_compare, | |
95 hashmap_hash_t hash) | |
96 { | |
97 hashmap_t* map = calloc(1, sizeof(hashmap_t)); | |
98 map->nbuckets = nbuckets; | |
99 map->key_size = key_size; | |
100 map->value_size = value_size; | |
101 map->count = 0; | |
102 map->root = calloc(nbuckets, sizeof(hashmap_item_p)); | |
103 map->item_dtor = item_dtor ? item_dtor : hashmap_item_dtor; | |
104 map->key_compare = key_compare ? key_compare : hashmap_key_compare; | |
105 map->hash = hash ? hash : hashmap_hash; | |
106 return map; | |
107 } | |
108 | |
109 void hashmap_done(hashmap_t* map) | |
110 { | |
111 int i; | |
112 for (i = 0; i < map->nbuckets; ++i) { | |
113 hashmap_item_t* item = map->root[i]; | |
114 while (item) { | |
115 hashmap_item_t* next = item->next; | |
116 map->item_dtor(item->key, map->key_size, item->value, map->value_size); | |
117 free(item); | |
118 item = next; | |
119 } | |
120 } | |
121 free(map->root); | |
122 free(map); | |
123 } | |
18937 | 124 |
23016 | 125 // does nothing if key already exists |
126 void hashmap_insert(hashmap_t* map, void* key, void* value) | |
127 { | |
128 unsigned hash = map->hash(key, map->key_size); | |
129 hashmap_item_t** next = map->root + (hash % map->nbuckets); | |
130 while (*next) { | |
131 if (map->key_compare(key, (*next)->key, map->key_size)) | |
132 return; | |
133 next = &((*next)->next); | |
134 assert(next); | |
135 } | |
136 (*next) = malloc(sizeof(hashmap_item_t)); | |
137 (*next)->key = malloc(map->key_size); | |
138 (*next)->value = malloc(map->value_size); | |
139 memcpy((*next)->key, key, map->key_size); | |
140 memcpy((*next)->value, value, map->value_size); | |
141 (*next)->next = 0; | |
142 | |
143 map->count ++; | |
144 } | |
145 | |
146 void* hashmap_find(hashmap_t* map, void* key) | |
147 { | |
148 unsigned hash = map->hash(key, map->key_size); | |
149 hashmap_item_t* item = map->root[hash % map->nbuckets]; | |
150 while (item) { | |
151 if (map->key_compare(key, item->key, map->key_size)) { | |
152 return item->value; | |
153 } | |
154 item = item->next; | |
155 } | |
156 return 0; | |
157 } | |
158 | |
159 //--------------------------------- | |
160 // font cache | |
161 | |
162 hashmap_t* font_cache; | |
163 | |
164 static unsigned font_desc_hash(void* buf, size_t len) | |
165 { | |
166 ass_font_desc_t* desc = buf; | |
167 unsigned hval; | |
168 hval = fnv_32a_str(desc->family, FNV1_32A_INIT); | |
169 hval = fnv_32a_buf(&desc->bold, sizeof(desc->bold), hval); | |
170 hval = fnv_32a_buf(&desc->italic, sizeof(desc->italic), hval); | |
171 return hval; | |
172 } | |
173 | |
174 static int font_compare(void* key1, void* key2, size_t key_size) { | |
175 ass_font_desc_t* a = key1; | |
176 ass_font_desc_t* b = key2; | |
18937 | 177 if (strcmp(a->family, b->family) != 0) |
178 return 0; | |
179 if (a->bold != b->bold) | |
180 return 0; | |
181 if (a->italic != b->italic) | |
182 return 0; | |
183 return 1; | |
184 } | |
185 | |
23016 | 186 static void font_hash_dtor(void* key, size_t key_size, void* value, size_t value_size) |
187 { | |
188 ass_font_free(value); | |
189 free(key); | |
190 } | |
191 | |
21317 | 192 ass_font_t* ass_font_cache_find(ass_font_desc_t* desc) |
18937 | 193 { |
23016 | 194 return hashmap_find(font_cache, desc); |
21317 | 195 } |
18937 | 196 |
21317 | 197 /** |
198 * \brief Add a face struct to cache. | |
199 * \param font font struct | |
200 */ | |
201 void ass_font_cache_add(ass_font_t* font) | |
202 { | |
23016 | 203 hashmap_insert(font_cache, &(font->desc), font); |
18937 | 204 } |
205 | |
21265 | 206 void ass_font_cache_init(void) |
18937 | 207 { |
23016 | 208 font_cache = hashmap_init(sizeof(ass_font_desc_t), |
209 sizeof(ass_font_t), | |
210 1000, | |
211 font_hash_dtor, font_compare, font_desc_hash); | |
18937 | 212 } |
213 | |
21265 | 214 void ass_font_cache_done(void) |
18937 | 215 { |
23016 | 216 hashmap_done(font_cache); |
18937 | 217 } |
218 | |
219 //--------------------------------- | |
23017 | 220 // bitmap cache |
18937 | 221 |
23017 | 222 hashmap_t* bitmap_cache; |
18937 | 223 |
23017 | 224 static void bitmap_hash_dtor(void* key, size_t key_size, void* value, size_t value_size) |
23016 | 225 { |
23017 | 226 bitmap_hash_val_t* v = value; |
23016 | 227 if (v->bm) ass_free_bitmap(v->bm); |
228 if (v->bm_o) ass_free_bitmap(v->bm_o); | |
229 if (v->bm_s) ass_free_bitmap(v->bm_s); | |
230 free(key); | |
231 free(value); | |
18937 | 232 } |
233 | |
23017 | 234 void cache_add_bitmap(bitmap_hash_key_t* key, bitmap_hash_val_t* val) |
18937 | 235 { |
23017 | 236 hashmap_insert(bitmap_cache, key, val); |
18937 | 237 } |
238 | |
239 /** | |
23017 | 240 * \brief Get a bitmap from bitmap cache. |
18937 | 241 * \param key hash key |
242 * \return requested hash val or 0 if not found | |
243 */ | |
23017 | 244 bitmap_hash_val_t* cache_find_bitmap(bitmap_hash_key_t* key) |
18937 | 245 { |
23017 | 246 return hashmap_find(bitmap_cache, key); |
18937 | 247 } |
248 | |
23017 | 249 void ass_bitmap_cache_init(void) |
18937 | 250 { |
23017 | 251 bitmap_cache = hashmap_init(sizeof(bitmap_hash_key_t), |
252 sizeof(bitmap_hash_val_t), | |
23016 | 253 0xFFFF + 13, |
23017 | 254 bitmap_hash_dtor, NULL, NULL); |
18937 | 255 } |
256 | |
23017 | 257 void ass_bitmap_cache_done(void) |
18937 | 258 { |
23017 | 259 hashmap_done(bitmap_cache); |
18937 | 260 } |
261 | |
23017 | 262 void ass_bitmap_cache_reset(void) |
19539 | 263 { |
23017 | 264 ass_bitmap_cache_done(); |
265 ass_bitmap_cache_init(); | |
19539 | 266 } |
267 |