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"
34 static const pixman_color_t transparent_black = { 0, 0, 0, 0 };
37 gradient_property_changed (pixman_image_t *image)
39 gradient_t *gradient = &image->gradient;
40 int n = gradient->n_stops;
41 pixman_gradient_stop_t *stops = gradient->stops;
42 pixman_gradient_stop_t *begin = &(gradient->stops[-1]);
43 pixman_gradient_stop_t *end = &(gradient->stops[n]);
45 switch (gradient->common.repeat)
48 case PIXMAN_REPEAT_NONE:
50 begin->color = transparent_black;
52 end->color = transparent_black;
55 case PIXMAN_REPEAT_NORMAL:
56 begin->x = stops[n - 1].x - pixman_fixed_1;
57 begin->color = stops[n - 1].color;
58 end->x = stops[0].x + pixman_fixed_1;
59 end->color = stops[0].color;
62 case PIXMAN_REPEAT_REFLECT:
63 begin->x = - stops[0].x;
64 begin->color = stops[0].color;
65 end->x = pixman_int_to_fixed (2) - stops[n - 1].x;
66 end->color = stops[n - 1].color;
69 case PIXMAN_REPEAT_PAD:
71 begin->color = stops[0].color;
73 end->color = stops[n - 1].color;
79 _pixman_init_gradient (gradient_t * gradient,
80 const pixman_gradient_stop_t *stops,
83 return_val_if_fail (n_stops > 0, FALSE);
85 /* We allocate two extra stops, one before the beginning of the stop list,
86 * and one after the end. These stops are initialized to whatever color
87 * would be used for positions outside the range of the stop list.
89 * This saves a bit of computation in the gradient walker.
91 * The pointer we store in the gradient_t struct still points to the
92 * first user-supplied struct, so when freeing, we will have to
96 pixman_malloc_ab (n_stops + 2, sizeof (pixman_gradient_stop_t));
100 gradient->stops += 1;
101 memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
102 gradient->n_stops = n_stops;
104 gradient->common.property_changed = gradient_property_changed;
110 _pixman_image_init (pixman_image_t *image)
112 image_common_t *common = &image->common;
114 pixman_region32_init (&common->clip_region);
116 common->alpha_count = 0;
117 common->have_clip_region = FALSE;
118 common->clip_sources = FALSE;
119 common->transform = NULL;
120 common->repeat = PIXMAN_REPEAT_NONE;
121 common->filter = PIXMAN_FILTER_NEAREST;
122 common->filter_params = NULL;
123 common->n_filter_params = 0;
124 common->alpha_map = NULL;
125 common->component_alpha = FALSE;
126 common->ref_count = 1;
127 common->property_changed = NULL;
128 common->client_clip = FALSE;
129 common->destroy_func = NULL;
130 common->destroy_data = NULL;
131 common->dirty = TRUE;
135 _pixman_image_fini (pixman_image_t *image)
137 image_common_t *common = (image_common_t *)image;
141 if (common->ref_count == 0)
143 if (image->common.destroy_func)
144 image->common.destroy_func (image, image->common.destroy_data);
146 pixman_region32_fini (&common->clip_region);
148 if (common->transform)
149 free (common->transform);
151 if (common->filter_params)
152 free (common->filter_params);
154 if (common->alpha_map)
155 pixman_image_unref ((pixman_image_t *)common->alpha_map);
157 if (image->type == LINEAR ||
158 image->type == RADIAL ||
159 image->type == CONICAL)
161 if (image->gradient.stops)
163 /* See _pixman_init_gradient() for an explanation of the - 1 */
164 free (image->gradient.stops - 1);
167 /* This will trigger if someone adds a property_changed
168 * method to the linear/radial/conical gradient overwriting
172 image->common.property_changed == gradient_property_changed);
175 if (image->type == BITS && image->bits.free_me)
176 free (image->bits.free_me);
185 _pixman_image_allocate (void)
187 pixman_image_t *image = malloc (sizeof (pixman_image_t));
190 _pixman_image_init (image);
196 image_property_changed (pixman_image_t *image)
198 image->common.dirty = TRUE;
202 PIXMAN_EXPORT pixman_image_t *
203 pixman_image_ref (pixman_image_t *image)
205 image->common.ref_count++;
210 /* returns TRUE when the image is freed */
211 PIXMAN_EXPORT pixman_bool_t
212 pixman_image_unref (pixman_image_t *image)
214 if (_pixman_image_fini (image))
224 pixman_image_set_destroy_function (pixman_image_t * image,
225 pixman_image_destroy_func_t func,
228 image->common.destroy_func = func;
229 image->common.destroy_data = data;
233 pixman_image_get_destroy_data (pixman_image_t *image)
235 return image->common.destroy_data;
239 _pixman_image_reset_clip_region (pixman_image_t *image)
241 image->common.have_clip_region = FALSE;
244 /* Executive Summary: This function is a no-op that only exists
245 * for historical reasons.
247 * There used to be a bug in the X server where it would rely on
248 * out-of-bounds accesses when it was asked to composite with a
249 * window as the source. It would create a pixman image pointing
250 * to some bogus position in memory, but then set a clip region
251 * to the position where the actual bits were.
253 * Due to a bug in old versions of pixman, where it would not clip
254 * against the image bounds when a clip region was set, this would
255 * actually work. So when the pixman bug was fixed, a workaround was
256 * added to allow certain out-of-bound accesses. This function disabled
259 * Since 0.21.2, pixman doesn't do these workarounds anymore, so now
260 * this function is a no-op.
263 pixman_disable_out_of_bounds_workaround (void)
268 compute_image_info (pixman_image_t *image)
270 pixman_format_code_t code;
274 if (!image->common.transform)
276 flags |= (FAST_PATH_ID_TRANSFORM |
277 FAST_PATH_X_UNIT_POSITIVE |
278 FAST_PATH_Y_UNIT_ZERO |
279 FAST_PATH_AFFINE_TRANSFORM);
283 flags |= FAST_PATH_HAS_TRANSFORM;
285 if (image->common.transform->matrix[2][0] == 0 &&
286 image->common.transform->matrix[2][1] == 0 &&
287 image->common.transform->matrix[2][2] == pixman_fixed_1)
289 flags |= FAST_PATH_AFFINE_TRANSFORM;
291 if (image->common.transform->matrix[0][1] == 0 &&
292 image->common.transform->matrix[1][0] == 0)
294 if (image->common.transform->matrix[0][0] == -pixman_fixed_1 &&
295 image->common.transform->matrix[1][1] == -pixman_fixed_1)
297 flags |= FAST_PATH_ROTATE_180_TRANSFORM;
299 flags |= FAST_PATH_SCALE_TRANSFORM;
301 else if (image->common.transform->matrix[0][0] == 0 &&
302 image->common.transform->matrix[1][1] == 0)
304 pixman_fixed_t m01 = image->common.transform->matrix[0][1];
305 if (m01 == -image->common.transform->matrix[1][0])
307 if (m01 == -pixman_fixed_1)
308 flags |= FAST_PATH_ROTATE_90_TRANSFORM;
309 else if (m01 == pixman_fixed_1)
310 flags |= FAST_PATH_ROTATE_270_TRANSFORM;
315 if (image->common.transform->matrix[0][0] > 0)
316 flags |= FAST_PATH_X_UNIT_POSITIVE;
318 if (image->common.transform->matrix[1][0] == 0)
319 flags |= FAST_PATH_Y_UNIT_ZERO;
323 switch (image->common.filter)
325 case PIXMAN_FILTER_NEAREST:
326 case PIXMAN_FILTER_FAST:
327 flags |= (FAST_PATH_NEAREST_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
330 case PIXMAN_FILTER_BILINEAR:
331 case PIXMAN_FILTER_GOOD:
332 case PIXMAN_FILTER_BEST:
333 flags |= (FAST_PATH_BILINEAR_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
335 /* Here we have a chance to optimize BILINEAR filter to NEAREST if
336 * they are equivalent for the currently used transformation matrix.
338 if (flags & FAST_PATH_ID_TRANSFORM)
340 flags |= FAST_PATH_NEAREST_FILTER;
343 /* affine and integer translation components in matrix ... */
344 ((flags & FAST_PATH_AFFINE_TRANSFORM) &&
345 !pixman_fixed_frac (image->common.transform->matrix[0][2] |
346 image->common.transform->matrix[1][2])) &&
348 /* ... combined with a simple rotation */
349 (flags & (FAST_PATH_ROTATE_90_TRANSFORM |
350 FAST_PATH_ROTATE_180_TRANSFORM |
351 FAST_PATH_ROTATE_270_TRANSFORM)) ||
352 /* ... or combined with a simple non-rotated translation */
353 (image->common.transform->matrix[0][0] == pixman_fixed_1 &&
354 image->common.transform->matrix[1][1] == pixman_fixed_1 &&
355 image->common.transform->matrix[0][1] == 0 &&
356 image->common.transform->matrix[1][0] == 0)
360 /* FIXME: there are some affine-test failures, showing that
361 * handling of BILINEAR and NEAREST filter is not quite
362 * equivalent when getting close to 32K for the translation
363 * components of the matrix. That's likely some bug, but for
364 * now just skip BILINEAR->NEAREST optimization in this case.
366 pixman_fixed_t magic_limit = pixman_int_to_fixed (30000);
367 if (image->common.transform->matrix[0][2] <= magic_limit &&
368 image->common.transform->matrix[1][2] <= magic_limit &&
369 image->common.transform->matrix[0][2] >= -magic_limit &&
370 image->common.transform->matrix[1][2] >= -magic_limit)
372 flags |= FAST_PATH_NEAREST_FILTER;
377 case PIXMAN_FILTER_CONVOLUTION:
381 flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
386 switch (image->common.repeat)
388 case PIXMAN_REPEAT_NONE:
390 FAST_PATH_NO_REFLECT_REPEAT |
391 FAST_PATH_NO_PAD_REPEAT |
392 FAST_PATH_NO_NORMAL_REPEAT;
395 case PIXMAN_REPEAT_REFLECT:
397 FAST_PATH_NO_PAD_REPEAT |
398 FAST_PATH_NO_NONE_REPEAT |
399 FAST_PATH_NO_NORMAL_REPEAT;
402 case PIXMAN_REPEAT_PAD:
404 FAST_PATH_NO_REFLECT_REPEAT |
405 FAST_PATH_NO_NONE_REPEAT |
406 FAST_PATH_NO_NORMAL_REPEAT;
411 FAST_PATH_NO_REFLECT_REPEAT |
412 FAST_PATH_NO_PAD_REPEAT |
413 FAST_PATH_NO_NONE_REPEAT;
417 /* Component alpha */
418 if (image->common.component_alpha)
419 flags |= FAST_PATH_COMPONENT_ALPHA;
421 flags |= FAST_PATH_UNIFIED_ALPHA;
423 flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NARROW_FORMAT);
425 /* Type specific checks */
431 if (image->solid.color.alpha == 0xffff)
432 flags |= FAST_PATH_IS_OPAQUE;
436 if (image->bits.width == 1 &&
437 image->bits.height == 1 &&
438 image->common.repeat != PIXMAN_REPEAT_NONE)
444 code = image->bits.format;
445 flags |= FAST_PATH_BITS_IMAGE;
448 if (!PIXMAN_FORMAT_A (image->bits.format) &&
449 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY &&
450 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
452 flags |= FAST_PATH_SAMPLES_OPAQUE;
454 if (image->common.repeat != PIXMAN_REPEAT_NONE)
455 flags |= FAST_PATH_IS_OPAQUE;
458 if (image->bits.read_func || image->bits.write_func)
459 flags &= ~FAST_PATH_NO_ACCESSORS;
461 if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
462 flags &= ~FAST_PATH_NARROW_FORMAT;
466 code = PIXMAN_unknown;
469 * As explained in pixman-radial-gradient.c, every point of
470 * the plane has a valid associated radius (and thus will be
471 * colored) if and only if a is negative (i.e. one of the two
472 * circles contains the other one).
475 if (image->radial.a >= 0)
482 code = PIXMAN_unknown;
484 if (image->common.repeat != PIXMAN_REPEAT_NONE)
488 flags |= FAST_PATH_IS_OPAQUE;
489 for (i = 0; i < image->gradient.n_stops; ++i)
491 if (image->gradient.stops[i].color.alpha != 0xffff)
493 flags &= ~FAST_PATH_IS_OPAQUE;
501 code = PIXMAN_unknown;
506 if (!image->common.alpha_map)
508 flags |= FAST_PATH_NO_ALPHA_MAP;
512 if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
513 flags &= ~FAST_PATH_NARROW_FORMAT;
516 /* Both alpha maps and convolution filters can introduce
517 * non-opaqueness in otherwise opaque images. Also
518 * an image with component alpha turned on is only opaque
519 * if all channels are opaque, so we simply turn it off
520 * unconditionally for those images.
522 if (image->common.alpha_map ||
523 image->common.filter == PIXMAN_FILTER_CONVOLUTION ||
524 image->common.component_alpha)
526 flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
529 image->common.flags = flags;
530 image->common.extended_format_code = code;
534 _pixman_image_validate (pixman_image_t *image)
536 if (image->common.dirty)
538 compute_image_info (image);
540 /* It is important that property_changed is
541 * called *after* compute_image_info() because
542 * property_changed() can make use of the flags
543 * to set up accessors etc.
545 if (image->common.property_changed)
546 image->common.property_changed (image);
548 image->common.dirty = FALSE;
551 if (image->common.alpha_map)
552 _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
555 PIXMAN_EXPORT pixman_bool_t
556 pixman_image_set_clip_region32 (pixman_image_t * image,
557 pixman_region32_t *region)
559 image_common_t *common = (image_common_t *)image;
560 pixman_bool_t result;
564 if ((result = pixman_region32_copy (&common->clip_region, region)))
565 image->common.have_clip_region = TRUE;
569 _pixman_image_reset_clip_region (image);
574 image_property_changed (image);
579 PIXMAN_EXPORT pixman_bool_t
580 pixman_image_set_clip_region (pixman_image_t * image,
581 pixman_region16_t *region)
583 image_common_t *common = (image_common_t *)image;
584 pixman_bool_t result;
588 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
589 image->common.have_clip_region = TRUE;
593 _pixman_image_reset_clip_region (image);
598 image_property_changed (image);
604 pixman_image_set_has_client_clip (pixman_image_t *image,
605 pixman_bool_t client_clip)
607 image->common.client_clip = client_clip;
610 PIXMAN_EXPORT pixman_bool_t
611 pixman_image_set_transform (pixman_image_t * image,
612 const pixman_transform_t *transform)
614 static const pixman_transform_t id =
616 { { pixman_fixed_1, 0, 0 },
617 { 0, pixman_fixed_1, 0 },
618 { 0, 0, pixman_fixed_1 } }
621 image_common_t *common = (image_common_t *)image;
622 pixman_bool_t result;
624 if (common->transform == transform)
627 if (!transform || memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
629 free (common->transform);
630 common->transform = NULL;
636 if (common->transform &&
637 memcmp (common->transform, transform, sizeof (pixman_transform_t) == 0))
642 if (common->transform == NULL)
643 common->transform = malloc (sizeof (pixman_transform_t));
645 if (common->transform == NULL)
652 memcpy (common->transform, transform, sizeof(pixman_transform_t));
657 image_property_changed (image);
663 pixman_image_set_repeat (pixman_image_t *image,
664 pixman_repeat_t repeat)
666 if (image->common.repeat == repeat)
669 image->common.repeat = repeat;
671 image_property_changed (image);
674 PIXMAN_EXPORT pixman_bool_t
675 pixman_image_set_filter (pixman_image_t * image,
676 pixman_filter_t filter,
677 const pixman_fixed_t *params,
680 image_common_t *common = (image_common_t *)image;
681 pixman_fixed_t *new_params;
683 if (params == common->filter_params && filter == common->filter)
689 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
694 params, n_params * sizeof (pixman_fixed_t));
697 common->filter = filter;
699 if (common->filter_params)
700 free (common->filter_params);
702 common->filter_params = new_params;
703 common->n_filter_params = n_params;
705 image_property_changed (image);
710 pixman_image_set_source_clipping (pixman_image_t *image,
711 pixman_bool_t clip_sources)
713 if (image->common.clip_sources == clip_sources)
716 image->common.clip_sources = clip_sources;
718 image_property_changed (image);
721 /* Unlike all the other property setters, this function does not
722 * copy the content of indexed. Doing this copying is simply
723 * way, way too expensive.
726 pixman_image_set_indexed (pixman_image_t * image,
727 const pixman_indexed_t *indexed)
729 bits_image_t *bits = (bits_image_t *)image;
731 if (bits->indexed == indexed)
734 bits->indexed = indexed;
736 image_property_changed (image);
740 pixman_image_set_alpha_map (pixman_image_t *image,
741 pixman_image_t *alpha_map,
745 image_common_t *common = (image_common_t *)image;
747 return_if_fail (!alpha_map || alpha_map->type == BITS);
749 if (alpha_map && common->alpha_count > 0)
751 /* If this image is being used as an alpha map itself,
752 * then you can't give it an alpha map of its own.
757 if (alpha_map && alpha_map->common.alpha_map)
759 /* If the image has an alpha map of its own,
760 * then it can't be used as an alpha map itself
765 if (common->alpha_map != (bits_image_t *)alpha_map)
767 if (common->alpha_map)
769 common->alpha_map->common.alpha_count--;
771 pixman_image_unref ((pixman_image_t *)common->alpha_map);
776 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
778 common->alpha_map->common.alpha_count++;
782 common->alpha_map = NULL;
786 common->alpha_origin_x = x;
787 common->alpha_origin_y = y;
789 image_property_changed (image);
793 pixman_image_set_component_alpha (pixman_image_t *image,
794 pixman_bool_t component_alpha)
796 if (image->common.component_alpha == component_alpha)
799 image->common.component_alpha = component_alpha;
801 image_property_changed (image);
804 PIXMAN_EXPORT pixman_bool_t
805 pixman_image_get_component_alpha (pixman_image_t *image)
807 return image->common.component_alpha;
811 pixman_image_set_accessors (pixman_image_t * image,
812 pixman_read_memory_func_t read_func,
813 pixman_write_memory_func_t write_func)
815 return_if_fail (image != NULL);
817 if (image->type == BITS)
819 image->bits.read_func = read_func;
820 image->bits.write_func = write_func;
822 image_property_changed (image);
826 PIXMAN_EXPORT uint32_t *
827 pixman_image_get_data (pixman_image_t *image)
829 if (image->type == BITS)
830 return image->bits.bits;
836 pixman_image_get_width (pixman_image_t *image)
838 if (image->type == BITS)
839 return image->bits.width;
845 pixman_image_get_height (pixman_image_t *image)
847 if (image->type == BITS)
848 return image->bits.height;
854 pixman_image_get_stride (pixman_image_t *image)
856 if (image->type == BITS)
857 return image->bits.rowstride * (int) sizeof (uint32_t);
863 pixman_image_get_depth (pixman_image_t *image)
865 if (image->type == BITS)
866 return PIXMAN_FORMAT_DEPTH (image->bits.format);
871 PIXMAN_EXPORT pixman_format_code_t
872 pixman_image_get_format (pixman_image_t *image)
874 if (image->type == BITS)
875 return image->bits.format;
881 _pixman_image_get_solid (pixman_implementation_t *imp,
882 pixman_image_t * image,
883 pixman_format_code_t format)
888 _pixman_implementation_src_iter_init (
889 imp, &iter, image, 0, 0, 1, 1,
890 (uint8_t *)&result, ITER_NARROW);
892 result = *iter.get_scanline (&iter, NULL);
894 /* If necessary, convert RGB <--> BGR. */
895 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
897 result = (((result & 0xff000000) >> 0) |
898 ((result & 0x00ff0000) >> 16) |
899 ((result & 0x0000ff00) >> 0) |
900 ((result & 0x000000ff) << 16));