2 * Copyright © 2000 SuSE, Inc.
3 * Copyright © 1999 Keith Packard
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.
22 * Author: Keith Packard, SuSE, Inc.
31 #include "pixman-private.h"
34 * Computing composite region
36 #define BOUND(v) (int16_t) ((v) < INT16_MIN ? INT16_MIN : (v) > INT16_MAX ? INT16_MAX : (v))
38 static inline pixman_bool_t
39 miClipPictureReg (pixman_region32_t * pRegion,
40 pixman_region32_t * pClip,
44 if (pixman_region32_n_rects(pRegion) == 1 &&
45 pixman_region32_n_rects(pClip) == 1)
47 pixman_box32_t * pRbox = pixman_region32_rectangles(pRegion, NULL);
48 pixman_box32_t * pCbox = pixman_region32_rectangles(pClip, NULL);
51 if (pRbox->x1 < (v = pCbox->x1 + dx))
53 if (pRbox->x2 > (v = pCbox->x2 + dx))
55 if (pRbox->y1 < (v = pCbox->y1 + dy))
57 if (pRbox->y2 > (v = pCbox->y2 + dy))
59 if (pRbox->x1 >= pRbox->x2 ||
60 pRbox->y1 >= pRbox->y2)
62 pixman_region32_init (pRegion);
65 else if (!pixman_region32_not_empty (pClip))
72 pixman_region32_translate (pRegion, -dx, -dy);
73 if (!pixman_region32_intersect (pRegion, pRegion, pClip))
76 pixman_region32_translate(pRegion, dx, dy);
78 return pixman_region32_not_empty(pRegion);
82 static inline pixman_bool_t
83 miClipPictureSrc (pixman_region32_t * pRegion,
84 pixman_image_t * pPicture,
88 /* Source clips are ignored, unless they are explicitly turned on
89 * and the clip in question was set by an X client. (Because if
90 * the clip was not set by a client, then it is a hierarchy
91 * clip and those should always be ignored for sources).
93 if (!pPicture->common.clip_sources || !pPicture->common.client_clip)
96 return miClipPictureReg (pRegion,
97 &pPicture->common.clip_region,
102 * returns FALSE if the final region is empty. Indistinguishable from
103 * an allocation failure, but rendering ignores those anyways.
106 pixman_compute_composite_region32 (pixman_region32_t * pRegion,
107 pixman_image_t * pSrc,
108 pixman_image_t * pMask,
109 pixman_image_t * pDst,
121 pRegion->extents.x1 = xDst;
123 pRegion->extents.x2 = BOUND(v);
124 pRegion->extents.y1 = yDst;
126 pRegion->extents.y2 = BOUND(v);
128 pRegion->extents.x1 = MAX (pRegion->extents.x1, 0);
129 pRegion->extents.y1 = MAX (pRegion->extents.y1, 0);
131 /* Some X servers rely on an old bug, where pixman would just believe the
132 * set clip_region and not clip against the destination geometry. So,
133 * since only X servers set "source clip", we don't clip against
134 * destination geometry when that is set.
136 if (!pDst->common.clip_sources)
138 pRegion->extents.x2 = MIN (pRegion->extents.x2, pDst->bits.width);
139 pRegion->extents.y2 = MIN (pRegion->extents.y2, pDst->bits.height);
144 /* Check for empty operation */
145 if (pRegion->extents.x1 >= pRegion->extents.x2 ||
146 pRegion->extents.y1 >= pRegion->extents.y2)
148 pixman_region32_init (pRegion);
152 if (pDst->common.have_clip_region)
154 if (!miClipPictureReg (pRegion, &pDst->common.clip_region, 0, 0))
156 pixman_region32_fini (pRegion);
161 if (pDst->common.alpha_map && pDst->common.alpha_map->common.have_clip_region)
163 if (!miClipPictureReg (pRegion, &pDst->common.alpha_map->common.clip_region,
164 -pDst->common.alpha_origin_x,
165 -pDst->common.alpha_origin_y))
167 pixman_region32_fini (pRegion);
172 /* clip against src */
173 if (pSrc->common.have_clip_region)
175 if (!miClipPictureSrc (pRegion, pSrc, xDst - xSrc, yDst - ySrc))
177 pixman_region32_fini (pRegion);
181 if (pSrc->common.alpha_map && pSrc->common.alpha_map->common.have_clip_region)
183 if (!miClipPictureSrc (pRegion, (pixman_image_t *)pSrc->common.alpha_map,
184 xDst - (xSrc - pSrc->common.alpha_origin_x),
185 yDst - (ySrc - pSrc->common.alpha_origin_y)))
187 pixman_region32_fini (pRegion);
191 /* clip against mask */
192 if (pMask && pMask->common.have_clip_region)
194 if (!miClipPictureSrc (pRegion, pMask, xDst - xMask, yDst - yMask))
196 pixman_region32_fini (pRegion);
199 if (pMask->common.alpha_map && pMask->common.alpha_map->common.have_clip_region)
201 if (!miClipPictureSrc (pRegion, (pixman_image_t *)pMask->common.alpha_map,
202 xDst - (xMask - pMask->common.alpha_origin_x),
203 yDst - (yMask - pMask->common.alpha_origin_y)))
205 pixman_region32_fini (pRegion);
214 PIXMAN_EXPORT pixman_bool_t
215 pixman_compute_composite_region (pixman_region16_t * pRegion,
216 pixman_image_t * pSrc,
217 pixman_image_t * pMask,
218 pixman_image_t * pDst,
228 pixman_region32_t r32;
229 pixman_bool_t retval;
231 pixman_region32_init (&r32);
233 retval = pixman_compute_composite_region32 (&r32, pSrc, pMask, pDst,
234 xSrc, ySrc, xMask, yMask, xDst, yDst,
239 if (!pixman_region16_copy_from_region32 (pRegion, &r32))
243 pixman_region32_fini (&r32);
248 pixman_multiply_overflows_int (unsigned int a,
251 return a >= INT32_MAX / b;
255 pixman_addition_overflows_int (unsigned int a,
258 return a > INT32_MAX - b;
262 pixman_malloc_ab(unsigned int a,
265 if (a >= INT32_MAX / b)
268 return malloc (a * b);
272 pixman_malloc_abc (unsigned int a,
276 if (a >= INT32_MAX / b)
278 else if (a * b >= INT32_MAX / c)
281 return malloc (a * b * c);
285 * Helper routine to expand a color component from 0 < n <= 8 bits to 16 bits by
288 static inline uint64_t
289 expand16(const uint8_t val, int nbits)
291 // Start out with the high bit of val in the high bit of result.
292 uint16_t result = (uint16_t)val << (16 - nbits);
297 // Copy the bits in result, doubling the number of bits each time, until we
300 result |= result >> nbits;
308 * This function expands images from ARGB8 format to ARGB16. To preserve
309 * precision, it needs to know the original source format. For example, if the
310 * source was PIXMAN_x1r5g5b5 and the red component contained bits 12345, then
311 * the expanded value is 12345123. To correctly expand this to 16 bits, it
312 * should be 1234512345123451 and not 1234512312345123.
315 pixman_expand(uint64_t *dst, const uint32_t *src,
316 pixman_format_code_t format, int width)
319 * Determine the sizes of each component and the masks and shifts required
320 * to extract them from the source pixel.
322 const int a_size = PIXMAN_FORMAT_A(format),
323 r_size = PIXMAN_FORMAT_R(format),
324 g_size = PIXMAN_FORMAT_G(format),
325 b_size = PIXMAN_FORMAT_B(format);
326 const int a_shift = 32 - a_size,
327 r_shift = 24 - r_size,
328 g_shift = 16 - g_size,
329 b_shift = 8 - b_size;
330 const uint8_t a_mask = ~(~0 << a_size),
331 r_mask = ~(~0 << r_size),
332 g_mask = ~(~0 << g_size),
333 b_mask = ~(~0 << b_size);
336 /* Start at the end so that we can do the expansion in place when src == dst */
337 for (i = width - 1; i >= 0; i--)
339 const uint32_t pixel = src[i];
340 // Extract the components.
341 const uint8_t a = (pixel >> a_shift) & a_mask,
342 r = (pixel >> r_shift) & r_mask,
343 g = (pixel >> g_shift) & g_mask,
344 b = (pixel >> b_shift) & b_mask;
345 const uint64_t a16 = a_size ? expand16(a, a_size) : 0xffff,
346 r16 = expand16(r, r_size),
347 g16 = expand16(g, g_size),
348 b16 = expand16(b, b_size);
350 dst[i] = a16 << 48 | r16 << 32 | g16 << 16 | b16;
355 * Contracting is easier than expanding. We just need to truncate the
359 pixman_contract(uint32_t *dst, const uint64_t *src, int width)
363 /* Start at the beginning so that we can do the contraction in place when
365 for (i = 0; i < width; i++)
367 const uint8_t a = src[i] >> 56,
371 dst[i] = a << 24 | r << 16 | g << 8 | b;
376 walk_region_internal (pixman_implementation_t *imp,
378 pixman_image_t * pSrc,
379 pixman_image_t * pMask,
380 pixman_image_t * pDst,
389 pixman_bool_t srcRepeat,
390 pixman_bool_t maskRepeat,
391 pixman_region32_t *region,
392 pixman_composite_func_t compositeRect)
395 const pixman_box32_t *pbox;
396 int w, h, w_this, h_this;
397 int x_msk, y_msk, x_src, y_src, x_dst, y_dst;
399 pbox = pixman_region32_rectangles (region, &n);
402 h = pbox->y2 - pbox->y1;
403 y_src = pbox->y1 - yDst + ySrc;
404 y_msk = pbox->y1 - yDst + yMask;
409 w = pbox->x2 - pbox->x1;
410 x_src = pbox->x1 - xDst + xSrc;
411 x_msk = pbox->x1 - xDst + xMask;
416 y_msk = MOD (y_msk, pMask->bits.height);
417 if (h_this > pMask->bits.height - y_msk)
418 h_this = pMask->bits.height - y_msk;
422 y_src = MOD (y_src, pSrc->bits.height);
423 if (h_this > pSrc->bits.height - y_src)
424 h_this = pSrc->bits.height - y_src;
431 x_msk = MOD (x_msk, pMask->bits.width);
432 if (w_this > pMask->bits.width - x_msk)
433 w_this = pMask->bits.width - x_msk;
437 x_src = MOD (x_src, pSrc->bits.width);
438 if (w_this > pSrc->bits.width - x_src)
439 w_this = pSrc->bits.width - x_src;
441 (*compositeRect) (imp,
442 op, pSrc, pMask, pDst,
443 x_src, y_src, x_msk, y_msk, x_dst, y_dst,
460 _pixman_walk_composite_region (pixman_implementation_t *imp,
462 pixman_image_t * pSrc,
463 pixman_image_t * pMask,
464 pixman_image_t * pDst,
473 pixman_composite_func_t compositeRect)
475 pixman_region32_t region;
477 pixman_region32_init (®ion);
479 if (pixman_compute_composite_region32 (
480 ®ion, pSrc, pMask, pDst, xSrc, ySrc, xMask, yMask, xDst, yDst, width, height))
482 walk_region_internal (imp, op,
484 xSrc, ySrc, xMask, yMask, xDst, yDst,
485 width, height, FALSE, FALSE,
490 pixman_region32_fini (®ion);
495 mask_is_solid (pixman_image_t *mask)
497 if (mask->type == SOLID)
500 if (mask->type == BITS &&
501 mask->common.repeat == PIXMAN_REPEAT_NORMAL &&
502 mask->bits.width == 1 &&
503 mask->bits.height == 1)
511 static const pixman_fast_path_t *
512 get_fast_path (const pixman_fast_path_t *fast_paths,
514 pixman_image_t *pSrc,
515 pixman_image_t *pMask,
516 pixman_image_t *pDst,
517 pixman_bool_t is_pixbuf)
519 const pixman_fast_path_t *info;
521 for (info = fast_paths; info->op != PIXMAN_OP_NONE; info++)
523 pixman_bool_t valid_src = FALSE;
524 pixman_bool_t valid_mask = FALSE;
529 if ((info->src_format == PIXMAN_solid && _pixman_image_is_solid (pSrc)) ||
530 (pSrc->type == BITS && info->src_format == pSrc->bits.format))
538 if ((info->mask_format == PIXMAN_null && !pMask) ||
539 (pMask && pMask->type == BITS && info->mask_format == pMask->bits.format))
543 if (info->flags & NEED_SOLID_MASK)
545 if (!pMask || !mask_is_solid (pMask))
549 if (info->flags & NEED_COMPONENT_ALPHA)
551 if (!pMask || !pMask->common.component_alpha)
559 if (info->dest_format != pDst->bits.format)
562 if ((info->flags & NEED_PIXBUF) && !is_pixbuf)
571 static inline pixman_bool_t
572 image_covers (pixman_image_t *image, pixman_box32_t *extents, int x, int y)
574 if (image->common.type == BITS && image->common.repeat == PIXMAN_REPEAT_NONE)
576 if (x > extents->x1 || y > extents->y1 ||
577 x + image->bits.width < extents->x2 ||
578 y + image->bits.height < extents->y2)
588 _pixman_run_fast_path (const pixman_fast_path_t *paths,
589 pixman_implementation_t *imp,
592 pixman_image_t *mask,
593 pixman_image_t *dest,
603 pixman_composite_func_t func = NULL;
604 pixman_bool_t src_repeat = src->common.repeat == PIXMAN_REPEAT_NORMAL;
605 pixman_bool_t mask_repeat = mask && mask->common.repeat == PIXMAN_REPEAT_NORMAL;
606 pixman_bool_t result;
608 if ((src->type == BITS || _pixman_image_is_solid (src)) &&
609 (!mask || mask->type == BITS)
610 && !src->common.transform && !(mask && mask->common.transform)
611 && !(mask && mask->common.alpha_map) && !src->common.alpha_map && !dest->common.alpha_map
612 && (src->common.filter != PIXMAN_FILTER_CONVOLUTION)
613 && (src->common.repeat != PIXMAN_REPEAT_PAD)
614 && (src->common.repeat != PIXMAN_REPEAT_REFLECT)
615 && (!mask || (mask->common.filter != PIXMAN_FILTER_CONVOLUTION &&
616 mask->common.repeat != PIXMAN_REPEAT_PAD &&
617 mask->common.repeat != PIXMAN_REPEAT_REFLECT))
618 && !src->common.read_func && !src->common.write_func
619 && !(mask && mask->common.read_func)
620 && !(mask && mask->common.write_func)
621 && !dest->common.read_func
622 && !dest->common.write_func)
624 const pixman_fast_path_t *info;
625 pixman_bool_t pixbuf;
628 src && src->type == BITS &&
629 mask && mask->type == BITS &&
630 src->bits.bits == mask->bits.bits &&
633 !mask->common.component_alpha &&
636 info = get_fast_path (paths, op, src, mask, dest, pixbuf);
642 if (info->src_format == PIXMAN_solid)
645 if (info->mask_format == PIXMAN_solid || info->flags & NEED_SOLID_MASK)
649 src->bits.width == 1 &&
650 src->bits.height == 1) ||
652 mask->bits.width == 1 &&
653 mask->bits.height == 1))
655 /* If src or mask are repeating 1x1 images and src_repeat or
656 * mask_repeat are still TRUE, it means the fast path we
657 * selected does not actually handle repeating images.
659 * So rather than call the "fast path" with a zillion
660 * 1x1 requests, we just fall back to the general code (which
661 * does do something sensible with 1x1 repeating images).
672 pixman_region32_t region;
673 pixman_region32_init (®ion);
675 if (pixman_compute_composite_region32 (
676 ®ion, src, mask, dest, src_x, src_y, mask_x, mask_y, dest_x, dest_y, width, height))
678 pixman_box32_t *extents = pixman_region32_extents (®ion);
680 if (image_covers (src, extents, dest_x - src_x, dest_y - src_y) &&
681 (!mask || image_covers (mask, extents, dest_x - mask_x, dest_y - mask_y)))
683 walk_region_internal (imp, op,
685 src_x, src_y, mask_x, mask_y,
688 src_repeat, mask_repeat,
696 pixman_region32_fini (®ion);
702 #define N_TMP_BOXES (16)
705 pixman_region16_copy_from_region32 (pixman_region16_t *dst,
706 pixman_region32_t *src)
709 pixman_box32_t *boxes32;
710 pixman_box16_t *boxes16;
711 pixman_bool_t retval;
713 boxes32 = pixman_region32_rectangles (src, &n_boxes);
715 boxes16 = pixman_malloc_ab (n_boxes, sizeof (pixman_box16_t));
720 for (i = 0; i < n_boxes; ++i)
722 boxes16[i].x1 = boxes32[i].x1;
723 boxes16[i].y1 = boxes32[i].y1;
724 boxes16[i].x2 = boxes32[i].x2;
725 boxes16[i].y2 = boxes32[i].y2;
728 pixman_region_fini (dst);
729 retval = pixman_region_init_rects (dst, boxes16, n_boxes);
735 pixman_region32_copy_from_region16 (pixman_region32_t *dst,
736 pixman_region16_t *src)
739 pixman_box16_t *boxes16;
740 pixman_box32_t *boxes32;
741 pixman_box32_t tmp_boxes[N_TMP_BOXES];
742 pixman_bool_t retval;
744 boxes16 = pixman_region_rectangles (src, &n_boxes);
746 if (n_boxes > N_TMP_BOXES)
747 boxes32 = pixman_malloc_ab (n_boxes, sizeof (pixman_box32_t));
754 for (i = 0; i < n_boxes; ++i)
756 boxes32[i].x1 = boxes16[i].x1;
757 boxes32[i].y1 = boxes16[i].y1;
758 boxes32[i].x2 = boxes16[i].x2;
759 boxes32[i].y2 = boxes16[i].y2;
762 pixman_region32_fini (dst);
763 retval = pixman_region32_init_rects (dst, boxes32, n_boxes);
765 if (boxes32 != tmp_boxes)