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_init (pixman_image_t *image)
55 image_common_t *common = &image->common;
57 pixman_region32_init (&common->clip_region);
59 common->alpha_count = 0;
60 common->have_clip_region = FALSE;
61 common->clip_sources = FALSE;
62 common->transform = NULL;
63 common->repeat = PIXMAN_REPEAT_NONE;
64 common->filter = PIXMAN_FILTER_NEAREST;
65 common->filter_params = NULL;
66 common->n_filter_params = 0;
67 common->alpha_map = NULL;
68 common->component_alpha = FALSE;
69 common->ref_count = 1;
70 common->property_changed = NULL;
71 common->client_clip = FALSE;
72 common->destroy_func = NULL;
73 common->destroy_data = NULL;
78 _pixman_image_fini (pixman_image_t *image)
80 image_common_t *common = (image_common_t *)image;
84 if (common->ref_count == 0)
86 if (image->common.destroy_func)
87 image->common.destroy_func (image, image->common.destroy_data);
89 pixman_region32_fini (&common->clip_region);
91 if (common->transform)
92 free (common->transform);
94 if (common->filter_params)
95 free (common->filter_params);
97 if (common->alpha_map)
98 pixman_image_unref ((pixman_image_t *)common->alpha_map);
100 if (image->type == LINEAR ||
101 image->type == RADIAL ||
102 image->type == CONICAL)
104 if (image->gradient.stops)
105 free (image->gradient.stops);
108 if (image->type == BITS && image->bits.free_me)
109 free (image->bits.free_me);
118 _pixman_image_allocate (void)
120 pixman_image_t *image = malloc (sizeof (pixman_image_t));
123 _pixman_image_init (image);
129 image_property_changed (pixman_image_t *image)
131 image->common.dirty = TRUE;
135 PIXMAN_EXPORT pixman_image_t *
136 pixman_image_ref (pixman_image_t *image)
138 image->common.ref_count++;
143 /* returns TRUE when the image is freed */
144 PIXMAN_EXPORT pixman_bool_t
145 pixman_image_unref (pixman_image_t *image)
147 if (_pixman_image_fini (image))
157 pixman_image_set_destroy_function (pixman_image_t * image,
158 pixman_image_destroy_func_t func,
161 image->common.destroy_func = func;
162 image->common.destroy_data = data;
166 pixman_image_get_destroy_data (pixman_image_t *image)
168 return image->common.destroy_data;
172 _pixman_image_reset_clip_region (pixman_image_t *image)
174 image->common.have_clip_region = FALSE;
177 /* Executive Summary: This function is a no-op that only exists
178 * for historical reasons.
180 * There used to be a bug in the X server where it would rely on
181 * out-of-bounds accesses when it was asked to composite with a
182 * window as the source. It would create a pixman image pointing
183 * to some bogus position in memory, but then set a clip region
184 * to the position where the actual bits were.
186 * Due to a bug in old versions of pixman, where it would not clip
187 * against the image bounds when a clip region was set, this would
188 * actually work. So when the pixman bug was fixed, a workaround was
189 * added to allow certain out-of-bound accesses. This function disabled
192 * Since 0.21.2, pixman doesn't do these workarounds anymore, so now
193 * this function is a no-op.
196 pixman_disable_out_of_bounds_workaround (void)
201 compute_image_info (pixman_image_t *image)
203 pixman_format_code_t code;
207 if (!image->common.transform)
209 flags |= (FAST_PATH_ID_TRANSFORM |
210 FAST_PATH_X_UNIT_POSITIVE |
211 FAST_PATH_Y_UNIT_ZERO |
212 FAST_PATH_AFFINE_TRANSFORM);
216 flags |= FAST_PATH_HAS_TRANSFORM;
218 if (image->common.transform->matrix[2][0] == 0 &&
219 image->common.transform->matrix[2][1] == 0 &&
220 image->common.transform->matrix[2][2] == pixman_fixed_1)
222 flags |= FAST_PATH_AFFINE_TRANSFORM;
224 if (image->common.transform->matrix[0][1] == 0 &&
225 image->common.transform->matrix[1][0] == 0)
227 if (image->common.transform->matrix[0][0] == -pixman_fixed_1 &&
228 image->common.transform->matrix[1][1] == -pixman_fixed_1)
230 flags |= FAST_PATH_ROTATE_180_TRANSFORM;
232 flags |= FAST_PATH_SCALE_TRANSFORM;
234 else if (image->common.transform->matrix[0][0] == 0 &&
235 image->common.transform->matrix[1][1] == 0)
237 pixman_fixed_t m01 = image->common.transform->matrix[0][1];
238 if (m01 == -image->common.transform->matrix[1][0])
240 if (m01 == -pixman_fixed_1)
241 flags |= FAST_PATH_ROTATE_90_TRANSFORM;
242 else if (m01 == pixman_fixed_1)
243 flags |= FAST_PATH_ROTATE_270_TRANSFORM;
248 if (image->common.transform->matrix[0][0] > 0)
249 flags |= FAST_PATH_X_UNIT_POSITIVE;
251 if (image->common.transform->matrix[1][0] == 0)
252 flags |= FAST_PATH_Y_UNIT_ZERO;
256 switch (image->common.filter)
258 case PIXMAN_FILTER_NEAREST:
259 case PIXMAN_FILTER_FAST:
260 flags |= (FAST_PATH_NEAREST_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
263 case PIXMAN_FILTER_BILINEAR:
264 case PIXMAN_FILTER_GOOD:
265 case PIXMAN_FILTER_BEST:
266 flags |= (FAST_PATH_BILINEAR_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
268 /* Here we have a chance to optimize BILINEAR filter to NEAREST if
269 * they are equivalent for the currently used transformation matrix.
271 if (flags & FAST_PATH_ID_TRANSFORM)
273 flags |= FAST_PATH_NEAREST_FILTER;
276 /* affine and integer translation components in matrix ... */
277 ((flags & FAST_PATH_AFFINE_TRANSFORM) &&
278 !pixman_fixed_frac (image->common.transform->matrix[0][2] |
279 image->common.transform->matrix[1][2])) &&
281 /* ... combined with a simple rotation */
282 (flags & (FAST_PATH_ROTATE_90_TRANSFORM |
283 FAST_PATH_ROTATE_180_TRANSFORM |
284 FAST_PATH_ROTATE_270_TRANSFORM)) ||
285 /* ... or combined with a simple non-rotated translation */
286 (image->common.transform->matrix[0][0] == pixman_fixed_1 &&
287 image->common.transform->matrix[1][1] == pixman_fixed_1 &&
288 image->common.transform->matrix[0][1] == 0 &&
289 image->common.transform->matrix[1][0] == 0)
293 /* FIXME: there are some affine-test failures, showing that
294 * handling of BILINEAR and NEAREST filter is not quite
295 * equivalent when getting close to 32K for the translation
296 * components of the matrix. That's likely some bug, but for
297 * now just skip BILINEAR->NEAREST optimization in this case.
299 pixman_fixed_t magic_limit = pixman_int_to_fixed (30000);
300 if (image->common.transform->matrix[0][2] <= magic_limit &&
301 image->common.transform->matrix[1][2] <= magic_limit &&
302 image->common.transform->matrix[0][2] >= -magic_limit &&
303 image->common.transform->matrix[1][2] >= -magic_limit)
305 flags |= FAST_PATH_NEAREST_FILTER;
310 case PIXMAN_FILTER_CONVOLUTION:
314 flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
319 switch (image->common.repeat)
321 case PIXMAN_REPEAT_NONE:
323 FAST_PATH_NO_REFLECT_REPEAT |
324 FAST_PATH_NO_PAD_REPEAT |
325 FAST_PATH_NO_NORMAL_REPEAT;
328 case PIXMAN_REPEAT_REFLECT:
330 FAST_PATH_NO_PAD_REPEAT |
331 FAST_PATH_NO_NONE_REPEAT |
332 FAST_PATH_NO_NORMAL_REPEAT;
335 case PIXMAN_REPEAT_PAD:
337 FAST_PATH_NO_REFLECT_REPEAT |
338 FAST_PATH_NO_NONE_REPEAT |
339 FAST_PATH_NO_NORMAL_REPEAT;
344 FAST_PATH_NO_REFLECT_REPEAT |
345 FAST_PATH_NO_PAD_REPEAT |
346 FAST_PATH_NO_NONE_REPEAT;
350 /* Component alpha */
351 if (image->common.component_alpha)
352 flags |= FAST_PATH_COMPONENT_ALPHA;
354 flags |= FAST_PATH_UNIFIED_ALPHA;
356 flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NARROW_FORMAT);
358 /* Type specific checks */
364 if (image->solid.color.alpha == 0xffff)
365 flags |= FAST_PATH_IS_OPAQUE;
369 if (image->bits.width == 1 &&
370 image->bits.height == 1 &&
371 image->common.repeat != PIXMAN_REPEAT_NONE)
377 code = image->bits.format;
380 if (!PIXMAN_FORMAT_A (image->bits.format) &&
381 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY &&
382 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
384 flags |= FAST_PATH_SAMPLES_OPAQUE;
386 if (image->common.repeat != PIXMAN_REPEAT_NONE)
387 flags |= FAST_PATH_IS_OPAQUE;
390 if (image->bits.read_func || image->bits.write_func)
391 flags &= ~FAST_PATH_NO_ACCESSORS;
393 if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
394 flags &= ~FAST_PATH_NARROW_FORMAT;
398 code = PIXMAN_unknown;
401 * As explained in pixman-radial-gradient.c, every point of
402 * the plane has a valid associated radius (and thus will be
403 * colored) if and only if a is negative (i.e. one of the two
404 * circles contains the other one).
407 if (image->radial.a >= 0)
414 code = PIXMAN_unknown;
416 if (image->common.repeat != PIXMAN_REPEAT_NONE)
420 flags |= FAST_PATH_IS_OPAQUE;
421 for (i = 0; i < image->gradient.n_stops; ++i)
423 if (image->gradient.stops[i].color.alpha != 0xffff)
425 flags &= ~FAST_PATH_IS_OPAQUE;
433 code = PIXMAN_unknown;
438 if (!image->common.alpha_map)
440 flags |= FAST_PATH_NO_ALPHA_MAP;
444 if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
445 flags &= ~FAST_PATH_NARROW_FORMAT;
448 /* Both alpha maps and convolution filters can introduce
449 * non-opaqueness in otherwise opaque images. Also
450 * an image with component alpha turned on is only opaque
451 * if all channels are opaque, so we simply turn it off
452 * unconditionally for those images.
454 if (image->common.alpha_map ||
455 image->common.filter == PIXMAN_FILTER_CONVOLUTION ||
456 image->common.component_alpha)
458 flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
461 image->common.flags = flags;
462 image->common.extended_format_code = code;
466 _pixman_image_validate (pixman_image_t *image)
468 if (image->common.dirty)
470 compute_image_info (image);
472 /* It is important that property_changed is
473 * called *after* compute_image_info() because
474 * property_changed() can make use of the flags
475 * to set up accessors etc.
477 if (image->common.property_changed)
478 image->common.property_changed (image);
480 image->common.dirty = FALSE;
483 if (image->common.alpha_map)
484 _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
487 PIXMAN_EXPORT pixman_bool_t
488 pixman_image_set_clip_region32 (pixman_image_t * image,
489 pixman_region32_t *region)
491 image_common_t *common = (image_common_t *)image;
492 pixman_bool_t result;
496 if ((result = pixman_region32_copy (&common->clip_region, region)))
497 image->common.have_clip_region = TRUE;
501 _pixman_image_reset_clip_region (image);
506 image_property_changed (image);
511 PIXMAN_EXPORT pixman_bool_t
512 pixman_image_set_clip_region (pixman_image_t * image,
513 pixman_region16_t *region)
515 image_common_t *common = (image_common_t *)image;
516 pixman_bool_t result;
520 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
521 image->common.have_clip_region = TRUE;
525 _pixman_image_reset_clip_region (image);
530 image_property_changed (image);
536 pixman_image_set_has_client_clip (pixman_image_t *image,
537 pixman_bool_t client_clip)
539 image->common.client_clip = client_clip;
542 PIXMAN_EXPORT pixman_bool_t
543 pixman_image_set_transform (pixman_image_t * image,
544 const pixman_transform_t *transform)
546 static const pixman_transform_t id =
548 { { pixman_fixed_1, 0, 0 },
549 { 0, pixman_fixed_1, 0 },
550 { 0, 0, pixman_fixed_1 } }
553 image_common_t *common = (image_common_t *)image;
554 pixman_bool_t result;
556 if (common->transform == transform)
559 if (!transform || memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
561 free (common->transform);
562 common->transform = NULL;
568 if (common->transform &&
569 memcmp (common->transform, transform, sizeof (pixman_transform_t) == 0))
574 if (common->transform == NULL)
575 common->transform = malloc (sizeof (pixman_transform_t));
577 if (common->transform == NULL)
584 memcpy (common->transform, transform, sizeof(pixman_transform_t));
589 image_property_changed (image);
595 pixman_image_set_repeat (pixman_image_t *image,
596 pixman_repeat_t repeat)
598 if (image->common.repeat == repeat)
601 image->common.repeat = repeat;
603 image_property_changed (image);
606 PIXMAN_EXPORT pixman_bool_t
607 pixman_image_set_filter (pixman_image_t * image,
608 pixman_filter_t filter,
609 const pixman_fixed_t *params,
612 image_common_t *common = (image_common_t *)image;
613 pixman_fixed_t *new_params;
615 if (params == common->filter_params && filter == common->filter)
621 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
626 params, n_params * sizeof (pixman_fixed_t));
629 common->filter = filter;
631 if (common->filter_params)
632 free (common->filter_params);
634 common->filter_params = new_params;
635 common->n_filter_params = n_params;
637 image_property_changed (image);
642 pixman_image_set_source_clipping (pixman_image_t *image,
643 pixman_bool_t clip_sources)
645 if (image->common.clip_sources == clip_sources)
648 image->common.clip_sources = clip_sources;
650 image_property_changed (image);
653 /* Unlike all the other property setters, this function does not
654 * copy the content of indexed. Doing this copying is simply
655 * way, way too expensive.
658 pixman_image_set_indexed (pixman_image_t * image,
659 const pixman_indexed_t *indexed)
661 bits_image_t *bits = (bits_image_t *)image;
663 if (bits->indexed == indexed)
666 bits->indexed = indexed;
668 image_property_changed (image);
672 pixman_image_set_alpha_map (pixman_image_t *image,
673 pixman_image_t *alpha_map,
677 image_common_t *common = (image_common_t *)image;
679 return_if_fail (!alpha_map || alpha_map->type == BITS);
681 if (alpha_map && common->alpha_count > 0)
683 /* If this image is being used as an alpha map itself,
684 * then you can't give it an alpha map of its own.
689 if (alpha_map && alpha_map->common.alpha_map)
691 /* If the image has an alpha map of its own,
692 * then it can't be used as an alpha map itself
697 if (common->alpha_map != (bits_image_t *)alpha_map)
699 if (common->alpha_map)
701 common->alpha_map->common.alpha_count--;
703 pixman_image_unref ((pixman_image_t *)common->alpha_map);
708 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
710 common->alpha_map->common.alpha_count++;
714 common->alpha_map = NULL;
718 common->alpha_origin_x = x;
719 common->alpha_origin_y = y;
721 image_property_changed (image);
725 pixman_image_set_component_alpha (pixman_image_t *image,
726 pixman_bool_t component_alpha)
728 if (image->common.component_alpha == component_alpha)
731 image->common.component_alpha = component_alpha;
733 image_property_changed (image);
736 PIXMAN_EXPORT pixman_bool_t
737 pixman_image_get_component_alpha (pixman_image_t *image)
739 return image->common.component_alpha;
743 pixman_image_set_accessors (pixman_image_t * image,
744 pixman_read_memory_func_t read_func,
745 pixman_write_memory_func_t write_func)
747 return_if_fail (image != NULL);
749 if (image->type == BITS)
751 image->bits.read_func = read_func;
752 image->bits.write_func = write_func;
754 image_property_changed (image);
758 PIXMAN_EXPORT uint32_t *
759 pixman_image_get_data (pixman_image_t *image)
761 if (image->type == BITS)
762 return image->bits.bits;
768 pixman_image_get_width (pixman_image_t *image)
770 if (image->type == BITS)
771 return image->bits.width;
777 pixman_image_get_height (pixman_image_t *image)
779 if (image->type == BITS)
780 return image->bits.height;
786 pixman_image_get_stride (pixman_image_t *image)
788 if (image->type == BITS)
789 return image->bits.rowstride * (int) sizeof (uint32_t);
795 pixman_image_get_depth (pixman_image_t *image)
797 if (image->type == BITS)
798 return PIXMAN_FORMAT_DEPTH (image->bits.format);
803 PIXMAN_EXPORT pixman_format_code_t
804 pixman_image_get_format (pixman_image_t *image)
806 if (image->type == BITS)
807 return image->bits.format;
813 _pixman_image_get_solid (pixman_implementation_t *imp,
814 pixman_image_t * image,
815 pixman_format_code_t format)
820 _pixman_implementation_src_iter_init (
821 imp, &iter, image, 0, 0, 1, 1,
822 (uint8_t *)&result, ITER_NARROW);
824 result = *iter.get_scanline (&iter, NULL);
826 /* If necessary, convert RGB <--> BGR. */
827 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
829 result = (((result & 0xff000000) >> 0) |
830 ((result & 0x00ff0000) >> 16) |
831 ((result & 0x0000ff00) >> 0) |
832 ((result & 0x000000ff) << 16));