1 /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2 /* cairo - a vector graphics library with display and print output
4 * Copyright © 2002 University of Southern California
5 * Copyright © 2005 Red Hat, Inc.
6 * Copyright © 2011 Intel Corporation
8 * This library is free software; you can redistribute it and/or
9 * modify it either under the terms of the GNU Lesser General Public
10 * License version 2.1 as published by the Free Software Foundation
11 * (the "LGPL") or, at your option, under the terms of the Mozilla
12 * Public License Version 1.1 (the "MPL"). If you do not alter this
13 * notice, a recipient may use your version of this file under either
14 * the MPL or the LGPL.
16 * You should have received a copy of the LGPL along with this library
17 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
19 * You should have received a copy of the MPL along with this library
20 * in the file COPYING-MPL-1.1
22 * The contents of this file are subject to the Mozilla Public License
23 * Version 1.1 (the "License"); you may not use this file except in
24 * compliance with the License. You may obtain a copy of the License at
25 * http://www.mozilla.org/MPL/
27 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
28 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
29 * the specific language governing rights and limitations.
31 * The Original Code is the cairo graphics library.
33 * The Initial Developer of the Original Code is University of Southern
37 * Carl D. Worth <cworth@cworth.org>
38 * Joonas Pihlaja <jpihlaja@cc.helsinki.fi>
39 * Chris Wilson <chris@chris-wilson.co.uk>
42 /* This compositor renders the shape to a mask using an image surface
43 * then calls composite.
48 #include "cairo-clip-inline.h"
49 #include "cairo-compositor-private.h"
50 #include "cairo-image-surface-private.h"
51 #include "cairo-pattern-inline.h"
52 #include "cairo-region-private.h"
53 #include "cairo-surface-observer-private.h"
54 #include "cairo-surface-offset-private.h"
55 #include "cairo-surface-snapshot-private.h"
56 #include "cairo-surface-subsurface-private.h"
58 typedef cairo_int_status_t
59 (*draw_func_t) (const cairo_mask_compositor_t *compositor,
63 const cairo_pattern_t *src,
64 const cairo_rectangle_int_t *src_sample,
67 const cairo_rectangle_int_t *extents,
70 static void do_unaligned_row(void (*blt)(void *closure,
79 int x1 = _cairo_fixed_integer_part (b->p1.x) - tx;
80 int x2 = _cairo_fixed_integer_part (b->p2.x) - tx;
82 if (! _cairo_fixed_is_integer (b->p1.x)) {
83 blt(closure, x1, y, 1, h,
84 coverage * (256 - _cairo_fixed_fractional_part (b->p1.x)));
89 blt(closure, x1, y, x2-x1, h, (coverage << 8) - (coverage >> 8));
91 if (! _cairo_fixed_is_integer (b->p2.x))
92 blt(closure, x2, y, 1, h,
93 coverage * _cairo_fixed_fractional_part (b->p2.x));
95 blt(closure, x1, y, 1, h,
96 coverage * (b->p2.x - b->p1.x));
99 static void do_unaligned_box(void (*blt)(void *closure,
100 int16_t x, int16_t y,
101 int16_t w, int16_t h,
104 const cairo_box_t *b, int tx, int ty)
106 int y1 = _cairo_fixed_integer_part (b->p1.y) - ty;
107 int y2 = _cairo_fixed_integer_part (b->p2.y) - ty;
109 if (! _cairo_fixed_is_integer (b->p1.y)) {
110 do_unaligned_row(blt, closure, b, tx, y1, 1,
111 256 - _cairo_fixed_fractional_part (b->p1.y));
116 do_unaligned_row(blt, closure, b, tx, y1, y2-y1, 256);
118 if (! _cairo_fixed_is_integer (b->p2.y))
119 do_unaligned_row(blt, closure, b, tx, y2, 1,
120 _cairo_fixed_fractional_part (b->p2.y));
122 do_unaligned_row(blt, closure, b, tx, y1, 1,
127 const cairo_mask_compositor_t *compositor;
128 cairo_surface_t *dst;
131 static void blt_in(void *closure,
132 int16_t x, int16_t y,
133 int16_t w, int16_t h,
136 struct blt_in *info = closure;
138 cairo_rectangle_int_t rect;
140 if (coverage == 0xffff)
148 _cairo_color_init_rgba (&color, 0, 0, 0, coverage / (double) 0xffff);
149 info->compositor->fill_rectangles (info->dst, CAIRO_OPERATOR_IN,
153 static cairo_surface_t *
154 create_composite_mask (const cairo_mask_compositor_t *compositor,
155 cairo_surface_t *dst,
157 draw_func_t draw_func,
158 draw_func_t mask_func,
159 const cairo_composite_rectangles_t *extents)
161 cairo_surface_t *surface;
162 cairo_int_status_t status;
166 surface = _cairo_surface_create_similar_scratch (dst, CAIRO_CONTENT_ALPHA,
167 extents->bounded.width,
168 extents->bounded.height);
169 if (unlikely (surface->status))
172 status = compositor->acquire (surface);
173 if (unlikely (status)) {
174 cairo_surface_destroy (surface);
175 return _cairo_int_surface_create_in_error (status);
178 if (!surface->is_clear) {
179 cairo_rectangle_int_t rect;
182 rect.width = extents->bounded.width;
183 rect.height = extents->bounded.height;
185 status = compositor->fill_rectangles (surface, CAIRO_OPERATOR_CLEAR,
186 CAIRO_COLOR_TRANSPARENT,
188 if (unlikely (status))
193 status = mask_func (compositor, surface, draw_closure,
194 CAIRO_OPERATOR_SOURCE, NULL, NULL,
195 extents->bounded.x, extents->bounded.y,
196 &extents->bounded, extents->clip);
197 if (likely (status != CAIRO_INT_STATUS_UNSUPPORTED))
201 /* Is it worth setting the clip region here? */
202 status = draw_func (compositor, surface, draw_closure,
203 CAIRO_OPERATOR_ADD, NULL, NULL,
204 extents->bounded.x, extents->bounded.y,
205 &extents->bounded, NULL);
206 if (unlikely (status))
209 info.compositor = compositor;
211 for (i = 0; i < extents->clip->num_boxes; i++) {
212 cairo_box_t *b = &extents->clip->boxes[i];
214 if (! _cairo_fixed_is_integer (b->p1.x) ||
215 ! _cairo_fixed_is_integer (b->p1.y) ||
216 ! _cairo_fixed_is_integer (b->p2.x) ||
217 ! _cairo_fixed_is_integer (b->p2.y))
219 do_unaligned_box(blt_in, &info, b,
225 if (extents->clip->path != NULL) {
226 status = _cairo_clip_combine_with_surface (extents->clip, surface,
229 if (unlikely (status))
234 compositor->release (surface);
235 surface->is_clear = FALSE;
239 compositor->release (surface);
240 if (status != CAIRO_INT_STATUS_NOTHING_TO_DO) {
241 cairo_surface_destroy (surface);
242 surface = _cairo_int_surface_create_in_error (status);
247 /* Handles compositing with a clip surface when the operator allows
248 * us to combine the clip with the mask
250 static cairo_status_t
251 clip_and_composite_with_mask (const cairo_mask_compositor_t *compositor,
253 draw_func_t draw_func,
254 draw_func_t mask_func,
256 cairo_pattern_t *pattern,
257 const cairo_composite_rectangles_t*extents)
259 cairo_surface_t *dst = extents->surface;
260 cairo_surface_t *mask, *src;
263 mask = create_composite_mask (compositor, dst, draw_closure,
264 draw_func, mask_func,
266 if (unlikely (mask->status))
269 if (pattern != NULL || dst->content != CAIRO_CONTENT_ALPHA) {
270 src = compositor->pattern_to_surface (dst,
271 &extents->source_pattern.base,
274 &extents->source_sample_area,
276 if (unlikely (src->status)) {
277 cairo_surface_destroy (mask);
281 compositor->composite (dst, op, src, mask,
282 extents->bounded.x + src_x,
283 extents->bounded.y + src_y,
285 extents->bounded.x, extents->bounded.y,
286 extents->bounded.width, extents->bounded.height);
288 cairo_surface_destroy (src);
290 compositor->composite (dst, op, mask, NULL,
293 extents->bounded.x, extents->bounded.y,
294 extents->bounded.width, extents->bounded.height);
296 cairo_surface_destroy (mask);
298 return CAIRO_STATUS_SUCCESS;
301 static cairo_surface_t *
302 get_clip_source (const cairo_mask_compositor_t *compositor,
304 cairo_surface_t *dst,
305 const cairo_rectangle_int_t *bounds,
306 int *out_x, int *out_y)
308 cairo_surface_pattern_t pattern;
309 cairo_rectangle_int_t r;
310 cairo_surface_t *surface;
312 surface = _cairo_clip_get_image (clip, dst, bounds);
313 if (unlikely (surface->status))
316 _cairo_pattern_init_for_surface (&pattern, surface);
317 pattern.base.filter = CAIRO_FILTER_NEAREST;
318 cairo_surface_destroy (surface);
321 r.width = bounds->width;
322 r.height = bounds->height;
324 surface = compositor->pattern_to_surface (dst, &pattern.base, TRUE,
325 &r, &r, out_x, out_y);
326 _cairo_pattern_fini (&pattern.base);
328 *out_x += -bounds->x;
329 *out_y += -bounds->y;
333 /* Handles compositing with a clip surface when we have to do the operation
334 * in two pieces and combine them together.
336 static cairo_status_t
337 clip_and_composite_combine (const cairo_mask_compositor_t *compositor,
339 draw_func_t draw_func,
341 const cairo_pattern_t *pattern,
342 const cairo_composite_rectangles_t*extents)
344 cairo_surface_t *dst = extents->surface;
345 cairo_surface_t *tmp, *clip;
346 cairo_status_t status;
349 tmp = _cairo_surface_create_similar_scratch (dst, dst->content,
350 extents->bounded.width,
351 extents->bounded.height);
352 if (unlikely (tmp->status)) {
353 status = tmp->status;
357 compositor->composite (tmp, CAIRO_OPERATOR_SOURCE, dst, NULL,
358 extents->bounded.x, extents->bounded.y,
361 extents->bounded.width, extents->bounded.height);
363 status = draw_func (compositor, tmp, draw_closure, op,
364 pattern, &extents->source_sample_area,
365 extents->bounded.x, extents->bounded.y,
366 &extents->bounded, NULL);
367 if (unlikely (status))
370 clip = get_clip_source (compositor,
371 extents->clip, dst, &extents->bounded,
373 if (unlikely ((status = clip->status))) {
374 cairo_surface_destroy (clip);
379 compositor->composite (dst, CAIRO_OPERATOR_SOURCE, tmp, clip,
382 extents->bounded.x, extents->bounded.y,
383 extents->bounded.width, extents->bounded.height);
385 /* Punch the clip out of the destination */
386 compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, clip, NULL,
389 extents->bounded.x, extents->bounded.y,
390 extents->bounded.width, extents->bounded.height);
392 /* Now add the two results together */
393 compositor->composite (dst, CAIRO_OPERATOR_ADD, tmp, clip,
396 extents->bounded.x, extents->bounded.y,
397 extents->bounded.width, extents->bounded.height);
399 cairo_surface_destroy (clip);
402 cairo_surface_destroy (tmp);
406 /* Handles compositing for %CAIRO_OPERATOR_SOURCE, which is special; it's
407 * defined as (src IN mask IN clip) ADD (dst OUT (mask IN clip))
409 static cairo_status_t
410 clip_and_composite_source (const cairo_mask_compositor_t *compositor,
412 draw_func_t draw_func,
413 draw_func_t mask_func,
414 cairo_pattern_t *pattern,
415 const cairo_composite_rectangles_t *extents)
417 cairo_surface_t *dst = extents->surface;
418 cairo_surface_t *mask, *src;
421 /* Create a surface that is mask IN clip */
422 mask = create_composite_mask (compositor, dst, draw_closure,
423 draw_func, mask_func,
425 if (unlikely (mask->status))
428 src = compositor->pattern_to_surface (dst,
432 &extents->source_sample_area,
434 if (unlikely (src->status)) {
435 cairo_surface_destroy (mask);
440 compositor->composite (dst, CAIRO_OPERATOR_SOURCE, src, mask,
441 extents->bounded.x + src_x, extents->bounded.y + src_y,
443 extents->bounded.x, extents->bounded.y,
444 extents->bounded.width, extents->bounded.height);
446 /* Compute dest' = dest OUT (mask IN clip) */
447 compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
449 extents->bounded.x, extents->bounded.y,
450 extents->bounded.width, extents->bounded.height);
452 /* Now compute (src IN (mask IN clip)) ADD dest' */
453 compositor->composite (dst, CAIRO_OPERATOR_ADD, src, mask,
454 extents->bounded.x + src_x, extents->bounded.y + src_y,
456 extents->bounded.x, extents->bounded.y,
457 extents->bounded.width, extents->bounded.height);
460 cairo_surface_destroy (src);
461 cairo_surface_destroy (mask);
463 return CAIRO_STATUS_SUCCESS;
467 can_reduce_alpha_op (cairo_operator_t op)
471 case CAIRO_OPERATOR_OVER:
472 case CAIRO_OPERATOR_SOURCE:
473 case CAIRO_OPERATOR_ADD:
481 reduce_alpha_op (cairo_surface_t *dst,
483 const cairo_pattern_t *pattern)
485 return dst->is_clear &&
486 dst->content == CAIRO_CONTENT_ALPHA &&
487 _cairo_pattern_is_opaque_solid (pattern) &&
488 can_reduce_alpha_op (op);
491 static cairo_status_t
492 fixup_unbounded (const cairo_mask_compositor_t *compositor,
493 cairo_surface_t *dst,
494 const cairo_composite_rectangles_t *extents)
496 cairo_rectangle_int_t rects[4];
499 if (extents->bounded.width == extents->unbounded.width &&
500 extents->bounded.height == extents->unbounded.height)
502 return CAIRO_STATUS_SUCCESS;
506 if (extents->bounded.width == 0 || extents->bounded.height == 0) {
507 rects[n].x = extents->unbounded.x;
508 rects[n].width = extents->unbounded.width;
509 rects[n].y = extents->unbounded.y;
510 rects[n].height = extents->unbounded.height;
514 if (extents->bounded.y != extents->unbounded.y) {
515 rects[n].x = extents->unbounded.x;
516 rects[n].width = extents->unbounded.width;
517 rects[n].y = extents->unbounded.y;
518 rects[n].height = extents->bounded.y - extents->unbounded.y;
522 if (extents->bounded.x != extents->unbounded.x) {
523 rects[n].x = extents->unbounded.x;
524 rects[n].width = extents->bounded.x - extents->unbounded.x;
525 rects[n].y = extents->bounded.y;
526 rects[n].height = extents->bounded.height;
530 if (extents->bounded.x + extents->bounded.width != extents->unbounded.x + extents->unbounded.width) {
531 rects[n].x = extents->bounded.x + extents->bounded.width;
532 rects[n].width = extents->unbounded.x + extents->unbounded.width - rects[n].x;
533 rects[n].y = extents->bounded.y;
534 rects[n].height = extents->bounded.height;
538 if (extents->bounded.y + extents->bounded.height != extents->unbounded.y + extents->unbounded.height) {
539 rects[n].x = extents->unbounded.x;
540 rects[n].width = extents->unbounded.width;
541 rects[n].y = extents->bounded.y + extents->bounded.height;
542 rects[n].height = extents->unbounded.y + extents->unbounded.height - rects[n].y;
547 return compositor->fill_rectangles (dst, CAIRO_OPERATOR_CLEAR,
548 CAIRO_COLOR_TRANSPARENT,
552 static cairo_status_t
553 fixup_unbounded_with_mask (const cairo_mask_compositor_t *compositor,
554 cairo_surface_t *dst,
555 const cairo_composite_rectangles_t *extents)
557 cairo_surface_t *mask;
560 mask = get_clip_source (compositor,
561 extents->clip, dst, &extents->unbounded,
563 if (unlikely (mask->status)) {
564 cairo_status_t status = mask->status;
565 cairo_surface_destroy (mask);
570 if (extents->bounded.y != extents->unbounded.y) {
571 int x = extents->unbounded.x;
572 int y = extents->unbounded.y;
573 int width = extents->unbounded.width;
574 int height = extents->bounded.y - y;
576 compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
577 x + mask_x, y + mask_y,
584 if (extents->bounded.x != extents->unbounded.x) {
585 int x = extents->unbounded.x;
586 int y = extents->bounded.y;
587 int width = extents->bounded.x - x;
588 int height = extents->bounded.height;
590 compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
591 x + mask_x, y + mask_y,
598 if (extents->bounded.x + extents->bounded.width != extents->unbounded.x + extents->unbounded.width) {
599 int x = extents->bounded.x + extents->bounded.width;
600 int y = extents->bounded.y;
601 int width = extents->unbounded.x + extents->unbounded.width - x;
602 int height = extents->bounded.height;
604 compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
605 x + mask_x, y + mask_y,
612 if (extents->bounded.y + extents->bounded.height != extents->unbounded.y + extents->unbounded.height) {
613 int x = extents->unbounded.x;
614 int y = extents->bounded.y + extents->bounded.height;
615 int width = extents->unbounded.width;
616 int height = extents->unbounded.y + extents->unbounded.height - y;
618 compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
619 x + mask_x, y + mask_y,
625 cairo_surface_destroy (mask);
627 return CAIRO_STATUS_SUCCESS;
630 static cairo_status_t
631 fixup_unbounded_boxes (const cairo_mask_compositor_t *compositor,
632 const cairo_composite_rectangles_t *extents,
633 cairo_boxes_t *boxes)
635 cairo_surface_t *dst = extents->surface;
637 cairo_region_t *clip_region;
639 cairo_status_t status;
640 struct _cairo_boxes_chunk *chunk;
643 assert (boxes->is_pixel_aligned);
646 if (_cairo_clip_is_region (extents->clip) &&
647 (clip_region = _cairo_clip_get_region (extents->clip)) &&
648 cairo_region_contains_rectangle (clip_region,
649 &extents->bounded) == CAIRO_REGION_OVERLAP_IN)
653 if (boxes->num_boxes <= 1 && clip_region == NULL)
654 return fixup_unbounded (compositor, dst, extents);
656 _cairo_boxes_init (&clear);
658 box.p1.x = _cairo_fixed_from_int (extents->unbounded.x + extents->unbounded.width);
659 box.p1.y = _cairo_fixed_from_int (extents->unbounded.y);
660 box.p2.x = _cairo_fixed_from_int (extents->unbounded.x);
661 box.p2.y = _cairo_fixed_from_int (extents->unbounded.y + extents->unbounded.height);
663 if (clip_region == NULL) {
666 _cairo_boxes_init (&tmp);
668 status = _cairo_boxes_add (&tmp, CAIRO_ANTIALIAS_DEFAULT, &box);
669 assert (status == CAIRO_STATUS_SUCCESS);
671 tmp.chunks.next = &boxes->chunks;
672 tmp.num_boxes += boxes->num_boxes;
674 status = _cairo_bentley_ottmann_tessellate_boxes (&tmp,
675 CAIRO_FILL_RULE_WINDING,
678 tmp.chunks.next = NULL;
680 pixman_box32_t *pbox;
682 pbox = pixman_region32_rectangles (&clip_region->rgn, &i);
683 _cairo_boxes_limit (&clear, (cairo_box_t *) pbox, i);
685 status = _cairo_boxes_add (&clear, CAIRO_ANTIALIAS_DEFAULT, &box);
686 assert (status == CAIRO_STATUS_SUCCESS);
688 for (chunk = &boxes->chunks; chunk != NULL; chunk = chunk->next) {
689 for (i = 0; i < chunk->count; i++) {
690 status = _cairo_boxes_add (&clear,
691 CAIRO_ANTIALIAS_DEFAULT,
693 if (unlikely (status)) {
694 _cairo_boxes_fini (&clear);
700 status = _cairo_bentley_ottmann_tessellate_boxes (&clear,
701 CAIRO_FILL_RULE_WINDING,
705 if (likely (status == CAIRO_STATUS_SUCCESS)) {
706 status = compositor->fill_boxes (dst,
707 CAIRO_OPERATOR_CLEAR,
708 CAIRO_COLOR_TRANSPARENT,
712 _cairo_boxes_fini (&clear);
718 NEED_CLIP_REGION = 0x1,
719 NEED_CLIP_SURFACE = 0x2,
720 FORCE_CLIP_REGION = 0x4,
724 need_bounded_clip (cairo_composite_rectangles_t *extents)
726 unsigned int flags = NEED_CLIP_REGION;
727 if (! _cairo_clip_is_region (extents->clip))
728 flags |= NEED_CLIP_SURFACE;
733 need_unbounded_clip (cairo_composite_rectangles_t *extents)
735 unsigned int flags = 0;
736 if (! extents->is_bounded) {
737 flags |= NEED_CLIP_REGION;
738 if (! _cairo_clip_is_region (extents->clip))
739 flags |= NEED_CLIP_SURFACE;
741 if (extents->clip->path != NULL)
742 flags |= NEED_CLIP_SURFACE;
746 static cairo_status_t
747 clip_and_composite (const cairo_mask_compositor_t *compositor,
748 draw_func_t draw_func,
749 draw_func_t mask_func,
751 cairo_composite_rectangles_t*extents,
752 unsigned int need_clip)
754 cairo_surface_t *dst = extents->surface;
755 cairo_operator_t op = extents->op;
756 cairo_pattern_t *src = &extents->source_pattern.base;
757 cairo_region_t *clip_region = NULL;
758 cairo_status_t status;
760 compositor->acquire (dst);
762 if (need_clip & NEED_CLIP_REGION) {
763 clip_region = _cairo_clip_get_region (extents->clip);
764 if ((need_clip & FORCE_CLIP_REGION) == 0 &&
765 _cairo_composite_rectangles_can_reduce_clip (extents,
768 if (clip_region != NULL) {
769 status = compositor->set_clip_region (dst, clip_region);
770 if (unlikely (status)) {
771 compositor->release (dst);
777 if (reduce_alpha_op (dst, op, &extents->source_pattern.base)) {
778 op = CAIRO_OPERATOR_ADD;
782 if (op == CAIRO_OPERATOR_SOURCE) {
783 status = clip_and_composite_source (compositor,
784 draw_closure, draw_func, mask_func,
787 if (op == CAIRO_OPERATOR_CLEAR) {
788 op = CAIRO_OPERATOR_DEST_OUT;
792 if (need_clip & NEED_CLIP_SURFACE) {
793 if (extents->is_bounded) {
794 status = clip_and_composite_with_mask (compositor,
800 status = clip_and_composite_combine (compositor,
806 status = draw_func (compositor,
808 op, src, &extents->source_sample_area,
815 if (status == CAIRO_STATUS_SUCCESS && ! extents->is_bounded) {
816 if (need_clip & NEED_CLIP_SURFACE)
817 status = fixup_unbounded_with_mask (compositor, dst, extents);
819 status = fixup_unbounded (compositor, dst, extents);
823 compositor->set_clip_region (dst, NULL);
825 compositor->release (dst);
830 static cairo_int_status_t
831 trim_extents_to_boxes (cairo_composite_rectangles_t *extents,
832 cairo_boxes_t *boxes)
836 _cairo_boxes_extents (boxes, &box);
837 return _cairo_composite_rectangles_intersect_mask_extents (extents, &box);
840 static cairo_status_t
841 upload_boxes (const cairo_mask_compositor_t *compositor,
842 cairo_composite_rectangles_t *extents,
843 cairo_boxes_t *boxes)
845 cairo_surface_t *dst = extents->surface;
846 const cairo_pattern_t *source = &extents->source_pattern.base;
847 cairo_surface_t *src;
848 cairo_rectangle_int_t limit;
849 cairo_int_status_t status;
852 src = _cairo_pattern_get_source ((cairo_surface_pattern_t *)source, &limit);
853 if (!(src->type == CAIRO_SURFACE_TYPE_IMAGE || src->type == dst->type))
854 return CAIRO_INT_STATUS_UNSUPPORTED;
856 if (! _cairo_matrix_is_integer_translation (&source->matrix, &tx, &ty))
857 return CAIRO_INT_STATUS_UNSUPPORTED;
859 /* Check that the data is entirely within the image */
860 if (extents->bounded.x + tx < limit.x || extents->bounded.y + ty < limit.y)
861 return CAIRO_INT_STATUS_UNSUPPORTED;
863 if (extents->bounded.x + extents->bounded.width + tx > limit.x + limit.width ||
864 extents->bounded.y + extents->bounded.height + ty > limit.y + limit.height)
865 return CAIRO_INT_STATUS_UNSUPPORTED;
870 if (src->type == CAIRO_SURFACE_TYPE_IMAGE)
871 status = compositor->draw_image_boxes (dst,
872 (cairo_image_surface_t *)src,
875 status = compositor->copy_boxes (dst, src, boxes, &extents->bounded,
881 static cairo_status_t
882 composite_boxes (const cairo_mask_compositor_t *compositor,
883 const cairo_composite_rectangles_t *extents,
884 cairo_boxes_t *boxes)
886 cairo_surface_t *dst = extents->surface;
887 cairo_operator_t op = extents->op;
888 const cairo_pattern_t *source = &extents->source_pattern.base;
889 cairo_bool_t need_clip_mask = extents->clip->path != NULL;
890 cairo_status_t status;
892 if (need_clip_mask &&
893 (! extents->is_bounded || op == CAIRO_OPERATOR_SOURCE))
895 return CAIRO_INT_STATUS_UNSUPPORTED;
898 status = compositor->acquire (dst);
899 if (unlikely (status))
902 if (! need_clip_mask && source->type == CAIRO_PATTERN_TYPE_SOLID) {
903 const cairo_color_t *color;
905 color = &((cairo_solid_pattern_t *) source)->color;
906 status = compositor->fill_boxes (dst, op, color, boxes);
908 cairo_surface_t *src, *mask = NULL;
910 int mask_x = 0, mask_y = 0;
912 if (need_clip_mask) {
913 mask = get_clip_source (compositor,
914 extents->clip, dst, &extents->bounded,
916 if (unlikely (mask->status)) {
917 status = mask->status;
918 cairo_surface_destroy (mask);
922 if (op == CAIRO_OPERATOR_CLEAR) {
924 op = CAIRO_OPERATOR_DEST_OUT;
928 if (source || mask == NULL) {
929 src = compositor->pattern_to_surface (dst, source, FALSE,
931 &extents->source_sample_area,
940 status = compositor->composite_boxes (dst, op, src, mask,
944 boxes, &extents->bounded);
946 cairo_surface_destroy (src);
947 cairo_surface_destroy (mask);
950 if (status == CAIRO_STATUS_SUCCESS && ! extents->is_bounded)
951 status = fixup_unbounded_boxes (compositor, extents, boxes);
953 compositor->release (dst);
958 static cairo_status_t
959 clip_and_composite_boxes (const cairo_mask_compositor_t *compositor,
960 cairo_composite_rectangles_t *extents,
961 cairo_boxes_t *boxes)
963 cairo_surface_t *dst = extents->surface;
964 cairo_int_status_t status;
966 if (boxes->num_boxes == 0) {
967 if (extents->is_bounded)
968 return CAIRO_STATUS_SUCCESS;
970 return fixup_unbounded_boxes (compositor, extents, boxes);
973 if (! boxes->is_pixel_aligned)
974 return CAIRO_INT_STATUS_UNSUPPORTED;
976 status = trim_extents_to_boxes (extents, boxes);
977 if (unlikely (status))
980 if (extents->source_pattern.base.type == CAIRO_PATTERN_TYPE_SURFACE &&
981 extents->clip->path == NULL &&
982 (extents->op == CAIRO_OPERATOR_SOURCE ||
983 (dst->is_clear && (extents->op == CAIRO_OPERATOR_OVER ||
984 extents->op == CAIRO_OPERATOR_ADD))))
986 status = upload_boxes (compositor, extents, boxes);
987 if (status != CAIRO_INT_STATUS_UNSUPPORTED)
991 return composite_boxes (compositor, extents, boxes);
994 /* high-level compositor interface */
996 static cairo_int_status_t
997 _cairo_mask_compositor_paint (const cairo_compositor_t *_compositor,
998 cairo_composite_rectangles_t *extents)
1000 cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
1001 cairo_boxes_t boxes;
1002 cairo_int_status_t status;
1004 status = compositor->check_composite (extents);
1005 if (unlikely (status))
1008 _cairo_clip_steal_boxes (extents->clip, &boxes);
1009 status = clip_and_composite_boxes (compositor, extents, &boxes);
1010 _cairo_clip_unsteal_boxes (extents->clip, &boxes);
1015 struct composite_opacity_info {
1016 const cairo_mask_compositor_t *compositor;
1018 cairo_surface_t *dst;
1019 cairo_surface_t *src;
1024 static void composite_opacity(void *closure,
1025 int16_t x, int16_t y,
1026 int16_t w, int16_t h,
1029 struct composite_opacity_info *info = closure;
1030 const cairo_mask_compositor_t *compositor = info->compositor;
1031 cairo_surface_t *mask;
1033 cairo_color_t color;
1034 cairo_solid_pattern_t solid;
1036 _cairo_color_init_rgba (&color, 0, 0, 0, info->opacity * coverage);
1037 _cairo_pattern_init_solid (&solid, &color);
1038 mask = compositor->pattern_to_surface (info->dst, &solid.base, TRUE,
1039 &_cairo_unbounded_rectangle,
1040 &_cairo_unbounded_rectangle,
1042 if (likely (mask->status == CAIRO_STATUS_SUCCESS)) {
1044 compositor->composite (info->dst, info->op, info->src, mask,
1045 x + info->src_x, y + info->src_y,
1050 compositor->composite (info->dst, info->op, mask, NULL,
1058 cairo_surface_destroy (mask);
1061 static cairo_int_status_t
1062 composite_opacity_boxes (const cairo_mask_compositor_t *compositor,
1063 cairo_surface_t *dst,
1065 cairo_operator_t op,
1066 const cairo_pattern_t *src_pattern,
1067 const cairo_rectangle_int_t *src_sample,
1070 const cairo_rectangle_int_t *extents,
1073 const cairo_solid_pattern_t *mask_pattern = closure;
1074 struct composite_opacity_info info;
1079 info.compositor = compositor;
1083 if (src_pattern != NULL) {
1084 info.src = compositor->pattern_to_surface (dst, src_pattern, FALSE,
1085 extents, src_sample,
1086 &info.src_x, &info.src_y);
1087 if (unlikely (info.src->status))
1088 return info.src->status;
1092 info.opacity = mask_pattern->color.alpha / (double) 0xffff;
1094 /* XXX for lots of boxes create a clip region for the fully opaque areas */
1095 for (i = 0; i < clip->num_boxes; i++)
1096 do_unaligned_box(composite_opacity, &info,
1097 &clip->boxes[i], dst_x, dst_y);
1098 cairo_surface_destroy (info.src);
1100 return CAIRO_STATUS_SUCCESS;
1103 struct composite_box_info {
1104 const cairo_mask_compositor_t *compositor;
1105 cairo_surface_t *dst;
1106 cairo_surface_t *src;
1111 static void composite_box(void *closure,
1112 int16_t x, int16_t y,
1113 int16_t w, int16_t h,
1116 struct composite_box_info *info = closure;
1117 const cairo_mask_compositor_t *compositor = info->compositor;
1119 if (! CAIRO_ALPHA_SHORT_IS_OPAQUE (coverage)) {
1120 cairo_surface_t *mask;
1121 cairo_color_t color;
1122 cairo_solid_pattern_t solid;
1125 _cairo_color_init_rgba (&color, 0, 0, 0, coverage / (double)0xffff);
1126 _cairo_pattern_init_solid (&solid, &color);
1128 mask = compositor->pattern_to_surface (info->dst, &solid.base, FALSE,
1129 &_cairo_unbounded_rectangle,
1130 &_cairo_unbounded_rectangle,
1133 if (likely (mask->status == CAIRO_STATUS_SUCCESS)) {
1134 compositor->composite (info->dst, info->op, info->src, mask,
1135 x + info->src_x, y + info->src_y,
1141 cairo_surface_destroy (mask);
1143 compositor->composite (info->dst, info->op, info->src, NULL,
1144 x + info->src_x, y + info->src_y,
1151 static cairo_int_status_t
1152 composite_mask_clip_boxes (const cairo_mask_compositor_t *compositor,
1153 cairo_surface_t *dst,
1155 cairo_operator_t op,
1156 const cairo_pattern_t *src_pattern,
1157 const cairo_rectangle_int_t *src_sample,
1160 const cairo_rectangle_int_t *extents,
1163 cairo_composite_rectangles_t *composite = closure;
1164 struct composite_box_info info;
1167 assert (src_pattern == NULL);
1168 assert (op == CAIRO_OPERATOR_SOURCE);
1170 info.compositor = compositor;
1171 info.op = CAIRO_OPERATOR_SOURCE;
1173 info.src = compositor->pattern_to_surface (dst,
1174 &composite->mask_pattern.base,
1176 &composite->mask_sample_area,
1177 &info.src_x, &info.src_y);
1178 if (unlikely (info.src->status))
1179 return info.src->status;
1181 info.src_x += dst_x;
1182 info.src_y += dst_y;
1184 for (i = 0; i < clip->num_boxes; i++)
1185 do_unaligned_box(composite_box, &info, &clip->boxes[i], dst_x, dst_y);
1187 cairo_surface_destroy (info.src);
1189 return CAIRO_STATUS_SUCCESS;
1192 static cairo_int_status_t
1193 composite_mask (const cairo_mask_compositor_t *compositor,
1194 cairo_surface_t *dst,
1196 cairo_operator_t op,
1197 const cairo_pattern_t *src_pattern,
1198 const cairo_rectangle_int_t *src_sample,
1201 const cairo_rectangle_int_t *extents,
1204 cairo_composite_rectangles_t *composite = closure;
1205 cairo_surface_t *src, *mask;
1209 if (src_pattern != NULL) {
1210 src = compositor->pattern_to_surface (dst, src_pattern, FALSE,
1211 extents, src_sample,
1213 if (unlikely (src->status))
1216 mask = compositor->pattern_to_surface (dst, &composite->mask_pattern.base, TRUE,
1217 extents, &composite->mask_sample_area,
1219 if (unlikely (mask->status)) {
1220 cairo_surface_destroy (src);
1221 return mask->status;
1224 compositor->composite (dst, op, src, mask,
1225 extents->x + src_x, extents->y + src_y,
1226 extents->x + mask_x, extents->y + mask_y,
1227 extents->x - dst_x, extents->y - dst_y,
1228 extents->width, extents->height);
1230 cairo_surface_destroy (mask);
1231 cairo_surface_destroy (src);
1233 src = compositor->pattern_to_surface (dst, &composite->mask_pattern.base, FALSE,
1234 extents, &composite->mask_sample_area,
1236 if (unlikely (src->status))
1239 compositor->composite (dst, op, src, NULL,
1240 extents->x + src_x, extents->y + src_y,
1242 extents->x - dst_x, extents->y - dst_y,
1243 extents->width, extents->height);
1245 cairo_surface_destroy (src);
1248 return CAIRO_STATUS_SUCCESS;
1251 static cairo_int_status_t
1252 _cairo_mask_compositor_mask (const cairo_compositor_t *_compositor,
1253 cairo_composite_rectangles_t *extents)
1255 const cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
1256 cairo_int_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
1258 status = compositor->check_composite (extents);
1259 if (unlikely (status))
1262 if (extents->mask_pattern.base.type == CAIRO_PATTERN_TYPE_SOLID &&
1263 extents->clip->path == NULL &&
1264 _cairo_clip_is_region (extents->clip)) {
1265 status = clip_and_composite (compositor,
1266 composite_opacity_boxes,
1267 composite_opacity_boxes,
1268 &extents->mask_pattern.solid,
1269 extents, need_unbounded_clip (extents));
1271 status = clip_and_composite (compositor,
1273 extents->clip->path == NULL ? composite_mask_clip_boxes : NULL,
1275 extents, need_bounded_clip (extents));
1281 static cairo_int_status_t
1282 _cairo_mask_compositor_stroke (const cairo_compositor_t *_compositor,
1283 cairo_composite_rectangles_t *extents,
1284 const cairo_path_fixed_t *path,
1285 const cairo_stroke_style_t *style,
1286 const cairo_matrix_t *ctm,
1287 const cairo_matrix_t *ctm_inverse,
1289 cairo_antialias_t antialias)
1291 const cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
1292 cairo_surface_t *mask;
1293 cairo_surface_pattern_t pattern;
1294 cairo_int_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
1296 status = compositor->check_composite (extents);
1297 if (unlikely (status))
1300 if (_cairo_path_fixed_stroke_is_rectilinear (path)) {
1301 cairo_boxes_t boxes;
1303 _cairo_boxes_init_with_clip (&boxes, extents->clip);
1304 status = _cairo_path_fixed_stroke_rectilinear_to_boxes (path,
1309 if (likely (status == CAIRO_INT_STATUS_SUCCESS))
1310 status = clip_and_composite_boxes (compositor, extents, &boxes);
1311 _cairo_boxes_fini (&boxes);
1315 if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
1316 mask = cairo_surface_create_similar_image (extents->surface,
1318 extents->bounded.width,
1319 extents->bounded.height);
1320 if (unlikely (mask->status)) {
1321 status = mask->status;
1322 cairo_surface_destroy (mask);
1326 status = _cairo_surface_offset_stroke (mask,
1330 &_cairo_pattern_white.base,
1331 path, style, ctm, ctm_inverse,
1332 tolerance, antialias,
1334 if (unlikely (status)) {
1335 cairo_surface_destroy (mask);
1339 _cairo_pattern_init_for_surface (&pattern, mask);
1340 cairo_surface_destroy (mask);
1342 cairo_matrix_init_translate (&pattern.base.matrix,
1343 -extents->bounded.x,
1344 -extents->bounded.y);
1345 pattern.base.filter = CAIRO_FILTER_NEAREST;
1346 pattern.base.extend = CAIRO_EXTEND_NONE;
1347 status = _cairo_surface_mask (extents->surface,
1349 &extents->source_pattern.base,
1352 _cairo_pattern_fini (&pattern.base);
1358 static cairo_int_status_t
1359 _cairo_mask_compositor_fill (const cairo_compositor_t *_compositor,
1360 cairo_composite_rectangles_t *extents,
1361 const cairo_path_fixed_t *path,
1362 cairo_fill_rule_t fill_rule,
1364 cairo_antialias_t antialias)
1366 const cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
1367 cairo_surface_t *mask;
1368 cairo_surface_pattern_t pattern;
1369 cairo_int_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
1371 status = compositor->check_composite (extents);
1372 if (unlikely (status))
1375 if (_cairo_path_fixed_fill_is_rectilinear (path)) {
1376 cairo_boxes_t boxes;
1378 _cairo_boxes_init_with_clip (&boxes, extents->clip);
1379 status = _cairo_path_fixed_fill_rectilinear_to_boxes (path,
1383 if (likely (status == CAIRO_INT_STATUS_SUCCESS))
1384 status = clip_and_composite_boxes (compositor, extents, &boxes);
1385 _cairo_boxes_fini (&boxes);
1388 if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
1389 mask = cairo_surface_create_similar_image (extents->surface,
1391 extents->bounded.width,
1392 extents->bounded.height);
1393 if (unlikely (mask->status)) {
1394 status = mask->status;
1395 cairo_surface_destroy (mask);
1399 status = _cairo_surface_offset_fill (mask,
1403 &_cairo_pattern_white.base,
1404 path, fill_rule, tolerance, antialias,
1406 if (unlikely (status)) {
1407 cairo_surface_destroy (mask);
1411 _cairo_pattern_init_for_surface (&pattern, mask);
1412 cairo_surface_destroy (mask);
1414 cairo_matrix_init_translate (&pattern.base.matrix,
1415 -extents->bounded.x,
1416 -extents->bounded.y);
1417 pattern.base.filter = CAIRO_FILTER_NEAREST;
1418 pattern.base.extend = CAIRO_EXTEND_NONE;
1419 status = _cairo_surface_mask (extents->surface,
1421 &extents->source_pattern.base,
1424 _cairo_pattern_fini (&pattern.base);
1430 static cairo_int_status_t
1431 _cairo_mask_compositor_glyphs (const cairo_compositor_t *_compositor,
1432 cairo_composite_rectangles_t *extents,
1433 cairo_scaled_font_t *scaled_font,
1434 cairo_glyph_t *glyphs,
1436 cairo_bool_t overlap)
1438 const cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
1439 cairo_surface_t *mask;
1440 cairo_surface_pattern_t pattern;
1441 cairo_int_status_t status;
1443 status = compositor->check_composite (extents);
1444 if (unlikely (status))
1445 return CAIRO_INT_STATUS_UNSUPPORTED;
1447 mask = cairo_surface_create_similar_image (extents->surface,
1449 extents->bounded.width,
1450 extents->bounded.height);
1451 if (unlikely (mask->status)) {
1452 status = mask->status;
1453 cairo_surface_destroy (mask);
1457 status = _cairo_surface_offset_glyphs (mask,
1461 &_cairo_pattern_white.base,
1462 scaled_font, glyphs, num_glyphs,
1464 if (unlikely (status)) {
1465 cairo_surface_destroy (mask);
1469 _cairo_pattern_init_for_surface (&pattern, mask);
1470 cairo_surface_destroy (mask);
1472 cairo_matrix_init_translate (&pattern.base.matrix,
1473 -extents->bounded.x,
1474 -extents->bounded.y);
1475 pattern.base.filter = CAIRO_FILTER_NEAREST;
1476 pattern.base.extend = CAIRO_EXTEND_NONE;
1477 status = _cairo_surface_mask (extents->surface,
1479 &extents->source_pattern.base,
1482 _cairo_pattern_fini (&pattern.base);
1488 _cairo_mask_compositor_init (cairo_mask_compositor_t *compositor,
1489 const cairo_compositor_t *delegate)
1491 compositor->base.delegate = delegate;
1493 compositor->base.paint = _cairo_mask_compositor_paint;
1494 compositor->base.mask = _cairo_mask_compositor_mask;
1495 compositor->base.fill = _cairo_mask_compositor_fill;
1496 compositor->base.stroke = _cairo_mask_compositor_stroke;
1497 compositor->base.glyphs = _cairo_mask_compositor_glyphs;