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 /* Executive Summary: This function is a no-op that only exists
242 * for historical reasons.
244 * There used to be a bug in the X server where it would rely on
245 * out-of-bounds accesses when it was asked to composite with a
246 * window as the source. It would create a pixman image pointing
247 * to some bogus position in memory, but then set a clip region
248 * to the position where the actual bits were.
250 * Due to a bug in old versions of pixman, where it would not clip
251 * against the image bounds when a clip region was set, this would
252 * actually work. So when the pixman bug was fixed, a workaround was
253 * added to allow certain out-of-bound accesses. This function disabled
256 * Since 0.21.2, pixman doesn't do these workarounds anymore, so now
257 * this function is a no-op.
260 pixman_disable_out_of_bounds_workaround (void)
265 compute_image_info (pixman_image_t *image)
267 pixman_format_code_t code;
271 if (!image->common.transform)
273 flags |= (FAST_PATH_ID_TRANSFORM |
274 FAST_PATH_X_UNIT_POSITIVE |
275 FAST_PATH_Y_UNIT_ZERO |
276 FAST_PATH_AFFINE_TRANSFORM);
280 flags |= FAST_PATH_HAS_TRANSFORM;
282 if (image->common.transform->matrix[2][0] == 0 &&
283 image->common.transform->matrix[2][1] == 0 &&
284 image->common.transform->matrix[2][2] == pixman_fixed_1)
286 flags |= FAST_PATH_AFFINE_TRANSFORM;
288 if (image->common.transform->matrix[0][1] == 0 &&
289 image->common.transform->matrix[1][0] == 0)
291 flags |= FAST_PATH_SCALE_TRANSFORM;
295 if (image->common.transform->matrix[0][0] > 0)
296 flags |= FAST_PATH_X_UNIT_POSITIVE;
298 if (image->common.transform->matrix[1][0] == 0)
299 flags |= FAST_PATH_Y_UNIT_ZERO;
303 switch (image->common.filter)
305 case PIXMAN_FILTER_NEAREST:
306 case PIXMAN_FILTER_FAST:
307 flags |= (FAST_PATH_NEAREST_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
310 case PIXMAN_FILTER_BILINEAR:
311 case PIXMAN_FILTER_GOOD:
312 case PIXMAN_FILTER_BEST:
313 flags |= (FAST_PATH_BILINEAR_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
316 case PIXMAN_FILTER_CONVOLUTION:
320 flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
325 switch (image->common.repeat)
327 case PIXMAN_REPEAT_NONE:
329 FAST_PATH_NO_REFLECT_REPEAT |
330 FAST_PATH_NO_PAD_REPEAT |
331 FAST_PATH_NO_NORMAL_REPEAT;
334 case PIXMAN_REPEAT_REFLECT:
336 FAST_PATH_NO_PAD_REPEAT |
337 FAST_PATH_NO_NONE_REPEAT |
338 FAST_PATH_NO_NORMAL_REPEAT;
341 case PIXMAN_REPEAT_PAD:
343 FAST_PATH_NO_REFLECT_REPEAT |
344 FAST_PATH_NO_NONE_REPEAT |
345 FAST_PATH_NO_NORMAL_REPEAT;
350 FAST_PATH_NO_REFLECT_REPEAT |
351 FAST_PATH_NO_PAD_REPEAT |
352 FAST_PATH_NO_NONE_REPEAT;
356 /* Component alpha */
357 if (image->common.component_alpha)
358 flags |= FAST_PATH_COMPONENT_ALPHA;
360 flags |= FAST_PATH_UNIFIED_ALPHA;
362 flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NARROW_FORMAT);
364 /* Type specific checks */
370 if (image->solid.color.alpha == 0xffff)
371 flags |= FAST_PATH_IS_OPAQUE;
375 if (image->bits.width == 1 &&
376 image->bits.height == 1 &&
377 image->common.repeat != PIXMAN_REPEAT_NONE)
383 code = image->bits.format;
386 if (!PIXMAN_FORMAT_A (image->bits.format) &&
387 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY &&
388 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
390 flags |= FAST_PATH_SAMPLES_OPAQUE;
392 if (image->common.repeat != PIXMAN_REPEAT_NONE)
393 flags |= FAST_PATH_IS_OPAQUE;
396 if (image->bits.read_func || image->bits.write_func)
397 flags &= ~FAST_PATH_NO_ACCESSORS;
399 if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
400 flags &= ~FAST_PATH_NARROW_FORMAT;
405 code = PIXMAN_unknown;
407 if (image->common.repeat != PIXMAN_REPEAT_NONE)
411 flags |= FAST_PATH_IS_OPAQUE;
412 for (i = 0; i < image->gradient.n_stops; ++i)
414 if (image->gradient.stops[i].color.alpha != 0xffff)
416 flags &= ~FAST_PATH_IS_OPAQUE;
424 code = PIXMAN_unknown;
429 if (!image->common.alpha_map)
431 flags |= FAST_PATH_NO_ALPHA_MAP;
435 if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
436 flags &= ~FAST_PATH_NARROW_FORMAT;
439 /* Both alpha maps and convolution filters can introduce
440 * non-opaqueness in otherwise opaque images. Also
441 * an image with component alpha turned on is only opaque
442 * if all channels are opaque, so we simply turn it off
443 * unconditionally for those images.
445 if (image->common.alpha_map ||
446 image->common.filter == PIXMAN_FILTER_CONVOLUTION ||
447 image->common.component_alpha)
449 flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
452 image->common.flags = flags;
453 image->common.extended_format_code = code;
457 _pixman_image_validate (pixman_image_t *image)
459 if (image->common.dirty)
461 compute_image_info (image);
463 /* It is important that property_changed is
464 * called *after* compute_image_info() because
465 * property_changed() can make use of the flags
466 * to set up accessors etc.
468 image->common.property_changed (image);
470 image->common.dirty = FALSE;
473 if (image->common.alpha_map)
474 _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
477 PIXMAN_EXPORT pixman_bool_t
478 pixman_image_set_clip_region32 (pixman_image_t * image,
479 pixman_region32_t *region)
481 image_common_t *common = (image_common_t *)image;
482 pixman_bool_t result;
486 if ((result = pixman_region32_copy (&common->clip_region, region)))
487 image->common.have_clip_region = TRUE;
491 _pixman_image_reset_clip_region (image);
496 image_property_changed (image);
501 PIXMAN_EXPORT pixman_bool_t
502 pixman_image_set_clip_region (pixman_image_t * image,
503 pixman_region16_t *region)
505 image_common_t *common = (image_common_t *)image;
506 pixman_bool_t result;
510 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
511 image->common.have_clip_region = TRUE;
515 _pixman_image_reset_clip_region (image);
520 image_property_changed (image);
526 pixman_image_set_has_client_clip (pixman_image_t *image,
527 pixman_bool_t client_clip)
529 image->common.client_clip = client_clip;
532 PIXMAN_EXPORT pixman_bool_t
533 pixman_image_set_transform (pixman_image_t * image,
534 const pixman_transform_t *transform)
536 static const pixman_transform_t id =
538 { { pixman_fixed_1, 0, 0 },
539 { 0, pixman_fixed_1, 0 },
540 { 0, 0, pixman_fixed_1 } }
543 image_common_t *common = (image_common_t *)image;
544 pixman_bool_t result;
546 if (common->transform == transform)
549 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
551 free (common->transform);
552 common->transform = NULL;
558 if (common->transform == NULL)
559 common->transform = malloc (sizeof (pixman_transform_t));
561 if (common->transform == NULL)
568 memcpy (common->transform, transform, sizeof(pixman_transform_t));
573 image_property_changed (image);
579 pixman_image_set_repeat (pixman_image_t *image,
580 pixman_repeat_t repeat)
582 image->common.repeat = repeat;
584 image_property_changed (image);
587 PIXMAN_EXPORT pixman_bool_t
588 pixman_image_set_filter (pixman_image_t * image,
589 pixman_filter_t filter,
590 const pixman_fixed_t *params,
593 image_common_t *common = (image_common_t *)image;
594 pixman_fixed_t *new_params;
596 if (params == common->filter_params && filter == common->filter)
602 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
607 params, n_params * sizeof (pixman_fixed_t));
610 common->filter = filter;
612 if (common->filter_params)
613 free (common->filter_params);
615 common->filter_params = new_params;
616 common->n_filter_params = n_params;
618 image_property_changed (image);
623 pixman_image_set_source_clipping (pixman_image_t *image,
624 pixman_bool_t clip_sources)
626 image->common.clip_sources = clip_sources;
628 image_property_changed (image);
631 /* Unlike all the other property setters, this function does not
632 * copy the content of indexed. Doing this copying is simply
633 * way, way too expensive.
636 pixman_image_set_indexed (pixman_image_t * image,
637 const pixman_indexed_t *indexed)
639 bits_image_t *bits = (bits_image_t *)image;
641 bits->indexed = indexed;
643 image_property_changed (image);
647 pixman_image_set_alpha_map (pixman_image_t *image,
648 pixman_image_t *alpha_map,
652 image_common_t *common = (image_common_t *)image;
654 return_if_fail (!alpha_map || alpha_map->type == BITS);
656 if (alpha_map && common->alpha_count > 0)
658 /* If this image is being used as an alpha map itself,
659 * then you can't give it an alpha map of its own.
664 if (alpha_map && alpha_map->common.alpha_map)
666 /* If the image has an alpha map of its own,
667 * then it can't be used as an alpha map itself
672 if (common->alpha_map != (bits_image_t *)alpha_map)
674 if (common->alpha_map)
676 common->alpha_map->common.alpha_count--;
678 pixman_image_unref ((pixman_image_t *)common->alpha_map);
683 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
685 common->alpha_map->common.alpha_count++;
689 common->alpha_map = NULL;
693 common->alpha_origin_x = x;
694 common->alpha_origin_y = y;
696 image_property_changed (image);
700 pixman_image_set_component_alpha (pixman_image_t *image,
701 pixman_bool_t component_alpha)
703 image->common.component_alpha = component_alpha;
705 image_property_changed (image);
708 PIXMAN_EXPORT pixman_bool_t
709 pixman_image_get_component_alpha (pixman_image_t *image)
711 return image->common.component_alpha;
715 pixman_image_set_accessors (pixman_image_t * image,
716 pixman_read_memory_func_t read_func,
717 pixman_write_memory_func_t write_func)
719 return_if_fail (image != NULL);
721 if (image->type == BITS)
723 image->bits.read_func = read_func;
724 image->bits.write_func = write_func;
726 image_property_changed (image);
730 PIXMAN_EXPORT uint32_t *
731 pixman_image_get_data (pixman_image_t *image)
733 if (image->type == BITS)
734 return image->bits.bits;
740 pixman_image_get_width (pixman_image_t *image)
742 if (image->type == BITS)
743 return image->bits.width;
749 pixman_image_get_height (pixman_image_t *image)
751 if (image->type == BITS)
752 return image->bits.height;
758 pixman_image_get_stride (pixman_image_t *image)
760 if (image->type == BITS)
761 return image->bits.rowstride * (int) sizeof (uint32_t);
767 pixman_image_get_depth (pixman_image_t *image)
769 if (image->type == BITS)
770 return PIXMAN_FORMAT_DEPTH (image->bits.format);
775 PIXMAN_EXPORT pixman_format_code_t
776 pixman_image_get_format (pixman_image_t *image)
778 if (image->type == BITS)
779 return image->bits.format;
785 _pixman_image_get_solid (pixman_image_t * image,
786 pixman_format_code_t format)
790 _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL);
792 /* If necessary, convert RGB <--> BGR. */
793 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
795 result = (((result & 0xff000000) >> 0) |
796 ((result & 0x00ff0000) >> 16) |
797 ((result & 0x0000ff00) >> 0) |
798 ((result & 0x000000ff) << 16));