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