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 free (common->transform);
149 free (common->filter_params);
151 if (common->alpha_map)
152 pixman_image_unref ((pixman_image_t *)common->alpha_map);
154 if (image->type == LINEAR ||
155 image->type == RADIAL ||
156 image->type == CONICAL)
158 if (image->gradient.stops)
160 /* See _pixman_init_gradient() for an explanation of the - 1 */
161 free (image->gradient.stops - 1);
164 /* This will trigger if someone adds a property_changed
165 * method to the linear/radial/conical gradient overwriting
169 image->common.property_changed == gradient_property_changed);
172 if (image->type == BITS && image->bits.free_me)
173 free (image->bits.free_me);
182 _pixman_image_allocate (void)
184 pixman_image_t *image = malloc (sizeof (pixman_image_t));
187 _pixman_image_init (image);
193 image_property_changed (pixman_image_t *image)
195 image->common.dirty = TRUE;
199 PIXMAN_EXPORT pixman_image_t *
200 pixman_image_ref (pixman_image_t *image)
202 image->common.ref_count++;
207 /* returns TRUE when the image is freed */
208 PIXMAN_EXPORT pixman_bool_t
209 pixman_image_unref (pixman_image_t *image)
211 if (_pixman_image_fini (image))
221 pixman_image_set_destroy_function (pixman_image_t * image,
222 pixman_image_destroy_func_t func,
225 image->common.destroy_func = func;
226 image->common.destroy_data = data;
230 pixman_image_get_destroy_data (pixman_image_t *image)
232 return image->common.destroy_data;
236 _pixman_image_reset_clip_region (pixman_image_t *image)
238 image->common.have_clip_region = FALSE;
241 /* Executive Summary: This function is a no-op that only exists
242 * for historical reasons.
244 * There used to be a bug in the X server where it would rely on
245 * out-of-bounds accesses when it was asked to composite with a
246 * window as the source. It would create a pixman image pointing
247 * to some bogus position in memory, but then set a clip region
248 * to the position where the actual bits were.
250 * Due to a bug in old versions of pixman, where it would not clip
251 * against the image bounds when a clip region was set, this would
252 * actually work. So when the pixman bug was fixed, a workaround was
253 * added to allow certain out-of-bound accesses. This function disabled
256 * Since 0.21.2, pixman doesn't do these workarounds anymore, so now
257 * this function is a no-op.
260 pixman_disable_out_of_bounds_workaround (void)
265 compute_image_info (pixman_image_t *image)
267 pixman_format_code_t code;
271 if (!image->common.transform)
273 flags |= (FAST_PATH_ID_TRANSFORM |
274 FAST_PATH_X_UNIT_POSITIVE |
275 FAST_PATH_Y_UNIT_ZERO |
276 FAST_PATH_AFFINE_TRANSFORM);
280 flags |= FAST_PATH_HAS_TRANSFORM;
282 if (image->common.transform->matrix[2][0] == 0 &&
283 image->common.transform->matrix[2][1] == 0 &&
284 image->common.transform->matrix[2][2] == pixman_fixed_1)
286 flags |= FAST_PATH_AFFINE_TRANSFORM;
288 if (image->common.transform->matrix[0][1] == 0 &&
289 image->common.transform->matrix[1][0] == 0)
291 if (image->common.transform->matrix[0][0] == -pixman_fixed_1 &&
292 image->common.transform->matrix[1][1] == -pixman_fixed_1)
294 flags |= FAST_PATH_ROTATE_180_TRANSFORM;
296 flags |= FAST_PATH_SCALE_TRANSFORM;
298 else if (image->common.transform->matrix[0][0] == 0 &&
299 image->common.transform->matrix[1][1] == 0)
301 pixman_fixed_t m01 = image->common.transform->matrix[0][1];
302 pixman_fixed_t m10 = image->common.transform->matrix[1][0];
304 if (m01 == -1 && m10 == 1)
305 flags |= FAST_PATH_ROTATE_90_TRANSFORM;
306 else if (m01 == 1 && m10 == -1)
307 flags |= FAST_PATH_ROTATE_270_TRANSFORM;
311 if (image->common.transform->matrix[0][0] > 0)
312 flags |= FAST_PATH_X_UNIT_POSITIVE;
314 if (image->common.transform->matrix[1][0] == 0)
315 flags |= FAST_PATH_Y_UNIT_ZERO;
319 switch (image->common.filter)
321 case PIXMAN_FILTER_NEAREST:
322 case PIXMAN_FILTER_FAST:
323 flags |= (FAST_PATH_NEAREST_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
326 case PIXMAN_FILTER_BILINEAR:
327 case PIXMAN_FILTER_GOOD:
328 case PIXMAN_FILTER_BEST:
329 flags |= (FAST_PATH_BILINEAR_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
331 /* Here we have a chance to optimize BILINEAR filter to NEAREST if
332 * they are equivalent for the currently used transformation matrix.
334 if (flags & FAST_PATH_ID_TRANSFORM)
336 flags |= FAST_PATH_NEAREST_FILTER;
339 /* affine and integer translation components in matrix ... */
340 ((flags & FAST_PATH_AFFINE_TRANSFORM) &&
341 !pixman_fixed_frac (image->common.transform->matrix[0][2] |
342 image->common.transform->matrix[1][2])) &&
344 /* ... combined with a simple rotation */
345 (flags & (FAST_PATH_ROTATE_90_TRANSFORM |
346 FAST_PATH_ROTATE_180_TRANSFORM |
347 FAST_PATH_ROTATE_270_TRANSFORM)) ||
348 /* ... or combined with a simple non-rotated translation */
349 (image->common.transform->matrix[0][0] == pixman_fixed_1 &&
350 image->common.transform->matrix[1][1] == pixman_fixed_1 &&
351 image->common.transform->matrix[0][1] == 0 &&
352 image->common.transform->matrix[1][0] == 0)
356 /* FIXME: there are some affine-test failures, showing that
357 * handling of BILINEAR and NEAREST filter is not quite
358 * equivalent when getting close to 32K for the translation
359 * components of the matrix. That's likely some bug, but for
360 * now just skip BILINEAR->NEAREST optimization in this case.
362 pixman_fixed_t magic_limit = pixman_int_to_fixed (30000);
363 if (image->common.transform->matrix[0][2] <= magic_limit &&
364 image->common.transform->matrix[1][2] <= magic_limit &&
365 image->common.transform->matrix[0][2] >= -magic_limit &&
366 image->common.transform->matrix[1][2] >= -magic_limit)
368 flags |= FAST_PATH_NEAREST_FILTER;
373 case PIXMAN_FILTER_CONVOLUTION:
377 flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
382 switch (image->common.repeat)
384 case PIXMAN_REPEAT_NONE:
386 FAST_PATH_NO_REFLECT_REPEAT |
387 FAST_PATH_NO_PAD_REPEAT |
388 FAST_PATH_NO_NORMAL_REPEAT;
391 case PIXMAN_REPEAT_REFLECT:
393 FAST_PATH_NO_PAD_REPEAT |
394 FAST_PATH_NO_NONE_REPEAT |
395 FAST_PATH_NO_NORMAL_REPEAT;
398 case PIXMAN_REPEAT_PAD:
400 FAST_PATH_NO_REFLECT_REPEAT |
401 FAST_PATH_NO_NONE_REPEAT |
402 FAST_PATH_NO_NORMAL_REPEAT;
407 FAST_PATH_NO_REFLECT_REPEAT |
408 FAST_PATH_NO_PAD_REPEAT |
409 FAST_PATH_NO_NONE_REPEAT;
413 /* Component alpha */
414 if (image->common.component_alpha)
415 flags |= FAST_PATH_COMPONENT_ALPHA;
417 flags |= FAST_PATH_UNIFIED_ALPHA;
419 flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NARROW_FORMAT);
421 /* Type specific checks */
427 if (image->solid.color.alpha == 0xffff)
428 flags |= FAST_PATH_IS_OPAQUE;
432 if (image->bits.width == 1 &&
433 image->bits.height == 1 &&
434 image->common.repeat != PIXMAN_REPEAT_NONE)
440 code = image->bits.format;
441 flags |= FAST_PATH_BITS_IMAGE;
444 if (!PIXMAN_FORMAT_A (image->bits.format) &&
445 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY &&
446 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
448 flags |= FAST_PATH_SAMPLES_OPAQUE;
450 if (image->common.repeat != PIXMAN_REPEAT_NONE)
451 flags |= FAST_PATH_IS_OPAQUE;
454 if (image->bits.read_func || image->bits.write_func)
455 flags &= ~FAST_PATH_NO_ACCESSORS;
457 if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
458 flags &= ~FAST_PATH_NARROW_FORMAT;
462 code = PIXMAN_unknown;
465 * As explained in pixman-radial-gradient.c, every point of
466 * the plane has a valid associated radius (and thus will be
467 * colored) if and only if a is negative (i.e. one of the two
468 * circles contains the other one).
471 if (image->radial.a >= 0)
478 code = PIXMAN_unknown;
480 if (image->common.repeat != PIXMAN_REPEAT_NONE)
484 flags |= FAST_PATH_IS_OPAQUE;
485 for (i = 0; i < image->gradient.n_stops; ++i)
487 if (image->gradient.stops[i].color.alpha != 0xffff)
489 flags &= ~FAST_PATH_IS_OPAQUE;
497 code = PIXMAN_unknown;
502 if (!image->common.alpha_map)
504 flags |= FAST_PATH_NO_ALPHA_MAP;
508 if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
509 flags &= ~FAST_PATH_NARROW_FORMAT;
512 /* Both alpha maps and convolution filters can introduce
513 * non-opaqueness in otherwise opaque images. Also
514 * an image with component alpha turned on is only opaque
515 * if all channels are opaque, so we simply turn it off
516 * unconditionally for those images.
518 if (image->common.alpha_map ||
519 image->common.filter == PIXMAN_FILTER_CONVOLUTION ||
520 image->common.component_alpha)
522 flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
525 image->common.flags = flags;
526 image->common.extended_format_code = code;
530 _pixman_image_validate (pixman_image_t *image)
532 if (image->common.dirty)
534 compute_image_info (image);
536 /* It is important that property_changed is
537 * called *after* compute_image_info() because
538 * property_changed() can make use of the flags
539 * to set up accessors etc.
541 if (image->common.property_changed)
542 image->common.property_changed (image);
544 image->common.dirty = FALSE;
547 if (image->common.alpha_map)
548 _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
551 PIXMAN_EXPORT pixman_bool_t
552 pixman_image_set_clip_region32 (pixman_image_t * image,
553 pixman_region32_t *region)
555 image_common_t *common = (image_common_t *)image;
556 pixman_bool_t result;
560 if ((result = pixman_region32_copy (&common->clip_region, region)))
561 image->common.have_clip_region = TRUE;
565 _pixman_image_reset_clip_region (image);
570 image_property_changed (image);
575 PIXMAN_EXPORT pixman_bool_t
576 pixman_image_set_clip_region (pixman_image_t * image,
577 pixman_region16_t *region)
579 image_common_t *common = (image_common_t *)image;
580 pixman_bool_t result;
584 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
585 image->common.have_clip_region = TRUE;
589 _pixman_image_reset_clip_region (image);
594 image_property_changed (image);
600 pixman_image_set_has_client_clip (pixman_image_t *image,
601 pixman_bool_t client_clip)
603 image->common.client_clip = client_clip;
606 PIXMAN_EXPORT pixman_bool_t
607 pixman_image_set_transform (pixman_image_t * image,
608 const pixman_transform_t *transform)
610 static const pixman_transform_t id =
612 { { pixman_fixed_1, 0, 0 },
613 { 0, pixman_fixed_1, 0 },
614 { 0, 0, pixman_fixed_1 } }
617 image_common_t *common = (image_common_t *)image;
618 pixman_bool_t result;
620 if (common->transform == transform)
623 if (!transform || memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
625 free (common->transform);
626 common->transform = NULL;
632 if (common->transform &&
633 memcmp (common->transform, transform, sizeof (pixman_transform_t)) == 0)
638 if (common->transform == NULL)
639 common->transform = malloc (sizeof (pixman_transform_t));
641 if (common->transform == NULL)
648 memcpy (common->transform, transform, sizeof(pixman_transform_t));
653 image_property_changed (image);
659 pixman_image_set_repeat (pixman_image_t *image,
660 pixman_repeat_t repeat)
662 if (image->common.repeat == repeat)
665 image->common.repeat = repeat;
667 image_property_changed (image);
670 PIXMAN_EXPORT pixman_bool_t
671 pixman_image_set_filter (pixman_image_t * image,
672 pixman_filter_t filter,
673 const pixman_fixed_t *params,
676 image_common_t *common = (image_common_t *)image;
677 pixman_fixed_t *new_params;
679 if (params == common->filter_params && filter == common->filter)
685 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
690 params, n_params * sizeof (pixman_fixed_t));
693 common->filter = filter;
695 if (common->filter_params)
696 free (common->filter_params);
698 common->filter_params = new_params;
699 common->n_filter_params = n_params;
701 image_property_changed (image);
706 pixman_image_set_source_clipping (pixman_image_t *image,
707 pixman_bool_t clip_sources)
709 if (image->common.clip_sources == clip_sources)
712 image->common.clip_sources = clip_sources;
714 image_property_changed (image);
717 /* Unlike all the other property setters, this function does not
718 * copy the content of indexed. Doing this copying is simply
719 * way, way too expensive.
722 pixman_image_set_indexed (pixman_image_t * image,
723 const pixman_indexed_t *indexed)
725 bits_image_t *bits = (bits_image_t *)image;
727 if (bits->indexed == indexed)
730 bits->indexed = indexed;
732 image_property_changed (image);
736 pixman_image_set_alpha_map (pixman_image_t *image,
737 pixman_image_t *alpha_map,
741 image_common_t *common = (image_common_t *)image;
743 return_if_fail (!alpha_map || alpha_map->type == BITS);
745 if (alpha_map && common->alpha_count > 0)
747 /* If this image is being used as an alpha map itself,
748 * then you can't give it an alpha map of its own.
753 if (alpha_map && alpha_map->common.alpha_map)
755 /* If the image has an alpha map of its own,
756 * then it can't be used as an alpha map itself
761 if (common->alpha_map != (bits_image_t *)alpha_map)
763 if (common->alpha_map)
765 common->alpha_map->common.alpha_count--;
767 pixman_image_unref ((pixman_image_t *)common->alpha_map);
772 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
774 common->alpha_map->common.alpha_count++;
778 common->alpha_map = NULL;
782 common->alpha_origin_x = x;
783 common->alpha_origin_y = y;
785 image_property_changed (image);
789 pixman_image_set_component_alpha (pixman_image_t *image,
790 pixman_bool_t component_alpha)
792 if (image->common.component_alpha == component_alpha)
795 image->common.component_alpha = component_alpha;
797 image_property_changed (image);
800 PIXMAN_EXPORT pixman_bool_t
801 pixman_image_get_component_alpha (pixman_image_t *image)
803 return image->common.component_alpha;
807 pixman_image_set_accessors (pixman_image_t * image,
808 pixman_read_memory_func_t read_func,
809 pixman_write_memory_func_t write_func)
811 return_if_fail (image != NULL);
813 if (image->type == BITS)
815 image->bits.read_func = read_func;
816 image->bits.write_func = write_func;
818 image_property_changed (image);
822 PIXMAN_EXPORT uint32_t *
823 pixman_image_get_data (pixman_image_t *image)
825 if (image->type == BITS)
826 return image->bits.bits;
832 pixman_image_get_width (pixman_image_t *image)
834 if (image->type == BITS)
835 return image->bits.width;
841 pixman_image_get_height (pixman_image_t *image)
843 if (image->type == BITS)
844 return image->bits.height;
850 pixman_image_get_stride (pixman_image_t *image)
852 if (image->type == BITS)
853 return image->bits.rowstride * (int) sizeof (uint32_t);
859 pixman_image_get_depth (pixman_image_t *image)
861 if (image->type == BITS)
862 return PIXMAN_FORMAT_DEPTH (image->bits.format);
867 PIXMAN_EXPORT pixman_format_code_t
868 pixman_image_get_format (pixman_image_t *image)
870 if (image->type == BITS)
871 return image->bits.format;
877 _pixman_image_get_solid (pixman_implementation_t *imp,
878 pixman_image_t * image,
879 pixman_format_code_t format)
884 _pixman_implementation_src_iter_init (
885 imp, &iter, image, 0, 0, 1, 1,
886 (uint8_t *)&result, ITER_NARROW, image->common.flags);
888 result = *iter.get_scanline (&iter, NULL);
890 /* If necessary, convert RGB <--> BGR. */
891 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
893 result = (((result & 0xff000000) >> 0) |
894 ((result & 0x00ff0000) >> 16) |
895 ((result & 0x0000ff00) >> 0) |
896 ((result & 0x000000ff) << 16));