2 * Copyright © 2000 SuSE, Inc.
3 * Copyright © 2007 Red Hat, Inc.
5 * Permission to use, copy, modify, distribute, and sell this software and its
6 * documentation for any purpose is hereby granted without fee, provided that
7 * the above copyright notice appear in all copies and that both that
8 * copyright notice and this permission notice appear in supporting
9 * documentation, and that the name of SuSE not be used in advertising or
10 * publicity pertaining to distribution of the software without specific,
11 * written prior permission. SuSE makes no representations about the
12 * suitability of this software for any purpose. It is provided "as is"
13 * without express or implied warranty.
15 * SuSE DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL SuSE
17 * BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
18 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
19 * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 #include "pixman-private.h"
35 _pixman_init_gradient (gradient_t * gradient,
36 const pixman_gradient_stop_t *stops,
39 return_val_if_fail (n_stops > 0, FALSE);
41 gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
45 memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
47 gradient->n_stops = n_stops;
53 _pixman_image_allocate (void)
55 pixman_image_t *image = malloc (sizeof (pixman_image_t));
59 image_common_t *common = &image->common;
61 pixman_region32_init (&common->clip_region);
63 common->alpha_count = 0;
64 common->have_clip_region = FALSE;
65 common->clip_sources = FALSE;
66 common->transform = NULL;
67 common->repeat = PIXMAN_REPEAT_NONE;
68 common->filter = PIXMAN_FILTER_NEAREST;
69 common->filter_params = NULL;
70 common->n_filter_params = 0;
71 common->alpha_map = NULL;
72 common->component_alpha = FALSE;
73 common->ref_count = 1;
74 common->property_changed = NULL;
75 common->client_clip = FALSE;
76 common->destroy_func = NULL;
77 common->destroy_data = NULL;
85 image_property_changed (pixman_image_t *image)
87 image->common.dirty = TRUE;
91 PIXMAN_EXPORT pixman_image_t *
92 pixman_image_ref (pixman_image_t *image)
94 image->common.ref_count++;
99 /* returns TRUE when the image is freed */
100 PIXMAN_EXPORT pixman_bool_t
101 pixman_image_unref (pixman_image_t *image)
103 image_common_t *common = (image_common_t *)image;
107 if (common->ref_count == 0)
109 if (image->common.destroy_func)
110 image->common.destroy_func (image, image->common.destroy_data);
112 pixman_region32_fini (&common->clip_region);
114 if (common->transform)
115 free (common->transform);
117 if (common->filter_params)
118 free (common->filter_params);
120 if (common->alpha_map)
121 pixman_image_unref ((pixman_image_t *)common->alpha_map);
123 if (image->type == LINEAR ||
124 image->type == RADIAL ||
125 image->type == CONICAL)
127 if (image->gradient.stops)
128 free (image->gradient.stops);
131 if (image->type == BITS && image->bits.free_me)
132 free (image->bits.free_me);
143 pixman_image_set_destroy_function (pixman_image_t * image,
144 pixman_image_destroy_func_t func,
147 image->common.destroy_func = func;
148 image->common.destroy_data = data;
152 pixman_image_get_destroy_data (pixman_image_t *image)
154 return image->common.destroy_data;
158 _pixman_image_reset_clip_region (pixman_image_t *image)
160 image->common.have_clip_region = FALSE;
163 /* Executive Summary: This function is a no-op that only exists
164 * for historical reasons.
166 * There used to be a bug in the X server where it would rely on
167 * out-of-bounds accesses when it was asked to composite with a
168 * window as the source. It would create a pixman image pointing
169 * to some bogus position in memory, but then set a clip region
170 * to the position where the actual bits were.
172 * Due to a bug in old versions of pixman, where it would not clip
173 * against the image bounds when a clip region was set, this would
174 * actually work. So when the pixman bug was fixed, a workaround was
175 * added to allow certain out-of-bound accesses. This function disabled
178 * Since 0.21.2, pixman doesn't do these workarounds anymore, so now
179 * this function is a no-op.
182 pixman_disable_out_of_bounds_workaround (void)
187 compute_image_info (pixman_image_t *image)
189 pixman_format_code_t code;
193 if (!image->common.transform)
195 flags |= (FAST_PATH_ID_TRANSFORM |
196 FAST_PATH_X_UNIT_POSITIVE |
197 FAST_PATH_Y_UNIT_ZERO |
198 FAST_PATH_AFFINE_TRANSFORM);
202 flags |= FAST_PATH_HAS_TRANSFORM;
204 if (image->common.transform->matrix[2][0] == 0 &&
205 image->common.transform->matrix[2][1] == 0 &&
206 image->common.transform->matrix[2][2] == pixman_fixed_1)
208 flags |= FAST_PATH_AFFINE_TRANSFORM;
210 if (image->common.transform->matrix[0][1] == 0 &&
211 image->common.transform->matrix[1][0] == 0)
213 if (image->common.transform->matrix[0][0] == -pixman_fixed_1 &&
214 image->common.transform->matrix[1][1] == -pixman_fixed_1)
216 flags |= FAST_PATH_ROTATE_180_TRANSFORM;
218 flags |= FAST_PATH_SCALE_TRANSFORM;
220 else if (image->common.transform->matrix[0][0] == 0 &&
221 image->common.transform->matrix[1][1] == 0)
223 pixman_fixed_t m01 = image->common.transform->matrix[0][1];
224 if (m01 == -image->common.transform->matrix[1][0])
226 if (m01 == -pixman_fixed_1)
227 flags |= FAST_PATH_ROTATE_90_TRANSFORM;
228 else if (m01 == pixman_fixed_1)
229 flags |= FAST_PATH_ROTATE_270_TRANSFORM;
234 if (image->common.transform->matrix[0][0] > 0)
235 flags |= FAST_PATH_X_UNIT_POSITIVE;
237 if (image->common.transform->matrix[1][0] == 0)
238 flags |= FAST_PATH_Y_UNIT_ZERO;
242 switch (image->common.filter)
244 case PIXMAN_FILTER_NEAREST:
245 case PIXMAN_FILTER_FAST:
246 flags |= (FAST_PATH_NEAREST_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
249 case PIXMAN_FILTER_BILINEAR:
250 case PIXMAN_FILTER_GOOD:
251 case PIXMAN_FILTER_BEST:
252 flags |= (FAST_PATH_BILINEAR_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
254 /* Here we have a chance to optimize BILINEAR filter to NEAREST if
255 * they are equivalent for the currently used transformation matrix.
257 if (flags & FAST_PATH_ID_TRANSFORM)
259 flags |= FAST_PATH_NEAREST_FILTER;
262 /* affine and integer translation components in matrix ... */
263 ((flags & FAST_PATH_AFFINE_TRANSFORM) &&
264 !pixman_fixed_frac (image->common.transform->matrix[0][2] |
265 image->common.transform->matrix[1][2])) &&
267 /* ... combined with a simple rotation */
268 (flags & (FAST_PATH_ROTATE_90_TRANSFORM |
269 FAST_PATH_ROTATE_180_TRANSFORM |
270 FAST_PATH_ROTATE_270_TRANSFORM)) ||
271 /* ... or combined with a simple non-rotated translation */
272 (image->common.transform->matrix[0][0] == pixman_fixed_1 &&
273 image->common.transform->matrix[1][1] == pixman_fixed_1 &&
274 image->common.transform->matrix[0][1] == 0 &&
275 image->common.transform->matrix[1][0] == 0)
279 /* FIXME: there are some affine-test failures, showing that
280 * handling of BILINEAR and NEAREST filter is not quite
281 * equivalent when getting close to 32K for the translation
282 * components of the matrix. That's likely some bug, but for
283 * now just skip BILINEAR->NEAREST optimization in this case.
285 pixman_fixed_t magic_limit = pixman_int_to_fixed (30000);
286 if (image->common.transform->matrix[0][2] <= magic_limit &&
287 image->common.transform->matrix[1][2] <= magic_limit &&
288 image->common.transform->matrix[0][2] >= -magic_limit &&
289 image->common.transform->matrix[1][2] >= -magic_limit)
291 flags |= FAST_PATH_NEAREST_FILTER;
296 case PIXMAN_FILTER_CONVOLUTION:
300 flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
305 switch (image->common.repeat)
307 case PIXMAN_REPEAT_NONE:
309 FAST_PATH_NO_REFLECT_REPEAT |
310 FAST_PATH_NO_PAD_REPEAT |
311 FAST_PATH_NO_NORMAL_REPEAT;
314 case PIXMAN_REPEAT_REFLECT:
316 FAST_PATH_NO_PAD_REPEAT |
317 FAST_PATH_NO_NONE_REPEAT |
318 FAST_PATH_NO_NORMAL_REPEAT;
321 case PIXMAN_REPEAT_PAD:
323 FAST_PATH_NO_REFLECT_REPEAT |
324 FAST_PATH_NO_NONE_REPEAT |
325 FAST_PATH_NO_NORMAL_REPEAT;
330 FAST_PATH_NO_REFLECT_REPEAT |
331 FAST_PATH_NO_PAD_REPEAT |
332 FAST_PATH_NO_NONE_REPEAT;
336 /* Component alpha */
337 if (image->common.component_alpha)
338 flags |= FAST_PATH_COMPONENT_ALPHA;
340 flags |= FAST_PATH_UNIFIED_ALPHA;
342 flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NARROW_FORMAT);
344 /* Type specific checks */
350 if (image->solid.color.alpha == 0xffff)
351 flags |= FAST_PATH_IS_OPAQUE;
355 if (image->bits.width == 1 &&
356 image->bits.height == 1 &&
357 image->common.repeat != PIXMAN_REPEAT_NONE)
363 code = image->bits.format;
366 if (!PIXMAN_FORMAT_A (image->bits.format) &&
367 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY &&
368 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
370 flags |= FAST_PATH_SAMPLES_OPAQUE;
372 if (image->common.repeat != PIXMAN_REPEAT_NONE)
373 flags |= FAST_PATH_IS_OPAQUE;
376 if (image->bits.read_func || image->bits.write_func)
377 flags &= ~FAST_PATH_NO_ACCESSORS;
379 if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
380 flags &= ~FAST_PATH_NARROW_FORMAT;
384 code = PIXMAN_unknown;
387 * As explained in pixman-radial-gradient.c, every point of
388 * the plane has a valid associated radius (and thus will be
389 * colored) if and only if a is negative (i.e. one of the two
390 * circles contains the other one).
393 if (image->radial.a >= 0)
400 code = PIXMAN_unknown;
402 if (image->common.repeat != PIXMAN_REPEAT_NONE)
406 flags |= FAST_PATH_IS_OPAQUE;
407 for (i = 0; i < image->gradient.n_stops; ++i)
409 if (image->gradient.stops[i].color.alpha != 0xffff)
411 flags &= ~FAST_PATH_IS_OPAQUE;
419 code = PIXMAN_unknown;
424 if (!image->common.alpha_map)
426 flags |= FAST_PATH_NO_ALPHA_MAP;
430 if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
431 flags &= ~FAST_PATH_NARROW_FORMAT;
434 /* Both alpha maps and convolution filters can introduce
435 * non-opaqueness in otherwise opaque images. Also
436 * an image with component alpha turned on is only opaque
437 * if all channels are opaque, so we simply turn it off
438 * unconditionally for those images.
440 if (image->common.alpha_map ||
441 image->common.filter == PIXMAN_FILTER_CONVOLUTION ||
442 image->common.component_alpha)
444 flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
447 image->common.flags = flags;
448 image->common.extended_format_code = code;
452 _pixman_image_validate (pixman_image_t *image)
454 if (image->common.dirty)
456 compute_image_info (image);
458 /* It is important that property_changed is
459 * called *after* compute_image_info() because
460 * property_changed() can make use of the flags
461 * to set up accessors etc.
463 if (image->common.property_changed)
464 image->common.property_changed (image);
466 image->common.dirty = FALSE;
469 if (image->common.alpha_map)
470 _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
473 PIXMAN_EXPORT pixman_bool_t
474 pixman_image_set_clip_region32 (pixman_image_t * image,
475 pixman_region32_t *region)
477 image_common_t *common = (image_common_t *)image;
478 pixman_bool_t result;
482 if ((result = pixman_region32_copy (&common->clip_region, region)))
483 image->common.have_clip_region = TRUE;
487 _pixman_image_reset_clip_region (image);
492 image_property_changed (image);
497 PIXMAN_EXPORT pixman_bool_t
498 pixman_image_set_clip_region (pixman_image_t * image,
499 pixman_region16_t *region)
501 image_common_t *common = (image_common_t *)image;
502 pixman_bool_t result;
506 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
507 image->common.have_clip_region = TRUE;
511 _pixman_image_reset_clip_region (image);
516 image_property_changed (image);
522 pixman_image_set_has_client_clip (pixman_image_t *image,
523 pixman_bool_t client_clip)
525 image->common.client_clip = client_clip;
528 PIXMAN_EXPORT pixman_bool_t
529 pixman_image_set_transform (pixman_image_t * image,
530 const pixman_transform_t *transform)
532 static const pixman_transform_t id =
534 { { pixman_fixed_1, 0, 0 },
535 { 0, pixman_fixed_1, 0 },
536 { 0, 0, pixman_fixed_1 } }
539 image_common_t *common = (image_common_t *)image;
540 pixman_bool_t result;
542 if (common->transform == transform)
545 if (!transform || memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
547 free (common->transform);
548 common->transform = NULL;
554 if (common->transform &&
555 memcmp (common->transform, transform, sizeof (pixman_transform_t) == 0))
560 if (common->transform == NULL)
561 common->transform = malloc (sizeof (pixman_transform_t));
563 if (common->transform == NULL)
570 memcpy (common->transform, transform, sizeof(pixman_transform_t));
575 image_property_changed (image);
581 pixman_image_set_repeat (pixman_image_t *image,
582 pixman_repeat_t repeat)
584 if (image->common.repeat == repeat)
587 image->common.repeat = repeat;
589 image_property_changed (image);
592 PIXMAN_EXPORT pixman_bool_t
593 pixman_image_set_filter (pixman_image_t * image,
594 pixman_filter_t filter,
595 const pixman_fixed_t *params,
598 image_common_t *common = (image_common_t *)image;
599 pixman_fixed_t *new_params;
601 if (params == common->filter_params && filter == common->filter)
607 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
612 params, n_params * sizeof (pixman_fixed_t));
615 common->filter = filter;
617 if (common->filter_params)
618 free (common->filter_params);
620 common->filter_params = new_params;
621 common->n_filter_params = n_params;
623 image_property_changed (image);
628 pixman_image_set_source_clipping (pixman_image_t *image,
629 pixman_bool_t clip_sources)
631 if (image->common.clip_sources == clip_sources)
634 image->common.clip_sources = clip_sources;
636 image_property_changed (image);
639 /* Unlike all the other property setters, this function does not
640 * copy the content of indexed. Doing this copying is simply
641 * way, way too expensive.
644 pixman_image_set_indexed (pixman_image_t * image,
645 const pixman_indexed_t *indexed)
647 bits_image_t *bits = (bits_image_t *)image;
649 if (bits->indexed == indexed)
652 bits->indexed = indexed;
654 image_property_changed (image);
658 pixman_image_set_alpha_map (pixman_image_t *image,
659 pixman_image_t *alpha_map,
663 image_common_t *common = (image_common_t *)image;
665 return_if_fail (!alpha_map || alpha_map->type == BITS);
667 if (alpha_map && common->alpha_count > 0)
669 /* If this image is being used as an alpha map itself,
670 * then you can't give it an alpha map of its own.
675 if (alpha_map && alpha_map->common.alpha_map)
677 /* If the image has an alpha map of its own,
678 * then it can't be used as an alpha map itself
683 if (common->alpha_map != (bits_image_t *)alpha_map)
685 if (common->alpha_map)
687 common->alpha_map->common.alpha_count--;
689 pixman_image_unref ((pixman_image_t *)common->alpha_map);
694 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
696 common->alpha_map->common.alpha_count++;
700 common->alpha_map = NULL;
704 common->alpha_origin_x = x;
705 common->alpha_origin_y = y;
707 image_property_changed (image);
711 pixman_image_set_component_alpha (pixman_image_t *image,
712 pixman_bool_t component_alpha)
714 if (image->common.component_alpha == component_alpha)
717 image->common.component_alpha = component_alpha;
719 image_property_changed (image);
722 PIXMAN_EXPORT pixman_bool_t
723 pixman_image_get_component_alpha (pixman_image_t *image)
725 return image->common.component_alpha;
729 pixman_image_set_accessors (pixman_image_t * image,
730 pixman_read_memory_func_t read_func,
731 pixman_write_memory_func_t write_func)
733 return_if_fail (image != NULL);
735 if (image->type == BITS)
737 image->bits.read_func = read_func;
738 image->bits.write_func = write_func;
740 image_property_changed (image);
744 PIXMAN_EXPORT uint32_t *
745 pixman_image_get_data (pixman_image_t *image)
747 if (image->type == BITS)
748 return image->bits.bits;
754 pixman_image_get_width (pixman_image_t *image)
756 if (image->type == BITS)
757 return image->bits.width;
763 pixman_image_get_height (pixman_image_t *image)
765 if (image->type == BITS)
766 return image->bits.height;
772 pixman_image_get_stride (pixman_image_t *image)
774 if (image->type == BITS)
775 return image->bits.rowstride * (int) sizeof (uint32_t);
781 pixman_image_get_depth (pixman_image_t *image)
783 if (image->type == BITS)
784 return PIXMAN_FORMAT_DEPTH (image->bits.format);
789 PIXMAN_EXPORT pixman_format_code_t
790 pixman_image_get_format (pixman_image_t *image)
792 if (image->type == BITS)
793 return image->bits.format;
799 _pixman_image_get_solid (pixman_implementation_t *imp,
800 pixman_image_t * image,
801 pixman_format_code_t format)
806 _pixman_implementation_src_iter_init (
807 imp, &iter, image, 0, 0, 1, 1,
808 (uint8_t *)&result, ITER_NARROW);
810 result = *iter.get_scanline (&iter, NULL);
812 /* If necessary, convert RGB <--> BGR. */
813 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
815 result = (((result & 0xff000000) >> 0) |
816 ((result & 0x00ff0000) >> 16) |
817 ((result & 0x0000ff00) >> 0) |
818 ((result & 0x000000ff) << 16));