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->client_clip = FALSE;
77 common->destroy_func = NULL;
78 common->destroy_data = NULL;
86 _pixman_image_classify (pixman_image_t *image,
92 if (image->common.classify)
93 return image->common.classify (image, x, y, width, height);
95 return SOURCE_IMAGE_CLASS_UNKNOWN;
99 image_property_changed (pixman_image_t *image)
101 image->common.dirty = TRUE;
105 PIXMAN_EXPORT pixman_image_t *
106 pixman_image_ref (pixman_image_t *image)
108 image->common.ref_count++;
113 /* returns TRUE when the image is freed */
114 PIXMAN_EXPORT pixman_bool_t
115 pixman_image_unref (pixman_image_t *image)
117 image_common_t *common = (image_common_t *)image;
121 if (common->ref_count == 0)
123 if (image->common.destroy_func)
124 image->common.destroy_func (image, image->common.destroy_data);
126 pixman_region32_fini (&common->clip_region);
128 if (common->transform)
129 free (common->transform);
131 if (common->filter_params)
132 free (common->filter_params);
134 if (common->alpha_map)
135 pixman_image_unref ((pixman_image_t *)common->alpha_map);
137 if (image->type == LINEAR ||
138 image->type == RADIAL ||
139 image->type == CONICAL)
141 if (image->gradient.stops)
142 free (image->gradient.stops);
145 if (image->type == BITS && image->bits.free_me)
146 free (image->bits.free_me);
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 flags |= FAST_PATH_SCALE_TRANSFORM;
231 if (image->common.transform->matrix[0][0] > 0)
232 flags |= FAST_PATH_X_UNIT_POSITIVE;
234 if (image->common.transform->matrix[1][0] == 0)
235 flags |= FAST_PATH_Y_UNIT_ZERO;
239 switch (image->common.filter)
241 case PIXMAN_FILTER_NEAREST:
242 case PIXMAN_FILTER_FAST:
243 flags |= (FAST_PATH_NEAREST_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
246 case PIXMAN_FILTER_BILINEAR:
247 case PIXMAN_FILTER_GOOD:
248 case PIXMAN_FILTER_BEST:
249 flags |= (FAST_PATH_BILINEAR_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
252 case PIXMAN_FILTER_CONVOLUTION:
256 flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
261 switch (image->common.repeat)
263 case PIXMAN_REPEAT_NONE:
265 FAST_PATH_NO_REFLECT_REPEAT |
266 FAST_PATH_NO_PAD_REPEAT |
267 FAST_PATH_NO_NORMAL_REPEAT;
270 case PIXMAN_REPEAT_REFLECT:
272 FAST_PATH_NO_PAD_REPEAT |
273 FAST_PATH_NO_NONE_REPEAT |
274 FAST_PATH_NO_NORMAL_REPEAT;
277 case PIXMAN_REPEAT_PAD:
279 FAST_PATH_NO_REFLECT_REPEAT |
280 FAST_PATH_NO_NONE_REPEAT |
281 FAST_PATH_NO_NORMAL_REPEAT;
286 FAST_PATH_NO_REFLECT_REPEAT |
287 FAST_PATH_NO_PAD_REPEAT |
288 FAST_PATH_NO_NONE_REPEAT;
292 /* Component alpha */
293 if (image->common.component_alpha)
294 flags |= FAST_PATH_COMPONENT_ALPHA;
296 flags |= FAST_PATH_UNIFIED_ALPHA;
298 flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NARROW_FORMAT);
300 /* Type specific checks */
306 if (image->solid.color.alpha == 0xffff)
307 flags |= FAST_PATH_IS_OPAQUE;
311 if (image->bits.width == 1 &&
312 image->bits.height == 1 &&
313 image->common.repeat != PIXMAN_REPEAT_NONE)
319 code = image->bits.format;
322 if (!PIXMAN_FORMAT_A (image->bits.format) &&
323 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY &&
324 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
326 flags |= FAST_PATH_SAMPLES_OPAQUE;
328 if (image->common.repeat != PIXMAN_REPEAT_NONE)
329 flags |= FAST_PATH_IS_OPAQUE;
332 if (image->bits.read_func || image->bits.write_func)
333 flags &= ~FAST_PATH_NO_ACCESSORS;
335 if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
336 flags &= ~FAST_PATH_NARROW_FORMAT;
340 code = PIXMAN_unknown;
343 * As explained in pixman-radial-gradient.c, every point of
344 * the plane has a valid associated radius (and thus will be
345 * colored) if and only if a is negative (i.e. one of the two
346 * circles contains the other one).
349 if (image->radial.a >= 0)
356 code = PIXMAN_unknown;
358 if (image->common.repeat != PIXMAN_REPEAT_NONE)
362 flags |= FAST_PATH_IS_OPAQUE;
363 for (i = 0; i < image->gradient.n_stops; ++i)
365 if (image->gradient.stops[i].color.alpha != 0xffff)
367 flags &= ~FAST_PATH_IS_OPAQUE;
375 code = PIXMAN_unknown;
380 if (!image->common.alpha_map)
382 flags |= FAST_PATH_NO_ALPHA_MAP;
386 if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
387 flags &= ~FAST_PATH_NARROW_FORMAT;
390 /* Both alpha maps and convolution filters can introduce
391 * non-opaqueness in otherwise opaque images. Also
392 * an image with component alpha turned on is only opaque
393 * if all channels are opaque, so we simply turn it off
394 * unconditionally for those images.
396 if (image->common.alpha_map ||
397 image->common.filter == PIXMAN_FILTER_CONVOLUTION ||
398 image->common.component_alpha)
400 flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
403 image->common.flags = flags;
404 image->common.extended_format_code = code;
408 _pixman_image_validate (pixman_image_t *image)
410 if (image->common.dirty)
412 compute_image_info (image);
414 /* It is important that property_changed is
415 * called *after* compute_image_info() because
416 * property_changed() can make use of the flags
417 * to set up accessors etc.
419 image->common.property_changed (image);
421 image->common.dirty = FALSE;
424 if (image->common.alpha_map)
425 _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
428 PIXMAN_EXPORT pixman_bool_t
429 pixman_image_set_clip_region32 (pixman_image_t * image,
430 pixman_region32_t *region)
432 image_common_t *common = (image_common_t *)image;
433 pixman_bool_t result;
437 if ((result = pixman_region32_copy (&common->clip_region, region)))
438 image->common.have_clip_region = TRUE;
442 _pixman_image_reset_clip_region (image);
447 image_property_changed (image);
452 PIXMAN_EXPORT pixman_bool_t
453 pixman_image_set_clip_region (pixman_image_t * image,
454 pixman_region16_t *region)
456 image_common_t *common = (image_common_t *)image;
457 pixman_bool_t result;
461 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
462 image->common.have_clip_region = TRUE;
466 _pixman_image_reset_clip_region (image);
471 image_property_changed (image);
477 pixman_image_set_has_client_clip (pixman_image_t *image,
478 pixman_bool_t client_clip)
480 image->common.client_clip = client_clip;
483 PIXMAN_EXPORT pixman_bool_t
484 pixman_image_set_transform (pixman_image_t * image,
485 const pixman_transform_t *transform)
487 static const pixman_transform_t id =
489 { { pixman_fixed_1, 0, 0 },
490 { 0, pixman_fixed_1, 0 },
491 { 0, 0, pixman_fixed_1 } }
494 image_common_t *common = (image_common_t *)image;
495 pixman_bool_t result;
497 if (common->transform == transform)
500 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
502 free (common->transform);
503 common->transform = NULL;
509 if (common->transform == NULL)
510 common->transform = malloc (sizeof (pixman_transform_t));
512 if (common->transform == NULL)
519 memcpy (common->transform, transform, sizeof(pixman_transform_t));
524 image_property_changed (image);
530 pixman_image_set_repeat (pixman_image_t *image,
531 pixman_repeat_t repeat)
533 image->common.repeat = repeat;
535 image_property_changed (image);
538 PIXMAN_EXPORT pixman_bool_t
539 pixman_image_set_filter (pixman_image_t * image,
540 pixman_filter_t filter,
541 const pixman_fixed_t *params,
544 image_common_t *common = (image_common_t *)image;
545 pixman_fixed_t *new_params;
547 if (params == common->filter_params && filter == common->filter)
553 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
558 params, n_params * sizeof (pixman_fixed_t));
561 common->filter = filter;
563 if (common->filter_params)
564 free (common->filter_params);
566 common->filter_params = new_params;
567 common->n_filter_params = n_params;
569 image_property_changed (image);
574 pixman_image_set_source_clipping (pixman_image_t *image,
575 pixman_bool_t clip_sources)
577 image->common.clip_sources = clip_sources;
579 image_property_changed (image);
582 /* Unlike all the other property setters, this function does not
583 * copy the content of indexed. Doing this copying is simply
584 * way, way too expensive.
587 pixman_image_set_indexed (pixman_image_t * image,
588 const pixman_indexed_t *indexed)
590 bits_image_t *bits = (bits_image_t *)image;
592 bits->indexed = indexed;
594 image_property_changed (image);
598 pixman_image_set_alpha_map (pixman_image_t *image,
599 pixman_image_t *alpha_map,
603 image_common_t *common = (image_common_t *)image;
605 return_if_fail (!alpha_map || alpha_map->type == BITS);
607 if (alpha_map && common->alpha_count > 0)
609 /* If this image is being used as an alpha map itself,
610 * then you can't give it an alpha map of its own.
615 if (alpha_map && alpha_map->common.alpha_map)
617 /* If the image has an alpha map of its own,
618 * then it can't be used as an alpha map itself
623 if (common->alpha_map != (bits_image_t *)alpha_map)
625 if (common->alpha_map)
627 common->alpha_map->common.alpha_count--;
629 pixman_image_unref ((pixman_image_t *)common->alpha_map);
634 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
636 common->alpha_map->common.alpha_count++;
640 common->alpha_map = NULL;
644 common->alpha_origin_x = x;
645 common->alpha_origin_y = y;
647 image_property_changed (image);
651 pixman_image_set_component_alpha (pixman_image_t *image,
652 pixman_bool_t component_alpha)
654 image->common.component_alpha = component_alpha;
656 image_property_changed (image);
659 PIXMAN_EXPORT pixman_bool_t
660 pixman_image_get_component_alpha (pixman_image_t *image)
662 return image->common.component_alpha;
666 pixman_image_set_accessors (pixman_image_t * image,
667 pixman_read_memory_func_t read_func,
668 pixman_write_memory_func_t write_func)
670 return_if_fail (image != NULL);
672 if (image->type == BITS)
674 image->bits.read_func = read_func;
675 image->bits.write_func = write_func;
677 image_property_changed (image);
681 PIXMAN_EXPORT uint32_t *
682 pixman_image_get_data (pixman_image_t *image)
684 if (image->type == BITS)
685 return image->bits.bits;
691 pixman_image_get_width (pixman_image_t *image)
693 if (image->type == BITS)
694 return image->bits.width;
700 pixman_image_get_height (pixman_image_t *image)
702 if (image->type == BITS)
703 return image->bits.height;
709 pixman_image_get_stride (pixman_image_t *image)
711 if (image->type == BITS)
712 return image->bits.rowstride * (int) sizeof (uint32_t);
718 pixman_image_get_depth (pixman_image_t *image)
720 if (image->type == BITS)
721 return PIXMAN_FORMAT_DEPTH (image->bits.format);
726 PIXMAN_EXPORT pixman_format_code_t
727 pixman_image_get_format (pixman_image_t *image)
729 if (image->type == BITS)
730 return image->bits.format;
736 _pixman_image_get_solid (pixman_implementation_t *imp,
737 pixman_image_t * image,
738 pixman_format_code_t format)
743 _pixman_implementation_src_iter_init (
744 imp, &iter, image, 0, 0, 1, 1,
745 (uint8_t *)&result, ITER_NARROW);
747 result = *iter.get_scanline (&iter, NULL);
749 /* If necessary, convert RGB <--> BGR. */
750 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
752 result = (((result & 0xff000000) >> 0) |
753 ((result & 0x00ff0000) >> 16) |
754 ((result & 0x0000ff00) >> 0) |
755 ((result & 0x000000ff) << 16));