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.
31 #include "pixman-private.h"
33 #define Alpha(x) ((x) >> 24)
35 static source_pict_class_t
36 SourcePictureClassify (pixman_image_t *image,
42 source_image_t *pict = &image->source;
44 pict->class = SOURCE_IMAGE_CLASS_UNKNOWN;
46 if (pict->common.type == LINEAR)
48 linear_gradient_t *linear = (linear_gradient_t *)pict;
50 pixman_fixed_32_32_t l;
51 pixman_fixed_48_16_t dx, dy, a, b, off;
52 pixman_fixed_48_16_t factors[4];
55 dx = linear->p2.x - linear->p1.x;
56 dy = linear->p2.y - linear->p1.y;
57 l = dx * dx + dy * dy;
68 off = (-a * linear->p1.x
69 -b * linear->p1.y) >> 16;
71 for (i = 0; i < 3; i++)
73 v.vector[0] = pixman_int_to_fixed ((i % 2) * (width - 1) + x);
74 v.vector[1] = pixman_int_to_fixed ((i / 2) * (height - 1) + y);
75 v.vector[2] = pixman_fixed_1;
77 if (pict->common.transform)
79 if (!pixman_transform_point_3d (pict->common.transform, &v))
81 pict->class = SOURCE_IMAGE_CLASS_UNKNOWN;
86 factors[i] = ((a * v.vector[0] + b * v.vector[1]) >> 16) + off;
89 if (factors[2] == factors[0])
90 pict->class = SOURCE_IMAGE_CLASS_HORIZONTAL;
91 else if (factors[1] == factors[0])
92 pict->class = SOURCE_IMAGE_CLASS_VERTICAL;
100 init_source_image (source_image_t *image)
102 image->class = SOURCE_IMAGE_CLASS_UNKNOWN;
103 image->common.classify = SourcePictureClassify;
107 init_gradient (gradient_t *gradient,
108 const pixman_gradient_stop_t *stops,
111 return_val_if_fail (n_stops > 0, FALSE);
113 init_source_image (&gradient->common);
115 gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
116 if (!gradient->stops)
119 memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
121 gradient->n_stops = n_stops;
123 gradient->stop_range = 0xffff;
124 gradient->color_table = NULL;
125 gradient->color_table_size = 0;
131 _pixman_image_allocate (void)
133 pixman_image_t *image = malloc (sizeof (pixman_image_t));
137 image_common_t *common = &image->common;
139 pixman_region32_init (&common->full_region);
140 pixman_region32_init (&common->clip_region);
141 common->src_clip = &common->full_region;
142 common->has_client_clip = FALSE;
143 common->transform = NULL;
144 common->repeat = PIXMAN_REPEAT_NONE;
145 common->filter = PIXMAN_FILTER_NEAREST;
146 common->filter_params = NULL;
147 common->n_filter_params = 0;
148 common->alpha_map = NULL;
149 common->component_alpha = FALSE;
150 common->ref_count = 1;
151 common->read_func = NULL;
152 common->write_func = NULL;
153 common->classify = NULL;
160 _pixman_image_classify (pixman_image_t *image,
166 if (image->common.classify)
167 return image->common.classify (image, x, y, width, height);
169 return SOURCE_IMAGE_CLASS_UNKNOWN;
172 #define READ_ACCESS(f) ((image->common.read_func)? f##_accessors : f)
174 static void fbFetchSolid(bits_image_t * image, int x, int y, int width, uint32_t *buffer, uint32_t *mask, uint32_t maskBits)
178 fetchPixelProc32 fetch = READ_ACCESS(pixman_fetchPixelProcForPicture32)(image);
180 color = fetch(image, 0, 0);
182 end = buffer + width;
187 static void fbFetchSolid64(bits_image_t * image, int x, int y, int width, uint64_t *buffer, void *unused, uint32_t unused2)
191 fetchPixelProc64 fetch = READ_ACCESS(pixman_fetchPixelProcForPicture64)(image);
193 color = fetch(image, 0, 0);
195 end = buffer + width;
200 static void fbFetch(bits_image_t * image, int x, int y, int width, uint32_t *buffer, uint32_t *mask, uint32_t maskBits)
202 fetchProc32 fetch = READ_ACCESS(pixman_fetchProcForPicture32)(image);
204 fetch(image, x, y, width, buffer);
207 static void fbFetch64(bits_image_t * image, int x, int y, int width, uint64_t *buffer, void *unused, uint32_t unused2)
209 fetchProc64 fetch = READ_ACCESS(pixman_fetchProcForPicture64)(image);
211 fetch(image, x, y, width, buffer);
215 _pixman_image_get_fetcher (pixman_image_t *image,
218 if (IS_SOURCE_IMAGE (image))
221 return (scanFetchProc)pixmanFetchSourcePict64;
223 return (scanFetchProc)pixmanFetchSourcePict;
227 bits_image_t *bits = (bits_image_t *)image;
229 if (bits->common.alpha_map)
232 return (scanFetchProc)READ_ACCESS(fbFetchExternalAlpha64);
234 return (scanFetchProc)READ_ACCESS(fbFetchExternalAlpha);
236 else if ((bits->common.repeat != PIXMAN_REPEAT_NONE) &&
241 return (scanFetchProc)fbFetchSolid64;
243 return (scanFetchProc)fbFetchSolid;
245 else if (!bits->common.transform && bits->common.filter != PIXMAN_FILTER_CONVOLUTION
246 && bits->common.repeat != PIXMAN_REPEAT_PAD && bits->common.repeat != PIXMAN_REPEAT_REFLECT)
249 return (scanFetchProc)fbFetch64;
251 return (scanFetchProc)fbFetch;
256 return (scanFetchProc)READ_ACCESS(fbFetchTransformed64);
258 return (scanFetchProc)READ_ACCESS(fbFetchTransformed);
265 #define WRITE_ACCESS(f) ((image->common.write_func)? f##_accessors : f)
268 fbStore(bits_image_t * image, int x, int y, int width, uint32_t *buffer)
272 storeProc32 store = WRITE_ACCESS(pixman_storeProcForPicture32)(image);
273 const pixman_indexed_t * indexed = image->indexed;
276 stride = image->rowstride;
278 store((pixman_image_t *)image, bits, buffer, x, width, indexed);
282 fbStore64(bits_image_t * image, int x, int y, int width, uint64_t *buffer)
286 storeProc64 store = WRITE_ACCESS(pixman_storeProcForPicture64)(image);
287 const pixman_indexed_t * indexed = image->indexed;
290 stride = image->rowstride;
292 store((pixman_image_t *)image, bits, buffer, x, width, indexed);
296 _pixman_image_get_storer (pixman_image_t *image,
299 if (image->common.alpha_map)
302 return (scanStoreProc)WRITE_ACCESS(fbStoreExternalAlpha64);
304 return (scanStoreProc)WRITE_ACCESS(fbStoreExternalAlpha);
309 return (scanStoreProc)fbStore64;
311 return (scanStoreProc)fbStore;
316 PIXMAN_EXPORT pixman_image_t *
317 pixman_image_ref (pixman_image_t *image)
319 image->common.ref_count++;
324 /* returns TRUE when the image is freed */
325 PIXMAN_EXPORT pixman_bool_t
326 pixman_image_unref (pixman_image_t *image)
328 image_common_t *common = (image_common_t *)image;
332 if (common->ref_count == 0)
334 pixman_region32_fini (&common->clip_region);
335 pixman_region32_fini (&common->full_region);
337 if (common->transform)
338 free (common->transform);
340 if (common->filter_params)
341 free (common->filter_params);
343 if (common->alpha_map)
344 pixman_image_unref ((pixman_image_t *)common->alpha_map);
347 if (image->type == BITS && image->bits.indexed)
348 free (image->bits.indexed);
352 memset (image, 0xaa, sizeof (pixman_image_t));
354 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
356 if (image->gradient.stops)
357 free (image->gradient.stops);
361 if (image->type == BITS && image->bits.free_me)
362 free (image->bits.free_me);
373 PIXMAN_EXPORT pixman_image_t *
374 pixman_image_create_linear_gradient (pixman_point_fixed_t *p1,
375 pixman_point_fixed_t *p2,
376 const pixman_gradient_stop_t *stops,
379 pixman_image_t *image;
380 linear_gradient_t *linear;
382 return_val_if_fail (n_stops >= 2, NULL);
384 image = _pixman_image_allocate();
389 linear = &image->linear;
391 if (!init_gradient (&linear->common, stops, n_stops))
400 image->type = LINEAR;
406 PIXMAN_EXPORT pixman_image_t *
407 pixman_image_create_radial_gradient (pixman_point_fixed_t *inner,
408 pixman_point_fixed_t *outer,
409 pixman_fixed_t inner_radius,
410 pixman_fixed_t outer_radius,
411 const pixman_gradient_stop_t *stops,
414 pixman_image_t *image;
415 radial_gradient_t *radial;
417 return_val_if_fail (n_stops >= 2, NULL);
419 image = _pixman_image_allocate();
424 radial = &image->radial;
426 if (!init_gradient (&radial->common, stops, n_stops))
432 image->type = RADIAL;
434 radial->c1.x = inner->x;
435 radial->c1.y = inner->y;
436 radial->c1.radius = inner_radius;
437 radial->c2.x = outer->x;
438 radial->c2.y = outer->y;
439 radial->c2.radius = outer_radius;
440 radial->cdx = pixman_fixed_to_double (radial->c2.x - radial->c1.x);
441 radial->cdy = pixman_fixed_to_double (radial->c2.y - radial->c1.y);
442 radial->dr = pixman_fixed_to_double (radial->c2.radius - radial->c1.radius);
443 radial->A = (radial->cdx * radial->cdx
444 + radial->cdy * radial->cdy
445 - radial->dr * radial->dr);
450 PIXMAN_EXPORT pixman_image_t *
451 pixman_image_create_conical_gradient (pixman_point_fixed_t *center,
452 pixman_fixed_t angle,
453 const pixman_gradient_stop_t *stops,
456 pixman_image_t *image = _pixman_image_allocate();
457 conical_gradient_t *conical;
462 conical = &image->conical;
464 if (!init_gradient (&conical->common, stops, n_stops))
470 image->type = CONICAL;
471 conical->center = *center;
472 conical->angle = angle;
478 create_bits (pixman_format_code_t format,
481 int *rowstride_bytes)
487 /* what follows is a long-winded way, avoiding any possibility of integer
488 * overflows, of saying:
489 * stride = ((width * bpp + FB_MASK) >> FB_SHIFT) * sizeof (uint32_t);
492 bpp = PIXMAN_FORMAT_BPP (format);
493 if (pixman_multiply_overflows_int (width, bpp))
496 stride = width * bpp;
497 if (pixman_addition_overflows_int (stride, FB_MASK))
504 if (pixman_multiply_overflows_int (stride, sizeof (uint32_t)))
507 stride *= sizeof (uint32_t);
509 if (pixman_multiply_overflows_int (height, stride))
512 buf_size = height * stride;
515 *rowstride_bytes = stride;
517 return calloc (buf_size, 1);
521 reset_clip_region (pixman_image_t *image)
523 pixman_region32_fini (&image->common.clip_region);
525 if (image->type == BITS)
527 pixman_region32_init_rect (&image->common.clip_region, 0, 0,
528 image->bits.width, image->bits.height);
532 pixman_region32_init (&image->common.clip_region);
536 PIXMAN_EXPORT pixman_image_t *
537 pixman_image_create_bits (pixman_format_code_t format,
543 pixman_image_t *image;
544 uint32_t *free_me = NULL;
546 /* must be a whole number of uint32_t's
548 return_val_if_fail (bits == NULL ||
549 (rowstride_bytes % sizeof (uint32_t)) == 0, NULL);
551 if (!bits && width && height)
553 free_me = bits = create_bits (format, width, height, &rowstride_bytes);
558 image = _pixman_image_allocate();
567 image->bits.format = format;
568 image->bits.width = width;
569 image->bits.height = height;
570 image->bits.bits = bits;
571 image->bits.free_me = free_me;
573 image->bits.rowstride = rowstride_bytes / (int) sizeof (uint32_t); /* we store it in number
576 image->bits.indexed = NULL;
578 pixman_region32_fini (&image->common.full_region);
579 pixman_region32_init_rect (&image->common.full_region, 0, 0,
580 image->bits.width, image->bits.height);
582 reset_clip_region (image);
586 PIXMAN_EXPORT pixman_bool_t
587 pixman_image_set_clip_region32 (pixman_image_t *image,
588 pixman_region32_t *region)
590 image_common_t *common = (image_common_t *)image;
594 return pixman_region32_copy (&common->clip_region, region);
598 reset_clip_region (image);
605 PIXMAN_EXPORT pixman_bool_t
606 pixman_image_set_clip_region (pixman_image_t *image,
607 pixman_region16_t *region)
609 image_common_t *common = (image_common_t *)image;
613 return pixman_region32_copy_from_region16 (&common->clip_region, region);
617 reset_clip_region (image);
623 /* Sets whether the clip region includes a clip region set by the client
626 pixman_image_set_has_client_clip (pixman_image_t *image,
627 pixman_bool_t client_clip)
629 image->common.has_client_clip = client_clip;
632 PIXMAN_EXPORT pixman_bool_t
633 pixman_image_set_transform (pixman_image_t *image,
634 const pixman_transform_t *transform)
636 static const pixman_transform_t id =
638 { { pixman_fixed_1, 0, 0 },
639 { 0, pixman_fixed_1, 0 },
640 { 0, 0, pixman_fixed_1 }
644 image_common_t *common = (image_common_t *)image;
646 if (common->transform == transform)
649 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
651 free(common->transform);
652 common->transform = NULL;
656 if (common->transform == NULL)
657 common->transform = malloc (sizeof (pixman_transform_t));
658 if (common->transform == NULL)
661 memcpy(common->transform, transform, sizeof(pixman_transform_t));
667 pixman_image_set_repeat (pixman_image_t *image,
668 pixman_repeat_t repeat)
670 image->common.repeat = repeat;
673 PIXMAN_EXPORT pixman_bool_t
674 pixman_image_set_filter (pixman_image_t *image,
675 pixman_filter_t filter,
676 const pixman_fixed_t *params,
679 image_common_t *common = (image_common_t *)image;
680 pixman_fixed_t *new_params;
682 if (params == common->filter_params && filter == common->filter)
688 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
693 params, n_params * sizeof (pixman_fixed_t));
696 common->filter = filter;
698 if (common->filter_params)
699 free (common->filter_params);
701 common->filter_params = new_params;
702 common->n_filter_params = n_params;
707 pixman_image_set_source_clipping (pixman_image_t *image,
708 pixman_bool_t source_clipping)
710 image_common_t *common = &image->common;
713 common->src_clip = &common->clip_region;
715 common->src_clip = &common->full_region;
718 /* Unlike all the other property setters, this function does not
719 * copy the content of indexed. Doing this copying is simply
720 * way, way too expensive.
723 pixman_image_set_indexed (pixman_image_t *image,
724 const pixman_indexed_t *indexed)
726 bits_image_t *bits = (bits_image_t *)image;
728 bits->indexed = indexed;
732 pixman_image_set_alpha_map (pixman_image_t *image,
733 pixman_image_t *alpha_map,
737 image_common_t *common = (image_common_t *)image;
739 return_if_fail (!alpha_map || alpha_map->type == BITS);
741 if (common->alpha_map != (bits_image_t *)alpha_map)
743 if (common->alpha_map)
744 pixman_image_unref ((pixman_image_t *)common->alpha_map);
747 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
749 common->alpha_map = NULL;
752 common->alpha_origin.x = x;
753 common->alpha_origin.y = y;
757 pixman_image_set_component_alpha (pixman_image_t *image,
758 pixman_bool_t component_alpha)
760 image->common.component_alpha = component_alpha;
765 pixman_image_set_accessors (pixman_image_t *image,
766 pixman_read_memory_func_t read_func,
767 pixman_write_memory_func_t write_func)
769 return_if_fail (image != NULL);
771 image->common.read_func = read_func;
772 image->common.write_func = write_func;
775 PIXMAN_EXPORT uint32_t *
776 pixman_image_get_data (pixman_image_t *image)
778 if (image->type == BITS)
779 return image->bits.bits;
785 pixman_image_get_width (pixman_image_t *image)
787 if (image->type == BITS)
788 return image->bits.width;
794 pixman_image_get_height (pixman_image_t *image)
796 if (image->type == BITS)
797 return image->bits.height;
803 pixman_image_get_stride (pixman_image_t *image)
805 if (image->type == BITS)
806 return image->bits.rowstride * (int) sizeof (uint32_t);
812 pixman_image_get_depth (pixman_image_t *image)
814 if (image->type == BITS)
815 return PIXMAN_FORMAT_DEPTH (image->bits.format);
821 color_to_uint32 (const pixman_color_t *color)
824 (color->alpha >> 8 << 24) |
825 (color->red >> 8 << 16) |
826 (color->green & 0xff00) |
831 color_to_pixel (pixman_color_t *color,
833 pixman_format_code_t format)
835 uint32_t c = color_to_uint32 (color);
837 if (!(format == PIXMAN_a8r8g8b8 ||
838 format == PIXMAN_x8r8g8b8 ||
839 format == PIXMAN_a8b8g8r8 ||
840 format == PIXMAN_x8b8g8r8 ||
841 format == PIXMAN_b8g8r8a8 ||
842 format == PIXMAN_b8g8r8x8 ||
843 format == PIXMAN_r5g6b5 ||
844 format == PIXMAN_b5g6r5 ||
845 format == PIXMAN_a8))
850 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
852 c = ((c & 0xff000000) >> 0) |
853 ((c & 0x00ff0000) >> 16) |
854 ((c & 0x0000ff00) >> 0) |
855 ((c & 0x000000ff) << 16);
857 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
859 c = ((c & 0xff000000) >> 24) |
860 ((c & 0x00ff0000) >> 8) |
861 ((c & 0x0000ff00) << 8) |
862 ((c & 0x000000ff) << 24);
865 if (format == PIXMAN_a8)
867 else if (format == PIXMAN_r5g6b5 ||
868 format == PIXMAN_b5g6r5)
869 c = cvt8888to0565 (c);
872 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
873 printf ("pixel: %x\n", c);
880 PIXMAN_EXPORT pixman_bool_t
881 pixman_image_fill_rectangles (pixman_op_t op,
882 pixman_image_t *dest,
883 pixman_color_t *color,
885 const pixman_rectangle16_t *rects)
887 pixman_image_t *solid;
891 if (color->alpha == 0xffff)
893 if (op == PIXMAN_OP_OVER)
897 if (op == PIXMAN_OP_CLEAR)
909 if (op == PIXMAN_OP_SRC)
913 if (color_to_pixel (color, &pixel, dest->bits.format))
915 for (i = 0; i < n_rects; ++i)
917 pixman_region32_t fill_region;
919 pixman_box32_t *boxes;
921 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
922 if (!pixman_region32_intersect (&fill_region,
924 &dest->common.clip_region))
928 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
929 for (j = 0; j < n_boxes; ++j)
931 const pixman_box32_t *box = &(boxes[j]);
932 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
933 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
937 pixman_region32_fini (&fill_region);
943 solid = pixman_image_create_solid_fill (color);
947 for (i = 0; i < n_rects; ++i)
949 const pixman_rectangle16_t *rect = &(rects[i]);
951 pixman_image_composite (op, solid, NULL, dest,
954 rect->width, rect->height);
957 pixman_image_unref (solid);
963 pixman_image_can_get_solid (pixman_image_t *image)
965 if (image->type == SOLID)
968 if (image->type != BITS ||
969 image->bits.width != 1 ||
970 image->bits.height != 1)
975 if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
978 switch (image->bits.format)
980 case PIXMAN_a8r8g8b8:
981 case PIXMAN_x8r8g8b8:
982 case PIXMAN_a8b8g8r8:
983 case PIXMAN_x8b8g8r8:
984 case PIXMAN_b8g8r8a8:
985 case PIXMAN_b8g8r8x8:
997 pixman_image_is_opaque(pixman_image_t *image)
1000 int gradientNumberOfColors = 0;
1002 if(image->common.alpha_map)
1008 if(PIXMAN_FORMAT_A(image->bits.format))
1015 gradientNumberOfColors = image->gradient.n_stops;
1017 while(i<gradientNumberOfColors)
1019 if(image->gradient.stops[i].color.alpha != 0xffff)
1026 if(Alpha(image->solid.color) != 0xff)
1031 /* Convolution filters can introduce translucency if the sum of the weights
1033 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
1036 if (image->common.repeat == PIXMAN_REPEAT_NONE)
1038 if (image->common.filter != PIXMAN_FILTER_NEAREST)
1041 if (image->common.transform)
1044 /* Gradients do not necessarily cover the entire compositing area */
1045 if (image->type == LINEAR || image->type == CONICAL || image->type == RADIAL)