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