/work/workdir/UnpackedTarball/pixman/pixman/pixman-glyph.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright 2010, 2012, Soren Sandmann <sandmann@cs.au.dk> |
3 | | * Copyright 2010, 2011, 2012, Red Hat, Inc |
4 | | * |
5 | | * Permission is hereby granted, free of charge, to any person obtaining a |
6 | | * copy of this software and associated documentation files (the "Software"), |
7 | | * to deal in the Software without restriction, including without limitation |
8 | | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
9 | | * and/or sell copies of the Software, and to permit persons to whom the |
10 | | * Software is furnished to do so, subject to the following conditions: |
11 | | * |
12 | | * The above copyright notice and this permission notice (including the next |
13 | | * paragraph) shall be included in all copies or substantial portions of the |
14 | | * Software. |
15 | | * |
16 | | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
17 | | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
18 | | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
19 | | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
20 | | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
21 | | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
22 | | * DEALINGS IN THE SOFTWARE. |
23 | | * |
24 | | * Author: Soren Sandmann <sandmann@cs.au.dk> |
25 | | */ |
26 | | |
27 | | #ifdef HAVE_CONFIG_H |
28 | | #include <pixman-config.h> |
29 | | #endif |
30 | | #include "pixman-private.h" |
31 | | |
32 | | #include <stdlib.h> |
33 | | |
34 | | typedef struct glyph_metrics_t glyph_metrics_t; |
35 | | typedef struct glyph_t glyph_t; |
36 | | |
37 | 2.79M | #define TOMBSTONE ((glyph_t *)0x1) |
38 | | |
39 | | /* XXX: These numbers are arbitrary---we've never done any measurements. |
40 | | */ |
41 | 72.3k | #define N_PIXELS_HIGH_WATER (4 * 1024 * 1024) |
42 | 1.89M | #define N_GLYPHS_HIGH_WATER (16384) |
43 | 135k | #define N_GLYPHS_LOW_WATER (8192) |
44 | 1.89M | #define HASH_SIZE (2 * N_GLYPHS_HIGH_WATER) |
45 | 1.82M | #define HASH_MASK (HASH_SIZE - 1) |
46 | | |
47 | | struct glyph_t |
48 | | { |
49 | | void * font_key; |
50 | | void * glyph_key; |
51 | | int origin_x; |
52 | | int origin_y; |
53 | | pixman_image_t * image; |
54 | | pixman_link_t mru_link; |
55 | | }; |
56 | | |
57 | | struct pixman_glyph_cache_t |
58 | | { |
59 | | int n_glyphs; |
60 | | int n_tombstones; |
61 | | int freeze_count; |
62 | | long n_pixels; |
63 | | pixman_list_t mru; |
64 | | glyph_t * glyphs[HASH_SIZE]; |
65 | | }; |
66 | | |
67 | | static void |
68 | | free_glyph (glyph_t *glyph) |
69 | 68.2k | { |
70 | 68.2k | pixman_list_unlink (&glyph->mru_link); |
71 | 68.2k | pixman_image_unref (glyph->image); |
72 | 68.2k | free (glyph); |
73 | 68.2k | } |
74 | | |
75 | | static unsigned int |
76 | | hash (const void *font_key, const void *glyph_key) |
77 | 1.47M | { |
78 | 1.47M | size_t key = (size_t)font_key + (size_t)glyph_key; |
79 | | |
80 | | /* This hash function is based on one found on Thomas Wang's |
81 | | * web page at |
82 | | * |
83 | | * http://www.concentric.net/~Ttwang/tech/inthash.htm |
84 | | * |
85 | | */ |
86 | 1.47M | key = (key << 15) - key - 1; |
87 | 1.47M | key = key ^ (key >> 12); |
88 | 1.47M | key = key + (key << 2); |
89 | 1.47M | key = key ^ (key >> 4); |
90 | 1.47M | key = key + (key << 3) + (key << 11); |
91 | 1.47M | key = key ^ (key >> 16); |
92 | | |
93 | 1.47M | return key; |
94 | 1.47M | } |
95 | | |
96 | | static glyph_t * |
97 | | lookup_glyph (pixman_glyph_cache_t *cache, |
98 | | void *font_key, |
99 | | void *glyph_key) |
100 | 1.33M | { |
101 | 1.33M | unsigned idx; |
102 | 1.33M | glyph_t *g; |
103 | | |
104 | 1.33M | idx = hash (font_key, glyph_key); |
105 | 1.34M | while ((g = cache->glyphs[idx++ & HASH_MASK])) |
106 | 1.25M | { |
107 | 1.25M | if (g != TOMBSTONE && |
108 | 1.25M | g->font_key == font_key && |
109 | 1.25M | g->glyph_key == glyph_key) |
110 | 1.25M | { |
111 | 1.25M | return g; |
112 | 1.25M | } |
113 | 1.25M | } |
114 | | |
115 | 84.3k | return NULL; |
116 | 1.33M | } |
117 | | |
118 | | static void |
119 | | insert_glyph (pixman_glyph_cache_t *cache, |
120 | | glyph_t *glyph) |
121 | 68.2k | { |
122 | 68.2k | unsigned idx; |
123 | 68.2k | glyph_t **loc; |
124 | | |
125 | 68.2k | idx = hash (glyph->font_key, glyph->glyph_key); |
126 | | |
127 | | /* Note: we assume that there is room in the table. If there isn't, |
128 | | * this will be an infinite loop. |
129 | | */ |
130 | 68.2k | do |
131 | 69.7k | { |
132 | 69.7k | loc = &cache->glyphs[idx++ & HASH_MASK]; |
133 | 69.7k | } while (*loc && *loc != TOMBSTONE); |
134 | | |
135 | 68.2k | if (*loc == TOMBSTONE) |
136 | 245 | cache->n_tombstones--; |
137 | 68.2k | cache->n_glyphs++; |
138 | 68.2k | cache->n_pixels += glyph->image->bits.width * glyph->image->bits.height; |
139 | | |
140 | 68.2k | *loc = glyph; |
141 | 68.2k | } |
142 | | |
143 | | static void |
144 | | remove_glyph (pixman_glyph_cache_t *cache, |
145 | | glyph_t *glyph) |
146 | 68.2k | { |
147 | 68.2k | unsigned idx; |
148 | | |
149 | 68.2k | idx = hash (glyph->font_key, glyph->glyph_key); |
150 | 69.7k | while (cache->glyphs[idx & HASH_MASK] != glyph) |
151 | 1.51k | idx++; |
152 | | |
153 | 68.2k | cache->glyphs[idx & HASH_MASK] = TOMBSTONE; |
154 | 68.2k | cache->n_tombstones++; |
155 | 68.2k | cache->n_glyphs--; |
156 | 68.2k | cache->n_pixels -= glyph->image->bits.width * glyph->image->bits.height; |
157 | | |
158 | | /* Eliminate tombstones if possible */ |
159 | 68.2k | if (cache->glyphs[(idx + 1) & HASH_MASK] == NULL) |
160 | 65.4k | { |
161 | 133k | while (cache->glyphs[idx & HASH_MASK] == TOMBSTONE) |
162 | 68.0k | { |
163 | 68.0k | cache->glyphs[idx & HASH_MASK] = NULL; |
164 | 68.0k | cache->n_tombstones--; |
165 | 68.0k | idx--; |
166 | 68.0k | } |
167 | 65.4k | } |
168 | 68.2k | } |
169 | | |
170 | | static void |
171 | | clear_table (pixman_glyph_cache_t *cache) |
172 | 0 | { |
173 | 0 | int i; |
174 | |
|
175 | 0 | for (i = 0; i < HASH_SIZE; ++i) |
176 | 0 | { |
177 | 0 | glyph_t *glyph = cache->glyphs[i]; |
178 | |
|
179 | 0 | if (glyph && glyph != TOMBSTONE) |
180 | 0 | free_glyph (glyph); |
181 | |
|
182 | 0 | cache->glyphs[i] = NULL; |
183 | 0 | } |
184 | |
|
185 | 0 | cache->n_glyphs = 0; |
186 | 0 | cache->n_tombstones = 0; |
187 | 0 | cache->n_pixels = 0; |
188 | 0 | } |
189 | | |
190 | | PIXMAN_EXPORT pixman_glyph_cache_t * |
191 | | pixman_glyph_cache_create (void) |
192 | 1 | { |
193 | 1 | pixman_glyph_cache_t *cache; |
194 | | |
195 | 1 | if (!(cache = malloc (sizeof *cache))) |
196 | 0 | return NULL; |
197 | | |
198 | 1 | memset (cache->glyphs, 0, sizeof (cache->glyphs)); |
199 | 1 | cache->n_glyphs = 0; |
200 | 1 | cache->n_tombstones = 0; |
201 | 1 | cache->freeze_count = 0; |
202 | 1 | cache->n_pixels = 0; |
203 | | |
204 | 1 | pixman_list_init (&cache->mru); |
205 | | |
206 | 1 | return cache; |
207 | 1 | } |
208 | | |
209 | | PIXMAN_EXPORT void |
210 | | pixman_glyph_cache_destroy (pixman_glyph_cache_t *cache) |
211 | 0 | { |
212 | 0 | return_if_fail (cache->freeze_count == 0); |
213 | | |
214 | 0 | clear_table (cache); |
215 | |
|
216 | 0 | free (cache); |
217 | 0 | } |
218 | | |
219 | | PIXMAN_EXPORT void |
220 | | pixman_glyph_cache_freeze (pixman_glyph_cache_t *cache) |
221 | 4.54k | { |
222 | 4.54k | cache->freeze_count++; |
223 | 4.54k | } |
224 | | |
225 | | PIXMAN_EXPORT void |
226 | | pixman_glyph_cache_thaw (pixman_glyph_cache_t *cache) |
227 | 4.54k | { |
228 | 4.54k | if (--cache->freeze_count == 0 && |
229 | 4.54k | (cache->n_glyphs + cache->n_tombstones > N_GLYPHS_HIGH_WATER || cache->n_pixels > N_PIXELS_HIGH_WATER)) |
230 | 130 | { |
231 | 130 | if (cache->n_tombstones > N_GLYPHS_LOW_WATER) |
232 | 0 | { |
233 | | /* More than half the entries are |
234 | | * tombstones. Just dump the whole table. |
235 | | */ |
236 | 0 | clear_table (cache); |
237 | 0 | } |
238 | | |
239 | 67.7k | while (cache->n_glyphs > N_GLYPHS_LOW_WATER || cache->n_pixels > N_PIXELS_HIGH_WATER) |
240 | 67.6k | { |
241 | 67.6k | glyph_t *glyph = CONTAINER_OF (glyph_t, mru_link, cache->mru.tail); |
242 | | |
243 | 67.6k | remove_glyph (cache, glyph); |
244 | 67.6k | free_glyph (glyph); |
245 | 67.6k | } |
246 | 130 | } |
247 | 4.54k | } |
248 | | |
249 | | PIXMAN_EXPORT const void * |
250 | | pixman_glyph_cache_lookup (pixman_glyph_cache_t *cache, |
251 | | void *font_key, |
252 | | void *glyph_key) |
253 | 1.31M | { |
254 | 1.31M | return lookup_glyph (cache, font_key, glyph_key); |
255 | 1.31M | } |
256 | | |
257 | | PIXMAN_EXPORT const void * |
258 | | pixman_glyph_cache_insert (pixman_glyph_cache_t *cache, |
259 | | void *font_key, |
260 | | void *glyph_key, |
261 | | int origin_x, |
262 | | int origin_y, |
263 | | pixman_image_t *image) |
264 | 68.2k | { |
265 | 68.2k | glyph_t *glyph; |
266 | 68.2k | int32_t width, height; |
267 | | |
268 | 68.2k | return_val_if_fail (cache->freeze_count > 0, NULL); |
269 | 68.2k | return_val_if_fail (image->type == BITS, NULL); |
270 | | |
271 | 68.2k | width = image->bits.width; |
272 | 68.2k | height = image->bits.height; |
273 | | |
274 | 68.2k | if (cache->n_glyphs >= HASH_SIZE) |
275 | 0 | return NULL; |
276 | | |
277 | 68.2k | if (!(glyph = malloc (sizeof *glyph))) |
278 | 0 | return NULL; |
279 | | |
280 | 68.2k | glyph->font_key = font_key; |
281 | 68.2k | glyph->glyph_key = glyph_key; |
282 | 68.2k | glyph->origin_x = origin_x; |
283 | 68.2k | glyph->origin_y = origin_y; |
284 | | |
285 | 68.2k | if (!(glyph->image = pixman_image_create_bits ( |
286 | 68.2k | image->bits.format, width, height, NULL, -1))) |
287 | 0 | { |
288 | 0 | free (glyph); |
289 | 0 | return NULL; |
290 | 0 | } |
291 | | |
292 | 68.2k | pixman_image_composite32 (PIXMAN_OP_SRC, |
293 | 68.2k | image, NULL, glyph->image, 0, 0, 0, 0, 0, 0, |
294 | 68.2k | width, height); |
295 | | |
296 | 68.2k | if (PIXMAN_FORMAT_A (glyph->image->bits.format) != 0 && |
297 | 68.2k | PIXMAN_FORMAT_RGB (glyph->image->bits.format) != 0) |
298 | 0 | { |
299 | 0 | pixman_image_set_component_alpha (glyph->image, TRUE); |
300 | 0 | } |
301 | | |
302 | 68.2k | pixman_list_prepend (&cache->mru, &glyph->mru_link); |
303 | | |
304 | 68.2k | _pixman_image_validate (glyph->image); |
305 | 68.2k | insert_glyph (cache, glyph); |
306 | | |
307 | 68.2k | return glyph; |
308 | 68.2k | } |
309 | | |
310 | | PIXMAN_EXPORT void |
311 | | pixman_glyph_cache_remove (pixman_glyph_cache_t *cache, |
312 | | void *font_key, |
313 | | void *glyph_key) |
314 | 16.7k | { |
315 | 16.7k | glyph_t *glyph; |
316 | | |
317 | 16.7k | if ((glyph = lookup_glyph (cache, font_key, glyph_key))) |
318 | 599 | { |
319 | 599 | remove_glyph (cache, glyph); |
320 | | |
321 | 599 | free_glyph (glyph); |
322 | 599 | } |
323 | 16.7k | } |
324 | | |
325 | | PIXMAN_EXPORT void |
326 | | pixman_glyph_get_extents (pixman_glyph_cache_t *cache, |
327 | | int n_glyphs, |
328 | | pixman_glyph_t *glyphs, |
329 | | pixman_box32_t *extents) |
330 | 0 | { |
331 | 0 | int i; |
332 | |
|
333 | 0 | extents->x1 = extents->y1 = INT32_MAX; |
334 | 0 | extents->x2 = extents->y2 = INT32_MIN; |
335 | |
|
336 | 0 | for (i = 0; i < n_glyphs; ++i) |
337 | 0 | { |
338 | 0 | glyph_t *glyph = (glyph_t *)glyphs[i].glyph; |
339 | 0 | int x1, y1, x2, y2; |
340 | |
|
341 | 0 | x1 = glyphs[i].x - glyph->origin_x; |
342 | 0 | y1 = glyphs[i].y - glyph->origin_y; |
343 | 0 | x2 = glyphs[i].x - glyph->origin_x + glyph->image->bits.width; |
344 | 0 | y2 = glyphs[i].y - glyph->origin_y + glyph->image->bits.height; |
345 | |
|
346 | 0 | if (x1 < extents->x1) |
347 | 0 | extents->x1 = x1; |
348 | 0 | if (y1 < extents->y1) |
349 | 0 | extents->y1 = y1; |
350 | 0 | if (x2 > extents->x2) |
351 | 0 | extents->x2 = x2; |
352 | 0 | if (y2 > extents->y2) |
353 | 0 | extents->y2 = y2; |
354 | 0 | } |
355 | 0 | } |
356 | | |
357 | | /* This function returns a format that is suitable for use as a mask for the |
358 | | * set of glyphs in question. |
359 | | */ |
360 | | PIXMAN_EXPORT pixman_format_code_t |
361 | | pixman_glyph_get_mask_format (pixman_glyph_cache_t *cache, |
362 | | int n_glyphs, |
363 | | const pixman_glyph_t *glyphs) |
364 | 1.30k | { |
365 | 1.30k | pixman_format_code_t format = PIXMAN_a1; |
366 | 1.30k | int i; |
367 | | |
368 | 1.31M | for (i = 0; i < n_glyphs; ++i) |
369 | 1.31M | { |
370 | 1.31M | const glyph_t *glyph = glyphs[i].glyph; |
371 | 1.31M | pixman_format_code_t glyph_format = glyph->image->bits.format; |
372 | | |
373 | 1.31M | if (PIXMAN_FORMAT_TYPE (glyph_format) == PIXMAN_TYPE_A) |
374 | 1.31M | { |
375 | 1.31M | if (PIXMAN_FORMAT_A (glyph_format) > PIXMAN_FORMAT_A (format)) |
376 | 1.30k | format = glyph_format; |
377 | 1.31M | } |
378 | 0 | else |
379 | 0 | { |
380 | 0 | return PIXMAN_a8r8g8b8; |
381 | 0 | } |
382 | 1.31M | } |
383 | | |
384 | 1.30k | return format; |
385 | 1.30k | } |
386 | | |
387 | | static pixman_bool_t |
388 | | box32_intersect (pixman_box32_t *dest, |
389 | | const pixman_box32_t *box1, |
390 | | const pixman_box32_t *box2) |
391 | 1.31M | { |
392 | 1.31M | dest->x1 = MAX (box1->x1, box2->x1); |
393 | 1.31M | dest->y1 = MAX (box1->y1, box2->y1); |
394 | 1.31M | dest->x2 = MIN (box1->x2, box2->x2); |
395 | 1.31M | dest->y2 = MIN (box1->y2, box2->y2); |
396 | | |
397 | 1.31M | return dest->x2 > dest->x1 && dest->y2 > dest->y1; |
398 | 1.31M | } |
399 | | |
400 | | #if defined(__GNUC__) && defined(__i386__) && !defined(__x86_64__) && \ |
401 | | !defined(__amd64__) |
402 | | __attribute__((__force_align_arg_pointer__)) |
403 | | #endif |
404 | | PIXMAN_EXPORT void |
405 | | pixman_composite_glyphs_no_mask (pixman_op_t op, |
406 | | pixman_image_t *src, |
407 | | pixman_image_t *dest, |
408 | | int32_t src_x, |
409 | | int32_t src_y, |
410 | | int32_t dest_x, |
411 | | int32_t dest_y, |
412 | | pixman_glyph_cache_t *cache, |
413 | | int n_glyphs, |
414 | | const pixman_glyph_t *glyphs) |
415 | 3.23k | { |
416 | 3.23k | pixman_region32_t region; |
417 | 3.23k | pixman_format_code_t glyph_format = PIXMAN_null; |
418 | 3.23k | uint32_t glyph_flags = 0; |
419 | 3.23k | pixman_format_code_t dest_format; |
420 | 3.23k | uint32_t dest_flags; |
421 | 3.23k | pixman_composite_func_t func = NULL; |
422 | 3.23k | pixman_implementation_t *implementation = NULL; |
423 | 3.23k | pixman_composite_info_t info; |
424 | 3.23k | int i; |
425 | | |
426 | 3.23k | _pixman_image_validate (src); |
427 | 3.23k | _pixman_image_validate (dest); |
428 | | |
429 | 3.23k | dest_format = dest->common.extended_format_code; |
430 | 3.23k | dest_flags = dest->common.flags; |
431 | | |
432 | 3.23k | pixman_region32_init (®ion); |
433 | 3.23k | if (!_pixman_compute_composite_region32 ( |
434 | 3.23k | ®ion, |
435 | 3.23k | src, NULL, dest, |
436 | 3.23k | src_x - dest_x, src_y - dest_y, 0, 0, 0, 0, |
437 | 3.23k | dest->bits.width, dest->bits.height)) |
438 | 0 | { |
439 | 0 | goto out; |
440 | 0 | } |
441 | | |
442 | 3.23k | info.op = op; |
443 | 3.23k | info.src_image = src; |
444 | 3.23k | info.dest_image = dest; |
445 | 3.23k | info.src_flags = src->common.flags; |
446 | 3.23k | info.dest_flags = dest->common.flags; |
447 | | |
448 | 7.79k | for (i = 0; i < n_glyphs; ++i) |
449 | 4.56k | { |
450 | 4.56k | glyph_t *glyph = (glyph_t *)glyphs[i].glyph; |
451 | 4.56k | pixman_image_t *glyph_img = glyph->image; |
452 | 4.56k | pixman_box32_t glyph_box; |
453 | 4.56k | pixman_box32_t *pbox; |
454 | 4.56k | uint32_t extra = FAST_PATH_SAMPLES_COVER_CLIP_NEAREST; |
455 | 4.56k | pixman_box32_t composite_box; |
456 | 4.56k | int n; |
457 | | |
458 | 4.56k | glyph_box.x1 = dest_x + glyphs[i].x - glyph->origin_x; |
459 | 4.56k | glyph_box.y1 = dest_y + glyphs[i].y - glyph->origin_y; |
460 | 4.56k | glyph_box.x2 = glyph_box.x1 + glyph->image->bits.width; |
461 | 4.56k | glyph_box.y2 = glyph_box.y1 + glyph->image->bits.height; |
462 | | |
463 | 4.56k | pbox = pixman_region32_rectangles (®ion, &n); |
464 | | |
465 | 4.56k | info.mask_image = glyph_img; |
466 | | |
467 | 9.13k | while (n--) |
468 | 4.56k | { |
469 | 4.56k | if (box32_intersect (&composite_box, pbox, &glyph_box)) |
470 | 3.04k | { |
471 | 3.04k | if (glyph_img->common.extended_format_code != glyph_format || |
472 | 0 | glyph_img->common.flags != glyph_flags) |
473 | 3.04k | { |
474 | 3.04k | glyph_format = glyph_img->common.extended_format_code; |
475 | 3.04k | glyph_flags = glyph_img->common.flags; |
476 | | |
477 | 3.04k | _pixman_implementation_lookup_composite ( |
478 | 3.04k | get_implementation(), op, |
479 | 3.04k | src->common.extended_format_code, src->common.flags, |
480 | 3.04k | glyph_format, glyph_flags | extra, |
481 | 3.04k | dest_format, dest_flags, |
482 | 3.04k | &implementation, &func); |
483 | 3.04k | } |
484 | | |
485 | 3.04k | info.src_x = src_x + composite_box.x1 - dest_x; |
486 | 3.04k | info.src_y = src_y + composite_box.y1 - dest_y; |
487 | 3.04k | info.mask_x = composite_box.x1 - (dest_x + glyphs[i].x - glyph->origin_x); |
488 | 3.04k | info.mask_y = composite_box.y1 - (dest_y + glyphs[i].y - glyph->origin_y); |
489 | 3.04k | info.dest_x = composite_box.x1; |
490 | 3.04k | info.dest_y = composite_box.y1; |
491 | 3.04k | info.width = composite_box.x2 - composite_box.x1; |
492 | 3.04k | info.height = composite_box.y2 - composite_box.y1; |
493 | | |
494 | 3.04k | info.mask_flags = glyph_flags; |
495 | | |
496 | 3.04k | func (implementation, &info); |
497 | 3.04k | } |
498 | | |
499 | 4.56k | pbox++; |
500 | 4.56k | } |
501 | 4.56k | pixman_list_move_to_front (&cache->mru, &glyph->mru_link); |
502 | 4.56k | } |
503 | | |
504 | 3.23k | out: |
505 | 3.23k | pixman_region32_fini (®ion); |
506 | 3.23k | } |
507 | | |
508 | | static void |
509 | | add_glyphs (pixman_glyph_cache_t *cache, |
510 | | pixman_image_t *dest, |
511 | | int off_x, int off_y, |
512 | | int n_glyphs, const pixman_glyph_t *glyphs) |
513 | 1.30k | { |
514 | 1.30k | pixman_format_code_t glyph_format = PIXMAN_null; |
515 | 1.30k | uint32_t glyph_flags = 0; |
516 | 1.30k | pixman_composite_func_t func = NULL; |
517 | 1.30k | pixman_implementation_t *implementation = NULL; |
518 | 1.30k | pixman_format_code_t dest_format; |
519 | 1.30k | uint32_t dest_flags; |
520 | 1.30k | pixman_box32_t dest_box; |
521 | 1.30k | pixman_composite_info_t info; |
522 | 1.30k | pixman_image_t *white_img = NULL; |
523 | 1.30k | pixman_bool_t white_src = FALSE; |
524 | 1.30k | int i; |
525 | | |
526 | 1.30k | _pixman_image_validate (dest); |
527 | | |
528 | 1.30k | dest_format = dest->common.extended_format_code; |
529 | 1.30k | dest_flags = dest->common.flags; |
530 | | |
531 | 1.30k | info.op = PIXMAN_OP_ADD; |
532 | 1.30k | info.dest_image = dest; |
533 | 1.30k | info.src_x = 0; |
534 | 1.30k | info.src_y = 0; |
535 | 1.30k | info.dest_flags = dest_flags; |
536 | | |
537 | 1.30k | dest_box.x1 = 0; |
538 | 1.30k | dest_box.y1 = 0; |
539 | 1.30k | dest_box.x2 = dest->bits.width; |
540 | 1.30k | dest_box.y2 = dest->bits.height; |
541 | | |
542 | 1.31M | for (i = 0; i < n_glyphs; ++i) |
543 | 1.31M | { |
544 | 1.31M | glyph_t *glyph = (glyph_t *)glyphs[i].glyph; |
545 | 1.31M | pixman_image_t *glyph_img = glyph->image; |
546 | 1.31M | pixman_box32_t glyph_box; |
547 | 1.31M | pixman_box32_t composite_box; |
548 | | |
549 | 1.31M | if (glyph_img->common.extended_format_code != glyph_format || |
550 | 1.31M | glyph_img->common.flags != glyph_flags) |
551 | 1.30k | { |
552 | 1.30k | pixman_format_code_t src_format, mask_format; |
553 | | |
554 | 1.30k | glyph_format = glyph_img->common.extended_format_code; |
555 | 1.30k | glyph_flags = glyph_img->common.flags; |
556 | | |
557 | 1.30k | if (glyph_format == dest->bits.format) |
558 | 1.30k | { |
559 | 1.30k | src_format = glyph_format; |
560 | 1.30k | mask_format = PIXMAN_null; |
561 | 1.30k | info.src_flags = glyph_flags | FAST_PATH_SAMPLES_COVER_CLIP_NEAREST; |
562 | 1.30k | info.mask_flags = FAST_PATH_IS_OPAQUE; |
563 | 1.30k | info.mask_image = NULL; |
564 | 1.30k | white_src = FALSE; |
565 | 1.30k | } |
566 | 0 | else |
567 | 0 | { |
568 | 0 | if (!white_img) |
569 | 0 | { |
570 | 0 | static const pixman_color_t white = { 0xffff, 0xffff, 0xffff, 0xffff }; |
571 | |
|
572 | 0 | if (!(white_img = pixman_image_create_solid_fill (&white))) |
573 | 0 | goto out; |
574 | | |
575 | 0 | _pixman_image_validate (white_img); |
576 | 0 | } |
577 | | |
578 | 0 | src_format = PIXMAN_solid; |
579 | 0 | mask_format = glyph_format; |
580 | 0 | info.src_flags = white_img->common.flags; |
581 | 0 | info.mask_flags = glyph_flags | FAST_PATH_SAMPLES_COVER_CLIP_NEAREST; |
582 | 0 | info.src_image = white_img; |
583 | 0 | white_src = TRUE; |
584 | 0 | } |
585 | | |
586 | 1.30k | _pixman_implementation_lookup_composite ( |
587 | 1.30k | get_implementation(), PIXMAN_OP_ADD, |
588 | 1.30k | src_format, info.src_flags, |
589 | 1.30k | mask_format, info.mask_flags, |
590 | 1.30k | dest_format, dest_flags, |
591 | 1.30k | &implementation, &func); |
592 | 1.30k | } |
593 | | |
594 | 1.31M | glyph_box.x1 = glyphs[i].x - glyph->origin_x + off_x; |
595 | 1.31M | glyph_box.y1 = glyphs[i].y - glyph->origin_y + off_y; |
596 | 1.31M | glyph_box.x2 = glyph_box.x1 + glyph->image->bits.width; |
597 | 1.31M | glyph_box.y2 = glyph_box.y1 + glyph->image->bits.height; |
598 | | |
599 | 1.31M | if (box32_intersect (&composite_box, &glyph_box, &dest_box)) |
600 | 15.4k | { |
601 | 15.4k | int src_x = composite_box.x1 - glyph_box.x1; |
602 | 15.4k | int src_y = composite_box.y1 - glyph_box.y1; |
603 | | |
604 | 15.4k | if (white_src) |
605 | 0 | info.mask_image = glyph_img; |
606 | 15.4k | else |
607 | 15.4k | info.src_image = glyph_img; |
608 | | |
609 | 15.4k | info.mask_x = info.src_x = src_x; |
610 | 15.4k | info.mask_y = info.src_y = src_y; |
611 | 15.4k | info.dest_x = composite_box.x1; |
612 | 15.4k | info.dest_y = composite_box.y1; |
613 | 15.4k | info.width = composite_box.x2 - composite_box.x1; |
614 | 15.4k | info.height = composite_box.y2 - composite_box.y1; |
615 | | |
616 | 15.4k | func (implementation, &info); |
617 | | |
618 | 15.4k | pixman_list_move_to_front (&cache->mru, &glyph->mru_link); |
619 | 15.4k | } |
620 | 1.31M | } |
621 | | |
622 | 1.30k | out: |
623 | 1.30k | if (white_img) |
624 | 0 | pixman_image_unref (white_img); |
625 | 1.30k | } |
626 | | |
627 | | /* Conceptually, for each glyph, (white IN glyph) is PIXMAN_OP_ADDed to an |
628 | | * infinitely big mask image at the position such that the glyph origin point |
629 | | * is positioned at the (glyphs[i].x, glyphs[i].y) point. |
630 | | * |
631 | | * Then (mask_x, mask_y) in the infinite mask and (src_x, src_y) in the source |
632 | | * image are both aligned with (dest_x, dest_y) in the destination image. Then |
633 | | * these three images are composited within the |
634 | | * |
635 | | * (dest_x, dest_y, dst_x + width, dst_y + height) |
636 | | * |
637 | | * rectangle. |
638 | | * |
639 | | * TODO: |
640 | | * - Trim the mask to the destination clip/image? |
641 | | * - Trim composite region based on sources, when the op ignores 0s. |
642 | | */ |
643 | | #if defined(__GNUC__) && !defined(__x86_64__) && !defined(__amd64__) |
644 | | __attribute__((__force_align_arg_pointer__)) |
645 | | #endif |
646 | | PIXMAN_EXPORT void |
647 | | pixman_composite_glyphs (pixman_op_t op, |
648 | | pixman_image_t *src, |
649 | | pixman_image_t *dest, |
650 | | pixman_format_code_t mask_format, |
651 | | int32_t src_x, |
652 | | int32_t src_y, |
653 | | int32_t mask_x, |
654 | | int32_t mask_y, |
655 | | int32_t dest_x, |
656 | | int32_t dest_y, |
657 | | int32_t width, |
658 | | int32_t height, |
659 | | pixman_glyph_cache_t *cache, |
660 | | int n_glyphs, |
661 | | const pixman_glyph_t *glyphs) |
662 | 1.30k | { |
663 | 1.30k | pixman_image_t *mask; |
664 | | |
665 | 1.30k | if (!(mask = pixman_image_create_bits (mask_format, width, height, NULL, -1))) |
666 | 0 | return; |
667 | | |
668 | 1.30k | if (PIXMAN_FORMAT_A (mask_format) != 0 && |
669 | 1.30k | PIXMAN_FORMAT_RGB (mask_format) != 0) |
670 | 0 | { |
671 | 0 | pixman_image_set_component_alpha (mask, TRUE); |
672 | 0 | } |
673 | | |
674 | 1.30k | add_glyphs (cache, mask, - mask_x, - mask_y, n_glyphs, glyphs); |
675 | | |
676 | 1.30k | pixman_image_composite32 (op, src, mask, dest, |
677 | 1.30k | src_x, src_y, |
678 | 1.30k | 0, 0, |
679 | 1.30k | dest_x, dest_y, |
680 | 1.30k | width, height); |
681 | | |
682 | 1.30k | pixman_image_unref (mask); |
683 | 1.30k | } |