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"
34 #define Alpha(x) ((x) >> 24)
37 _pixman_init_gradient (gradient_t *gradient,
38 const pixman_gradient_stop_t *stops,
41 return_val_if_fail (n_stops > 0, FALSE);
43 gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
47 memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
49 gradient->n_stops = n_stops;
51 gradient->stop_range = 0xffff;
52 gradient->color_table = NULL;
53 gradient->color_table_size = 0;
54 gradient->common.class = SOURCE_IMAGE_CLASS_UNKNOWN;
60 _pixman_image_allocate (void)
62 pixman_image_t *image = malloc (sizeof (pixman_image_t));
66 image_common_t *common = &image->common;
68 pixman_region32_init (&common->full_region);
69 pixman_region32_init (&common->clip_region);
70 common->src_clip = &common->full_region;
71 common->has_client_clip = FALSE;
72 common->transform = NULL;
73 common->repeat = PIXMAN_REPEAT_NONE;
74 common->filter = PIXMAN_FILTER_NEAREST;
75 common->filter_params = NULL;
76 common->n_filter_params = 0;
77 common->alpha_map = NULL;
78 common->component_alpha = FALSE;
79 common->ref_count = 1;
80 common->read_func = NULL;
81 common->write_func = NULL;
82 common->classify = NULL;
89 _pixman_image_classify (pixman_image_t *image,
95 if (image->common.classify)
96 return image->common.classify (image, x, y, width, height);
98 return SOURCE_IMAGE_CLASS_UNKNOWN;
101 #define READ_ACCESS(f) ((image->common.read_func)? f##_accessors : f)
103 static void fbFetchSolid(bits_image_t * image, int x, int y, int width, uint32_t *buffer, uint32_t *mask, uint32_t maskBits)
107 fetchPixelProc32 fetch = READ_ACCESS(pixman_fetchPixelProcForPicture32)(image);
109 color = fetch(image, 0, 0);
111 end = buffer + width;
116 static void fbFetchSolid64(bits_image_t * image, int x, int y, int width, uint64_t *buffer, void *unused, uint32_t unused2)
120 fetchPixelProc64 fetch = READ_ACCESS(pixman_fetchPixelProcForPicture64)(image);
122 color = fetch(image, 0, 0);
124 end = buffer + width;
129 static void fbFetch(bits_image_t * image, int x, int y, int width, uint32_t *buffer, uint32_t *mask, uint32_t maskBits)
131 fetchProc32 fetch = READ_ACCESS(pixman_fetchProcForPicture32)(image);
133 fetch(image, x, y, width, buffer);
136 static void fbFetch64(bits_image_t * image, int x, int y, int width, uint64_t *buffer, void *unused, uint32_t unused2)
138 fetchProc64 fetch = READ_ACCESS(pixman_fetchProcForPicture64)(image);
140 fetch(image, x, y, width, buffer);
144 set_fetchers (pixman_image_t *image)
146 if (!IS_SOURCE_IMAGE (image))
148 bits_image_t *bits = (bits_image_t *)image;
150 if (bits->common.alpha_map)
152 image->common.get_scanline_64 =
153 (scanFetchProc)READ_ACCESS(fbFetchExternalAlpha64);
154 image->common.get_scanline_32 =
155 (scanFetchProc)READ_ACCESS(fbFetchExternalAlpha);
157 else if ((bits->common.repeat != PIXMAN_REPEAT_NONE) &&
161 image->common.get_scanline_64 = (scanFetchProc)fbFetchSolid64;
162 image->common.get_scanline_32 = (scanFetchProc)fbFetchSolid;
164 else if (!bits->common.transform &&
165 bits->common.filter != PIXMAN_FILTER_CONVOLUTION &&
166 bits->common.repeat != PIXMAN_REPEAT_PAD &&
167 bits->common.repeat != PIXMAN_REPEAT_REFLECT)
169 image->common.get_scanline_64 = (scanFetchProc)fbFetch64;
170 image->common.get_scanline_32 = (scanFetchProc)fbFetch;
174 image->common.get_scanline_64 = (scanFetchProc)READ_ACCESS(fbFetchTransformed64);
175 image->common.get_scanline_32 = (scanFetchProc)READ_ACCESS(fbFetchTransformed);
181 _pixman_image_get_fetcher (pixman_image_t *image,
184 set_fetchers (image);
186 assert (image->common.get_scanline_64);
187 assert (image->common.get_scanline_32);
190 return image->common.get_scanline_64;
192 return image->common.get_scanline_32;
197 #define WRITE_ACCESS(f) ((image->common.write_func)? f##_accessors : f)
200 fbStore(bits_image_t * image, int x, int y, int width, uint32_t *buffer)
204 storeProc32 store = WRITE_ACCESS(pixman_storeProcForPicture32)(image);
205 const pixman_indexed_t * indexed = image->indexed;
208 stride = image->rowstride;
210 store((pixman_image_t *)image, bits, buffer, x, width, indexed);
214 fbStore64(bits_image_t * image, int x, int y, int width, uint64_t *buffer)
218 storeProc64 store = WRITE_ACCESS(pixman_storeProcForPicture64)(image);
219 const pixman_indexed_t * indexed = image->indexed;
222 stride = image->rowstride;
224 store((pixman_image_t *)image, bits, buffer, x, width, indexed);
228 _pixman_image_get_storer (pixman_image_t *image,
231 if (image->common.alpha_map)
234 return (scanStoreProc)WRITE_ACCESS(fbStoreExternalAlpha64);
236 return (scanStoreProc)WRITE_ACCESS(fbStoreExternalAlpha);
241 return (scanStoreProc)fbStore64;
243 return (scanStoreProc)fbStore;
248 image_property_changed (pixman_image_t *image)
252 image->common.property_changed (image);
256 PIXMAN_EXPORT pixman_image_t *
257 pixman_image_ref (pixman_image_t *image)
259 image->common.ref_count++;
264 /* returns TRUE when the image is freed */
265 PIXMAN_EXPORT pixman_bool_t
266 pixman_image_unref (pixman_image_t *image)
268 image_common_t *common = (image_common_t *)image;
272 if (common->ref_count == 0)
274 pixman_region32_fini (&common->clip_region);
275 pixman_region32_fini (&common->full_region);
277 if (common->transform)
278 free (common->transform);
280 if (common->filter_params)
281 free (common->filter_params);
283 if (common->alpha_map)
284 pixman_image_unref ((pixman_image_t *)common->alpha_map);
287 if (image->type == BITS && image->bits.indexed)
288 free (image->bits.indexed);
292 memset (image, 0xaa, sizeof (pixman_image_t));
294 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
296 if (image->gradient.stops)
297 free (image->gradient.stops);
301 if (image->type == BITS && image->bits.free_me)
302 free (image->bits.free_me);
315 _pixman_image_reset_clip_region (pixman_image_t *image)
317 pixman_region32_fini (&image->common.clip_region);
319 if (image->type == BITS)
321 pixman_region32_init_rect (&image->common.clip_region, 0, 0,
322 image->bits.width, image->bits.height);
326 pixman_region32_init (&image->common.clip_region);
330 PIXMAN_EXPORT pixman_bool_t
331 pixman_image_set_clip_region32 (pixman_image_t *image,
332 pixman_region32_t *region)
334 image_common_t *common = (image_common_t *)image;
335 pixman_bool_t result;
339 result = pixman_region32_copy (&common->clip_region, region);
343 _pixman_image_reset_clip_region (image);
348 image_property_changed (image);
354 PIXMAN_EXPORT pixman_bool_t
355 pixman_image_set_clip_region (pixman_image_t *image,
356 pixman_region16_t *region)
358 image_common_t *common = (image_common_t *)image;
359 pixman_bool_t result;
363 result = pixman_region32_copy_from_region16 (&common->clip_region, region);
367 _pixman_image_reset_clip_region (image);
372 image_property_changed (image);
377 /* Sets whether the clip region includes a clip region set by the client
380 pixman_image_set_has_client_clip (pixman_image_t *image,
381 pixman_bool_t client_clip)
383 image->common.has_client_clip = client_clip;
385 image_property_changed (image);
388 PIXMAN_EXPORT pixman_bool_t
389 pixman_image_set_transform (pixman_image_t *image,
390 const pixman_transform_t *transform)
392 static const pixman_transform_t id =
394 { { pixman_fixed_1, 0, 0 },
395 { 0, pixman_fixed_1, 0 },
396 { 0, 0, pixman_fixed_1 }
400 image_common_t *common = (image_common_t *)image;
401 pixman_bool_t result;
403 if (common->transform == transform)
406 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
408 free(common->transform);
409 common->transform = NULL;
414 if (common->transform == NULL)
415 common->transform = malloc (sizeof (pixman_transform_t));
417 if (common->transform == NULL)
423 memcpy(common->transform, transform, sizeof(pixman_transform_t));
426 image_property_changed (image);
432 pixman_image_set_repeat (pixman_image_t *image,
433 pixman_repeat_t repeat)
435 image->common.repeat = repeat;
437 image_property_changed (image);
440 PIXMAN_EXPORT pixman_bool_t
441 pixman_image_set_filter (pixman_image_t *image,
442 pixman_filter_t filter,
443 const pixman_fixed_t *params,
446 image_common_t *common = (image_common_t *)image;
447 pixman_fixed_t *new_params;
449 if (params == common->filter_params && filter == common->filter)
455 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
460 params, n_params * sizeof (pixman_fixed_t));
463 common->filter = filter;
465 if (common->filter_params)
466 free (common->filter_params);
468 common->filter_params = new_params;
469 common->n_filter_params = n_params;
471 image_property_changed (image);
476 pixman_image_set_source_clipping (pixman_image_t *image,
477 pixman_bool_t source_clipping)
479 image_common_t *common = &image->common;
482 common->src_clip = &common->clip_region;
484 common->src_clip = &common->full_region;
486 image_property_changed (image);
489 /* Unlike all the other property setters, this function does not
490 * copy the content of indexed. Doing this copying is simply
491 * way, way too expensive.
494 pixman_image_set_indexed (pixman_image_t *image,
495 const pixman_indexed_t *indexed)
497 bits_image_t *bits = (bits_image_t *)image;
499 bits->indexed = indexed;
501 image_property_changed (image);
505 pixman_image_set_alpha_map (pixman_image_t *image,
506 pixman_image_t *alpha_map,
510 image_common_t *common = (image_common_t *)image;
512 return_if_fail (!alpha_map || alpha_map->type == BITS);
514 if (common->alpha_map != (bits_image_t *)alpha_map)
516 if (common->alpha_map)
517 pixman_image_unref ((pixman_image_t *)common->alpha_map);
520 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
522 common->alpha_map = NULL;
525 common->alpha_origin.x = x;
526 common->alpha_origin.y = y;
528 image_property_changed (image);
532 pixman_image_set_component_alpha (pixman_image_t *image,
533 pixman_bool_t component_alpha)
535 image->common.component_alpha = component_alpha;
537 image_property_changed (image);
542 pixman_image_set_accessors (pixman_image_t *image,
543 pixman_read_memory_func_t read_func,
544 pixman_write_memory_func_t write_func)
546 return_if_fail (image != NULL);
548 image->common.read_func = read_func;
549 image->common.write_func = write_func;
551 image_property_changed (image);
554 PIXMAN_EXPORT uint32_t *
555 pixman_image_get_data (pixman_image_t *image)
557 if (image->type == BITS)
558 return image->bits.bits;
564 pixman_image_get_width (pixman_image_t *image)
566 if (image->type == BITS)
567 return image->bits.width;
573 pixman_image_get_height (pixman_image_t *image)
575 if (image->type == BITS)
576 return image->bits.height;
582 pixman_image_get_stride (pixman_image_t *image)
584 if (image->type == BITS)
585 return image->bits.rowstride * (int) sizeof (uint32_t);
591 pixman_image_get_depth (pixman_image_t *image)
593 if (image->type == BITS)
594 return PIXMAN_FORMAT_DEPTH (image->bits.format);
600 color_to_uint32 (const pixman_color_t *color)
603 (color->alpha >> 8 << 24) |
604 (color->red >> 8 << 16) |
605 (color->green & 0xff00) |
610 color_to_pixel (pixman_color_t *color,
612 pixman_format_code_t format)
614 uint32_t c = color_to_uint32 (color);
616 if (!(format == PIXMAN_a8r8g8b8 ||
617 format == PIXMAN_x8r8g8b8 ||
618 format == PIXMAN_a8b8g8r8 ||
619 format == PIXMAN_x8b8g8r8 ||
620 format == PIXMAN_b8g8r8a8 ||
621 format == PIXMAN_b8g8r8x8 ||
622 format == PIXMAN_r5g6b5 ||
623 format == PIXMAN_b5g6r5 ||
624 format == PIXMAN_a8))
629 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
631 c = ((c & 0xff000000) >> 0) |
632 ((c & 0x00ff0000) >> 16) |
633 ((c & 0x0000ff00) >> 0) |
634 ((c & 0x000000ff) << 16);
636 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
638 c = ((c & 0xff000000) >> 24) |
639 ((c & 0x00ff0000) >> 8) |
640 ((c & 0x0000ff00) << 8) |
641 ((c & 0x000000ff) << 24);
644 if (format == PIXMAN_a8)
646 else if (format == PIXMAN_r5g6b5 ||
647 format == PIXMAN_b5g6r5)
648 c = cvt8888to0565 (c);
651 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
652 printf ("pixel: %x\n", c);
659 PIXMAN_EXPORT pixman_bool_t
660 pixman_image_fill_rectangles (pixman_op_t op,
661 pixman_image_t *dest,
662 pixman_color_t *color,
664 const pixman_rectangle16_t *rects)
666 pixman_image_t *solid;
670 if (color->alpha == 0xffff)
672 if (op == PIXMAN_OP_OVER)
676 if (op == PIXMAN_OP_CLEAR)
688 if (op == PIXMAN_OP_SRC)
692 if (color_to_pixel (color, &pixel, dest->bits.format))
694 for (i = 0; i < n_rects; ++i)
696 pixman_region32_t fill_region;
698 pixman_box32_t *boxes;
700 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
701 if (!pixman_region32_intersect (&fill_region,
703 &dest->common.clip_region))
707 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
708 for (j = 0; j < n_boxes; ++j)
710 const pixman_box32_t *box = &(boxes[j]);
711 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
712 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
716 pixman_region32_fini (&fill_region);
722 solid = pixman_image_create_solid_fill (color);
726 for (i = 0; i < n_rects; ++i)
728 const pixman_rectangle16_t *rect = &(rects[i]);
730 pixman_image_composite (op, solid, NULL, dest,
733 rect->width, rect->height);
736 pixman_image_unref (solid);
742 pixman_image_can_get_solid (pixman_image_t *image)
744 if (image->type == SOLID)
747 if (image->type != BITS ||
748 image->bits.width != 1 ||
749 image->bits.height != 1)
754 if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
757 switch (image->bits.format)
759 case PIXMAN_a8r8g8b8:
760 case PIXMAN_x8r8g8b8:
761 case PIXMAN_a8b8g8r8:
762 case PIXMAN_x8b8g8r8:
763 case PIXMAN_b8g8r8a8:
764 case PIXMAN_b8g8r8x8:
776 pixman_image_is_opaque(pixman_image_t *image)
779 int gradientNumberOfColors = 0;
781 if(image->common.alpha_map)
787 if(PIXMAN_FORMAT_A(image->bits.format))
794 gradientNumberOfColors = image->gradient.n_stops;
796 while(i<gradientNumberOfColors)
798 if(image->gradient.stops[i].color.alpha != 0xffff)
805 if(Alpha(image->solid.color) != 0xff)
810 /* Convolution filters can introduce translucency if the sum of the weights
812 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
815 if (image->common.repeat == PIXMAN_REPEAT_NONE)
817 if (image->common.filter != PIXMAN_FILTER_NEAREST)
820 if (image->common.transform)
823 /* Gradients do not necessarily cover the entire compositing area */
824 if (image->type == LINEAR || image->type == CONICAL || image->type == RADIAL)