7976a79832851c7e12dcee112f65ea02ed043ab8
[framework/graphics/cairo.git] / src / cairo-mask-compositor.c
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
3  *
4  * Copyright © 2002 University of Southern California
5  * Copyright © 2005 Red Hat, Inc.
6  * Copyright © 2011 Intel Corporation
7  *
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.
15  *
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
21  *
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/
26  *
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.
30  *
31  * The Original Code is the cairo graphics library.
32  *
33  * The Initial Developer of the Original Code is University of Southern
34  * California.
35  *
36  * Contributor(s):
37  *      Carl D. Worth <cworth@cworth.org>
38  *      Joonas Pihlaja <jpihlaja@cc.helsinki.fi>
39  *      Chris Wilson <chris@chris-wilson.co.uk>
40  */
41
42 /* This compositor renders the shape to a mask using an image surface
43  * then calls composite.
44  */
45
46 #include "cairoint.h"
47
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"
57
58 typedef cairo_int_status_t
59 (*draw_func_t) (const cairo_mask_compositor_t *compositor,
60                 cairo_surface_t                 *dst,
61                 void                            *closure,
62                 cairo_operator_t                 op,
63                 const cairo_pattern_t           *src,
64                 const cairo_rectangle_int_t     *src_sample,
65                 int                              dst_x,
66                 int                              dst_y,
67                 const cairo_rectangle_int_t     *extents,
68                 cairo_clip_t                    *clip);
69
70 static void do_unaligned_row(void (*blt)(void *closure,
71                                          int16_t x, int16_t y,
72                                          int16_t w, int16_t h,
73                                          uint16_t coverage),
74                              void *closure,
75                              const cairo_box_t *b,
76                              int tx, int y, int h,
77                              uint16_t coverage)
78 {
79     int x1 = _cairo_fixed_integer_part (b->p1.x) - tx;
80     int x2 = _cairo_fixed_integer_part (b->p2.x) - tx;
81     if (x2 > x1) {
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)));
85             x1++;
86         }
87
88         if (x2 > x1)
89             blt(closure, x1, y, x2-x1, h, (coverage << 8) - (coverage >> 8));
90
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));
94     } else
95         blt(closure, x1, y, 1, h,
96             coverage * (b->p2.x - b->p1.x));
97 }
98
99 static void do_unaligned_box(void (*blt)(void *closure,
100                                          int16_t x, int16_t y,
101                                          int16_t w, int16_t h,
102                                          uint16_t coverage),
103                              void *closure,
104                              const cairo_box_t *b, int tx, int ty)
105 {
106     int y1 = _cairo_fixed_integer_part (b->p1.y) - ty;
107     int y2 = _cairo_fixed_integer_part (b->p2.y) - ty;
108     if (y2 > y1) {
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));
112             y1++;
113         }
114
115         if (y2 > y1)
116             do_unaligned_row(blt, closure, b, tx, y1, y2-y1, 256);
117
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));
121     } else
122         do_unaligned_row(blt, closure, b, tx, y1, 1,
123                          b->p2.y - b->p1.y);
124 }
125
126 struct blt_in {
127     const cairo_mask_compositor_t *compositor;
128     cairo_surface_t *dst;
129 };
130
131 static void blt_in(void *closure,
132                    int16_t x, int16_t y,
133                    int16_t w, int16_t h,
134                    uint16_t coverage)
135 {
136     struct blt_in *info = closure;
137     cairo_color_t color;
138     cairo_rectangle_int_t rect;
139
140     if (coverage == 0xffff)
141         return;
142
143     rect.x = x;
144     rect.y = y;
145     rect.width  = w;
146     rect.height = h;
147
148     _cairo_color_init_rgba (&color, 0, 0, 0, coverage / (double) 0xffff);
149     info->compositor->fill_rectangles (info->dst, CAIRO_OPERATOR_IN,
150                                        &color, &rect, 1);
151 }
152
153 static cairo_surface_t *
154 create_composite_mask (const cairo_mask_compositor_t *compositor,
155                        cairo_surface_t          *dst,
156                        void                     *draw_closure,
157                        draw_func_t               draw_func,
158                        draw_func_t               mask_func,
159                        const cairo_composite_rectangles_t *extents)
160 {
161     cairo_surface_t *surface;
162     cairo_int_status_t status;
163     struct blt_in info;
164     int i;
165
166     surface = _cairo_surface_create_similar_scratch (dst, CAIRO_CONTENT_ALPHA,
167                                                      extents->bounded.width,
168                                                      extents->bounded.height);
169     if (unlikely (surface->status))
170         return surface;
171
172     status = compositor->acquire (surface);
173     if (unlikely (status)) {
174         cairo_surface_destroy (surface);
175         return _cairo_surface_create_in_error (status);
176     }
177
178     if (!surface->is_clear) {
179         cairo_rectangle_int_t rect;
180
181         rect.x = rect.y = 0;
182         rect.width = extents->bounded.width;
183         rect.height = extents->bounded.height;
184
185         status = compositor->fill_rectangles (surface, CAIRO_OPERATOR_CLEAR,
186                                               CAIRO_COLOR_TRANSPARENT,
187                                               &rect, 1);
188         if (unlikely (status))
189             goto error;
190     }
191
192     if (mask_func) {
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))
198             goto out;
199     }
200
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))
207         goto error;
208
209     info.compositor = compositor;
210     info.dst = surface;
211     for (i = 0; i < extents->clip->num_boxes; i++) {
212         cairo_box_t *b = &extents->clip->boxes[i];
213
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))
218         {
219             do_unaligned_box(blt_in, &info, b,
220                              extents->bounded.x,
221                              extents->bounded.y);
222         }
223     }
224
225     if (extents->clip->path != NULL) {
226         status = _cairo_clip_combine_with_surface (extents->clip, surface,
227                                                    extents->bounded.x,
228                                                    extents->bounded.y);
229         if (unlikely (status))
230             goto error;
231     }
232
233 out:
234     compositor->release (surface);
235     surface->is_clear = FALSE;
236     return surface;
237
238 error:
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);
243     }
244     return surface;
245 }
246
247 /* Handles compositing with a clip surface when the operator allows
248  * us to combine the clip with the mask
249  */
250 static cairo_status_t
251 clip_and_composite_with_mask (const cairo_mask_compositor_t *compositor,
252                               void                      *draw_closure,
253                               draw_func_t                draw_func,
254                               draw_func_t                mask_func,
255                               cairo_operator_t           op,
256                               cairo_pattern_t           *pattern,
257                               const cairo_composite_rectangles_t*extents)
258 {
259     cairo_surface_t *dst = extents->surface;
260     cairo_surface_t *mask, *src;
261     int src_x, src_y;
262
263     mask = create_composite_mask (compositor, dst, draw_closure,
264                                   draw_func, mask_func,
265                                   extents);
266     if (unlikely (mask->status))
267         return mask->status;
268
269     if (pattern != NULL || dst->content != CAIRO_CONTENT_ALPHA) {
270         src = compositor->pattern_to_surface (dst,
271                                               &extents->source_pattern.base,
272                                               FALSE,
273                                               &extents->bounded,
274                                               &extents->source_sample_area,
275                                               &src_x, &src_y);
276         if (unlikely (src->status)) {
277             cairo_surface_destroy (mask);
278             return src->status;
279         }
280
281         compositor->composite (dst, op, src, mask,
282                                extents->bounded.x + src_x,
283                                extents->bounded.y + src_y,
284                                0, 0,
285                                extents->bounded.x,      extents->bounded.y,
286                                extents->bounded.width,  extents->bounded.height);
287
288         cairo_surface_destroy (src);
289     } else {
290         compositor->composite (dst, op, mask, NULL,
291                                0, 0,
292                                0, 0,
293                                extents->bounded.x,      extents->bounded.y,
294                                extents->bounded.width,  extents->bounded.height);
295     }
296     cairo_surface_destroy (mask);
297
298     return CAIRO_STATUS_SUCCESS;
299 }
300
301 /* Handles compositing with a clip surface when we have to do the operation
302  * in two pieces and combine them together.
303  */
304 static cairo_status_t
305 clip_and_composite_combine (const cairo_mask_compositor_t *compositor,
306                             void                        *draw_closure,
307                             draw_func_t          draw_func,
308                             cairo_operator_t             op,
309                             const cairo_pattern_t       *pattern,
310                             const cairo_composite_rectangles_t*extents)
311 {
312     cairo_surface_t *dst = extents->surface;
313     cairo_surface_t *tmp, *clip;
314     cairo_status_t status;
315
316     tmp = _cairo_surface_create_similar_scratch (dst, dst->content,
317                                                  extents->bounded.width,
318                                                  extents->bounded.height);
319     if (unlikely (tmp->status))
320         return tmp->status;
321
322     compositor->composite (tmp, CAIRO_OPERATOR_SOURCE, dst, NULL,
323                            extents->bounded.x,      extents->bounded.y,
324                            0, 0,
325                            0, 0,
326                            extents->bounded.width,  extents->bounded.height);
327
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))
333         goto cleanup;
334
335     clip = _cairo_clip_get_image (extents->clip, dst, &extents->bounded);
336     if (unlikely ((status = clip->status)))
337         goto cleanup;
338
339     if (dst->is_clear) {
340         compositor->composite (dst, CAIRO_OPERATOR_SOURCE, tmp, clip,
341                                0, 0,
342                                0, 0,
343                                extents->bounded.x,      extents->bounded.y,
344                                extents->bounded.width,  extents->bounded.height);
345     } else {
346         /* Punch the clip out of the destination */
347         compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, clip, NULL,
348                                0, 0,
349                                0, 0,
350                                extents->bounded.x,     extents->bounded.y,
351                                extents->bounded.width, extents->bounded.height);
352
353         /* Now add the two results together */
354         compositor->composite (dst, CAIRO_OPERATOR_ADD, tmp, clip,
355                                0, 0,
356                                0, 0,
357                                extents->bounded.x,     extents->bounded.y,
358                                extents->bounded.width, extents->bounded.height);
359     }
360     cairo_surface_destroy (clip);
361
362 cleanup:
363     cairo_surface_destroy (tmp);
364     return status;
365 }
366
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))
369  */
370 static cairo_status_t
371 clip_and_composite_source (const cairo_mask_compositor_t        *compositor,
372                            void                         *draw_closure,
373                            draw_func_t                   draw_func,
374                            draw_func_t                   mask_func,
375                            cairo_pattern_t              *pattern,
376                            const cairo_composite_rectangles_t   *extents)
377 {
378     cairo_surface_t *dst = extents->surface;
379     cairo_surface_t *mask, *src;
380     int src_x, src_y;
381
382     /* Create a surface that is mask IN clip */
383     mask = create_composite_mask (compositor, dst, draw_closure,
384                                   draw_func, mask_func,
385                                   extents);
386     if (unlikely (mask->status))
387         return mask->status;
388
389     src = compositor->pattern_to_surface (dst,
390                                           pattern,
391                                           FALSE,
392                                           &extents->bounded,
393                                           &extents->source_sample_area,
394                                           &src_x, &src_y);
395     if (unlikely (src->status)) {
396         cairo_surface_destroy (mask);
397         return src->status;
398     }
399
400     if (dst->is_clear) {
401         compositor->composite (dst, CAIRO_OPERATOR_SOURCE, src, mask,
402                                extents->bounded.x + src_x, extents->bounded.y + src_y,
403                                0, 0,
404                                extents->bounded.x,      extents->bounded.y,
405                                extents->bounded.width,  extents->bounded.height);
406     } else {
407         /* Compute dest' = dest OUT (mask IN clip) */
408         compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
409                                0, 0, 0, 0,
410                                extents->bounded.x,     extents->bounded.y,
411                                extents->bounded.width, extents->bounded.height);
412
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,
416                                0, 0,
417                                extents->bounded.x,     extents->bounded.y,
418                                extents->bounded.width, extents->bounded.height);
419     }
420
421     cairo_surface_destroy (src);
422     cairo_surface_destroy (mask);
423
424     return CAIRO_STATUS_SUCCESS;
425 }
426
427 static cairo_bool_t
428 can_reduce_alpha_op (cairo_operator_t op)
429 {
430     int iop = op;
431     switch (iop) {
432     case CAIRO_OPERATOR_OVER:
433     case CAIRO_OPERATOR_SOURCE:
434     case CAIRO_OPERATOR_ADD:
435         return TRUE;
436     default:
437         return FALSE;
438     }
439 }
440
441 static cairo_bool_t
442 reduce_alpha_op (cairo_surface_t *dst,
443                  cairo_operator_t op,
444                  const cairo_pattern_t *pattern)
445 {
446     return dst->is_clear &&
447            dst->content == CAIRO_CONTENT_ALPHA &&
448            _cairo_pattern_is_opaque_solid (pattern) &&
449            can_reduce_alpha_op (op);
450 }
451
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)
456 {
457     cairo_rectangle_int_t rects[4];
458     int n;
459
460     if (extents->bounded.width  == extents->unbounded.width &&
461         extents->bounded.height == extents->unbounded.height)
462     {
463         return CAIRO_STATUS_SUCCESS;
464     }
465
466     n = 0;
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;
472         n++;
473     } else {
474         /* top */
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;
480             n++;
481         }
482         /* left */
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;
488             n++;
489         }
490         /* right */
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;
496             n++;
497         }
498         /* bottom */
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;
504             n++;
505         }
506     }
507
508     return compositor->fill_rectangles (dst, CAIRO_OPERATOR_CLEAR,
509                                         CAIRO_COLOR_TRANSPARENT,
510                                         rects, n);
511 }
512
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)
517 {
518     cairo_clip_t *clip = extents->clip;
519     cairo_surface_t *mask;
520     int mask_x, mask_y;
521
522     mask = _cairo_clip_get_image (clip, dst, &extents->unbounded);
523     if (unlikely (mask->status))
524         return mask->status;
525
526     mask_x = -extents->unbounded.x;
527     mask_y = -extents->unbounded.y;
528
529     /* top */
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;
535
536         compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
537                                x + mask_x, y + mask_y,
538                                0, 0,
539                                x, y,
540                                width, height);
541     }
542
543     /* left */
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;
549
550         compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
551                                x + mask_x, y + mask_y,
552                                0, 0,
553                                x, y,
554                                width, height);
555     }
556
557     /* right */
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;
563
564         compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
565                                x + mask_x, y + mask_y,
566                                0, 0,
567                                x, y,
568                                width, height);
569     }
570
571     /* bottom */
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;
577
578         compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
579                                x + mask_x, y + mask_y,
580                                0, 0,
581                                x, y,
582                                width, height);
583     }
584
585     cairo_surface_destroy (mask);
586
587     return CAIRO_STATUS_SUCCESS;
588 }
589
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)
594 {
595     cairo_surface_t *dst = extents->surface;
596     cairo_boxes_t clear;
597     cairo_region_t *clip_region;
598     cairo_box_t box;
599     cairo_status_t status;
600     struct _cairo_boxes_chunk *chunk;
601     int i;
602
603     assert (boxes->is_pixel_aligned);
604
605     clip_region = NULL;
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)
610         clip_region = NULL;
611
612
613     if (boxes->num_boxes <= 1 && clip_region == NULL)
614         return fixup_unbounded (compositor, dst, extents);
615
616     _cairo_boxes_init (&clear);
617
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);
622
623     if (clip_region == NULL) {
624         cairo_boxes_t tmp;
625
626         _cairo_boxes_init (&tmp);
627
628         status = _cairo_boxes_add (&tmp, CAIRO_ANTIALIAS_DEFAULT, &box);
629         assert (status == CAIRO_STATUS_SUCCESS);
630
631         tmp.chunks.next = &boxes->chunks;
632         tmp.num_boxes += boxes->num_boxes;
633
634         status = _cairo_bentley_ottmann_tessellate_boxes (&tmp,
635                                                           CAIRO_FILL_RULE_WINDING,
636                                                           &clear);
637
638         tmp.chunks.next = NULL;
639     } else {
640         pixman_box32_t *pbox;
641
642         pbox = pixman_region32_rectangles (&clip_region->rgn, &i);
643         _cairo_boxes_limit (&clear, (cairo_box_t *) pbox, i);
644
645         status = _cairo_boxes_add (&clear, CAIRO_ANTIALIAS_DEFAULT, &box);
646         assert (status == CAIRO_STATUS_SUCCESS);
647
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,
652                                            &chunk->base[i]);
653                 if (unlikely (status)) {
654                     _cairo_boxes_fini (&clear);
655                     return status;
656                 }
657             }
658         }
659
660         status = _cairo_bentley_ottmann_tessellate_boxes (&clear,
661                                                           CAIRO_FILL_RULE_WINDING,
662                                                           &clear);
663     }
664
665     if (likely (status == CAIRO_STATUS_SUCCESS)) {
666         status = compositor->fill_boxes (dst,
667                                          CAIRO_OPERATOR_CLEAR,
668                                          CAIRO_COLOR_TRANSPARENT,
669                                          &clear);
670     }
671
672     _cairo_boxes_fini (&clear);
673
674     return status;
675 }
676
677 enum {
678     NEED_CLIP_REGION = 0x1,
679     NEED_CLIP_SURFACE = 0x2,
680     FORCE_CLIP_REGION = 0x4,
681 };
682
683 static cairo_bool_t
684 need_bounded_clip (cairo_composite_rectangles_t *extents)
685 {
686     unsigned int flags = NEED_CLIP_REGION;
687     if (! _cairo_clip_is_region (extents->clip))
688         flags |= NEED_CLIP_SURFACE;
689     return flags;
690 }
691
692 static cairo_bool_t
693 need_unbounded_clip (cairo_composite_rectangles_t *extents)
694 {
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;
700     }
701     if (extents->clip->path != NULL)
702         flags |= NEED_CLIP_SURFACE;
703     return flags;
704 }
705
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,
710                     void                        *draw_closure,
711                     cairo_composite_rectangles_t*extents,
712                     unsigned int need_clip)
713 {
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;
719
720     compositor->acquire (dst);
721
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,
726                                                          extents->clip))
727             clip_region = NULL;
728         if (clip_region != NULL) {
729             status = compositor->set_clip_region (dst, clip_region);
730             if (unlikely (status)) {
731                 compositor->release (dst);
732                 return status;
733             }
734         }
735     }
736
737     if (reduce_alpha_op (dst, op, &extents->source_pattern.base)) {
738         op = CAIRO_OPERATOR_ADD;
739         src = NULL;
740     }
741
742     if (op == CAIRO_OPERATOR_SOURCE) {
743         status = clip_and_composite_source (compositor,
744                                             draw_closure, draw_func, mask_func,
745                                             src, extents);
746     } else {
747         if (op == CAIRO_OPERATOR_CLEAR) {
748             op = CAIRO_OPERATOR_DEST_OUT;
749             src = NULL;
750         }
751
752         if (need_clip & NEED_CLIP_SURFACE) {
753             if (extents->is_bounded) {
754                 status = clip_and_composite_with_mask (compositor,
755                                                        draw_closure,
756                                                        draw_func,
757                                                        mask_func,
758                                                        op, src, extents);
759             } else {
760                 status = clip_and_composite_combine (compositor,
761                                                      draw_closure,
762                                                      draw_func,
763                                                      op, src, extents);
764             }
765         } else {
766             status = draw_func (compositor,
767                                 dst, draw_closure,
768                                 op, src, &extents->source_sample_area,
769                                 0, 0,
770                                 &extents->bounded,
771                                 extents->clip);
772         }
773     }
774
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);
778         else
779             status = fixup_unbounded (compositor, dst, extents);
780     }
781
782     if (clip_region)
783         compositor->set_clip_region (dst, NULL);
784
785     compositor->release (dst);
786
787     return status;
788 }
789
790 static cairo_int_status_t
791 trim_extents_to_boxes (cairo_composite_rectangles_t *extents,
792                        cairo_boxes_t *boxes)
793 {
794     cairo_box_t box;
795
796     _cairo_boxes_extents (boxes, &box);
797     return _cairo_composite_rectangles_intersect_mask_extents (extents, &box);
798 }
799
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)
804 {
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;
810     int tx, ty;
811
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;
815
816     if (! _cairo_matrix_is_integer_translation (&source->matrix, &tx, &ty))
817         return CAIRO_INT_STATUS_UNSUPPORTED;
818
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;
822
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;
826
827     tx += limit.x;
828     ty += limit.y;
829
830     if (src->type == CAIRO_SURFACE_TYPE_IMAGE)
831         status = compositor->draw_image_boxes (dst,
832                                                (cairo_image_surface_t *)src,
833                                                boxes, tx, ty);
834     else
835         status = compositor->copy_boxes (dst, src, boxes, &extents->bounded,
836                                          tx, ty);
837
838     return status;
839 }
840
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)
845 {
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;
851
852     if (need_clip_mask &&
853         (! extents->is_bounded || op == CAIRO_OPERATOR_SOURCE))
854     {
855         return CAIRO_INT_STATUS_UNSUPPORTED;
856     }
857
858     status = compositor->acquire (dst);
859     if (unlikely (status))
860         return status;
861
862     if (! need_clip_mask && source->type == CAIRO_PATTERN_TYPE_SOLID) {
863         const cairo_color_t *color;
864
865         color = &((cairo_solid_pattern_t *) source)->color;
866         status = compositor->fill_boxes (dst, op, color, boxes);
867     } else {
868         cairo_surface_t *src, *mask = NULL;
869         int src_x, src_y;
870         int mask_x = 0, mask_y = 0;
871
872         if (need_clip_mask) {
873             mask = _cairo_clip_get_image (extents->clip, dst,
874                                           &extents->bounded);
875             if (unlikely (mask->status))
876                 return mask->status;
877
878             if (op == CAIRO_OPERATOR_CLEAR) {
879                 source = NULL;
880                 op = CAIRO_OPERATOR_DEST_OUT;
881             }
882
883             mask_x = -extents->bounded.x;
884             mask_y = -extents->bounded.y;
885         }
886
887         if (source || mask == NULL) {
888             src = compositor->pattern_to_surface (dst, source, FALSE,
889                                                   &extents->bounded,
890                                                   &extents->source_sample_area,
891                                                   &src_x, &src_y);
892         } else {
893             src = mask;
894             src_x = mask_x;
895             src_y = mask_y;
896             mask = NULL;
897         }
898
899         status = compositor->composite_boxes (dst, op, src, mask,
900                                               src_x, src_y,
901                                               mask_x, mask_y,
902                                               0, 0,
903                                               boxes, &extents->bounded);
904
905         cairo_surface_destroy (src);
906         cairo_surface_destroy (mask);
907     }
908
909     if (status == CAIRO_STATUS_SUCCESS && ! extents->is_bounded)
910         status = fixup_unbounded_boxes (compositor, extents, boxes);
911
912     compositor->release (dst);
913
914     return status;
915 }
916
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)
921 {
922     cairo_surface_t *dst = extents->surface;
923     cairo_int_status_t status;
924
925     if (boxes->num_boxes == 0) {
926         if (extents->is_bounded)
927             return CAIRO_STATUS_SUCCESS;
928
929         return fixup_unbounded_boxes (compositor, extents, boxes);
930     }
931
932     if (! boxes->is_pixel_aligned)
933         return CAIRO_INT_STATUS_UNSUPPORTED;
934
935     status = trim_extents_to_boxes (extents, boxes);
936     if (unlikely (status))
937         return status;
938
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))))
944     {
945         status = upload_boxes (compositor, extents, boxes);
946         if (status != CAIRO_INT_STATUS_UNSUPPORTED)
947             return status;
948     }
949
950     return composite_boxes (compositor, extents, boxes);
951 }
952
953 /* high-level compositor interface */
954
955 static cairo_int_status_t
956 _cairo_mask_compositor_paint (const cairo_compositor_t *_compositor,
957                               cairo_composite_rectangles_t *extents)
958 {
959     cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
960     cairo_boxes_t boxes;
961     cairo_int_status_t status;
962
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);
966
967     return status;
968 }
969
970 struct composite_opacity_info {
971     const cairo_mask_compositor_t *compositor;
972     uint8_t op;
973     cairo_surface_t *dst;
974     cairo_surface_t *src;
975     int src_x, src_y;
976     double opacity;
977 };
978
979 static void composite_opacity(void *closure,
980                               int16_t x, int16_t y,
981                               int16_t w, int16_t h,
982                               uint16_t coverage)
983 {
984     struct composite_opacity_info *info = closure;
985     const cairo_mask_compositor_t *compositor = info->compositor;
986     cairo_surface_t *mask;
987     int mask_x, mask_y;
988     cairo_color_t color;
989     cairo_solid_pattern_t solid;
990
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,
996                                            &mask_x, &mask_y);
997     if (likely (mask->status == CAIRO_STATUS_SUCCESS)) {
998         if (info->src) {
999             compositor->composite (info->dst, info->op, info->src, mask,
1000                                    x + info->src_x,  y + info->src_y,
1001                                    mask_x,           mask_y,
1002                                    x,                y,
1003                                    w,                h);
1004         } else {
1005             compositor->composite (info->dst, info->op, mask, NULL,
1006                                    mask_x,            mask_y,
1007                                    0,                 0,
1008                                    x,                 y,
1009                                    w,                 h);
1010         }
1011     }
1012
1013     cairo_surface_destroy (mask);
1014 }
1015
1016 static cairo_int_status_t
1017 composite_opacity_boxes (const cairo_mask_compositor_t *compositor,
1018                          cairo_surface_t                *dst,
1019                          void                           *closure,
1020                          cairo_operator_t                op,
1021                          const cairo_pattern_t          *src_pattern,
1022                          const cairo_rectangle_int_t    *src_sample,
1023                          int                             dst_x,
1024                          int                             dst_y,
1025                          const cairo_rectangle_int_t    *extents,
1026                          cairo_clip_t                   *clip)
1027 {
1028     const cairo_solid_pattern_t *mask_pattern = closure;
1029     struct composite_opacity_info info;
1030     int i;
1031
1032     assert (clip);
1033
1034     info.compositor = compositor;
1035     info.op = op;
1036     info.dst = dst;
1037
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;
1044     } else
1045         info.src = NULL;
1046
1047     info.opacity = mask_pattern->color.alpha / (double) 0xffff;
1048
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);
1054
1055     return CAIRO_STATUS_SUCCESS;
1056 }
1057
1058 struct composite_box_info {
1059     const cairo_mask_compositor_t *compositor;
1060     cairo_surface_t *dst;
1061     cairo_surface_t *src;
1062     int src_x, src_y;
1063     uint8_t op;
1064 };
1065
1066 static void composite_box(void *closure,
1067                           int16_t x, int16_t y,
1068                           int16_t w, int16_t h,
1069                           uint16_t coverage)
1070 {
1071     struct composite_box_info *info = closure;
1072     const cairo_mask_compositor_t *compositor = info->compositor;
1073
1074     if (! CAIRO_ALPHA_SHORT_IS_OPAQUE (coverage)) {
1075         cairo_surface_t *mask;
1076         cairo_color_t color;
1077         cairo_solid_pattern_t solid;
1078         int mask_x, mask_y;
1079
1080         _cairo_color_init_rgba (&color, 0, 0, 0, coverage / (double)0xffff);
1081         _cairo_pattern_init_solid (&solid, &color);
1082
1083         mask = compositor->pattern_to_surface (info->dst, &solid.base, FALSE,
1084                                                &_cairo_unbounded_rectangle,
1085                                                &_cairo_unbounded_rectangle,
1086                                                &mask_x, &mask_y);
1087
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,
1091                                    mask_x,           mask_y,
1092                                    x,                y,
1093                                    w,                h);
1094         }
1095
1096         cairo_surface_destroy (mask);
1097     } else {
1098         compositor->composite (info->dst, info->op, info->src, NULL,
1099                                x + info->src_x,  y + info->src_y,
1100                                0,                0,
1101                                x,                y,
1102                                w,                h);
1103     }
1104 }
1105
1106 static cairo_int_status_t
1107 composite_mask_clip_boxes (const cairo_mask_compositor_t *compositor,
1108                            cairo_surface_t              *dst,
1109                            void                         *closure,
1110                            cairo_operator_t              op,
1111                            const cairo_pattern_t        *src_pattern,
1112                            const cairo_rectangle_int_t  *src_sample,
1113                            int                           dst_x,
1114                            int                           dst_y,
1115                            const cairo_rectangle_int_t  *extents,
1116                            cairo_clip_t                 *clip)
1117 {
1118     cairo_composite_rectangles_t *composite = closure;
1119     struct composite_box_info info;
1120     int i;
1121
1122     assert (src_pattern == NULL);
1123     assert (op == CAIRO_OPERATOR_SOURCE);
1124
1125     info.compositor = compositor;
1126     info.op = CAIRO_OPERATOR_SOURCE;
1127     info.dst = dst;
1128     info.src = compositor->pattern_to_surface (dst,
1129                                                &composite->mask_pattern.base,
1130                                                FALSE, extents,
1131                                                &composite->mask_sample_area,
1132                                                &info.src_x, &info.src_y);
1133     if (unlikely (info.src->status))
1134         return info.src->status;
1135
1136     info.src_x += dst_x;
1137     info.src_y += dst_y;
1138
1139     for (i = 0; i < clip->num_boxes; i++)
1140         do_unaligned_box(composite_box, &info, &clip->boxes[i], dst_x, dst_y);
1141
1142     cairo_surface_destroy (info.src);
1143
1144     return CAIRO_STATUS_SUCCESS;
1145 }
1146
1147 static cairo_int_status_t
1148 composite_mask (const cairo_mask_compositor_t *compositor,
1149                 cairo_surface_t                 *dst,
1150                 void                            *closure,
1151                 cairo_operator_t                 op,
1152                 const cairo_pattern_t           *src_pattern,
1153                 const cairo_rectangle_int_t     *src_sample,
1154                 int                              dst_x,
1155                 int                              dst_y,
1156                 const cairo_rectangle_int_t     *extents,
1157                 cairo_clip_t                    *clip)
1158 {
1159     cairo_composite_rectangles_t *composite = closure;
1160     cairo_surface_t *src, *mask;
1161     int src_x, src_y;
1162     int mask_x, mask_y;
1163
1164     if (src_pattern != NULL) {
1165         src = compositor->pattern_to_surface (dst, src_pattern, FALSE,
1166                                               extents, src_sample,
1167                                               &src_x, &src_y);
1168         if (unlikely (src->status))
1169             return src->status;
1170
1171         mask = compositor->pattern_to_surface (dst, &composite->mask_pattern.base, TRUE,
1172                                                extents, &composite->mask_sample_area,
1173                                                &mask_x, &mask_y);
1174         if (unlikely (mask->status)) {
1175             cairo_surface_destroy (src);
1176             return mask->status;
1177         }
1178
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);
1184
1185         cairo_surface_destroy (mask);
1186         cairo_surface_destroy (src);
1187     } else {
1188         src = compositor->pattern_to_surface (dst, &composite->mask_pattern.base, FALSE,
1189                                               extents, &composite->mask_sample_area,
1190                                               &src_x, &src_y);
1191         if (unlikely (src->status))
1192             return src->status;
1193
1194         compositor->composite (dst, op, src, NULL,
1195                                extents->x + src_x,  extents->y + src_y,
1196                                0, 0,
1197                                extents->x - dst_x,  extents->y - dst_y,
1198                                extents->width,      extents->height);
1199
1200         cairo_surface_destroy (src);
1201     }
1202
1203     return CAIRO_STATUS_SUCCESS;
1204 }
1205
1206 static cairo_int_status_t
1207 _cairo_mask_compositor_mask (const cairo_compositor_t *_compositor,
1208                              cairo_composite_rectangles_t *extents)
1209 {
1210     const cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
1211     cairo_int_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
1212
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));
1221     } else {
1222         status = clip_and_composite (compositor,
1223                                      composite_mask,
1224                                      extents->clip->path == NULL ? composite_mask_clip_boxes : NULL,
1225                                      extents,
1226                                      extents, need_bounded_clip (extents));
1227     }
1228
1229     return status;
1230 }
1231
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,
1239                                double            tolerance,
1240                                cairo_antialias_t         antialias)
1241 {
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;
1246
1247     status = CAIRO_INT_STATUS_UNSUPPORTED;
1248     if (_cairo_path_fixed_stroke_is_rectilinear (path)) {
1249         cairo_boxes_t boxes;
1250
1251         _cairo_boxes_init_with_clip (&boxes, extents->clip);
1252         status = _cairo_path_fixed_stroke_rectilinear_to_boxes (path,
1253                                                                 style,
1254                                                                 ctm,
1255                                                                 antialias,
1256                                                                 &boxes);
1257         if (likely (status == CAIRO_INT_STATUS_SUCCESS))
1258             status = clip_and_composite_boxes (compositor, extents, &boxes);
1259         _cairo_boxes_fini (&boxes);
1260     }
1261
1262
1263     if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
1264         mask = cairo_surface_create_similar_image (extents->surface,
1265                                                    CAIRO_FORMAT_A8,
1266                                                    extents->bounded.width,
1267                                                    extents->bounded.height);
1268         if (unlikely (mask->status))
1269             return mask->status;
1270
1271         status = _cairo_surface_offset_stroke (mask,
1272                                                extents->bounded.x,
1273                                                extents->bounded.y,
1274                                                CAIRO_OPERATOR_ADD,
1275                                                &_cairo_pattern_white.base,
1276                                                path, style, ctm, ctm_inverse,
1277                                                tolerance, antialias,
1278                                                extents->clip);
1279         if (unlikely (status)) {
1280             cairo_surface_destroy (mask);
1281             return status;
1282         }
1283
1284         _cairo_pattern_init_for_surface (&pattern, mask);
1285         cairo_surface_destroy (mask);
1286
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,
1293                                       extents->op,
1294                                       &extents->source_pattern.base,
1295                                       &pattern.base,
1296                                       extents->clip);
1297         _cairo_pattern_fini (&pattern.base);
1298     }
1299
1300     return status;
1301 }
1302
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,
1308                              double                      tolerance,
1309                              cairo_antialias_t   antialias)
1310 {
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;
1315
1316     status = CAIRO_INT_STATUS_UNSUPPORTED;
1317     if (_cairo_path_fixed_fill_is_rectilinear (path)) {
1318         cairo_boxes_t boxes;
1319
1320         _cairo_boxes_init_with_clip (&boxes, extents->clip);
1321         status = _cairo_path_fixed_fill_rectilinear_to_boxes (path,
1322                                                               fill_rule,
1323                                                               antialias,
1324                                                               &boxes);
1325         if (likely (status == CAIRO_INT_STATUS_SUCCESS))
1326             status = clip_and_composite_boxes (compositor, extents, &boxes);
1327         _cairo_boxes_fini (&boxes);
1328     }
1329
1330     if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
1331         mask = cairo_surface_create_similar_image (extents->surface,
1332                                                    CAIRO_FORMAT_A8,
1333                                                    extents->bounded.width,
1334                                                    extents->bounded.height);
1335         if (unlikely (mask->status))
1336             return mask->status;
1337
1338         status = _cairo_surface_offset_fill (mask,
1339                                              extents->bounded.x,
1340                                              extents->bounded.y,
1341                                              CAIRO_OPERATOR_ADD,
1342                                              &_cairo_pattern_white.base,
1343                                              path, fill_rule, tolerance, antialias,
1344                                              extents->clip);
1345         if (unlikely (status)) {
1346             cairo_surface_destroy (mask);
1347             return status;
1348         }
1349
1350         _cairo_pattern_init_for_surface (&pattern, mask);
1351         cairo_surface_destroy (mask);
1352
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,
1359                                       extents->op,
1360                                       &extents->source_pattern.base,
1361                                       &pattern.base,
1362                                       extents->clip);
1363         _cairo_pattern_fini (&pattern.base);
1364     }
1365
1366     return status;
1367 }
1368
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,
1374                                int                       num_glyphs,
1375                                cairo_bool_t              overlap)
1376 {
1377     cairo_surface_t *mask;
1378     cairo_surface_pattern_t pattern;
1379     cairo_int_status_t status;
1380
1381     mask = cairo_surface_create_similar_image (extents->surface,
1382                                                CAIRO_FORMAT_A8,
1383                                                extents->bounded.width,
1384                                                extents->bounded.height);
1385     if (unlikely (mask->status))
1386         return mask->status;
1387
1388     status = _cairo_surface_offset_glyphs (mask,
1389                                            extents->bounded.x,
1390                                            extents->bounded.y,
1391                                            CAIRO_OPERATOR_ADD,
1392                                            &_cairo_pattern_white.base,
1393                                            scaled_font, glyphs, num_glyphs,
1394                                            extents->clip);
1395     if (unlikely (status)) {
1396         cairo_surface_destroy (mask);
1397         return status;
1398     }
1399
1400     _cairo_pattern_init_for_surface (&pattern, mask);
1401     cairo_surface_destroy (mask);
1402
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,
1409                                   extents->op,
1410                                   &extents->source_pattern.base,
1411                                   &pattern.base,
1412                                   extents->clip);
1413     _cairo_pattern_fini (&pattern.base);
1414
1415     return status;
1416 }
1417
1418 void
1419 _cairo_mask_compositor_init (cairo_mask_compositor_t *compositor,
1420                              const cairo_compositor_t *delegate)
1421 {
1422     compositor->base.delegate = delegate;
1423
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;
1429 }