Make the operator strength reduction constant time.
[profile/ivi/pixman.git] / pixman / pixman.c
1 /* -*- Mode: c; c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t; -*- */
2 /*
3  * Copyright © 2000 SuSE, Inc.
4  * Copyright © 2007 Red Hat, Inc.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of SuSE not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  SuSE makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Author:  Keith Packard, SuSE, Inc.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 #include "pixman-private.h"
30
31 #include <stdlib.h>
32
33 /*
34  * Operator optimizations based on source or destination opacity
35  */
36 typedef struct
37 {
38     pixman_op_t op;
39     pixman_op_t op_src_dst_opaque;
40     pixman_op_t op_src_opaque;
41     pixman_op_t op_dst_opaque;
42 } operator_info_t;
43
44 #define NO_OPTIMIZATION(a) (a), (a), (a), (a)
45
46 static const operator_info_t operator_table[] =
47 {
48     /* Input Operator           SRC&DST Opaque          SRC Opaque              DST Opaque      */
49     { NO_OPTIMIZATION (PIXMAN_OP_CLEAR) },
50     { NO_OPTIMIZATION (PIXMAN_OP_SRC) },
51     { NO_OPTIMIZATION (PIXMAN_OP_DST) },
52     { PIXMAN_OP_OVER,           PIXMAN_OP_SRC,          PIXMAN_OP_SRC,          PIXMAN_OP_OVER },
53     { PIXMAN_OP_OVER_REVERSE,   PIXMAN_OP_DST,          PIXMAN_OP_OVER_REVERSE, PIXMAN_OP_DST },
54     { PIXMAN_OP_IN,             PIXMAN_OP_SRC,          PIXMAN_OP_IN,           PIXMAN_OP_SRC },
55     { PIXMAN_OP_IN_REVERSE,     PIXMAN_OP_DST,          PIXMAN_OP_DST,          PIXMAN_OP_IN_REVERSE },
56     { PIXMAN_OP_OUT,            PIXMAN_OP_CLEAR,        PIXMAN_OP_OUT,          PIXMAN_OP_CLEAR },
57     { PIXMAN_OP_OUT_REVERSE,    PIXMAN_OP_CLEAR,        PIXMAN_OP_CLEAR,        PIXMAN_OP_OUT_REVERSE },
58     { PIXMAN_OP_ATOP,           PIXMAN_OP_SRC,          PIXMAN_OP_IN,           PIXMAN_OP_OVER },
59     { PIXMAN_OP_ATOP_REVERSE,   PIXMAN_OP_DST,          PIXMAN_OP_OVER_REVERSE, PIXMAN_OP_IN_REVERSE },
60     { PIXMAN_OP_XOR,            PIXMAN_OP_CLEAR,        PIXMAN_OP_OUT,          PIXMAN_OP_OUT_REVERSE },
61     { NO_OPTIMIZATION (PIXMAN_OP_ADD) },
62     { PIXMAN_OP_SATURATE,       PIXMAN_OP_DST,          PIXMAN_OP_OVER_REVERSE, PIXMAN_OP_DST },
63
64     { PIXMAN_OP_NONE /* 0x0e */ },
65     { PIXMAN_OP_NONE /* 0x0f */ },
66
67     { NO_OPTIMIZATION (PIXMAN_OP_DISJOINT_CLEAR) },
68     { NO_OPTIMIZATION (PIXMAN_OP_DISJOINT_SRC) },
69     { NO_OPTIMIZATION (PIXMAN_OP_DISJOINT_DST) },
70     { NO_OPTIMIZATION (PIXMAN_OP_DISJOINT_OVER) },
71     { NO_OPTIMIZATION (PIXMAN_OP_DISJOINT_OVER_REVERSE) },
72     { NO_OPTIMIZATION (PIXMAN_OP_DISJOINT_IN) },
73     { NO_OPTIMIZATION (PIXMAN_OP_DISJOINT_IN_REVERSE) },
74     { NO_OPTIMIZATION (PIXMAN_OP_DISJOINT_OUT) },
75     { NO_OPTIMIZATION (PIXMAN_OP_DISJOINT_OUT_REVERSE) },
76     { NO_OPTIMIZATION (PIXMAN_OP_DISJOINT_ATOP) },
77     { NO_OPTIMIZATION (PIXMAN_OP_DISJOINT_ATOP_REVERSE) },
78     { NO_OPTIMIZATION (PIXMAN_OP_DISJOINT_XOR) },
79
80     { PIXMAN_OP_NONE /* 0x1c */ },
81     { PIXMAN_OP_NONE /* 0x1d */ },
82     { PIXMAN_OP_NONE /* 0x1e */ },
83     { PIXMAN_OP_NONE /* 0x1f */ },
84
85     { NO_OPTIMIZATION (PIXMAN_OP_CONJOINT_CLEAR) },
86     { NO_OPTIMIZATION (PIXMAN_OP_CONJOINT_SRC) },
87     { NO_OPTIMIZATION (PIXMAN_OP_CONJOINT_DST) },
88     { NO_OPTIMIZATION (PIXMAN_OP_CONJOINT_OVER) },
89     { NO_OPTIMIZATION (PIXMAN_OP_CONJOINT_OVER_REVERSE) },
90     { NO_OPTIMIZATION (PIXMAN_OP_CONJOINT_IN) },
91     { NO_OPTIMIZATION (PIXMAN_OP_CONJOINT_IN_REVERSE) },
92     { NO_OPTIMIZATION (PIXMAN_OP_CONJOINT_OUT) },
93     { NO_OPTIMIZATION (PIXMAN_OP_CONJOINT_OUT_REVERSE) },
94     { NO_OPTIMIZATION (PIXMAN_OP_CONJOINT_ATOP) },
95     { NO_OPTIMIZATION (PIXMAN_OP_CONJOINT_ATOP_REVERSE) },
96     { NO_OPTIMIZATION (PIXMAN_OP_CONJOINT_XOR) },
97
98     { PIXMAN_OP_NONE /* 0x2c */ },
99     { PIXMAN_OP_NONE /* 0x2d */ },
100     { PIXMAN_OP_NONE /* 0x2e */ },
101     { PIXMAN_OP_NONE /* 0x2f */ },
102
103     { NO_OPTIMIZATION (PIXMAN_OP_MULTIPLY) },
104     { NO_OPTIMIZATION (PIXMAN_OP_SCREEN) },
105     { NO_OPTIMIZATION (PIXMAN_OP_OVERLAY) },
106     { NO_OPTIMIZATION (PIXMAN_OP_DARKEN) },
107     { NO_OPTIMIZATION (PIXMAN_OP_LIGHTEN) },
108     { NO_OPTIMIZATION (PIXMAN_OP_COLOR_DODGE) },
109     { NO_OPTIMIZATION (PIXMAN_OP_COLOR_BURN) },
110     { NO_OPTIMIZATION (PIXMAN_OP_HARD_LIGHT) },
111     { NO_OPTIMIZATION (PIXMAN_OP_SOFT_LIGHT) },
112     { NO_OPTIMIZATION (PIXMAN_OP_DIFFERENCE) },
113     { NO_OPTIMIZATION (PIXMAN_OP_EXCLUSION) },
114     { NO_OPTIMIZATION (PIXMAN_OP_HSL_HUE) },
115     { NO_OPTIMIZATION (PIXMAN_OP_HSL_SATURATION) },
116     { NO_OPTIMIZATION (PIXMAN_OP_HSL_COLOR) },
117     { NO_OPTIMIZATION (PIXMAN_OP_HSL_LUMINOSITY) },
118
119     { PIXMAN_OP_NONE }
120 };
121
122 static pixman_implementation_t *imp;
123
124 /*
125  * Optimize the current operator based on opacity of source or destination
126  * The output operator should be mathematically equivalent to the source.
127  */
128 static pixman_op_t
129 optimize_operator (pixman_op_t     op,
130                    uint32_t        src_flags,
131                    uint32_t        mask_flags,
132                    uint32_t        dst_flags)
133 {
134     const operator_info_t *info = &(operator_table[op]);
135     pixman_bool_t is_source_opaque, is_dest_opaque;
136
137     assert (info->op == op);
138
139     is_source_opaque = (src_flags & mask_flags) & FAST_PATH_IS_OPAQUE;
140     is_dest_opaque = dst_flags & FAST_PATH_IS_OPAQUE;
141
142     if (is_source_opaque && is_dest_opaque)
143         return info->op_src_dst_opaque;
144     else if (is_source_opaque)
145         return info->op_src_opaque;
146     else if (is_dest_opaque)
147         return info->op_dst_opaque;
148
149     return op;
150 }
151
152 static void
153 apply_workaround (pixman_image_t *image,
154                   int32_t *       x,
155                   int32_t *       y,
156                   uint32_t **     save_bits,
157                   int *           save_dx,
158                   int *           save_dy)
159 {
160     if (image && (image->common.flags & FAST_PATH_NEEDS_WORKAROUND))
161     {
162         /* Some X servers generate images that point to the
163          * wrong place in memory, but then set the clip region
164          * to point to the right place. Because of an old bug
165          * in pixman, this would actually work.
166          *
167          * Here we try and undo the damage
168          */
169         int bpp = PIXMAN_FORMAT_BPP (image->bits.format) / 8;
170         pixman_box32_t *extents;
171         uint8_t *t;
172         int dx, dy;
173         
174         extents = pixman_region32_extents (&(image->common.clip_region));
175         dx = extents->x1;
176         dy = extents->y1;
177         
178         *save_bits = image->bits.bits;
179         
180         *x -= dx;
181         *y -= dy;
182         pixman_region32_translate (&(image->common.clip_region), -dx, -dy);
183         
184         t = (uint8_t *)image->bits.bits;
185         t += dy * image->bits.rowstride * 4 + dx * bpp;
186         image->bits.bits = (uint32_t *)t;
187         
188         *save_dx = dx;
189         *save_dy = dy;
190     }
191 }
192
193 static void
194 unapply_workaround (pixman_image_t *image, uint32_t *bits, int dx, int dy)
195 {
196     if (image && (image->common.flags & FAST_PATH_NEEDS_WORKAROUND))
197     {
198         image->bits.bits = bits;
199         pixman_region32_translate (&image->common.clip_region, dx, dy);
200     }
201 }
202
203 /*
204  * Computing composite region
205  */
206 static inline pixman_bool_t
207 clip_general_image (pixman_region32_t * region,
208                     pixman_region32_t * clip,
209                     int                 dx,
210                     int                 dy)
211 {
212     if (pixman_region32_n_rects (region) == 1 &&
213         pixman_region32_n_rects (clip) == 1)
214     {
215         pixman_box32_t *  rbox = pixman_region32_rectangles (region, NULL);
216         pixman_box32_t *  cbox = pixman_region32_rectangles (clip, NULL);
217         int v;
218
219         if (rbox->x1 < (v = cbox->x1 + dx))
220             rbox->x1 = v;
221         if (rbox->x2 > (v = cbox->x2 + dx))
222             rbox->x2 = v;
223         if (rbox->y1 < (v = cbox->y1 + dy))
224             rbox->y1 = v;
225         if (rbox->y2 > (v = cbox->y2 + dy))
226             rbox->y2 = v;
227         if (rbox->x1 >= rbox->x2 || rbox->y1 >= rbox->y2)
228         {
229             pixman_region32_init (region);
230             return FALSE;
231         }
232     }
233     else if (!pixman_region32_not_empty (clip))
234     {
235         return FALSE;
236     }
237     else
238     {
239         if (dx || dy)
240             pixman_region32_translate (region, -dx, -dy);
241
242         if (!pixman_region32_intersect (region, region, clip))
243             return FALSE;
244
245         if (dx || dy)
246             pixman_region32_translate (region, dx, dy);
247     }
248
249     return pixman_region32_not_empty (region);
250 }
251
252 static inline pixman_bool_t
253 clip_source_image (pixman_region32_t * region,
254                    pixman_image_t *    image,
255                    int                 dx,
256                    int                 dy)
257 {
258     /* Source clips are ignored, unless they are explicitly turned on
259      * and the clip in question was set by an X client. (Because if
260      * the clip was not set by a client, then it is a hierarchy
261      * clip and those should always be ignored for sources).
262      */
263     if (!image->common.clip_sources || !image->common.client_clip)
264         return TRUE;
265
266     return clip_general_image (region,
267                                &image->common.clip_region,
268                                dx, dy);
269 }
270
271 /*
272  * returns FALSE if the final region is empty.  Indistinguishable from
273  * an allocation failure, but rendering ignores those anyways.
274  */
275 static pixman_bool_t
276 pixman_compute_composite_region32 (pixman_region32_t * region,
277                                    pixman_image_t *    src_image,
278                                    pixman_image_t *    mask_image,
279                                    pixman_image_t *    dst_image,
280                                    int32_t             src_x,
281                                    int32_t             src_y,
282                                    int32_t             mask_x,
283                                    int32_t             mask_y,
284                                    int32_t             dest_x,
285                                    int32_t             dest_y,
286                                    int32_t             width,
287                                    int32_t             height)
288 {
289     region->extents.x1 = dest_x;
290     region->extents.x2 = dest_x + width;
291     region->extents.y1 = dest_y;
292     region->extents.y2 = dest_y + height;
293
294     region->extents.x1 = MAX (region->extents.x1, 0);
295     region->extents.y1 = MAX (region->extents.y1, 0);
296     region->extents.x2 = MIN (region->extents.x2, dst_image->bits.width);
297     region->extents.y2 = MIN (region->extents.y2, dst_image->bits.height);
298
299     region->data = 0;
300
301     /* Check for empty operation */
302     if (region->extents.x1 >= region->extents.x2 ||
303         region->extents.y1 >= region->extents.y2)
304     {
305         pixman_region32_init (region);
306         return FALSE;
307     }
308
309     if (dst_image->common.have_clip_region)
310     {
311         if (!clip_general_image (region, &dst_image->common.clip_region, 0, 0))
312         {
313             pixman_region32_fini (region);
314             return FALSE;
315         }
316     }
317
318     if (dst_image->common.alpha_map && dst_image->common.alpha_map->common.have_clip_region)
319     {
320         if (!clip_general_image (region, &dst_image->common.alpha_map->common.clip_region,
321                                  -dst_image->common.alpha_origin_x,
322                                  -dst_image->common.alpha_origin_y))
323         {
324             pixman_region32_fini (region);
325             return FALSE;
326         }
327     }
328
329     /* clip against src */
330     if (src_image->common.have_clip_region)
331     {
332         if (!clip_source_image (region, src_image, dest_x - src_x, dest_y - src_y))
333         {
334             pixman_region32_fini (region);
335             return FALSE;
336         }
337     }
338     if (src_image->common.alpha_map && src_image->common.alpha_map->common.have_clip_region)
339     {
340         if (!clip_source_image (region, (pixman_image_t *)src_image->common.alpha_map,
341                                 dest_x - (src_x - src_image->common.alpha_origin_x),
342                                 dest_y - (src_y - src_image->common.alpha_origin_y)))
343         {
344             pixman_region32_fini (region);
345             return FALSE;
346         }
347     }
348     /* clip against mask */
349     if (mask_image && mask_image->common.have_clip_region)
350     {
351         if (!clip_source_image (region, mask_image, dest_x - mask_x, dest_y - mask_y))
352         {
353             pixman_region32_fini (region);
354             return FALSE;
355         }
356         if (mask_image->common.alpha_map && mask_image->common.alpha_map->common.have_clip_region)
357         {
358             if (!clip_source_image (region, (pixman_image_t *)mask_image->common.alpha_map,
359                                     dest_x - (mask_x - mask_image->common.alpha_origin_x),
360                                     dest_y - (mask_y - mask_image->common.alpha_origin_y)))
361             {
362                 pixman_region32_fini (region);
363                 return FALSE;
364             }
365         }
366     }
367
368     return TRUE;
369 }
370
371 static void
372 walk_region_internal (pixman_implementation_t *imp,
373                       pixman_op_t              op,
374                       pixman_image_t *         src_image,
375                       pixman_image_t *         mask_image,
376                       pixman_image_t *         dst_image,
377                       int32_t                  src_x,
378                       int32_t                  src_y,
379                       int32_t                  mask_x,
380                       int32_t                  mask_y,
381                       int32_t                  dest_x,
382                       int32_t                  dest_y,
383                       int32_t                  width,
384                       int32_t                  height,
385                       pixman_bool_t            src_repeat,
386                       pixman_bool_t            mask_repeat,
387                       pixman_region32_t *      region,
388                       pixman_composite_func_t  composite_rect)
389 {
390     int w, h, w_this, h_this;
391     int x_msk, y_msk, x_src, y_src, x_dst, y_dst;
392     int src_dy = src_y - dest_y;
393     int src_dx = src_x - dest_x;
394     int mask_dy = mask_y - dest_y;
395     int mask_dx = mask_x - dest_x;
396     const pixman_box32_t *pbox;
397     int n;
398
399     pbox = pixman_region32_rectangles (region, &n);
400
401     /* Fast path for non-repeating sources */
402     if (!src_repeat && !mask_repeat)
403     {
404        while (n--)
405        {
406            (*composite_rect) (imp, op,
407                               src_image, mask_image, dst_image,
408                               pbox->x1 + src_dx,
409                               pbox->y1 + src_dy,
410                               pbox->x1 + mask_dx,
411                               pbox->y1 + mask_dy,
412                               pbox->x1,
413                               pbox->y1,
414                               pbox->x2 - pbox->x1,
415                               pbox->y2 - pbox->y1);
416            
417            pbox++;
418        }
419
420        return;
421     }
422     
423     while (n--)
424     {
425         h = pbox->y2 - pbox->y1;
426         y_src = pbox->y1 + src_dy;
427         y_msk = pbox->y1 + mask_dy;
428         y_dst = pbox->y1;
429
430         while (h)
431         {
432             h_this = h;
433             w = pbox->x2 - pbox->x1;
434             x_src = pbox->x1 + src_dx;
435             x_msk = pbox->x1 + mask_dx;
436             x_dst = pbox->x1;
437
438             if (mask_repeat)
439             {
440                 y_msk = MOD (y_msk, mask_image->bits.height);
441                 if (h_this > mask_image->bits.height - y_msk)
442                     h_this = mask_image->bits.height - y_msk;
443             }
444
445             if (src_repeat)
446             {
447                 y_src = MOD (y_src, src_image->bits.height);
448                 if (h_this > src_image->bits.height - y_src)
449                     h_this = src_image->bits.height - y_src;
450             }
451
452             while (w)
453             {
454                 w_this = w;
455
456                 if (mask_repeat)
457                 {
458                     x_msk = MOD (x_msk, mask_image->bits.width);
459                     if (w_this > mask_image->bits.width - x_msk)
460                         w_this = mask_image->bits.width - x_msk;
461                 }
462
463                 if (src_repeat)
464                 {
465                     x_src = MOD (x_src, src_image->bits.width);
466                     if (w_this > src_image->bits.width - x_src)
467                         w_this = src_image->bits.width - x_src;
468                 }
469
470                 (*composite_rect) (imp, op,
471                                    src_image, mask_image, dst_image,
472                                    x_src, y_src, x_msk, y_msk, x_dst, y_dst,
473                                    w_this, h_this);
474                 w -= w_this;
475
476                 x_src += w_this;
477                 x_msk += w_this;
478                 x_dst += w_this;
479             }
480
481             h -= h_this;
482             y_src += h_this;
483             y_msk += h_this;
484             y_dst += h_this;
485         }
486
487         pbox++;
488     }
489 }
490
491 static force_inline pixman_bool_t
492 image_covers (pixman_image_t *image,
493               pixman_box32_t *extents,
494               int             x,
495               int             y)
496 {
497     if (image->common.type == BITS &&
498         image->common.repeat == PIXMAN_REPEAT_NONE)
499     {
500         if (x > extents->x1 || y > extents->y1 ||
501             x + image->bits.width < extents->x2 ||
502             y + image->bits.height < extents->y2)
503         {
504             return FALSE;
505         }
506     }
507
508     return TRUE;
509 }
510
511 static void
512 do_composite (pixman_implementation_t *imp,
513               pixman_op_t              op,
514               pixman_image_t          *src,
515               pixman_image_t          *mask,
516               pixman_image_t          *dest,
517               int                      src_x,
518               int                      src_y,
519               int                      mask_x,
520               int                      mask_y,
521               int                      dest_x,
522               int                      dest_y,
523               int                      width,
524               int                      height)
525 {
526 #define N_CACHED_FAST_PATHS 8
527     static THREAD_LOCAL pixman_fast_path_t tls_cache[N_CACHED_FAST_PATHS];
528     pixman_format_code_t src_format, mask_format, dest_format;
529     uint32_t src_flags, mask_flags, dest_flags;
530     pixman_region32_t region;
531     pixman_box32_t *extents;
532     uint32_t *src_bits;
533     int src_dx, src_dy;
534     uint32_t *mask_bits;
535     int mask_dx, mask_dy;
536     uint32_t *dest_bits;
537     int dest_dx, dest_dy;
538     pixman_bool_t need_workaround;
539     pixman_fast_path_t *cache;
540     const pixman_fast_path_t *info;
541     int i;
542
543     src_format = src->common.extended_format_code;
544     src_flags = src->common.flags;
545
546     if (mask)
547     {
548         mask_format = mask->common.extended_format_code;
549         mask_flags = mask->common.flags;
550     }
551     else
552     {
553         mask_format = PIXMAN_null;
554         mask_flags = FAST_PATH_IS_OPAQUE;
555     }
556
557     dest_format = dest->common.extended_format_code;
558     dest_flags = dest->common.flags;
559
560     /* Check for pixbufs */
561     if ((mask_format == PIXMAN_a8r8g8b8 || mask_format == PIXMAN_a8b8g8r8) &&
562         (src->type == BITS && src->bits.bits == mask->bits.bits)           &&
563         (src->common.repeat == mask->common.repeat)                        &&
564         (src_x == mask_x && src_y == mask_y))
565     {
566         if (src_format == PIXMAN_x8b8g8r8)
567             src_format = mask_format = PIXMAN_pixbuf;
568         else if (src_format == PIXMAN_x8r8g8b8)
569             src_format = mask_format = PIXMAN_rpixbuf;
570     }
571
572     /* Check for workaround */
573     need_workaround = (src_flags | mask_flags | dest_flags) & FAST_PATH_NEEDS_WORKAROUND;
574
575     if (need_workaround)
576     {
577         apply_workaround (src, &src_x, &src_y, &src_bits, &src_dx, &src_dy);
578         apply_workaround (mask, &mask_x, &mask_y, &mask_bits, &mask_dx, &mask_dy);
579         apply_workaround (dest, &dest_x, &dest_y, &dest_bits, &dest_dx, &dest_dy);
580     }
581
582     pixman_region32_init (&region);
583     
584     if (!pixman_compute_composite_region32 (
585             &region, src, mask, dest,
586             src_x, src_y, mask_x, mask_y, dest_x, dest_y, width, height))
587     {
588         return;
589     }
590     
591     extents = pixman_region32_extents (&region);
592     
593     if (image_covers (src, extents, dest_x - src_x, dest_y - src_y))
594         src_flags |= FAST_PATH_COVERS_CLIP;
595     
596     if (mask && image_covers (mask, extents, dest_x - mask_x, dest_y - mask_y))
597         mask_flags |= FAST_PATH_COVERS_CLIP;
598
599     /*
600      * Check if we can replace our operator by a simpler one
601      * if the src or dest are opaque. The output operator should be
602      * mathematically equivalent to the source.
603      */
604     op = optimize_operator (op, src_flags, mask_flags, dest_flags);
605     if (op == PIXMAN_OP_DST ||
606         op == PIXMAN_OP_CONJOINT_DST ||
607         op == PIXMAN_OP_DISJOINT_DST)
608     {
609         return;
610     }
611
612     cache = tls_cache;
613
614     for (i = 0; i < N_CACHED_FAST_PATHS; ++i)
615     {
616         info = &(cache[i]);
617
618         /* Note that we check for equality here, not whether
619          * the cached fast path matches. This is to prevent
620          * us from selecting an overly general fast path
621          * when a more specific one would work.
622          */
623         if (info->op == op                      &&
624             info->src_format == src_format      &&
625             info->mask_format == mask_format    &&
626             info->dest_format == dest_format    &&
627             info->src_flags == src_flags        &&
628             info->mask_flags == mask_flags      &&
629             info->dest_flags == dest_flags      &&
630             info->func)
631         {
632             goto found;
633         }
634     }
635
636     while (imp)
637     {
638         info = imp->fast_paths;
639
640         while (info->op != PIXMAN_OP_NONE)
641         {
642             if ((info->op == op || info->op == PIXMAN_OP_any)           &&
643                 /* Formats */
644                 ((info->src_format == src_format) ||
645                  (info->src_format == PIXMAN_any))                      &&
646                 ((info->mask_format == mask_format) ||
647                  (info->mask_format == PIXMAN_any))                     &&
648                 ((info->dest_format == dest_format) ||
649                  (info->dest_format == PIXMAN_any))                     &&
650                 /* Flags */
651                 (info->src_flags & src_flags) == info->src_flags        &&
652                 (info->mask_flags & mask_flags) == info->mask_flags     &&
653                 (info->dest_flags & dest_flags) == info->dest_flags)
654             {
655                 /* Set i to the last spot in the cache so that the
656                  * move-to-front code below will work
657                  */
658                 i = N_CACHED_FAST_PATHS - 1;
659
660                 goto found;
661             }
662
663             ++info;
664         }
665
666         imp = imp->delegate;
667     }
668
669     /* We didn't find a compositing routine. This should not happen, but if
670      * it somehow does, just exit rather than crash.
671      */
672     goto out;
673
674 found:
675     walk_region_internal (imp, op,
676                           src, mask, dest,
677                           src_x, src_y, mask_x, mask_y,
678                           dest_x, dest_y,
679                           width, height,
680                           (src_flags & FAST_PATH_SIMPLE_REPEAT),
681                           (mask_flags & FAST_PATH_SIMPLE_REPEAT),
682                           &region, info->func);
683
684     if (i)
685     {
686         /* Make a copy of info->func, because info->func may change when
687          * we update the cache.
688          */
689         pixman_composite_func_t func = info->func;
690         
691         while (i--)
692             cache[i + 1] = cache[i];
693
694         cache[0].op = op;
695         cache[0].src_format = src_format;
696         cache[0].src_flags = src_flags;
697         cache[0].mask_format = mask_format;
698         cache[0].mask_flags = mask_flags;
699         cache[0].dest_format = dest_format;
700         cache[0].dest_flags = dest_flags;
701         cache[0].func = func;
702     }
703
704 out:
705     if (need_workaround)
706     {
707         unapply_workaround (src, src_bits, src_dx, src_dy);
708         unapply_workaround (mask, mask_bits, mask_dx, mask_dy);
709         unapply_workaround (dest, dest_bits, dest_dx, dest_dy);
710     }
711
712     pixman_region32_fini (&region);
713 }
714
715 PIXMAN_EXPORT void
716 pixman_image_composite (pixman_op_t      op,
717                         pixman_image_t * src,
718                         pixman_image_t * mask,
719                         pixman_image_t * dest,
720                         int16_t          src_x,
721                         int16_t          src_y,
722                         int16_t          mask_x,
723                         int16_t          mask_y,
724                         int16_t          dest_x,
725                         int16_t          dest_y,
726                         uint16_t         width,
727                         uint16_t         height)
728 {
729     pixman_image_composite32 (op, src, mask, dest, src_x, src_y, 
730                               mask_x, mask_y, dest_x, dest_y, width, height);
731 }
732
733 /*
734  * Work around GCC bug causing crashes in Mozilla with SSE2
735  *
736  * When using -msse, gcc generates movdqa instructions assuming that
737  * the stack is 16 byte aligned. Unfortunately some applications, such
738  * as Mozilla and Mono, end up aligning the stack to 4 bytes, which
739  * causes the movdqa instructions to fail.
740  *
741  * The __force_align_arg_pointer__ makes gcc generate a prologue that
742  * realigns the stack pointer to 16 bytes.
743  *
744  * On x86-64 this is not necessary because the standard ABI already
745  * calls for a 16 byte aligned stack.
746  *
747  * See https://bugs.freedesktop.org/show_bug.cgi?id=15693
748  */
749 #if defined (USE_SSE2) && defined(__GNUC__) && !defined(__x86_64__) && !defined(__amd64__)
750 __attribute__((__force_align_arg_pointer__))
751 #endif
752 PIXMAN_EXPORT void
753 pixman_image_composite32 (pixman_op_t      op,
754                           pixman_image_t * src,
755                           pixman_image_t * mask,
756                           pixman_image_t * dest,
757                           int32_t          src_x,
758                           int32_t          src_y,
759                           int32_t          mask_x,
760                           int32_t          mask_y,
761                           int32_t          dest_x,
762                           int32_t          dest_y,
763                           int32_t          width,
764                           int32_t          height)
765 {
766     _pixman_image_validate (src);
767     if (mask)
768         _pixman_image_validate (mask);
769     _pixman_image_validate (dest);
770
771     if (!imp)
772         imp = _pixman_choose_implementation ();
773
774     do_composite (imp, op,
775                   src, mask, dest,
776                   src_x, src_y,
777                   mask_x, mask_y,
778                   dest_x, dest_y,
779                   width, height);
780 }
781
782 PIXMAN_EXPORT pixman_bool_t
783 pixman_blt (uint32_t *src_bits,
784             uint32_t *dst_bits,
785             int       src_stride,
786             int       dst_stride,
787             int       src_bpp,
788             int       dst_bpp,
789             int       src_x,
790             int       src_y,
791             int       dst_x,
792             int       dst_y,
793             int       width,
794             int       height)
795 {
796     if (!imp)
797         imp = _pixman_choose_implementation ();
798
799     return _pixman_implementation_blt (imp, src_bits, dst_bits, src_stride, dst_stride,
800                                        src_bpp, dst_bpp,
801                                        src_x, src_y,
802                                        dst_x, dst_y,
803                                        width, height);
804 }
805
806 PIXMAN_EXPORT pixman_bool_t
807 pixman_fill (uint32_t *bits,
808              int       stride,
809              int       bpp,
810              int       x,
811              int       y,
812              int       width,
813              int       height,
814              uint32_t xor)
815 {
816     if (!imp)
817         imp = _pixman_choose_implementation ();
818
819     return _pixman_implementation_fill (imp, bits, stride, bpp, x, y, width, height, xor);
820 }
821
822 static uint32_t
823 color_to_uint32 (const pixman_color_t *color)
824 {
825     return
826         (color->alpha >> 8 << 24) |
827         (color->red >> 8 << 16) |
828         (color->green & 0xff00) |
829         (color->blue >> 8);
830 }
831
832 static pixman_bool_t
833 color_to_pixel (pixman_color_t *     color,
834                 uint32_t *           pixel,
835                 pixman_format_code_t format)
836 {
837     uint32_t c = color_to_uint32 (color);
838
839     if (!(format == PIXMAN_a8r8g8b8     ||
840           format == PIXMAN_x8r8g8b8     ||
841           format == PIXMAN_a8b8g8r8     ||
842           format == PIXMAN_x8b8g8r8     ||
843           format == PIXMAN_b8g8r8a8     ||
844           format == PIXMAN_b8g8r8x8     ||
845           format == PIXMAN_r5g6b5       ||
846           format == PIXMAN_b5g6r5       ||
847           format == PIXMAN_a8))
848     {
849         return FALSE;
850     }
851
852     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
853     {
854         c = ((c & 0xff000000) >>  0) |
855             ((c & 0x00ff0000) >> 16) |
856             ((c & 0x0000ff00) >>  0) |
857             ((c & 0x000000ff) << 16);
858     }
859     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
860     {
861         c = ((c & 0xff000000) >> 24) |
862             ((c & 0x00ff0000) >>  8) |
863             ((c & 0x0000ff00) <<  8) |
864             ((c & 0x000000ff) << 24);
865     }
866
867     if (format == PIXMAN_a8)
868         c = c >> 24;
869     else if (format == PIXMAN_r5g6b5 ||
870              format == PIXMAN_b5g6r5)
871         c = CONVERT_8888_TO_0565 (c);
872
873 #if 0
874     printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
875     printf ("pixel: %x\n", c);
876 #endif
877
878     *pixel = c;
879     return TRUE;
880 }
881
882 PIXMAN_EXPORT pixman_bool_t
883 pixman_image_fill_rectangles (pixman_op_t                 op,
884                               pixman_image_t *            dest,
885                               pixman_color_t *            color,
886                               int                         n_rects,
887                               const pixman_rectangle16_t *rects)
888 {
889     pixman_box32_t stack_boxes[6];
890     pixman_box32_t *boxes;
891     pixman_bool_t result;
892     int i;
893
894     if (n_rects > 6)
895     {
896         boxes = pixman_malloc_ab (sizeof (pixman_box32_t), n_rects);
897         if (boxes == NULL)
898             return FALSE;
899     }
900     else
901     {
902         boxes = stack_boxes;
903     }
904
905     for (i = 0; i < n_rects; ++i)
906     {
907         boxes[i].x1 = rects[i].x;
908         boxes[i].y1 = rects[i].y;
909         boxes[i].x2 = boxes[i].x1 + rects[i].width;
910         boxes[i].y2 = boxes[i].y1 + rects[i].height;
911     }
912
913     result = pixman_image_fill_boxes (op, dest, color, n_rects, boxes);
914
915     if (boxes != stack_boxes)
916         free (boxes);
917     
918     return result;
919 }
920
921 PIXMAN_EXPORT pixman_bool_t
922 pixman_image_fill_boxes (pixman_op_t           op,
923                          pixman_image_t *      dest,
924                          pixman_color_t *      color,
925                          int                   n_boxes,
926                          const pixman_box32_t *boxes)
927 {
928     pixman_image_t *solid;
929     pixman_color_t c;
930     int i;
931
932     _pixman_image_validate (dest);
933     
934     if (color->alpha == 0xffff)
935     {
936         if (op == PIXMAN_OP_OVER)
937             op = PIXMAN_OP_SRC;
938     }
939
940     if (op == PIXMAN_OP_CLEAR)
941     {
942         c.red = 0;
943         c.green = 0;
944         c.blue = 0;
945         c.alpha = 0;
946
947         color = &c;
948
949         op = PIXMAN_OP_SRC;
950     }
951
952     if (op == PIXMAN_OP_SRC)
953     {
954         uint32_t pixel;
955
956         if (color_to_pixel (color, &pixel, dest->bits.format))
957         {
958             pixman_region32_t fill_region;
959             int n_rects, j;
960             pixman_box32_t *rects;
961
962             if (!pixman_region32_init_rects (&fill_region, boxes, n_boxes))
963                 return FALSE;
964
965             if (dest->common.have_clip_region)
966             {
967                 if (!pixman_region32_intersect (&fill_region,
968                                                 &fill_region,
969                                                 &dest->common.clip_region))
970                     return FALSE;
971             }
972
973             rects = pixman_region32_rectangles (&fill_region, &n_rects);
974             for (j = 0; j < n_rects; ++j)
975             {
976                 const pixman_box32_t *rect = &(rects[j]);
977                 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
978                              rect->x1, rect->y1, rect->x2 - rect->x1, rect->y2 - rect->y1,
979                              pixel);
980             }
981
982             pixman_region32_fini (&fill_region);
983             return TRUE;
984         }
985     }
986
987     solid = pixman_image_create_solid_fill (color);
988     if (!solid)
989         return FALSE;
990
991     for (i = 0; i < n_boxes; ++i)
992     {
993         const pixman_box32_t *box = &(boxes[i]);
994
995         pixman_image_composite32 (op, solid, NULL, dest,
996                                   0, 0, 0, 0,
997                                   box->x1, box->y1,
998                                   box->x2 - box->x1, box->y2 - box->y1);
999     }
1000
1001     pixman_image_unref (solid);
1002
1003     return TRUE;
1004 }
1005
1006 /**
1007  * pixman_version:
1008  *
1009  * Returns the version of the pixman library encoded in a single
1010  * integer as per %PIXMAN_VERSION_ENCODE. The encoding ensures that
1011  * later versions compare greater than earlier versions.
1012  *
1013  * A run-time comparison to check that pixman's version is greater than
1014  * or equal to version X.Y.Z could be performed as follows:
1015  *
1016  * <informalexample><programlisting>
1017  * if (pixman_version() >= PIXMAN_VERSION_ENCODE(X,Y,Z)) {...}
1018  * </programlisting></informalexample>
1019  *
1020  * See also pixman_version_string() as well as the compile-time
1021  * equivalents %PIXMAN_VERSION and %PIXMAN_VERSION_STRING.
1022  *
1023  * Return value: the encoded version.
1024  **/
1025 PIXMAN_EXPORT int
1026 pixman_version (void)
1027 {
1028     return PIXMAN_VERSION;
1029 }
1030
1031 /**
1032  * pixman_version_string:
1033  *
1034  * Returns the version of the pixman library as a human-readable string
1035  * of the form "X.Y.Z".
1036  *
1037  * See also pixman_version() as well as the compile-time equivalents
1038  * %PIXMAN_VERSION_STRING and %PIXMAN_VERSION.
1039  *
1040  * Return value: a string containing the version.
1041  **/
1042 PIXMAN_EXPORT const char*
1043 pixman_version_string (void)
1044 {
1045     return PIXMAN_VERSION_STRING;
1046 }
1047
1048 /**
1049  * pixman_format_supported_source:
1050  * @format: A pixman_format_code_t format
1051  *
1052  * Return value: whether the provided format code is a supported
1053  * format for a pixman surface used as a source in
1054  * rendering.
1055  *
1056  * Currently, all pixman_format_code_t values are supported.
1057  **/
1058 PIXMAN_EXPORT pixman_bool_t
1059 pixman_format_supported_source (pixman_format_code_t format)
1060 {
1061     switch (format)
1062     {
1063     /* 32 bpp formats */
1064     case PIXMAN_a2b10g10r10:
1065     case PIXMAN_x2b10g10r10:
1066     case PIXMAN_a2r10g10b10:
1067     case PIXMAN_x2r10g10b10:
1068     case PIXMAN_a8r8g8b8:
1069     case PIXMAN_x8r8g8b8:
1070     case PIXMAN_a8b8g8r8:
1071     case PIXMAN_x8b8g8r8:
1072     case PIXMAN_b8g8r8a8:
1073     case PIXMAN_b8g8r8x8:
1074     case PIXMAN_r8g8b8:
1075     case PIXMAN_b8g8r8:
1076     case PIXMAN_r5g6b5:
1077     case PIXMAN_b5g6r5:
1078     /* 16 bpp formats */
1079     case PIXMAN_a1r5g5b5:
1080     case PIXMAN_x1r5g5b5:
1081     case PIXMAN_a1b5g5r5:
1082     case PIXMAN_x1b5g5r5:
1083     case PIXMAN_a4r4g4b4:
1084     case PIXMAN_x4r4g4b4:
1085     case PIXMAN_a4b4g4r4:
1086     case PIXMAN_x4b4g4r4:
1087     /* 8bpp formats */
1088     case PIXMAN_a8:
1089     case PIXMAN_r3g3b2:
1090     case PIXMAN_b2g3r3:
1091     case PIXMAN_a2r2g2b2:
1092     case PIXMAN_a2b2g2r2:
1093     case PIXMAN_c8:
1094     case PIXMAN_g8:
1095     case PIXMAN_x4a4:
1096     /* Collides with PIXMAN_c8
1097        case PIXMAN_x4c4:
1098      */
1099     /* Collides with PIXMAN_g8
1100        case PIXMAN_x4g4:
1101      */
1102     /* 4bpp formats */
1103     case PIXMAN_a4:
1104     case PIXMAN_r1g2b1:
1105     case PIXMAN_b1g2r1:
1106     case PIXMAN_a1r1g1b1:
1107     case PIXMAN_a1b1g1r1:
1108     case PIXMAN_c4:
1109     case PIXMAN_g4:
1110     /* 1bpp formats */
1111     case PIXMAN_a1:
1112     case PIXMAN_g1:
1113     /* YUV formats */
1114     case PIXMAN_yuy2:
1115     case PIXMAN_yv12:
1116         return TRUE;
1117
1118     default:
1119         return FALSE;
1120     }
1121 }
1122
1123 /**
1124  * pixman_format_supported_destination:
1125  * @format: A pixman_format_code_t format
1126  *
1127  * Return value: whether the provided format code is a supported
1128  * format for a pixman surface used as a destination in
1129  * rendering.
1130  *
1131  * Currently, all pixman_format_code_t values are supported
1132  * except for the YUV formats.
1133  **/
1134 PIXMAN_EXPORT pixman_bool_t
1135 pixman_format_supported_destination (pixman_format_code_t format)
1136 {
1137     /* YUV formats cannot be written to at the moment */
1138     if (format == PIXMAN_yuy2 || format == PIXMAN_yv12)
1139         return FALSE;
1140
1141     return pixman_format_supported_source (format);
1142 }
1143
1144 PIXMAN_EXPORT pixman_bool_t
1145 pixman_compute_composite_region (pixman_region16_t * region,
1146                                  pixman_image_t *    src_image,
1147                                  pixman_image_t *    mask_image,
1148                                  pixman_image_t *    dst_image,
1149                                  int16_t             src_x,
1150                                  int16_t             src_y,
1151                                  int16_t             mask_x,
1152                                  int16_t             mask_y,
1153                                  int16_t             dest_x,
1154                                  int16_t             dest_y,
1155                                  uint16_t            width,
1156                                  uint16_t            height)
1157 {
1158     pixman_region32_t r32;
1159     pixman_bool_t retval;
1160
1161     pixman_region32_init (&r32);
1162
1163     retval = pixman_compute_composite_region32 (
1164         &r32, src_image, mask_image, dst_image,
1165         src_x, src_y, mask_x, mask_y, dest_x, dest_y,
1166         width, height);
1167
1168     if (retval)
1169     {
1170         if (!pixman_region16_copy_from_region32 (region, &r32))
1171             retval = FALSE;
1172     }
1173
1174     pixman_region32_fini (&r32);
1175     return retval;
1176 }