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,
172 int mask_dx, mask_dy;
174 int dest_dx, dest_dy;
176 _pixman_image_validate (src);
178 _pixman_image_validate (mask);
179 _pixman_image_validate (dest);
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.
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)
195 imp = _pixman_choose_implementation ();
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);
204 _pixman_implementation_composite (imp, op,
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);
219 PIXMAN_EXPORT pixman_bool_t
220 pixman_blt (uint32_t *src_bits,
234 imp = _pixman_choose_implementation ();
236 return _pixman_implementation_blt (imp, src_bits, dst_bits, src_stride, dst_stride,
243 PIXMAN_EXPORT pixman_bool_t
244 pixman_fill (uint32_t *bits,
254 imp = _pixman_choose_implementation ();
256 return _pixman_implementation_fill (imp, bits, stride, bpp, x, y, width, height, xor);
260 color_to_uint32 (const pixman_color_t *color)
263 (color->alpha >> 8 << 24) |
264 (color->red >> 8 << 16) |
265 (color->green & 0xff00) |
270 color_to_pixel (pixman_color_t * color,
272 pixman_format_code_t format)
274 uint32_t c = color_to_uint32 (color);
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))
289 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
291 c = ((c & 0xff000000) >> 0) |
292 ((c & 0x00ff0000) >> 16) |
293 ((c & 0x0000ff00) >> 0) |
294 ((c & 0x000000ff) << 16);
296 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
298 c = ((c & 0xff000000) >> 24) |
299 ((c & 0x00ff0000) >> 8) |
300 ((c & 0x0000ff00) << 8) |
301 ((c & 0x000000ff) << 24);
304 if (format == PIXMAN_a8)
306 else if (format == PIXMAN_r5g6b5 ||
307 format == PIXMAN_b5g6r5)
308 c = CONVERT_8888_TO_0565 (c);
311 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
312 printf ("pixel: %x\n", c);
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,
324 const pixman_rectangle16_t *rects)
326 pixman_image_t *solid;
330 _pixman_image_validate (dest);
332 if (color->alpha == 0xffff)
334 if (op == PIXMAN_OP_OVER)
338 if (op == PIXMAN_OP_CLEAR)
350 if (op == PIXMAN_OP_SRC)
354 if (color_to_pixel (color, &pixel, dest->bits.format))
356 for (i = 0; i < n_rects; ++i)
358 pixman_region32_t fill_region;
360 pixman_box32_t *boxes;
362 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
364 if (dest->common.have_clip_region)
366 if (!pixman_region32_intersect (&fill_region,
368 &dest->common.clip_region))
372 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
373 for (j = 0; j < n_boxes; ++j)
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,
381 pixman_region32_fini (&fill_region);
387 solid = pixman_image_create_solid_fill (color);
391 for (i = 0; i < n_rects; ++i)
393 const pixman_rectangle16_t *rect = &(rects[i]);
395 pixman_image_composite (op, solid, NULL, dest,
398 rect->width, rect->height);
401 pixman_image_unref (solid);
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.
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:
416 * <informalexample><programlisting>
417 * if (pixman_version() >= PIXMAN_VERSION_ENCODE(X,Y,Z)) {...}
418 * </programlisting></informalexample>
420 * See also pixman_version_string() as well as the compile-time
421 * equivalents %PIXMAN_VERSION and %PIXMAN_VERSION_STRING.
423 * Return value: the encoded version.
426 pixman_version (void)
428 return PIXMAN_VERSION;
432 * pixman_version_string:
434 * Returns the version of the pixman library as a human-readable string
435 * of the form "X.Y.Z".
437 * See also pixman_version() as well as the compile-time equivalents
438 * %PIXMAN_VERSION_STRING and %PIXMAN_VERSION.
440 * Return value: a string containing the version.
442 PIXMAN_EXPORT const char*
443 pixman_version_string (void)
445 return PIXMAN_VERSION_STRING;
449 * pixman_format_supported_source:
450 * @format: A pixman_format_code_t format
452 * Return value: whether the provided format code is a supported
453 * format for a pixman surface used as a source in
456 * Currently, all pixman_format_code_t values are supported.
458 PIXMAN_EXPORT pixman_bool_t
459 pixman_format_supported_source (pixman_format_code_t format)
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:
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:
491 case PIXMAN_a2r2g2b2:
492 case PIXMAN_a2b2g2r2:
496 /* Collides with PIXMAN_c8
499 /* Collides with PIXMAN_g8
506 case PIXMAN_a1r1g1b1:
507 case PIXMAN_a1b1g1r1:
524 * pixman_format_supported_destination:
525 * @format: A pixman_format_code_t format
527 * Return value: whether the provided format code is a supported
528 * format for a pixman surface used as a destination in
531 * Currently, all pixman_format_code_t values are supported
532 * except for the YUV formats.
534 PIXMAN_EXPORT pixman_bool_t
535 pixman_format_supported_destination (pixman_format_code_t format)
537 /* YUV formats cannot be written to at the moment */
538 if (format == PIXMAN_yuy2 || format == PIXMAN_yv12)
541 return pixman_format_supported_source (format);