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)
70 uint32_t *mask8 = NULL;
72 /* Contract the mask image, if one exists, so that the 32-bit fetch
73 * function can use it.
77 mask8 = pixman_malloc_ab (width, sizeof(uint32_t));
81 pixman_contract (mask8, (uint64_t *)mask, width);
84 /* Fetch the source image into the first half of buffer. */
85 _pixman_image_get_scanline_32 (image, x, y, width, (uint32_t*)buffer, mask8);
87 /* Expand from 32bpp to 64bpp in place. */
88 pixman_expand ((uint64_t *)buffer, buffer, PIXMAN_a8r8g8b8, width);
94 _pixman_image_allocate (void)
96 pixman_image_t *image = malloc (sizeof (pixman_image_t));
100 image_common_t *common = &image->common;
102 pixman_region32_init (&common->clip_region);
104 common->alpha_count = 0;
105 common->have_clip_region = FALSE;
106 common->clip_sources = FALSE;
107 common->transform = NULL;
108 common->repeat = PIXMAN_REPEAT_NONE;
109 common->filter = PIXMAN_FILTER_NEAREST;
110 common->filter_params = NULL;
111 common->n_filter_params = 0;
112 common->alpha_map = NULL;
113 common->component_alpha = FALSE;
114 common->ref_count = 1;
115 common->classify = NULL;
116 common->client_clip = FALSE;
117 common->destroy_func = NULL;
118 common->destroy_data = NULL;
119 common->dirty = TRUE;
126 _pixman_image_classify (pixman_image_t *image,
132 if (image->common.classify)
133 return image->common.classify (image, x, y, width, height);
135 return SOURCE_IMAGE_CLASS_UNKNOWN;
139 _pixman_image_get_scanline_32 (pixman_image_t *image,
144 const uint32_t *mask)
146 image->common.get_scanline_32 (image, x, y, width, buffer, mask);
149 /* Even thought the type of buffer is uint32_t *, the function actually expects
150 * a uint64_t *buffer.
153 _pixman_image_get_scanline_64 (pixman_image_t *image,
158 const uint32_t *unused)
160 image->common.get_scanline_64 (image, x, y, width, buffer, unused);
164 image_property_changed (pixman_image_t *image)
166 image->common.dirty = TRUE;
170 PIXMAN_EXPORT pixman_image_t *
171 pixman_image_ref (pixman_image_t *image)
173 image->common.ref_count++;
178 /* returns TRUE when the image is freed */
179 PIXMAN_EXPORT pixman_bool_t
180 pixman_image_unref (pixman_image_t *image)
182 image_common_t *common = (image_common_t *)image;
186 if (common->ref_count == 0)
188 if (image->common.destroy_func)
189 image->common.destroy_func (image, image->common.destroy_data);
191 pixman_region32_fini (&common->clip_region);
193 if (common->transform)
194 free (common->transform);
196 if (common->filter_params)
197 free (common->filter_params);
199 if (common->alpha_map)
200 pixman_image_unref ((pixman_image_t *)common->alpha_map);
202 if (image->type == LINEAR ||
203 image->type == RADIAL ||
204 image->type == CONICAL)
206 if (image->gradient.stops)
207 free (image->gradient.stops);
210 if (image->type == BITS && image->bits.free_me)
211 free (image->bits.free_me);
222 pixman_image_set_destroy_function (pixman_image_t * image,
223 pixman_image_destroy_func_t func,
226 image->common.destroy_func = func;
227 image->common.destroy_data = data;
231 pixman_image_get_destroy_data (pixman_image_t *image)
233 return image->common.destroy_data;
237 _pixman_image_reset_clip_region (pixman_image_t *image)
239 image->common.have_clip_region = FALSE;
242 static pixman_bool_t out_of_bounds_workaround = TRUE;
244 /* Old X servers rely on out-of-bounds accesses when they are asked
245 * to composite with a window as the source. They create a pixman image
246 * pointing to some bogus position in memory, but then they set a clip
247 * region to the position where the actual bits are.
249 * Due to a bug in old versions of pixman, where it would not clip
250 * against the image bounds when a clip region was set, this would
251 * actually work. So by default we allow certain out-of-bound access
252 * to happen unless explicitly disabled.
254 * Fixed X servers should call this function to disable the workaround.
257 pixman_disable_out_of_bounds_workaround (void)
259 out_of_bounds_workaround = FALSE;
263 source_image_needs_out_of_bounds_workaround (bits_image_t *image)
265 if (image->common.clip_sources &&
266 image->common.repeat == PIXMAN_REPEAT_NONE &&
267 image->common.have_clip_region &&
268 out_of_bounds_workaround)
270 if (!image->common.client_clip)
272 /* There is no client clip, so if the clip region extends beyond the
273 * drawable geometry, it must be because the X server generated the
276 const pixman_box32_t *extents =
277 pixman_region32_extents (&image->common.clip_region);
279 if (extents->x1 >= 0 && extents->x2 <= image->width &&
280 extents->y1 >= 0 && extents->y2 <= image->height)
293 compute_image_info (pixman_image_t *image)
295 pixman_format_code_t code;
299 if (!image->common.transform)
301 flags |= (FAST_PATH_ID_TRANSFORM |
302 FAST_PATH_X_UNIT_POSITIVE |
303 FAST_PATH_Y_UNIT_ZERO |
304 FAST_PATH_AFFINE_TRANSFORM);
308 flags |= FAST_PATH_HAS_TRANSFORM;
310 if (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_AFFINE_TRANSFORM;
316 if (image->common.transform->matrix[0][1] == 0 &&
317 image->common.transform->matrix[1][0] == 0)
319 flags |= FAST_PATH_SCALE_TRANSFORM;
323 if (image->common.transform->matrix[0][0] > 0)
324 flags |= FAST_PATH_X_UNIT_POSITIVE;
326 if (image->common.transform->matrix[1][0] == 0)
327 flags |= FAST_PATH_Y_UNIT_ZERO;
331 switch (image->common.filter)
333 case PIXMAN_FILTER_NEAREST:
334 case PIXMAN_FILTER_FAST:
335 flags |= (FAST_PATH_NEAREST_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
338 case PIXMAN_FILTER_BILINEAR:
339 case PIXMAN_FILTER_GOOD:
340 case PIXMAN_FILTER_BEST:
341 flags |= (FAST_PATH_BILINEAR_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
344 case PIXMAN_FILTER_CONVOLUTION:
348 flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
353 switch (image->common.repeat)
355 case PIXMAN_REPEAT_NONE:
357 FAST_PATH_NO_REFLECT_REPEAT |
358 FAST_PATH_NO_PAD_REPEAT |
359 FAST_PATH_NO_NORMAL_REPEAT;
362 case PIXMAN_REPEAT_REFLECT:
364 FAST_PATH_NO_PAD_REPEAT |
365 FAST_PATH_NO_NONE_REPEAT |
366 FAST_PATH_NO_NORMAL_REPEAT |
367 FAST_PATH_COVERS_CLIP;
370 case PIXMAN_REPEAT_PAD:
372 FAST_PATH_NO_REFLECT_REPEAT |
373 FAST_PATH_NO_NONE_REPEAT |
374 FAST_PATH_NO_NORMAL_REPEAT |
375 FAST_PATH_COVERS_CLIP;
380 FAST_PATH_NO_REFLECT_REPEAT |
381 FAST_PATH_NO_PAD_REPEAT |
382 FAST_PATH_NO_NONE_REPEAT |
383 FAST_PATH_COVERS_CLIP;
387 /* Component alpha */
388 if (image->common.component_alpha)
389 flags |= FAST_PATH_COMPONENT_ALPHA;
391 flags |= FAST_PATH_UNIFIED_ALPHA;
393 flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NARROW_FORMAT);
395 /* Type specific checks */
401 if (image->solid.color.alpha == 0xffff)
402 flags |= FAST_PATH_IS_OPAQUE;
404 flags |= FAST_PATH_COVERS_CLIP;
408 if (image->bits.width == 1 &&
409 image->bits.height == 1 &&
410 image->common.repeat != PIXMAN_REPEAT_NONE)
416 code = image->bits.format;
418 if (!image->common.transform &&
419 image->common.repeat == PIXMAN_REPEAT_NORMAL)
421 flags |= FAST_PATH_SIMPLE_REPEAT;
425 if (!PIXMAN_FORMAT_A (image->bits.format) &&
426 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY &&
427 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
429 flags |= FAST_PATH_SAMPLES_OPAQUE;
431 if (image->common.repeat != PIXMAN_REPEAT_NONE)
432 flags |= FAST_PATH_IS_OPAQUE;
435 if (source_image_needs_out_of_bounds_workaround (&image->bits))
436 flags |= FAST_PATH_NEEDS_WORKAROUND;
438 if (image->bits.read_func || image->bits.write_func)
439 flags &= ~FAST_PATH_NO_ACCESSORS;
441 if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
442 flags &= ~FAST_PATH_NARROW_FORMAT;
447 code = PIXMAN_unknown;
449 if (image->common.repeat != PIXMAN_REPEAT_NONE)
453 flags |= FAST_PATH_IS_OPAQUE;
454 for (i = 0; i < image->gradient.n_stops; ++i)
456 if (image->gradient.stops[i].color.alpha != 0xffff)
458 flags &= ~FAST_PATH_IS_OPAQUE;
466 code = PIXMAN_unknown;
471 if (!image->common.alpha_map)
473 flags |= FAST_PATH_NO_ALPHA_MAP;
477 if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
478 flags &= ~FAST_PATH_NARROW_FORMAT;
481 /* Both alpha maps and convolution filters can introduce
482 * non-opaqueness in otherwise opaque images. Also
483 * an image with component alpha turned on is only opaque
484 * if all channels are opaque, so we simply turn it off
485 * unconditionally for those images.
487 if (image->common.alpha_map ||
488 image->common.filter == PIXMAN_FILTER_CONVOLUTION ||
489 image->common.component_alpha)
491 flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
494 image->common.flags = flags;
495 image->common.extended_format_code = code;
499 _pixman_image_validate (pixman_image_t *image)
501 if (image->common.dirty)
503 compute_image_info (image);
505 /* It is important that property_changed is
506 * called *after* compute_image_info() because
507 * property_changed() can make use of the flags
508 * to set up accessors etc.
510 image->common.property_changed (image);
512 image->common.dirty = FALSE;
515 if (image->common.alpha_map)
516 _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
519 PIXMAN_EXPORT pixman_bool_t
520 pixman_image_set_clip_region32 (pixman_image_t * image,
521 pixman_region32_t *region)
523 image_common_t *common = (image_common_t *)image;
524 pixman_bool_t result;
528 if ((result = pixman_region32_copy (&common->clip_region, region)))
529 image->common.have_clip_region = TRUE;
533 _pixman_image_reset_clip_region (image);
538 image_property_changed (image);
543 PIXMAN_EXPORT pixman_bool_t
544 pixman_image_set_clip_region (pixman_image_t * image,
545 pixman_region16_t *region)
547 image_common_t *common = (image_common_t *)image;
548 pixman_bool_t result;
552 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
553 image->common.have_clip_region = TRUE;
557 _pixman_image_reset_clip_region (image);
562 image_property_changed (image);
568 pixman_image_set_has_client_clip (pixman_image_t *image,
569 pixman_bool_t client_clip)
571 image->common.client_clip = client_clip;
574 PIXMAN_EXPORT pixman_bool_t
575 pixman_image_set_transform (pixman_image_t * image,
576 const pixman_transform_t *transform)
578 static const pixman_transform_t id =
580 { { pixman_fixed_1, 0, 0 },
581 { 0, pixman_fixed_1, 0 },
582 { 0, 0, pixman_fixed_1 } }
585 image_common_t *common = (image_common_t *)image;
586 pixman_bool_t result;
588 if (common->transform == transform)
591 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
593 free (common->transform);
594 common->transform = NULL;
600 if (common->transform == NULL)
601 common->transform = malloc (sizeof (pixman_transform_t));
603 if (common->transform == NULL)
610 memcpy (common->transform, transform, sizeof(pixman_transform_t));
615 image_property_changed (image);
621 pixman_image_set_repeat (pixman_image_t *image,
622 pixman_repeat_t repeat)
624 image->common.repeat = repeat;
626 image_property_changed (image);
629 PIXMAN_EXPORT pixman_bool_t
630 pixman_image_set_filter (pixman_image_t * image,
631 pixman_filter_t filter,
632 const pixman_fixed_t *params,
635 image_common_t *common = (image_common_t *)image;
636 pixman_fixed_t *new_params;
638 if (params == common->filter_params && filter == common->filter)
644 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
649 params, n_params * sizeof (pixman_fixed_t));
652 common->filter = filter;
654 if (common->filter_params)
655 free (common->filter_params);
657 common->filter_params = new_params;
658 common->n_filter_params = n_params;
660 image_property_changed (image);
665 pixman_image_set_source_clipping (pixman_image_t *image,
666 pixman_bool_t clip_sources)
668 image->common.clip_sources = clip_sources;
670 image_property_changed (image);
673 /* Unlike all the other property setters, this function does not
674 * copy the content of indexed. Doing this copying is simply
675 * way, way too expensive.
678 pixman_image_set_indexed (pixman_image_t * image,
679 const pixman_indexed_t *indexed)
681 bits_image_t *bits = (bits_image_t *)image;
683 bits->indexed = indexed;
685 image_property_changed (image);
689 pixman_image_set_alpha_map (pixman_image_t *image,
690 pixman_image_t *alpha_map,
694 image_common_t *common = (image_common_t *)image;
696 return_if_fail (!alpha_map || alpha_map->type == BITS);
698 if (alpha_map && common->alpha_count > 0)
700 /* If this image is being used as an alpha map itself,
701 * then you can't give it an alpha map of its own.
706 if (alpha_map && alpha_map->common.alpha_map)
708 /* If the image has an alpha map of its own,
709 * then it can't be used as an alpha map itself
714 if (common->alpha_map != (bits_image_t *)alpha_map)
716 if (common->alpha_map)
718 common->alpha_map->common.alpha_count--;
720 pixman_image_unref ((pixman_image_t *)common->alpha_map);
725 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
727 common->alpha_map->common.alpha_count++;
731 common->alpha_map = NULL;
735 common->alpha_origin_x = x;
736 common->alpha_origin_y = y;
738 image_property_changed (image);
742 pixman_image_set_component_alpha (pixman_image_t *image,
743 pixman_bool_t component_alpha)
745 image->common.component_alpha = component_alpha;
747 image_property_changed (image);
750 PIXMAN_EXPORT pixman_bool_t
751 pixman_image_get_component_alpha (pixman_image_t *image)
753 return image->common.component_alpha;
757 pixman_image_set_accessors (pixman_image_t * image,
758 pixman_read_memory_func_t read_func,
759 pixman_write_memory_func_t write_func)
761 return_if_fail (image != NULL);
763 if (image->type == BITS)
765 image->bits.read_func = read_func;
766 image->bits.write_func = write_func;
768 image_property_changed (image);
772 PIXMAN_EXPORT uint32_t *
773 pixman_image_get_data (pixman_image_t *image)
775 if (image->type == BITS)
776 return image->bits.bits;
782 pixman_image_get_width (pixman_image_t *image)
784 if (image->type == BITS)
785 return image->bits.width;
791 pixman_image_get_height (pixman_image_t *image)
793 if (image->type == BITS)
794 return image->bits.height;
800 pixman_image_get_stride (pixman_image_t *image)
802 if (image->type == BITS)
803 return image->bits.rowstride * (int) sizeof (uint32_t);
809 pixman_image_get_depth (pixman_image_t *image)
811 if (image->type == BITS)
812 return PIXMAN_FORMAT_DEPTH (image->bits.format);
817 PIXMAN_EXPORT pixman_format_code_t
818 pixman_image_get_format (pixman_image_t *image)
820 if (image->type == BITS)
821 return image->bits.format;
827 _pixman_image_get_solid (pixman_image_t * image,
828 pixman_format_code_t format)
832 _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL);
834 /* If necessary, convert RGB <--> BGR. */
835 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
837 result = (((result & 0xff000000) >> 0) |
838 ((result & 0x00ff0000) >> 16) |
839 ((result & 0x0000ff00) >> 0) |
840 ((result & 0x000000ff) << 16));