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);
279 #define WRITE_ACCESS(f) ((image->common.write_func)? f##_accessors : f)
282 fbStore(bits_image_t * image, int x, int y, int width, uint32_t *buffer)
286 storeProc32 store = WRITE_ACCESS(pixman_storeProcForPicture32)(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 fbStore64(bits_image_t * image, int x, int y, int width, uint64_t *buffer)
300 storeProc64 store = WRITE_ACCESS(pixman_storeProcForPicture64)(image);
301 const pixman_indexed_t * indexed = image->indexed;
304 stride = image->rowstride;
306 store((pixman_image_t *)image, bits, buffer, x, width, indexed);
310 _pixman_image_get_storer (pixman_image_t *image,
313 if (image->common.alpha_map)
316 return (scanStoreProc)WRITE_ACCESS(fbStoreExternalAlpha64);
318 return (scanStoreProc)WRITE_ACCESS(fbStoreExternalAlpha);
323 return (scanStoreProc)fbStore64;
325 return (scanStoreProc)fbStore;
330 PIXMAN_EXPORT pixman_image_t *
331 pixman_image_ref (pixman_image_t *image)
333 image->common.ref_count++;
338 /* returns TRUE when the image is freed */
339 PIXMAN_EXPORT pixman_bool_t
340 pixman_image_unref (pixman_image_t *image)
342 image_common_t *common = (image_common_t *)image;
346 if (common->ref_count == 0)
348 pixman_region32_fini (&common->clip_region);
349 pixman_region32_fini (&common->full_region);
351 if (common->transform)
352 free (common->transform);
354 if (common->filter_params)
355 free (common->filter_params);
357 if (common->alpha_map)
358 pixman_image_unref ((pixman_image_t *)common->alpha_map);
361 if (image->type == BITS && image->bits.indexed)
362 free (image->bits.indexed);
366 memset (image, 0xaa, sizeof (pixman_image_t));
368 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
370 if (image->gradient.stops)
371 free (image->gradient.stops);
375 if (image->type == BITS && image->bits.free_me)
376 free (image->bits.free_me);
387 PIXMAN_EXPORT pixman_image_t *
388 pixman_image_create_solid_fill (pixman_color_t *color)
390 pixman_image_t *img = allocate_image();
394 init_source_image (&img->solid.common);
397 img->solid.color = color_to_uint32 (color);
402 PIXMAN_EXPORT pixman_image_t *
403 pixman_image_create_linear_gradient (pixman_point_fixed_t *p1,
404 pixman_point_fixed_t *p2,
405 const pixman_gradient_stop_t *stops,
408 pixman_image_t *image;
409 linear_gradient_t *linear;
411 return_val_if_fail (n_stops >= 2, NULL);
413 image = allocate_image();
418 linear = &image->linear;
420 if (!init_gradient (&linear->common, stops, n_stops))
429 image->type = LINEAR;
435 PIXMAN_EXPORT pixman_image_t *
436 pixman_image_create_radial_gradient (pixman_point_fixed_t *inner,
437 pixman_point_fixed_t *outer,
438 pixman_fixed_t inner_radius,
439 pixman_fixed_t outer_radius,
440 const pixman_gradient_stop_t *stops,
443 pixman_image_t *image;
444 radial_gradient_t *radial;
446 return_val_if_fail (n_stops >= 2, NULL);
448 image = allocate_image();
453 radial = &image->radial;
455 if (!init_gradient (&radial->common, stops, n_stops))
461 image->type = RADIAL;
463 radial->c1.x = inner->x;
464 radial->c1.y = inner->y;
465 radial->c1.radius = inner_radius;
466 radial->c2.x = outer->x;
467 radial->c2.y = outer->y;
468 radial->c2.radius = outer_radius;
469 radial->cdx = pixman_fixed_to_double (radial->c2.x - radial->c1.x);
470 radial->cdy = pixman_fixed_to_double (radial->c2.y - radial->c1.y);
471 radial->dr = pixman_fixed_to_double (radial->c2.radius - radial->c1.radius);
472 radial->A = (radial->cdx * radial->cdx
473 + radial->cdy * radial->cdy
474 - radial->dr * radial->dr);
479 PIXMAN_EXPORT pixman_image_t *
480 pixman_image_create_conical_gradient (pixman_point_fixed_t *center,
481 pixman_fixed_t angle,
482 const pixman_gradient_stop_t *stops,
485 pixman_image_t *image = allocate_image();
486 conical_gradient_t *conical;
491 conical = &image->conical;
493 if (!init_gradient (&conical->common, stops, n_stops))
499 image->type = CONICAL;
500 conical->center = *center;
501 conical->angle = angle;
507 create_bits (pixman_format_code_t format,
510 int *rowstride_bytes)
516 /* what follows is a long-winded way, avoiding any possibility of integer
517 * overflows, of saying:
518 * stride = ((width * bpp + FB_MASK) >> FB_SHIFT) * sizeof (uint32_t);
521 bpp = PIXMAN_FORMAT_BPP (format);
522 if (pixman_multiply_overflows_int (width, bpp))
525 stride = width * bpp;
526 if (pixman_addition_overflows_int (stride, FB_MASK))
533 if (pixman_multiply_overflows_int (stride, sizeof (uint32_t)))
536 stride *= sizeof (uint32_t);
538 if (pixman_multiply_overflows_int (height, stride))
541 buf_size = height * stride;
544 *rowstride_bytes = stride;
546 return calloc (buf_size, 1);
550 reset_clip_region (pixman_image_t *image)
552 pixman_region32_fini (&image->common.clip_region);
554 if (image->type == BITS)
556 pixman_region32_init_rect (&image->common.clip_region, 0, 0,
557 image->bits.width, image->bits.height);
561 pixman_region32_init (&image->common.clip_region);
565 PIXMAN_EXPORT pixman_image_t *
566 pixman_image_create_bits (pixman_format_code_t format,
572 pixman_image_t *image;
573 uint32_t *free_me = NULL;
575 /* must be a whole number of uint32_t's
577 return_val_if_fail (bits == NULL ||
578 (rowstride_bytes % sizeof (uint32_t)) == 0, NULL);
580 if (!bits && width && height)
582 free_me = bits = create_bits (format, width, height, &rowstride_bytes);
587 image = allocate_image();
596 image->bits.format = format;
597 image->bits.width = width;
598 image->bits.height = height;
599 image->bits.bits = bits;
600 image->bits.free_me = free_me;
602 image->bits.rowstride = rowstride_bytes / (int) sizeof (uint32_t); /* we store it in number
605 image->bits.indexed = NULL;
607 pixman_region32_fini (&image->common.full_region);
608 pixman_region32_init_rect (&image->common.full_region, 0, 0,
609 image->bits.width, image->bits.height);
611 reset_clip_region (image);
615 PIXMAN_EXPORT pixman_bool_t
616 pixman_image_set_clip_region32 (pixman_image_t *image,
617 pixman_region32_t *region)
619 image_common_t *common = (image_common_t *)image;
623 return pixman_region32_copy (&common->clip_region, region);
627 reset_clip_region (image);
634 PIXMAN_EXPORT pixman_bool_t
635 pixman_image_set_clip_region (pixman_image_t *image,
636 pixman_region16_t *region)
638 image_common_t *common = (image_common_t *)image;
642 return pixman_region32_copy_from_region16 (&common->clip_region, region);
646 reset_clip_region (image);
652 /* Sets whether the clip region includes a clip region set by the client
655 pixman_image_set_has_client_clip (pixman_image_t *image,
656 pixman_bool_t client_clip)
658 image->common.has_client_clip = client_clip;
661 PIXMAN_EXPORT pixman_bool_t
662 pixman_image_set_transform (pixman_image_t *image,
663 const pixman_transform_t *transform)
665 static const pixman_transform_t id =
667 { { pixman_fixed_1, 0, 0 },
668 { 0, pixman_fixed_1, 0 },
669 { 0, 0, pixman_fixed_1 }
673 image_common_t *common = (image_common_t *)image;
675 if (common->transform == transform)
678 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
680 free(common->transform);
681 common->transform = NULL;
685 if (common->transform == NULL)
686 common->transform = malloc (sizeof (pixman_transform_t));
687 if (common->transform == NULL)
690 memcpy(common->transform, transform, sizeof(pixman_transform_t));
696 pixman_image_set_repeat (pixman_image_t *image,
697 pixman_repeat_t repeat)
699 image->common.repeat = repeat;
702 PIXMAN_EXPORT pixman_bool_t
703 pixman_image_set_filter (pixman_image_t *image,
704 pixman_filter_t filter,
705 const pixman_fixed_t *params,
708 image_common_t *common = (image_common_t *)image;
709 pixman_fixed_t *new_params;
711 if (params == common->filter_params && filter == common->filter)
717 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
722 params, n_params * sizeof (pixman_fixed_t));
725 common->filter = filter;
727 if (common->filter_params)
728 free (common->filter_params);
730 common->filter_params = new_params;
731 common->n_filter_params = n_params;
736 pixman_image_set_source_clipping (pixman_image_t *image,
737 pixman_bool_t source_clipping)
739 image_common_t *common = &image->common;
742 common->src_clip = &common->clip_region;
744 common->src_clip = &common->full_region;
747 /* Unlike all the other property setters, this function does not
748 * copy the content of indexed. Doing this copying is simply
749 * way, way too expensive.
752 pixman_image_set_indexed (pixman_image_t *image,
753 const pixman_indexed_t *indexed)
755 bits_image_t *bits = (bits_image_t *)image;
757 bits->indexed = indexed;
761 pixman_image_set_alpha_map (pixman_image_t *image,
762 pixman_image_t *alpha_map,
766 image_common_t *common = (image_common_t *)image;
768 return_if_fail (!alpha_map || alpha_map->type == BITS);
770 if (common->alpha_map != (bits_image_t *)alpha_map)
772 if (common->alpha_map)
773 pixman_image_unref ((pixman_image_t *)common->alpha_map);
776 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
778 common->alpha_map = NULL;
781 common->alpha_origin.x = x;
782 common->alpha_origin.y = y;
786 pixman_image_set_component_alpha (pixman_image_t *image,
787 pixman_bool_t component_alpha)
789 image->common.component_alpha = component_alpha;
794 pixman_image_set_accessors (pixman_image_t *image,
795 pixman_read_memory_func_t read_func,
796 pixman_write_memory_func_t write_func)
798 return_if_fail (image != NULL);
800 image->common.read_func = read_func;
801 image->common.write_func = write_func;
804 PIXMAN_EXPORT uint32_t *
805 pixman_image_get_data (pixman_image_t *image)
807 if (image->type == BITS)
808 return image->bits.bits;
814 pixman_image_get_width (pixman_image_t *image)
816 if (image->type == BITS)
817 return image->bits.width;
823 pixman_image_get_height (pixman_image_t *image)
825 if (image->type == BITS)
826 return image->bits.height;
832 pixman_image_get_stride (pixman_image_t *image)
834 if (image->type == BITS)
835 return image->bits.rowstride * (int) sizeof (uint32_t);
841 pixman_image_get_depth (pixman_image_t *image)
843 if (image->type == BITS)
844 return PIXMAN_FORMAT_DEPTH (image->bits.format);
850 color_to_pixel (pixman_color_t *color,
852 pixman_format_code_t format)
854 uint32_t c = color_to_uint32 (color);
856 if (!(format == PIXMAN_a8r8g8b8 ||
857 format == PIXMAN_x8r8g8b8 ||
858 format == PIXMAN_a8b8g8r8 ||
859 format == PIXMAN_x8b8g8r8 ||
860 format == PIXMAN_b8g8r8a8 ||
861 format == PIXMAN_b8g8r8x8 ||
862 format == PIXMAN_r5g6b5 ||
863 format == PIXMAN_b5g6r5 ||
864 format == PIXMAN_a8))
869 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
871 c = ((c & 0xff000000) >> 0) |
872 ((c & 0x00ff0000) >> 16) |
873 ((c & 0x0000ff00) >> 0) |
874 ((c & 0x000000ff) << 16);
876 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
878 c = ((c & 0xff000000) >> 24) |
879 ((c & 0x00ff0000) >> 8) |
880 ((c & 0x0000ff00) << 8) |
881 ((c & 0x000000ff) << 24);
884 if (format == PIXMAN_a8)
886 else if (format == PIXMAN_r5g6b5 ||
887 format == PIXMAN_b5g6r5)
888 c = cvt8888to0565 (c);
891 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
892 printf ("pixel: %x\n", c);
899 PIXMAN_EXPORT pixman_bool_t
900 pixman_image_fill_rectangles (pixman_op_t op,
901 pixman_image_t *dest,
902 pixman_color_t *color,
904 const pixman_rectangle16_t *rects)
906 pixman_image_t *solid;
910 if (color->alpha == 0xffff)
912 if (op == PIXMAN_OP_OVER)
916 if (op == PIXMAN_OP_CLEAR)
928 if (op == PIXMAN_OP_SRC)
932 if (color_to_pixel (color, &pixel, dest->bits.format))
934 for (i = 0; i < n_rects; ++i)
936 pixman_region32_t fill_region;
938 pixman_box32_t *boxes;
940 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
941 if (!pixman_region32_intersect (&fill_region,
943 &dest->common.clip_region))
947 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
948 for (j = 0; j < n_boxes; ++j)
950 const pixman_box32_t *box = &(boxes[j]);
951 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
952 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
956 pixman_region32_fini (&fill_region);
962 solid = pixman_image_create_solid_fill (color);
966 for (i = 0; i < n_rects; ++i)
968 const pixman_rectangle16_t *rect = &(rects[i]);
970 pixman_image_composite (op, solid, NULL, dest,
973 rect->width, rect->height);
976 pixman_image_unref (solid);
982 pixman_image_can_get_solid (pixman_image_t *image)
984 if (image->type == SOLID)
987 if (image->type != BITS ||
988 image->bits.width != 1 ||
989 image->bits.height != 1)
994 if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
997 switch (image->bits.format)
999 case PIXMAN_a8r8g8b8:
1000 case PIXMAN_x8r8g8b8:
1001 case PIXMAN_a8b8g8r8:
1002 case PIXMAN_x8b8g8r8:
1003 case PIXMAN_b8g8r8a8:
1004 case PIXMAN_b8g8r8x8:
1016 pixman_image_is_opaque(pixman_image_t *image)
1019 int gradientNumberOfColors = 0;
1021 if(image->common.alpha_map)
1027 if(PIXMAN_FORMAT_A(image->bits.format))
1034 gradientNumberOfColors = image->gradient.n_stops;
1036 while(i<gradientNumberOfColors)
1038 if(image->gradient.stops[i].color.alpha != 0xffff)
1045 if(Alpha(image->solid.color) != 0xff)
1050 /* Convolution filters can introduce translucency if the sum of the weights
1052 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
1055 if (image->common.repeat == PIXMAN_REPEAT_NONE)
1057 if (image->common.filter != PIXMAN_FILTER_NEAREST)
1060 if (image->common.transform)
1063 /* Gradients do not necessarily cover the entire compositing area */
1064 if (image->type == LINEAR || image->type == CONICAL || image->type == RADIAL)