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;
54 _pixman_image_allocate (void)
56 pixman_image_t *image = malloc (sizeof (pixman_image_t));
60 image_common_t *common = &image->common;
62 pixman_region32_init (&common->clip_region);
64 common->alpha_count = 0;
65 common->have_clip_region = FALSE;
66 common->clip_sources = FALSE;
67 common->transform = NULL;
68 common->repeat = PIXMAN_REPEAT_NONE;
69 common->filter = PIXMAN_FILTER_NEAREST;
70 common->filter_params = NULL;
71 common->n_filter_params = 0;
72 common->alpha_map = NULL;
73 common->component_alpha = FALSE;
74 common->ref_count = 1;
75 common->property_changed = NULL;
76 common->client_clip = FALSE;
77 common->destroy_func = NULL;
78 common->destroy_data = NULL;
86 image_property_changed (pixman_image_t *image)
88 image->common.dirty = TRUE;
92 PIXMAN_EXPORT pixman_image_t *
93 pixman_image_ref (pixman_image_t *image)
95 image->common.ref_count++;
100 /* returns TRUE when the image is freed */
101 PIXMAN_EXPORT pixman_bool_t
102 pixman_image_unref (pixman_image_t *image)
104 image_common_t *common = (image_common_t *)image;
108 if (common->ref_count == 0)
110 if (image->common.destroy_func)
111 image->common.destroy_func (image, image->common.destroy_data);
113 pixman_region32_fini (&common->clip_region);
115 if (common->transform)
116 free (common->transform);
118 if (common->filter_params)
119 free (common->filter_params);
121 if (common->alpha_map)
122 pixman_image_unref ((pixman_image_t *)common->alpha_map);
124 if (image->type == LINEAR ||
125 image->type == RADIAL ||
126 image->type == CONICAL)
128 if (image->gradient.stops)
129 free (image->gradient.stops);
132 if (image->type == BITS && image->bits.free_me)
133 free (image->bits.free_me);
144 pixman_image_set_destroy_function (pixman_image_t * image,
145 pixman_image_destroy_func_t func,
148 image->common.destroy_func = func;
149 image->common.destroy_data = data;
153 pixman_image_get_destroy_data (pixman_image_t *image)
155 return image->common.destroy_data;
159 _pixman_image_reset_clip_region (pixman_image_t *image)
161 image->common.have_clip_region = FALSE;
164 /* Executive Summary: This function is a no-op that only exists
165 * for historical reasons.
167 * There used to be a bug in the X server where it would rely on
168 * out-of-bounds accesses when it was asked to composite with a
169 * window as the source. It would create a pixman image pointing
170 * to some bogus position in memory, but then set a clip region
171 * to the position where the actual bits were.
173 * Due to a bug in old versions of pixman, where it would not clip
174 * against the image bounds when a clip region was set, this would
175 * actually work. So when the pixman bug was fixed, a workaround was
176 * added to allow certain out-of-bound accesses. This function disabled
179 * Since 0.21.2, pixman doesn't do these workarounds anymore, so now
180 * this function is a no-op.
183 pixman_disable_out_of_bounds_workaround (void)
188 compute_image_info (pixman_image_t *image)
190 pixman_format_code_t code;
194 if (!image->common.transform)
196 flags |= (FAST_PATH_ID_TRANSFORM |
197 FAST_PATH_X_UNIT_POSITIVE |
198 FAST_PATH_Y_UNIT_ZERO |
199 FAST_PATH_AFFINE_TRANSFORM);
203 flags |= FAST_PATH_HAS_TRANSFORM;
205 if (image->common.transform->matrix[2][0] == 0 &&
206 image->common.transform->matrix[2][1] == 0 &&
207 image->common.transform->matrix[2][2] == pixman_fixed_1)
209 flags |= FAST_PATH_AFFINE_TRANSFORM;
211 if (image->common.transform->matrix[0][1] == 0 &&
212 image->common.transform->matrix[1][0] == 0)
214 if (image->common.transform->matrix[0][0] == -pixman_fixed_1 &&
215 image->common.transform->matrix[1][1] == -pixman_fixed_1)
217 flags |= FAST_PATH_ROTATE_180_TRANSFORM;
219 flags |= FAST_PATH_SCALE_TRANSFORM;
221 else if (image->common.transform->matrix[0][0] == 0 &&
222 image->common.transform->matrix[1][1] == 0)
224 pixman_fixed_t m01 = image->common.transform->matrix[0][1];
225 if (m01 == -image->common.transform->matrix[1][0])
227 if (m01 == -pixman_fixed_1)
228 flags |= FAST_PATH_ROTATE_90_TRANSFORM;
229 else if (m01 == pixman_fixed_1)
230 flags |= FAST_PATH_ROTATE_270_TRANSFORM;
235 if (image->common.transform->matrix[0][0] > 0)
236 flags |= FAST_PATH_X_UNIT_POSITIVE;
238 if (image->common.transform->matrix[1][0] == 0)
239 flags |= FAST_PATH_Y_UNIT_ZERO;
243 switch (image->common.filter)
245 case PIXMAN_FILTER_NEAREST:
246 case PIXMAN_FILTER_FAST:
247 flags |= (FAST_PATH_NEAREST_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
250 case PIXMAN_FILTER_BILINEAR:
251 case PIXMAN_FILTER_GOOD:
252 case PIXMAN_FILTER_BEST:
253 flags |= (FAST_PATH_BILINEAR_FILTER | FAST_PATH_NO_CONVOLUTION_FILTER);
256 case PIXMAN_FILTER_CONVOLUTION:
260 flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
265 switch (image->common.repeat)
267 case PIXMAN_REPEAT_NONE:
269 FAST_PATH_NO_REFLECT_REPEAT |
270 FAST_PATH_NO_PAD_REPEAT |
271 FAST_PATH_NO_NORMAL_REPEAT;
274 case PIXMAN_REPEAT_REFLECT:
276 FAST_PATH_NO_PAD_REPEAT |
277 FAST_PATH_NO_NONE_REPEAT |
278 FAST_PATH_NO_NORMAL_REPEAT;
281 case PIXMAN_REPEAT_PAD:
283 FAST_PATH_NO_REFLECT_REPEAT |
284 FAST_PATH_NO_NONE_REPEAT |
285 FAST_PATH_NO_NORMAL_REPEAT;
290 FAST_PATH_NO_REFLECT_REPEAT |
291 FAST_PATH_NO_PAD_REPEAT |
292 FAST_PATH_NO_NONE_REPEAT;
296 /* Component alpha */
297 if (image->common.component_alpha)
298 flags |= FAST_PATH_COMPONENT_ALPHA;
300 flags |= FAST_PATH_UNIFIED_ALPHA;
302 flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NARROW_FORMAT);
304 /* Type specific checks */
310 if (image->solid.color.alpha == 0xffff)
311 flags |= FAST_PATH_IS_OPAQUE;
315 if (image->bits.width == 1 &&
316 image->bits.height == 1 &&
317 image->common.repeat != PIXMAN_REPEAT_NONE)
323 code = image->bits.format;
326 if (!PIXMAN_FORMAT_A (image->bits.format) &&
327 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_GRAY &&
328 PIXMAN_FORMAT_TYPE (image->bits.format) != PIXMAN_TYPE_COLOR)
330 flags |= FAST_PATH_SAMPLES_OPAQUE;
332 if (image->common.repeat != PIXMAN_REPEAT_NONE)
333 flags |= FAST_PATH_IS_OPAQUE;
336 if (image->bits.read_func || image->bits.write_func)
337 flags &= ~FAST_PATH_NO_ACCESSORS;
339 if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
340 flags &= ~FAST_PATH_NARROW_FORMAT;
344 code = PIXMAN_unknown;
347 * As explained in pixman-radial-gradient.c, every point of
348 * the plane has a valid associated radius (and thus will be
349 * colored) if and only if a is negative (i.e. one of the two
350 * circles contains the other one).
353 if (image->radial.a >= 0)
360 code = PIXMAN_unknown;
362 if (image->common.repeat != PIXMAN_REPEAT_NONE)
366 flags |= FAST_PATH_IS_OPAQUE;
367 for (i = 0; i < image->gradient.n_stops; ++i)
369 if (image->gradient.stops[i].color.alpha != 0xffff)
371 flags &= ~FAST_PATH_IS_OPAQUE;
379 code = PIXMAN_unknown;
384 if (!image->common.alpha_map)
386 flags |= FAST_PATH_NO_ALPHA_MAP;
390 if (PIXMAN_FORMAT_IS_WIDE (image->common.alpha_map->format))
391 flags &= ~FAST_PATH_NARROW_FORMAT;
394 /* Both alpha maps and convolution filters can introduce
395 * non-opaqueness in otherwise opaque images. Also
396 * an image with component alpha turned on is only opaque
397 * if all channels are opaque, so we simply turn it off
398 * unconditionally for those images.
400 if (image->common.alpha_map ||
401 image->common.filter == PIXMAN_FILTER_CONVOLUTION ||
402 image->common.component_alpha)
404 flags &= ~(FAST_PATH_IS_OPAQUE | FAST_PATH_SAMPLES_OPAQUE);
407 image->common.flags = flags;
408 image->common.extended_format_code = code;
412 _pixman_image_validate (pixman_image_t *image)
414 if (image->common.dirty)
416 compute_image_info (image);
418 /* It is important that property_changed is
419 * called *after* compute_image_info() because
420 * property_changed() can make use of the flags
421 * to set up accessors etc.
423 if (image->common.property_changed)
424 image->common.property_changed (image);
426 image->common.dirty = FALSE;
429 if (image->common.alpha_map)
430 _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
433 PIXMAN_EXPORT pixman_bool_t
434 pixman_image_set_clip_region32 (pixman_image_t * image,
435 pixman_region32_t *region)
437 image_common_t *common = (image_common_t *)image;
438 pixman_bool_t result;
442 if ((result = pixman_region32_copy (&common->clip_region, region)))
443 image->common.have_clip_region = TRUE;
447 _pixman_image_reset_clip_region (image);
452 image_property_changed (image);
457 PIXMAN_EXPORT pixman_bool_t
458 pixman_image_set_clip_region (pixman_image_t * image,
459 pixman_region16_t *region)
461 image_common_t *common = (image_common_t *)image;
462 pixman_bool_t result;
466 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
467 image->common.have_clip_region = TRUE;
471 _pixman_image_reset_clip_region (image);
476 image_property_changed (image);
482 pixman_image_set_has_client_clip (pixman_image_t *image,
483 pixman_bool_t client_clip)
485 image->common.client_clip = client_clip;
488 PIXMAN_EXPORT pixman_bool_t
489 pixman_image_set_transform (pixman_image_t * image,
490 const pixman_transform_t *transform)
492 static const pixman_transform_t id =
494 { { pixman_fixed_1, 0, 0 },
495 { 0, pixman_fixed_1, 0 },
496 { 0, 0, pixman_fixed_1 } }
499 image_common_t *common = (image_common_t *)image;
500 pixman_bool_t result;
502 if (common->transform == transform)
505 if (!transform || memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
507 free (common->transform);
508 common->transform = NULL;
514 if (common->transform &&
515 memcmp (common->transform, transform, sizeof (pixman_transform_t) == 0))
520 if (common->transform == NULL)
521 common->transform = malloc (sizeof (pixman_transform_t));
523 if (common->transform == NULL)
530 memcpy (common->transform, transform, sizeof(pixman_transform_t));
535 image_property_changed (image);
541 pixman_image_set_repeat (pixman_image_t *image,
542 pixman_repeat_t repeat)
544 if (image->common.repeat == repeat)
547 image->common.repeat = repeat;
549 image_property_changed (image);
552 PIXMAN_EXPORT pixman_bool_t
553 pixman_image_set_filter (pixman_image_t * image,
554 pixman_filter_t filter,
555 const pixman_fixed_t *params,
558 image_common_t *common = (image_common_t *)image;
559 pixman_fixed_t *new_params;
561 if (params == common->filter_params && filter == common->filter)
567 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
572 params, n_params * sizeof (pixman_fixed_t));
575 common->filter = filter;
577 if (common->filter_params)
578 free (common->filter_params);
580 common->filter_params = new_params;
581 common->n_filter_params = n_params;
583 image_property_changed (image);
588 pixman_image_set_source_clipping (pixman_image_t *image,
589 pixman_bool_t clip_sources)
591 if (image->common.clip_sources == clip_sources)
594 image->common.clip_sources = clip_sources;
596 image_property_changed (image);
599 /* Unlike all the other property setters, this function does not
600 * copy the content of indexed. Doing this copying is simply
601 * way, way too expensive.
604 pixman_image_set_indexed (pixman_image_t * image,
605 const pixman_indexed_t *indexed)
607 bits_image_t *bits = (bits_image_t *)image;
609 if (bits->indexed == indexed)
612 bits->indexed = indexed;
614 image_property_changed (image);
618 pixman_image_set_alpha_map (pixman_image_t *image,
619 pixman_image_t *alpha_map,
623 image_common_t *common = (image_common_t *)image;
625 return_if_fail (!alpha_map || alpha_map->type == BITS);
627 if (alpha_map && common->alpha_count > 0)
629 /* If this image is being used as an alpha map itself,
630 * then you can't give it an alpha map of its own.
635 if (alpha_map && alpha_map->common.alpha_map)
637 /* If the image has an alpha map of its own,
638 * then it can't be used as an alpha map itself
643 if (common->alpha_map != (bits_image_t *)alpha_map)
645 if (common->alpha_map)
647 common->alpha_map->common.alpha_count--;
649 pixman_image_unref ((pixman_image_t *)common->alpha_map);
654 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
656 common->alpha_map->common.alpha_count++;
660 common->alpha_map = NULL;
664 common->alpha_origin_x = x;
665 common->alpha_origin_y = y;
667 image_property_changed (image);
671 pixman_image_set_component_alpha (pixman_image_t *image,
672 pixman_bool_t component_alpha)
674 if (image->common.component_alpha == component_alpha)
677 image->common.component_alpha = component_alpha;
679 image_property_changed (image);
682 PIXMAN_EXPORT pixman_bool_t
683 pixman_image_get_component_alpha (pixman_image_t *image)
685 return image->common.component_alpha;
689 pixman_image_set_accessors (pixman_image_t * image,
690 pixman_read_memory_func_t read_func,
691 pixman_write_memory_func_t write_func)
693 return_if_fail (image != NULL);
695 if (image->type == BITS)
697 image->bits.read_func = read_func;
698 image->bits.write_func = write_func;
700 image_property_changed (image);
704 PIXMAN_EXPORT uint32_t *
705 pixman_image_get_data (pixman_image_t *image)
707 if (image->type == BITS)
708 return image->bits.bits;
714 pixman_image_get_width (pixman_image_t *image)
716 if (image->type == BITS)
717 return image->bits.width;
723 pixman_image_get_height (pixman_image_t *image)
725 if (image->type == BITS)
726 return image->bits.height;
732 pixman_image_get_stride (pixman_image_t *image)
734 if (image->type == BITS)
735 return image->bits.rowstride * (int) sizeof (uint32_t);
741 pixman_image_get_depth (pixman_image_t *image)
743 if (image->type == BITS)
744 return PIXMAN_FORMAT_DEPTH (image->bits.format);
749 PIXMAN_EXPORT pixman_format_code_t
750 pixman_image_get_format (pixman_image_t *image)
752 if (image->type == BITS)
753 return image->bits.format;
759 _pixman_image_get_solid (pixman_implementation_t *imp,
760 pixman_image_t * image,
761 pixman_format_code_t format)
766 _pixman_implementation_src_iter_init (
767 imp, &iter, image, 0, 0, 1, 1,
768 (uint8_t *)&result, ITER_NARROW);
770 result = *iter.get_scanline (&iter, NULL);
772 /* If necessary, convert RGB <--> BGR. */
773 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
775 result = (((result & 0xff000000) >> 0) |
776 ((result & 0x00ff0000) >> 16) |
777 ((result & 0x0000ff00) >> 0) |
778 ((result & 0x000000ff) << 16));