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