Split computation of sample area into own function
[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 static pixman_implementation_t *global_implementation;
34
35 #ifdef TOOLCHAIN_SUPPORTS_ATTRIBUTE_CONSTRUCTOR
36 static void __attribute__((constructor))
37 pixman_constructor (void)
38 {
39     global_implementation = _pixman_choose_implementation ();
40 }
41 #endif
42
43 static force_inline pixman_implementation_t *
44 get_implementation (void)
45 {
46 #ifndef TOOLCHAIN_SUPPORTS_ATTRIBUTE_CONSTRUCTOR
47     if (!global_implementation)
48         global_implementation = _pixman_choose_implementation ();
49 #endif
50     return global_implementation;
51 }
52
53 typedef struct operator_info_t operator_info_t;
54
55 struct operator_info_t
56 {
57     uint8_t     opaque_info[4];
58 };
59
60 #define PACK(neither, src, dest, both)                  \
61     {{      (uint8_t)PIXMAN_OP_ ## neither,             \
62             (uint8_t)PIXMAN_OP_ ## src,                 \
63             (uint8_t)PIXMAN_OP_ ## dest,                \
64             (uint8_t)PIXMAN_OP_ ## both         }}
65
66 static const operator_info_t operator_table[] =
67 {
68     /*    Neither Opaque         Src Opaque             Dst Opaque             Both Opaque */
69     PACK (CLEAR,                 CLEAR,                 CLEAR,                 CLEAR),
70     PACK (SRC,                   SRC,                   SRC,                   SRC),
71     PACK (DST,                   DST,                   DST,                   DST),
72     PACK (OVER,                  SRC,                   OVER,                  SRC),
73     PACK (OVER_REVERSE,          OVER_REVERSE,          DST,                   DST),
74     PACK (IN,                    IN,                    SRC,                   SRC),
75     PACK (IN_REVERSE,            DST,                   IN_REVERSE,            DST),
76     PACK (OUT,                   OUT,                   CLEAR,                 CLEAR),
77     PACK (OUT_REVERSE,           CLEAR,                 OUT_REVERSE,           CLEAR),
78     PACK (ATOP,                  IN,                    OVER,                  SRC),
79     PACK (ATOP_REVERSE,          OVER_REVERSE,          IN_REVERSE,            DST),
80     PACK (XOR,                   OUT,                   OUT_REVERSE,           CLEAR),
81     PACK (ADD,                   ADD,                   ADD,                   ADD),
82     PACK (SATURATE,              OVER_REVERSE,          DST,                   DST),
83
84     {{ 0 /* 0x0e */ }},
85     {{ 0 /* 0x0f */ }},
86
87     PACK (CLEAR,                 CLEAR,                 CLEAR,                 CLEAR),
88     PACK (SRC,                   SRC,                   SRC,                   SRC),
89     PACK (DST,                   DST,                   DST,                   DST),
90     PACK (DISJOINT_OVER,         DISJOINT_OVER,         DISJOINT_OVER,         DISJOINT_OVER),
91     PACK (DISJOINT_OVER_REVERSE, DISJOINT_OVER_REVERSE, DISJOINT_OVER_REVERSE, DISJOINT_OVER_REVERSE),
92     PACK (DISJOINT_IN,           DISJOINT_IN,           DISJOINT_IN,           DISJOINT_IN),
93     PACK (DISJOINT_IN_REVERSE,   DISJOINT_IN_REVERSE,   DISJOINT_IN_REVERSE,   DISJOINT_IN_REVERSE),
94     PACK (DISJOINT_OUT,          DISJOINT_OUT,          DISJOINT_OUT,          DISJOINT_OUT),
95     PACK (DISJOINT_OUT_REVERSE,  DISJOINT_OUT_REVERSE,  DISJOINT_OUT_REVERSE,  DISJOINT_OUT_REVERSE),
96     PACK (DISJOINT_ATOP,         DISJOINT_ATOP,         DISJOINT_ATOP,         DISJOINT_ATOP),
97     PACK (DISJOINT_ATOP_REVERSE, DISJOINT_ATOP_REVERSE, DISJOINT_ATOP_REVERSE, DISJOINT_ATOP_REVERSE),
98     PACK (DISJOINT_XOR,          DISJOINT_XOR,          DISJOINT_XOR,          DISJOINT_XOR),
99
100     {{ 0 /* 0x1c */ }},
101     {{ 0 /* 0x1d */ }},
102     {{ 0 /* 0x1e */ }},
103     {{ 0 /* 0x1f */ }},
104
105     PACK (CLEAR,                 CLEAR,                 CLEAR,                 CLEAR),
106     PACK (SRC,                   SRC,                   SRC,                   SRC),
107     PACK (DST,                   DST,                   DST,                   DST),
108     PACK (CONJOINT_OVER,         CONJOINT_OVER,         CONJOINT_OVER,         CONJOINT_OVER),
109     PACK (CONJOINT_OVER_REVERSE, CONJOINT_OVER_REVERSE, CONJOINT_OVER_REVERSE, CONJOINT_OVER_REVERSE),
110     PACK (CONJOINT_IN,           CONJOINT_IN,           CONJOINT_IN,           CONJOINT_IN),
111     PACK (CONJOINT_IN_REVERSE,   CONJOINT_IN_REVERSE,   CONJOINT_IN_REVERSE,   CONJOINT_IN_REVERSE),
112     PACK (CONJOINT_OUT,          CONJOINT_OUT,          CONJOINT_OUT,          CONJOINT_OUT),
113     PACK (CONJOINT_OUT_REVERSE,  CONJOINT_OUT_REVERSE,  CONJOINT_OUT_REVERSE,  CONJOINT_OUT_REVERSE),
114     PACK (CONJOINT_ATOP,         CONJOINT_ATOP,         CONJOINT_ATOP,         CONJOINT_ATOP),
115     PACK (CONJOINT_ATOP_REVERSE, CONJOINT_ATOP_REVERSE, CONJOINT_ATOP_REVERSE, CONJOINT_ATOP_REVERSE),
116     PACK (CONJOINT_XOR,          CONJOINT_XOR,          CONJOINT_XOR,          CONJOINT_XOR),
117
118     {{ 0 /* 0x2c */ }},
119     {{ 0 /* 0x2d */ }},
120     {{ 0 /* 0x2e */ }},
121     {{ 0 /* 0x2f */ }},
122
123     PACK (MULTIPLY,              MULTIPLY,              MULTIPLY,              MULTIPLY),
124     PACK (SCREEN,                SCREEN,                SCREEN,                SCREEN),
125     PACK (OVERLAY,               OVERLAY,               OVERLAY,               OVERLAY),
126     PACK (DARKEN,                DARKEN,                DARKEN,                DARKEN),
127     PACK (LIGHTEN,               LIGHTEN,               LIGHTEN,               LIGHTEN),
128     PACK (COLOR_DODGE,           COLOR_DODGE,           COLOR_DODGE,           COLOR_DODGE),
129     PACK (COLOR_BURN,            COLOR_BURN,            COLOR_BURN,            COLOR_BURN),
130     PACK (HARD_LIGHT,            HARD_LIGHT,            HARD_LIGHT,            HARD_LIGHT),
131     PACK (SOFT_LIGHT,            SOFT_LIGHT,            SOFT_LIGHT,            SOFT_LIGHT),
132     PACK (DIFFERENCE,            DIFFERENCE,            DIFFERENCE,            DIFFERENCE),
133     PACK (EXCLUSION,             EXCLUSION,             EXCLUSION,             EXCLUSION),
134     PACK (HSL_HUE,               HSL_HUE,               HSL_HUE,               HSL_HUE),
135     PACK (HSL_SATURATION,        HSL_SATURATION,        HSL_SATURATION,        HSL_SATURATION),
136     PACK (HSL_COLOR,             HSL_COLOR,             HSL_COLOR,             HSL_COLOR),
137     PACK (HSL_LUMINOSITY,        HSL_LUMINOSITY,        HSL_LUMINOSITY,        HSL_LUMINOSITY),
138 };
139
140 /*
141  * Optimize the current operator based on opacity of source or destination
142  * The output operator should be mathematically equivalent to the source.
143  */
144 static pixman_op_t
145 optimize_operator (pixman_op_t     op,
146                    uint32_t        src_flags,
147                    uint32_t        mask_flags,
148                    uint32_t        dst_flags)
149 {
150     pixman_bool_t is_source_opaque, is_dest_opaque;
151
152 #define OPAQUE_SHIFT 13
153     
154     COMPILE_TIME_ASSERT (FAST_PATH_IS_OPAQUE == (1 << OPAQUE_SHIFT));
155     
156     is_dest_opaque = (dst_flags & FAST_PATH_IS_OPAQUE);
157     is_source_opaque = ((src_flags & mask_flags) & FAST_PATH_IS_OPAQUE);
158
159     is_dest_opaque >>= OPAQUE_SHIFT - 1;
160     is_source_opaque >>= OPAQUE_SHIFT;
161
162     return operator_table[op].opaque_info[is_dest_opaque | is_source_opaque];
163 }
164
165 /*
166  * Computing composite region
167  */
168 static inline pixman_bool_t
169 clip_general_image (pixman_region32_t * region,
170                     pixman_region32_t * clip,
171                     int                 dx,
172                     int                 dy)
173 {
174     if (pixman_region32_n_rects (region) == 1 &&
175         pixman_region32_n_rects (clip) == 1)
176     {
177         pixman_box32_t *  rbox = pixman_region32_rectangles (region, NULL);
178         pixman_box32_t *  cbox = pixman_region32_rectangles (clip, NULL);
179         int v;
180
181         if (rbox->x1 < (v = cbox->x1 + dx))
182             rbox->x1 = v;
183         if (rbox->x2 > (v = cbox->x2 + dx))
184             rbox->x2 = v;
185         if (rbox->y1 < (v = cbox->y1 + dy))
186             rbox->y1 = v;
187         if (rbox->y2 > (v = cbox->y2 + dy))
188             rbox->y2 = v;
189         if (rbox->x1 >= rbox->x2 || rbox->y1 >= rbox->y2)
190         {
191             pixman_region32_init (region);
192             return FALSE;
193         }
194     }
195     else if (!pixman_region32_not_empty (clip))
196     {
197         return FALSE;
198     }
199     else
200     {
201         if (dx || dy)
202             pixman_region32_translate (region, -dx, -dy);
203
204         if (!pixman_region32_intersect (region, region, clip))
205             return FALSE;
206
207         if (dx || dy)
208             pixman_region32_translate (region, dx, dy);
209     }
210
211     return pixman_region32_not_empty (region);
212 }
213
214 static inline pixman_bool_t
215 clip_source_image (pixman_region32_t * region,
216                    pixman_image_t *    image,
217                    int                 dx,
218                    int                 dy)
219 {
220     /* Source clips are ignored, unless they are explicitly turned on
221      * and the clip in question was set by an X client. (Because if
222      * the clip was not set by a client, then it is a hierarchy
223      * clip and those should always be ignored for sources).
224      */
225     if (!image->common.clip_sources || !image->common.client_clip)
226         return TRUE;
227
228     return clip_general_image (region,
229                                &image->common.clip_region,
230                                dx, dy);
231 }
232
233 /*
234  * returns FALSE if the final region is empty.  Indistinguishable from
235  * an allocation failure, but rendering ignores those anyways.
236  */
237 static pixman_bool_t
238 pixman_compute_composite_region32 (pixman_region32_t * region,
239                                    pixman_image_t *    src_image,
240                                    pixman_image_t *    mask_image,
241                                    pixman_image_t *    dest_image,
242                                    int32_t             src_x,
243                                    int32_t             src_y,
244                                    int32_t             mask_x,
245                                    int32_t             mask_y,
246                                    int32_t             dest_x,
247                                    int32_t             dest_y,
248                                    int32_t             width,
249                                    int32_t             height)
250 {
251     region->extents.x1 = dest_x;
252     region->extents.x2 = dest_x + width;
253     region->extents.y1 = dest_y;
254     region->extents.y2 = dest_y + height;
255
256     region->extents.x1 = MAX (region->extents.x1, 0);
257     region->extents.y1 = MAX (region->extents.y1, 0);
258     region->extents.x2 = MIN (region->extents.x2, dest_image->bits.width);
259     region->extents.y2 = MIN (region->extents.y2, dest_image->bits.height);
260
261     region->data = 0;
262
263     /* Check for empty operation */
264     if (region->extents.x1 >= region->extents.x2 ||
265         region->extents.y1 >= region->extents.y2)
266     {
267         region->extents.x1 = 0;
268         region->extents.x2 = 0;
269         region->extents.y1 = 0;
270         region->extents.y2 = 0;
271         return FALSE;
272     }
273
274     if (dest_image->common.have_clip_region)
275     {
276         if (!clip_general_image (region, &dest_image->common.clip_region, 0, 0))
277             return FALSE;
278     }
279
280     if (dest_image->common.alpha_map)
281     {
282         if (!pixman_region32_intersect_rect (region, region,
283                                              dest_image->common.alpha_origin_x,
284                                              dest_image->common.alpha_origin_y,
285                                              dest_image->common.alpha_map->width,
286                                              dest_image->common.alpha_map->height))
287         {
288             return FALSE;
289         }
290         if (!pixman_region32_not_empty (region))
291             return FALSE;
292         if (dest_image->common.alpha_map->common.have_clip_region)
293         {
294             if (!clip_general_image (region, &dest_image->common.alpha_map->common.clip_region,
295                                      -dest_image->common.alpha_origin_x,
296                                      -dest_image->common.alpha_origin_y))
297             {
298                 return FALSE;
299             }
300         }
301     }
302
303     /* clip against src */
304     if (src_image->common.have_clip_region)
305     {
306         if (!clip_source_image (region, src_image, dest_x - src_x, dest_y - src_y))
307             return FALSE;
308     }
309     if (src_image->common.alpha_map && src_image->common.alpha_map->common.have_clip_region)
310     {
311         if (!clip_source_image (region, (pixman_image_t *)src_image->common.alpha_map,
312                                 dest_x - (src_x - src_image->common.alpha_origin_x),
313                                 dest_y - (src_y - src_image->common.alpha_origin_y)))
314         {
315             return FALSE;
316         }
317     }
318     /* clip against mask */
319     if (mask_image && mask_image->common.have_clip_region)
320     {
321         if (!clip_source_image (region, mask_image, dest_x - mask_x, dest_y - mask_y))
322             return FALSE;
323
324         if (mask_image->common.alpha_map && mask_image->common.alpha_map->common.have_clip_region)
325         {
326             if (!clip_source_image (region, (pixman_image_t *)mask_image->common.alpha_map,
327                                     dest_x - (mask_x - mask_image->common.alpha_origin_x),
328                                     dest_y - (mask_y - mask_image->common.alpha_origin_y)))
329             {
330                 return FALSE;
331             }
332         }
333     }
334
335     return TRUE;
336 }
337
338 #define N_CACHED_FAST_PATHS 8
339
340 typedef struct
341 {
342     struct
343     {
344         pixman_implementation_t *       imp;
345         pixman_fast_path_t              fast_path;
346     } cache [N_CACHED_FAST_PATHS];
347 } cache_t;
348
349 PIXMAN_DEFINE_THREAD_LOCAL (cache_t, fast_path_cache);
350
351 static force_inline pixman_bool_t
352 lookup_composite_function (pixman_op_t                  op,
353                            pixman_format_code_t         src_format,
354                            uint32_t                     src_flags,
355                            pixman_format_code_t         mask_format,
356                            uint32_t                     mask_flags,
357                            pixman_format_code_t         dest_format,
358                            uint32_t                     dest_flags,
359                            pixman_implementation_t    **out_imp,
360                            pixman_composite_func_t     *out_func)
361 {
362     pixman_implementation_t *imp;
363     cache_t *cache;
364     int i;
365
366     /* Check cache for fast paths */
367     cache = PIXMAN_GET_THREAD_LOCAL (fast_path_cache);
368
369     for (i = 0; i < N_CACHED_FAST_PATHS; ++i)
370     {
371         const pixman_fast_path_t *info = &(cache->cache[i].fast_path);
372
373         /* Note that we check for equality here, not whether
374          * the cached fast path matches. This is to prevent
375          * us from selecting an overly general fast path
376          * when a more specific one would work.
377          */
378         if (info->op == op                      &&
379             info->src_format == src_format      &&
380             info->mask_format == mask_format    &&
381             info->dest_format == dest_format    &&
382             info->src_flags == src_flags        &&
383             info->mask_flags == mask_flags      &&
384             info->dest_flags == dest_flags      &&
385             info->func)
386         {
387             *out_imp = cache->cache[i].imp;
388             *out_func = cache->cache[i].fast_path.func;
389
390             goto update_cache;
391         }
392     }
393
394     for (imp = get_implementation (); imp != NULL; imp = imp->delegate)
395     {
396         const pixman_fast_path_t *info = imp->fast_paths;
397
398         while (info->op != PIXMAN_OP_NONE)
399         {
400             if ((info->op == op || info->op == PIXMAN_OP_any)           &&
401                 /* Formats */
402                 ((info->src_format == src_format) ||
403                  (info->src_format == PIXMAN_any))                      &&
404                 ((info->mask_format == mask_format) ||
405                  (info->mask_format == PIXMAN_any))                     &&
406                 ((info->dest_format == dest_format) ||
407                  (info->dest_format == PIXMAN_any))                     &&
408                 /* Flags */
409                 (info->src_flags & src_flags) == info->src_flags        &&
410                 (info->mask_flags & mask_flags) == info->mask_flags     &&
411                 (info->dest_flags & dest_flags) == info->dest_flags)
412             {
413                 *out_imp = imp;
414                 *out_func = info->func;
415
416                 /* Set i to the last spot in the cache so that the
417                  * move-to-front code below will work
418                  */
419                 i = N_CACHED_FAST_PATHS - 1;
420
421                 goto update_cache;
422             }
423
424             ++info;
425         }
426     }
427     return FALSE;
428
429 update_cache:
430     if (i)
431     {
432         while (i--)
433             cache->cache[i + 1] = cache->cache[i];
434
435         cache->cache[0].imp = *out_imp;
436         cache->cache[0].fast_path.op = op;
437         cache->cache[0].fast_path.src_format = src_format;
438         cache->cache[0].fast_path.src_flags = src_flags;
439         cache->cache[0].fast_path.mask_format = mask_format;
440         cache->cache[0].fast_path.mask_flags = mask_flags;
441         cache->cache[0].fast_path.dest_format = dest_format;
442         cache->cache[0].fast_path.dest_flags = dest_flags;
443         cache->cache[0].fast_path.func = *out_func;
444     }
445
446     return TRUE;
447 }
448
449 typedef struct
450 {
451     pixman_fixed_48_16_t        x1;
452     pixman_fixed_48_16_t        y1;
453     pixman_fixed_48_16_t        x2;
454     pixman_fixed_48_16_t        y2;
455 } box_48_16_t;
456
457 static pixman_bool_t
458 compute_transformed_extents (pixman_transform_t *transform,
459                              const pixman_box32_t *extents,
460                              box_48_16_t *transformed)
461 {
462     pixman_fixed_48_16_t tx1, ty1, tx2, ty2;
463     pixman_fixed_t x1, y1, x2, y2;
464     int i;
465
466     x1 = pixman_int_to_fixed (extents->x1) + pixman_fixed_1 / 2;
467     y1 = pixman_int_to_fixed (extents->y1) + pixman_fixed_1 / 2;
468     x2 = pixman_int_to_fixed (extents->x2) - pixman_fixed_1 / 2;
469     y2 = pixman_int_to_fixed (extents->y2) - pixman_fixed_1 / 2;
470
471     if (!transform)
472     {
473         transformed->x1 = x1;
474         transformed->y1 = y1;
475         transformed->x2 = x2;
476         transformed->y2 = y2;
477
478         return TRUE;
479     }
480
481     tx1 = ty1 = INT64_MAX;
482     tx2 = ty2 = INT64_MIN;
483
484     for (i = 0; i < 4; ++i)
485     {
486         pixman_fixed_48_16_t tx, ty;
487         pixman_vector_t v;
488
489         v.vector[0] = (i & 0x01)? x1 : x2;
490         v.vector[1] = (i & 0x02)? y1 : y2;
491         v.vector[2] = pixman_fixed_1;
492
493         if (!pixman_transform_point (transform, &v))
494             return FALSE;
495
496         tx = (pixman_fixed_48_16_t)v.vector[0];
497         ty = (pixman_fixed_48_16_t)v.vector[1];
498
499         if (tx < tx1)
500             tx1 = tx;
501         if (ty < ty1)
502             ty1 = ty;
503         if (tx > tx2)
504             tx2 = tx;
505         if (ty > ty2)
506             ty2 = ty;
507     }
508
509     transformed->x1 = tx1;
510     transformed->y1 = ty1;
511     transformed->x2 = tx2;
512     transformed->y2 = ty2;
513
514     return TRUE;
515 }
516
517 static pixman_bool_t
518 compute_sample_extents (pixman_transform_t *transform,
519                         pixman_box32_t *extents,
520                         pixman_fixed_t x_off, pixman_fixed_t y_off,
521                         pixman_fixed_t width, pixman_fixed_t height)
522 {
523     box_48_16_t transformed;
524
525     if (!compute_transformed_extents (transform, extents, &transformed))
526         return FALSE;
527
528     /* Expand the source area by a tiny bit so account of different rounding that
529      * may happen during sampling. Note that (8 * pixman_fixed_e) is very far from
530      * 0.5 so this won't cause the area computed to be overly pessimistic.
531      */
532     transformed.x1 += x_off - 8 * pixman_fixed_e;
533     transformed.y1 += y_off - 8 * pixman_fixed_e;
534     transformed.x2 += x_off + width + 8 * pixman_fixed_e;
535     transformed.y2 += y_off + height + 8 * pixman_fixed_e;
536
537     if (transformed.x1 < pixman_min_fixed_48_16 || transformed.x1 > pixman_max_fixed_48_16 ||
538         transformed.y1 < pixman_min_fixed_48_16 || transformed.y1 > pixman_max_fixed_48_16 ||
539         transformed.x2 < pixman_min_fixed_48_16 || transformed.x2 > pixman_max_fixed_48_16 ||
540         transformed.y2 < pixman_min_fixed_48_16 || transformed.y2 > pixman_max_fixed_48_16)
541     {
542         return FALSE;
543     }
544     else
545     {
546         extents->x1 = pixman_fixed_to_int (transformed.x1);
547         extents->y1 = pixman_fixed_to_int (transformed.y1);
548         extents->x2 = pixman_fixed_to_int (transformed.x2) + 1;
549         extents->y2 = pixman_fixed_to_int (transformed.y2) + 1;
550
551         return TRUE;
552     }
553 }
554
555 #define IS_16BIT(x) (((x) >= INT16_MIN) && ((x) <= INT16_MAX))
556
557 static pixman_bool_t
558 analyze_extent (pixman_image_t       *image,
559                 const pixman_box32_t *extents,
560                 uint32_t             *flags)
561 {
562     pixman_transform_t *transform;
563     pixman_fixed_t *params;
564     pixman_fixed_t x_off, y_off;
565     pixman_fixed_t width, height;
566     pixman_box32_t ex;
567
568     if (!image)
569         return TRUE;
570
571     /* Some compositing functions walk one step
572      * outside the destination rectangle, so we
573      * check here that the expanded-by-one source
574      * extents in destination space fits in 16 bits
575      */
576     if (!IS_16BIT (extents->x1 - 1)             ||
577         !IS_16BIT (extents->y1 - 1)             ||
578         !IS_16BIT (extents->x2 + 1)             ||
579         !IS_16BIT (extents->y2 + 1))
580     {
581         return FALSE;
582     }
583
584     transform = image->common.transform;
585     if (image->common.type == BITS)
586     {
587         /* During repeat mode calculations we might convert the
588          * width/height of an image to fixed 16.16, so we need
589          * them to be smaller than 16 bits.
590          */
591         if (image->bits.width >= 0x7fff || image->bits.height >= 0x7fff)
592             return FALSE;
593
594 #define ID_AND_NEAREST (FAST_PATH_ID_TRANSFORM | FAST_PATH_NEAREST_FILTER)
595
596         if ((image->common.flags & ID_AND_NEAREST) == ID_AND_NEAREST &&
597             extents->x1 >= 0 &&
598             extents->y1 >= 0 &&
599             extents->x2 <= image->bits.width &&
600             extents->y2 <= image->bits.height)
601         {
602             *flags |= FAST_PATH_SAMPLES_COVER_CLIP;
603             return TRUE;
604         }
605
606         switch (image->common.filter)
607         {
608         case PIXMAN_FILTER_CONVOLUTION:
609             params = image->common.filter_params;
610             x_off = - pixman_fixed_e - ((params[0] - pixman_fixed_1) >> 1);
611             y_off = - pixman_fixed_e - ((params[1] - pixman_fixed_1) >> 1);
612             width = params[0];
613             height = params[1];
614             break;
615
616         case PIXMAN_FILTER_GOOD:
617         case PIXMAN_FILTER_BEST:
618         case PIXMAN_FILTER_BILINEAR:
619             x_off = - pixman_fixed_1 / 2;
620             y_off = - pixman_fixed_1 / 2;
621             width = pixman_fixed_1;
622             height = pixman_fixed_1;
623             break;
624
625         case PIXMAN_FILTER_FAST:
626         case PIXMAN_FILTER_NEAREST:
627             x_off = - pixman_fixed_e;
628             y_off = - pixman_fixed_e;
629             width = 0;
630             height = 0;
631             break;
632
633         default:
634             return FALSE;
635         }
636
637         /* Check whether the non-expanded, transformed extent is entirely within
638          * the source image, and set the FAST_PATH_SAMPLES_COVER_CLIP if it is.
639          */
640         ex = *extents;
641         if (compute_sample_extents (transform, &ex, x_off, y_off, width, height) &&
642             ex.x1 >= 0 && ex.y1 >= 0 &&
643             ex.x2 <= image->bits.width && ex.y2 <= image->bits.height)
644         {
645             *flags |= FAST_PATH_SAMPLES_COVER_CLIP;
646         }
647     }
648     else
649     {
650         x_off = 0;
651         y_off = 0;
652         width = 0;
653         height = 0;
654     }
655
656     /* Check that the extents expanded by one don't overflow. This ensures that
657      * compositing functions can simply walk the source space using 16.16
658      * variables without worrying about overflow.
659      */
660     ex.x1 = extents->x1 - 1;
661     ex.y1 = extents->y1 - 1;
662     ex.x2 = extents->x2 + 1;
663     ex.y2 = extents->y2 + 1;
664
665     if (!compute_sample_extents (transform, &ex, x_off, y_off, width, height))
666         return FALSE;
667
668     return TRUE;
669 }
670
671 /*
672  * Work around GCC bug causing crashes in Mozilla with SSE2
673  *
674  * When using -msse, gcc generates movdqa instructions assuming that
675  * the stack is 16 byte aligned. Unfortunately some applications, such
676  * as Mozilla and Mono, end up aligning the stack to 4 bytes, which
677  * causes the movdqa instructions to fail.
678  *
679  * The __force_align_arg_pointer__ makes gcc generate a prologue that
680  * realigns the stack pointer to 16 bytes.
681  *
682  * On x86-64 this is not necessary because the standard ABI already
683  * calls for a 16 byte aligned stack.
684  *
685  * See https://bugs.freedesktop.org/show_bug.cgi?id=15693
686  */
687 #if defined (USE_SSE2) && defined(__GNUC__) && !defined(__x86_64__) && !defined(__amd64__)
688 __attribute__((__force_align_arg_pointer__))
689 #endif
690 PIXMAN_EXPORT void
691 pixman_image_composite32 (pixman_op_t      op,
692                           pixman_image_t * src,
693                           pixman_image_t * mask,
694                           pixman_image_t * dest,
695                           int32_t          src_x,
696                           int32_t          src_y,
697                           int32_t          mask_x,
698                           int32_t          mask_y,
699                           int32_t          dest_x,
700                           int32_t          dest_y,
701                           int32_t          width,
702                           int32_t          height)
703 {
704     pixman_format_code_t src_format, mask_format, dest_format;
705     uint32_t src_flags, mask_flags, dest_flags;
706     pixman_region32_t region;
707     pixman_box32_t extents;
708     pixman_implementation_t *imp;
709     pixman_composite_func_t func;
710
711     _pixman_image_validate (src);
712     if (mask)
713         _pixman_image_validate (mask);
714     _pixman_image_validate (dest);
715
716     src_format = src->common.extended_format_code;
717     src_flags = src->common.flags;
718
719     if (mask)
720     {
721         mask_format = mask->common.extended_format_code;
722         mask_flags = mask->common.flags;
723     }
724     else
725     {
726         mask_format = PIXMAN_null;
727         mask_flags = FAST_PATH_IS_OPAQUE;
728     }
729
730     dest_format = dest->common.extended_format_code;
731     dest_flags = dest->common.flags;
732
733     /* Check for pixbufs */
734     if ((mask_format == PIXMAN_a8r8g8b8 || mask_format == PIXMAN_a8b8g8r8) &&
735         (src->type == BITS && src->bits.bits == mask->bits.bits)           &&
736         (src->common.repeat == mask->common.repeat)                        &&
737         (src_x == mask_x && src_y == mask_y))
738     {
739         if (src_format == PIXMAN_x8b8g8r8)
740             src_format = mask_format = PIXMAN_pixbuf;
741         else if (src_format == PIXMAN_x8r8g8b8)
742             src_format = mask_format = PIXMAN_rpixbuf;
743     }
744
745     pixman_region32_init (&region);
746
747     if (!pixman_compute_composite_region32 (
748             &region, src, mask, dest,
749             src_x, src_y, mask_x, mask_y, dest_x, dest_y, width, height))
750     {
751         goto out;
752     }
753
754     extents = *pixman_region32_extents (&region);
755
756     extents.x1 -= dest_x - src_x;
757     extents.y1 -= dest_y - src_y;
758     extents.x2 -= dest_x - src_x;
759     extents.y2 -= dest_y - src_y;
760
761     if (!analyze_extent (src, &extents, &src_flags))
762         goto out;
763
764     extents.x1 -= src_x - mask_x;
765     extents.y1 -= src_y - mask_y;
766     extents.x2 -= src_x - mask_x;
767     extents.y2 -= src_y - mask_y;
768
769     if (!analyze_extent (mask, &extents, &mask_flags))
770         goto out;
771
772     /* If the clip is within the source samples, and the samples are opaque,
773      * then the source is effectively opaque.
774      */
775 #define BOTH (FAST_PATH_SAMPLES_OPAQUE | FAST_PATH_SAMPLES_COVER_CLIP)
776
777     if ((src_flags & BOTH) == BOTH)
778         src_flags |= FAST_PATH_IS_OPAQUE;
779
780     if ((mask_flags & BOTH) == BOTH)
781         mask_flags |= FAST_PATH_IS_OPAQUE;
782
783     /*
784      * Check if we can replace our operator by a simpler one
785      * if the src or dest are opaque. The output operator should be
786      * mathematically equivalent to the source.
787      */
788     op = optimize_operator (op, src_flags, mask_flags, dest_flags);
789
790     if (lookup_composite_function (op,
791                                    src_format, src_flags,
792                                    mask_format, mask_flags,
793                                    dest_format, dest_flags,
794                                    &imp, &func))
795     {
796         pixman_composite_info_t info;
797         const pixman_box32_t *pbox;
798         int n;
799
800         info.op = op;
801         info.src_image = src;
802         info.mask_image = mask;
803         info.dest_image = dest;
804
805         pbox = pixman_region32_rectangles (&region, &n);
806
807         while (n--)
808         {
809             info.src_x = pbox->x1 + src_x - dest_x;
810             info.src_y = pbox->y1 + src_y - dest_y;
811             info.mask_x = pbox->x1 + mask_x - dest_x;
812             info.mask_y = pbox->y1 + mask_y - dest_y;
813             info.dest_x = pbox->x1;
814             info.dest_y = pbox->y1;
815             info.width = pbox->x2 - pbox->x1;
816             info.height = pbox->y2 - pbox->y1;
817
818             func (imp, &info);
819
820             pbox++;
821         }
822     }
823
824 out:
825     pixman_region32_fini (&region);
826 }
827
828 PIXMAN_EXPORT void
829 pixman_image_composite (pixman_op_t      op,
830                         pixman_image_t * src,
831                         pixman_image_t * mask,
832                         pixman_image_t * dest,
833                         int16_t          src_x,
834                         int16_t          src_y,
835                         int16_t          mask_x,
836                         int16_t          mask_y,
837                         int16_t          dest_x,
838                         int16_t          dest_y,
839                         uint16_t         width,
840                         uint16_t         height)
841 {
842     pixman_image_composite32 (op, src, mask, dest, src_x, src_y, 
843                               mask_x, mask_y, dest_x, dest_y, width, height);
844 }
845
846 PIXMAN_EXPORT pixman_bool_t
847 pixman_blt (uint32_t *src_bits,
848             uint32_t *dst_bits,
849             int       src_stride,
850             int       dst_stride,
851             int       src_bpp,
852             int       dst_bpp,
853             int       src_x,
854             int       src_y,
855             int       dest_x,
856             int       dest_y,
857             int       width,
858             int       height)
859 {
860     return _pixman_implementation_blt (get_implementation(),
861                                        src_bits, dst_bits, src_stride, dst_stride,
862                                        src_bpp, dst_bpp,
863                                        src_x, src_y,
864                                        dest_x, dest_y,
865                                        width, height);
866 }
867
868 PIXMAN_EXPORT pixman_bool_t
869 pixman_fill (uint32_t *bits,
870              int       stride,
871              int       bpp,
872              int       x,
873              int       y,
874              int       width,
875              int       height,
876              uint32_t xor)
877 {
878     return _pixman_implementation_fill (
879         get_implementation(), bits, stride, bpp, x, y, width, height, xor);
880 }
881
882 static uint32_t
883 color_to_uint32 (const pixman_color_t *color)
884 {
885     return
886         (color->alpha >> 8 << 24) |
887         (color->red >> 8 << 16) |
888         (color->green & 0xff00) |
889         (color->blue >> 8);
890 }
891
892 static pixman_bool_t
893 color_to_pixel (pixman_color_t *     color,
894                 uint32_t *           pixel,
895                 pixman_format_code_t format)
896 {
897     uint32_t c = color_to_uint32 (color);
898
899     if (!(format == PIXMAN_a8r8g8b8     ||
900           format == PIXMAN_x8r8g8b8     ||
901           format == PIXMAN_a8b8g8r8     ||
902           format == PIXMAN_x8b8g8r8     ||
903           format == PIXMAN_b8g8r8a8     ||
904           format == PIXMAN_b8g8r8x8     ||
905           format == PIXMAN_r8g8b8a8     ||
906           format == PIXMAN_r8g8b8x8     ||
907           format == PIXMAN_r5g6b5       ||
908           format == PIXMAN_b5g6r5       ||
909           format == PIXMAN_a8           ||
910           format == PIXMAN_a1))
911     {
912         return FALSE;
913     }
914
915     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
916     {
917         c = ((c & 0xff000000) >>  0) |
918             ((c & 0x00ff0000) >> 16) |
919             ((c & 0x0000ff00) >>  0) |
920             ((c & 0x000000ff) << 16);
921     }
922     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
923     {
924         c = ((c & 0xff000000) >> 24) |
925             ((c & 0x00ff0000) >>  8) |
926             ((c & 0x0000ff00) <<  8) |
927             ((c & 0x000000ff) << 24);
928     }
929     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_RGBA)
930         c = ((c & 0xff000000) >> 24) | (c << 8);
931
932     if (format == PIXMAN_a1)
933         c = c >> 31;
934     else if (format == PIXMAN_a8)
935         c = c >> 24;
936     else if (format == PIXMAN_r5g6b5 ||
937              format == PIXMAN_b5g6r5)
938         c = CONVERT_8888_TO_0565 (c);
939
940 #if 0
941     printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
942     printf ("pixel: %x\n", c);
943 #endif
944
945     *pixel = c;
946     return TRUE;
947 }
948
949 PIXMAN_EXPORT pixman_bool_t
950 pixman_image_fill_rectangles (pixman_op_t                 op,
951                               pixman_image_t *            dest,
952                               pixman_color_t *            color,
953                               int                         n_rects,
954                               const pixman_rectangle16_t *rects)
955 {
956     pixman_box32_t stack_boxes[6];
957     pixman_box32_t *boxes;
958     pixman_bool_t result;
959     int i;
960
961     if (n_rects > 6)
962     {
963         boxes = pixman_malloc_ab (sizeof (pixman_box32_t), n_rects);
964         if (boxes == NULL)
965             return FALSE;
966     }
967     else
968     {
969         boxes = stack_boxes;
970     }
971
972     for (i = 0; i < n_rects; ++i)
973     {
974         boxes[i].x1 = rects[i].x;
975         boxes[i].y1 = rects[i].y;
976         boxes[i].x2 = boxes[i].x1 + rects[i].width;
977         boxes[i].y2 = boxes[i].y1 + rects[i].height;
978     }
979
980     result = pixman_image_fill_boxes (op, dest, color, n_rects, boxes);
981
982     if (boxes != stack_boxes)
983         free (boxes);
984     
985     return result;
986 }
987
988 PIXMAN_EXPORT pixman_bool_t
989 pixman_image_fill_boxes (pixman_op_t           op,
990                          pixman_image_t *      dest,
991                          pixman_color_t *      color,
992                          int                   n_boxes,
993                          const pixman_box32_t *boxes)
994 {
995     pixman_image_t *solid;
996     pixman_color_t c;
997     int i;
998
999     _pixman_image_validate (dest);
1000     
1001     if (color->alpha == 0xffff)
1002     {
1003         if (op == PIXMAN_OP_OVER)
1004             op = PIXMAN_OP_SRC;
1005     }
1006
1007     if (op == PIXMAN_OP_CLEAR)
1008     {
1009         c.red = 0;
1010         c.green = 0;
1011         c.blue = 0;
1012         c.alpha = 0;
1013
1014         color = &c;
1015
1016         op = PIXMAN_OP_SRC;
1017     }
1018
1019     if (op == PIXMAN_OP_SRC)
1020     {
1021         uint32_t pixel;
1022
1023         if (color_to_pixel (color, &pixel, dest->bits.format))
1024         {
1025             pixman_region32_t fill_region;
1026             int n_rects, j;
1027             pixman_box32_t *rects;
1028
1029             if (!pixman_region32_init_rects (&fill_region, boxes, n_boxes))
1030                 return FALSE;
1031
1032             if (dest->common.have_clip_region)
1033             {
1034                 if (!pixman_region32_intersect (&fill_region,
1035                                                 &fill_region,
1036                                                 &dest->common.clip_region))
1037                     return FALSE;
1038             }
1039
1040             rects = pixman_region32_rectangles (&fill_region, &n_rects);
1041             for (j = 0; j < n_rects; ++j)
1042             {
1043                 const pixman_box32_t *rect = &(rects[j]);
1044                 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
1045                              rect->x1, rect->y1, rect->x2 - rect->x1, rect->y2 - rect->y1,
1046                              pixel);
1047             }
1048
1049             pixman_region32_fini (&fill_region);
1050             return TRUE;
1051         }
1052     }
1053
1054     solid = pixman_image_create_solid_fill (color);
1055     if (!solid)
1056         return FALSE;
1057
1058     for (i = 0; i < n_boxes; ++i)
1059     {
1060         const pixman_box32_t *box = &(boxes[i]);
1061
1062         pixman_image_composite32 (op, solid, NULL, dest,
1063                                   0, 0, 0, 0,
1064                                   box->x1, box->y1,
1065                                   box->x2 - box->x1, box->y2 - box->y1);
1066     }
1067
1068     pixman_image_unref (solid);
1069
1070     return TRUE;
1071 }
1072
1073 /**
1074  * pixman_version:
1075  *
1076  * Returns the version of the pixman library encoded in a single
1077  * integer as per %PIXMAN_VERSION_ENCODE. The encoding ensures that
1078  * later versions compare greater than earlier versions.
1079  *
1080  * A run-time comparison to check that pixman's version is greater than
1081  * or equal to version X.Y.Z could be performed as follows:
1082  *
1083  * <informalexample><programlisting>
1084  * if (pixman_version() >= PIXMAN_VERSION_ENCODE(X,Y,Z)) {...}
1085  * </programlisting></informalexample>
1086  *
1087  * See also pixman_version_string() as well as the compile-time
1088  * equivalents %PIXMAN_VERSION and %PIXMAN_VERSION_STRING.
1089  *
1090  * Return value: the encoded version.
1091  **/
1092 PIXMAN_EXPORT int
1093 pixman_version (void)
1094 {
1095     return PIXMAN_VERSION;
1096 }
1097
1098 /**
1099  * pixman_version_string:
1100  *
1101  * Returns the version of the pixman library as a human-readable string
1102  * of the form "X.Y.Z".
1103  *
1104  * See also pixman_version() as well as the compile-time equivalents
1105  * %PIXMAN_VERSION_STRING and %PIXMAN_VERSION.
1106  *
1107  * Return value: a string containing the version.
1108  **/
1109 PIXMAN_EXPORT const char*
1110 pixman_version_string (void)
1111 {
1112     return PIXMAN_VERSION_STRING;
1113 }
1114
1115 /**
1116  * pixman_format_supported_source:
1117  * @format: A pixman_format_code_t format
1118  *
1119  * Return value: whether the provided format code is a supported
1120  * format for a pixman surface used as a source in
1121  * rendering.
1122  *
1123  * Currently, all pixman_format_code_t values are supported.
1124  **/
1125 PIXMAN_EXPORT pixman_bool_t
1126 pixman_format_supported_source (pixman_format_code_t format)
1127 {
1128     switch (format)
1129     {
1130     /* 32 bpp formats */
1131     case PIXMAN_a2b10g10r10:
1132     case PIXMAN_x2b10g10r10:
1133     case PIXMAN_a2r10g10b10:
1134     case PIXMAN_x2r10g10b10:
1135     case PIXMAN_a8r8g8b8:
1136     case PIXMAN_x8r8g8b8:
1137     case PIXMAN_a8b8g8r8:
1138     case PIXMAN_x8b8g8r8:
1139     case PIXMAN_b8g8r8a8:
1140     case PIXMAN_b8g8r8x8:
1141     case PIXMAN_r8g8b8a8:
1142     case PIXMAN_r8g8b8x8:
1143     case PIXMAN_r8g8b8:
1144     case PIXMAN_b8g8r8:
1145     case PIXMAN_r5g6b5:
1146     case PIXMAN_b5g6r5:
1147     case PIXMAN_x14r6g6b6:
1148     /* 16 bpp formats */
1149     case PIXMAN_a1r5g5b5:
1150     case PIXMAN_x1r5g5b5:
1151     case PIXMAN_a1b5g5r5:
1152     case PIXMAN_x1b5g5r5:
1153     case PIXMAN_a4r4g4b4:
1154     case PIXMAN_x4r4g4b4:
1155     case PIXMAN_a4b4g4r4:
1156     case PIXMAN_x4b4g4r4:
1157     /* 8bpp formats */
1158     case PIXMAN_a8:
1159     case PIXMAN_r3g3b2:
1160     case PIXMAN_b2g3r3:
1161     case PIXMAN_a2r2g2b2:
1162     case PIXMAN_a2b2g2r2:
1163     case PIXMAN_c8:
1164     case PIXMAN_g8:
1165     case PIXMAN_x4a4:
1166     /* Collides with PIXMAN_c8
1167        case PIXMAN_x4c4:
1168      */
1169     /* Collides with PIXMAN_g8
1170        case PIXMAN_x4g4:
1171      */
1172     /* 4bpp formats */
1173     case PIXMAN_a4:
1174     case PIXMAN_r1g2b1:
1175     case PIXMAN_b1g2r1:
1176     case PIXMAN_a1r1g1b1:
1177     case PIXMAN_a1b1g1r1:
1178     case PIXMAN_c4:
1179     case PIXMAN_g4:
1180     /* 1bpp formats */
1181     case PIXMAN_a1:
1182     case PIXMAN_g1:
1183     /* YUV formats */
1184     case PIXMAN_yuy2:
1185     case PIXMAN_yv12:
1186         return TRUE;
1187
1188     default:
1189         return FALSE;
1190     }
1191 }
1192
1193 /**
1194  * pixman_format_supported_destination:
1195  * @format: A pixman_format_code_t format
1196  *
1197  * Return value: whether the provided format code is a supported
1198  * format for a pixman surface used as a destination in
1199  * rendering.
1200  *
1201  * Currently, all pixman_format_code_t values are supported
1202  * except for the YUV formats.
1203  **/
1204 PIXMAN_EXPORT pixman_bool_t
1205 pixman_format_supported_destination (pixman_format_code_t format)
1206 {
1207     /* YUV formats cannot be written to at the moment */
1208     if (format == PIXMAN_yuy2 || format == PIXMAN_yv12)
1209         return FALSE;
1210
1211     return pixman_format_supported_source (format);
1212 }
1213
1214 PIXMAN_EXPORT pixman_bool_t
1215 pixman_compute_composite_region (pixman_region16_t * region,
1216                                  pixman_image_t *    src_image,
1217                                  pixman_image_t *    mask_image,
1218                                  pixman_image_t *    dest_image,
1219                                  int16_t             src_x,
1220                                  int16_t             src_y,
1221                                  int16_t             mask_x,
1222                                  int16_t             mask_y,
1223                                  int16_t             dest_x,
1224                                  int16_t             dest_y,
1225                                  uint16_t            width,
1226                                  uint16_t            height)
1227 {
1228     pixman_region32_t r32;
1229     pixman_bool_t retval;
1230
1231     pixman_region32_init (&r32);
1232
1233     retval = pixman_compute_composite_region32 (
1234         &r32, src_image, mask_image, dest_image,
1235         src_x, src_y, mask_x, mask_y, dest_x, dest_y,
1236         width, height);
1237
1238     if (retval)
1239     {
1240         if (!pixman_region16_copy_from_region32 (region, &r32))
1241             retval = FALSE;
1242     }
1243
1244     pixman_region32_fini (&r32);
1245     return retval;
1246 }