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