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 op_src_dst_opaque;
38 pixman_op_t op_src_opaque;
39 pixman_op_t op_dst_opaque;
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 },
58 static pixman_implementation_t *imp;
61 * Check if the current operator could be optimized
63 static const optimized_operator_info_t*
64 pixman_operator_can_be_optimized (pixman_op_t op)
66 const optimized_operator_info_t *info;
68 for (info = optimized_operators; info->op != PIXMAN_OP_NONE; info++)
77 * Optimize the current operator based on opacity of source or destination
78 * The output operator should be mathematically equivalent to the source.
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)
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);
90 if (!info || mask_image)
93 is_source_opaque = _pixman_image_is_opaque (src_image);
94 is_dest_opaque = _pixman_image_is_opaque (dst_image);
96 if (is_source_opaque == FALSE && is_dest_opaque == FALSE)
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;
111 apply_workaround (pixman_image_t *image,
114 uint32_t ** save_bits,
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.
123 * Here we try and undo the damage
125 int bpp = PIXMAN_FORMAT_BPP (image->bits.format) / 8;
126 pixman_box32_t *extents;
130 extents = pixman_region32_extents (&(image->common.clip_region));
134 *save_bits = image->bits.bits;
138 pixman_region32_translate (&(image->common.clip_region), -dx, -dy);
140 t = (uint8_t *)image->bits.bits;
141 t += dy * image->bits.rowstride * 4 + dx * bpp;
142 image->bits.bits = (uint32_t *)t;
149 unapply_workaround (pixman_image_t *image, uint32_t *bits, int dx, int dy)
151 image->bits.bits = bits;
152 pixman_region32_translate (&image->common.clip_region, dx, dy);
156 pixman_image_composite (pixman_op_t op,
157 pixman_image_t * src,
158 pixman_image_t * mask,
159 pixman_image_t * dest,
169 pixman_image_composite32 (op, src, mask, dest, src_x, src_y,
170 mask_x, mask_y, dest_x, dest_y, width, height);
174 pixman_image_composite32 (pixman_op_t op,
175 pixman_image_t * src,
176 pixman_image_t * mask,
177 pixman_image_t * dest,
190 int mask_dx, mask_dy;
192 int dest_dx, dest_dy;
194 _pixman_image_validate (src);
196 _pixman_image_validate (mask);
197 _pixman_image_validate (dest);
200 * Check if we can replace our operator by a simpler one
201 * if the src or dest are opaque. The output operator should be
202 * mathematically equivalent to the source.
204 op = pixman_optimize_operator(op, src, mask, dest);
205 if (op == PIXMAN_OP_DST ||
206 op == PIXMAN_OP_CONJOINT_DST ||
207 op == PIXMAN_OP_DISJOINT_DST)
213 imp = _pixman_choose_implementation ();
215 if (src->common.need_workaround)
216 apply_workaround (src, &src_x, &src_y, &src_bits, &src_dx, &src_dy);
217 if (mask && mask->common.need_workaround)
218 apply_workaround (mask, &mask_x, &mask_y, &mask_bits, &mask_dx, &mask_dy);
219 if (dest->common.need_workaround)
220 apply_workaround (dest, &dest_x, &dest_y, &dest_bits, &dest_dx, &dest_dy);
222 _pixman_implementation_composite (imp, op,
229 if (src->common.need_workaround)
230 unapply_workaround (src, src_bits, src_dx, src_dy);
231 if (mask && mask->common.need_workaround)
232 unapply_workaround (mask, mask_bits, mask_dx, mask_dy);
233 if (dest->common.need_workaround)
234 unapply_workaround (dest, dest_bits, dest_dx, dest_dy);
237 PIXMAN_EXPORT pixman_bool_t
238 pixman_blt (uint32_t *src_bits,
252 imp = _pixman_choose_implementation ();
254 return _pixman_implementation_blt (imp, src_bits, dst_bits, src_stride, dst_stride,
261 PIXMAN_EXPORT pixman_bool_t
262 pixman_fill (uint32_t *bits,
272 imp = _pixman_choose_implementation ();
274 return _pixman_implementation_fill (imp, bits, stride, bpp, x, y, width, height, xor);
278 color_to_uint32 (const pixman_color_t *color)
281 (color->alpha >> 8 << 24) |
282 (color->red >> 8 << 16) |
283 (color->green & 0xff00) |
288 color_to_pixel (pixman_color_t * color,
290 pixman_format_code_t format)
292 uint32_t c = color_to_uint32 (color);
294 if (!(format == PIXMAN_a8r8g8b8 ||
295 format == PIXMAN_x8r8g8b8 ||
296 format == PIXMAN_a8b8g8r8 ||
297 format == PIXMAN_x8b8g8r8 ||
298 format == PIXMAN_b8g8r8a8 ||
299 format == PIXMAN_b8g8r8x8 ||
300 format == PIXMAN_r5g6b5 ||
301 format == PIXMAN_b5g6r5 ||
302 format == PIXMAN_a8))
307 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
309 c = ((c & 0xff000000) >> 0) |
310 ((c & 0x00ff0000) >> 16) |
311 ((c & 0x0000ff00) >> 0) |
312 ((c & 0x000000ff) << 16);
314 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
316 c = ((c & 0xff000000) >> 24) |
317 ((c & 0x00ff0000) >> 8) |
318 ((c & 0x0000ff00) << 8) |
319 ((c & 0x000000ff) << 24);
322 if (format == PIXMAN_a8)
324 else if (format == PIXMAN_r5g6b5 ||
325 format == PIXMAN_b5g6r5)
326 c = CONVERT_8888_TO_0565 (c);
329 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
330 printf ("pixel: %x\n", c);
337 PIXMAN_EXPORT pixman_bool_t
338 pixman_image_fill_rectangles (pixman_op_t op,
339 pixman_image_t * dest,
340 pixman_color_t * color,
342 const pixman_rectangle16_t *rects)
344 pixman_image_t *solid;
348 _pixman_image_validate (dest);
350 if (color->alpha == 0xffff)
352 if (op == PIXMAN_OP_OVER)
356 if (op == PIXMAN_OP_CLEAR)
368 if (op == PIXMAN_OP_SRC)
372 if (color_to_pixel (color, &pixel, dest->bits.format))
374 for (i = 0; i < n_rects; ++i)
376 pixman_region32_t fill_region;
378 pixman_box32_t *boxes;
380 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
382 if (dest->common.have_clip_region)
384 if (!pixman_region32_intersect (&fill_region,
386 &dest->common.clip_region))
390 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
391 for (j = 0; j < n_boxes; ++j)
393 const pixman_box32_t *box = &(boxes[j]);
394 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
395 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
399 pixman_region32_fini (&fill_region);
405 solid = pixman_image_create_solid_fill (color);
409 for (i = 0; i < n_rects; ++i)
411 const pixman_rectangle16_t *rect = &(rects[i]);
413 pixman_image_composite (op, solid, NULL, dest,
416 rect->width, rect->height);
419 pixman_image_unref (solid);
427 * Returns the version of the pixman library encoded in a single
428 * integer as per %PIXMAN_VERSION_ENCODE. The encoding ensures that
429 * later versions compare greater than earlier versions.
431 * A run-time comparison to check that pixman's version is greater than
432 * or equal to version X.Y.Z could be performed as follows:
434 * <informalexample><programlisting>
435 * if (pixman_version() >= PIXMAN_VERSION_ENCODE(X,Y,Z)) {...}
436 * </programlisting></informalexample>
438 * See also pixman_version_string() as well as the compile-time
439 * equivalents %PIXMAN_VERSION and %PIXMAN_VERSION_STRING.
441 * Return value: the encoded version.
444 pixman_version (void)
446 return PIXMAN_VERSION;
450 * pixman_version_string:
452 * Returns the version of the pixman library as a human-readable string
453 * of the form "X.Y.Z".
455 * See also pixman_version() as well as the compile-time equivalents
456 * %PIXMAN_VERSION_STRING and %PIXMAN_VERSION.
458 * Return value: a string containing the version.
460 PIXMAN_EXPORT const char*
461 pixman_version_string (void)
463 return PIXMAN_VERSION_STRING;
467 * pixman_format_supported_source:
468 * @format: A pixman_format_code_t format
470 * Return value: whether the provided format code is a supported
471 * format for a pixman surface used as a source in
474 * Currently, all pixman_format_code_t values are supported.
476 PIXMAN_EXPORT pixman_bool_t
477 pixman_format_supported_source (pixman_format_code_t format)
482 case PIXMAN_a2b10g10r10:
483 case PIXMAN_x2b10g10r10:
484 case PIXMAN_a2r10g10b10:
485 case PIXMAN_x2r10g10b10:
486 case PIXMAN_a8r8g8b8:
487 case PIXMAN_x8r8g8b8:
488 case PIXMAN_a8b8g8r8:
489 case PIXMAN_x8b8g8r8:
490 case PIXMAN_b8g8r8a8:
491 case PIXMAN_b8g8r8x8:
497 case PIXMAN_a1r5g5b5:
498 case PIXMAN_x1r5g5b5:
499 case PIXMAN_a1b5g5r5:
500 case PIXMAN_x1b5g5r5:
501 case PIXMAN_a4r4g4b4:
502 case PIXMAN_x4r4g4b4:
503 case PIXMAN_a4b4g4r4:
504 case PIXMAN_x4b4g4r4:
509 case PIXMAN_a2r2g2b2:
510 case PIXMAN_a2b2g2r2:
514 /* Collides with PIXMAN_c8
517 /* Collides with PIXMAN_g8
524 case PIXMAN_a1r1g1b1:
525 case PIXMAN_a1b1g1r1:
542 * pixman_format_supported_destination:
543 * @format: A pixman_format_code_t format
545 * Return value: whether the provided format code is a supported
546 * format for a pixman surface used as a destination in
549 * Currently, all pixman_format_code_t values are supported
550 * except for the YUV formats.
552 PIXMAN_EXPORT pixman_bool_t
553 pixman_format_supported_destination (pixman_format_code_t format)
555 /* YUV formats cannot be written to at the moment */
556 if (format == PIXMAN_yuy2 || format == PIXMAN_yv12)
559 return pixman_format_supported_source (format);