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;
51 gradient->common.class = SOURCE_IMAGE_CLASS_UNKNOWN;
57 * By default, just evaluate the image at 32bpp and expand. Individual image
58 * types can plug in a better scanline getter if they want to. For example
59 * we could produce smoother gradients by evaluating them at higher color
60 * depth, but that's a project for the future.
63 _pixman_image_get_scanline_generic_64 (pixman_image_t * image,
68 const uint32_t * mask,
71 uint32_t *mask8 = NULL;
73 /* Contract the mask image, if one exists, so that the 32-bit fetch
74 * function can use it.
78 mask8 = pixman_malloc_ab (width, sizeof(uint32_t));
82 pixman_contract (mask8, (uint64_t *)mask, width);
85 /* Fetch the source image into the first half of buffer. */
86 _pixman_image_get_scanline_32 (image, x, y, width, (uint32_t*)buffer, mask8,
89 /* Expand from 32bpp to 64bpp in place. */
90 pixman_expand ((uint64_t *)buffer, buffer, PIXMAN_a8r8g8b8, width);
96 _pixman_image_allocate (void)
98 pixman_image_t *image = malloc (sizeof (pixman_image_t));
102 image_common_t *common = &image->common;
104 pixman_region32_init (&common->clip_region);
106 common->have_clip_region = FALSE;
107 common->clip_sources = FALSE;
108 common->transform = NULL;
109 common->repeat = PIXMAN_REPEAT_NONE;
110 common->filter = PIXMAN_FILTER_NEAREST;
111 common->filter_params = NULL;
112 common->n_filter_params = 0;
113 common->alpha_map = NULL;
114 common->component_alpha = FALSE;
115 common->ref_count = 1;
116 common->classify = NULL;
117 common->client_clip = FALSE;
118 common->destroy_func = NULL;
119 common->destroy_data = NULL;
120 common->dirty = TRUE;
127 _pixman_image_classify (pixman_image_t *image,
133 if (image->common.classify)
134 return image->common.classify (image, x, y, width, height);
136 return SOURCE_IMAGE_CLASS_UNKNOWN;
140 _pixman_image_get_scanline_32 (pixman_image_t *image,
145 const uint32_t *mask,
148 image->common.get_scanline_32 (image, x, y, width, buffer, mask, mask_bits);
151 /* Even thought the type of buffer is uint32_t *, the function actually expects
152 * a uint64_t *buffer.
155 _pixman_image_get_scanline_64 (pixman_image_t *image,
160 const uint32_t *unused,
163 image->common.get_scanline_64 (image, x, y, width, buffer, unused, unused2);
167 image_property_changed (pixman_image_t *image)
169 image->common.dirty = TRUE;
173 PIXMAN_EXPORT pixman_image_t *
174 pixman_image_ref (pixman_image_t *image)
176 image->common.ref_count++;
181 /* returns TRUE when the image is freed */
182 PIXMAN_EXPORT pixman_bool_t
183 pixman_image_unref (pixman_image_t *image)
185 image_common_t *common = (image_common_t *)image;
189 if (common->ref_count == 0)
191 if (image->common.destroy_func)
192 image->common.destroy_func (image, image->common.destroy_data);
194 pixman_region32_fini (&common->clip_region);
196 if (common->transform)
197 free (common->transform);
199 if (common->filter_params)
200 free (common->filter_params);
202 if (common->alpha_map)
203 pixman_image_unref ((pixman_image_t *)common->alpha_map);
205 if (image->type == LINEAR ||
206 image->type == RADIAL ||
207 image->type == CONICAL)
209 if (image->gradient.stops)
210 free (image->gradient.stops);
213 if (image->type == BITS && image->bits.free_me)
214 free (image->bits.free_me);
225 pixman_image_set_destroy_function (pixman_image_t * image,
226 pixman_image_destroy_func_t func,
229 image->common.destroy_func = func;
230 image->common.destroy_data = data;
234 pixman_image_get_destroy_data (pixman_image_t *image)
236 return image->common.destroy_data;
240 _pixman_image_reset_clip_region (pixman_image_t *image)
242 image->common.have_clip_region = FALSE;
245 static pixman_bool_t out_of_bounds_workaround = TRUE;
247 /* Old X servers rely on out-of-bounds accesses when they are asked
248 * to composite with a window as the source. They create a pixman image
249 * pointing to some bogus position in memory, but then they set a clip
250 * region to the position where the actual bits are.
252 * Due to a bug in old versions of pixman, where it would not clip
253 * against the image bounds when a clip region was set, this would
254 * actually work. So by default we allow certain out-of-bound access
255 * to happen unless explicitly disabled.
257 * Fixed X servers should call this function to disable the workaround.
260 pixman_disable_out_of_bounds_workaround (void)
262 out_of_bounds_workaround = FALSE;
266 source_image_needs_out_of_bounds_workaround (bits_image_t *image)
268 if (image->common.clip_sources &&
269 image->common.repeat == PIXMAN_REPEAT_NONE &&
270 image->common.have_clip_region &&
271 out_of_bounds_workaround)
273 if (!image->common.client_clip)
275 /* There is no client clip, so if the clip region extends beyond the
276 * drawable geometry, it must be because the X server generated the
279 const pixman_box32_t *extents =
280 pixman_region32_extents (&image->common.clip_region);
282 if (extents->x1 >= 0 && extents->x2 <= image->width &&
283 extents->y1 >= 0 && extents->y2 <= image->height)
296 compute_image_info (pixman_image_t *image)
298 pixman_format_code_t code;
302 if (!image->common.transform)
304 flags |= (FAST_PATH_ID_TRANSFORM | FAST_PATH_X_UNIT_POSITIVE);
308 if (image->common.transform->matrix[0][1] == 0 &&
309 image->common.transform->matrix[1][0] == 0 &&
310 image->common.transform->matrix[2][0] == 0 &&
311 image->common.transform->matrix[2][1] == 0 &&
312 image->common.transform->matrix[2][2] == pixman_fixed_1)
314 flags |= FAST_PATH_SCALE_TRANSFORM;
317 if (image->common.transform->matrix[0][0] > 0)
318 flags |= FAST_PATH_X_UNIT_POSITIVE;
322 if (!image->common.alpha_map)
323 flags |= FAST_PATH_NO_ALPHA_MAP;
326 switch (image->common.filter)
328 case PIXMAN_FILTER_NEAREST:
329 case PIXMAN_FILTER_FAST:
330 flags |= (FAST_PATH_NEAREST_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
333 case PIXMAN_FILTER_CONVOLUTION:
337 flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
342 switch (image->common.repeat)
344 case PIXMAN_REPEAT_NONE:
345 flags |= FAST_PATH_NO_REFLECT_REPEAT | FAST_PATH_NO_PAD_REPEAT;
348 case PIXMAN_REPEAT_REFLECT:
349 flags |= FAST_PATH_NO_PAD_REPEAT | FAST_PATH_NO_NONE_REPEAT;
352 case PIXMAN_REPEAT_PAD:
353 flags |= FAST_PATH_NO_REFLECT_REPEAT | FAST_PATH_NO_NONE_REPEAT;
357 flags |= FAST_PATH_NO_REFLECT_REPEAT | FAST_PATH_NO_PAD_REPEAT | FAST_PATH_NO_NONE_REPEAT;
361 /* Component alpha */
362 if (image->common.component_alpha)
363 flags |= FAST_PATH_COMPONENT_ALPHA;
365 flags |= FAST_PATH_UNIFIED_ALPHA;
367 flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NO_WIDE_FORMAT);
369 /* Type specific checks */
375 if (image->solid.color.alpha == 0xffff)
376 flags |= FAST_PATH_IS_OPAQUE;
380 if (image->bits.width == 1 &&
381 image->bits.height == 1 &&
382 image->common.repeat != PIXMAN_REPEAT_NONE)
388 code = image->bits.format;
390 if (!image->common.transform &&
391 image->common.repeat == PIXMAN_REPEAT_NORMAL)
393 flags |= FAST_PATH_SIMPLE_REPEAT;
397 if (image->common.repeat != PIXMAN_REPEAT_NONE &&
398 !PIXMAN_FORMAT_A (image->bits.format))
400 flags |= FAST_PATH_IS_OPAQUE;
403 if (source_image_needs_out_of_bounds_workaround (&image->bits))
404 flags |= FAST_PATH_NEEDS_WORKAROUND;
406 if (image->bits.read_func || image->bits.write_func)
407 flags &= ~FAST_PATH_NO_ACCESSORS;
409 if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
410 flags &= ~FAST_PATH_NO_WIDE_FORMAT;
415 code = PIXMAN_unknown;
417 if (image->common.repeat != PIXMAN_REPEAT_NONE)
421 flags |= FAST_PATH_IS_OPAQUE;
422 for (i = 0; i < image->gradient.n_stops; ++i)
424 if (image->gradient.stops[i].color.alpha != 0xffff)
426 flags &= ~FAST_PATH_IS_OPAQUE;
434 code = PIXMAN_unknown;
438 /* Both alpha maps and convolution filters can introduce
439 * non-opaqueness in otherwise opaque images. Also
440 * an image with component alpha turned on is only opaque
441 * if all channels are opaque, so we simply turn it off
442 * unconditionally for those images.
444 if (image->common.alpha_map ||
445 image->common.filter == PIXMAN_FILTER_CONVOLUTION ||
446 image->common.component_alpha)
448 flags &= ~FAST_PATH_IS_OPAQUE;
451 image->common.flags = flags;
452 image->common.extended_format_code = code;
456 _pixman_image_validate (pixman_image_t *image)
458 if (image->common.dirty)
460 compute_image_info (image);
462 /* It is important that property_changed is
463 * called *after* compute_image_info() because
464 * property_changed() can make use of the flags
465 * to set up accessors etc.
467 image->common.property_changed (image);
469 image->common.dirty = FALSE;
472 if (image->common.alpha_map)
473 _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
476 PIXMAN_EXPORT pixman_bool_t
477 pixman_image_set_clip_region32 (pixman_image_t * image,
478 pixman_region32_t *region)
480 image_common_t *common = (image_common_t *)image;
481 pixman_bool_t result;
485 if ((result = pixman_region32_copy (&common->clip_region, region)))
486 image->common.have_clip_region = TRUE;
490 _pixman_image_reset_clip_region (image);
495 image_property_changed (image);
500 PIXMAN_EXPORT pixman_bool_t
501 pixman_image_set_clip_region (pixman_image_t * image,
502 pixman_region16_t *region)
504 image_common_t *common = (image_common_t *)image;
505 pixman_bool_t result;
509 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
510 image->common.have_clip_region = TRUE;
514 _pixman_image_reset_clip_region (image);
519 image_property_changed (image);
525 pixman_image_set_has_client_clip (pixman_image_t *image,
526 pixman_bool_t client_clip)
528 image->common.client_clip = client_clip;
531 PIXMAN_EXPORT pixman_bool_t
532 pixman_image_set_transform (pixman_image_t * image,
533 const pixman_transform_t *transform)
535 static const pixman_transform_t id =
537 { { pixman_fixed_1, 0, 0 },
538 { 0, pixman_fixed_1, 0 },
539 { 0, 0, pixman_fixed_1 } }
542 image_common_t *common = (image_common_t *)image;
543 pixman_bool_t result;
545 if (common->transform == transform)
548 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
550 free (common->transform);
551 common->transform = NULL;
557 if (common->transform == NULL)
558 common->transform = malloc (sizeof (pixman_transform_t));
560 if (common->transform == NULL)
567 memcpy (common->transform, transform, sizeof(pixman_transform_t));
572 image_property_changed (image);
578 pixman_image_set_repeat (pixman_image_t *image,
579 pixman_repeat_t repeat)
581 image->common.repeat = repeat;
583 image_property_changed (image);
586 PIXMAN_EXPORT pixman_bool_t
587 pixman_image_set_filter (pixman_image_t * image,
588 pixman_filter_t filter,
589 const pixman_fixed_t *params,
592 image_common_t *common = (image_common_t *)image;
593 pixman_fixed_t *new_params;
595 if (params == common->filter_params && filter == common->filter)
601 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
606 params, n_params * sizeof (pixman_fixed_t));
609 common->filter = filter;
611 if (common->filter_params)
612 free (common->filter_params);
614 common->filter_params = new_params;
615 common->n_filter_params = n_params;
617 image_property_changed (image);
622 pixman_image_set_source_clipping (pixman_image_t *image,
623 pixman_bool_t clip_sources)
625 image->common.clip_sources = clip_sources;
627 image_property_changed (image);
630 /* Unlike all the other property setters, this function does not
631 * copy the content of indexed. Doing this copying is simply
632 * way, way too expensive.
635 pixman_image_set_indexed (pixman_image_t * image,
636 const pixman_indexed_t *indexed)
638 bits_image_t *bits = (bits_image_t *)image;
640 bits->indexed = indexed;
642 image_property_changed (image);
646 pixman_image_set_alpha_map (pixman_image_t *image,
647 pixman_image_t *alpha_map,
651 image_common_t *common = (image_common_t *)image;
653 return_if_fail (!alpha_map || alpha_map->type == BITS);
655 if (common->alpha_map != (bits_image_t *)alpha_map)
657 if (common->alpha_map)
658 pixman_image_unref ((pixman_image_t *)common->alpha_map);
661 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
663 common->alpha_map = NULL;
666 common->alpha_origin_x = x;
667 common->alpha_origin_y = y;
669 image_property_changed (image);
673 pixman_image_set_component_alpha (pixman_image_t *image,
674 pixman_bool_t component_alpha)
676 image->common.component_alpha = component_alpha;
678 image_property_changed (image);
682 pixman_image_set_accessors (pixman_image_t * image,
683 pixman_read_memory_func_t read_func,
684 pixman_write_memory_func_t write_func)
686 return_if_fail (image != NULL);
688 if (image->type == BITS)
690 image->bits.read_func = read_func;
691 image->bits.write_func = write_func;
693 image_property_changed (image);
697 PIXMAN_EXPORT uint32_t *
698 pixman_image_get_data (pixman_image_t *image)
700 if (image->type == BITS)
701 return image->bits.bits;
707 pixman_image_get_width (pixman_image_t *image)
709 if (image->type == BITS)
710 return image->bits.width;
716 pixman_image_get_height (pixman_image_t *image)
718 if (image->type == BITS)
719 return image->bits.height;
725 pixman_image_get_stride (pixman_image_t *image)
727 if (image->type == BITS)
728 return image->bits.rowstride * (int) sizeof (uint32_t);
734 pixman_image_get_depth (pixman_image_t *image)
736 if (image->type == BITS)
737 return PIXMAN_FORMAT_DEPTH (image->bits.format);
743 _pixman_image_get_solid (pixman_image_t * image,
744 pixman_format_code_t format)
748 _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL, 0);
750 /* If necessary, convert RGB <--> BGR. */
751 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
753 result = (((result & 0xff000000) >> 0) |
754 ((result & 0x00ff0000) >> 16) |
755 ((result & 0x0000ff00) >> 0) |
756 ((result & 0x000000ff) << 16));