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)
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->color_table = NULL;
52 gradient->color_table_size = 0;
53 gradient->common.class = SOURCE_IMAGE_CLASS_UNKNOWN;
59 _pixman_image_allocate (void)
61 pixman_image_t *image = malloc (sizeof (pixman_image_t));
65 image_common_t *common = &image->common;
67 pixman_region32_init (&common->full_region);
68 pixman_region32_init (&common->clip_region);
69 common->src_clip = &common->full_region;
70 common->has_client_clip = FALSE;
71 common->transform = NULL;
72 common->repeat = PIXMAN_REPEAT_NONE;
73 common->filter = PIXMAN_FILTER_NEAREST;
74 common->filter_params = NULL;
75 common->n_filter_params = 0;
76 common->alpha_map = NULL;
77 common->component_alpha = FALSE;
78 common->ref_count = 1;
79 common->read_func = NULL;
80 common->write_func = NULL;
81 common->classify = NULL;
88 _pixman_image_classify (pixman_image_t *image,
94 if (image->common.classify)
95 return image->common.classify (image, x, y, width, height);
97 return SOURCE_IMAGE_CLASS_UNKNOWN;
100 #define READ_ACCESS(f) ((image->common.read_func)? f##_accessors : f)
102 static void fbFetchSolid(bits_image_t * image, int x, int y, int width, uint32_t *buffer, uint32_t *mask, uint32_t maskBits)
106 fetchPixelProc32 fetch = READ_ACCESS(pixman_fetchPixelProcForPicture32)(image);
108 color = fetch(image, 0, 0);
110 end = buffer + width;
115 static void fbFetchSolid64(bits_image_t * image, int x, int y, int width, uint64_t *buffer, void *unused, uint32_t unused2)
119 fetchPixelProc64 fetch = READ_ACCESS(pixman_fetchPixelProcForPicture64)(image);
121 color = fetch(image, 0, 0);
123 end = buffer + width;
128 static void fbFetch(bits_image_t * image, int x, int y, int width, uint32_t *buffer, uint32_t *mask, uint32_t maskBits)
130 fetchProc32 fetch = READ_ACCESS(pixman_fetchProcForPicture32)(image);
132 fetch(image, x, y, width, buffer);
135 static void fbFetch64(bits_image_t * image, int x, int y, int width, uint64_t *buffer, void *unused, uint32_t unused2)
137 fetchProc64 fetch = READ_ACCESS(pixman_fetchProcForPicture64)(image);
139 fetch(image, x, y, width, buffer);
143 _pixman_image_get_fetcher (pixman_image_t *image,
146 if (IS_SOURCE_IMAGE (image))
149 return (scanFetchProc)pixmanFetchSourcePict64;
151 return (scanFetchProc)pixmanFetchSourcePict;
155 bits_image_t *bits = (bits_image_t *)image;
157 if (bits->common.alpha_map)
160 return (scanFetchProc)READ_ACCESS(fbFetchExternalAlpha64);
162 return (scanFetchProc)READ_ACCESS(fbFetchExternalAlpha);
164 else if ((bits->common.repeat != PIXMAN_REPEAT_NONE) &&
169 return (scanFetchProc)fbFetchSolid64;
171 return (scanFetchProc)fbFetchSolid;
173 else if (!bits->common.transform && bits->common.filter != PIXMAN_FILTER_CONVOLUTION
174 && bits->common.repeat != PIXMAN_REPEAT_PAD && bits->common.repeat != PIXMAN_REPEAT_REFLECT)
177 return (scanFetchProc)fbFetch64;
179 return (scanFetchProc)fbFetch;
184 return (scanFetchProc)READ_ACCESS(fbFetchTransformed64);
186 return (scanFetchProc)READ_ACCESS(fbFetchTransformed);
193 #define WRITE_ACCESS(f) ((image->common.write_func)? f##_accessors : f)
196 fbStore(bits_image_t * image, int x, int y, int width, uint32_t *buffer)
200 storeProc32 store = WRITE_ACCESS(pixman_storeProcForPicture32)(image);
201 const pixman_indexed_t * indexed = image->indexed;
204 stride = image->rowstride;
206 store((pixman_image_t *)image, bits, buffer, x, width, indexed);
210 fbStore64(bits_image_t * image, int x, int y, int width, uint64_t *buffer)
214 storeProc64 store = WRITE_ACCESS(pixman_storeProcForPicture64)(image);
215 const pixman_indexed_t * indexed = image->indexed;
218 stride = image->rowstride;
220 store((pixman_image_t *)image, bits, buffer, x, width, indexed);
224 _pixman_image_get_storer (pixman_image_t *image,
227 if (image->common.alpha_map)
230 return (scanStoreProc)WRITE_ACCESS(fbStoreExternalAlpha64);
232 return (scanStoreProc)WRITE_ACCESS(fbStoreExternalAlpha);
237 return (scanStoreProc)fbStore64;
239 return (scanStoreProc)fbStore;
244 image_property_changed (pixman_image_t *image)
246 image->common.property_changed (image);
250 PIXMAN_EXPORT pixman_image_t *
251 pixman_image_ref (pixman_image_t *image)
253 image->common.ref_count++;
258 /* returns TRUE when the image is freed */
259 PIXMAN_EXPORT pixman_bool_t
260 pixman_image_unref (pixman_image_t *image)
262 image_common_t *common = (image_common_t *)image;
266 if (common->ref_count == 0)
268 pixman_region32_fini (&common->clip_region);
269 pixman_region32_fini (&common->full_region);
271 if (common->transform)
272 free (common->transform);
274 if (common->filter_params)
275 free (common->filter_params);
277 if (common->alpha_map)
278 pixman_image_unref ((pixman_image_t *)common->alpha_map);
281 if (image->type == BITS && image->bits.indexed)
282 free (image->bits.indexed);
286 memset (image, 0xaa, sizeof (pixman_image_t));
288 if (image->type == LINEAR || image->type == RADIAL || image->type == CONICAL)
290 if (image->gradient.stops)
291 free (image->gradient.stops);
295 if (image->type == BITS && image->bits.free_me)
296 free (image->bits.free_me);
309 _pixman_image_reset_clip_region (pixman_image_t *image)
311 pixman_region32_fini (&image->common.clip_region);
313 if (image->type == BITS)
315 pixman_region32_init_rect (&image->common.clip_region, 0, 0,
316 image->bits.width, image->bits.height);
320 pixman_region32_init (&image->common.clip_region);
324 PIXMAN_EXPORT pixman_bool_t
325 pixman_image_set_clip_region32 (pixman_image_t *image,
326 pixman_region32_t *region)
328 image_common_t *common = (image_common_t *)image;
329 pixman_bool_t result;
333 result = pixman_region32_copy (&common->clip_region, region);
337 _pixman_image_reset_clip_region (image);
342 image_property_changed (image);
348 PIXMAN_EXPORT pixman_bool_t
349 pixman_image_set_clip_region (pixman_image_t *image,
350 pixman_region16_t *region)
352 image_common_t *common = (image_common_t *)image;
353 pixman_bool_t result;
357 result = pixman_region32_copy_from_region16 (&common->clip_region, region);
361 _pixman_image_reset_clip_region (image);
366 image_property_changed (image);
371 /* Sets whether the clip region includes a clip region set by the client
374 pixman_image_set_has_client_clip (pixman_image_t *image,
375 pixman_bool_t client_clip)
377 image->common.has_client_clip = client_clip;
379 image_property_changed (image);
382 PIXMAN_EXPORT pixman_bool_t
383 pixman_image_set_transform (pixman_image_t *image,
384 const pixman_transform_t *transform)
386 static const pixman_transform_t id =
388 { { pixman_fixed_1, 0, 0 },
389 { 0, pixman_fixed_1, 0 },
390 { 0, 0, pixman_fixed_1 }
394 image_common_t *common = (image_common_t *)image;
395 pixman_bool_t result;
397 if (common->transform == transform)
400 if (memcmp (&id, transform, sizeof (pixman_transform_t)) == 0)
402 free(common->transform);
403 common->transform = NULL;
408 if (common->transform == NULL)
409 common->transform = malloc (sizeof (pixman_transform_t));
411 if (common->transform == NULL)
417 memcpy(common->transform, transform, sizeof(pixman_transform_t));
420 image_property_changed (image);
426 pixman_image_set_repeat (pixman_image_t *image,
427 pixman_repeat_t repeat)
429 image->common.repeat = repeat;
431 image_property_changed (image);
434 PIXMAN_EXPORT pixman_bool_t
435 pixman_image_set_filter (pixman_image_t *image,
436 pixman_filter_t filter,
437 const pixman_fixed_t *params,
440 image_common_t *common = (image_common_t *)image;
441 pixman_fixed_t *new_params;
443 if (params == common->filter_params && filter == common->filter)
449 new_params = pixman_malloc_ab (n_params, sizeof (pixman_fixed_t));
454 params, n_params * sizeof (pixman_fixed_t));
457 common->filter = filter;
459 if (common->filter_params)
460 free (common->filter_params);
462 common->filter_params = new_params;
463 common->n_filter_params = n_params;
465 image_property_changed (image);
470 pixman_image_set_source_clipping (pixman_image_t *image,
471 pixman_bool_t source_clipping)
473 image_common_t *common = &image->common;
476 common->src_clip = &common->clip_region;
478 common->src_clip = &common->full_region;
480 image_property_changed (image);
483 /* Unlike all the other property setters, this function does not
484 * copy the content of indexed. Doing this copying is simply
485 * way, way too expensive.
488 pixman_image_set_indexed (pixman_image_t *image,
489 const pixman_indexed_t *indexed)
491 bits_image_t *bits = (bits_image_t *)image;
493 bits->indexed = indexed;
495 image_property_changed (image);
499 pixman_image_set_alpha_map (pixman_image_t *image,
500 pixman_image_t *alpha_map,
504 image_common_t *common = (image_common_t *)image;
506 return_if_fail (!alpha_map || alpha_map->type == BITS);
508 if (common->alpha_map != (bits_image_t *)alpha_map)
510 if (common->alpha_map)
511 pixman_image_unref ((pixman_image_t *)common->alpha_map);
514 common->alpha_map = (bits_image_t *)pixman_image_ref (alpha_map);
516 common->alpha_map = NULL;
519 common->alpha_origin.x = x;
520 common->alpha_origin.y = y;
522 image_property_changed (image);
526 pixman_image_set_component_alpha (pixman_image_t *image,
527 pixman_bool_t component_alpha)
529 image->common.component_alpha = component_alpha;
531 image_property_changed (image);
536 pixman_image_set_accessors (pixman_image_t *image,
537 pixman_read_memory_func_t read_func,
538 pixman_write_memory_func_t write_func)
540 return_if_fail (image != NULL);
542 image->common.read_func = read_func;
543 image->common.write_func = write_func;
545 image_property_changed (image);
548 PIXMAN_EXPORT uint32_t *
549 pixman_image_get_data (pixman_image_t *image)
551 if (image->type == BITS)
552 return image->bits.bits;
558 pixman_image_get_width (pixman_image_t *image)
560 if (image->type == BITS)
561 return image->bits.width;
567 pixman_image_get_height (pixman_image_t *image)
569 if (image->type == BITS)
570 return image->bits.height;
576 pixman_image_get_stride (pixman_image_t *image)
578 if (image->type == BITS)
579 return image->bits.rowstride * (int) sizeof (uint32_t);
585 pixman_image_get_depth (pixman_image_t *image)
587 if (image->type == BITS)
588 return PIXMAN_FORMAT_DEPTH (image->bits.format);
594 color_to_uint32 (const pixman_color_t *color)
597 (color->alpha >> 8 << 24) |
598 (color->red >> 8 << 16) |
599 (color->green & 0xff00) |
604 color_to_pixel (pixman_color_t *color,
606 pixman_format_code_t format)
608 uint32_t c = color_to_uint32 (color);
610 if (!(format == PIXMAN_a8r8g8b8 ||
611 format == PIXMAN_x8r8g8b8 ||
612 format == PIXMAN_a8b8g8r8 ||
613 format == PIXMAN_x8b8g8r8 ||
614 format == PIXMAN_b8g8r8a8 ||
615 format == PIXMAN_b8g8r8x8 ||
616 format == PIXMAN_r5g6b5 ||
617 format == PIXMAN_b5g6r5 ||
618 format == PIXMAN_a8))
623 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_ABGR)
625 c = ((c & 0xff000000) >> 0) |
626 ((c & 0x00ff0000) >> 16) |
627 ((c & 0x0000ff00) >> 0) |
628 ((c & 0x000000ff) << 16);
630 if (PIXMAN_FORMAT_TYPE (format) == PIXMAN_TYPE_BGRA)
632 c = ((c & 0xff000000) >> 24) |
633 ((c & 0x00ff0000) >> 8) |
634 ((c & 0x0000ff00) << 8) |
635 ((c & 0x000000ff) << 24);
638 if (format == PIXMAN_a8)
640 else if (format == PIXMAN_r5g6b5 ||
641 format == PIXMAN_b5g6r5)
642 c = cvt8888to0565 (c);
645 printf ("color: %x %x %x %x\n", color->alpha, color->red, color->green, color->blue);
646 printf ("pixel: %x\n", c);
653 PIXMAN_EXPORT pixman_bool_t
654 pixman_image_fill_rectangles (pixman_op_t op,
655 pixman_image_t *dest,
656 pixman_color_t *color,
658 const pixman_rectangle16_t *rects)
660 pixman_image_t *solid;
664 if (color->alpha == 0xffff)
666 if (op == PIXMAN_OP_OVER)
670 if (op == PIXMAN_OP_CLEAR)
682 if (op == PIXMAN_OP_SRC)
686 if (color_to_pixel (color, &pixel, dest->bits.format))
688 for (i = 0; i < n_rects; ++i)
690 pixman_region32_t fill_region;
692 pixman_box32_t *boxes;
694 pixman_region32_init_rect (&fill_region, rects[i].x, rects[i].y, rects[i].width, rects[i].height);
695 if (!pixman_region32_intersect (&fill_region,
697 &dest->common.clip_region))
701 boxes = pixman_region32_rectangles (&fill_region, &n_boxes);
702 for (j = 0; j < n_boxes; ++j)
704 const pixman_box32_t *box = &(boxes[j]);
705 pixman_fill (dest->bits.bits, dest->bits.rowstride, PIXMAN_FORMAT_BPP (dest->bits.format),
706 box->x1, box->y1, box->x2 - box->x1, box->y2 - box->y1,
710 pixman_region32_fini (&fill_region);
716 solid = pixman_image_create_solid_fill (color);
720 for (i = 0; i < n_rects; ++i)
722 const pixman_rectangle16_t *rect = &(rects[i]);
724 pixman_image_composite (op, solid, NULL, dest,
727 rect->width, rect->height);
730 pixman_image_unref (solid);
736 pixman_image_can_get_solid (pixman_image_t *image)
738 if (image->type == SOLID)
741 if (image->type != BITS ||
742 image->bits.width != 1 ||
743 image->bits.height != 1)
748 if (image->common.repeat != PIXMAN_REPEAT_NORMAL)
751 switch (image->bits.format)
753 case PIXMAN_a8r8g8b8:
754 case PIXMAN_x8r8g8b8:
755 case PIXMAN_a8b8g8r8:
756 case PIXMAN_x8b8g8r8:
757 case PIXMAN_b8g8r8a8:
758 case PIXMAN_b8g8r8x8:
770 pixman_image_is_opaque(pixman_image_t *image)
773 int gradientNumberOfColors = 0;
775 if(image->common.alpha_map)
781 if(PIXMAN_FORMAT_A(image->bits.format))
788 gradientNumberOfColors = image->gradient.n_stops;
790 while(i<gradientNumberOfColors)
792 if(image->gradient.stops[i].color.alpha != 0xffff)
799 if(Alpha(image->solid.color) != 0xff)
804 /* Convolution filters can introduce translucency if the sum of the weights
806 if (image->common.filter == PIXMAN_FILTER_CONVOLUTION)
809 if (image->common.repeat == PIXMAN_REPEAT_NONE)
811 if (image->common.filter != PIXMAN_FILTER_NEAREST)
814 if (image->common.transform)
817 /* Gradients do not necessarily cover the entire compositing area */
818 if (image->type == LINEAR || image->type == CONICAL || image->type == RADIAL)