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"
33 #include "pixman-combine32.h"
36 _pixman_init_gradient (gradient_t * gradient,
37 const pixman_gradient_stop_t *stops,
40 return_val_if_fail (n_stops > 0, FALSE);
42 gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
46 memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
48 gradient->n_stops = n_stops;
54 _pixman_image_allocate (void)
56 pixman_image_t *image = malloc (sizeof (pixman_image_t));
60 image_common_t *common = &image->common;
62 pixman_region32_init (&common->clip_region);
64 common->alpha_count = 0;
65 common->have_clip_region = FALSE;
66 common->clip_sources = FALSE;
67 common->transform = NULL;
68 common->repeat = PIXMAN_REPEAT_NONE;
69 common->filter = PIXMAN_FILTER_NEAREST;
70 common->filter_params = NULL;
71 common->n_filter_params = 0;
72 common->alpha_map = NULL;
73 common->component_alpha = FALSE;
74 common->ref_count = 1;
75 common->classify = NULL;
76 common->property_changed = NULL;
77 common->client_clip = FALSE;
78 common->destroy_func = NULL;
79 common->destroy_data = NULL;
87 _pixman_image_classify (pixman_image_t *image,
93 if (image->common.classify)
94 return image->common.classify (image, x, y, width, height);
96 return SOURCE_IMAGE_CLASS_UNKNOWN;
100 image_property_changed (pixman_image_t *image)
102 image->common.dirty = TRUE;
106 PIXMAN_EXPORT pixman_image_t *
107 pixman_image_ref (pixman_image_t *image)
109 image->common.ref_count++;
114 /* returns TRUE when the image is freed */
115 PIXMAN_EXPORT pixman_bool_t
116 pixman_image_unref (pixman_image_t *image)
118 image_common_t *common = (image_common_t *)image;
122 if (common->ref_count == 0)
124 if (image->common.destroy_func)
125 image->common.destroy_func (image, image->common.destroy_data);
127 pixman_region32_fini (&common->clip_region);
129 if (common->transform)
130 free (common->transform);
132 if (common->filter_params)
133 free (common->filter_params);
135 if (common->alpha_map)
136 pixman_image_unref ((pixman_image_t *)common->alpha_map);
138 if (image->type == LINEAR ||
139 image->type == RADIAL ||
140 image->type == CONICAL)
142 if (image->gradient.stops)
143 free (image->gradient.stops);
146 if (image->type == BITS && image->bits.free_me)
147 free (image->bits.free_me);
158 pixman_image_set_destroy_function (pixman_image_t * image,
159 pixman_image_destroy_func_t func,
162 image->common.destroy_func = func;
163 image->common.destroy_data = data;
167 pixman_image_get_destroy_data (pixman_image_t *image)
169 return image->common.destroy_data;
173 _pixman_image_reset_clip_region (pixman_image_t *image)
175 image->common.have_clip_region = FALSE;
178 /* Executive Summary: This function is a no-op that only exists
179 * for historical reasons.
181 * There used to be a bug in the X server where it would rely on
182 * out-of-bounds accesses when it was asked to composite with a
183 * window as the source. It would create a pixman image pointing
184 * to some bogus position in memory, but then set a clip region
185 * to the position where the actual bits were.
187 * Due to a bug in old versions of pixman, where it would not clip
188 * against the image bounds when a clip region was set, this would
189 * actually work. So when the pixman bug was fixed, a workaround was
190 * added to allow certain out-of-bound accesses. This function disabled
193 * Since 0.21.2, pixman doesn't do these workarounds anymore, so now
194 * this function is a no-op.
197 pixman_disable_out_of_bounds_workaround (void)
202 compute_image_info (pixman_image_t *image)
204 pixman_format_code_t code;
208 if (!image->common.transform)
210 flags |= (FAST_PATH_ID_TRANSFORM |
211 FAST_PATH_X_UNIT_POSITIVE |
212 FAST_PATH_Y_UNIT_ZERO |
213 FAST_PATH_AFFINE_TRANSFORM);
217 flags |= FAST_PATH_HAS_TRANSFORM;
219 if (image->common.transform->matrix[2][0] == 0 &&
220 image->common.transform->matrix[2][1] == 0 &&
221 image->common.transform->matrix[2][2] == pixman_fixed_1)
223 flags |= FAST_PATH_AFFINE_TRANSFORM;
225 if (image->common.transform->matrix[0][1] == 0 &&
226 image->common.transform->matrix[1][0] == 0)
228 flags |= FAST_PATH_SCALE_TRANSFORM;
232 if (image->common.transform->matrix[0][0] > 0)
233 flags |= FAST_PATH_X_UNIT_POSITIVE;
235 if (image->common.transform->matrix[1][0] == 0)
236 flags |= FAST_PATH_Y_UNIT_ZERO;
240 switch (image->common.filter)
242 case PIXMAN_FILTER_NEAREST:
243 case PIXMAN_FILTER_FAST:
244 flags |= (FAST_PATH_NEAREST_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
247 case PIXMAN_FILTER_BILINEAR:
248 case PIXMAN_FILTER_GOOD:
249 case PIXMAN_FILTER_BEST:
250 flags |= (FAST_PATH_BILINEAR_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
253 case PIXMAN_FILTER_CONVOLUTION:
257 flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
262 switch (image->common.repeat)
264 case PIXMAN_REPEAT_NONE:
266 FAST_PATH_NO_REFLECT_REPEAT |
267 FAST_PATH_NO_PAD_REPEAT |
268 FAST_PATH_NO_NORMAL_REPEAT;
271 case PIXMAN_REPEAT_REFLECT:
273 FAST_PATH_NO_PAD_REPEAT |
274 FAST_PATH_NO_NONE_REPEAT |
275 FAST_PATH_NO_NORMAL_REPEAT;
278 case PIXMAN_REPEAT_PAD:
280 FAST_PATH_NO_REFLECT_REPEAT |
281 FAST_PATH_NO_NONE_REPEAT |
282 FAST_PATH_NO_NORMAL_REPEAT;
287 FAST_PATH_NO_REFLECT_REPEAT |
288 FAST_PATH_NO_PAD_REPEAT |
289 FAST_PATH_NO_NONE_REPEAT;
293 /* Component alpha */
294 if (image->common.component_alpha)
295 flags |= FAST_PATH_COMPONENT_ALPHA;
297 flags |= FAST_PATH_UNIFIED_ALPHA;
299 flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NARROW_FORMAT);
301 /* Type specific checks */
307 if (image->solid.color.alpha == 0xffff)
308 flags |= FAST_PATH_IS_OPAQUE;
312 if (image->bits.width == 1 &&
313 image->bits.height == 1 &&
314 image->common.repeat != PIXMAN_REPEAT_NONE)
320 code = image->bits.format;
323 if (!PIXMAN_FORMAT_A (image->bits.format) &&
324 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY &&
325 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
327 flags |= FAST_PATH_SAMPLES_OPAQUE;
329 if (image->common.repeat != PIXMAN_REPEAT_NONE)
330 flags |= FAST_PATH_IS_OPAQUE;
333 if (image->bits.read_func || image->bits.write_func)
334 flags &= ~FAST_PATH_NO_ACCESSORS;
336 if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
337 flags &= ~FAST_PATH_NARROW_FORMAT;
341 code = PIXMAN_unknown;
344 * As explained in pixman-radial-gradient.c, every point of
345 * the plane has a valid associated radius (and thus will be
346 * colored) if and only if a is negative (i.e. one of the two
347 * circles contains the other one).
350 if (image->radial.a >= 0)
357 code = PIXMAN_unknown;
359 if (image->common.repeat != PIXMAN_REPEAT_NONE)
363 flags |= FAST_PATH_IS_OPAQUE;
364 for (i = 0; i < image->gradient.n_stops; ++i)
366 if (image->gradient.stops[i].color.alpha != 0xffff)
368 flags &= ~FAST_PATH_IS_OPAQUE;
376 code = PIXMAN_unknown;
381 if (!image->common.alpha_map)
383 flags |= FAST_PATH_NO_ALPHA_MAP;
387 if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
388 flags &= ~FAST_PATH_NARROW_FORMAT;
391 /* Both alpha maps and convolution filters can introduce
392 * non-opaqueness in otherwise opaque images. Also
393 * an image with component alpha turned on is only opaque
394 * if all channels are opaque, so we simply turn it off
395 * unconditionally for those images.
397 if (image->common.alpha_map ||
398 image->common.filter == PIXMAN_FILTER_CONVOLUTION ||
399 image->common.component_alpha)
401 flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
404 image->common.flags = flags;
405 image->common.extended_format_code = code;
409 _pixman_image_validate (pixman_image_t *image)
411 if (image->common.dirty)
413 compute_image_info (image);
415 /* It is important that property_changed is
416 * called *after* compute_image_info() because
417 * property_changed() can make use of the flags
418 * to set up accessors etc.
420 if (image->common.property_changed)
421 image->common.property_changed (image);
423 image->common.dirty = FALSE;
426 if (image->common.alpha_map)
427 _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
430 PIXMAN_EXPORT pixman_bool_t
431 pixman_image_set_clip_region32 (pixman_image_t * image,
432 pixman_region32_t *region)
434 image_common_t *common = (image_common_t *)image;
435 pixman_bool_t result;
439 if ((result = pixman_region32_copy (&common->clip_region, region)))
440 image->common.have_clip_region = TRUE;
444 _pixman_image_reset_clip_region (image);
449 image_property_changed (image);
454 PIXMAN_EXPORT pixman_bool_t
455 pixman_image_set_clip_region (pixman_image_t * image,
456 pixman_region16_t *region)
458 image_common_t *common = (image_common_t *)image;
459 pixman_bool_t result;
463 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
464 image->common.have_clip_region = TRUE;
468 _pixman_image_reset_clip_region (image);
473 image_property_changed (image);
479 pixman_image_set_has_client_clip (pixman_image_t *image,
480 pixman_bool_t client_clip)
482 image->common.client_clip = client_clip;
485 PIXMAN_EXPORT pixman_bool_t
486 pixman_image_set_transform (pixman_image_t * image,
487 const pixman_transform_t *transform)
489 static const pixman_transform_t id =
491 { { pixman_fixed_1, 0, 0 },
492 { 0, pixman_fixed_1, 0 },
493 { 0, 0, pixman_fixed_1 } }
496 image_common_t *common = (image_common_t *)image;
497 pixman_bool_t result;
499 if (common->transform == transform)
502 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
504 free (common->transform);
505 common->transform = NULL;
511 if (common->transform == NULL)
512 common->transform = malloc (sizeof (pixman_transform_t));
514 if (common->transform == NULL)
521 memcpy (common->transform, transform, sizeof(pixman_transform_t));
526 image_property_changed (image);
532 pixman_image_set_repeat (pixman_image_t *image,
533 pixman_repeat_t repeat)
535 image->common.repeat = repeat;
537 image_property_changed (image);
540 PIXMAN_EXPORT pixman_bool_t
541 pixman_image_set_filter (pixman_image_t * image,
542 pixman_filter_t filter,
543 const pixman_fixed_t *params,
546 image_common_t *common = (image_common_t *)image;
547 pixman_fixed_t *new_params;
549 if (params == common->filter_params && filter == common->filter)
555 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
560 params, n_params * sizeof (pixman_fixed_t));
563 common->filter = filter;
565 if (common->filter_params)
566 free (common->filter_params);
568 common->filter_params = new_params;
569 common->n_filter_params = n_params;
571 image_property_changed (image);
576 pixman_image_set_source_clipping (pixman_image_t *image,
577 pixman_bool_t clip_sources)
579 image->common.clip_sources = clip_sources;
581 image_property_changed (image);
584 /* Unlike all the other property setters, this function does not
585 * copy the content of indexed. Doing this copying is simply
586 * way, way too expensive.
589 pixman_image_set_indexed (pixman_image_t * image,
590 const pixman_indexed_t *indexed)
592 bits_image_t *bits = (bits_image_t *)image;
594 bits->indexed = indexed;
596 image_property_changed (image);
600 pixman_image_set_alpha_map (pixman_image_t *image,
601 pixman_image_t *alpha_map,
605 image_common_t *common = (image_common_t *)image;
607 return_if_fail (!alpha_map || alpha_map->type == BITS);
609 if (alpha_map && common->alpha_count > 0)
611 /* If this image is being used as an alpha map itself,
612 * then you can't give it an alpha map of its own.
617 if (alpha_map && alpha_map->common.alpha_map)
619 /* If the image has an alpha map of its own,
620 * then it can't be used as an alpha map itself
625 if (common->alpha_map != (bits_image_t *)alpha_map)
627 if (common->alpha_map)
629 common->alpha_map->common.alpha_count--;
631 pixman_image_unref ((pixman_image_t *)common->alpha_map);
636 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
638 common->alpha_map->common.alpha_count++;
642 common->alpha_map = NULL;
646 common->alpha_origin_x = x;
647 common->alpha_origin_y = y;
649 image_property_changed (image);
653 pixman_image_set_component_alpha (pixman_image_t *image,
654 pixman_bool_t component_alpha)
656 image->common.component_alpha = component_alpha;
658 image_property_changed (image);
661 PIXMAN_EXPORT pixman_bool_t
662 pixman_image_get_component_alpha (pixman_image_t *image)
664 return image->common.component_alpha;
668 pixman_image_set_accessors (pixman_image_t * image,
669 pixman_read_memory_func_t read_func,
670 pixman_write_memory_func_t write_func)
672 return_if_fail (image != NULL);
674 if (image->type == BITS)
676 image->bits.read_func = read_func;
677 image->bits.write_func = write_func;
679 image_property_changed (image);
683 PIXMAN_EXPORT uint32_t *
684 pixman_image_get_data (pixman_image_t *image)
686 if (image->type == BITS)
687 return image->bits.bits;
693 pixman_image_get_width (pixman_image_t *image)
695 if (image->type == BITS)
696 return image->bits.width;
702 pixman_image_get_height (pixman_image_t *image)
704 if (image->type == BITS)
705 return image->bits.height;
711 pixman_image_get_stride (pixman_image_t *image)
713 if (image->type == BITS)
714 return image->bits.rowstride * (int) sizeof (uint32_t);
720 pixman_image_get_depth (pixman_image_t *image)
722 if (image->type == BITS)
723 return PIXMAN_FORMAT_DEPTH (image->bits.format);
728 PIXMAN_EXPORT pixman_format_code_t
729 pixman_image_get_format (pixman_image_t *image)
731 if (image->type == BITS)
732 return image->bits.format;
738 _pixman_image_get_solid (pixman_implementation_t *imp,
739 pixman_image_t * image,
740 pixman_format_code_t format)
745 _pixman_implementation_src_iter_init (
746 imp, &iter, image, 0, 0, 1, 1,
747 (uint8_t *)&result, ITER_NARROW);
749 result = *iter.get_scanline (&iter, NULL);
751 /* If necessary, convert RGB <--> BGR. */
752 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
754 result = (((result & 0xff000000) >> 0) |
755 ((result & 0x00ff0000) >> 16) |
756 ((result & 0x0000ff00) >> 0) |
757 ((result & 0x000000ff) << 16));