Move compositing to its own function, do_composite()
[profile/ivi/pixman.git] / pixman / pixman.c
1 /* -*- Mode: c; c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t; -*- */
2 /*
3  * Copyright © 2000 SuSE, Inc.
4  * Copyright © 2007 Red Hat, Inc.
5  *
6  * Permission to use, copy, modify, distribute, and sell this software and its
7  * documentation for any purpose is hereby granted without fee, provided that
8  * the above copyright notice appear in all copies and that both that
9  * copyright notice and this permission notice appear in supporting
10  * documentation, and that the name of SuSE not be used in advertising or
11  * publicity pertaining to distribution of the software without specific,
12  * written prior permission.  SuSE makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as is"
14  * without express or implied warranty.
15  *
16  * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
18  * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
21  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Author:  Keith Packard, SuSE, Inc.
24  */
25
26 #ifdef HAVE_CONFIG_H
27 #include <config.h>
28 #endif
29 #include "pixman-private.h"
30
31 #include <stdlib.h>
32
33 /*
34  * Operator optimizations based on source or destination opacity
35  */
36 typedef struct
37 {
38     pixman_op_t op;
39     pixman_op_t op_src_dst_opaque;
40     pixman_op_t op_src_opaque;
41     pixman_op_t op_dst_opaque;
42 } optimized_operator_info_t;
43
44 static const optimized_operator_info_t optimized_operators[] =
45 {
46     /* Input Operator           SRC&DST Opaque          SRC Opaque              DST Opaque      */
47     { PIXMAN_OP_OVER,           PIXMAN_OP_SRC,          PIXMAN_OP_SRC,          PIXMAN_OP_OVER },
48     { PIXMAN_OP_OVER_REVERSE,   PIXMAN_OP_DST,          PIXMAN_OP_OVER_REVERSE, PIXMAN_OP_DST },
49     { PIXMAN_OP_IN,             PIXMAN_OP_SRC,          PIXMAN_OP_IN,           PIXMAN_OP_SRC },
50     { PIXMAN_OP_IN_REVERSE,     PIXMAN_OP_DST,          PIXMAN_OP_DST,          PIXMAN_OP_IN_REVERSE },
51     { PIXMAN_OP_OUT,            PIXMAN_OP_CLEAR,        PIXMAN_OP_OUT,          PIXMAN_OP_CLEAR },
52     { PIXMAN_OP_OUT_REVERSE,    PIXMAN_OP_CLEAR,        PIXMAN_OP_CLEAR,        PIXMAN_OP_OUT_REVERSE },
53     { PIXMAN_OP_ATOP,           PIXMAN_OP_SRC,          PIXMAN_OP_IN,           PIXMAN_OP_OVER },
54     { PIXMAN_OP_ATOP_REVERSE,   PIXMAN_OP_DST,          PIXMAN_OP_OVER_REVERSE, PIXMAN_OP_IN_REVERSE },
55     { PIXMAN_OP_XOR,            PIXMAN_OP_CLEAR,        PIXMAN_OP_OUT,          PIXMAN_OP_OUT_REVERSE },
56     { PIXMAN_OP_SATURATE,       PIXMAN_OP_DST,          PIXMAN_OP_OVER_REVERSE, PIXMAN_OP_DST },
57     { PIXMAN_OP_NONE }
58 };
59
60 static pixman_implementation_t *imp;
61
62 /*
63  * Check if the current operator could be optimized
64  */
65 static const optimized_operator_info_t*
66 pixman_operator_can_be_optimized (pixman_op_t op)
67 {
68     const optimized_operator_info_t *info;
69
70     for (info = optimized_operators; info->op != PIXMAN_OP_NONE; info++)
71     {
72         if (info->op == op)
73             return info;
74     }
75     return NULL;
76 }
77
78 /*
79  * Optimize the current operator based on opacity of source or destination
80  * The output operator should be mathematically equivalent to the source.
81  */
82 static pixman_op_t
83 pixman_optimize_operator (pixman_op_t     op,
84                           pixman_image_t *src_image,
85                           pixman_image_t *mask_image,
86                           pixman_image_t *dst_image)
87 {
88     pixman_bool_t is_source_opaque;
89     pixman_bool_t is_dest_opaque;
90     const optimized_operator_info_t *info = pixman_operator_can_be_optimized (op);
91
92     if (!info || mask_image)
93         return op;
94
95     is_source_opaque = _pixman_image_is_opaque (src_image);
96     is_dest_opaque = _pixman_image_is_opaque (dst_image);
97
98     if (is_source_opaque == FALSE && is_dest_opaque == FALSE)
99         return op;
100
101     if (is_source_opaque && is_dest_opaque)
102         return info->op_src_dst_opaque;
103     else if (is_source_opaque)
104         return info->op_src_opaque;
105     else if (is_dest_opaque)
106         return info->op_dst_opaque;
107
108     return op;
109
110 }
111
112 static void
113 apply_workaround (pixman_image_t *image,
114                   int32_t *       x,
115                   int32_t *       y,
116                   uint32_t **     save_bits,
117                   int *           save_dx,
118                   int *           save_dy)
119 {
120     if (image && image->common.need_workaround)
121     {
122         /* Some X servers generate images that point to the
123          * wrong place in memory, but then set the clip region
124          * to point to the right place. Because of an old bug
125          * in pixman, this would actually work.
126          *
127          * Here we try and undo the damage
128          */
129         int bpp = PIXMAN_FORMAT_BPP (image->bits.format) / 8;
130         pixman_box32_t *extents;
131         uint8_t *t;
132         int dx, dy;
133         
134         extents = pixman_region32_extents (&(image->common.clip_region));
135         dx = extents->x1;
136         dy = extents->y1;
137         
138         *save_bits = image->bits.bits;
139         
140         *x -= dx;
141         *y -= dy;
142         pixman_region32_translate (&(image->common.clip_region), -dx, -dy);
143         
144         t = (uint8_t *)image->bits.bits;
145         t += dy * image->bits.rowstride * 4 + dx * bpp;
146         image->bits.bits = (uint32_t *)t;
147         
148         *save_dx = dx;
149         *save_dy = dy;
150     }
151 }
152
153 static void
154 unapply_workaround (pixman_image_t *image, uint32_t *bits, int dx, int dy)
155 {
156     if (image && image->common.need_workaround)
157     {
158         image->bits.bits = bits;
159         pixman_region32_translate (&image->common.clip_region, dx, dy);
160     }
161 }
162
163 static void
164 do_composite (pixman_implementation_t *imp,
165               pixman_op_t              op,
166               pixman_image_t          *src,
167               pixman_image_t          *mask,
168               pixman_image_t          *dest,
169               int                      src_x,
170               int                      src_y,
171               int                      mask_x,
172               int                      mask_y,
173               int                      dest_x,
174               int                      dest_y,
175               int                      width,
176               int                      height)
177 {
178     while (imp)
179     {
180         if (_pixman_run_fast_path (imp->fast_paths, imp,
181                                    op, src, mask, dest,
182                                    src_x, src_y,
183                                    mask_x, mask_y,
184                                    dest_x, dest_y,
185                                    width, height))
186         {
187             return;
188         }
189
190         imp = imp->delegate;
191     }
192 }
193
194 /*
195  * Work around GCC bug causing crashes in Mozilla with SSE2
196  *
197  * When using -msse, gcc generates movdqa instructions assuming that
198  * the stack is 16 byte aligned. Unfortunately some applications, such
199  * as Mozilla and Mono, end up aligning the stack to 4 bytes, which
200  * causes the movdqa instructions to fail.
201  *
202  * The __force_align_arg_pointer__ makes gcc generate a prologue that
203  * realigns the stack pointer to 16 bytes.
204  *
205  * On x86-64 this is not necessary because the standard ABI already
206  * calls for a 16 byte aligned stack.
207  *
208  * See https://bugs.freedesktop.org/show_bug.cgi?id=15693
209  */
210 #if defined (USE_SSE2) && defined(__GNUC__) && !defined(__x86_64__) && !defined(__amd64__)
211 __attribute__((__force_align_arg_pointer__))
212 #endif
213 PIXMAN_EXPORT void
214 pixman_image_composite (pixman_op_t      op,
215                         pixman_image_t * src,
216                         pixman_image_t * mask,
217                         pixman_image_t * dest,
218                         int16_t          src_x,
219                         int16_t          src_y,
220                         int16_t          mask_x,
221                         int16_t          mask_y,
222                         int16_t          dest_x,
223                         int16_t          dest_y,
224                         uint16_t         width,
225                         uint16_t         height)
226 {
227     pixman_image_composite32 (op, src, mask, dest, src_x, src_y, 
228                               mask_x, mask_y, dest_x, dest_y, width, height);
229 }
230
231 PIXMAN_EXPORT void
232 pixman_image_composite32 (pixman_op_t      op,
233                           pixman_image_t * src,
234                           pixman_image_t * mask,
235                           pixman_image_t * dest,
236                           int32_t          src_x,
237                           int32_t          src_y,
238                           int32_t          mask_x,
239                           int32_t          mask_y,
240                           int32_t          dest_x,
241                           int32_t          dest_y,
242                           int32_t          width,
243                           int32_t          height)
244 {
245     uint32_t *src_bits;
246     int src_dx, src_dy;
247     uint32_t *mask_bits;
248     int mask_dx, mask_dy;
249     uint32_t *dest_bits;
250     int dest_dx, dest_dy;
251     pixman_bool_t need_workaround;
252
253     _pixman_image_validate (src);
254     if (mask)
255         _pixman_image_validate (mask);
256     _pixman_image_validate (dest);
257     
258     /*
259      * Check if we can replace our operator by a simpler one
260      * if the src or dest are opaque. The output operator should be
261      * mathematically equivalent to the source.
262      */
263     op = pixman_optimize_operator(op, src, mask, dest);
264     if (op == PIXMAN_OP_DST             ||
265         op == PIXMAN_OP_CONJOINT_DST    ||
266         op == PIXMAN_OP_DISJOINT_DST)
267     {
268         return;
269     }
270
271     if (!imp)
272         imp = _pixman_choose_implementation ();
273
274     need_workaround =
275         (src->common.need_workaround)                   ||
276         (mask && mask->common.need_workaround)          ||
277         (dest->common.need_workaround);
278    
279     if (need_workaround)
280     {
281         apply_workaround (src, &src_x, &src_y, &src_bits, &src_dx, &src_dy);
282         apply_workaround (mask, &mask_x, &mask_y, &mask_bits, &mask_dx, &mask_dy);
283         apply_workaround (dest, &dest_x, &dest_y, &dest_bits, &dest_dx, &dest_dy);
284     }
285
286     do_composite (imp, op,
287                   src, mask, dest,
288                   src_x, src_y,
289                   mask_x, mask_y,
290                   dest_x, dest_y,
291                   width, height);
292     
293     if (need_workaround)
294     {
295         if (src->common.need_workaround)
296             unapply_workaround (src, src_bits, src_dx, src_dy);
297         if (mask && mask->common.need_workaround)
298             unapply_workaround (mask, mask_bits, mask_dx, mask_dy);
299         if (dest->common.need_workaround)
300             unapply_workaround (dest, dest_bits, dest_dx, dest_dy);
301     }
302 }
303
304 PIXMAN_EXPORT pixman_bool_t
305 pixman_blt (uint32_t *src_bits,
306             uint32_t *dst_bits,
307             int       src_stride,
308             int       dst_stride,
309             int       src_bpp,
310             int       dst_bpp,
311             int       src_x,
312             int       src_y,
313             int       dst_x,
314             int       dst_y,
315             int       width,
316             int       height)
317 {
318     if (!imp)
319         imp = _pixman_choose_implementation ();
320
321     return _pixman_implementation_blt (imp, src_bits, dst_bits, src_stride, dst_stride,
322                                        src_bpp, dst_bpp,
323                                        src_x, src_y,
324                                        dst_x, dst_y,
325                                        width, height);
326 }
327
328 PIXMAN_EXPORT pixman_bool_t
329 pixman_fill (uint32_t *bits,
330              int       stride,
331              int       bpp,
332              int       x,
333              int       y,
334              int       width,
335              int       height,
336              uint32_t xor)
337 {
338     if (!imp)
339         imp = _pixman_choose_implementation ();
340
341     return _pixman_implementation_fill (imp, bits, stride, bpp, x, y, width, height, xor);
342 }
343
344 static uint32_t
345 color_to_uint32 (const pixman_color_t *color)
346 {
347     return
348         (color->alpha >> 8 << 24) |
349         (color->red >> 8 << 16) |
350         (color->green & 0xff00) |
351         (color->blue >> 8);
352 }
353
354 static pixman_bool_t
355 color_to_pixel (pixman_color_t *     color,
356                 uint32_t *           pixel,
357                 pixman_format_code_t format)
358 {
359     uint32_t c = color_to_uint32 (color);
360
361     if (!(format == PIXMAN_a8r8g8b8     ||
362           format == PIXMAN_x8r8g8b8     ||
363           format == PIXMAN_a8b8g8r8     ||
364           format == PIXMAN_x8b8g8r8     ||
365           format == PIXMAN_b8g8r8a8     ||
366           format == PIXMAN_b8g8r8x8     ||
367           format == PIXMAN_r5g6b5       ||
368           format == PIXMAN_b5g6r5       ||
369           format == PIXMAN_a8))
370     {
371         return FALSE;
372     }
373
374     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
375     {
376         c = ((c & 0xff000000) >>  0) |
377             ((c & 0x00ff0000) >> 16) |
378             ((c & 0x0000ff00) >>  0) |
379             ((c & 0x000000ff) << 16);
380     }
381     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
382     {
383         c = ((c & 0xff000000) >> 24) |
384             ((c & 0x00ff0000) >>  8) |
385             ((c & 0x0000ff00) <<  8) |
386             ((c & 0x000000ff) << 24);
387     }
388
389     if (format == PIXMAN_a8)
390         c = c >> 24;
391     else if (format == PIXMAN_r5g6b5 ||
392              format == PIXMAN_b5g6r5)
393         c = CONVERT_8888_TO_0565 (c);
394
395 #if 0
396     printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
397     printf ("pixel: %x\n", c);
398 #endif
399
400     *pixel = c;
401     return TRUE;
402 }
403
404 PIXMAN_EXPORT pixman_bool_t
405 pixman_image_fill_rectangles (pixman_op_t                 op,
406                               pixman_image_t *            dest,
407                               pixman_color_t *            color,
408                               int                         n_rects,
409                               const pixman_rectangle16_t *rects)
410 {
411     pixman_box32_t stack_boxes[6];
412     pixman_box32_t *boxes;
413     pixman_bool_t result;
414     int i;
415
416     if (n_rects > 6)
417     {
418         boxes = pixman_malloc_ab (sizeof (pixman_box32_t), n_rects);
419         if (boxes == NULL)
420             return FALSE;
421     }
422     else
423     {
424         boxes = stack_boxes;
425     }
426
427     for (i = 0; i < n_rects; ++i)
428     {
429         boxes[i].x1 = rects[i].x;
430         boxes[i].y1 = rects[i].y;
431         boxes[i].x2 = boxes[i].x1 + rects[i].width;
432         boxes[i].y2 = boxes[i].y1 + rects[i].height;
433     }
434
435     result = pixman_image_fill_boxes (op, dest, color, n_rects, boxes);
436
437     if (boxes != stack_boxes)
438         free (boxes);
439     
440     return result;
441 }
442
443 PIXMAN_EXPORT pixman_bool_t
444 pixman_image_fill_boxes (pixman_op_t           op,
445                          pixman_image_t *      dest,
446                          pixman_color_t *      color,
447                          int                   n_boxes,
448                          const pixman_box32_t *boxes)
449 {
450     pixman_image_t *solid;
451     pixman_color_t c;
452     int i;
453
454     _pixman_image_validate (dest);
455     
456     if (color->alpha == 0xffff)
457     {
458         if (op == PIXMAN_OP_OVER)
459             op = PIXMAN_OP_SRC;
460     }
461
462     if (op == PIXMAN_OP_CLEAR)
463     {
464         c.red = 0;
465         c.green = 0;
466         c.blue = 0;
467         c.alpha = 0;
468
469         color = &c;
470
471         op = PIXMAN_OP_SRC;
472     }
473
474     if (op == PIXMAN_OP_SRC)
475     {
476         uint32_t pixel;
477
478         if (color_to_pixel (color, &pixel, dest->bits.format))
479         {
480             pixman_region32_t fill_region;
481             int n_rects, j;
482             pixman_box32_t *rects;
483
484             if (!pixman_region32_init_rects (&fill_region, boxes, n_boxes))
485                 return FALSE;
486
487             if (dest->common.have_clip_region)
488             {
489                 if (!pixman_region32_intersect (&fill_region,
490                                                 &fill_region,
491                                                 &dest->common.clip_region))
492                     return FALSE;
493             }
494
495             rects = pixman_region32_rectangles (&fill_region, &n_rects);
496             for (j = 0; j < n_rects; ++j)
497             {
498                 const pixman_box32_t *rect = &(rects[j]);
499                 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
500                              rect->x1, rect->y1, rect->x2 - rect->x1, rect->y2 - rect->y1,
501                              pixel);
502             }
503
504             pixman_region32_fini (&fill_region);
505             return TRUE;
506         }
507     }
508
509     solid = pixman_image_create_solid_fill (color);
510     if (!solid)
511         return FALSE;
512
513     for (i = 0; i < n_boxes; ++i)
514     {
515         const pixman_box32_t *box = &(boxes[i]);
516
517         pixman_image_composite32 (op, solid, NULL, dest,
518                                   0, 0, 0, 0,
519                                   box->x1, box->y1,
520                                   box->x2 - box->x1, box->y2 - box->y1);
521     }
522
523     pixman_image_unref (solid);
524
525     return TRUE;
526 }
527
528 /**
529  * pixman_version:
530  *
531  * Returns the version of the pixman library encoded in a single
532  * integer as per %PIXMAN_VERSION_ENCODE. The encoding ensures that
533  * later versions compare greater than earlier versions.
534  *
535  * A run-time comparison to check that pixman's version is greater than
536  * or equal to version X.Y.Z could be performed as follows:
537  *
538  * <informalexample><programlisting>
539  * if (pixman_version() >= PIXMAN_VERSION_ENCODE(X,Y,Z)) {...}
540  * </programlisting></informalexample>
541  *
542  * See also pixman_version_string() as well as the compile-time
543  * equivalents %PIXMAN_VERSION and %PIXMAN_VERSION_STRING.
544  *
545  * Return value: the encoded version.
546  **/
547 PIXMAN_EXPORT int
548 pixman_version (void)
549 {
550     return PIXMAN_VERSION;
551 }
552
553 /**
554  * pixman_version_string:
555  *
556  * Returns the version of the pixman library as a human-readable string
557  * of the form "X.Y.Z".
558  *
559  * See also pixman_version() as well as the compile-time equivalents
560  * %PIXMAN_VERSION_STRING and %PIXMAN_VERSION.
561  *
562  * Return value: a string containing the version.
563  **/
564 PIXMAN_EXPORT const char*
565 pixman_version_string (void)
566 {
567     return PIXMAN_VERSION_STRING;
568 }
569
570 /**
571  * pixman_format_supported_source:
572  * @format: A pixman_format_code_t format
573  *
574  * Return value: whether the provided format code is a supported
575  * format for a pixman surface used as a source in
576  * rendering.
577  *
578  * Currently, all pixman_format_code_t values are supported.
579  **/
580 PIXMAN_EXPORT pixman_bool_t
581 pixman_format_supported_source (pixman_format_code_t format)
582 {
583     switch (format)
584     {
585     /* 32 bpp formats */
586     case PIXMAN_a2b10g10r10:
587     case PIXMAN_x2b10g10r10:
588     case PIXMAN_a2r10g10b10:
589     case PIXMAN_x2r10g10b10:
590     case PIXMAN_a8r8g8b8:
591     case PIXMAN_x8r8g8b8:
592     case PIXMAN_a8b8g8r8:
593     case PIXMAN_x8b8g8r8:
594     case PIXMAN_b8g8r8a8:
595     case PIXMAN_b8g8r8x8:
596     case PIXMAN_r8g8b8:
597     case PIXMAN_b8g8r8:
598     case PIXMAN_r5g6b5:
599     case PIXMAN_b5g6r5:
600     /* 16 bpp formats */
601     case PIXMAN_a1r5g5b5:
602     case PIXMAN_x1r5g5b5:
603     case PIXMAN_a1b5g5r5:
604     case PIXMAN_x1b5g5r5:
605     case PIXMAN_a4r4g4b4:
606     case PIXMAN_x4r4g4b4:
607     case PIXMAN_a4b4g4r4:
608     case PIXMAN_x4b4g4r4:
609     /* 8bpp formats */
610     case PIXMAN_a8:
611     case PIXMAN_r3g3b2:
612     case PIXMAN_b2g3r3:
613     case PIXMAN_a2r2g2b2:
614     case PIXMAN_a2b2g2r2:
615     case PIXMAN_c8:
616     case PIXMAN_g8:
617     case PIXMAN_x4a4:
618     /* Collides with PIXMAN_c8
619        case PIXMAN_x4c4:
620      */
621     /* Collides with PIXMAN_g8
622        case PIXMAN_x4g4:
623      */
624     /* 4bpp formats */
625     case PIXMAN_a4:
626     case PIXMAN_r1g2b1:
627     case PIXMAN_b1g2r1:
628     case PIXMAN_a1r1g1b1:
629     case PIXMAN_a1b1g1r1:
630     case PIXMAN_c4:
631     case PIXMAN_g4:
632     /* 1bpp formats */
633     case PIXMAN_a1:
634     case PIXMAN_g1:
635     /* YUV formats */
636     case PIXMAN_yuy2:
637     case PIXMAN_yv12:
638         return TRUE;
639
640     default:
641         return FALSE;
642     }
643 }
644
645 /**
646  * pixman_format_supported_destination:
647  * @format: A pixman_format_code_t format
648  *
649  * Return value: whether the provided format code is a supported
650  * format for a pixman surface used as a destination in
651  * rendering.
652  *
653  * Currently, all pixman_format_code_t values are supported
654  * except for the YUV formats.
655  **/
656 PIXMAN_EXPORT pixman_bool_t
657 pixman_format_supported_destination (pixman_format_code_t format)
658 {
659     /* YUV formats cannot be written to at the moment */
660     if (format == PIXMAN_yuy2 || format == PIXMAN_yv12)
661         return FALSE;
662
663     return pixman_format_supported_source (format);
664 }
665