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