2 * Copyright (C) 2006 Evgeniy Stepanov <eugeni.stepanov@gmail.com>
4 * This file is part of libass.
6 * libass is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * libass is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with libass; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include FT_FREETYPE_H
30 #include "ass_utils.h"
32 #include "ass_fontconfig.h"
34 #include "ass_bitmap.h"
35 #include "ass_cache.h"
37 static unsigned hashmap_hash(void *buf
, size_t len
)
39 return fnv_32a_buf(buf
, len
, FNV1_32A_INIT
);
42 static int hashmap_key_compare(void *a
, void *b
, size_t size
)
44 return memcmp(a
, b
, size
) == 0;
47 static void hashmap_item_dtor(void *key
, size_t key_size
, void *value
,
54 Hashmap
*hashmap_init(ASS_Library
*library
, size_t key_size
,
55 size_t value_size
, int nbuckets
,
56 HashmapItemDtor item_dtor
,
57 HashmapKeyCompare key_compare
,
60 Hashmap
*map
= calloc(1, sizeof(Hashmap
));
61 map
->library
= library
;
62 map
->nbuckets
= nbuckets
;
63 map
->key_size
= key_size
;
64 map
->value_size
= value_size
;
65 map
->root
= calloc(nbuckets
, sizeof(hashmap_item_p
));
66 map
->item_dtor
= item_dtor
? item_dtor
: hashmap_item_dtor
;
67 map
->key_compare
= key_compare
? key_compare
: hashmap_key_compare
;
68 map
->hash
= hash
? hash
: hashmap_hash
;
72 void hashmap_done(Hashmap
*map
)
76 if (map
->count
> 0 || map
->hit_count
+ map
->miss_count
> 0)
77 ass_msg(map
->library
, MSGL_V
,
78 "cache statistics: \n total accesses: %d\n hits: %d\n "
79 "misses: %d\n object count: %d",
80 map
->hit_count
+ map
->miss_count
, map
->hit_count
,
81 map
->miss_count
, map
->count
);
83 for (i
= 0; i
< map
->nbuckets
; ++i
) {
84 HashmapItem
*item
= map
->root
[i
];
86 HashmapItem
*next
= item
->next
;
87 map
->item_dtor(item
->key
, map
->key_size
, item
->value
,
97 // does nothing if key already exists
98 void *hashmap_insert(Hashmap
*map
, void *key
, void *value
)
100 unsigned hash
= map
->hash(key
, map
->key_size
);
101 HashmapItem
**next
= map
->root
+ (hash
% map
->nbuckets
);
103 if (map
->key_compare(key
, (*next
)->key
, map
->key_size
))
104 return (*next
)->value
;
105 next
= &((*next
)->next
);
108 (*next
) = malloc(sizeof(HashmapItem
));
109 (*next
)->key
= malloc(map
->key_size
);
110 (*next
)->value
= malloc(map
->value_size
);
111 memcpy((*next
)->key
, key
, map
->key_size
);
112 memcpy((*next
)->value
, value
, map
->value_size
);
116 return (*next
)->value
;
119 void *hashmap_find(Hashmap
*map
, void *key
)
121 unsigned hash
= map
->hash(key
, map
->key_size
);
122 HashmapItem
*item
= map
->root
[hash
% map
->nbuckets
];
124 if (map
->key_compare(key
, item
->key
, map
->key_size
)) {
134 //---------------------------------
137 static unsigned font_desc_hash(void *buf
, size_t len
)
139 ASS_FontDesc
*desc
= buf
;
141 hval
= fnv_32a_str(desc
->family
, FNV1_32A_INIT
);
142 hval
= fnv_32a_buf(&desc
->bold
, sizeof(desc
->bold
), hval
);
143 hval
= fnv_32a_buf(&desc
->italic
, sizeof(desc
->italic
), hval
);
147 static int font_compare(void *key1
, void *key2
, size_t key_size
)
149 ASS_FontDesc
*a
= key1
;
150 ASS_FontDesc
*b
= key2
;
151 if (strcmp(a
->family
, b
->family
) != 0)
153 if (a
->bold
!= b
->bold
)
155 if (a
->italic
!= b
->italic
)
157 if (a
->treat_family_as_pattern
!= b
->treat_family_as_pattern
)
159 if (a
->vertical
!= b
->vertical
)
164 static void font_hash_dtor(void *key
, size_t key_size
, void *value
,
167 ass_font_free(value
);
171 ASS_Font
*ass_font_cache_find(Hashmap
*font_cache
,
174 return hashmap_find(font_cache
, desc
);
178 * \brief Add a face struct to cache.
179 * \param font font struct
181 void *ass_font_cache_add(Hashmap
*font_cache
, ASS_Font
*font
)
183 return hashmap_insert(font_cache
, &(font
->desc
), font
);
186 Hashmap
*ass_font_cache_init(ASS_Library
*library
)
189 font_cache
= hashmap_init(library
, sizeof(ASS_FontDesc
),
192 font_hash_dtor
, font_compare
, font_desc_hash
);
196 void ass_font_cache_done(Hashmap
*font_cache
)
198 hashmap_done(font_cache
);
202 // Create hash/compare functions for bitmap and glyph
203 #define CREATE_HASH_FUNCTIONS
204 #include "ass_cache_template.h"
205 #define CREATE_COMPARISON_FUNCTIONS
206 #include "ass_cache_template.h"
208 //---------------------------------
211 static void bitmap_hash_dtor(void *key
, size_t key_size
, void *value
,
214 BitmapHashValue
*v
= value
;
216 ass_free_bitmap(v
->bm
);
218 ass_free_bitmap(v
->bm_o
);
220 ass_free_bitmap(v
->bm_s
);
225 void *cache_add_bitmap(Hashmap
*bitmap_cache
, BitmapHashKey
*key
,
226 BitmapHashValue
*val
)
228 // Note: this is only an approximation
230 bitmap_cache
->cache_size
+= val
->bm_o
->w
* val
->bm_o
->h
* 3;
232 bitmap_cache
->cache_size
+= val
->bm
->w
* val
->bm
->h
* 3;
234 return hashmap_insert(bitmap_cache
, key
, val
);
238 * \brief Get a bitmap from bitmap cache.
239 * \param key hash key
240 * \return requested hash val or 0 if not found
242 BitmapHashValue
*cache_find_bitmap(Hashmap
*bitmap_cache
,
245 return hashmap_find(bitmap_cache
, key
);
248 Hashmap
*ass_bitmap_cache_init(ASS_Library
*library
)
250 Hashmap
*bitmap_cache
;
251 bitmap_cache
= hashmap_init(library
,
252 sizeof(BitmapHashKey
),
253 sizeof(BitmapHashValue
),
255 bitmap_hash_dtor
, bitmap_compare
,
260 void ass_bitmap_cache_done(Hashmap
*bitmap_cache
)
262 hashmap_done(bitmap_cache
);
265 Hashmap
*ass_bitmap_cache_reset(Hashmap
*bitmap_cache
)
267 ASS_Library
*lib
= bitmap_cache
->library
;
269 ass_bitmap_cache_done(bitmap_cache
);
270 return ass_bitmap_cache_init(lib
);
273 //---------------------------------
276 static void glyph_hash_dtor(void *key
, size_t key_size
, void *value
,
279 GlyphHashValue
*v
= value
;
281 FT_Done_Glyph(v
->glyph
);
282 if (v
->outline_glyph
)
283 FT_Done_Glyph(v
->outline_glyph
);
288 void *cache_add_glyph(Hashmap
*glyph_cache
, GlyphHashKey
*key
,
291 if (val
->glyph
&& val
->glyph
->format
== FT_GLYPH_FORMAT_BITMAP
) {
292 FT_Bitmap
*bitmap
= &((FT_BitmapGlyph
) val
->glyph
)->bitmap
;
293 glyph_cache
->cache_size
+= bitmap
->rows
* bitmap
->pitch
;
296 return hashmap_insert(glyph_cache
, key
, val
);
300 * \brief Get a glyph from glyph cache.
301 * \param key hash key
302 * \return requested hash val or 0 if not found
304 GlyphHashValue
*cache_find_glyph(Hashmap
*glyph_cache
,
307 return hashmap_find(glyph_cache
, key
);
310 Hashmap
*ass_glyph_cache_init(ASS_Library
*library
)
312 Hashmap
*glyph_cache
;
313 glyph_cache
= hashmap_init(library
, sizeof(GlyphHashKey
),
314 sizeof(GlyphHashValue
),
316 glyph_hash_dtor
, glyph_compare
, glyph_hash
);
320 void ass_glyph_cache_done(Hashmap
*glyph_cache
)
322 hashmap_done(glyph_cache
);
325 Hashmap
*ass_glyph_cache_reset(Hashmap
*glyph_cache
)
327 ASS_Library
*lib
= glyph_cache
->library
;
329 ass_glyph_cache_done(glyph_cache
);
330 return ass_glyph_cache_init(lib
);
334 //---------------------------------
337 static void composite_hash_dtor(void *key
, size_t key_size
, void *value
,
340 CompositeHashValue
*v
= value
;
347 void *cache_add_composite(Hashmap
*composite_cache
,
348 CompositeHashKey
*key
,
349 CompositeHashValue
*val
)
351 return hashmap_insert(composite_cache
, key
, val
);
355 * \brief Get a composite bitmap from composite cache.
356 * \param key hash key
357 * \return requested hash val or 0 if not found
359 CompositeHashValue
*cache_find_composite(Hashmap
*composite_cache
,
360 CompositeHashKey
*key
)
362 return hashmap_find(composite_cache
, key
);
365 Hashmap
*ass_composite_cache_init(ASS_Library
*library
)
367 Hashmap
*composite_cache
;
368 composite_cache
= hashmap_init(library
, sizeof(CompositeHashKey
),
369 sizeof(CompositeHashValue
),
371 composite_hash_dtor
, composite_compare
,
373 return composite_cache
;
376 void ass_composite_cache_done(Hashmap
*composite_cache
)
378 hashmap_done(composite_cache
);
381 Hashmap
*ass_composite_cache_reset(Hashmap
*composite_cache
)
383 ASS_Library
*lib
= composite_cache
->library
;
385 ass_composite_cache_done(composite_cache
);
386 return ass_composite_cache_init(lib
);