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 == SOLID)
48 pict->class = SOURCE_IMAGE_CLASS_HORIZONTAL;
50 else if (pict->common.type == LINEAR)
52 linear_gradient_t *linear = (linear_gradient_t *)pict;
54 pixman_fixed_32_32_t l;
55 pixman_fixed_48_16_t dx, dy, a, b, off;
56 pixman_fixed_48_16_t factors[4];
59 dx = linear->p2.x - linear->p1.x;
60 dy = linear->p2.y - linear->p1.y;
61 l = dx * dx + dy * dy;
72 off = (-a * linear->p1.x
73 -b * linear->p1.y) >> 16;
75 for (i = 0; i < 3; i++)
77 v.vector[0] = pixman_int_to_fixed ((i % 2) * (width - 1) + x);
78 v.vector[1] = pixman_int_to_fixed ((i / 2) * (height - 1) + y);
79 v.vector[2] = pixman_fixed_1;
81 if (pict->common.transform)
83 if (!pixman_transform_point_3d (pict->common.transform, &v))
85 pict->class = SOURCE_IMAGE_CLASS_UNKNOWN;
90 factors[i] = ((a * v.vector[0] + b * v.vector[1]) >> 16) + off;
93 if (factors[2] == factors[0])
94 pict->class = SOURCE_IMAGE_CLASS_HORIZONTAL;
95 else if (factors[1] == factors[0])
96 pict->class = SOURCE_IMAGE_CLASS_VERTICAL;
104 init_source_image (source_image_t *image)
106 image->class = SOURCE_IMAGE_CLASS_UNKNOWN;
107 image->common.classify = SourcePictureClassify;
111 init_gradient (gradient_t *gradient,
112 const pixman_gradient_stop_t *stops,
115 return_val_if_fail (n_stops > 0, FALSE);
117 init_source_image (&gradient->common);
119 gradient->stops = pixman_malloc_ab (n_stops, sizeof (pixman_gradient_stop_t));
120 if (!gradient->stops)
123 memcpy (gradient->stops, stops, n_stops * sizeof (pixman_gradient_stop_t));
125 gradient->n_stops = n_stops;
127 gradient->stop_range = 0xffff;
128 gradient->color_table = NULL;
129 gradient->color_table_size = 0;
135 color_to_uint32 (const pixman_color_t *color)
138 (color->alpha >> 8 << 24) |
139 (color->red >> 8 << 16) |
140 (color->green & 0xff00) |
144 static pixman_image_t *
145 allocate_image (void)
147 pixman_image_t *image = malloc (sizeof (pixman_image_t));
151 image_common_t *common = &image->common;
153 pixman_region32_init (&common->full_region);
154 pixman_region32_init (&common->clip_region);
155 common->src_clip = &common->full_region;
156 common->has_client_clip = FALSE;
157 common->transform = NULL;
158 common->repeat = PIXMAN_REPEAT_NONE;
159 common->filter = PIXMAN_FILTER_NEAREST;
160 common->filter_params = NULL;
161 common->n_filter_params = 0;
162 common->alpha_map = NULL;
163 common->component_alpha = FALSE;
164 common->ref_count = 1;
165 common->read_func = NULL;
166 common->write_func = NULL;
167 common->classify = NULL;
174 _pixman_image_classify (pixman_image_t *image,
180 if (image->common.classify)
181 return image->common.classify (image, x, y, width, height);
183 return SOURCE_IMAGE_CLASS_UNKNOWN;
186 #define READ_ACCESS(f) ((image->common.read_func)? f##_accessors : f)
188 static void fbFetchSolid(bits_image_t * image, int x, int y, int width, uint32_t *buffer, uint32_t *mask, uint32_t maskBits)
192 fetchPixelProc32 fetch = READ_ACCESS(pixman_fetchPixelProcForPicture32)(image);
194 color = fetch(image, 0, 0);
196 end = buffer + width;
201 static void fbFetchSolid64(bits_image_t * image, int x, int y, int width, uint64_t *buffer, void *unused, uint32_t unused2)
205 fetchPixelProc64 fetch = READ_ACCESS(pixman_fetchPixelProcForPicture64)(image);
207 color = fetch(image, 0, 0);
209 end = buffer + width;
214 static void fbFetch(bits_image_t * image, int x, int y, int width, uint32_t *buffer, uint32_t *mask, uint32_t maskBits)
216 fetchProc32 fetch = READ_ACCESS(pixman_fetchProcForPicture32)(image);
218 fetch(image, x, y, width, buffer);
221 static void fbFetch64(bits_image_t * image, int x, int y, int width, uint64_t *buffer, void *unused, uint32_t unused2)
223 fetchProc64 fetch = READ_ACCESS(pixman_fetchProcForPicture64)(image);
225 fetch(image, x, y, width, buffer);
229 _pixman_image_get_fetcher (pixman_image_t *image,
232 if (IS_SOURCE_IMAGE (image))
235 return (scanFetchProc)pixmanFetchSourcePict64;
237 return (scanFetchProc)pixmanFetchSourcePict;
241 bits_image_t *bits = (bits_image_t *)image;
243 if (bits->common.alpha_map)
246 return (scanFetchProc)READ_ACCESS(fbFetchExternalAlpha64);
248 return (scanFetchProc)READ_ACCESS(fbFetchExternalAlpha);
250 else if ((bits->common.repeat != PIXMAN_REPEAT_NONE) &&
255 return (scanFetchProc)fbFetchSolid64;
257 return (scanFetchProc)fbFetchSolid;
259 else if (!bits->common.transform && bits->common.filter != PIXMAN_FILTER_CONVOLUTION
260 && bits->common.repeat != PIXMAN_REPEAT_PAD && bits->common.repeat != PIXMAN_REPEAT_REFLECT)
263 return (scanFetchProc)fbFetch64;
265 return (scanFetchProc)fbFetch;
270 return (scanFetchProc)READ_ACCESS(fbFetchTransformed64);
272 return (scanFetchProc)READ_ACCESS(fbFetchTransformed);
277 #define WRITE_ACCESS(f) ((image->common.write_func)? f##_accessors : f)
280 fbStore(bits_image_t * image, int x, int y, int width, uint32_t *buffer)
284 storeProc32 store = WRITE_ACCESS(pixman_storeProcForPicture32)(image);
285 const pixman_indexed_t * indexed = image->indexed;
288 stride = image->rowstride;
290 store((pixman_image_t *)image, bits, buffer, x, width, indexed);
294 fbStore64(bits_image_t * image, int x, int y, int width, uint64_t *buffer)
298 storeProc64 store = WRITE_ACCESS(pixman_storeProcForPicture64)(image);
299 const pixman_indexed_t * indexed = image->indexed;
302 stride = image->rowstride;
304 store((pixman_image_t *)image, bits, buffer, x, width, indexed);
308 _pixman_image_get_storer (pixman_image_t *image,
311 if (image->common.alpha_map)
314 return (scanStoreProc)WRITE_ACCESS(fbStoreExternalAlpha64);
316 return (scanStoreProc)WRITE_ACCESS(fbStoreExternalAlpha);
321 return (scanStoreProc)fbStore64;
323 return (scanStoreProc)fbStore;
328 PIXMAN_EXPORT pixman_image_t *
329 pixman_image_ref (pixman_image_t *image)
331 image->common.ref_count++;
336 /* returns TRUE when the image is freed */
337 PIXMAN_EXPORT pixman_bool_t
338 pixman_image_unref (pixman_image_t *image)
340 image_common_t *common = (image_common_t *)image;
344 if (common->ref_count == 0)
346 pixman_region32_fini (&common->clip_region);
347 pixman_region32_fini (&common->full_region);
349 if (common->transform)
350 free (common->transform);
352 if (common->filter_params)
353 free (common->filter_params);
355 if (common->alpha_map)
356 pixman_image_unref ((pixman_image_t *)common->alpha_map);
359 if (image->type == BITS && image->bits.indexed)
360 free (image->bits.indexed);
364 memset (image, 0xaa, sizeof (pixman_image_t));
366 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
368 if (image->gradient.stops)
369 free (image->gradient.stops);
373 if (image->type == BITS && image->bits.free_me)
374 free (image->bits.free_me);
385 PIXMAN_EXPORT pixman_image_t *
386 pixman_image_create_solid_fill (pixman_color_t *color)
388 pixman_image_t *img = allocate_image();
392 init_source_image (&img->solid.common);
395 img->solid.color = color_to_uint32 (color);
400 PIXMAN_EXPORT pixman_image_t *
401 pixman_image_create_linear_gradient (pixman_point_fixed_t *p1,
402 pixman_point_fixed_t *p2,
403 const pixman_gradient_stop_t *stops,
406 pixman_image_t *image;
407 linear_gradient_t *linear;
409 return_val_if_fail (n_stops >= 2, NULL);
411 image = allocate_image();
416 linear = &image->linear;
418 if (!init_gradient (&linear->common, stops, n_stops))
427 image->type = LINEAR;
433 PIXMAN_EXPORT pixman_image_t *
434 pixman_image_create_radial_gradient (pixman_point_fixed_t *inner,
435 pixman_point_fixed_t *outer,
436 pixman_fixed_t inner_radius,
437 pixman_fixed_t outer_radius,
438 const pixman_gradient_stop_t *stops,
441 pixman_image_t *image;
442 radial_gradient_t *radial;
444 return_val_if_fail (n_stops >= 2, NULL);
446 image = allocate_image();
451 radial = &image->radial;
453 if (!init_gradient (&radial->common, stops, n_stops))
459 image->type = RADIAL;
461 radial->c1.x = inner->x;
462 radial->c1.y = inner->y;
463 radial->c1.radius = inner_radius;
464 radial->c2.x = outer->x;
465 radial->c2.y = outer->y;
466 radial->c2.radius = outer_radius;
467 radial->cdx = pixman_fixed_to_double (radial->c2.x - radial->c1.x);
468 radial->cdy = pixman_fixed_to_double (radial->c2.y - radial->c1.y);
469 radial->dr = pixman_fixed_to_double (radial->c2.radius - radial->c1.radius);
470 radial->A = (radial->cdx * radial->cdx
471 + radial->cdy * radial->cdy
472 - radial->dr * radial->dr);
477 PIXMAN_EXPORT pixman_image_t *
478 pixman_image_create_conical_gradient (pixman_point_fixed_t *center,
479 pixman_fixed_t angle,
480 const pixman_gradient_stop_t *stops,
483 pixman_image_t *image = allocate_image();
484 conical_gradient_t *conical;
489 conical = &image->conical;
491 if (!init_gradient (&conical->common, stops, n_stops))
497 image->type = CONICAL;
498 conical->center = *center;
499 conical->angle = angle;
505 create_bits (pixman_format_code_t format,
508 int *rowstride_bytes)
514 /* what follows is a long-winded way, avoiding any possibility of integer
515 * overflows, of saying:
516 * stride = ((width * bpp + FB_MASK) >> FB_SHIFT) * sizeof (uint32_t);
519 bpp = PIXMAN_FORMAT_BPP (format);
520 if (pixman_multiply_overflows_int (width, bpp))
523 stride = width * bpp;
524 if (pixman_addition_overflows_int (stride, FB_MASK))
531 if (pixman_multiply_overflows_int (stride, sizeof (uint32_t)))
534 stride *= sizeof (uint32_t);
536 if (pixman_multiply_overflows_int (height, stride))
539 buf_size = height * stride;
542 *rowstride_bytes = stride;
544 return calloc (buf_size, 1);
548 reset_clip_region (pixman_image_t *image)
550 pixman_region32_fini (&image->common.clip_region);
552 if (image->type == BITS)
554 pixman_region32_init_rect (&image->common.clip_region, 0, 0,
555 image->bits.width, image->bits.height);
559 pixman_region32_init (&image->common.clip_region);
563 PIXMAN_EXPORT pixman_image_t *
564 pixman_image_create_bits (pixman_format_code_t format,
570 pixman_image_t *image;
571 uint32_t *free_me = NULL;
573 /* must be a whole number of uint32_t's
575 return_val_if_fail (bits == NULL ||
576 (rowstride_bytes % sizeof (uint32_t)) == 0, NULL);
578 if (!bits && width && height)
580 free_me = bits = create_bits (format, width, height, &rowstride_bytes);
585 image = allocate_image();
594 image->bits.format = format;
595 image->bits.width = width;
596 image->bits.height = height;
597 image->bits.bits = bits;
598 image->bits.free_me = free_me;
600 image->bits.rowstride = rowstride_bytes / (int) sizeof (uint32_t); /* we store it in number
603 image->bits.indexed = NULL;
605 pixman_region32_fini (&image->common.full_region);
606 pixman_region32_init_rect (&image->common.full_region, 0, 0,
607 image->bits.width, image->bits.height);
609 reset_clip_region (image);
613 PIXMAN_EXPORT pixman_bool_t
614 pixman_image_set_clip_region32 (pixman_image_t *image,
615 pixman_region32_t *region)
617 image_common_t *common = (image_common_t *)image;
621 return pixman_region32_copy (&common->clip_region, region);
625 reset_clip_region (image);
632 PIXMAN_EXPORT pixman_bool_t
633 pixman_image_set_clip_region (pixman_image_t *image,
634 pixman_region16_t *region)
636 image_common_t *common = (image_common_t *)image;
640 return pixman_region32_copy_from_region16 (&common->clip_region, region);
644 reset_clip_region (image);
650 /* Sets whether the clip region includes a clip region set by the client
653 pixman_image_set_has_client_clip (pixman_image_t *image,
654 pixman_bool_t client_clip)
656 image->common.has_client_clip = client_clip;
659 PIXMAN_EXPORT pixman_bool_t
660 pixman_image_set_transform (pixman_image_t *image,
661 const pixman_transform_t *transform)
663 static const pixman_transform_t id =
665 { { pixman_fixed_1, 0, 0 },
666 { 0, pixman_fixed_1, 0 },
667 { 0, 0, pixman_fixed_1 }
671 image_common_t *common = (image_common_t *)image;
673 if (common->transform == transform)
676 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
678 free(common->transform);
679 common->transform = NULL;
683 if (common->transform == NULL)
684 common->transform = malloc (sizeof (pixman_transform_t));
685 if (common->transform == NULL)
688 memcpy(common->transform, transform, sizeof(pixman_transform_t));
694 pixman_image_set_repeat (pixman_image_t *image,
695 pixman_repeat_t repeat)
697 image->common.repeat = repeat;
700 PIXMAN_EXPORT pixman_bool_t
701 pixman_image_set_filter (pixman_image_t *image,
702 pixman_filter_t filter,
703 const pixman_fixed_t *params,
706 image_common_t *common = (image_common_t *)image;
707 pixman_fixed_t *new_params;
709 if (params == common->filter_params && filter == common->filter)
715 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
720 params, n_params * sizeof (pixman_fixed_t));
723 common->filter = filter;
725 if (common->filter_params)
726 free (common->filter_params);
728 common->filter_params = new_params;
729 common->n_filter_params = n_params;
734 pixman_image_set_source_clipping (pixman_image_t *image,
735 pixman_bool_t source_clipping)
737 image_common_t *common = &image->common;
740 common->src_clip = &common->clip_region;
742 common->src_clip = &common->full_region;
745 /* Unlike all the other property setters, this function does not
746 * copy the content of indexed. Doing this copying is simply
747 * way, way too expensive.
750 pixman_image_set_indexed (pixman_image_t *image,
751 const pixman_indexed_t *indexed)
753 bits_image_t *bits = (bits_image_t *)image;
755 bits->indexed = indexed;
759 pixman_image_set_alpha_map (pixman_image_t *image,
760 pixman_image_t *alpha_map,
764 image_common_t *common = (image_common_t *)image;
766 return_if_fail (!alpha_map || alpha_map->type == BITS);
768 if (common->alpha_map != (bits_image_t *)alpha_map)
770 if (common->alpha_map)
771 pixman_image_unref ((pixman_image_t *)common->alpha_map);
774 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
776 common->alpha_map = NULL;
779 common->alpha_origin.x = x;
780 common->alpha_origin.y = y;
784 pixman_image_set_component_alpha (pixman_image_t *image,
785 pixman_bool_t component_alpha)
787 image->common.component_alpha = component_alpha;
792 pixman_image_set_accessors (pixman_image_t *image,
793 pixman_read_memory_func_t read_func,
794 pixman_write_memory_func_t write_func)
796 return_if_fail (image != NULL);
798 image->common.read_func = read_func;
799 image->common.write_func = write_func;
802 PIXMAN_EXPORT uint32_t *
803 pixman_image_get_data (pixman_image_t *image)
805 if (image->type == BITS)
806 return image->bits.bits;
812 pixman_image_get_width (pixman_image_t *image)
814 if (image->type == BITS)
815 return image->bits.width;
821 pixman_image_get_height (pixman_image_t *image)
823 if (image->type == BITS)
824 return image->bits.height;
830 pixman_image_get_stride (pixman_image_t *image)
832 if (image->type == BITS)
833 return image->bits.rowstride * (int) sizeof (uint32_t);
839 pixman_image_get_depth (pixman_image_t *image)
841 if (image->type == BITS)
842 return PIXMAN_FORMAT_DEPTH (image->bits.format);
848 color_to_pixel (pixman_color_t *color,
850 pixman_format_code_t format)
852 uint32_t c = color_to_uint32 (color);
854 if (!(format == PIXMAN_a8r8g8b8 ||
855 format == PIXMAN_x8r8g8b8 ||
856 format == PIXMAN_a8b8g8r8 ||
857 format == PIXMAN_x8b8g8r8 ||
858 format == PIXMAN_b8g8r8a8 ||
859 format == PIXMAN_b8g8r8x8 ||
860 format == PIXMAN_r5g6b5 ||
861 format == PIXMAN_b5g6r5 ||
862 format == PIXMAN_a8))
867 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
869 c = ((c & 0xff000000) >> 0) |
870 ((c & 0x00ff0000) >> 16) |
871 ((c & 0x0000ff00) >> 0) |
872 ((c & 0x000000ff) << 16);
874 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
876 c = ((c & 0xff000000) >> 24) |
877 ((c & 0x00ff0000) >> 8) |
878 ((c & 0x0000ff00) << 8) |
879 ((c & 0x000000ff) << 24);
882 if (format == PIXMAN_a8)
884 else if (format == PIXMAN_r5g6b5 ||
885 format == PIXMAN_b5g6r5)
886 c = cvt8888to0565 (c);
889 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
890 printf ("pixel: %x\n", c);
897 PIXMAN_EXPORT pixman_bool_t
898 pixman_image_fill_rectangles (pixman_op_t op,
899 pixman_image_t *dest,
900 pixman_color_t *color,
902 const pixman_rectangle16_t *rects)
904 pixman_image_t *solid;
908 if (color->alpha == 0xffff)
910 if (op == PIXMAN_OP_OVER)
914 if (op == PIXMAN_OP_CLEAR)
926 if (op == PIXMAN_OP_SRC)
930 if (color_to_pixel (color, &pixel, dest->bits.format))
932 for (i = 0; i < n_rects; ++i)
934 pixman_region32_t fill_region;
936 pixman_box32_t *boxes;
938 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
939 if (!pixman_region32_intersect (&fill_region,
941 &dest->common.clip_region))
945 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
946 for (j = 0; j < n_boxes; ++j)
948 const pixman_box32_t *box = &(boxes[j]);
949 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
950 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
954 pixman_region32_fini (&fill_region);
960 solid = pixman_image_create_solid_fill (color);
964 for (i = 0; i < n_rects; ++i)
966 const pixman_rectangle16_t *rect = &(rects[i]);
968 pixman_image_composite (op, solid, NULL, dest,
971 rect->width, rect->height);
974 pixman_image_unref (solid);
980 pixman_image_can_get_solid (pixman_image_t *image)
982 if (image->type == SOLID)
985 if (image->type != BITS ||
986 image->bits.width != 1 ||
987 image->bits.height != 1)
992 if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
995 switch (image->bits.format)
997 case PIXMAN_a8r8g8b8:
998 case PIXMAN_x8r8g8b8:
999 case PIXMAN_a8b8g8r8:
1000 case PIXMAN_x8b8g8r8:
1001 case PIXMAN_b8g8r8a8:
1002 case PIXMAN_b8g8r8x8:
1014 pixman_image_is_opaque(pixman_image_t *image)
1017 int gradientNumberOfColors = 0;
1019 if(image->common.alpha_map)
1025 if(PIXMAN_FORMAT_A(image->bits.format))
1032 gradientNumberOfColors = image->gradient.n_stops;
1034 while(i<gradientNumberOfColors)
1036 if(image->gradient.stops[i].color.alpha != 0xffff)
1043 if(Alpha(image->solid.color) != 0xff)
1048 /* Convolution filters can introduce translucency if the sum of the weights
1050 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
1053 if (image->common.repeat == PIXMAN_REPEAT_NONE)
1055 if (image->common.filter != PIXMAN_FILTER_NEAREST)
1058 if (image->common.transform)
1061 /* Gradients do not necessarily cover the entire compositing area */
1062 if (image->type == LINEAR || image->type == CONICAL || image->type == RADIAL)