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;
50 gradient->stop_range = 0xffff;
56 * By default, just evaluate the image at 32bpp and expand. Individual image
57 * types can plug in a better scanline getter if they want to. For example
58 * we could produce smoother gradients by evaluating them at higher color
59 * depth, but that's a project for the future.
62 _pixman_image_get_scanline_generic_64 (pixman_image_t * image,
67 const uint32_t * mask)
69 uint32_t *mask8 = NULL;
71 /* Contract the mask image, if one exists, so that the 32-bit fetch
72 * function can use it.
76 mask8 = pixman_malloc_ab (width, sizeof(uint32_t));
80 pixman_contract (mask8, (uint64_t *)mask, width);
83 /* Fetch the source image into the first half of buffer. */
84 _pixman_image_get_scanline_32 (image, x, y, width, (uint32_t*)buffer, mask8);
86 /* Expand from 32bpp to 64bpp in place. */
87 pixman_expand ((uint64_t *)buffer, buffer, PIXMAN_a8r8g8b8, width);
93 _pixman_image_allocate (void)
95 pixman_image_t *image = malloc (sizeof (pixman_image_t));
99 image_common_t *common = &image->common;
101 pixman_region32_init (&common->clip_region);
103 common->alpha_count = 0;
104 common->have_clip_region = FALSE;
105 common->clip_sources = FALSE;
106 common->transform = NULL;
107 common->repeat = PIXMAN_REPEAT_NONE;
108 common->filter = PIXMAN_FILTER_NEAREST;
109 common->filter_params = NULL;
110 common->n_filter_params = 0;
111 common->alpha_map = NULL;
112 common->component_alpha = FALSE;
113 common->ref_count = 1;
114 common->classify = NULL;
115 common->client_clip = FALSE;
116 common->destroy_func = NULL;
117 common->destroy_data = NULL;
118 common->dirty = TRUE;
125 _pixman_image_classify (pixman_image_t *image,
131 if (image->common.classify)
132 return image->common.classify (image, x, y, width, height);
134 return SOURCE_IMAGE_CLASS_UNKNOWN;
138 _pixman_image_get_scanline_32 (pixman_image_t *image,
143 const uint32_t *mask)
145 image->common.get_scanline_32 (image, x, y, width, buffer, mask);
148 /* Even thought the type of buffer is uint32_t *, the function actually expects
149 * a uint64_t *buffer.
152 _pixman_image_get_scanline_64 (pixman_image_t *image,
157 const uint32_t *unused)
159 image->common.get_scanline_64 (image, x, y, width, buffer, unused);
163 image_property_changed (pixman_image_t *image)
165 image->common.dirty = TRUE;
169 PIXMAN_EXPORT pixman_image_t *
170 pixman_image_ref (pixman_image_t *image)
172 image->common.ref_count++;
177 /* returns TRUE when the image is freed */
178 PIXMAN_EXPORT pixman_bool_t
179 pixman_image_unref (pixman_image_t *image)
181 image_common_t *common = (image_common_t *)image;
185 if (common->ref_count == 0)
187 if (image->common.destroy_func)
188 image->common.destroy_func (image, image->common.destroy_data);
190 pixman_region32_fini (&common->clip_region);
192 if (common->transform)
193 free (common->transform);
195 if (common->filter_params)
196 free (common->filter_params);
198 if (common->alpha_map)
199 pixman_image_unref ((pixman_image_t *)common->alpha_map);
201 if (image->type == LINEAR ||
202 image->type == RADIAL ||
203 image->type == CONICAL)
205 if (image->gradient.stops)
206 free (image->gradient.stops);
209 if (image->type == BITS && image->bits.free_me)
210 free (image->bits.free_me);
221 pixman_image_set_destroy_function (pixman_image_t * image,
222 pixman_image_destroy_func_t func,
225 image->common.destroy_func = func;
226 image->common.destroy_data = data;
230 pixman_image_get_destroy_data (pixman_image_t *image)
232 return image->common.destroy_data;
236 _pixman_image_reset_clip_region (pixman_image_t *image)
238 image->common.have_clip_region = FALSE;
241 static pixman_bool_t out_of_bounds_workaround = TRUE;
243 /* Old X servers rely on out-of-bounds accesses when they are asked
244 * to composite with a window as the source. They create a pixman image
245 * pointing to some bogus position in memory, but then they set a clip
246 * region to the position where the actual bits are.
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 by default we allow certain out-of-bound access
251 * to happen unless explicitly disabled.
253 * Fixed X servers should call this function to disable the workaround.
256 pixman_disable_out_of_bounds_workaround (void)
258 out_of_bounds_workaround = FALSE;
262 source_image_needs_out_of_bounds_workaround (bits_image_t *image)
264 if (image->common.clip_sources &&
265 image->common.repeat == PIXMAN_REPEAT_NONE &&
266 image->common.have_clip_region &&
267 out_of_bounds_workaround)
269 if (!image->common.client_clip)
271 /* There is no client clip, so if the clip region extends beyond the
272 * drawable geometry, it must be because the X server generated the
275 const pixman_box32_t *extents =
276 pixman_region32_extents (&image->common.clip_region);
278 if (extents->x1 >= 0 && extents->x2 <= image->width &&
279 extents->y1 >= 0 && extents->y2 <= image->height)
292 compute_image_info (pixman_image_t *image)
294 pixman_format_code_t code;
298 if (!image->common.transform)
300 flags |= (FAST_PATH_ID_TRANSFORM |
301 FAST_PATH_X_UNIT_POSITIVE |
302 FAST_PATH_Y_UNIT_ZERO |
303 FAST_PATH_AFFINE_TRANSFORM);
307 flags |= FAST_PATH_HAS_TRANSFORM;
309 if (image->common.transform->matrix[2][0] == 0 &&
310 image->common.transform->matrix[2][1] == 0 &&
311 image->common.transform->matrix[2][2] == pixman_fixed_1)
313 flags |= FAST_PATH_AFFINE_TRANSFORM;
315 if (image->common.transform->matrix[0][1] == 0 &&
316 image->common.transform->matrix[1][0] == 0)
318 flags |= FAST_PATH_SCALE_TRANSFORM;
322 if (image->common.transform->matrix[0][0] > 0)
323 flags |= FAST_PATH_X_UNIT_POSITIVE;
325 if (image->common.transform->matrix[1][0] == 0)
326 flags |= FAST_PATH_Y_UNIT_ZERO;
330 switch (image->common.filter)
332 case PIXMAN_FILTER_NEAREST:
333 case PIXMAN_FILTER_FAST:
334 flags |= (FAST_PATH_NEAREST_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
337 case PIXMAN_FILTER_BILINEAR:
338 case PIXMAN_FILTER_GOOD:
339 case PIXMAN_FILTER_BEST:
340 flags |= (FAST_PATH_BILINEAR_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
343 case PIXMAN_FILTER_CONVOLUTION:
347 flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
352 switch (image->common.repeat)
354 case PIXMAN_REPEAT_NONE:
356 FAST_PATH_NO_REFLECT_REPEAT |
357 FAST_PATH_NO_PAD_REPEAT |
358 FAST_PATH_NO_NORMAL_REPEAT;
361 case PIXMAN_REPEAT_REFLECT:
363 FAST_PATH_NO_PAD_REPEAT |
364 FAST_PATH_NO_NONE_REPEAT |
365 FAST_PATH_NO_NORMAL_REPEAT;
368 case PIXMAN_REPEAT_PAD:
370 FAST_PATH_NO_REFLECT_REPEAT |
371 FAST_PATH_NO_NONE_REPEAT |
372 FAST_PATH_NO_NORMAL_REPEAT;
377 FAST_PATH_NO_REFLECT_REPEAT |
378 FAST_PATH_NO_PAD_REPEAT |
379 FAST_PATH_NO_NONE_REPEAT;
383 /* Component alpha */
384 if (image->common.component_alpha)
385 flags |= FAST_PATH_COMPONENT_ALPHA;
387 flags |= FAST_PATH_UNIFIED_ALPHA;
389 flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NARROW_FORMAT);
391 /* Type specific checks */
397 if (image->solid.color.alpha == 0xffff)
398 flags |= FAST_PATH_IS_OPAQUE;
402 if (image->bits.width == 1 &&
403 image->bits.height == 1 &&
404 image->common.repeat != PIXMAN_REPEAT_NONE)
410 code = image->bits.format;
413 if (!PIXMAN_FORMAT_A (image->bits.format) &&
414 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY &&
415 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
417 flags |= FAST_PATH_SAMPLES_OPAQUE;
419 if (image->common.repeat != PIXMAN_REPEAT_NONE)
420 flags |= FAST_PATH_IS_OPAQUE;
423 if (source_image_needs_out_of_bounds_workaround (&image->bits))
424 flags |= FAST_PATH_NEEDS_WORKAROUND;
426 if (image->bits.read_func || image->bits.write_func)
427 flags &= ~FAST_PATH_NO_ACCESSORS;
429 if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
430 flags &= ~FAST_PATH_NARROW_FORMAT;
435 code = PIXMAN_unknown;
437 if (image->common.repeat != PIXMAN_REPEAT_NONE)
441 flags |= FAST_PATH_IS_OPAQUE;
442 for (i = 0; i < image->gradient.n_stops; ++i)
444 if (image->gradient.stops[i].color.alpha != 0xffff)
446 flags &= ~FAST_PATH_IS_OPAQUE;
454 code = PIXMAN_unknown;
459 if (!image->common.alpha_map)
461 flags |= FAST_PATH_NO_ALPHA_MAP;
465 if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
466 flags &= ~FAST_PATH_NARROW_FORMAT;
469 /* Both alpha maps and convolution filters can introduce
470 * non-opaqueness in otherwise opaque images. Also
471 * an image with component alpha turned on is only opaque
472 * if all channels are opaque, so we simply turn it off
473 * unconditionally for those images.
475 if (image->common.alpha_map ||
476 image->common.filter == PIXMAN_FILTER_CONVOLUTION ||
477 image->common.component_alpha)
479 flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
482 image->common.flags = flags;
483 image->common.extended_format_code = code;
487 _pixman_image_validate (pixman_image_t *image)
489 if (image->common.dirty)
491 compute_image_info (image);
493 /* It is important that property_changed is
494 * called *after* compute_image_info() because
495 * property_changed() can make use of the flags
496 * to set up accessors etc.
498 image->common.property_changed (image);
500 image->common.dirty = FALSE;
503 if (image->common.alpha_map)
504 _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
507 PIXMAN_EXPORT pixman_bool_t
508 pixman_image_set_clip_region32 (pixman_image_t * image,
509 pixman_region32_t *region)
511 image_common_t *common = (image_common_t *)image;
512 pixman_bool_t result;
516 if ((result = pixman_region32_copy (&common->clip_region, region)))
517 image->common.have_clip_region = TRUE;
521 _pixman_image_reset_clip_region (image);
526 image_property_changed (image);
531 PIXMAN_EXPORT pixman_bool_t
532 pixman_image_set_clip_region (pixman_image_t * image,
533 pixman_region16_t *region)
535 image_common_t *common = (image_common_t *)image;
536 pixman_bool_t result;
540 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
541 image->common.have_clip_region = TRUE;
545 _pixman_image_reset_clip_region (image);
550 image_property_changed (image);
556 pixman_image_set_has_client_clip (pixman_image_t *image,
557 pixman_bool_t client_clip)
559 image->common.client_clip = client_clip;
562 PIXMAN_EXPORT pixman_bool_t
563 pixman_image_set_transform (pixman_image_t * image,
564 const pixman_transform_t *transform)
566 static const pixman_transform_t id =
568 { { pixman_fixed_1, 0, 0 },
569 { 0, pixman_fixed_1, 0 },
570 { 0, 0, pixman_fixed_1 } }
573 image_common_t *common = (image_common_t *)image;
574 pixman_bool_t result;
576 if (common->transform == transform)
579 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
581 free (common->transform);
582 common->transform = NULL;
588 if (common->transform == NULL)
589 common->transform = malloc (sizeof (pixman_transform_t));
591 if (common->transform == NULL)
598 memcpy (common->transform, transform, sizeof(pixman_transform_t));
603 image_property_changed (image);
609 pixman_image_set_repeat (pixman_image_t *image,
610 pixman_repeat_t repeat)
612 image->common.repeat = repeat;
614 image_property_changed (image);
617 PIXMAN_EXPORT pixman_bool_t
618 pixman_image_set_filter (pixman_image_t * image,
619 pixman_filter_t filter,
620 const pixman_fixed_t *params,
623 image_common_t *common = (image_common_t *)image;
624 pixman_fixed_t *new_params;
626 if (params == common->filter_params && filter == common->filter)
632 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
637 params, n_params * sizeof (pixman_fixed_t));
640 common->filter = filter;
642 if (common->filter_params)
643 free (common->filter_params);
645 common->filter_params = new_params;
646 common->n_filter_params = n_params;
648 image_property_changed (image);
653 pixman_image_set_source_clipping (pixman_image_t *image,
654 pixman_bool_t clip_sources)
656 image->common.clip_sources = clip_sources;
658 image_property_changed (image);
661 /* Unlike all the other property setters, this function does not
662 * copy the content of indexed. Doing this copying is simply
663 * way, way too expensive.
666 pixman_image_set_indexed (pixman_image_t * image,
667 const pixman_indexed_t *indexed)
669 bits_image_t *bits = (bits_image_t *)image;
671 bits->indexed = indexed;
673 image_property_changed (image);
677 pixman_image_set_alpha_map (pixman_image_t *image,
678 pixman_image_t *alpha_map,
682 image_common_t *common = (image_common_t *)image;
684 return_if_fail (!alpha_map || alpha_map->type == BITS);
686 if (alpha_map && common->alpha_count > 0)
688 /* If this image is being used as an alpha map itself,
689 * then you can't give it an alpha map of its own.
694 if (alpha_map && alpha_map->common.alpha_map)
696 /* If the image has an alpha map of its own,
697 * then it can't be used as an alpha map itself
702 if (common->alpha_map != (bits_image_t *)alpha_map)
704 if (common->alpha_map)
706 common->alpha_map->common.alpha_count--;
708 pixman_image_unref ((pixman_image_t *)common->alpha_map);
713 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
715 common->alpha_map->common.alpha_count++;
719 common->alpha_map = NULL;
723 common->alpha_origin_x = x;
724 common->alpha_origin_y = y;
726 image_property_changed (image);
730 pixman_image_set_component_alpha (pixman_image_t *image,
731 pixman_bool_t component_alpha)
733 image->common.component_alpha = component_alpha;
735 image_property_changed (image);
738 PIXMAN_EXPORT pixman_bool_t
739 pixman_image_get_component_alpha (pixman_image_t *image)
741 return image->common.component_alpha;
745 pixman_image_set_accessors (pixman_image_t * image,
746 pixman_read_memory_func_t read_func,
747 pixman_write_memory_func_t write_func)
749 return_if_fail (image != NULL);
751 if (image->type == BITS)
753 image->bits.read_func = read_func;
754 image->bits.write_func = write_func;
756 image_property_changed (image);
760 PIXMAN_EXPORT uint32_t *
761 pixman_image_get_data (pixman_image_t *image)
763 if (image->type == BITS)
764 return image->bits.bits;
770 pixman_image_get_width (pixman_image_t *image)
772 if (image->type == BITS)
773 return image->bits.width;
779 pixman_image_get_height (pixman_image_t *image)
781 if (image->type == BITS)
782 return image->bits.height;
788 pixman_image_get_stride (pixman_image_t *image)
790 if (image->type == BITS)
791 return image->bits.rowstride * (int) sizeof (uint32_t);
797 pixman_image_get_depth (pixman_image_t *image)
799 if (image->type == BITS)
800 return PIXMAN_FORMAT_DEPTH (image->bits.format);
805 PIXMAN_EXPORT pixman_format_code_t
806 pixman_image_get_format (pixman_image_t *image)
808 if (image->type == BITS)
809 return image->bits.format;
815 _pixman_image_get_solid (pixman_image_t * image,
816 pixman_format_code_t format)
820 _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL);
822 /* If necessary, convert RGB <--> BGR. */
823 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
825 result = (((result & 0xff000000) >> 0) |
826 ((result & 0x00ff0000) >> 16) |
827 ((result & 0x0000ff00) >> 0) |
828 ((result & 0x000000ff) << 16));