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