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