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