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_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_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 /* Handles compositing with a clip surface when we have to do the operation
302 * in two pieces and combine them together.
304 static cairo_status_t
305 clip_and_composite_combine (const cairo_mask_compositor_t *compositor,
307 draw_func_t draw_func,
309 const cairo_pattern_t *pattern,
310 const cairo_composite_rectangles_t*extents)
312 cairo_surface_t *dst = extents->surface;
313 cairo_surface_t *tmp, *clip;
314 cairo_status_t status;
316 tmp = _cairo_surface_create_similar_scratch (dst, dst->content,
317 extents->bounded.width,
318 extents->bounded.height);
319 if (unlikely (tmp->status))
322 compositor->composite (tmp, CAIRO_OPERATOR_SOURCE, dst, NULL,
323 extents->bounded.x, extents->bounded.y,
326 extents->bounded.width, extents->bounded.height);
328 status = draw_func (compositor, tmp, draw_closure, op,
329 pattern, &extents->source_sample_area,
330 extents->bounded.x, extents->bounded.y,
331 &extents->bounded, NULL);
332 if (unlikely (status))
335 clip = _cairo_clip_get_image (extents->clip, dst, &extents->bounded);
336 if (unlikely ((status = clip->status)))
340 compositor->composite (dst, CAIRO_OPERATOR_SOURCE, tmp, clip,
343 extents->bounded.x, extents->bounded.y,
344 extents->bounded.width, extents->bounded.height);
346 /* Punch the clip out of the destination */
347 compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, clip, NULL,
350 extents->bounded.x, extents->bounded.y,
351 extents->bounded.width, extents->bounded.height);
353 /* Now add the two results together */
354 compositor->composite (dst, CAIRO_OPERATOR_ADD, tmp, clip,
357 extents->bounded.x, extents->bounded.y,
358 extents->bounded.width, extents->bounded.height);
360 cairo_surface_destroy (clip);
363 cairo_surface_destroy (tmp);
367 /* Handles compositing for %CAIRO_OPERATOR_SOURCE, which is special; it's
368 * defined as (src IN mask IN clip) ADD (dst OUT (mask IN clip))
370 static cairo_status_t
371 clip_and_composite_source (const cairo_mask_compositor_t *compositor,
373 draw_func_t draw_func,
374 draw_func_t mask_func,
375 cairo_pattern_t *pattern,
376 const cairo_composite_rectangles_t *extents)
378 cairo_surface_t *dst = extents->surface;
379 cairo_surface_t *mask, *src;
382 /* Create a surface that is mask IN clip */
383 mask = create_composite_mask (compositor, dst, draw_closure,
384 draw_func, mask_func,
386 if (unlikely (mask->status))
389 src = compositor->pattern_to_surface (dst,
393 &extents->source_sample_area,
395 if (unlikely (src->status)) {
396 cairo_surface_destroy (mask);
401 compositor->composite (dst, CAIRO_OPERATOR_SOURCE, src, mask,
402 extents->bounded.x + src_x, extents->bounded.y + src_y,
404 extents->bounded.x, extents->bounded.y,
405 extents->bounded.width, extents->bounded.height);
407 /* Compute dest' = dest OUT (mask IN clip) */
408 compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
410 extents->bounded.x, extents->bounded.y,
411 extents->bounded.width, extents->bounded.height);
413 /* Now compute (src IN (mask IN clip)) ADD dest' */
414 compositor->composite (dst, CAIRO_OPERATOR_ADD, src, mask,
415 extents->bounded.x + src_x, extents->bounded.y + src_y,
417 extents->bounded.x, extents->bounded.y,
418 extents->bounded.width, extents->bounded.height);
421 cairo_surface_destroy (src);
422 cairo_surface_destroy (mask);
424 return CAIRO_STATUS_SUCCESS;
428 can_reduce_alpha_op (cairo_operator_t op)
432 case CAIRO_OPERATOR_OVER:
433 case CAIRO_OPERATOR_SOURCE:
434 case CAIRO_OPERATOR_ADD:
442 reduce_alpha_op (cairo_surface_t *dst,
444 const cairo_pattern_t *pattern)
446 return dst->is_clear &&
447 dst->content == CAIRO_CONTENT_ALPHA &&
448 _cairo_pattern_is_opaque_solid (pattern) &&
449 can_reduce_alpha_op (op);
452 static cairo_status_t
453 fixup_unbounded (const cairo_mask_compositor_t *compositor,
454 cairo_surface_t *dst,
455 const cairo_composite_rectangles_t *extents)
457 cairo_rectangle_int_t rects[4];
460 if (extents->bounded.width == extents->unbounded.width &&
461 extents->bounded.height == extents->unbounded.height)
463 return CAIRO_STATUS_SUCCESS;
467 if (extents->bounded.width == 0 || extents->bounded.height == 0) {
468 rects[n].x = extents->unbounded.x;
469 rects[n].width = extents->unbounded.width;
470 rects[n].y = extents->unbounded.y;
471 rects[n].height = extents->unbounded.height;
475 if (extents->bounded.y != extents->unbounded.y) {
476 rects[n].x = extents->unbounded.x;
477 rects[n].width = extents->unbounded.width;
478 rects[n].y = extents->unbounded.y;
479 rects[n].height = extents->bounded.y - extents->unbounded.y;
483 if (extents->bounded.x != extents->unbounded.x) {
484 rects[n].x = extents->unbounded.x;
485 rects[n].width = extents->bounded.x - extents->unbounded.x;
486 rects[n].y = extents->bounded.y;
487 rects[n].height = extents->bounded.height;
491 if (extents->bounded.x + extents->bounded.width != extents->unbounded.x + extents->unbounded.width) {
492 rects[n].x = extents->bounded.x + extents->bounded.width;
493 rects[n].width = extents->unbounded.x + extents->unbounded.width - rects[n].x;
494 rects[n].y = extents->bounded.y;
495 rects[n].height = extents->bounded.height;
499 if (extents->bounded.y + extents->bounded.height != extents->unbounded.y + extents->unbounded.height) {
500 rects[n].x = extents->unbounded.x;
501 rects[n].width = extents->unbounded.width;
502 rects[n].y = extents->bounded.y + extents->bounded.height;
503 rects[n].height = extents->unbounded.y + extents->unbounded.height - rects[n].y;
508 return compositor->fill_rectangles (dst, CAIRO_OPERATOR_CLEAR,
509 CAIRO_COLOR_TRANSPARENT,
513 static cairo_status_t
514 fixup_unbounded_with_mask (const cairo_mask_compositor_t *compositor,
515 cairo_surface_t *dst,
516 const cairo_composite_rectangles_t *extents)
518 cairo_clip_t *clip = extents->clip;
519 cairo_surface_t *mask;
522 mask = _cairo_clip_get_image (clip, dst, &extents->unbounded);
523 if (unlikely (mask->status))
526 mask_x = -extents->unbounded.x;
527 mask_y = -extents->unbounded.y;
530 if (extents->bounded.y != extents->unbounded.y) {
531 int x = extents->unbounded.x;
532 int y = extents->unbounded.y;
533 int width = extents->unbounded.width;
534 int height = extents->bounded.y - y;
536 compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
537 x + mask_x, y + mask_y,
544 if (extents->bounded.x != extents->unbounded.x) {
545 int x = extents->unbounded.x;
546 int y = extents->bounded.y;
547 int width = extents->bounded.x - x;
548 int height = extents->bounded.height;
550 compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
551 x + mask_x, y + mask_y,
558 if (extents->bounded.x + extents->bounded.width != extents->unbounded.x + extents->unbounded.width) {
559 int x = extents->bounded.x + extents->bounded.width;
560 int y = extents->bounded.y;
561 int width = extents->unbounded.x + extents->unbounded.width - x;
562 int height = extents->bounded.height;
564 compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
565 x + mask_x, y + mask_y,
572 if (extents->bounded.y + extents->bounded.height != extents->unbounded.y + extents->unbounded.height) {
573 int x = extents->unbounded.x;
574 int y = extents->bounded.y + extents->bounded.height;
575 int width = extents->unbounded.width;
576 int height = extents->unbounded.y + extents->unbounded.height - y;
578 compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
579 x + mask_x, y + mask_y,
585 cairo_surface_destroy (mask);
587 return CAIRO_STATUS_SUCCESS;
590 static cairo_status_t
591 fixup_unbounded_boxes (const cairo_mask_compositor_t *compositor,
592 const cairo_composite_rectangles_t *extents,
593 cairo_boxes_t *boxes)
595 cairo_surface_t *dst = extents->surface;
597 cairo_region_t *clip_region;
599 cairo_status_t status;
600 struct _cairo_boxes_chunk *chunk;
603 assert (boxes->is_pixel_aligned);
606 if (_cairo_clip_is_region (extents->clip) &&
607 (clip_region = _cairo_clip_get_region (extents->clip)) &&
608 cairo_region_contains_rectangle (clip_region,
609 &extents->bounded) == CAIRO_REGION_OVERLAP_IN)
613 if (boxes->num_boxes <= 1 && clip_region == NULL)
614 return fixup_unbounded (compositor, dst, extents);
616 _cairo_boxes_init (&clear);
618 box.p1.x = _cairo_fixed_from_int (extents->unbounded.x + extents->unbounded.width);
619 box.p1.y = _cairo_fixed_from_int (extents->unbounded.y);
620 box.p2.x = _cairo_fixed_from_int (extents->unbounded.x);
621 box.p2.y = _cairo_fixed_from_int (extents->unbounded.y + extents->unbounded.height);
623 if (clip_region == NULL) {
626 _cairo_boxes_init (&tmp);
628 status = _cairo_boxes_add (&tmp, CAIRO_ANTIALIAS_DEFAULT, &box);
629 assert (status == CAIRO_STATUS_SUCCESS);
631 tmp.chunks.next = &boxes->chunks;
632 tmp.num_boxes += boxes->num_boxes;
634 status = _cairo_bentley_ottmann_tessellate_boxes (&tmp,
635 CAIRO_FILL_RULE_WINDING,
638 tmp.chunks.next = NULL;
640 pixman_box32_t *pbox;
642 pbox = pixman_region32_rectangles (&clip_region->rgn, &i);
643 _cairo_boxes_limit (&clear, (cairo_box_t *) pbox, i);
645 status = _cairo_boxes_add (&clear, CAIRO_ANTIALIAS_DEFAULT, &box);
646 assert (status == CAIRO_STATUS_SUCCESS);
648 for (chunk = &boxes->chunks; chunk != NULL; chunk = chunk->next) {
649 for (i = 0; i < chunk->count; i++) {
650 status = _cairo_boxes_add (&clear,
651 CAIRO_ANTIALIAS_DEFAULT,
653 if (unlikely (status)) {
654 _cairo_boxes_fini (&clear);
660 status = _cairo_bentley_ottmann_tessellate_boxes (&clear,
661 CAIRO_FILL_RULE_WINDING,
665 if (likely (status == CAIRO_STATUS_SUCCESS)) {
666 status = compositor->fill_boxes (dst,
667 CAIRO_OPERATOR_CLEAR,
668 CAIRO_COLOR_TRANSPARENT,
672 _cairo_boxes_fini (&clear);
678 NEED_CLIP_REGION = 0x1,
679 NEED_CLIP_SURFACE = 0x2,
680 FORCE_CLIP_REGION = 0x4,
684 need_bounded_clip (cairo_composite_rectangles_t *extents)
686 unsigned int flags = NEED_CLIP_REGION;
687 if (! _cairo_clip_is_region (extents->clip))
688 flags |= NEED_CLIP_SURFACE;
693 need_unbounded_clip (cairo_composite_rectangles_t *extents)
695 unsigned int flags = 0;
696 if (! extents->is_bounded) {
697 flags |= NEED_CLIP_REGION;
698 if (! _cairo_clip_is_region (extents->clip))
699 flags |= NEED_CLIP_SURFACE;
701 if (extents->clip->path != NULL)
702 flags |= NEED_CLIP_SURFACE;
706 static cairo_status_t
707 clip_and_composite (const cairo_mask_compositor_t *compositor,
708 draw_func_t draw_func,
709 draw_func_t mask_func,
711 cairo_composite_rectangles_t*extents,
712 unsigned int need_clip)
714 cairo_surface_t *dst = extents->surface;
715 cairo_operator_t op = extents->op;
716 cairo_pattern_t *src = &extents->source_pattern.base;
717 cairo_region_t *clip_region = NULL;
718 cairo_status_t status;
720 compositor->acquire (dst);
722 if (need_clip & NEED_CLIP_REGION) {
723 clip_region = _cairo_clip_get_region (extents->clip);
724 if ((need_clip & FORCE_CLIP_REGION) == 0 &&
725 _cairo_composite_rectangles_can_reduce_clip (extents,
728 if (clip_region != NULL) {
729 status = compositor->set_clip_region (dst, clip_region);
730 if (unlikely (status)) {
731 compositor->release (dst);
737 if (reduce_alpha_op (dst, op, &extents->source_pattern.base)) {
738 op = CAIRO_OPERATOR_ADD;
742 if (op == CAIRO_OPERATOR_SOURCE) {
743 status = clip_and_composite_source (compositor,
744 draw_closure, draw_func, mask_func,
747 if (op == CAIRO_OPERATOR_CLEAR) {
748 op = CAIRO_OPERATOR_DEST_OUT;
752 if (need_clip & NEED_CLIP_SURFACE) {
753 if (extents->is_bounded) {
754 status = clip_and_composite_with_mask (compositor,
760 status = clip_and_composite_combine (compositor,
766 status = draw_func (compositor,
768 op, src, &extents->source_sample_area,
775 if (status == CAIRO_STATUS_SUCCESS && ! extents->is_bounded) {
776 if (need_clip & NEED_CLIP_SURFACE)
777 status = fixup_unbounded_with_mask (compositor, dst, extents);
779 status = fixup_unbounded (compositor, dst, extents);
783 compositor->set_clip_region (dst, NULL);
785 compositor->release (dst);
790 static cairo_int_status_t
791 trim_extents_to_boxes (cairo_composite_rectangles_t *extents,
792 cairo_boxes_t *boxes)
796 _cairo_boxes_extents (boxes, &box);
797 return _cairo_composite_rectangles_intersect_mask_extents (extents, &box);
800 static cairo_status_t
801 upload_boxes (const cairo_mask_compositor_t *compositor,
802 cairo_composite_rectangles_t *extents,
803 cairo_boxes_t *boxes)
805 cairo_surface_t *dst = extents->surface;
806 const cairo_pattern_t *source = &extents->source_pattern.base;
807 cairo_surface_t *src;
808 cairo_rectangle_int_t limit;
809 cairo_int_status_t status;
812 src = _cairo_pattern_get_source ((cairo_surface_pattern_t *)source, &limit);
813 if (!(src->type == CAIRO_SURFACE_TYPE_IMAGE || src->type == dst->type))
814 return CAIRO_INT_STATUS_UNSUPPORTED;
816 if (! _cairo_matrix_is_integer_translation (&source->matrix, &tx, &ty))
817 return CAIRO_INT_STATUS_UNSUPPORTED;
819 /* Check that the data is entirely within the image */
820 if (extents->bounded.x + tx < limit.x || extents->bounded.y + ty < limit.y)
821 return CAIRO_INT_STATUS_UNSUPPORTED;
823 if (extents->bounded.x + extents->bounded.width + tx > limit.x + limit.width ||
824 extents->bounded.y + extents->bounded.height + ty > limit.y + limit.height)
825 return CAIRO_INT_STATUS_UNSUPPORTED;
830 if (src->type == CAIRO_SURFACE_TYPE_IMAGE)
831 status = compositor->draw_image_boxes (dst,
832 (cairo_image_surface_t *)src,
835 status = compositor->copy_boxes (dst, src, boxes, &extents->bounded,
841 static cairo_status_t
842 composite_boxes (const cairo_mask_compositor_t *compositor,
843 const cairo_composite_rectangles_t *extents,
844 cairo_boxes_t *boxes)
846 cairo_surface_t *dst = extents->surface;
847 cairo_operator_t op = extents->op;
848 const cairo_pattern_t *source = &extents->source_pattern.base;
849 cairo_bool_t need_clip_mask = extents->clip->path != NULL;
850 cairo_status_t status;
852 if (need_clip_mask &&
853 (! extents->is_bounded || op == CAIRO_OPERATOR_SOURCE))
855 return CAIRO_INT_STATUS_UNSUPPORTED;
858 status = compositor->acquire (dst);
859 if (unlikely (status))
862 if (! need_clip_mask && source->type == CAIRO_PATTERN_TYPE_SOLID) {
863 const cairo_color_t *color;
865 color = &((cairo_solid_pattern_t *) source)->color;
866 status = compositor->fill_boxes (dst, op, color, boxes);
868 cairo_surface_t *src, *mask = NULL;
870 int mask_x = 0, mask_y = 0;
872 if (need_clip_mask) {
873 mask = _cairo_clip_get_image (extents->clip, dst,
875 if (unlikely (mask->status))
878 if (op == CAIRO_OPERATOR_CLEAR) {
880 op = CAIRO_OPERATOR_DEST_OUT;
883 mask_x = -extents->bounded.x;
884 mask_y = -extents->bounded.y;
887 if (source || mask == NULL) {
888 src = compositor->pattern_to_surface (dst, source, FALSE,
890 &extents->source_sample_area,
899 status = compositor->composite_boxes (dst, op, src, mask,
903 boxes, &extents->bounded);
905 cairo_surface_destroy (src);
906 cairo_surface_destroy (mask);
909 if (status == CAIRO_STATUS_SUCCESS && ! extents->is_bounded)
910 status = fixup_unbounded_boxes (compositor, extents, boxes);
912 compositor->release (dst);
917 static cairo_status_t
918 clip_and_composite_boxes (const cairo_mask_compositor_t *compositor,
919 cairo_composite_rectangles_t *extents,
920 cairo_boxes_t *boxes)
922 cairo_surface_t *dst = extents->surface;
923 cairo_int_status_t status;
925 if (boxes->num_boxes == 0) {
926 if (extents->is_bounded)
927 return CAIRO_STATUS_SUCCESS;
929 return fixup_unbounded_boxes (compositor, extents, boxes);
932 if (! boxes->is_pixel_aligned)
933 return CAIRO_INT_STATUS_UNSUPPORTED;
935 status = trim_extents_to_boxes (extents, boxes);
936 if (unlikely (status))
939 if (extents->source_pattern.base.type == CAIRO_PATTERN_TYPE_SURFACE &&
940 extents->clip->path == NULL &&
941 (extents->op == CAIRO_OPERATOR_SOURCE ||
942 (dst->is_clear && (extents->op == CAIRO_OPERATOR_OVER ||
943 extents->op == CAIRO_OPERATOR_ADD))))
945 status = upload_boxes (compositor, extents, boxes);
946 if (status != CAIRO_INT_STATUS_UNSUPPORTED)
950 return composite_boxes (compositor, extents, boxes);
953 /* high-level compositor interface */
955 static cairo_int_status_t
956 _cairo_mask_compositor_paint (const cairo_compositor_t *_compositor,
957 cairo_composite_rectangles_t *extents)
959 cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
961 cairo_int_status_t status;
963 _cairo_clip_steal_boxes (extents->clip, &boxes);
964 status = clip_and_composite_boxes (compositor, extents, &boxes);
965 _cairo_clip_unsteal_boxes (extents->clip, &boxes);
970 struct composite_opacity_info {
971 const cairo_mask_compositor_t *compositor;
973 cairo_surface_t *dst;
974 cairo_surface_t *src;
979 static void composite_opacity(void *closure,
980 int16_t x, int16_t y,
981 int16_t w, int16_t h,
984 struct composite_opacity_info *info = closure;
985 const cairo_mask_compositor_t *compositor = info->compositor;
986 cairo_surface_t *mask;
989 cairo_solid_pattern_t solid;
991 _cairo_color_init_rgba (&color, 0, 0, 0, info->opacity * coverage);
992 _cairo_pattern_init_solid (&solid, &color);
993 mask = compositor->pattern_to_surface (info->dst, &solid.base, TRUE,
994 &_cairo_unbounded_rectangle,
995 &_cairo_unbounded_rectangle,
997 if (likely (mask->status == CAIRO_STATUS_SUCCESS)) {
999 compositor->composite (info->dst, info->op, info->src, mask,
1000 x + info->src_x, y + info->src_y,
1005 compositor->composite (info->dst, info->op, mask, NULL,
1013 cairo_surface_destroy (mask);
1016 static cairo_int_status_t
1017 composite_opacity_boxes (const cairo_mask_compositor_t *compositor,
1018 cairo_surface_t *dst,
1020 cairo_operator_t op,
1021 const cairo_pattern_t *src_pattern,
1022 const cairo_rectangle_int_t *src_sample,
1025 const cairo_rectangle_int_t *extents,
1028 const cairo_solid_pattern_t *mask_pattern = closure;
1029 struct composite_opacity_info info;
1034 info.compositor = compositor;
1038 if (src_pattern != NULL) {
1039 info.src = compositor->pattern_to_surface (dst, src_pattern, FALSE,
1040 extents, src_sample,
1041 &info.src_x, &info.src_y);
1042 if (unlikely (info.src->status))
1043 return info.src->status;
1047 info.opacity = mask_pattern->color.alpha / (double) 0xffff;
1049 /* XXX for lots of boxes create a clip region for the fully opaque areas */
1050 for (i = 0; i < clip->num_boxes; i++)
1051 do_unaligned_box(composite_opacity, &info,
1052 &clip->boxes[i], dst_x, dst_y);
1053 cairo_surface_destroy (info.src);
1055 return CAIRO_STATUS_SUCCESS;
1058 struct composite_box_info {
1059 const cairo_mask_compositor_t *compositor;
1060 cairo_surface_t *dst;
1061 cairo_surface_t *src;
1066 static void composite_box(void *closure,
1067 int16_t x, int16_t y,
1068 int16_t w, int16_t h,
1071 struct composite_box_info *info = closure;
1072 const cairo_mask_compositor_t *compositor = info->compositor;
1074 if (! CAIRO_ALPHA_SHORT_IS_OPAQUE (coverage)) {
1075 cairo_surface_t *mask;
1076 cairo_color_t color;
1077 cairo_solid_pattern_t solid;
1080 _cairo_color_init_rgba (&color, 0, 0, 0, coverage / (double)0xffff);
1081 _cairo_pattern_init_solid (&solid, &color);
1083 mask = compositor->pattern_to_surface (info->dst, &solid.base, FALSE,
1084 &_cairo_unbounded_rectangle,
1085 &_cairo_unbounded_rectangle,
1088 if (likely (mask->status == CAIRO_STATUS_SUCCESS)) {
1089 compositor->composite (info->dst, info->op, info->src, mask,
1090 x + info->src_x, y + info->src_y,
1096 cairo_surface_destroy (mask);
1098 compositor->composite (info->dst, info->op, info->src, NULL,
1099 x + info->src_x, y + info->src_y,
1106 static cairo_int_status_t
1107 composite_mask_clip_boxes (const cairo_mask_compositor_t *compositor,
1108 cairo_surface_t *dst,
1110 cairo_operator_t op,
1111 const cairo_pattern_t *src_pattern,
1112 const cairo_rectangle_int_t *src_sample,
1115 const cairo_rectangle_int_t *extents,
1118 cairo_composite_rectangles_t *composite = closure;
1119 struct composite_box_info info;
1122 assert (src_pattern == NULL);
1123 assert (op == CAIRO_OPERATOR_SOURCE);
1125 info.compositor = compositor;
1126 info.op = CAIRO_OPERATOR_SOURCE;
1128 info.src = compositor->pattern_to_surface (dst,
1129 &composite->mask_pattern.base,
1131 &composite->mask_sample_area,
1132 &info.src_x, &info.src_y);
1133 if (unlikely (info.src->status))
1134 return info.src->status;
1136 info.src_x += dst_x;
1137 info.src_y += dst_y;
1139 for (i = 0; i < clip->num_boxes; i++)
1140 do_unaligned_box(composite_box, &info, &clip->boxes[i], dst_x, dst_y);
1142 cairo_surface_destroy (info.src);
1144 return CAIRO_STATUS_SUCCESS;
1147 static cairo_int_status_t
1148 composite_mask (const cairo_mask_compositor_t *compositor,
1149 cairo_surface_t *dst,
1151 cairo_operator_t op,
1152 const cairo_pattern_t *src_pattern,
1153 const cairo_rectangle_int_t *src_sample,
1156 const cairo_rectangle_int_t *extents,
1159 cairo_composite_rectangles_t *composite = closure;
1160 cairo_surface_t *src, *mask;
1164 if (src_pattern != NULL) {
1165 src = compositor->pattern_to_surface (dst, src_pattern, FALSE,
1166 extents, src_sample,
1168 if (unlikely (src->status))
1171 mask = compositor->pattern_to_surface (dst, &composite->mask_pattern.base, TRUE,
1172 extents, &composite->mask_sample_area,
1174 if (unlikely (mask->status)) {
1175 cairo_surface_destroy (src);
1176 return mask->status;
1179 compositor->composite (dst, op, src, mask,
1180 extents->x + src_x, extents->y + src_y,
1181 extents->x + mask_x, extents->y + mask_y,
1182 extents->x - dst_x, extents->y - dst_y,
1183 extents->width, extents->height);
1185 cairo_surface_destroy (mask);
1186 cairo_surface_destroy (src);
1188 src = compositor->pattern_to_surface (dst, &composite->mask_pattern.base, FALSE,
1189 extents, &composite->mask_sample_area,
1191 if (unlikely (src->status))
1194 compositor->composite (dst, op, src, NULL,
1195 extents->x + src_x, extents->y + src_y,
1197 extents->x - dst_x, extents->y - dst_y,
1198 extents->width, extents->height);
1200 cairo_surface_destroy (src);
1203 return CAIRO_STATUS_SUCCESS;
1206 static cairo_int_status_t
1207 _cairo_mask_compositor_mask (const cairo_compositor_t *_compositor,
1208 cairo_composite_rectangles_t *extents)
1210 const cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
1211 cairo_int_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
1213 if (extents->mask_pattern.base.type == CAIRO_PATTERN_TYPE_SOLID &&
1214 extents->clip->path == NULL &&
1215 ! _cairo_clip_is_region (extents->clip)) {
1216 status = clip_and_composite (compositor,
1217 composite_opacity_boxes,
1218 composite_opacity_boxes,
1219 &extents->mask_pattern.solid,
1220 extents, need_unbounded_clip (extents));
1222 status = clip_and_composite (compositor,
1224 extents->clip->path == NULL ? composite_mask_clip_boxes : NULL,
1226 extents, need_bounded_clip (extents));
1232 static cairo_int_status_t
1233 _cairo_mask_compositor_stroke (const cairo_compositor_t *_compositor,
1234 cairo_composite_rectangles_t *extents,
1235 const cairo_path_fixed_t *path,
1236 const cairo_stroke_style_t *style,
1237 const cairo_matrix_t *ctm,
1238 const cairo_matrix_t *ctm_inverse,
1240 cairo_antialias_t antialias)
1242 const cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
1243 cairo_surface_t *mask;
1244 cairo_surface_pattern_t pattern;
1245 cairo_int_status_t status;
1247 status = CAIRO_INT_STATUS_UNSUPPORTED;
1248 if (_cairo_path_fixed_stroke_is_rectilinear (path)) {
1249 cairo_boxes_t boxes;
1251 _cairo_boxes_init_with_clip (&boxes, extents->clip);
1252 status = _cairo_path_fixed_stroke_rectilinear_to_boxes (path,
1257 if (likely (status == CAIRO_INT_STATUS_SUCCESS))
1258 status = clip_and_composite_boxes (compositor, extents, &boxes);
1259 _cairo_boxes_fini (&boxes);
1263 if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
1264 mask = cairo_surface_create_similar_image (extents->surface,
1266 extents->bounded.width,
1267 extents->bounded.height);
1268 if (unlikely (mask->status))
1269 return mask->status;
1271 status = _cairo_surface_offset_stroke (mask,
1275 &_cairo_pattern_white.base,
1276 path, style, ctm, ctm_inverse,
1277 tolerance, antialias,
1279 if (unlikely (status)) {
1280 cairo_surface_destroy (mask);
1284 _cairo_pattern_init_for_surface (&pattern, mask);
1285 cairo_surface_destroy (mask);
1287 cairo_matrix_init_translate (&pattern.base.matrix,
1288 -extents->bounded.x,
1289 -extents->bounded.y);
1290 pattern.base.filter = CAIRO_FILTER_NEAREST;
1291 pattern.base.extend = CAIRO_EXTEND_NONE;
1292 status = _cairo_surface_mask (extents->surface,
1294 &extents->source_pattern.base,
1297 _cairo_pattern_fini (&pattern.base);
1303 static cairo_int_status_t
1304 _cairo_mask_compositor_fill (const cairo_compositor_t *_compositor,
1305 cairo_composite_rectangles_t *extents,
1306 const cairo_path_fixed_t *path,
1307 cairo_fill_rule_t fill_rule,
1309 cairo_antialias_t antialias)
1311 const cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
1312 cairo_surface_t *mask;
1313 cairo_surface_pattern_t pattern;
1314 cairo_int_status_t status;
1316 status = CAIRO_INT_STATUS_UNSUPPORTED;
1317 if (_cairo_path_fixed_fill_is_rectilinear (path)) {
1318 cairo_boxes_t boxes;
1320 _cairo_boxes_init_with_clip (&boxes, extents->clip);
1321 status = _cairo_path_fixed_fill_rectilinear_to_boxes (path,
1325 if (likely (status == CAIRO_INT_STATUS_SUCCESS))
1326 status = clip_and_composite_boxes (compositor, extents, &boxes);
1327 _cairo_boxes_fini (&boxes);
1330 if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
1331 mask = cairo_surface_create_similar_image (extents->surface,
1333 extents->bounded.width,
1334 extents->bounded.height);
1335 if (unlikely (mask->status))
1336 return mask->status;
1338 status = _cairo_surface_offset_fill (mask,
1342 &_cairo_pattern_white.base,
1343 path, fill_rule, tolerance, antialias,
1345 if (unlikely (status)) {
1346 cairo_surface_destroy (mask);
1350 _cairo_pattern_init_for_surface (&pattern, mask);
1351 cairo_surface_destroy (mask);
1353 cairo_matrix_init_translate (&pattern.base.matrix,
1354 -extents->bounded.x,
1355 -extents->bounded.y);
1356 pattern.base.filter = CAIRO_FILTER_NEAREST;
1357 pattern.base.extend = CAIRO_EXTEND_NONE;
1358 status = _cairo_surface_mask (extents->surface,
1360 &extents->source_pattern.base,
1363 _cairo_pattern_fini (&pattern.base);
1369 static cairo_int_status_t
1370 _cairo_mask_compositor_glyphs (const cairo_compositor_t *_compositor,
1371 cairo_composite_rectangles_t *extents,
1372 cairo_scaled_font_t *scaled_font,
1373 cairo_glyph_t *glyphs,
1375 cairo_bool_t overlap)
1377 cairo_surface_t *mask;
1378 cairo_surface_pattern_t pattern;
1379 cairo_int_status_t status;
1381 mask = cairo_surface_create_similar_image (extents->surface,
1383 extents->bounded.width,
1384 extents->bounded.height);
1385 if (unlikely (mask->status))
1386 return mask->status;
1388 status = _cairo_surface_offset_glyphs (mask,
1392 &_cairo_pattern_white.base,
1393 scaled_font, glyphs, num_glyphs,
1395 if (unlikely (status)) {
1396 cairo_surface_destroy (mask);
1400 _cairo_pattern_init_for_surface (&pattern, mask);
1401 cairo_surface_destroy (mask);
1403 cairo_matrix_init_translate (&pattern.base.matrix,
1404 -extents->bounded.x,
1405 -extents->bounded.y);
1406 pattern.base.filter = CAIRO_FILTER_NEAREST;
1407 pattern.base.extend = CAIRO_EXTEND_NONE;
1408 status = _cairo_surface_mask (extents->surface,
1410 &extents->source_pattern.base,
1413 _cairo_pattern_fini (&pattern.base);
1419 _cairo_mask_compositor_init (cairo_mask_compositor_t *compositor,
1420 const cairo_compositor_t *delegate)
1422 compositor->base.delegate = delegate;
1424 compositor->base.paint = _cairo_mask_compositor_paint;
1425 compositor->base.mask = _cairo_mask_compositor_mask;
1426 compositor->base.fill = _cairo_mask_compositor_fill;
1427 compositor->base.stroke = _cairo_mask_compositor_stroke;
1428 compositor->base.glyphs = _cairo_mask_compositor_glyphs;