Merge branch 'eliminate-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 w, h, w_this, h_this;
351     int x_msk, y_msk, x_src, y_src, x_dst, y_dst;
352     int src_dy = src_y - dest_y;
353     int src_dx = src_x - dest_x;
354     int mask_dy = mask_y - dest_y;
355     int mask_dx = mask_x - dest_x;
356     const pixman_box32_t *pbox;
357     int n;
358
359     pbox = pixman_region32_rectangles (region, &n);
360
361     /* Fast path for non-repeating sources */
362     if (!src_repeat && !mask_repeat)
363     {
364        while (n--)
365        {
366            (*composite_rect) (imp, op,
367                               src_image, mask_image, dst_image,
368                               pbox->x1 + src_dx,
369                               pbox->y1 + src_dy,
370                               pbox->x1 + mask_dx,
371                               pbox->y1 + mask_dy,
372                               pbox->x1,
373                               pbox->y1,
374                               pbox->x2 - pbox->x1,
375                               pbox->y2 - pbox->y1);
376            
377            pbox++;
378        }
379
380        return;
381     }
382     
383     while (n--)
384     {
385         h = pbox->y2 - pbox->y1;
386         y_src = pbox->y1 + src_dy;
387         y_msk = pbox->y1 + mask_dy;
388         y_dst = pbox->y1;
389
390         while (h)
391         {
392             h_this = h;
393             w = pbox->x2 - pbox->x1;
394             x_src = pbox->x1 + src_dx;
395             x_msk = pbox->x1 + mask_dx;
396             x_dst = pbox->x1;
397
398             if (mask_repeat)
399             {
400                 y_msk = MOD (y_msk, mask_image->bits.height);
401                 if (h_this > mask_image->bits.height - y_msk)
402                     h_this = mask_image->bits.height - y_msk;
403             }
404
405             if (src_repeat)
406             {
407                 y_src = MOD (y_src, src_image->bits.height);
408                 if (h_this > src_image->bits.height - y_src)
409                     h_this = src_image->bits.height - y_src;
410             }
411
412             while (w)
413             {
414                 w_this = w;
415
416                 if (mask_repeat)
417                 {
418                     x_msk = MOD (x_msk, mask_image->bits.width);
419                     if (w_this > mask_image->bits.width - x_msk)
420                         w_this = mask_image->bits.width - x_msk;
421                 }
422
423                 if (src_repeat)
424                 {
425                     x_src = MOD (x_src, src_image->bits.width);
426                     if (w_this > src_image->bits.width - x_src)
427                         w_this = src_image->bits.width - x_src;
428                 }
429
430                 (*composite_rect) (imp, op,
431                                    src_image, mask_image, dst_image,
432                                    x_src, y_src, x_msk, y_msk, x_dst, y_dst,
433                                    w_this, h_this);
434                 w -= w_this;
435
436                 x_src += w_this;
437                 x_msk += w_this;
438                 x_dst += w_this;
439             }
440
441             h -= h_this;
442             y_src += h_this;
443             y_msk += h_this;
444             y_dst += h_this;
445         }
446
447         pbox++;
448     }
449 }
450
451 static void
452 get_image_info (pixman_image_t       *image,
453                 pixman_format_code_t *code,
454                 uint32_t             *flags)
455 {
456     *flags = 0;
457     
458     if (!image->common.transform)
459     {
460         *flags |= FAST_PATH_ID_TRANSFORM;
461     }
462     else
463     {
464         if (image->common.transform->matrix[0][1] == 0 &&
465             image->common.transform->matrix[1][0] == 0 &&
466             image->common.transform->matrix[2][0] == 0 &&
467             image->common.transform->matrix[2][1] == 0 &&
468             image->common.transform->matrix[2][2] == pixman_fixed_1)
469         {
470             *flags |= FAST_PATH_SCALE_TRANSFORM;
471         }
472     }
473     
474     if (!image->common.alpha_map)
475         *flags |= FAST_PATH_NO_ALPHA_MAP;
476     
477     if (image->common.filter != PIXMAN_FILTER_CONVOLUTION)
478     {
479         *flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
480         
481         if (image->common.filter == PIXMAN_FILTER_NEAREST)
482             *flags |= FAST_PATH_NEAREST_FILTER;
483     }
484     
485     if (image->common.repeat != PIXMAN_REPEAT_PAD)
486         *flags |= FAST_PATH_NO_PAD_REPEAT;
487     
488     if (image->common.repeat != PIXMAN_REPEAT_REFLECT)
489         *flags |= FAST_PATH_NO_REFLECT_REPEAT;
490     
491     *flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NO_WIDE_FORMAT);
492     if (image->type == BITS)
493     {
494         if (image->bits.read_func || image->bits.write_func)
495             *flags &= ~FAST_PATH_NO_ACCESSORS;
496         
497         if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
498             *flags &= ~FAST_PATH_NO_WIDE_FORMAT;
499     }
500     
501     if (image->common.component_alpha)
502         *flags |= FAST_PATH_COMPONENT_ALPHA;
503     else
504         *flags |= FAST_PATH_UNIFIED_ALPHA;
505     
506     if (_pixman_image_is_solid (image))
507         *code = PIXMAN_solid;
508     else if (image->common.type == BITS)
509         *code = image->bits.format;
510     else
511         *code = PIXMAN_unknown;
512 }
513
514 static force_inline pixman_bool_t
515 image_covers (pixman_image_t *image,
516               pixman_box32_t *extents,
517               int             x,
518               int             y)
519 {
520     if (image->common.type == BITS &&
521         image->common.repeat == PIXMAN_REPEAT_NONE)
522     {
523         if (x > extents->x1 || y > extents->y1 ||
524             x + image->bits.width < extents->x2 ||
525             y + image->bits.height < extents->y2)
526         {
527             return FALSE;
528         }
529     }
530
531     return TRUE;
532 }
533
534 static void
535 do_composite (pixman_implementation_t *imp,
536               pixman_op_t              op,
537               pixman_image_t          *src,
538               pixman_image_t          *mask,
539               pixman_image_t          *dest,
540               int                      src_x,
541               int                      src_y,
542               int                      mask_x,
543               int                      mask_y,
544               int                      dest_x,
545               int                      dest_y,
546               int                      width,
547               int                      height)
548 {
549     pixman_format_code_t src_format, mask_format, dest_format;
550     uint32_t src_flags, mask_flags, dest_flags;
551     pixman_bool_t src_repeat, mask_repeat;
552     pixman_region32_t region;
553     pixman_box32_t *extents;
554
555     get_image_info (src,  &src_format,  &src_flags);
556     if (mask)
557     {
558         get_image_info (mask, &mask_format, &mask_flags);
559     }
560     else
561     {
562         mask_format = PIXMAN_null;
563         mask_flags = 0;
564     }
565     get_image_info (dest, &dest_format, &dest_flags);
566     
567     /* Check for pixbufs */
568     if ((mask_format == PIXMAN_a8r8g8b8 || mask_format == PIXMAN_a8b8g8r8) &&
569         (src->type == BITS && src->bits.bits == mask->bits.bits)           &&
570         (src->common.repeat == mask->common.repeat)                        &&
571         (src_x == mask_x && src_y == mask_y))
572     {
573         if (src_format == PIXMAN_x8b8g8r8)
574             src_format = mask_format = PIXMAN_pixbuf;
575         else if (src_format == PIXMAN_x8r8g8b8)
576             src_format = mask_format = PIXMAN_rpixbuf;
577     }
578             
579     src_repeat =
580         src->type == BITS                                       &&
581         src_flags & FAST_PATH_ID_TRANSFORM                      &&
582         src->common.repeat == PIXMAN_REPEAT_NORMAL              &&
583         src_format != PIXMAN_solid;
584     
585     mask_repeat =
586         mask                                                    &&
587         mask->type == BITS                                      &&
588         mask_flags & FAST_PATH_ID_TRANSFORM                     &&
589         mask->common.repeat == PIXMAN_REPEAT_NORMAL             &&
590         mask_format != PIXMAN_solid;
591     
592     pixman_region32_init (&region);
593     
594     if (!pixman_compute_composite_region32 (
595             &region, src, mask, dest,
596             src_x, src_y, mask_x, mask_y, dest_x, dest_y, width, height))
597     {
598         return;
599     }
600     
601     extents = pixman_region32_extents (&region);
602     
603     if (image_covers (src, extents, dest_x - src_x, dest_y - src_y))
604         src_flags |= FAST_PATH_COVERS_CLIP;
605     
606     if (mask && image_covers (mask, extents, dest_x - mask_x, dest_y - mask_y))
607         mask_flags |= FAST_PATH_COVERS_CLIP;
608             
609     while (imp)
610     {
611         const pixman_fast_path_t *info;
612             
613         for (info = imp->fast_paths; info->op != PIXMAN_OP_NONE; ++info)
614         {
615             if ((info->op == op || info->op == PIXMAN_OP_any)           &&
616                 /* src */
617                 ((info->src_format == src_format) ||
618                  (info->src_format == PIXMAN_any))                      &&
619                 (info->src_flags & src_flags) == info->src_flags        &&
620                 /* mask */
621                 ((info->mask_format == mask_format) ||
622                  (info->mask_format == PIXMAN_any))                     &&
623                 (info->mask_flags & mask_flags) == info->mask_flags     &&
624                 /* dest */
625                 ((info->dest_format == dest_format) ||
626                  (info->dest_format == PIXMAN_any))                     &&
627                 (info->dest_flags & dest_flags) == info->dest_flags)
628             {
629                 walk_region_internal (imp, op,
630                                       src, mask, dest,
631                                       src_x, src_y, mask_x, mask_y,
632                                       dest_x, dest_y,
633                                       width, height,
634                                       src_repeat, mask_repeat,
635                                       &region,
636                                       info->func);
637                 
638                 goto done;
639             }
640         }
641         
642         imp = imp->delegate;
643     }
644
645 done:
646     pixman_region32_fini (&region);
647 }
648
649 /*
650  * Work around GCC bug causing crashes in Mozilla with SSE2
651  *
652  * When using -msse, gcc generates movdqa instructions assuming that
653  * the stack is 16 byte aligned. Unfortunately some applications, such
654  * as Mozilla and Mono, end up aligning the stack to 4 bytes, which
655  * causes the movdqa instructions to fail.
656  *
657  * The __force_align_arg_pointer__ makes gcc generate a prologue that
658  * realigns the stack pointer to 16 bytes.
659  *
660  * On x86-64 this is not necessary because the standard ABI already
661  * calls for a 16 byte aligned stack.
662  *
663  * See https://bugs.freedesktop.org/show_bug.cgi?id=15693
664  */
665 #if defined (USE_SSE2) && defined(__GNUC__) && !defined(__x86_64__) && !defined(__amd64__)
666 __attribute__((__force_align_arg_pointer__))
667 #endif
668 PIXMAN_EXPORT void
669 pixman_image_composite (pixman_op_t      op,
670                         pixman_image_t * src,
671                         pixman_image_t * mask,
672                         pixman_image_t * dest,
673                         int16_t          src_x,
674                         int16_t          src_y,
675                         int16_t          mask_x,
676                         int16_t          mask_y,
677                         int16_t          dest_x,
678                         int16_t          dest_y,
679                         uint16_t         width,
680                         uint16_t         height)
681 {
682     pixman_image_composite32 (op, src, mask, dest, src_x, src_y, 
683                               mask_x, mask_y, dest_x, dest_y, width, height);
684 }
685
686 PIXMAN_EXPORT void
687 pixman_image_composite32 (pixman_op_t      op,
688                           pixman_image_t * src,
689                           pixman_image_t * mask,
690                           pixman_image_t * dest,
691                           int32_t          src_x,
692                           int32_t          src_y,
693                           int32_t          mask_x,
694                           int32_t          mask_y,
695                           int32_t          dest_x,
696                           int32_t          dest_y,
697                           int32_t          width,
698                           int32_t          height)
699 {
700     uint32_t *src_bits;
701     int src_dx, src_dy;
702     uint32_t *mask_bits;
703     int mask_dx, mask_dy;
704     uint32_t *dest_bits;
705     int dest_dx, dest_dy;
706     pixman_bool_t need_workaround;
707
708     _pixman_image_validate (src);
709     if (mask)
710         _pixman_image_validate (mask);
711     _pixman_image_validate (dest);
712     
713     /*
714      * Check if we can replace our operator by a simpler one
715      * if the src or dest are opaque. The output operator should be
716      * mathematically equivalent to the source.
717      */
718     op = pixman_optimize_operator(op, src, mask, dest);
719     if (op == PIXMAN_OP_DST             ||
720         op == PIXMAN_OP_CONJOINT_DST    ||
721         op == PIXMAN_OP_DISJOINT_DST)
722     {
723         return;
724     }
725
726     if (!imp)
727         imp = _pixman_choose_implementation ();
728
729     need_workaround =
730         (src->common.need_workaround)                   ||
731         (mask && mask->common.need_workaround)          ||
732         (dest->common.need_workaround);
733    
734     if (need_workaround)
735     {
736         apply_workaround (src, &src_x, &src_y, &src_bits, &src_dx, &src_dy);
737         apply_workaround (mask, &mask_x, &mask_y, &mask_bits, &mask_dx, &mask_dy);
738         apply_workaround (dest, &dest_x, &dest_y, &dest_bits, &dest_dx, &dest_dy);
739     }
740
741     do_composite (imp, op,
742                   src, mask, dest,
743                   src_x, src_y,
744                   mask_x, mask_y,
745                   dest_x, dest_y,
746                   width, height);
747     
748     if (need_workaround)
749     {
750         if (src->common.need_workaround)
751             unapply_workaround (src, src_bits, src_dx, src_dy);
752         if (mask && mask->common.need_workaround)
753             unapply_workaround (mask, mask_bits, mask_dx, mask_dy);
754         if (dest->common.need_workaround)
755             unapply_workaround (dest, dest_bits, dest_dx, dest_dy);
756     }
757 }
758
759 PIXMAN_EXPORT pixman_bool_t
760 pixman_blt (uint32_t *src_bits,
761             uint32_t *dst_bits,
762             int       src_stride,
763             int       dst_stride,
764             int       src_bpp,
765             int       dst_bpp,
766             int       src_x,
767             int       src_y,
768             int       dst_x,
769             int       dst_y,
770             int       width,
771             int       height)
772 {
773     if (!imp)
774         imp = _pixman_choose_implementation ();
775
776     return _pixman_implementation_blt (imp, src_bits, dst_bits, src_stride, dst_stride,
777                                        src_bpp, dst_bpp,
778                                        src_x, src_y,
779                                        dst_x, dst_y,
780                                        width, height);
781 }
782
783 PIXMAN_EXPORT pixman_bool_t
784 pixman_fill (uint32_t *bits,
785              int       stride,
786              int       bpp,
787              int       x,
788              int       y,
789              int       width,
790              int       height,
791              uint32_t xor)
792 {
793     if (!imp)
794         imp = _pixman_choose_implementation ();
795
796     return _pixman_implementation_fill (imp, bits, stride, bpp, x, y, width, height, xor);
797 }
798
799 static uint32_t
800 color_to_uint32 (const pixman_color_t *color)
801 {
802     return
803         (color->alpha >> 8 << 24) |
804         (color->red >> 8 << 16) |
805         (color->green & 0xff00) |
806         (color->blue >> 8);
807 }
808
809 static pixman_bool_t
810 color_to_pixel (pixman_color_t *     color,
811                 uint32_t *           pixel,
812                 pixman_format_code_t format)
813 {
814     uint32_t c = color_to_uint32 (color);
815
816     if (!(format == PIXMAN_a8r8g8b8     ||
817           format == PIXMAN_x8r8g8b8     ||
818           format == PIXMAN_a8b8g8r8     ||
819           format == PIXMAN_x8b8g8r8     ||
820           format == PIXMAN_b8g8r8a8     ||
821           format == PIXMAN_b8g8r8x8     ||
822           format == PIXMAN_r5g6b5       ||
823           format == PIXMAN_b5g6r5       ||
824           format == PIXMAN_a8))
825     {
826         return FALSE;
827     }
828
829     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
830     {
831         c = ((c & 0xff000000) >>  0) |
832             ((c & 0x00ff0000) >> 16) |
833             ((c & 0x0000ff00) >>  0) |
834             ((c & 0x000000ff) << 16);
835     }
836     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
837     {
838         c = ((c & 0xff000000) >> 24) |
839             ((c & 0x00ff0000) >>  8) |
840             ((c & 0x0000ff00) <<  8) |
841             ((c & 0x000000ff) << 24);
842     }
843
844     if (format == PIXMAN_a8)
845         c = c >> 24;
846     else if (format == PIXMAN_r5g6b5 ||
847              format == PIXMAN_b5g6r5)
848         c = CONVERT_8888_TO_0565 (c);
849
850 #if 0
851     printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
852     printf ("pixel: %x\n", c);
853 #endif
854
855     *pixel = c;
856     return TRUE;
857 }
858
859 PIXMAN_EXPORT pixman_bool_t
860 pixman_image_fill_rectangles (pixman_op_t                 op,
861                               pixman_image_t *            dest,
862                               pixman_color_t *            color,
863                               int                         n_rects,
864                               const pixman_rectangle16_t *rects)
865 {
866     pixman_box32_t stack_boxes[6];
867     pixman_box32_t *boxes;
868     pixman_bool_t result;
869     int i;
870
871     if (n_rects > 6)
872     {
873         boxes = pixman_malloc_ab (sizeof (pixman_box32_t), n_rects);
874         if (boxes == NULL)
875             return FALSE;
876     }
877     else
878     {
879         boxes = stack_boxes;
880     }
881
882     for (i = 0; i < n_rects; ++i)
883     {
884         boxes[i].x1 = rects[i].x;
885         boxes[i].y1 = rects[i].y;
886         boxes[i].x2 = boxes[i].x1 + rects[i].width;
887         boxes[i].y2 = boxes[i].y1 + rects[i].height;
888     }
889
890     result = pixman_image_fill_boxes (op, dest, color, n_rects, boxes);
891
892     if (boxes != stack_boxes)
893         free (boxes);
894     
895     return result;
896 }
897
898 PIXMAN_EXPORT pixman_bool_t
899 pixman_image_fill_boxes (pixman_op_t           op,
900                          pixman_image_t *      dest,
901                          pixman_color_t *      color,
902                          int                   n_boxes,
903                          const pixman_box32_t *boxes)
904 {
905     pixman_image_t *solid;
906     pixman_color_t c;
907     int i;
908
909     _pixman_image_validate (dest);
910     
911     if (color->alpha == 0xffff)
912     {
913         if (op == PIXMAN_OP_OVER)
914             op = PIXMAN_OP_SRC;
915     }
916
917     if (op == PIXMAN_OP_CLEAR)
918     {
919         c.red = 0;
920         c.green = 0;
921         c.blue = 0;
922         c.alpha = 0;
923
924         color = &c;
925
926         op = PIXMAN_OP_SRC;
927     }
928
929     if (op == PIXMAN_OP_SRC)
930     {
931         uint32_t pixel;
932
933         if (color_to_pixel (color, &pixel, dest->bits.format))
934         {
935             pixman_region32_t fill_region;
936             int n_rects, j;
937             pixman_box32_t *rects;
938
939             if (!pixman_region32_init_rects (&fill_region, boxes, n_boxes))
940                 return FALSE;
941
942             if (dest->common.have_clip_region)
943             {
944                 if (!pixman_region32_intersect (&fill_region,
945                                                 &fill_region,
946                                                 &dest->common.clip_region))
947                     return FALSE;
948             }
949
950             rects = pixman_region32_rectangles (&fill_region, &n_rects);
951             for (j = 0; j < n_rects; ++j)
952             {
953                 const pixman_box32_t *rect = &(rects[j]);
954                 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
955                              rect->x1, rect->y1, rect->x2 - rect->x1, rect->y2 - rect->y1,
956                              pixel);
957             }
958
959             pixman_region32_fini (&fill_region);
960             return TRUE;
961         }
962     }
963
964     solid = pixman_image_create_solid_fill (color);
965     if (!solid)
966         return FALSE;
967
968     for (i = 0; i < n_boxes; ++i)
969     {
970         const pixman_box32_t *box = &(boxes[i]);
971
972         pixman_image_composite32 (op, solid, NULL, dest,
973                                   0, 0, 0, 0,
974                                   box->x1, box->y1,
975                                   box->x2 - box->x1, box->y2 - box->y1);
976     }
977
978     pixman_image_unref (solid);
979
980     return TRUE;
981 }
982
983 /**
984  * pixman_version:
985  *
986  * Returns the version of the pixman library encoded in a single
987  * integer as per %PIXMAN_VERSION_ENCODE. The encoding ensures that
988  * later versions compare greater than earlier versions.
989  *
990  * A run-time comparison to check that pixman's version is greater than
991  * or equal to version X.Y.Z could be performed as follows:
992  *
993  * <informalexample><programlisting>
994  * if (pixman_version() >= PIXMAN_VERSION_ENCODE(X,Y,Z)) {...}
995  * </programlisting></informalexample>
996  *
997  * See also pixman_version_string() as well as the compile-time
998  * equivalents %PIXMAN_VERSION and %PIXMAN_VERSION_STRING.
999  *
1000  * Return value: the encoded version.
1001  **/
1002 PIXMAN_EXPORT int
1003 pixman_version (void)
1004 {
1005     return PIXMAN_VERSION;
1006 }
1007
1008 /**
1009  * pixman_version_string:
1010  *
1011  * Returns the version of the pixman library as a human-readable string
1012  * of the form "X.Y.Z".
1013  *
1014  * See also pixman_version() as well as the compile-time equivalents
1015  * %PIXMAN_VERSION_STRING and %PIXMAN_VERSION.
1016  *
1017  * Return value: a string containing the version.
1018  **/
1019 PIXMAN_EXPORT const char*
1020 pixman_version_string (void)
1021 {
1022     return PIXMAN_VERSION_STRING;
1023 }
1024
1025 /**
1026  * pixman_format_supported_source:
1027  * @format: A pixman_format_code_t format
1028  *
1029  * Return value: whether the provided format code is a supported
1030  * format for a pixman surface used as a source in
1031  * rendering.
1032  *
1033  * Currently, all pixman_format_code_t values are supported.
1034  **/
1035 PIXMAN_EXPORT pixman_bool_t
1036 pixman_format_supported_source (pixman_format_code_t format)
1037 {
1038     switch (format)
1039     {
1040     /* 32 bpp formats */
1041     case PIXMAN_a2b10g10r10:
1042     case PIXMAN_x2b10g10r10:
1043     case PIXMAN_a2r10g10b10:
1044     case PIXMAN_x2r10g10b10:
1045     case PIXMAN_a8r8g8b8:
1046     case PIXMAN_x8r8g8b8:
1047     case PIXMAN_a8b8g8r8:
1048     case PIXMAN_x8b8g8r8:
1049     case PIXMAN_b8g8r8a8:
1050     case PIXMAN_b8g8r8x8:
1051     case PIXMAN_r8g8b8:
1052     case PIXMAN_b8g8r8:
1053     case PIXMAN_r5g6b5:
1054     case PIXMAN_b5g6r5:
1055     /* 16 bpp formats */
1056     case PIXMAN_a1r5g5b5:
1057     case PIXMAN_x1r5g5b5:
1058     case PIXMAN_a1b5g5r5:
1059     case PIXMAN_x1b5g5r5:
1060     case PIXMAN_a4r4g4b4:
1061     case PIXMAN_x4r4g4b4:
1062     case PIXMAN_a4b4g4r4:
1063     case PIXMAN_x4b4g4r4:
1064     /* 8bpp formats */
1065     case PIXMAN_a8:
1066     case PIXMAN_r3g3b2:
1067     case PIXMAN_b2g3r3:
1068     case PIXMAN_a2r2g2b2:
1069     case PIXMAN_a2b2g2r2:
1070     case PIXMAN_c8:
1071     case PIXMAN_g8:
1072     case PIXMAN_x4a4:
1073     /* Collides with PIXMAN_c8
1074        case PIXMAN_x4c4:
1075      */
1076     /* Collides with PIXMAN_g8
1077        case PIXMAN_x4g4:
1078      */
1079     /* 4bpp formats */
1080     case PIXMAN_a4:
1081     case PIXMAN_r1g2b1:
1082     case PIXMAN_b1g2r1:
1083     case PIXMAN_a1r1g1b1:
1084     case PIXMAN_a1b1g1r1:
1085     case PIXMAN_c4:
1086     case PIXMAN_g4:
1087     /* 1bpp formats */
1088     case PIXMAN_a1:
1089     case PIXMAN_g1:
1090     /* YUV formats */
1091     case PIXMAN_yuy2:
1092     case PIXMAN_yv12:
1093         return TRUE;
1094
1095     default:
1096         return FALSE;
1097     }
1098 }
1099
1100 /**
1101  * pixman_format_supported_destination:
1102  * @format: A pixman_format_code_t format
1103  *
1104  * Return value: whether the provided format code is a supported
1105  * format for a pixman surface used as a destination in
1106  * rendering.
1107  *
1108  * Currently, all pixman_format_code_t values are supported
1109  * except for the YUV formats.
1110  **/
1111 PIXMAN_EXPORT pixman_bool_t
1112 pixman_format_supported_destination (pixman_format_code_t format)
1113 {
1114     /* YUV formats cannot be written to at the moment */
1115     if (format == PIXMAN_yuy2 || format == PIXMAN_yv12)
1116         return FALSE;
1117
1118     return pixman_format_supported_source (format);
1119 }
1120
1121 PIXMAN_EXPORT pixman_bool_t
1122 pixman_compute_composite_region (pixman_region16_t * region,
1123                                  pixman_image_t *    src_image,
1124                                  pixman_image_t *    mask_image,
1125                                  pixman_image_t *    dst_image,
1126                                  int16_t             src_x,
1127                                  int16_t             src_y,
1128                                  int16_t             mask_x,
1129                                  int16_t             mask_y,
1130                                  int16_t             dest_x,
1131                                  int16_t             dest_y,
1132                                  uint16_t            width,
1133                                  uint16_t            height)
1134 {
1135     pixman_region32_t r32;
1136     pixman_bool_t retval;
1137
1138     pixman_region32_init (&r32);
1139
1140     retval = pixman_compute_composite_region32 (
1141         &r32, src_image, mask_image, dst_image,
1142         src_x, src_y, mask_x, mask_y, dest_x, dest_y,
1143         width, height);
1144
1145     if (retval)
1146     {
1147         if (!pixman_region16_copy_from_region32 (region, &r32))
1148             retval = FALSE;
1149     }
1150
1151     pixman_region32_fini (&r32);
1152     return retval;
1153 }