Don't call _pixman_implementation_composite() anymore.
[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     /* Some X servers generate images that point to the
121      * wrong place in memory, but then set the clip region
122      * to point to the right place. Because of an old bug
123      * in pixman, this would actually work.
124      *
125      * Here we try and undo the damage
126      */
127     int bpp = PIXMAN_FORMAT_BPP (image->bits.format) / 8;
128     pixman_box32_t *extents;
129     uint8_t *t;
130     int dx, dy;
131
132     extents = pixman_region32_extents (&(image->common.clip_region));
133     dx = extents->x1;
134     dy = extents->y1;
135
136     *save_bits = image->bits.bits;
137
138     *x -= dx;
139     *y -= dy;
140     pixman_region32_translate (&(image->common.clip_region), -dx, -dy);
141
142     t = (uint8_t *)image->bits.bits;
143     t += dy * image->bits.rowstride * 4 + dx * bpp;
144     image->bits.bits = (uint32_t *)t;
145
146     *save_dx = dx;
147     *save_dy = dy;
148 }
149
150 static void
151 unapply_workaround (pixman_image_t *image, uint32_t *bits, int dx, int dy)
152 {
153     image->bits.bits = bits;
154     pixman_region32_translate (&image->common.clip_region, dx, dy);
155 }
156
157 PIXMAN_EXPORT void
158 pixman_image_composite (pixman_op_t      op,
159                         pixman_image_t * src,
160                         pixman_image_t * mask,
161                         pixman_image_t * dest,
162                         int16_t          src_x,
163                         int16_t          src_y,
164                         int16_t          mask_x,
165                         int16_t          mask_y,
166                         int16_t          dest_x,
167                         int16_t          dest_y,
168                         uint16_t         width,
169                         uint16_t         height)
170 {
171     pixman_image_composite32 (op, src, mask, dest, src_x, src_y, 
172                               mask_x, mask_y, dest_x, dest_y, width, height);
173 }
174
175 PIXMAN_EXPORT void
176 pixman_image_composite32 (pixman_op_t      op,
177                           pixman_image_t * src,
178                           pixman_image_t * mask,
179                           pixman_image_t * dest,
180                           int32_t          src_x,
181                           int32_t          src_y,
182                           int32_t          mask_x,
183                           int32_t          mask_y,
184                           int32_t          dest_x,
185                           int32_t          dest_y,
186                           int32_t          width,
187                           int32_t          height)
188 {
189     uint32_t *src_bits;
190     int src_dx, src_dy;
191     uint32_t *mask_bits;
192     int mask_dx, mask_dy;
193     uint32_t *dest_bits;
194     int dest_dx, dest_dy;
195     pixman_implementation_t *i;
196
197     _pixman_image_validate (src);
198     if (mask)
199         _pixman_image_validate (mask);
200     _pixman_image_validate (dest);
201     
202     /*
203      * Check if we can replace our operator by a simpler one
204      * if the src or dest are opaque. The output operator should be
205      * mathematically equivalent to the source.
206      */
207     op = pixman_optimize_operator(op, src, mask, dest);
208     if (op == PIXMAN_OP_DST             ||
209         op == PIXMAN_OP_CONJOINT_DST    ||
210         op == PIXMAN_OP_DISJOINT_DST)
211     {
212         return;
213     }
214
215     if (!imp)
216         imp = _pixman_choose_implementation ();
217
218     if (src->common.need_workaround)
219         apply_workaround (src, &src_x, &src_y, &src_bits, &src_dx, &src_dy);
220     if (mask && mask->common.need_workaround)
221         apply_workaround (mask, &mask_x, &mask_y, &mask_bits, &mask_dx, &mask_dy);
222     if (dest->common.need_workaround)
223         apply_workaround (dest, &dest_x, &dest_y, &dest_bits, &dest_dx, &dest_dy);
224
225     for (i = imp; i != NULL; i = i->delegate)
226     {
227         if (_pixman_run_fast_path (i->fast_paths, imp,
228                                    op, src, mask, dest,
229                                    src_x, src_y,
230                                    mask_x, mask_y,
231                                    dest_x, dest_y,
232                                    width, height))
233         {
234             break;
235         }
236     }
237                 
238     if (src->common.need_workaround)
239         unapply_workaround (src, src_bits, src_dx, src_dy);
240     if (mask && mask->common.need_workaround)
241         unapply_workaround (mask, mask_bits, mask_dx, mask_dy);
242     if (dest->common.need_workaround)
243         unapply_workaround (dest, dest_bits, dest_dx, dest_dy);
244 }
245
246 PIXMAN_EXPORT pixman_bool_t
247 pixman_blt (uint32_t *src_bits,
248             uint32_t *dst_bits,
249             int       src_stride,
250             int       dst_stride,
251             int       src_bpp,
252             int       dst_bpp,
253             int       src_x,
254             int       src_y,
255             int       dst_x,
256             int       dst_y,
257             int       width,
258             int       height)
259 {
260     if (!imp)
261         imp = _pixman_choose_implementation ();
262
263     return _pixman_implementation_blt (imp, src_bits, dst_bits, src_stride, dst_stride,
264                                        src_bpp, dst_bpp,
265                                        src_x, src_y,
266                                        dst_x, dst_y,
267                                        width, height);
268 }
269
270 PIXMAN_EXPORT pixman_bool_t
271 pixman_fill (uint32_t *bits,
272              int       stride,
273              int       bpp,
274              int       x,
275              int       y,
276              int       width,
277              int       height,
278              uint32_t xor)
279 {
280     if (!imp)
281         imp = _pixman_choose_implementation ();
282
283     return _pixman_implementation_fill (imp, bits, stride, bpp, x, y, width, height, xor);
284 }
285
286 static uint32_t
287 color_to_uint32 (const pixman_color_t *color)
288 {
289     return
290         (color->alpha >> 8 << 24) |
291         (color->red >> 8 << 16) |
292         (color->green & 0xff00) |
293         (color->blue >> 8);
294 }
295
296 static pixman_bool_t
297 color_to_pixel (pixman_color_t *     color,
298                 uint32_t *           pixel,
299                 pixman_format_code_t format)
300 {
301     uint32_t c = color_to_uint32 (color);
302
303     if (!(format == PIXMAN_a8r8g8b8     ||
304           format == PIXMAN_x8r8g8b8     ||
305           format == PIXMAN_a8b8g8r8     ||
306           format == PIXMAN_x8b8g8r8     ||
307           format == PIXMAN_b8g8r8a8     ||
308           format == PIXMAN_b8g8r8x8     ||
309           format == PIXMAN_r5g6b5       ||
310           format == PIXMAN_b5g6r5       ||
311           format == PIXMAN_a8))
312     {
313         return FALSE;
314     }
315
316     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
317     {
318         c = ((c & 0xff000000) >>  0) |
319             ((c & 0x00ff0000) >> 16) |
320             ((c & 0x0000ff00) >>  0) |
321             ((c & 0x000000ff) << 16);
322     }
323     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
324     {
325         c = ((c & 0xff000000) >> 24) |
326             ((c & 0x00ff0000) >>  8) |
327             ((c & 0x0000ff00) <<  8) |
328             ((c & 0x000000ff) << 24);
329     }
330
331     if (format == PIXMAN_a8)
332         c = c >> 24;
333     else if (format == PIXMAN_r5g6b5 ||
334              format == PIXMAN_b5g6r5)
335         c = CONVERT_8888_TO_0565 (c);
336
337 #if 0
338     printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
339     printf ("pixel: %x\n", c);
340 #endif
341
342     *pixel = c;
343     return TRUE;
344 }
345
346 PIXMAN_EXPORT pixman_bool_t
347 pixman_image_fill_rectangles (pixman_op_t                 op,
348                               pixman_image_t *            dest,
349                               pixman_color_t *            color,
350                               int                         n_rects,
351                               const pixman_rectangle16_t *rects)
352 {
353     pixman_box32_t stack_boxes[6];
354     pixman_box32_t *boxes;
355     pixman_bool_t result;
356     int i;
357
358     if (n_rects > 6)
359     {
360         boxes = pixman_malloc_ab (sizeof (pixman_box32_t), n_rects);
361         if (boxes == NULL)
362             return FALSE;
363     }
364     else
365     {
366         boxes = stack_boxes;
367     }
368
369     for (i = 0; i < n_rects; ++i)
370     {
371         boxes[i].x1 = rects[i].x;
372         boxes[i].y1 = rects[i].y;
373         boxes[i].x2 = boxes[i].x1 + rects[i].width;
374         boxes[i].y2 = boxes[i].y1 + rects[i].height;
375     }
376
377     result = pixman_image_fill_boxes (op, dest, color, n_rects, boxes);
378
379     if (boxes != stack_boxes)
380         free (boxes);
381     
382     return result;
383 }
384
385 PIXMAN_EXPORT pixman_bool_t
386 pixman_image_fill_boxes (pixman_op_t           op,
387                          pixman_image_t *      dest,
388                          pixman_color_t *      color,
389                          int                   n_boxes,
390                          const pixman_box32_t *boxes)
391 {
392     pixman_image_t *solid;
393     pixman_color_t c;
394     int i;
395
396     _pixman_image_validate (dest);
397     
398     if (color->alpha == 0xffff)
399     {
400         if (op == PIXMAN_OP_OVER)
401             op = PIXMAN_OP_SRC;
402     }
403
404     if (op == PIXMAN_OP_CLEAR)
405     {
406         c.red = 0;
407         c.green = 0;
408         c.blue = 0;
409         c.alpha = 0;
410
411         color = &c;
412
413         op = PIXMAN_OP_SRC;
414     }
415
416     if (op == PIXMAN_OP_SRC)
417     {
418         uint32_t pixel;
419
420         if (color_to_pixel (color, &pixel, dest->bits.format))
421         {
422             pixman_region32_t fill_region;
423             int n_rects, j;
424             pixman_box32_t *rects;
425
426             if (!pixman_region32_init_rects (&fill_region, boxes, n_boxes))
427                 return FALSE;
428
429             if (dest->common.have_clip_region)
430             {
431                 if (!pixman_region32_intersect (&fill_region,
432                                                 &fill_region,
433                                                 &dest->common.clip_region))
434                     return FALSE;
435             }
436
437             rects = pixman_region32_rectangles (&fill_region, &n_rects);
438             for (j = 0; j < n_rects; ++j)
439             {
440                 const pixman_box32_t *rect = &(rects[j]);
441                 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
442                              rect->x1, rect->y1, rect->x2 - rect->x1, rect->y2 - rect->y1,
443                              pixel);
444             }
445
446             pixman_region32_fini (&fill_region);
447             return TRUE;
448         }
449     }
450
451     solid = pixman_image_create_solid_fill (color);
452     if (!solid)
453         return FALSE;
454
455     for (i = 0; i < n_boxes; ++i)
456     {
457         const pixman_box32_t *box = &(boxes[i]);
458
459         pixman_image_composite32 (op, solid, NULL, dest,
460                                   0, 0, 0, 0,
461                                   box->x1, box->y1,
462                                   box->x2 - box->x1, box->y2 - box->y1);
463     }
464
465     pixman_image_unref (solid);
466
467     return TRUE;
468 }
469
470 /**
471  * pixman_version:
472  *
473  * Returns the version of the pixman library encoded in a single
474  * integer as per %PIXMAN_VERSION_ENCODE. The encoding ensures that
475  * later versions compare greater than earlier versions.
476  *
477  * A run-time comparison to check that pixman's version is greater than
478  * or equal to version X.Y.Z could be performed as follows:
479  *
480  * <informalexample><programlisting>
481  * if (pixman_version() >= PIXMAN_VERSION_ENCODE(X,Y,Z)) {...}
482  * </programlisting></informalexample>
483  *
484  * See also pixman_version_string() as well as the compile-time
485  * equivalents %PIXMAN_VERSION and %PIXMAN_VERSION_STRING.
486  *
487  * Return value: the encoded version.
488  **/
489 PIXMAN_EXPORT int
490 pixman_version (void)
491 {
492     return PIXMAN_VERSION;
493 }
494
495 /**
496  * pixman_version_string:
497  *
498  * Returns the version of the pixman library as a human-readable string
499  * of the form "X.Y.Z".
500  *
501  * See also pixman_version() as well as the compile-time equivalents
502  * %PIXMAN_VERSION_STRING and %PIXMAN_VERSION.
503  *
504  * Return value: a string containing the version.
505  **/
506 PIXMAN_EXPORT const char*
507 pixman_version_string (void)
508 {
509     return PIXMAN_VERSION_STRING;
510 }
511
512 /**
513  * pixman_format_supported_source:
514  * @format: A pixman_format_code_t format
515  *
516  * Return value: whether the provided format code is a supported
517  * format for a pixman surface used as a source in
518  * rendering.
519  *
520  * Currently, all pixman_format_code_t values are supported.
521  **/
522 PIXMAN_EXPORT pixman_bool_t
523 pixman_format_supported_source (pixman_format_code_t format)
524 {
525     switch (format)
526     {
527     /* 32 bpp formats */
528     case PIXMAN_a2b10g10r10:
529     case PIXMAN_x2b10g10r10:
530     case PIXMAN_a2r10g10b10:
531     case PIXMAN_x2r10g10b10:
532     case PIXMAN_a8r8g8b8:
533     case PIXMAN_x8r8g8b8:
534     case PIXMAN_a8b8g8r8:
535     case PIXMAN_x8b8g8r8:
536     case PIXMAN_b8g8r8a8:
537     case PIXMAN_b8g8r8x8:
538     case PIXMAN_r8g8b8:
539     case PIXMAN_b8g8r8:
540     case PIXMAN_r5g6b5:
541     case PIXMAN_b5g6r5:
542     /* 16 bpp formats */
543     case PIXMAN_a1r5g5b5:
544     case PIXMAN_x1r5g5b5:
545     case PIXMAN_a1b5g5r5:
546     case PIXMAN_x1b5g5r5:
547     case PIXMAN_a4r4g4b4:
548     case PIXMAN_x4r4g4b4:
549     case PIXMAN_a4b4g4r4:
550     case PIXMAN_x4b4g4r4:
551     /* 8bpp formats */
552     case PIXMAN_a8:
553     case PIXMAN_r3g3b2:
554     case PIXMAN_b2g3r3:
555     case PIXMAN_a2r2g2b2:
556     case PIXMAN_a2b2g2r2:
557     case PIXMAN_c8:
558     case PIXMAN_g8:
559     case PIXMAN_x4a4:
560     /* Collides with PIXMAN_c8
561        case PIXMAN_x4c4:
562      */
563     /* Collides with PIXMAN_g8
564        case PIXMAN_x4g4:
565      */
566     /* 4bpp formats */
567     case PIXMAN_a4:
568     case PIXMAN_r1g2b1:
569     case PIXMAN_b1g2r1:
570     case PIXMAN_a1r1g1b1:
571     case PIXMAN_a1b1g1r1:
572     case PIXMAN_c4:
573     case PIXMAN_g4:
574     /* 1bpp formats */
575     case PIXMAN_a1:
576     case PIXMAN_g1:
577     /* YUV formats */
578     case PIXMAN_yuy2:
579     case PIXMAN_yv12:
580         return TRUE;
581
582     default:
583         return FALSE;
584     }
585 }
586
587 /**
588  * pixman_format_supported_destination:
589  * @format: A pixman_format_code_t format
590  *
591  * Return value: whether the provided format code is a supported
592  * format for a pixman surface used as a destination in
593  * rendering.
594  *
595  * Currently, all pixman_format_code_t values are supported
596  * except for the YUV formats.
597  **/
598 PIXMAN_EXPORT pixman_bool_t
599 pixman_format_supported_destination (pixman_format_code_t format)
600 {
601     /* YUV formats cannot be written to at the moment */
602     if (format == PIXMAN_yuy2 || format == PIXMAN_yv12)
603         return FALSE;
604
605     return pixman_format_supported_source (format);
606 }
607