1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
|
#include "fitz_base.h"
#include "fitz_tree.h"
#include "fitz_draw.h"
typedef struct fz_hash_s fz_hash;
typedef struct fz_key_s fz_key;
typedef struct fz_val_s fz_val;
struct fz_glyphcache_s
{
int slots;
int size;
fz_hash *hash;
fz_val *lru;
unsigned char *buffer;
int load;
int used;
};
struct fz_key_s
{
void *fid;
int a, b;
int c, d;
unsigned short cid;
unsigned char e, f;
};
struct fz_hash_s
{
fz_key key;
fz_val *val;
};
struct fz_val_s
{
fz_hash *ent;
unsigned char *samples;
short w, h, x, y;
int uses;
};
static unsigned int hashkey(fz_key *key)
{
unsigned char *s = (unsigned char*)key;
unsigned int hash = 0;
unsigned int i;
for (i = 0; i < sizeof(fz_key); i++)
{
hash += s[i];
hash += (hash << 10);
hash ^= (hash >> 6);
}
hash += (hash << 3);
hash ^= (hash >> 11);
hash += (hash << 15);
return hash;
}
fz_error
fz_newglyphcache(fz_glyphcache **arenap, int slots, int size)
{
fz_glyphcache *arena;
arena = *arenap = fz_malloc(sizeof(fz_glyphcache));
if (!arena)
return fz_rethrow(-1, "out of memory");
arena->slots = slots;
arena->size = size;
arena->hash = nil;
arena->lru = nil;
arena->buffer = nil;
arena->hash = fz_malloc(sizeof(fz_hash) * slots);
if (!arena->hash)
goto cleanup;
arena->lru = fz_malloc(sizeof(fz_val) * slots);
if (!arena->lru)
goto cleanup;
arena->buffer = fz_malloc(size);
if (!arena->buffer)
goto cleanup;
memset(arena->hash, 0, sizeof(fz_hash) * slots);
memset(arena->lru, 0, sizeof(fz_val) * slots);
memset(arena->buffer, 0, size);
arena->load = 0;
arena->used = 0;
return fz_okay;
cleanup:
fz_free(arena->hash);
fz_free(arena->lru);
fz_free(arena->buffer);
fz_free(arena);
return fz_rethrow(-1, "out of memory");
}
void
fz_dropglyphcache(fz_glyphcache *arena)
{
fz_free(arena->hash);
fz_free(arena->lru);
fz_free(arena->buffer);
fz_free(arena);
}
static int hokay = 0;
static int hcoll = 0;
static int hdist = 0;
static int coos = 0;
static int covf = 0;
static int ghits = 0;
static int gmisses = 0;
static fz_val *
hashfind(fz_glyphcache *arena, fz_key *key)
{
fz_hash *tab = arena->hash;
int pos = hashkey(key) % arena->slots;
while (1)
{
if (!tab[pos].val)
return nil;
if (memcmp(key, &tab[pos].key, sizeof (fz_key)) == 0)
return tab[pos].val;
pos = pos + 1;
if (pos == arena->slots)
pos = 0;
}
}
static void
hashinsert(fz_glyphcache *arena, fz_key *key, fz_val *val)
{
fz_hash *tab = arena->hash;
int pos = hashkey(key) % arena->slots;
int didcoll = 0;
while (1)
{
if (!tab[pos].val)
{
tab[pos].key = *key;
tab[pos].val = val;
tab[pos].val->ent = &tab[pos];
if (didcoll) hcoll ++; else hokay ++; hdist += didcoll;
return;
}
pos = pos + 1;
if (pos == arena->slots)
pos = 0;
didcoll ++;
}
}
/*
static void
hashremove(fz_glyphcache *arena, fz_key *key)
{
fz_hash *tab = arena->hash;
unsigned int pos = hashkey(key) % arena->slots;
unsigned int hole;
unsigned int look;
unsigned int code;
while (1)
{
if (!tab[pos].val)
return; // boo hoo! tried to remove non-existant key
if (memcmp(key, &tab[pos].key, sizeof (fz_key)) == 0)
{
tab[pos].val = nil;
hole = pos;
look = hole + 1;
if (look == arena->slots)
look = 0;
while (tab[look].val)
{
code = (hashkey(&tab[look].key) % arena->slots);
if ((code <= hole && hole < look) ||
(look < code && code <= hole) ||
(hole < look && look < code))
{
tab[hole] = tab[look];
tab[hole].val->ent = &tab[hole];
tab[look].val = nil;
hole = look;
}
look = look + 1;
if (look == arena->slots)
look = 0;
}
return;
}
pos = pos + 1;
if (pos == arena->slots)
pos = 0;
}
}
*/
void
fz_debugglyphcache(fz_glyphcache *arena)
{
printf("cache load %d / %d (%d / %d bytes)\n",
arena->load, arena->slots, arena->used, arena->size);
printf("no-colliders: %d colliders: %d\n", hokay, hcoll);
printf("avg dist: %d / %d: %g\n", hdist, hcoll, (double)hdist / hcoll);
printf("out-of-space evicts: %d\n", coos);
printf("out-of-hash evicts: %d\n", covf);
printf("hits = %d misses = %d ratio = %g\n", ghits, gmisses, (float)ghits / (ghits + gmisses));
/*
int i;
for (i = 0; i < arena->slots; i++)
{
if (!arena->hash[i].val)
printf("glyph % 4d: empty\n", i);
else {
fz_key *k = &arena->hash[i].key;
fz_val *b = arena->hash[i].val;
printf("glyph % 4d: %p %d [%g %g %g %g + %d %d] "
"-> [%dx%d %d,%d]\n", i,
k->fid, k->cid,
k->a / 65536.0,
k->b / 65536.0,
k->c / 65536.0,
k->d / 65536.0,
k->e, k->f,
b->w, b->h, b->x, b->y);
}
}
for (i = 0; i < arena->load; i++)
printf("lru %04d: glyph %d (%d)\n", i,
arena->lru[i].ent - arena->hash, arena->lru[i].uses);
*/
}
static void
bubble(fz_glyphcache *arena, int i)
{
fz_val tmp;
if (i == 0 || arena->load < 2)
return;
tmp = arena->lru[i - 1];
arena->lru[i - 1] = arena->lru[i];
arena->lru[i] = tmp;
arena->lru[i - 1].ent->val = &arena->lru[i - 1];
arena->lru[i].ent->val = &arena->lru[i];
}
/*
static void
evictlast(fz_glyphcache *arena)
{
fz_val *lru = arena->lru;
unsigned char *s, *e;
int i, k;
fz_key key;
if (arena->load == 0)
return;
k = arena->load - 1;
s = lru[k].samples;
e = s + lru[k].w * lru[k].h;
// pack buffer to fill hole
memmove(s, e, arena->buffer + arena->used - e);
memset(arena->buffer + arena->used - (e - s), 0, e - s);
arena->used -= e - s;
// update lru pointers
for (i = 0; i < k; i++) // XXX this is DOG slow! XXX
if (lru[i].samples >= e)
lru[i].samples -= e - s;
// remove hash entry
key = lru[k].ent->key;
hashremove(arena, &key);
arena->load --;
}
*/
static void
evictall(fz_glyphcache *arena)
{
memset(arena->hash, 0, sizeof(fz_hash) * arena->slots);
memset(arena->lru, 0, sizeof(fz_val) * arena->slots);
memset(arena->buffer, 0, arena->size);
arena->load = 0;
arena->used = 0;
}
fz_error
fz_renderglyph(fz_glyphcache *arena, fz_glyph *glyph, fz_font *font, int cid, fz_matrix ctm)
{
fz_error error;
fz_key key;
fz_val *val;
int size;
key.fid = font;
key.cid = cid;
key.a = ctm.a * 65536;
key.b = ctm.b * 65536;
key.c = ctm.c * 65536;
key.d = ctm.d * 65536;
key.e = (ctm.e - fz_floor(ctm.e)) * 256;
key.f = (ctm.f - fz_floor(ctm.f)) * 256;
val = hashfind(arena, &key);
if (val)
{
val->uses ++;
glyph->w = val->w;
glyph->h = val->h;
glyph->x = val->x;
glyph->y = val->y;
glyph->samples = val->samples;
bubble(arena, val - arena->lru);
ghits++;
return fz_okay;
}
gmisses++;
ctm.e = fz_floor(ctm.e) + key.e / 256.0;
ctm.f = fz_floor(ctm.f) + key.f / 256.0;
if (font->ftface)
{
error = fz_renderftglyph(glyph, font, cid, ctm);
if (error)
return error;
}
else if (font->t3procs)
{
error = fz_rendert3glyph(glyph, font, cid, ctm);
if (error)
return error;
}
else
{
return fz_throw("uninitialized font structure");
}
size = glyph->w * glyph->h;
if (size > arena->size / 6)
return fz_okay;
while (arena->load > arena->slots * 75 / 100)
{
covf ++;
evictall(arena);
}
while (arena->used + size >= arena->size)
{
coos ++;
evictall(arena);
}
val = &arena->lru[arena->load++];
val->uses = 0;
val->w = glyph->w;
val->h = glyph->h;
val->x = glyph->x;
val->y = glyph->y;
val->samples = arena->buffer + arena->used;
arena->used += size;
memcpy(val->samples, glyph->samples, glyph->w * glyph->h);
glyph->samples = val->samples;
hashinsert(arena, &key, val);
return fz_okay;
}
|