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 },
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,
80 pixman_image_t *src_image,
81 pixman_image_t *mask_image,
82 pixman_image_t *dst_image)
84 pixman_bool_t is_source_opaque;
85 pixman_bool_t is_dest_opaque;
86 const optimized_operator_info_t *info = pixman_operator_can_be_optimized (op);
88 if (!info || mask_image)
91 is_source_opaque = _pixman_image_is_opaque (src_image);
92 is_dest_opaque = _pixman_image_is_opaque (dst_image);
94 if (is_source_opaque == FALSE && is_dest_opaque == FALSE)
97 if (is_source_opaque && is_dest_opaque)
98 return info->op_src_dst_opaque;
99 else if (is_source_opaque)
100 return info->op_src_opaque;
101 else if (is_dest_opaque)
102 return info->op_dst_opaque;
108 static pixman_implementation_t *imp;
111 pixman_image_composite (pixman_op_t op,
112 pixman_image_t * src,
113 pixman_image_t * mask,
114 pixman_image_t * dest,
125 * Check if we can replace our operator by a simpler one
126 * if the src or dest are opaque. The output operator should be
127 * mathematically equivalent to the source.
129 op = pixman_optimize_operator(op, src, mask, dest);
130 if (op == PIXMAN_OP_DST ||
131 op == PIXMAN_OP_CONJOINT_DST ||
132 op == PIXMAN_OP_DISJOINT_DST)
138 imp = _pixman_choose_implementation ();
140 _pixman_implementation_composite (imp, op,
148 PIXMAN_EXPORT pixman_bool_t
149 pixman_blt (uint32_t *src_bits,
163 imp = _pixman_choose_implementation ();
165 return _pixman_implementation_blt (imp, src_bits, dst_bits, src_stride, dst_stride,
172 PIXMAN_EXPORT pixman_bool_t
173 pixman_fill (uint32_t *bits,
183 imp = _pixman_choose_implementation ();
185 return _pixman_implementation_fill (imp, bits, stride, bpp, x, y, width, height, xor);
189 color_to_uint32 (const pixman_color_t *color)
192 (color->alpha >> 8 << 24) |
193 (color->red >> 8 << 16) |
194 (color->green & 0xff00) |
199 color_to_pixel (pixman_color_t * color,
201 pixman_format_code_t format)
203 uint32_t c = color_to_uint32 (color);
205 if (!(format == PIXMAN_a8r8g8b8 ||
206 format == PIXMAN_x8r8g8b8 ||
207 format == PIXMAN_a8b8g8r8 ||
208 format == PIXMAN_x8b8g8r8 ||
209 format == PIXMAN_b8g8r8a8 ||
210 format == PIXMAN_b8g8r8x8 ||
211 format == PIXMAN_r5g6b5 ||
212 format == PIXMAN_b5g6r5 ||
213 format == PIXMAN_a8))
218 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
220 c = ((c & 0xff000000) >> 0) |
221 ((c & 0x00ff0000) >> 16) |
222 ((c & 0x0000ff00) >> 0) |
223 ((c & 0x000000ff) << 16);
225 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
227 c = ((c & 0xff000000) >> 24) |
228 ((c & 0x00ff0000) >> 8) |
229 ((c & 0x0000ff00) << 8) |
230 ((c & 0x000000ff) << 24);
233 if (format == PIXMAN_a8)
235 else if (format == PIXMAN_r5g6b5 ||
236 format == PIXMAN_b5g6r5)
237 c = CONVERT_8888_TO_0565 (c);
240 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
241 printf ("pixel: %x\n", c);
248 PIXMAN_EXPORT pixman_bool_t
249 pixman_image_fill_rectangles (pixman_op_t op,
250 pixman_image_t * dest,
251 pixman_color_t * color,
253 const pixman_rectangle16_t *rects)
255 pixman_image_t *solid;
259 if (color->alpha == 0xffff)
261 if (op == PIXMAN_OP_OVER)
265 if (op == PIXMAN_OP_CLEAR)
277 if (op == PIXMAN_OP_SRC)
281 if (color_to_pixel (color, &pixel, dest->bits.format))
283 for (i = 0; i < n_rects; ++i)
285 pixman_region32_t fill_region;
287 pixman_box32_t *boxes;
289 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
291 if (dest->common.have_clip_region)
293 if (!pixman_region32_intersect (&fill_region,
295 &dest->common.clip_region))
299 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
300 for (j = 0; j < n_boxes; ++j)
302 const pixman_box32_t *box = &(boxes[j]);
303 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
304 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
308 pixman_region32_fini (&fill_region);
314 solid = pixman_image_create_solid_fill (color);
318 for (i = 0; i < n_rects; ++i)
320 const pixman_rectangle16_t *rect = &(rects[i]);
322 pixman_image_composite (op, solid, NULL, dest,
325 rect->width, rect->height);
328 pixman_image_unref (solid);
336 * Returns the version of the pixman library encoded in a single
337 * integer as per %PIXMAN_VERSION_ENCODE. The encoding ensures that
338 * later versions compare greater than earlier versions.
340 * A run-time comparison to check that pixman's version is greater than
341 * or equal to version X.Y.Z could be performed as follows:
343 * <informalexample><programlisting>
344 * if (pixman_version() >= PIXMAN_VERSION_ENCODE(X,Y,Z)) {...}
345 * </programlisting></informalexample>
347 * See also pixman_version_string() as well as the compile-time
348 * equivalents %PIXMAN_VERSION and %PIXMAN_VERSION_STRING.
350 * Return value: the encoded version.
353 pixman_version (void)
355 return PIXMAN_VERSION;
359 * pixman_version_string:
361 * Returns the version of the pixman library as a human-readable string
362 * of the form "X.Y.Z".
364 * See also pixman_version() as well as the compile-time equivalents
365 * %PIXMAN_VERSION_STRING and %PIXMAN_VERSION.
367 * Return value: a string containing the version.
369 PIXMAN_EXPORT const char*
370 pixman_version_string (void)
372 return PIXMAN_VERSION_STRING;
376 * pixman_format_supported_source:
377 * @format: A pixman_format_code_t format
379 * Return value: whether the provided format code is a supported
380 * format for a pixman surface used as a source in
383 * Currently, all pixman_format_code_t values are supported.
385 PIXMAN_EXPORT pixman_bool_t
386 pixman_format_supported_source (pixman_format_code_t format)
391 case PIXMAN_a2b10g10r10:
392 case PIXMAN_x2b10g10r10:
393 case PIXMAN_a2r10g10b10:
394 case PIXMAN_x2r10g10b10:
395 case PIXMAN_a8r8g8b8:
396 case PIXMAN_x8r8g8b8:
397 case PIXMAN_a8b8g8r8:
398 case PIXMAN_x8b8g8r8:
399 case PIXMAN_b8g8r8a8:
400 case PIXMAN_b8g8r8x8:
406 case PIXMAN_a1r5g5b5:
407 case PIXMAN_x1r5g5b5:
408 case PIXMAN_a1b5g5r5:
409 case PIXMAN_x1b5g5r5:
410 case PIXMAN_a4r4g4b4:
411 case PIXMAN_x4r4g4b4:
412 case PIXMAN_a4b4g4r4:
413 case PIXMAN_x4b4g4r4:
418 case PIXMAN_a2r2g2b2:
419 case PIXMAN_a2b2g2r2:
423 /* Collides with PIXMAN_c8
426 /* Collides with PIXMAN_g8
433 case PIXMAN_a1r1g1b1:
434 case PIXMAN_a1b1g1r1:
451 * pixman_format_supported_destination:
452 * @format: A pixman_format_code_t format
454 * Return value: whether the provided format code is a supported
455 * format for a pixman surface used as a destination in
458 * Currently, all pixman_format_code_t values are supported
459 * except for the YUV formats.
461 PIXMAN_EXPORT pixman_bool_t
462 pixman_format_supported_destination (pixman_format_code_t format)
464 /* YUV formats cannot be written to at the moment */
465 if (format == PIXMAN_yuy2 || format == PIXMAN_yv12)
468 return pixman_format_supported_source (format);