]> granicus.if.org Git - libass/blob - libass/ass_cache.h
cache: switch to gradual cache clearing
[libass] / libass / ass_cache.h
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 #ifndef LIBASS_CACHE_H
21 #define LIBASS_CACHE_H
22
23 #include "ass.h"
24 #include "ass_font.h"
25 #include "ass_bitmap.h"
26
27 typedef struct cache Cache;
28
29 // cache values
30
31 typedef struct {
32     Bitmap *bm;               // the actual bitmaps
33     Bitmap *bm_o;
34 } BitmapHashValue;
35
36 typedef struct {
37     Bitmap *bm;
38     Bitmap *bm_o;
39     Bitmap *bm_s;
40 } CompositeHashValue;
41
42 typedef struct {
43     ASS_Outline *outline;
44     ASS_Outline *border;
45     FT_BBox bbox_scaled;        // bbox after scaling, but before rotation
46     FT_Vector advance;          // 26.6, advance distance to the next outline in line
47     int asc, desc;              // ascender/descender
48 } OutlineHashValue;
49
50 typedef struct {
51     FT_Glyph_Metrics metrics;
52 } GlyphMetricsHashValue;
53
54 // Create definitions for bitmap, outline and composite hash keys
55 #define CREATE_STRUCT_DEFINITIONS
56 #include "ass_cache_template.h"
57
58 // Type-specific function pointers
59 typedef unsigned(*HashFunction)(void *key, size_t key_size);
60 typedef size_t(*ItemSize)(void *value, size_t value_size);
61 typedef unsigned(*HashCompare)(void *a, void *b, size_t key_size);
62 typedef void(*CacheItemDestructor)(void *key, void *value);
63
64 // cache hash keys
65
66 typedef struct outline_hash_key {
67     enum {
68         OUTLINE_GLYPH,
69         OUTLINE_DRAWING,
70     } type;
71     union {
72         GlyphHashKey glyph;
73         DrawingHashKey drawing;
74     } u;
75 } OutlineHashKey;
76
77 typedef struct bitmap_hash_key {
78     enum {
79         BITMAP_OUTLINE,
80         BITMAP_CLIP,
81     } type;
82     union {
83         OutlineBitmapHashKey outline;
84         ClipMaskHashKey clip;
85     } u;
86 } BitmapHashKey;
87
88 typedef struct {
89     BitmapHashValue *image;
90     int x, y;
91 } BitmapRef;
92
93 enum {
94     FILTER_BORDER_STYLE_3 = 1,
95     FILTER_NONZERO_BORDER = 2,
96     FILTER_NONZERO_SHADOW = 4,
97     FILTER_DRAW_SHADOW    = 8,  // VSFilter compatibility
98 };
99
100 typedef struct {
101     FilterDesc filter;
102     size_t bitmap_count;
103     BitmapRef *bitmaps;
104 } CompositeHashKey;
105
106 Cache *ass_cache_create(HashFunction hash_func, HashCompare compare_func,
107                         CacheItemDestructor destruct_func, ItemSize size_func,
108                         size_t key_size, size_t value_size);
109 bool ass_cache_get(Cache *cache, void *key, void *value_ptr);
110 void *ass_cache_get_key(void *value);
111 void ass_cache_commit(void *value);
112 void ass_cache_cancel(void *value);
113 void ass_cache_inc_ref(void *value);
114 void ass_cache_dec_ref(void *value);
115 void ass_cache_cut(Cache *cache, size_t max_size);
116 void ass_cache_stats(Cache *cache, size_t *size, unsigned *hits,
117                      unsigned *misses, unsigned *count);
118 void ass_cache_empty(Cache *cache);
119 void ass_cache_done(Cache *cache);
120 Cache *ass_font_cache_create(void);
121 Cache *ass_outline_cache_create(void);
122 Cache *ass_glyph_metrics_cache_create(void);
123 Cache *ass_bitmap_cache_create(void);
124 Cache *ass_composite_cache_create(void);
125
126 #endif                          /* LIBASS_CACHE_H */