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);
255 case PIXMAN_FILTER_CONVOLUTION:
259 flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
264 switch (image->common.repeat)
266 case PIXMAN_REPEAT_NONE:
268 FAST_PATH_NO_REFLECT_REPEAT |
269 FAST_PATH_NO_PAD_REPEAT |
270 FAST_PATH_NO_NORMAL_REPEAT;
273 case PIXMAN_REPEAT_REFLECT:
275 FAST_PATH_NO_PAD_REPEAT |
276 FAST_PATH_NO_NONE_REPEAT |
277 FAST_PATH_NO_NORMAL_REPEAT;
280 case PIXMAN_REPEAT_PAD:
282 FAST_PATH_NO_REFLECT_REPEAT |
283 FAST_PATH_NO_NONE_REPEAT |
284 FAST_PATH_NO_NORMAL_REPEAT;
289 FAST_PATH_NO_REFLECT_REPEAT |
290 FAST_PATH_NO_PAD_REPEAT |
291 FAST_PATH_NO_NONE_REPEAT;
295 /* Component alpha */
296 if (image->common.component_alpha)
297 flags |= FAST_PATH_COMPONENT_ALPHA;
299 flags |= FAST_PATH_UNIFIED_ALPHA;
301 flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NARROW_FORMAT);
303 /* Type specific checks */
309 if (image->solid.color.alpha == 0xffff)
310 flags |= FAST_PATH_IS_OPAQUE;
314 if (image->bits.width == 1 &&
315 image->bits.height == 1 &&
316 image->common.repeat != PIXMAN_REPEAT_NONE)
322 code = image->bits.format;
325 if (!PIXMAN_FORMAT_A (image->bits.format) &&
326 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY &&
327 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
329 flags |= FAST_PATH_SAMPLES_OPAQUE;
331 if (image->common.repeat != PIXMAN_REPEAT_NONE)
332 flags |= FAST_PATH_IS_OPAQUE;
335 if (image->bits.read_func || image->bits.write_func)
336 flags &= ~FAST_PATH_NO_ACCESSORS;
338 if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
339 flags &= ~FAST_PATH_NARROW_FORMAT;
343 code = PIXMAN_unknown;
346 * As explained in pixman-radial-gradient.c, every point of
347 * the plane has a valid associated radius (and thus will be
348 * colored) if and only if a is negative (i.e. one of the two
349 * circles contains the other one).
352 if (image->radial.a >= 0)
359 code = PIXMAN_unknown;
361 if (image->common.repeat != PIXMAN_REPEAT_NONE)
365 flags |= FAST_PATH_IS_OPAQUE;
366 for (i = 0; i < image->gradient.n_stops; ++i)
368 if (image->gradient.stops[i].color.alpha != 0xffff)
370 flags &= ~FAST_PATH_IS_OPAQUE;
378 code = PIXMAN_unknown;
383 if (!image->common.alpha_map)
385 flags |= FAST_PATH_NO_ALPHA_MAP;
389 if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
390 flags &= ~FAST_PATH_NARROW_FORMAT;
393 /* Both alpha maps and convolution filters can introduce
394 * non-opaqueness in otherwise opaque images. Also
395 * an image with component alpha turned on is only opaque
396 * if all channels are opaque, so we simply turn it off
397 * unconditionally for those images.
399 if (image->common.alpha_map ||
400 image->common.filter == PIXMAN_FILTER_CONVOLUTION ||
401 image->common.component_alpha)
403 flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
406 image->common.flags = flags;
407 image->common.extended_format_code = code;
411 _pixman_image_validate (pixman_image_t *image)
413 if (image->common.dirty)
415 compute_image_info (image);
417 /* It is important that property_changed is
418 * called *after* compute_image_info() because
419 * property_changed() can make use of the flags
420 * to set up accessors etc.
422 if (image->common.property_changed)
423 image->common.property_changed (image);
425 image->common.dirty = FALSE;
428 if (image->common.alpha_map)
429 _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
432 PIXMAN_EXPORT pixman_bool_t
433 pixman_image_set_clip_region32 (pixman_image_t * image,
434 pixman_region32_t *region)
436 image_common_t *common = (image_common_t *)image;
437 pixman_bool_t result;
441 if ((result = pixman_region32_copy (&common->clip_region, region)))
442 image->common.have_clip_region = TRUE;
446 _pixman_image_reset_clip_region (image);
451 image_property_changed (image);
456 PIXMAN_EXPORT pixman_bool_t
457 pixman_image_set_clip_region (pixman_image_t * image,
458 pixman_region16_t *region)
460 image_common_t *common = (image_common_t *)image;
461 pixman_bool_t result;
465 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
466 image->common.have_clip_region = TRUE;
470 _pixman_image_reset_clip_region (image);
475 image_property_changed (image);
481 pixman_image_set_has_client_clip (pixman_image_t *image,
482 pixman_bool_t client_clip)
484 image->common.client_clip = client_clip;
487 PIXMAN_EXPORT pixman_bool_t
488 pixman_image_set_transform (pixman_image_t * image,
489 const pixman_transform_t *transform)
491 static const pixman_transform_t id =
493 { { pixman_fixed_1, 0, 0 },
494 { 0, pixman_fixed_1, 0 },
495 { 0, 0, pixman_fixed_1 } }
498 image_common_t *common = (image_common_t *)image;
499 pixman_bool_t result;
501 if (common->transform == transform)
504 if (!transform || memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
506 free (common->transform);
507 common->transform = NULL;
513 if (common->transform &&
514 memcmp (common->transform, transform, sizeof (pixman_transform_t) == 0))
519 if (common->transform == NULL)
520 common->transform = malloc (sizeof (pixman_transform_t));
522 if (common->transform == NULL)
529 memcpy (common->transform, transform, sizeof(pixman_transform_t));
534 image_property_changed (image);
540 pixman_image_set_repeat (pixman_image_t *image,
541 pixman_repeat_t repeat)
543 if (image->common.repeat == repeat)
546 image->common.repeat = repeat;
548 image_property_changed (image);
551 PIXMAN_EXPORT pixman_bool_t
552 pixman_image_set_filter (pixman_image_t * image,
553 pixman_filter_t filter,
554 const pixman_fixed_t *params,
557 image_common_t *common = (image_common_t *)image;
558 pixman_fixed_t *new_params;
560 if (params == common->filter_params && filter == common->filter)
566 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
571 params, n_params * sizeof (pixman_fixed_t));
574 common->filter = filter;
576 if (common->filter_params)
577 free (common->filter_params);
579 common->filter_params = new_params;
580 common->n_filter_params = n_params;
582 image_property_changed (image);
587 pixman_image_set_source_clipping (pixman_image_t *image,
588 pixman_bool_t clip_sources)
590 if (image->common.clip_sources == clip_sources)
593 image->common.clip_sources = clip_sources;
595 image_property_changed (image);
598 /* Unlike all the other property setters, this function does not
599 * copy the content of indexed. Doing this copying is simply
600 * way, way too expensive.
603 pixman_image_set_indexed (pixman_image_t * image,
604 const pixman_indexed_t *indexed)
606 bits_image_t *bits = (bits_image_t *)image;
608 if (bits->indexed == indexed)
611 bits->indexed = indexed;
613 image_property_changed (image);
617 pixman_image_set_alpha_map (pixman_image_t *image,
618 pixman_image_t *alpha_map,
622 image_common_t *common = (image_common_t *)image;
624 return_if_fail (!alpha_map || alpha_map->type == BITS);
626 if (alpha_map && common->alpha_count > 0)
628 /* If this image is being used as an alpha map itself,
629 * then you can't give it an alpha map of its own.
634 if (alpha_map && alpha_map->common.alpha_map)
636 /* If the image has an alpha map of its own,
637 * then it can't be used as an alpha map itself
642 if (common->alpha_map != (bits_image_t *)alpha_map)
644 if (common->alpha_map)
646 common->alpha_map->common.alpha_count--;
648 pixman_image_unref ((pixman_image_t *)common->alpha_map);
653 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
655 common->alpha_map->common.alpha_count++;
659 common->alpha_map = NULL;
663 common->alpha_origin_x = x;
664 common->alpha_origin_y = y;
666 image_property_changed (image);
670 pixman_image_set_component_alpha (pixman_image_t *image,
671 pixman_bool_t component_alpha)
673 if (image->common.component_alpha == component_alpha)
676 image->common.component_alpha = component_alpha;
678 image_property_changed (image);
681 PIXMAN_EXPORT pixman_bool_t
682 pixman_image_get_component_alpha (pixman_image_t *image)
684 return image->common.component_alpha;
688 pixman_image_set_accessors (pixman_image_t * image,
689 pixman_read_memory_func_t read_func,
690 pixman_write_memory_func_t write_func)
692 return_if_fail (image != NULL);
694 if (image->type == BITS)
696 image->bits.read_func = read_func;
697 image->bits.write_func = write_func;
699 image_property_changed (image);
703 PIXMAN_EXPORT uint32_t *
704 pixman_image_get_data (pixman_image_t *image)
706 if (image->type == BITS)
707 return image->bits.bits;
713 pixman_image_get_width (pixman_image_t *image)
715 if (image->type == BITS)
716 return image->bits.width;
722 pixman_image_get_height (pixman_image_t *image)
724 if (image->type == BITS)
725 return image->bits.height;
731 pixman_image_get_stride (pixman_image_t *image)
733 if (image->type == BITS)
734 return image->bits.rowstride * (int) sizeof (uint32_t);
740 pixman_image_get_depth (pixman_image_t *image)
742 if (image->type == BITS)
743 return PIXMAN_FORMAT_DEPTH (image->bits.format);
748 PIXMAN_EXPORT pixman_format_code_t
749 pixman_image_get_format (pixman_image_t *image)
751 if (image->type == BITS)
752 return image->bits.format;
758 _pixman_image_get_solid (pixman_implementation_t *imp,
759 pixman_image_t * image,
760 pixman_format_code_t format)
765 _pixman_implementation_src_iter_init (
766 imp, &iter, image, 0, 0, 1, 1,
767 (uint8_t *)&result, ITER_NARROW);
769 result = *iter.get_scanline (&iter, NULL);
771 /* If necessary, convert RGB <--> BGR. */
772 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
774 result = (((result & 0xff000000) >> 0) |
775 ((result & 0x00ff0000) >> 16) |
776 ((result & 0x0000ff00) >> 0) |
777 ((result & 0x000000ff) << 16));