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
|
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <stdlib.h>
#include "xf86.h"
#include "fb.h"
#include "mipict.h"
#include "compat-api.h"
#include "glyph_cache.h"
#include "utils.h"
#define CACHE_PICTURE_SIZE 1024
#define GLYPH_MIN_SIZE 8
#define GLYPH_MAX_SIZE 64
#define GLYPH_RATIO_SIZE (GLYPH_MAX_SIZE / GLYPH_MIN_SIZE)
#define GLYPH_CACHE_SIZE \
(CACHE_PICTURE_SIZE * CACHE_PICTURE_SIZE / \
(GLYPH_MIN_SIZE * GLYPH_MIN_SIZE))
struct glyph_cache {
PicturePtr picture;
GlyphPtr *glyphs;
uint16_t count, evict;
glyph_upload_t upload;
};
struct glyph_cache_priv {
CloseScreenProcPtr CloseScreen;
unsigned num_caches;
struct glyph_cache cache[0];
};
struct glyph_priv {
struct glyph_cache *cache;
xPoint pos;
uint16_t size, index;
};
static DevPrivateKeyRec glyph_key;
static struct glyph_priv *glyph_get_priv(GlyphPtr glyph)
{
return dixGetPrivate(&glyph->devPrivates, &glyph_key);
}
static void glyph_set_priv(GlyphPtr glyph, struct glyph_priv *priv)
{
dixSetPrivate(&glyph->devPrivates, &glyph_key, priv);
}
static DevPrivateKeyRec glyph_cache_key;
static struct glyph_cache_priv *glyph_cache_get_priv(ScreenPtr pScreen)
{
return dixGetPrivate(&pScreen->devPrivates, &glyph_cache_key);
}
static void glyph_cache_set_priv(ScreenPtr pScreen,
struct glyph_cache_priv *priv)
{
dixSetPrivate(&pScreen->devPrivates, &glyph_cache_key, priv);
}
static PicturePtr create_picture(ScreenPtr pScreen, int width, int height,
int depth, PictFormatPtr pPictFormat, unsigned usage_hint)
{
PicturePtr picture;
PixmapPtr pixmap;
CARD32 component_alpha;
int error;
pixmap = pScreen->CreatePixmap(pScreen, width, height, depth,
usage_hint);
if (!pixmap)
return NULL;
component_alpha = NeedsComponent(pPictFormat->format);
picture = CreatePicture(0, &pixmap->drawable, pPictFormat,
CPComponentAlpha, &component_alpha,
serverClient, &error);
pScreen->DestroyPixmap(pixmap);
return picture;
}
static void glyph_cache_fini(ScreenPtr pScreen)
{
struct glyph_cache_priv *priv = glyph_cache_get_priv(pScreen);
unsigned i;
for (i = 0; i < priv->num_caches; i++) {
struct glyph_cache *cache = &priv->cache[i];
if (cache->picture)
FreePicture(cache->picture, 0);
if (cache->glyphs)
free(cache->glyphs);
}
glyph_cache_set_priv(pScreen, NULL);
free(priv);
}
static Bool glyph_cache_CloseScreen(CLOSE_SCREEN_ARGS_DECL)
{
struct glyph_cache_priv *priv = glyph_cache_get_priv(pScreen);
pScreen->CloseScreen = priv->CloseScreen;
glyph_cache_fini(pScreen);
return pScreen->CloseScreen(CLOSE_SCREEN_ARGS);
}
Bool glyph_cache_init(ScreenPtr pScreen, glyph_upload_t upload,
const unsigned *formats, size_t num_formats, unsigned usage_hint)
{
struct glyph_cache_priv *priv;
unsigned i;
size_t size;
if (!dixRegisterPrivateKey(&glyph_cache_key, PRIVATE_SCREEN, 0))
return FALSE;
if (!dixRegisterPrivateKey(&glyph_key, PRIVATE_GLYPH, 0))
return FALSE;
size = offsetof(struct glyph_cache_priv, cache[num_formats]);
priv = malloc(size);
if (!priv)
return FALSE;
memset(priv, 0, size);
priv->num_caches = num_formats;
glyph_cache_set_priv(pScreen, priv);
for (i = 0; i < priv->num_caches; i++) {
struct glyph_cache *cache = &priv->cache[i];
PictFormatPtr pPictFormat;
PicturePtr picture;
unsigned format = formats[i];
int depth = PIXMAN_FORMAT_DEPTH(format);
pPictFormat = PictureMatchFormat(pScreen, depth, format);
if (!pPictFormat)
goto fail;
picture = create_picture(pScreen, CACHE_PICTURE_SIZE,
CACHE_PICTURE_SIZE, depth,
pPictFormat, usage_hint);
if (!picture)
goto fail;
ValidatePicture(picture);
cache->picture = picture;
cache->glyphs = calloc(GLYPH_CACHE_SIZE, sizeof(*cache->glyphs));
if (!cache->glyphs)
goto fail;
cache->evict = rand() % GLYPH_CACHE_SIZE;
cache->upload = upload;
}
priv->CloseScreen = pScreen->CloseScreen;
pScreen->CloseScreen = glyph_cache_CloseScreen;
return TRUE;
fail:
glyph_cache_fini(pScreen);
return FALSE;
}
static unsigned glyph_size_to_count(unsigned size)
{
size /= GLYPH_MIN_SIZE;
return size * size;
}
static unsigned glyph_count_to_mask(unsigned count)
{
return ~(count - 1);
}
static unsigned glyph_size_to_mask(unsigned size)
{
return glyph_count_to_mask(glyph_size_to_count(size));
}
static struct glyph_cache *glyph_get_cache(ScreenPtr pScreen, GlyphPtr pGlyph)
{
PicturePtr pGlyphPicture;
struct glyph_cache_priv *priv = glyph_cache_get_priv(pScreen);
unsigned i;
if (!priv)
return NULL;
pGlyphPicture = GetGlyphPicture(pGlyph, pScreen);
for (i = 0; i < priv->num_caches; i++) {
struct glyph_cache *cache = &priv->cache[i];
if (PICT_FORMAT_RGB(cache->picture->format) ==
PICT_FORMAT_RGB(pGlyphPicture->format))
return cache;
}
return NULL;
}
static struct glyph_priv *__glyph_cache(ScreenPtr pScreen, GlyphPtr pGlyph)
{
struct glyph_cache *cache;
struct glyph_priv *priv;
unsigned size, sz, mask, count, index, i;
sz = pGlyph->info.width;
if (sz < pGlyph->info.height)
sz = pGlyph->info.height;
if (sz > GLYPH_MAX_SIZE)
return NULL;
cache = glyph_get_cache(pScreen, pGlyph);
if (!cache)
return NULL;
for (size = GLYPH_MIN_SIZE; size <= GLYPH_MAX_SIZE; size *= 2)
if (sz <= size)
break;
count = glyph_size_to_count(size);
mask = glyph_count_to_mask(count);
index = (cache->count + count - 1) & mask;
if (index < GLYPH_CACHE_SIZE) {
cache->count = index + count;
priv = NULL;
} else {
priv = NULL;
for (count = size; count <= GLYPH_MAX_SIZE; count *= 2) {
unsigned i = cache->evict & glyph_size_to_mask(count);
GlyphPtr evicted = cache->glyphs[i];
if (!evicted)
continue;
priv = glyph_get_priv(evicted);
if (priv->size >= count) {
glyph_set_priv(evicted, NULL);
cache->glyphs[i] = NULL;
index = cache->evict & glyph_size_to_mask(size);
} else {
priv = NULL;
}
break;
}
if (!priv) {
unsigned s;
count = glyph_size_to_count(size);
mask = glyph_count_to_mask(count);
index = cache->evict & mask;
for (s = 0; s < count; s++) {
GlyphPtr evicted = cache->glyphs[index + s];
if (!evicted)
continue;
priv = glyph_get_priv(evicted);
glyph_set_priv(evicted, NULL);
cache->glyphs[index + s] = NULL;
}
}
cache->evict = rand() % GLYPH_CACHE_SIZE;
}
if (!priv)
priv = malloc(sizeof(*priv));
if (!priv)
return NULL;
glyph_set_priv(pGlyph, priv);
cache->glyphs[index] = pGlyph;
priv->cache = cache;
priv->size = size;
priv->index = index;
i = index / (GLYPH_RATIO_SIZE * GLYPH_RATIO_SIZE);
priv->pos.x = i % (CACHE_PICTURE_SIZE / GLYPH_MAX_SIZE) * GLYPH_MAX_SIZE;
priv->pos.y = (i / (CACHE_PICTURE_SIZE / GLYPH_MAX_SIZE)) * GLYPH_MAX_SIZE;
for (i = GLYPH_MIN_SIZE; i < GLYPH_MAX_SIZE; i *= 2) {
if (index & 1)
priv->pos.x += i;
if (index & 2)
priv->pos.y += i;
index >>= 2;
}
cache->upload(pScreen, cache->picture, pGlyph,
GetGlyphPicture(pGlyph, pScreen),
priv->pos.x, priv->pos.y);
return priv;
}
PicturePtr glyph_cache_only(ScreenPtr pScreen, GlyphPtr pGlyph, xPoint *pos)
{
struct glyph_priv *priv;
priv = glyph_get_priv(pGlyph);
if (priv) {
*pos = priv->pos;
return priv->cache->picture;
}
return NULL;
}
PicturePtr glyph_cache(ScreenPtr pScreen, GlyphPtr pGlyph, xPoint *pos)
{
struct glyph_priv *priv;
priv = glyph_get_priv(pGlyph);
if (!priv)
priv = __glyph_cache(pScreen, pGlyph);
if (priv) {
*pos = priv->pos;
return priv->cache->picture;
}
pos->x = 0;
pos->y = 0;
return GetGlyphPicture(pGlyph, pScreen);
}
void glyph_cache_remove(ScreenPtr pScreen, GlyphPtr pGlyph)
{
struct glyph_priv *priv;
priv = glyph_get_priv(pGlyph);
if (priv) {
priv->cache->glyphs[priv->index] = NULL;
glyph_set_priv(pGlyph, NULL);
}
}
/* Pre-load glyphs into the glyph cache before we start rendering. */
Bool glyph_cache_preload(ScreenPtr pScreen, int nlist, GlyphListPtr list,
GlyphPtr *glyphs)
{
while (nlist--) {
int n = list->len;
while (n--) {
GlyphPtr glyph = *glyphs++;
if (glyph->info.width == 0 || glyph->info.height == 0)
continue;
if (glyph_get_priv(glyph))
continue;
if (!__glyph_cache(pScreen, glyph))
return FALSE;
}
list++;
}
return TRUE;
}
|