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->need_workaround = FALSE;
121 common->dirty = TRUE;
128 _pixman_image_classify (pixman_image_t *image,
134 if (image->common.classify)
135 return image->common.classify (image, x, y, width, height);
137 return SOURCE_IMAGE_CLASS_UNKNOWN;
141 _pixman_image_get_scanline_32 (pixman_image_t *image,
146 const uint32_t *mask,
149 image->common.get_scanline_32 (image, x, y, width, buffer, mask, mask_bits);
152 /* Even thought the type of buffer is uint32_t *, the function actually expects
153 * a uint64_t *buffer.
156 _pixman_image_get_scanline_64 (pixman_image_t *image,
161 const uint32_t *unused,
164 image->common.get_scanline_64 (image, x, y, width, buffer, unused, unused2);
168 image_property_changed (pixman_image_t *image)
170 image->common.dirty = TRUE;
174 PIXMAN_EXPORT pixman_image_t *
175 pixman_image_ref (pixman_image_t *image)
177 image->common.ref_count++;
182 /* returns TRUE when the image is freed */
183 PIXMAN_EXPORT pixman_bool_t
184 pixman_image_unref (pixman_image_t *image)
186 image_common_t *common = (image_common_t *)image;
190 if (common->ref_count == 0)
192 if (image->common.destroy_func)
193 image->common.destroy_func (image, image->common.destroy_data);
195 pixman_region32_fini (&common->clip_region);
197 if (common->transform)
198 free (common->transform);
200 if (common->filter_params)
201 free (common->filter_params);
203 if (common->alpha_map)
204 pixman_image_unref ((pixman_image_t *)common->alpha_map);
206 if (image->type == LINEAR ||
207 image->type == RADIAL ||
208 image->type == CONICAL)
210 if (image->gradient.stops)
211 free (image->gradient.stops);
214 if (image->type == BITS && image->bits.free_me)
215 free (image->bits.free_me);
226 pixman_image_set_destroy_function (pixman_image_t * image,
227 pixman_image_destroy_func_t func,
230 image->common.destroy_func = func;
231 image->common.destroy_data = data;
235 pixman_image_get_destroy_data (pixman_image_t *image)
237 return image->common.destroy_data;
241 _pixman_image_reset_clip_region (pixman_image_t *image)
243 image->common.have_clip_region = FALSE;
247 compute_image_info (pixman_image_t *image)
249 pixman_format_code_t code;
252 if (!image->common.transform)
254 flags |= FAST_PATH_ID_TRANSFORM;
256 else if (image->common.transform &&
257 image->common.transform->matrix[0][1] == 0 &&
258 image->common.transform->matrix[1][0] == 0 &&
259 image->common.transform->matrix[2][0] == 0 &&
260 image->common.transform->matrix[2][1] == 0 &&
261 image->common.transform->matrix[2][2] == pixman_fixed_1)
263 flags |= FAST_PATH_SCALE_TRANSFORM;
266 if (!image->common.alpha_map)
267 flags |= FAST_PATH_NO_ALPHA_MAP;
269 if (image->common.filter != PIXMAN_FILTER_CONVOLUTION)
271 flags |= FAST_PATH_NO_CONVOLUTION_FILTER;
273 if (image->common.filter == PIXMAN_FILTER_NEAREST)
274 flags |= FAST_PATH_NEAREST_FILTER;
277 if (image->common.repeat != PIXMAN_REPEAT_PAD)
278 flags |= FAST_PATH_NO_PAD_REPEAT;
280 if (image->common.repeat != PIXMAN_REPEAT_REFLECT)
281 flags |= FAST_PATH_NO_REFLECT_REPEAT;
283 flags |= (FAST_PATH_NO_ACCESSORS | FAST_PATH_NO_WIDE_FORMAT);
284 if (image->common.type == BITS)
286 if (image->bits.read_func || image->bits.write_func)
287 flags &= ~FAST_PATH_NO_ACCESSORS;
289 if (PIXMAN_FORMAT_IS_WIDE (image->bits.format))
290 flags &= ~FAST_PATH_NO_WIDE_FORMAT;
293 if (image->common.component_alpha)
294 flags |= FAST_PATH_COMPONENT_ALPHA;
296 flags |= FAST_PATH_UNIFIED_ALPHA;
298 if (image->type == SOLID)
302 if (image->solid.color.alpha == 0xffff)
303 flags |= FAST_PATH_IS_OPAQUE;
305 else if (image->common.type == BITS)
307 if (image->bits.width == 1 &&
308 image->bits.height == 1 &&
309 image->common.repeat != PIXMAN_REPEAT_NONE)
315 code = image->bits.format;
317 if (!image->common.transform &&
318 image->common.repeat == PIXMAN_REPEAT_NORMAL)
320 flags |= FAST_PATH_SIMPLE_REPEAT;
324 if (image->common.repeat != PIXMAN_REPEAT_NONE &&
325 !PIXMAN_FORMAT_A (image->bits.format))
327 flags |= FAST_PATH_IS_OPAQUE;
332 code = PIXMAN_unknown;
334 if (image->type == LINEAR || image->type == RADIAL)
336 if (image->common.repeat != PIXMAN_REPEAT_NONE)
340 flags |= FAST_PATH_IS_OPAQUE;
342 for (i = 0; i < image->gradient.n_stops; ++i)
344 if (image->gradient.stops[i].color.alpha != 0xffff)
346 flags &= ~FAST_PATH_IS_OPAQUE;
354 /* Both alpha maps and convolution filters can introduce
355 * non-opaqueness in otherwise opaque images. Also
356 * an image with component alpha turned on is only opaque
357 * if all channels are opaque, so we simply turn it off
358 * unconditionally for those images.
360 if (image->common.alpha_map ||
361 image->common.filter == PIXMAN_FILTER_CONVOLUTION ||
362 image->common.component_alpha)
364 flags &= ~FAST_PATH_IS_OPAQUE;
367 image->common.flags = flags;
368 image->common.extended_format_code = code;
372 _pixman_image_validate (pixman_image_t *image)
374 if (image->common.dirty)
376 image->common.property_changed (image);
378 compute_image_info (image);
380 image->common.dirty = FALSE;
383 if (image->common.alpha_map)
384 _pixman_image_validate ((pixman_image_t *)image->common.alpha_map);
387 PIXMAN_EXPORT pixman_bool_t
388 pixman_image_set_clip_region32 (pixman_image_t * image,
389 pixman_region32_t *region)
391 image_common_t *common = (image_common_t *)image;
392 pixman_bool_t result;
396 if ((result = pixman_region32_copy (&common->clip_region, region)))
397 image->common.have_clip_region = TRUE;
401 _pixman_image_reset_clip_region (image);
406 image_property_changed (image);
411 PIXMAN_EXPORT pixman_bool_t
412 pixman_image_set_clip_region (pixman_image_t * image,
413 pixman_region16_t *region)
415 image_common_t *common = (image_common_t *)image;
416 pixman_bool_t result;
420 if ((result = pixman_region32_copy_from_region16 (&common->clip_region, region)))
421 image->common.have_clip_region = TRUE;
425 _pixman_image_reset_clip_region (image);
430 image_property_changed (image);
436 pixman_image_set_has_client_clip (pixman_image_t *image,
437 pixman_bool_t client_clip)
439 image->common.client_clip = client_clip;
442 PIXMAN_EXPORT pixman_bool_t
443 pixman_image_set_transform (pixman_image_t * image,
444 const pixman_transform_t *transform)
446 static const pixman_transform_t id =
448 { { pixman_fixed_1, 0, 0 },
449 { 0, pixman_fixed_1, 0 },
450 { 0, 0, pixman_fixed_1 } }
453 image_common_t *common = (image_common_t *)image;
454 pixman_bool_t result;
456 if (common->transform == transform)
459 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
461 free (common->transform);
462 common->transform = NULL;
468 if (common->transform == NULL)
469 common->transform = malloc (sizeof (pixman_transform_t));
471 if (common->transform == NULL)
478 memcpy (common->transform, transform, sizeof(pixman_transform_t));
483 image_property_changed (image);
489 pixman_image_set_repeat (pixman_image_t *image,
490 pixman_repeat_t repeat)
492 image->common.repeat = repeat;
494 image_property_changed (image);
497 PIXMAN_EXPORT pixman_bool_t
498 pixman_image_set_filter (pixman_image_t * image,
499 pixman_filter_t filter,
500 const pixman_fixed_t *params,
503 image_common_t *common = (image_common_t *)image;
504 pixman_fixed_t *new_params;
506 if (params == common->filter_params && filter == common->filter)
512 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
517 params, n_params * sizeof (pixman_fixed_t));
520 common->filter = filter;
522 if (common->filter_params)
523 free (common->filter_params);
525 common->filter_params = new_params;
526 common->n_filter_params = n_params;
528 image_property_changed (image);
533 pixman_image_set_source_clipping (pixman_image_t *image,
534 pixman_bool_t clip_sources)
536 image->common.clip_sources = clip_sources;
538 image_property_changed (image);
541 /* Unlike all the other property setters, this function does not
542 * copy the content of indexed. Doing this copying is simply
543 * way, way too expensive.
546 pixman_image_set_indexed (pixman_image_t * image,
547 const pixman_indexed_t *indexed)
549 bits_image_t *bits = (bits_image_t *)image;
551 bits->indexed = indexed;
553 image_property_changed (image);
557 pixman_image_set_alpha_map (pixman_image_t *image,
558 pixman_image_t *alpha_map,
562 image_common_t *common = (image_common_t *)image;
564 return_if_fail (!alpha_map || alpha_map->type == BITS);
566 if (common->alpha_map != (bits_image_t *)alpha_map)
568 if (common->alpha_map)
569 pixman_image_unref ((pixman_image_t *)common->alpha_map);
572 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
574 common->alpha_map = NULL;
577 common->alpha_origin_x = x;
578 common->alpha_origin_y = y;
580 image_property_changed (image);
584 pixman_image_set_component_alpha (pixman_image_t *image,
585 pixman_bool_t component_alpha)
587 image->common.component_alpha = component_alpha;
589 image_property_changed (image);
593 pixman_image_set_accessors (pixman_image_t * image,
594 pixman_read_memory_func_t read_func,
595 pixman_write_memory_func_t write_func)
597 return_if_fail (image != NULL);
599 if (image->type == BITS)
601 image->bits.read_func = read_func;
602 image->bits.write_func = write_func;
604 image_property_changed (image);
608 PIXMAN_EXPORT uint32_t *
609 pixman_image_get_data (pixman_image_t *image)
611 if (image->type == BITS)
612 return image->bits.bits;
618 pixman_image_get_width (pixman_image_t *image)
620 if (image->type == BITS)
621 return image->bits.width;
627 pixman_image_get_height (pixman_image_t *image)
629 if (image->type == BITS)
630 return image->bits.height;
636 pixman_image_get_stride (pixman_image_t *image)
638 if (image->type == BITS)
639 return image->bits.rowstride * (int) sizeof (uint32_t);
645 pixman_image_get_depth (pixman_image_t *image)
647 if (image->type == BITS)
648 return PIXMAN_FORMAT_DEPTH (image->bits.format);
654 _pixman_image_get_solid (pixman_image_t * image,
655 pixman_format_code_t format)
659 _pixman_image_get_scanline_32 (image, 0, 0, 1, &result, NULL, 0);
661 /* If necessary, convert RGB <--> BGR. */
662 if (PIXMAN_FORMAT_TYPE (format) != PIXMAN_TYPE_ARGB)
664 result = (((result & 0xff000000) >> 0) |
665 ((result & 0x00ff0000) >> 16) |
666 ((result & 0x0000ff00) >> 0) |
667 ((result & 0x000000ff) << 16));