Move pixman_version() to pixman.c
[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                 opSrcDstOpaque;
38     pixman_op_t                 opSrcOpaque;
39     pixman_op_t                 opDstOpaque;
40 } OptimizedOperatorInfo;
41
42 static const OptimizedOperatorInfo 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 /*
59  * Check if the current operator could be optimized
60  */
61 static const OptimizedOperatorInfo*
62 pixman_operator_can_be_optimized(pixman_op_t op)
63 {
64     const OptimizedOperatorInfo *info;
65
66     for (info = optimized_operators; info->op != PIXMAN_OP_NONE; info++)
67     {
68         if(info->op == op)
69             return info;
70     }
71     return NULL;
72 }
73
74 /*
75  * Optimize the current operator based on opacity of source or destination
76  * The output operator should be mathematically equivalent to the source.
77  */
78 static pixman_op_t
79 pixman_optimize_operator(pixman_op_t op, pixman_image_t *pSrc, pixman_image_t *pMask, pixman_image_t *pDst )
80 {
81     pixman_bool_t is_source_opaque;
82     pixman_bool_t is_dest_opaque;
83     const OptimizedOperatorInfo *info = pixman_operator_can_be_optimized(op);
84
85     if(!info || pMask)
86         return op;
87
88     is_source_opaque = pixman_image_is_opaque(pSrc);
89     is_dest_opaque = pixman_image_is_opaque(pDst);
90
91     if(is_source_opaque == FALSE && is_dest_opaque == FALSE)
92         return op;
93
94     if(is_source_opaque && is_dest_opaque)
95         return info->opSrcDstOpaque;
96     else if(is_source_opaque)
97         return info->opSrcOpaque;
98     else if(is_dest_opaque)
99         return info->opDstOpaque;
100
101     return op;
102
103 }
104
105 static pixman_implementation_t *imp;
106
107 PIXMAN_EXPORT void
108 pixman_image_composite (pixman_op_t      op,
109                         pixman_image_t * src,
110                         pixman_image_t * mask,
111                         pixman_image_t * dest,
112                         int16_t      src_x,
113                         int16_t      src_y,
114                         int16_t      mask_x,
115                         int16_t      mask_y,
116                         int16_t      dest_x,
117                         int16_t      dest_y,
118                         uint16_t     width,
119                         uint16_t     height)
120 {
121     /*
122      * Check if we can replace our operator by a simpler one if the src or dest are opaque
123      * The output operator should be mathematically equivalent to the source.
124      */
125     op = pixman_optimize_operator(op, src, mask, dest);
126     if(op == PIXMAN_OP_DST)
127         return;
128
129     if (!imp)
130         imp = _pixman_choose_implementation();
131
132     _pixman_implementation_composite (imp, op,
133                                       src, mask, dest,
134                                       src_x, src_y,
135                                       mask_x, mask_y,
136                                       dest_x, dest_y,
137                                       width, height);
138 }
139
140 PIXMAN_EXPORT pixman_bool_t
141 pixman_blt (uint32_t *src_bits,
142             uint32_t *dst_bits,
143             int src_stride,
144             int dst_stride,
145             int src_bpp,
146             int dst_bpp,
147             int src_x, int src_y,
148             int dst_x, int dst_y,
149             int width, int height)
150 {
151     if (!imp)
152         imp = _pixman_choose_implementation();
153     
154     return _pixman_implementation_blt (imp, src_bits, dst_bits, src_stride, dst_stride,
155                                        src_bpp, dst_bpp,
156                                        src_x, src_y,
157                                        dst_x, dst_y,
158                                        width, height);
159 }
160
161 PIXMAN_EXPORT pixman_bool_t
162 pixman_fill (uint32_t *bits,
163              int stride,
164              int bpp,
165              int x,
166              int y,
167              int width,
168              int height,
169              uint32_t xor)
170 {
171     if (!imp)
172         imp = _pixman_choose_implementation();
173
174     return _pixman_implementation_fill (imp, bits, stride, bpp, x, y, width, height, xor);
175 }
176
177 static uint32_t
178 color_to_uint32 (const pixman_color_t *color)
179 {
180     return
181         (color->alpha >> 8 << 24) |
182         (color->red >> 8 << 16) |
183         (color->green & 0xff00) |
184         (color->blue >> 8);
185 }
186
187 static pixman_bool_t
188 color_to_pixel (pixman_color_t *color,
189                 uint32_t       *pixel,
190                 pixman_format_code_t format)
191 {
192     uint32_t c = color_to_uint32 (color);
193
194     if (!(format == PIXMAN_a8r8g8b8     ||
195           format == PIXMAN_x8r8g8b8     ||
196           format == PIXMAN_a8b8g8r8     ||
197           format == PIXMAN_x8b8g8r8     ||
198           format == PIXMAN_b8g8r8a8     ||
199           format == PIXMAN_b8g8r8x8     ||
200           format == PIXMAN_r5g6b5       ||
201           format == PIXMAN_b5g6r5       ||
202           format == PIXMAN_a8))
203     {
204         return FALSE;
205     }
206
207     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
208     {
209         c = ((c & 0xff000000) >>  0) |
210             ((c & 0x00ff0000) >> 16) |
211             ((c & 0x0000ff00) >>  0) |
212             ((c & 0x000000ff) << 16);
213     }
214     if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
215     {
216         c = ((c & 0xff000000) >> 24) |
217             ((c & 0x00ff0000) >>  8) |
218             ((c & 0x0000ff00) <<  8) |
219             ((c & 0x000000ff) << 24);
220     }
221
222     if (format == PIXMAN_a8)
223         c = c >> 24;
224     else if (format == PIXMAN_r5g6b5 ||
225              format == PIXMAN_b5g6r5)
226         c = cvt8888to0565 (c);
227
228 #if 0
229     printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
230     printf ("pixel: %x\n", c);
231 #endif
232
233     *pixel = c;
234     return TRUE;
235 }
236
237 PIXMAN_EXPORT pixman_bool_t
238 pixman_image_fill_rectangles (pixman_op_t                   op,
239                               pixman_image_t               *dest,
240                               pixman_color_t               *color,
241                               int                           n_rects,
242                               const pixman_rectangle16_t   *rects)
243 {
244     pixman_image_t *solid;
245     pixman_color_t c;
246     int i;
247
248     if (color->alpha == 0xffff)
249     {
250         if (op == PIXMAN_OP_OVER)
251             op = PIXMAN_OP_SRC;
252     }
253
254     if (op == PIXMAN_OP_CLEAR)
255     {
256         c.red = 0;
257         c.green = 0;
258         c.blue = 0;
259         c.alpha = 0;
260
261         color = &c;
262
263         op = PIXMAN_OP_SRC;
264     }
265
266     if (op == PIXMAN_OP_SRC)
267     {
268         uint32_t pixel;
269
270         if (color_to_pixel (color, &pixel, dest->bits.format))
271         {
272             for (i = 0; i < n_rects; ++i)
273             {
274                 pixman_region32_t fill_region;
275                 int n_boxes, j;
276                 pixman_box32_t *boxes;
277
278                 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
279
280                 if (dest->common.have_clip_region)
281                 {
282                     if (!pixman_region32_intersect (&fill_region,
283                                                     &fill_region,
284                                                     &dest->common.clip_region))
285                         return FALSE;
286                 }
287
288                 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
289                 for (j = 0; j < n_boxes; ++j)
290                 {
291                     const pixman_box32_t *box = &(boxes[j]);
292                     pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
293                                  box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
294                                  pixel);
295                 }
296
297                 pixman_region32_fini (&fill_region);
298             }
299             return TRUE;
300         }
301     }
302
303     solid = pixman_image_create_solid_fill (color);
304     if (!solid)
305         return FALSE;
306
307     for (i = 0; i < n_rects; ++i)
308     {
309         const pixman_rectangle16_t *rect = &(rects[i]);
310
311         pixman_image_composite (op, solid, NULL, dest,
312                                 0, 0, 0, 0,
313                                 rect->x, rect->y,
314                                 rect->width, rect->height);
315     }
316
317     pixman_image_unref (solid);
318
319     return TRUE;
320 }
321
322 /**
323  * pixman_version:
324  *
325  * Returns the version of the pixman library encoded in a single
326  * integer as per %PIXMAN_VERSION_ENCODE. The encoding ensures that
327  * later versions compare greater than earlier versions.
328  *
329  * A run-time comparison to check that pixman's version is greater than
330  * or equal to version X.Y.Z could be performed as follows:
331  *
332  * <informalexample><programlisting>
333  * if (pixman_version() >= PIXMAN_VERSION_ENCODE(X,Y,Z)) {...}
334  * </programlisting></informalexample>
335  *
336  * See also pixman_version_string() as well as the compile-time
337  * equivalents %PIXMAN_VERSION and %PIXMAN_VERSION_STRING.
338  *
339  * Return value: the encoded version.
340  **/
341 PIXMAN_EXPORT int
342 pixman_version (void)
343 {
344     return PIXMAN_VERSION;
345 }
346