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 * By default, just evaluate the image at 32bpp and expand. Individual image
55 * types can plug in a better scanline getter if they want to. For example
56 * we could produce smoother gradients by evaluating them at higher color
57 * depth, but that's a project for the future.
60 _pixman_image_get_scanline_generic_64 (pixman_image_t * image,
65 const uint32_t * mask)
67 uint32_t *mask8 = NULL;
69 /* Contract the mask image, if one exists, so that the 32-bit fetch
70 * function can use it.
74 mask8 = pixman_malloc_ab (width, sizeof(uint32_t));
78 pixman_contract (mask8, (uint64_t *)mask, width);
81 /* Fetch the source image into the first half of buffer. */
82 _pixman_image_get_scanline_32 (image, x, y, width, (uint32_t*)buffer, mask8);
84 /* Expand from 32bpp to 64bpp in place. */
85 pixman_expand ((uint64_t *)buffer, buffer, PIXMAN_a8r8g8b8, width);
91 _pixman_image_allocate (void)
93 pixman_image_t *image = malloc (sizeof (pixman_image_t));
97 image_common_t *common = &image->common;
99 pixman_region32_init (&common->clip_region);
101 common->alpha_count = 0;
102 common->have_clip_region = FALSE;
103 common->clip_sources = FALSE;
104 common->transform = NULL;
105 common->repeat = PIXMAN_REPEAT_NONE;
106 common->filter = PIXMAN_FILTER_NEAREST;
107 common->filter_params = NULL;
108 common->n_filter_params = 0;
109 common->alpha_map = NULL;
110 common->component_alpha = FALSE;
111 common->ref_count = 1;
112 common->classify = NULL;
113 common->client_clip = FALSE;
114 common->destroy_func = NULL;
115 common->destroy_data = NULL;
116 common->dirty = TRUE;
123 _pixman_image_classify (pixman_image_t *image,
129 if (image->common.classify)
130 return image->common.classify (image, x, y, width, height);
132 return SOURCE_IMAGE_CLASS_UNKNOWN;
136 _pixman_image_get_scanline_32 (pixman_image_t *image,
141 const uint32_t *mask)
143 image->common.get_scanline_32 (image, x, y, width, buffer, mask);
146 /* Even thought the type of buffer is uint32_t *, the function actually expects
147 * a uint64_t *buffer.
150 _pixman_image_get_scanline_64 (pixman_image_t *image,
155 const uint32_t *unused)
157 image->common.get_scanline_64 (image, x, y, width, buffer, unused);
161 image_property_changed (pixman_image_t *image)
163 image->common.dirty = TRUE;
167 PIXMAN_EXPORT pixman_image_t *
168 pixman_image_ref (pixman_image_t *image)
170 image->common.ref_count++;
175 /* returns TRUE when the image is freed */
176 PIXMAN_EXPORT pixman_bool_t
177 pixman_image_unref (pixman_image_t *image)
179 image_common_t *common = (image_common_t *)image;
183 if (common->ref_count == 0)
185 if (image->common.destroy_func)
186 image->common.destroy_func (image, image->common.destroy_data);
188 pixman_region32_fini (&common->clip_region);
190 if (common->transform)
191 free (common->transform);
193 if (common->filter_params)
194 free (common->filter_params);
196 if (common->alpha_map)
197 pixman_image_unref ((pixman_image_t *)common->alpha_map);
199 if (image->type == LINEAR ||
200 image->type == RADIAL ||
201 image->type == CONICAL)
203 if (image->gradient.stops)
204 free (image->gradient.stops);
207 if (image->type == BITS && image->bits.free_me)
208 free (image->bits.free_me);
219 pixman_image_set_destroy_function (pixman_image_t * image,
220 pixman_image_destroy_func_t func,
223 image->common.destroy_func = func;
224 image->common.destroy_data = data;
228 pixman_image_get_destroy_data (pixman_image_t *image)
230 return image->common.destroy_data;
234 _pixman_image_reset_clip_region (pixman_image_t *image)
236 image->common.have_clip_region = FALSE;
239 /* Executive Summary: This function is a no-op that only exists
240 * for historical reasons.
242 * There used to be a bug in the X server where it would rely on
243 * out-of-bounds accesses when it was asked to composite with a
244 * window as the source. It would create a pixman image pointing
245 * to some bogus position in memory, but then set a clip region
246 * to the position where the actual bits were.
248 * Due to a bug in old versions of pixman, where it would not clip
249 * against the image bounds when a clip region was set, this would
250 * actually work. So when the pixman bug was fixed, a workaround was
251 * added to allow certain out-of-bound accesses. This function disabled
254 * Since 0.21.2, pixman doesn't do these workarounds anymore, so now
255 * this function is a no-op.
258 pixman_disable_out_of_bounds_workaround (void)
263 compute_image_info (pixman_image_t *image)
265 pixman_format_code_t code;
269 if (!image->common.transform)
271 flags |= (FAST_PATH_ID_TRANSFORM |
272 FAST_PATH_X_UNIT_POSITIVE |
273 FAST_PATH_Y_UNIT_ZERO |
274 FAST_PATH_AFFINE_TRANSFORM);
278 flags |= FAST_PATH_HAS_TRANSFORM;
280 if (image->common.transform->matrix[2][0] == 0 &&
281 image->common.transform->matrix[2][1] == 0 &&
282 image->common.transform->matrix[2][2] == pixman_fixed_1)
284 flags |= FAST_PATH_AFFINE_TRANSFORM;
286 if (image->common.transform->matrix[0][1] == 0 &&
287 image->common.transform->matrix[1][0] == 0)
289 flags |= FAST_PATH_SCALE_TRANSFORM;
293 if (image->common.transform->matrix[0][0] > 0)
294 flags |= FAST_PATH_X_UNIT_POSITIVE;
296 if (image->common.transform->matrix[1][0] == 0)
297 flags |= FAST_PATH_Y_UNIT_ZERO;
301 switch (image->common.filter)
303 case PIXMAN_FILTER_NEAREST:
304 case PIXMAN_FILTER_FAST:
305 flags |= (FAST_PATH_NEAREST_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
308 case PIXMAN_FILTER_BILINEAR:
309 case PIXMAN_FILTER_GOOD:
310 case PIXMAN_FILTER_BEST:
311 flags |= (FAST_PATH_BILINEAR_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
314 case PIXMAN_FILTER_CONVOLUTION:
318 flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
323 switch (image->common.repeat)
325 case PIXMAN_REPEAT_NONE:
327 FAST_PATH_NO_REFLECT_REPEAT |
328 FAST_PATH_NO_PAD_REPEAT |
329 FAST_PATH_NO_NORMAL_REPEAT;
332 case PIXMAN_REPEAT_REFLECT:
334 FAST_PATH_NO_PAD_REPEAT |
335 FAST_PATH_NO_NONE_REPEAT |
336 FAST_PATH_NO_NORMAL_REPEAT;
339 case PIXMAN_REPEAT_PAD:
341 FAST_PATH_NO_REFLECT_REPEAT |
342 FAST_PATH_NO_NONE_REPEAT |
343 FAST_PATH_NO_NORMAL_REPEAT;
348 FAST_PATH_NO_REFLECT_REPEAT |
349 FAST_PATH_NO_PAD_REPEAT |
350 FAST_PATH_NO_NONE_REPEAT;
354 /* Component alpha */
355 if (image->common.component_alpha)
356 flags |= FAST_PATH_COMPONENT_ALPHA;
358 flags |= FAST_PATH_UNIFIED_ALPHA;
360 flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NARROW_FORMAT);
362 /* Type specific checks */
368 if (image->solid.color.alpha == 0xffff)
369 flags |= FAST_PATH_IS_OPAQUE;
373 if (image->bits.width == 1 &&
374 image->bits.height == 1 &&
375 image->common.repeat != PIXMAN_REPEAT_NONE)
381 code = image->bits.format;
384 if (!PIXMAN_FORMAT_A (image->bits.format) &&
385 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY &&
386 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
388 flags |= FAST_PATH_SAMPLES_OPAQUE;
390 if (image->common.repeat != PIXMAN_REPEAT_NONE)
391 flags |= FAST_PATH_IS_OPAQUE;
394 if (image->bits.read_func || image->bits.write_func)
395 flags &= ~FAST_PATH_NO_ACCESSORS;
397 if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
398 flags &= ~FAST_PATH_NARROW_FORMAT;
402 code = PIXMAN_unknown;
405 * As explained in pixman-radial-gradient.c, every point of
406 * the plane has a valid associated radius (and thus will be
407 * colored) if and only if a is negative (i.e. one of the two
408 * circles contains the other one).
411 if (image->radial.a >= 0)
417 code = PIXMAN_unknown;
419 if (image->common.repeat != PIXMAN_REPEAT_NONE)
423 flags |= FAST_PATH_IS_OPAQUE;
424 for (i = 0; i < image->gradient.n_stops; ++i)
426 if (image->gradient.stops[i].color.alpha != 0xffff)
428 flags &= ~FAST_PATH_IS_OPAQUE;
436 code = PIXMAN_unknown;
441 if (!image->common.alpha_map)
443 flags |= FAST_PATH_NO_ALPHA_MAP;
447 if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
448 flags &= ~FAST_PATH_NARROW_FORMAT;
451 /* Both alpha maps and convolution filters can introduce
452 * non-opaqueness in otherwise opaque images. Also
453 * an image with component alpha turned on is only opaque
454 * if all channels are opaque, so we simply turn it off
455 * unconditionally for those images.
457 if (image->common.alpha_map ||
458 image->common.filter == PIXMAN_FILTER_CONVOLUTION ||
459 image->common.component_alpha)
461 flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
464 image->common.flags = flags;
465 image->common.extended_format_code = code;
469 _pixman_image_validate (pixman_image_t *image)
471 if (image->common.dirty)
473 compute_image_info (image);
475 /* It is important that property_changed is
476 * called *after* compute_image_info() because
477 * property_changed() can make use of the flags
478 * to set up accessors etc.
480 image->common.property_changed (image);
482 image->common.dirty = FALSE;
485 if (image->common.alpha_map)
486 _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
489 PIXMAN_EXPORT pixman_bool_t
490 pixman_image_set_clip_region32 (pixman_image_t * image,
491 pixman_region32_t *region)
493 image_common_t *common = (image_common_t *)image;
494 pixman_bool_t result;
498 if ((result = pixman_region32_copy (&common->clip_region, region)))
499 image->common.have_clip_region = TRUE;
503 _pixman_image_reset_clip_region (image);
508 image_property_changed (image);
513 PIXMAN_EXPORT pixman_bool_t
514 pixman_image_set_clip_region (pixman_image_t * image,
515 pixman_region16_t *region)
517 image_common_t *common = (image_common_t *)image;
518 pixman_bool_t result;
522 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
523 image->common.have_clip_region = TRUE;
527 _pixman_image_reset_clip_region (image);
532 image_property_changed (image);
538 pixman_image_set_has_client_clip (pixman_image_t *image,
539 pixman_bool_t client_clip)
541 image->common.client_clip = client_clip;
544 PIXMAN_EXPORT pixman_bool_t
545 pixman_image_set_transform (pixman_image_t * image,
546 const pixman_transform_t *transform)
548 static const pixman_transform_t id =
550 { { pixman_fixed_1, 0, 0 },
551 { 0, pixman_fixed_1, 0 },
552 { 0, 0, pixman_fixed_1 } }
555 image_common_t *common = (image_common_t *)image;
556 pixman_bool_t result;
558 if (common->transform == transform)
561 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
563 free (common->transform);
564 common->transform = NULL;
570 if (common->transform == NULL)
571 common->transform = malloc (sizeof (pixman_transform_t));
573 if (common->transform == NULL)
580 memcpy (common->transform, transform, sizeof(pixman_transform_t));
585 image_property_changed (image);
591 pixman_image_set_repeat (pixman_image_t *image,
592 pixman_repeat_t repeat)
594 image->common.repeat = repeat;
596 image_property_changed (image);
599 PIXMAN_EXPORT pixman_bool_t
600 pixman_image_set_filter (pixman_image_t * image,
601 pixman_filter_t filter,
602 const pixman_fixed_t *params,
605 image_common_t *common = (image_common_t *)image;
606 pixman_fixed_t *new_params;
608 if (params == common->filter_params && filter == common->filter)
614 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
619 params, n_params * sizeof (pixman_fixed_t));
622 common->filter = filter;
624 if (common->filter_params)
625 free (common->filter_params);
627 common->filter_params = new_params;
628 common->n_filter_params = n_params;
630 image_property_changed (image);
635 pixman_image_set_source_clipping (pixman_image_t *image,
636 pixman_bool_t clip_sources)
638 image->common.clip_sources = clip_sources;
640 image_property_changed (image);
643 /* Unlike all the other property setters, this function does not
644 * copy the content of indexed. Doing this copying is simply
645 * way, way too expensive.
648 pixman_image_set_indexed (pixman_image_t * image,
649 const pixman_indexed_t *indexed)
651 bits_image_t *bits = (bits_image_t *)image;
653 bits->indexed = indexed;
655 image_property_changed (image);
659 pixman_image_set_alpha_map (pixman_image_t *image,
660 pixman_image_t *alpha_map,
664 image_common_t *common = (image_common_t *)image;
666 return_if_fail (!alpha_map || alpha_map->type == BITS);
668 if (alpha_map && common->alpha_count > 0)
670 /* If this image is being used as an alpha map itself,
671 * then you can't give it an alpha map of its own.
676 if (alpha_map && alpha_map->common.alpha_map)
678 /* If the image has an alpha map of its own,
679 * then it can't be used as an alpha map itself
684 if (common->alpha_map != (bits_image_t *)alpha_map)
686 if (common->alpha_map)
688 common->alpha_map->common.alpha_count--;
690 pixman_image_unref ((pixman_image_t *)common->alpha_map);
695 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
697 common->alpha_map->common.alpha_count++;
701 common->alpha_map = NULL;
705 common->alpha_origin_x = x;
706 common->alpha_origin_y = y;
708 image_property_changed (image);
712 pixman_image_set_component_alpha (pixman_image_t *image,
713 pixman_bool_t component_alpha)
715 image->common.component_alpha = component_alpha;
717 image_property_changed (image);
720 PIXMAN_EXPORT pixman_bool_t
721 pixman_image_get_component_alpha (pixman_image_t *image)
723 return image->common.component_alpha;
727 pixman_image_set_accessors (pixman_image_t * image,
728 pixman_read_memory_func_t read_func,
729 pixman_write_memory_func_t write_func)
731 return_if_fail (image != NULL);
733 if (image->type == BITS)
735 image->bits.read_func = read_func;
736 image->bits.write_func = write_func;
738 image_property_changed (image);
742 PIXMAN_EXPORT uint32_t *
743 pixman_image_get_data (pixman_image_t *image)
745 if (image->type == BITS)
746 return image->bits.bits;
752 pixman_image_get_width (pixman_image_t *image)
754 if (image->type == BITS)
755 return image->bits.width;
761 pixman_image_get_height (pixman_image_t *image)
763 if (image->type == BITS)
764 return image->bits.height;
770 pixman_image_get_stride (pixman_image_t *image)
772 if (image->type == BITS)
773 return image->bits.rowstride * (int) sizeof (uint32_t);
779 pixman_image_get_depth (pixman_image_t *image)
781 if (image->type == BITS)
782 return PIXMAN_FORMAT_DEPTH (image->bits.format);
787 PIXMAN_EXPORT pixman_format_code_t
788 pixman_image_get_format (pixman_image_t *image)
790 if (image->type == BITS)
791 return image->bits.format;
797 _pixman_image_get_solid (pixman_image_t * image,
798 pixman_format_code_t format)
802 _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL);
804 /* If necessary, convert RGB <--> BGR. */
805 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
807 result = (((result & 0xff000000) >> 0) |
808 ((result & 0x00ff0000) >> 16) |
809 ((result & 0x0000ff00) >> 0) |
810 ((result & 0x000000ff) << 16));