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"
35 _pixman_init_gradient (gradient_t * gradient,
36 const pixman_gradient_stop_t *stops,
39 return_val_if_fail (n_stops > 0, FALSE);
41 gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
45 memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
47 gradient->n_stops = n_stops;
53 _pixman_image_allocate (void)
55 pixman_image_t *image = malloc (sizeof (pixman_image_t));
59 image_common_t *common = &image->common;
61 pixman_region32_init (&common->clip_region);
63 common->alpha_count = 0;
64 common->have_clip_region = FALSE;
65 common->clip_sources = FALSE;
66 common->transform = NULL;
67 common->repeat = PIXMAN_REPEAT_NONE;
68 common->filter = PIXMAN_FILTER_NEAREST;
69 common->filter_params = NULL;
70 common->n_filter_params = 0;
71 common->alpha_map = NULL;
72 common->component_alpha = FALSE;
73 common->ref_count = 1;
74 common->property_changed = NULL;
75 common->client_clip = FALSE;
76 common->destroy_func = NULL;
77 common->destroy_data = NULL;
85 image_property_changed (pixman_image_t *image)
87 image->common.dirty = TRUE;
91 PIXMAN_EXPORT pixman_image_t *
92 pixman_image_ref (pixman_image_t *image)
94 image->common.ref_count++;
99 /* returns TRUE when the image is freed */
100 PIXMAN_EXPORT pixman_bool_t
101 pixman_image_unref (pixman_image_t *image)
103 image_common_t *common = (image_common_t *)image;
107 if (common->ref_count == 0)
109 if (image->common.destroy_func)
110 image->common.destroy_func (image, image->common.destroy_data);
112 pixman_region32_fini (&common->clip_region);
114 if (common->transform)
115 free (common->transform);
117 if (common->filter_params)
118 free (common->filter_params);
120 if (common->alpha_map)
121 pixman_image_unref ((pixman_image_t *)common->alpha_map);
123 if (image->type == LINEAR ||
124 image->type == RADIAL ||
125 image->type == CONICAL)
127 if (image->gradient.stops)
128 free (image->gradient.stops);
131 if (image->type == BITS && image->bits.free_me)
132 free (image->bits.free_me);
143 pixman_image_set_destroy_function (pixman_image_t * image,
144 pixman_image_destroy_func_t func,
147 image->common.destroy_func = func;
148 image->common.destroy_data = data;
152 pixman_image_get_destroy_data (pixman_image_t *image)
154 return image->common.destroy_data;
158 _pixman_image_reset_clip_region (pixman_image_t *image)
160 image->common.have_clip_region = FALSE;
163 /* Executive Summary: This function is a no-op that only exists
164 * for historical reasons.
166 * There used to be a bug in the X server where it would rely on
167 * out-of-bounds accesses when it was asked to composite with a
168 * window as the source. It would create a pixman image pointing
169 * to some bogus position in memory, but then set a clip region
170 * to the position where the actual bits were.
172 * Due to a bug in old versions of pixman, where it would not clip
173 * against the image bounds when a clip region was set, this would
174 * actually work. So when the pixman bug was fixed, a workaround was
175 * added to allow certain out-of-bound accesses. This function disabled
178 * Since 0.21.2, pixman doesn't do these workarounds anymore, so now
179 * this function is a no-op.
182 pixman_disable_out_of_bounds_workaround (void)
187 compute_image_info (pixman_image_t *image)
189 pixman_format_code_t code;
193 if (!image->common.transform)
195 flags |= (FAST_PATH_ID_TRANSFORM |
196 FAST_PATH_X_UNIT_POSITIVE |
197 FAST_PATH_Y_UNIT_ZERO |
198 FAST_PATH_AFFINE_TRANSFORM);
202 flags |= FAST_PATH_HAS_TRANSFORM;
204 if (image->common.transform->matrix[2][0] == 0 &&
205 image->common.transform->matrix[2][1] == 0 &&
206 image->common.transform->matrix[2][2] == pixman_fixed_1)
208 flags |= FAST_PATH_AFFINE_TRANSFORM;
210 if (image->common.transform->matrix[0][1] == 0 &&
211 image->common.transform->matrix[1][0] == 0)
213 if (image->common.transform->matrix[0][0] == -pixman_fixed_1 &&
214 image->common.transform->matrix[1][1] == -pixman_fixed_1)
216 flags |= FAST_PATH_ROTATE_180_TRANSFORM;
218 flags |= FAST_PATH_SCALE_TRANSFORM;
220 else if (image->common.transform->matrix[0][0] == 0 &&
221 image->common.transform->matrix[1][1] == 0)
223 pixman_fixed_t m01 = image->common.transform->matrix[0][1];
224 if (m01 == -image->common.transform->matrix[1][0])
226 if (m01 == -pixman_fixed_1)
227 flags |= FAST_PATH_ROTATE_90_TRANSFORM;
228 else if (m01 == pixman_fixed_1)
229 flags |= FAST_PATH_ROTATE_270_TRANSFORM;
234 if (image->common.transform->matrix[0][0] > 0)
235 flags |= FAST_PATH_X_UNIT_POSITIVE;
237 if (image->common.transform->matrix[1][0] == 0)
238 flags |= FAST_PATH_Y_UNIT_ZERO;
242 switch (image->common.filter)
244 case PIXMAN_FILTER_NEAREST:
245 case PIXMAN_FILTER_FAST:
246 flags |= (FAST_PATH_NEAREST_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
249 case PIXMAN_FILTER_BILINEAR:
250 case PIXMAN_FILTER_GOOD:
251 case PIXMAN_FILTER_BEST:
252 flags |= (FAST_PATH_BILINEAR_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
254 /* Reduce BILINEAR to NEAREST for identity transforms */
255 if (flags & FAST_PATH_ID_TRANSFORM)
256 flags |= FAST_PATH_NEAREST_FILTER;
259 case PIXMAN_FILTER_CONVOLUTION:
263 flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
268 switch (image->common.repeat)
270 case PIXMAN_REPEAT_NONE:
272 FAST_PATH_NO_REFLECT_REPEAT |
273 FAST_PATH_NO_PAD_REPEAT |
274 FAST_PATH_NO_NORMAL_REPEAT;
277 case PIXMAN_REPEAT_REFLECT:
279 FAST_PATH_NO_PAD_REPEAT |
280 FAST_PATH_NO_NONE_REPEAT |
281 FAST_PATH_NO_NORMAL_REPEAT;
284 case PIXMAN_REPEAT_PAD:
286 FAST_PATH_NO_REFLECT_REPEAT |
287 FAST_PATH_NO_NONE_REPEAT |
288 FAST_PATH_NO_NORMAL_REPEAT;
293 FAST_PATH_NO_REFLECT_REPEAT |
294 FAST_PATH_NO_PAD_REPEAT |
295 FAST_PATH_NO_NONE_REPEAT;
299 /* Component alpha */
300 if (image->common.component_alpha)
301 flags |= FAST_PATH_COMPONENT_ALPHA;
303 flags |= FAST_PATH_UNIFIED_ALPHA;
305 flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NARROW_FORMAT);
307 /* Type specific checks */
313 if (image->solid.color.alpha == 0xffff)
314 flags |= FAST_PATH_IS_OPAQUE;
318 if (image->bits.width == 1 &&
319 image->bits.height == 1 &&
320 image->common.repeat != PIXMAN_REPEAT_NONE)
326 code = image->bits.format;
329 if (!PIXMAN_FORMAT_A (image->bits.format) &&
330 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY &&
331 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
333 flags |= FAST_PATH_SAMPLES_OPAQUE;
335 if (image->common.repeat != PIXMAN_REPEAT_NONE)
336 flags |= FAST_PATH_IS_OPAQUE;
339 if (image->bits.read_func || image->bits.write_func)
340 flags &= ~FAST_PATH_NO_ACCESSORS;
342 if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
343 flags &= ~FAST_PATH_NARROW_FORMAT;
347 code = PIXMAN_unknown;
350 * As explained in pixman-radial-gradient.c, every point of
351 * the plane has a valid associated radius (and thus will be
352 * colored) if and only if a is negative (i.e. one of the two
353 * circles contains the other one).
356 if (image->radial.a >= 0)
363 code = PIXMAN_unknown;
365 if (image->common.repeat != PIXMAN_REPEAT_NONE)
369 flags |= FAST_PATH_IS_OPAQUE;
370 for (i = 0; i < image->gradient.n_stops; ++i)
372 if (image->gradient.stops[i].color.alpha != 0xffff)
374 flags &= ~FAST_PATH_IS_OPAQUE;
382 code = PIXMAN_unknown;
387 if (!image->common.alpha_map)
389 flags |= FAST_PATH_NO_ALPHA_MAP;
393 if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
394 flags &= ~FAST_PATH_NARROW_FORMAT;
397 /* Both alpha maps and convolution filters can introduce
398 * non-opaqueness in otherwise opaque images. Also
399 * an image with component alpha turned on is only opaque
400 * if all channels are opaque, so we simply turn it off
401 * unconditionally for those images.
403 if (image->common.alpha_map ||
404 image->common.filter == PIXMAN_FILTER_CONVOLUTION ||
405 image->common.component_alpha)
407 flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
410 image->common.flags = flags;
411 image->common.extended_format_code = code;
415 _pixman_image_validate (pixman_image_t *image)
417 if (image->common.dirty)
419 compute_image_info (image);
421 /* It is important that property_changed is
422 * called *after* compute_image_info() because
423 * property_changed() can make use of the flags
424 * to set up accessors etc.
426 if (image->common.property_changed)
427 image->common.property_changed (image);
429 image->common.dirty = FALSE;
432 if (image->common.alpha_map)
433 _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
436 PIXMAN_EXPORT pixman_bool_t
437 pixman_image_set_clip_region32 (pixman_image_t * image,
438 pixman_region32_t *region)
440 image_common_t *common = (image_common_t *)image;
441 pixman_bool_t result;
445 if ((result = pixman_region32_copy (&common->clip_region, region)))
446 image->common.have_clip_region = TRUE;
450 _pixman_image_reset_clip_region (image);
455 image_property_changed (image);
460 PIXMAN_EXPORT pixman_bool_t
461 pixman_image_set_clip_region (pixman_image_t * image,
462 pixman_region16_t *region)
464 image_common_t *common = (image_common_t *)image;
465 pixman_bool_t result;
469 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
470 image->common.have_clip_region = TRUE;
474 _pixman_image_reset_clip_region (image);
479 image_property_changed (image);
485 pixman_image_set_has_client_clip (pixman_image_t *image,
486 pixman_bool_t client_clip)
488 image->common.client_clip = client_clip;
491 PIXMAN_EXPORT pixman_bool_t
492 pixman_image_set_transform (pixman_image_t * image,
493 const pixman_transform_t *transform)
495 static const pixman_transform_t id =
497 { { pixman_fixed_1, 0, 0 },
498 { 0, pixman_fixed_1, 0 },
499 { 0, 0, pixman_fixed_1 } }
502 image_common_t *common = (image_common_t *)image;
503 pixman_bool_t result;
505 if (common->transform == transform)
508 if (!transform || memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
510 free (common->transform);
511 common->transform = NULL;
517 if (common->transform &&
518 memcmp (common->transform, transform, sizeof (pixman_transform_t) == 0))
523 if (common->transform == NULL)
524 common->transform = malloc (sizeof (pixman_transform_t));
526 if (common->transform == NULL)
533 memcpy (common->transform, transform, sizeof(pixman_transform_t));
538 image_property_changed (image);
544 pixman_image_set_repeat (pixman_image_t *image,
545 pixman_repeat_t repeat)
547 if (image->common.repeat == repeat)
550 image->common.repeat = repeat;
552 image_property_changed (image);
555 PIXMAN_EXPORT pixman_bool_t
556 pixman_image_set_filter (pixman_image_t * image,
557 pixman_filter_t filter,
558 const pixman_fixed_t *params,
561 image_common_t *common = (image_common_t *)image;
562 pixman_fixed_t *new_params;
564 if (params == common->filter_params && filter == common->filter)
570 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
575 params, n_params * sizeof (pixman_fixed_t));
578 common->filter = filter;
580 if (common->filter_params)
581 free (common->filter_params);
583 common->filter_params = new_params;
584 common->n_filter_params = n_params;
586 image_property_changed (image);
591 pixman_image_set_source_clipping (pixman_image_t *image,
592 pixman_bool_t clip_sources)
594 if (image->common.clip_sources == clip_sources)
597 image->common.clip_sources = clip_sources;
599 image_property_changed (image);
602 /* Unlike all the other property setters, this function does not
603 * copy the content of indexed. Doing this copying is simply
604 * way, way too expensive.
607 pixman_image_set_indexed (pixman_image_t * image,
608 const pixman_indexed_t *indexed)
610 bits_image_t *bits = (bits_image_t *)image;
612 if (bits->indexed == indexed)
615 bits->indexed = indexed;
617 image_property_changed (image);
621 pixman_image_set_alpha_map (pixman_image_t *image,
622 pixman_image_t *alpha_map,
626 image_common_t *common = (image_common_t *)image;
628 return_if_fail (!alpha_map || alpha_map->type == BITS);
630 if (alpha_map && common->alpha_count > 0)
632 /* If this image is being used as an alpha map itself,
633 * then you can't give it an alpha map of its own.
638 if (alpha_map && alpha_map->common.alpha_map)
640 /* If the image has an alpha map of its own,
641 * then it can't be used as an alpha map itself
646 if (common->alpha_map != (bits_image_t *)alpha_map)
648 if (common->alpha_map)
650 common->alpha_map->common.alpha_count--;
652 pixman_image_unref ((pixman_image_t *)common->alpha_map);
657 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
659 common->alpha_map->common.alpha_count++;
663 common->alpha_map = NULL;
667 common->alpha_origin_x = x;
668 common->alpha_origin_y = y;
670 image_property_changed (image);
674 pixman_image_set_component_alpha (pixman_image_t *image,
675 pixman_bool_t component_alpha)
677 if (image->common.component_alpha == component_alpha)
680 image->common.component_alpha = component_alpha;
682 image_property_changed (image);
685 PIXMAN_EXPORT pixman_bool_t
686 pixman_image_get_component_alpha (pixman_image_t *image)
688 return image->common.component_alpha;
692 pixman_image_set_accessors (pixman_image_t * image,
693 pixman_read_memory_func_t read_func,
694 pixman_write_memory_func_t write_func)
696 return_if_fail (image != NULL);
698 if (image->type == BITS)
700 image->bits.read_func = read_func;
701 image->bits.write_func = write_func;
703 image_property_changed (image);
707 PIXMAN_EXPORT uint32_t *
708 pixman_image_get_data (pixman_image_t *image)
710 if (image->type == BITS)
711 return image->bits.bits;
717 pixman_image_get_width (pixman_image_t *image)
719 if (image->type == BITS)
720 return image->bits.width;
726 pixman_image_get_height (pixman_image_t *image)
728 if (image->type == BITS)
729 return image->bits.height;
735 pixman_image_get_stride (pixman_image_t *image)
737 if (image->type == BITS)
738 return image->bits.rowstride * (int) sizeof (uint32_t);
744 pixman_image_get_depth (pixman_image_t *image)
746 if (image->type == BITS)
747 return PIXMAN_FORMAT_DEPTH (image->bits.format);
752 PIXMAN_EXPORT pixman_format_code_t
753 pixman_image_get_format (pixman_image_t *image)
755 if (image->type == BITS)
756 return image->bits.format;
762 _pixman_image_get_solid (pixman_implementation_t *imp,
763 pixman_image_t * image,
764 pixman_format_code_t format)
769 _pixman_implementation_src_iter_init (
770 imp, &iter, image, 0, 0, 1, 1,
771 (uint8_t *)&result, ITER_NARROW);
773 result = *iter.get_scanline (&iter, NULL);
775 /* If necessary, convert RGB <--> BGR. */
776 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
778 result = (((result & 0xff000000) >> 0) |
779 ((result & 0x00ff0000) >> 16) |
780 ((result & 0x0000ff00) >> 0) |
781 ((result & 0x000000ff) << 16));