1 /* -*- Mode: c; c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t; -*- */
3 * Copyright © 2000 SuSE, Inc.
4 * Copyright © 2007 Red Hat, Inc.
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.
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.
23 * Author: Keith Packard, SuSE, Inc.
29 #include "pixman-private.h"
32 * Operator optimizations based on source or destination opacity
37 pixman_op_t opSrcDstOpaque;
38 pixman_op_t opSrcOpaque;
39 pixman_op_t opDstOpaque;
40 } optimized_operator_info_t;
42 static const optimized_operator_info_t optimized_operators[] =
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 },
59 * Check if the current operator could be optimized
61 static const optimized_operator_info_t*
62 pixman_operator_can_be_optimized(pixman_op_t op)
64 const optimized_operator_info_t *info;
66 for (info = optimized_operators; info->op != PIXMAN_OP_NONE; info++)
75 * Optimize the current operator based on opacity of source or destination
76 * The output operator should be mathematically equivalent to the source.
79 pixman_optimize_operator(pixman_op_t op, pixman_image_t *src_image, pixman_image_t *mask_image, pixman_image_t *dst_image )
81 pixman_bool_t is_source_opaque;
82 pixman_bool_t is_dest_opaque;
83 const optimized_operator_info_t *info = pixman_operator_can_be_optimized(op);
85 if(!info || mask_image)
88 is_source_opaque = _pixman_image_is_opaque(src_image);
89 is_dest_opaque = _pixman_image_is_opaque(dst_image);
91 if(is_source_opaque == FALSE && is_dest_opaque == FALSE)
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;
105 static pixman_implementation_t *imp;
108 pixman_image_composite (pixman_op_t op,
109 pixman_image_t * src,
110 pixman_image_t * mask,
111 pixman_image_t * dest,
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.
125 op = pixman_optimize_operator(op, src, mask, dest);
126 if(op == PIXMAN_OP_DST)
130 imp = _pixman_choose_implementation();
132 _pixman_implementation_composite (imp, op,
140 PIXMAN_EXPORT pixman_bool_t
141 pixman_blt (uint32_t *src_bits,
147 int src_x, int src_y,
148 int dst_x, int dst_y,
149 int width, int height)
152 imp = _pixman_choose_implementation();
154 return _pixman_implementation_blt (imp, src_bits, dst_bits, src_stride, dst_stride,
161 PIXMAN_EXPORT pixman_bool_t
162 pixman_fill (uint32_t *bits,
172 imp = _pixman_choose_implementation();
174 return _pixman_implementation_fill (imp, bits, stride, bpp, x, y, width, height, xor);
178 color_to_uint32 (const pixman_color_t *color)
181 (color->alpha >> 8 << 24) |
182 (color->red >> 8 << 16) |
183 (color->green & 0xff00) |
188 color_to_pixel (pixman_color_t *color,
190 pixman_format_code_t format)
192 uint32_t c = color_to_uint32 (color);
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))
207 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
209 c = ((c & 0xff000000) >> 0) |
210 ((c & 0x00ff0000) >> 16) |
211 ((c & 0x0000ff00) >> 0) |
212 ((c & 0x000000ff) << 16);
214 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
216 c = ((c & 0xff000000) >> 24) |
217 ((c & 0x00ff0000) >> 8) |
218 ((c & 0x0000ff00) << 8) |
219 ((c & 0x000000ff) << 24);
222 if (format == PIXMAN_a8)
224 else if (format == PIXMAN_r5g6b5 ||
225 format == PIXMAN_b5g6r5)
226 c = cvt8888to0565 (c);
229 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
230 printf ("pixel: %x\n", c);
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,
242 const pixman_rectangle16_t *rects)
244 pixman_image_t *solid;
248 if (color->alpha == 0xffff)
250 if (op == PIXMAN_OP_OVER)
254 if (op == PIXMAN_OP_CLEAR)
266 if (op == PIXMAN_OP_SRC)
270 if (color_to_pixel (color, &pixel, dest->bits.format))
272 for (i = 0; i < n_rects; ++i)
274 pixman_region32_t fill_region;
276 pixman_box32_t *boxes;
278 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
280 if (dest->common.have_clip_region)
282 if (!pixman_region32_intersect (&fill_region,
284 &dest->common.clip_region))
288 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
289 for (j = 0; j < n_boxes; ++j)
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,
297 pixman_region32_fini (&fill_region);
303 solid = pixman_image_create_solid_fill (color);
307 for (i = 0; i < n_rects; ++i)
309 const pixman_rectangle16_t *rect = &(rects[i]);
311 pixman_image_composite (op, solid, NULL, dest,
314 rect->width, rect->height);
317 pixman_image_unref (solid);
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.
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:
332 * <informalexample><programlisting>
333 * if (pixman_version() >= PIXMAN_VERSION_ENCODE(X,Y,Z)) {...}
334 * </programlisting></informalexample>
336 * See also pixman_version_string() as well as the compile-time
337 * equivalents %PIXMAN_VERSION and %PIXMAN_VERSION_STRING.
339 * Return value: the encoded version.
342 pixman_version (void)
344 return PIXMAN_VERSION;
348 * pixman_version_string:
350 * Returns the version of the pixman library as a human-readable string
351 * of the form "X.Y.Z".
353 * See also pixman_version() as well as the compile-time equivalents
354 * %PIXMAN_VERSION_STRING and %PIXMAN_VERSION.
356 * Return value: a string containing the version.
358 PIXMAN_EXPORT const char*
359 pixman_version_string (void)
361 return PIXMAN_VERSION_STRING;
365 * pixman_format_supported_source:
366 * @format: A pixman_format_code_t format
368 * Return value: whether the provided format code is a supported
369 * format for a pixman surface used as a source in
372 * Currently, all pixman_format_code_t values are supported.
374 PIXMAN_EXPORT pixman_bool_t
375 pixman_format_supported_source (pixman_format_code_t format)
379 case PIXMAN_a2b10g10r10:
380 case PIXMAN_x2b10g10r10:
381 case PIXMAN_a2r10g10b10:
382 case PIXMAN_x2r10g10b10:
383 case PIXMAN_a8r8g8b8:
384 case PIXMAN_x8r8g8b8:
385 case PIXMAN_a8b8g8r8:
386 case PIXMAN_x8b8g8r8:
387 case PIXMAN_b8g8r8a8:
388 case PIXMAN_b8g8r8x8:
394 case PIXMAN_a1r5g5b5:
395 case PIXMAN_x1r5g5b5:
396 case PIXMAN_a1b5g5r5:
397 case PIXMAN_x1b5g5r5:
398 case PIXMAN_a4r4g4b4:
399 case PIXMAN_x4r4g4b4:
400 case PIXMAN_a4b4g4r4:
401 case PIXMAN_x4b4g4r4:
406 case PIXMAN_a2r2g2b2:
407 case PIXMAN_a2b2g2r2:
411 /* Collides with PIXMAN_c8
414 /* Collides with PIXMAN_g8
421 case PIXMAN_a1r1g1b1:
422 case PIXMAN_a1b1g1r1:
439 * pixman_format_supported_destination:
440 * @format: A pixman_format_code_t format
442 * Return value: whether the provided format code is a supported
443 * format for a pixman surface used as a destination in
446 * Currently, all pixman_format_code_t values are supported
447 * except for the YUV formats.
449 PIXMAN_EXPORT pixman_bool_t
450 pixman_format_supported_destination (pixman_format_code_t format)
452 /* YUV formats cannot be written to at the moment */
453 if (format == PIXMAN_yuy2 || format == PIXMAN_yv12)
456 return pixman_format_supported_source (format);