tizen 2.3.1 release
[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_int_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_int_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 static cairo_surface_t *
302 get_clip_source (const cairo_mask_compositor_t *compositor,
303                  cairo_clip_t *clip,
304                  cairo_surface_t *dst,
305                  const cairo_rectangle_int_t *bounds,
306                  int *out_x, int *out_y)
307 {
308     cairo_surface_pattern_t pattern;
309     cairo_rectangle_int_t r;
310     cairo_surface_t *surface;
311
312     surface = _cairo_clip_get_image (clip, dst, bounds);
313     if (unlikely (surface->status))
314         return surface;
315
316     _cairo_pattern_init_for_surface (&pattern, surface);
317     pattern.base.filter = CAIRO_FILTER_NEAREST;
318     cairo_surface_destroy (surface);
319
320     r.x = r.y = 0;
321     r.width  = bounds->width;
322     r.height = bounds->height;
323
324     surface = compositor->pattern_to_surface (dst, &pattern.base, TRUE,
325                                               &r, &r, out_x, out_y);
326     _cairo_pattern_fini (&pattern.base);
327
328     *out_x += -bounds->x;
329     *out_y += -bounds->y;
330     return surface;
331 }
332
333 /* Handles compositing with a clip surface when we have to do the operation
334  * in two pieces and combine them together.
335  */
336 static cairo_status_t
337 clip_and_composite_combine (const cairo_mask_compositor_t *compositor,
338                             void                        *draw_closure,
339                             draw_func_t          draw_func,
340                             cairo_operator_t             op,
341                             const cairo_pattern_t       *pattern,
342                             const cairo_composite_rectangles_t*extents)
343 {
344     cairo_surface_t *dst = extents->surface;
345     cairo_surface_t *tmp, *clip;
346     cairo_status_t status;
347     int clip_x, clip_y;
348
349     tmp = _cairo_surface_create_similar_scratch (dst, dst->content,
350                                                  extents->bounded.width,
351                                                  extents->bounded.height);
352     if (unlikely (tmp->status)) {
353         status = tmp->status;
354         goto cleanup;
355     }
356
357     compositor->composite (tmp, CAIRO_OPERATOR_SOURCE, dst, NULL,
358                            extents->bounded.x,      extents->bounded.y,
359                            0, 0,
360                            0, 0,
361                            extents->bounded.width,  extents->bounded.height);
362
363     status = draw_func (compositor, tmp, draw_closure, op,
364                         pattern, &extents->source_sample_area,
365                         extents->bounded.x, extents->bounded.y,
366                         &extents->bounded, NULL);
367     if (unlikely (status))
368         goto cleanup;
369
370     clip = get_clip_source (compositor,
371                             extents->clip, dst, &extents->bounded,
372                             &clip_x, &clip_y);
373     if (unlikely ((status = clip->status))) {
374         cairo_surface_destroy (clip);
375         goto cleanup;
376     }
377
378     if (dst->is_clear) {
379         compositor->composite (dst, CAIRO_OPERATOR_SOURCE, tmp, clip,
380                                0, 0,
381                                clip_x, clip_y,
382                                extents->bounded.x,      extents->bounded.y,
383                                extents->bounded.width,  extents->bounded.height);
384     } else {
385         /* Punch the clip out of the destination */
386         compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, clip, NULL,
387                                clip_x, clip_y,
388                                0, 0,
389                                extents->bounded.x,     extents->bounded.y,
390                                extents->bounded.width, extents->bounded.height);
391
392         /* Now add the two results together */
393         compositor->composite (dst, CAIRO_OPERATOR_ADD, tmp, clip,
394                                0, 0,
395                                clip_x, clip_y,
396                                extents->bounded.x,     extents->bounded.y,
397                                extents->bounded.width, extents->bounded.height);
398     }
399     cairo_surface_destroy (clip);
400
401 cleanup:
402     cairo_surface_destroy (tmp);
403     return status;
404 }
405
406 /* Handles compositing for %CAIRO_OPERATOR_SOURCE, which is special; it's
407  * defined as (src IN mask IN clip) ADD (dst OUT (mask IN clip))
408  */
409 static cairo_status_t
410 clip_and_composite_source (const cairo_mask_compositor_t        *compositor,
411                            void                         *draw_closure,
412                            draw_func_t                   draw_func,
413                            draw_func_t                   mask_func,
414                            cairo_pattern_t              *pattern,
415                            const cairo_composite_rectangles_t   *extents)
416 {
417     cairo_surface_t *dst = extents->surface;
418     cairo_surface_t *mask, *src;
419     int src_x, src_y;
420
421     /* Create a surface that is mask IN clip */
422     mask = create_composite_mask (compositor, dst, draw_closure,
423                                   draw_func, mask_func,
424                                   extents);
425     if (unlikely (mask->status))
426         return mask->status;
427
428     src = compositor->pattern_to_surface (dst,
429                                           pattern,
430                                           FALSE,
431                                           &extents->bounded,
432                                           &extents->source_sample_area,
433                                           &src_x, &src_y);
434     if (unlikely (src->status)) {
435         cairo_surface_destroy (mask);
436         return src->status;
437     }
438
439     if (dst->is_clear) {
440         compositor->composite (dst, CAIRO_OPERATOR_SOURCE, src, mask,
441                                extents->bounded.x + src_x, extents->bounded.y + src_y,
442                                0, 0,
443                                extents->bounded.x,      extents->bounded.y,
444                                extents->bounded.width,  extents->bounded.height);
445     } else {
446         /* Compute dest' = dest OUT (mask IN clip) */
447         compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
448                                0, 0, 0, 0,
449                                extents->bounded.x,     extents->bounded.y,
450                                extents->bounded.width, extents->bounded.height);
451
452         /* Now compute (src IN (mask IN clip)) ADD dest' */
453         compositor->composite (dst, CAIRO_OPERATOR_ADD, src, mask,
454                                extents->bounded.x + src_x, extents->bounded.y + src_y,
455                                0, 0,
456                                extents->bounded.x,     extents->bounded.y,
457                                extents->bounded.width, extents->bounded.height);
458     }
459
460     cairo_surface_destroy (src);
461     cairo_surface_destroy (mask);
462
463     return CAIRO_STATUS_SUCCESS;
464 }
465
466 static cairo_bool_t
467 can_reduce_alpha_op (cairo_operator_t op)
468 {
469     int iop = op;
470     switch (iop) {
471     case CAIRO_OPERATOR_OVER:
472     case CAIRO_OPERATOR_SOURCE:
473     case CAIRO_OPERATOR_ADD:
474         return TRUE;
475     default:
476         return FALSE;
477     }
478 }
479
480 static cairo_bool_t
481 reduce_alpha_op (cairo_surface_t *dst,
482                  cairo_operator_t op,
483                  const cairo_pattern_t *pattern)
484 {
485     return dst->is_clear &&
486            dst->content == CAIRO_CONTENT_ALPHA &&
487            _cairo_pattern_is_opaque_solid (pattern) &&
488            can_reduce_alpha_op (op);
489 }
490
491 static cairo_status_t
492 fixup_unbounded (const cairo_mask_compositor_t *compositor,
493                  cairo_surface_t *dst,
494                  const cairo_composite_rectangles_t *extents)
495 {
496     cairo_rectangle_int_t rects[4];
497     int n;
498
499     if (extents->bounded.width  == extents->unbounded.width &&
500         extents->bounded.height == extents->unbounded.height)
501     {
502         return CAIRO_STATUS_SUCCESS;
503     }
504
505     n = 0;
506     if (extents->bounded.width == 0 || extents->bounded.height == 0) {
507         rects[n].x = extents->unbounded.x;
508         rects[n].width = extents->unbounded.width;
509         rects[n].y = extents->unbounded.y;
510         rects[n].height = extents->unbounded.height;
511         n++;
512     } else {
513         /* top */
514         if (extents->bounded.y != extents->unbounded.y) {
515             rects[n].x = extents->unbounded.x;
516             rects[n].width = extents->unbounded.width;
517             rects[n].y = extents->unbounded.y;
518             rects[n].height = extents->bounded.y - extents->unbounded.y;
519             n++;
520         }
521         /* left */
522         if (extents->bounded.x != extents->unbounded.x) {
523             rects[n].x = extents->unbounded.x;
524             rects[n].width = extents->bounded.x - extents->unbounded.x;
525             rects[n].y = extents->bounded.y;
526             rects[n].height = extents->bounded.height;
527             n++;
528         }
529         /* right */
530         if (extents->bounded.x + extents->bounded.width != extents->unbounded.x + extents->unbounded.width) {
531             rects[n].x = extents->bounded.x + extents->bounded.width;
532             rects[n].width = extents->unbounded.x + extents->unbounded.width - rects[n].x;
533             rects[n].y = extents->bounded.y;
534             rects[n].height = extents->bounded.height;
535             n++;
536         }
537         /* bottom */
538         if (extents->bounded.y + extents->bounded.height != extents->unbounded.y + extents->unbounded.height) {
539             rects[n].x = extents->unbounded.x;
540             rects[n].width = extents->unbounded.width;
541             rects[n].y = extents->bounded.y + extents->bounded.height;
542             rects[n].height = extents->unbounded.y + extents->unbounded.height - rects[n].y;
543             n++;
544         }
545     }
546
547     return compositor->fill_rectangles (dst, CAIRO_OPERATOR_CLEAR,
548                                         CAIRO_COLOR_TRANSPARENT,
549                                         rects, n);
550 }
551
552 static cairo_status_t
553 fixup_unbounded_with_mask (const cairo_mask_compositor_t *compositor,
554                            cairo_surface_t *dst,
555                            const cairo_composite_rectangles_t *extents)
556 {
557     cairo_surface_t *mask;
558     int mask_x, mask_y;
559
560     mask = get_clip_source (compositor,
561                             extents->clip, dst, &extents->unbounded,
562                             &mask_x, &mask_y);
563     if (unlikely (mask->status)) {
564         cairo_status_t status = mask->status;
565         cairo_surface_destroy (mask);
566         return status;
567     }
568
569     /* top */
570     if (extents->bounded.y != extents->unbounded.y) {
571         int x = extents->unbounded.x;
572         int y = extents->unbounded.y;
573         int width = extents->unbounded.width;
574         int height = extents->bounded.y - y;
575
576         compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
577                                x + mask_x, y + mask_y,
578                                0, 0,
579                                x, y,
580                                width, height);
581     }
582
583     /* left */
584     if (extents->bounded.x != extents->unbounded.x) {
585         int x = extents->unbounded.x;
586         int y = extents->bounded.y;
587         int width = extents->bounded.x - x;
588         int height = extents->bounded.height;
589
590         compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
591                                x + mask_x, y + mask_y,
592                                0, 0,
593                                x, y,
594                                width, height);
595     }
596
597     /* right */
598     if (extents->bounded.x + extents->bounded.width != extents->unbounded.x + extents->unbounded.width) {
599         int x = extents->bounded.x + extents->bounded.width;
600         int y = extents->bounded.y;
601         int width = extents->unbounded.x + extents->unbounded.width - x;
602         int height = extents->bounded.height;
603
604         compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
605                                x + mask_x, y + mask_y,
606                                0, 0,
607                                x, y,
608                                width, height);
609     }
610
611     /* bottom */
612     if (extents->bounded.y + extents->bounded.height != extents->unbounded.y + extents->unbounded.height) {
613         int x = extents->unbounded.x;
614         int y = extents->bounded.y + extents->bounded.height;
615         int width = extents->unbounded.width;
616         int height = extents->unbounded.y + extents->unbounded.height - y;
617
618         compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
619                                x + mask_x, y + mask_y,
620                                0, 0,
621                                x, y,
622                                width, height);
623     }
624
625     cairo_surface_destroy (mask);
626
627     return CAIRO_STATUS_SUCCESS;
628 }
629
630 static cairo_status_t
631 fixup_unbounded_boxes (const cairo_mask_compositor_t *compositor,
632                        const cairo_composite_rectangles_t *extents,
633                        cairo_boxes_t *boxes)
634 {
635     cairo_surface_t *dst = extents->surface;
636     cairo_boxes_t clear;
637     cairo_region_t *clip_region;
638     cairo_box_t box;
639     cairo_status_t status;
640     struct _cairo_boxes_chunk *chunk;
641     int i;
642
643     assert (boxes->is_pixel_aligned);
644
645     clip_region = NULL;
646     if (_cairo_clip_is_region (extents->clip) &&
647         (clip_region = _cairo_clip_get_region (extents->clip)) &&
648         cairo_region_contains_rectangle (clip_region,
649                                          &extents->bounded) == CAIRO_REGION_OVERLAP_IN)
650         clip_region = NULL;
651
652
653     if (boxes->num_boxes <= 1 && clip_region == NULL)
654         return fixup_unbounded (compositor, dst, extents);
655
656     _cairo_boxes_init (&clear);
657
658     box.p1.x = _cairo_fixed_from_int (extents->unbounded.x + extents->unbounded.width);
659     box.p1.y = _cairo_fixed_from_int (extents->unbounded.y);
660     box.p2.x = _cairo_fixed_from_int (extents->unbounded.x);
661     box.p2.y = _cairo_fixed_from_int (extents->unbounded.y + extents->unbounded.height);
662
663     if (clip_region == NULL) {
664         cairo_boxes_t tmp;
665
666         _cairo_boxes_init (&tmp);
667
668         status = _cairo_boxes_add (&tmp, CAIRO_ANTIALIAS_DEFAULT, &box);
669         assert (status == CAIRO_STATUS_SUCCESS);
670
671         tmp.chunks.next = &boxes->chunks;
672         tmp.num_boxes += boxes->num_boxes;
673
674         status = _cairo_bentley_ottmann_tessellate_boxes (&tmp,
675                                                           CAIRO_FILL_RULE_WINDING,
676                                                           &clear);
677
678         tmp.chunks.next = NULL;
679     } else {
680         pixman_box32_t *pbox;
681
682         pbox = pixman_region32_rectangles (&clip_region->rgn, &i);
683         _cairo_boxes_limit (&clear, (cairo_box_t *) pbox, i);
684
685         status = _cairo_boxes_add (&clear, CAIRO_ANTIALIAS_DEFAULT, &box);
686         assert (status == CAIRO_STATUS_SUCCESS);
687
688         for (chunk = &boxes->chunks; chunk != NULL; chunk = chunk->next) {
689             for (i = 0; i < chunk->count; i++) {
690                 status = _cairo_boxes_add (&clear,
691                                            CAIRO_ANTIALIAS_DEFAULT,
692                                            &chunk->base[i]);
693                 if (unlikely (status)) {
694                     _cairo_boxes_fini (&clear);
695                     return status;
696                 }
697             }
698         }
699
700         status = _cairo_bentley_ottmann_tessellate_boxes (&clear,
701                                                           CAIRO_FILL_RULE_WINDING,
702                                                           &clear);
703     }
704
705     if (likely (status == CAIRO_STATUS_SUCCESS)) {
706         status = compositor->fill_boxes (dst,
707                                          CAIRO_OPERATOR_CLEAR,
708                                          CAIRO_COLOR_TRANSPARENT,
709                                          &clear);
710     }
711
712     _cairo_boxes_fini (&clear);
713
714     return status;
715 }
716
717 enum {
718     NEED_CLIP_REGION = 0x1,
719     NEED_CLIP_SURFACE = 0x2,
720     FORCE_CLIP_REGION = 0x4,
721 };
722
723 static cairo_bool_t
724 need_bounded_clip (cairo_composite_rectangles_t *extents)
725 {
726     unsigned int flags = NEED_CLIP_REGION;
727     if (! _cairo_clip_is_region (extents->clip))
728         flags |= NEED_CLIP_SURFACE;
729     return flags;
730 }
731
732 static cairo_bool_t
733 need_unbounded_clip (cairo_composite_rectangles_t *extents)
734 {
735     unsigned int flags = 0;
736     if (! extents->is_bounded) {
737         flags |= NEED_CLIP_REGION;
738         if (! _cairo_clip_is_region (extents->clip))
739             flags |= NEED_CLIP_SURFACE;
740     }
741     if (extents->clip->path != NULL)
742         flags |= NEED_CLIP_SURFACE;
743     return flags;
744 }
745
746 static cairo_status_t
747 clip_and_composite (const cairo_mask_compositor_t *compositor,
748                     draw_func_t                  draw_func,
749                     draw_func_t                  mask_func,
750                     void                        *draw_closure,
751                     cairo_composite_rectangles_t*extents,
752                     unsigned int need_clip)
753 {
754     cairo_surface_t *dst = extents->surface;
755     cairo_operator_t op = extents->op;
756     cairo_pattern_t *src = &extents->source_pattern.base;
757     cairo_region_t *clip_region = NULL;
758     cairo_status_t status;
759
760     compositor->acquire (dst);
761
762     if (need_clip & NEED_CLIP_REGION) {
763         clip_region = _cairo_clip_get_region (extents->clip);
764         if ((need_clip & FORCE_CLIP_REGION) == 0 &&
765             _cairo_composite_rectangles_can_reduce_clip (extents,
766                                                          extents->clip))
767             clip_region = NULL;
768         if (clip_region != NULL) {
769             status = compositor->set_clip_region (dst, clip_region);
770             if (unlikely (status)) {
771                 compositor->release (dst);
772                 return status;
773             }
774         }
775     }
776
777     if (reduce_alpha_op (dst, op, &extents->source_pattern.base)) {
778         op = CAIRO_OPERATOR_ADD;
779         src = NULL;
780     }
781
782     if (op == CAIRO_OPERATOR_SOURCE) {
783         status = clip_and_composite_source (compositor,
784                                             draw_closure, draw_func, mask_func,
785                                             src, extents);
786     } else {
787         if (op == CAIRO_OPERATOR_CLEAR) {
788             op = CAIRO_OPERATOR_DEST_OUT;
789             src = NULL;
790         }
791
792         if (need_clip & NEED_CLIP_SURFACE) {
793             if (extents->is_bounded) {
794                 status = clip_and_composite_with_mask (compositor,
795                                                        draw_closure,
796                                                        draw_func,
797                                                        mask_func,
798                                                        op, src, extents);
799             } else {
800                 status = clip_and_composite_combine (compositor,
801                                                      draw_closure,
802                                                      draw_func,
803                                                      op, src, extents);
804             }
805         } else {
806             status = draw_func (compositor,
807                                 dst, draw_closure,
808                                 op, src, &extents->source_sample_area,
809                                 0, 0,
810                                 &extents->bounded,
811                                 extents->clip);
812         }
813     }
814
815     if (status == CAIRO_STATUS_SUCCESS && ! extents->is_bounded) {
816         if (need_clip & NEED_CLIP_SURFACE)
817             status = fixup_unbounded_with_mask (compositor, dst, extents);
818         else
819             status = fixup_unbounded (compositor, dst, extents);
820     }
821
822     if (clip_region)
823         compositor->set_clip_region (dst, NULL);
824
825     compositor->release (dst);
826
827     return status;
828 }
829
830 static cairo_int_status_t
831 trim_extents_to_boxes (cairo_composite_rectangles_t *extents,
832                        cairo_boxes_t *boxes)
833 {
834     cairo_box_t box;
835
836     _cairo_boxes_extents (boxes, &box);
837     return _cairo_composite_rectangles_intersect_mask_extents (extents, &box);
838 }
839
840 static cairo_status_t
841 upload_boxes (const cairo_mask_compositor_t *compositor,
842               cairo_composite_rectangles_t *extents,
843               cairo_boxes_t *boxes)
844 {
845     cairo_surface_t *dst = extents->surface;
846     const cairo_pattern_t *source = &extents->source_pattern.base;
847     cairo_surface_t *src;
848     cairo_rectangle_int_t limit;
849     cairo_int_status_t status;
850     int tx, ty;
851
852     src = _cairo_pattern_get_source ((cairo_surface_pattern_t *)source, &limit);
853     if (!(src->type == CAIRO_SURFACE_TYPE_IMAGE || src->type == dst->type))
854         return CAIRO_INT_STATUS_UNSUPPORTED;
855
856     if (! _cairo_matrix_is_integer_translation (&source->matrix, &tx, &ty))
857         return CAIRO_INT_STATUS_UNSUPPORTED;
858
859     /* Check that the data is entirely within the image */
860     if (extents->bounded.x + tx < limit.x || extents->bounded.y + ty < limit.y)
861         return CAIRO_INT_STATUS_UNSUPPORTED;
862
863     if (extents->bounded.x + extents->bounded.width  + tx > limit.x + limit.width ||
864         extents->bounded.y + extents->bounded.height + ty > limit.y + limit.height)
865         return CAIRO_INT_STATUS_UNSUPPORTED;
866
867     tx += limit.x;
868     ty += limit.y;
869
870     if (src->type == CAIRO_SURFACE_TYPE_IMAGE)
871         status = compositor->draw_image_boxes (dst,
872                                                (cairo_image_surface_t *)src,
873                                                boxes, tx, ty);
874     else
875         status = compositor->copy_boxes (dst, src, boxes, &extents->bounded,
876                                          tx, ty);
877
878     return status;
879 }
880
881 static cairo_status_t
882 composite_boxes (const cairo_mask_compositor_t *compositor,
883                  const cairo_composite_rectangles_t *extents,
884                  cairo_boxes_t *boxes)
885 {
886     cairo_surface_t *dst = extents->surface;
887     cairo_operator_t op = extents->op;
888     const cairo_pattern_t *source = &extents->source_pattern.base;
889     cairo_bool_t need_clip_mask = extents->clip->path != NULL;
890     cairo_status_t status;
891
892     if (need_clip_mask &&
893         (! extents->is_bounded || op == CAIRO_OPERATOR_SOURCE))
894     {
895         return CAIRO_INT_STATUS_UNSUPPORTED;
896     }
897
898     status = compositor->acquire (dst);
899     if (unlikely (status))
900         return status;
901
902     if (! need_clip_mask && source->type == CAIRO_PATTERN_TYPE_SOLID) {
903         const cairo_color_t *color;
904
905         color = &((cairo_solid_pattern_t *) source)->color;
906         status = compositor->fill_boxes (dst, op, color, boxes);
907     } else {
908         cairo_surface_t *src, *mask = NULL;
909         int src_x, src_y;
910         int mask_x = 0, mask_y = 0;
911
912         if (need_clip_mask) {
913             mask = get_clip_source (compositor,
914                                     extents->clip, dst, &extents->bounded,
915                                     &mask_x, &mask_y);
916             if (unlikely (mask->status)) {
917                 status = mask->status;
918                 cairo_surface_destroy (mask);
919                 return status;
920             }
921
922             if (op == CAIRO_OPERATOR_CLEAR) {
923                 source = NULL;
924                 op = CAIRO_OPERATOR_DEST_OUT;
925             }
926         }
927
928         if (source || mask == NULL) {
929             src = compositor->pattern_to_surface (dst, source, FALSE,
930                                                   &extents->bounded,
931                                                   &extents->source_sample_area,
932                                                   &src_x, &src_y);
933         } else {
934             src = mask;
935             src_x = mask_x;
936             src_y = mask_y;
937             mask = NULL;
938         }
939
940         status = compositor->composite_boxes (dst, op, src, mask,
941                                               src_x, src_y,
942                                               mask_x, mask_y,
943                                               0, 0,
944                                               boxes, &extents->bounded);
945
946         cairo_surface_destroy (src);
947         cairo_surface_destroy (mask);
948     }
949
950     if (status == CAIRO_STATUS_SUCCESS && ! extents->is_bounded)
951         status = fixup_unbounded_boxes (compositor, extents, boxes);
952
953     compositor->release (dst);
954
955     return status;
956 }
957
958 static cairo_status_t
959 clip_and_composite_boxes (const cairo_mask_compositor_t *compositor,
960                           cairo_composite_rectangles_t *extents,
961                           cairo_boxes_t *boxes)
962 {
963     cairo_surface_t *dst = extents->surface;
964     cairo_int_status_t status;
965
966     if (boxes->num_boxes == 0) {
967         if (extents->is_bounded)
968             return CAIRO_STATUS_SUCCESS;
969
970         return fixup_unbounded_boxes (compositor, extents, boxes);
971     }
972
973     if (! boxes->is_pixel_aligned)
974         return CAIRO_INT_STATUS_UNSUPPORTED;
975
976     status = trim_extents_to_boxes (extents, boxes);
977     if (unlikely (status))
978         return status;
979
980     if (extents->source_pattern.base.type == CAIRO_PATTERN_TYPE_SURFACE &&
981         extents->clip->path == NULL &&
982         (extents->op == CAIRO_OPERATOR_SOURCE ||
983          (dst->is_clear && (extents->op == CAIRO_OPERATOR_OVER ||
984                             extents->op == CAIRO_OPERATOR_ADD))))
985     {
986         status = upload_boxes (compositor, extents, boxes);
987         if (status != CAIRO_INT_STATUS_UNSUPPORTED)
988             return status;
989     }
990
991     return composite_boxes (compositor, extents, boxes);
992 }
993
994 /* high-level compositor interface */
995
996 static cairo_int_status_t
997 _cairo_mask_compositor_paint (const cairo_compositor_t *_compositor,
998                               cairo_composite_rectangles_t *extents)
999 {
1000     cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
1001     cairo_boxes_t boxes;
1002     cairo_int_status_t status;
1003
1004     status = compositor->check_composite (extents);
1005     if (unlikely (status))
1006         return status;
1007
1008     _cairo_clip_steal_boxes (extents->clip, &boxes);
1009     status = clip_and_composite_boxes (compositor, extents, &boxes);
1010     _cairo_clip_unsteal_boxes (extents->clip, &boxes);
1011
1012     return status;
1013 }
1014
1015 struct composite_opacity_info {
1016     const cairo_mask_compositor_t *compositor;
1017     uint8_t op;
1018     cairo_surface_t *dst;
1019     cairo_surface_t *src;
1020     int src_x, src_y;
1021     double opacity;
1022 };
1023
1024 static void composite_opacity(void *closure,
1025                               int16_t x, int16_t y,
1026                               int16_t w, int16_t h,
1027                               uint16_t coverage)
1028 {
1029     struct composite_opacity_info *info = closure;
1030     const cairo_mask_compositor_t *compositor = info->compositor;
1031     cairo_surface_t *mask;
1032     int mask_x, mask_y;
1033     cairo_color_t color;
1034     cairo_solid_pattern_t solid;
1035
1036     _cairo_color_init_rgba (&color, 0, 0, 0, info->opacity * coverage);
1037     _cairo_pattern_init_solid (&solid, &color);
1038     mask = compositor->pattern_to_surface (info->dst, &solid.base, TRUE,
1039                                            &_cairo_unbounded_rectangle,
1040                                            &_cairo_unbounded_rectangle,
1041                                            &mask_x, &mask_y);
1042     if (likely (mask->status == CAIRO_STATUS_SUCCESS)) {
1043         if (info->src) {
1044             compositor->composite (info->dst, info->op, info->src, mask,
1045                                    x + info->src_x,  y + info->src_y,
1046                                    mask_x,           mask_y,
1047                                    x,                y,
1048                                    w,                h);
1049         } else {
1050             compositor->composite (info->dst, info->op, mask, NULL,
1051                                    mask_x,            mask_y,
1052                                    0,                 0,
1053                                    x,                 y,
1054                                    w,                 h);
1055         }
1056     }
1057
1058     cairo_surface_destroy (mask);
1059 }
1060
1061 static cairo_int_status_t
1062 composite_opacity_boxes (const cairo_mask_compositor_t *compositor,
1063                          cairo_surface_t                *dst,
1064                          void                           *closure,
1065                          cairo_operator_t                op,
1066                          const cairo_pattern_t          *src_pattern,
1067                          const cairo_rectangle_int_t    *src_sample,
1068                          int                             dst_x,
1069                          int                             dst_y,
1070                          const cairo_rectangle_int_t    *extents,
1071                          cairo_clip_t                   *clip)
1072 {
1073     const cairo_solid_pattern_t *mask_pattern = closure;
1074     struct composite_opacity_info info;
1075     int i;
1076
1077     assert (clip);
1078
1079     info.compositor = compositor;
1080     info.op = op;
1081     info.dst = dst;
1082
1083     if (src_pattern != NULL) {
1084         info.src = compositor->pattern_to_surface (dst, src_pattern, FALSE,
1085                                                    extents, src_sample,
1086                                                    &info.src_x, &info.src_y);
1087         if (unlikely (info.src->status))
1088             return info.src->status;
1089     } else
1090         info.src = NULL;
1091
1092     info.opacity = mask_pattern->color.alpha / (double) 0xffff;
1093
1094     /* XXX for lots of boxes create a clip region for the fully opaque areas */
1095     for (i = 0; i < clip->num_boxes; i++)
1096         do_unaligned_box(composite_opacity, &info,
1097                          &clip->boxes[i], dst_x, dst_y);
1098     cairo_surface_destroy (info.src);
1099
1100     return CAIRO_STATUS_SUCCESS;
1101 }
1102
1103 struct composite_box_info {
1104     const cairo_mask_compositor_t *compositor;
1105     cairo_surface_t *dst;
1106     cairo_surface_t *src;
1107     int src_x, src_y;
1108     uint8_t op;
1109 };
1110
1111 static void composite_box(void *closure,
1112                           int16_t x, int16_t y,
1113                           int16_t w, int16_t h,
1114                           uint16_t coverage)
1115 {
1116     struct composite_box_info *info = closure;
1117     const cairo_mask_compositor_t *compositor = info->compositor;
1118
1119     if (! CAIRO_ALPHA_SHORT_IS_OPAQUE (coverage)) {
1120         cairo_surface_t *mask;
1121         cairo_color_t color;
1122         cairo_solid_pattern_t solid;
1123         int mask_x, mask_y;
1124
1125         _cairo_color_init_rgba (&color, 0, 0, 0, coverage / (double)0xffff);
1126         _cairo_pattern_init_solid (&solid, &color);
1127
1128         mask = compositor->pattern_to_surface (info->dst, &solid.base, FALSE,
1129                                                &_cairo_unbounded_rectangle,
1130                                                &_cairo_unbounded_rectangle,
1131                                                &mask_x, &mask_y);
1132
1133         if (likely (mask->status == CAIRO_STATUS_SUCCESS)) {
1134             compositor->composite (info->dst, info->op, info->src, mask,
1135                                    x + info->src_x,  y + info->src_y,
1136                                    mask_x,           mask_y,
1137                                    x,                y,
1138                                    w,                h);
1139         }
1140
1141         cairo_surface_destroy (mask);
1142     } else {
1143         compositor->composite (info->dst, info->op, info->src, NULL,
1144                                x + info->src_x,  y + info->src_y,
1145                                0,                0,
1146                                x,                y,
1147                                w,                h);
1148     }
1149 }
1150
1151 static cairo_int_status_t
1152 composite_mask_clip_boxes (const cairo_mask_compositor_t *compositor,
1153                            cairo_surface_t              *dst,
1154                            void                         *closure,
1155                            cairo_operator_t              op,
1156                            const cairo_pattern_t        *src_pattern,
1157                            const cairo_rectangle_int_t  *src_sample,
1158                            int                           dst_x,
1159                            int                           dst_y,
1160                            const cairo_rectangle_int_t  *extents,
1161                            cairo_clip_t                 *clip)
1162 {
1163     cairo_composite_rectangles_t *composite = closure;
1164     struct composite_box_info info;
1165     int i;
1166
1167     assert (src_pattern == NULL);
1168     assert (op == CAIRO_OPERATOR_SOURCE);
1169
1170     info.compositor = compositor;
1171     info.op = CAIRO_OPERATOR_SOURCE;
1172     info.dst = dst;
1173     info.src = compositor->pattern_to_surface (dst,
1174                                                &composite->mask_pattern.base,
1175                                                FALSE, extents,
1176                                                &composite->mask_sample_area,
1177                                                &info.src_x, &info.src_y);
1178     if (unlikely (info.src->status))
1179         return info.src->status;
1180
1181     info.src_x += dst_x;
1182     info.src_y += dst_y;
1183
1184     for (i = 0; i < clip->num_boxes; i++)
1185         do_unaligned_box(composite_box, &info, &clip->boxes[i], dst_x, dst_y);
1186
1187     cairo_surface_destroy (info.src);
1188
1189     return CAIRO_STATUS_SUCCESS;
1190 }
1191
1192 static cairo_int_status_t
1193 composite_mask (const cairo_mask_compositor_t *compositor,
1194                 cairo_surface_t                 *dst,
1195                 void                            *closure,
1196                 cairo_operator_t                 op,
1197                 const cairo_pattern_t           *src_pattern,
1198                 const cairo_rectangle_int_t     *src_sample,
1199                 int                              dst_x,
1200                 int                              dst_y,
1201                 const cairo_rectangle_int_t     *extents,
1202                 cairo_clip_t                    *clip)
1203 {
1204     cairo_composite_rectangles_t *composite = closure;
1205     cairo_surface_t *src, *mask;
1206     int src_x, src_y;
1207     int mask_x, mask_y;
1208
1209     if (src_pattern != NULL) {
1210         src = compositor->pattern_to_surface (dst, src_pattern, FALSE,
1211                                               extents, src_sample,
1212                                               &src_x, &src_y);
1213         if (unlikely (src->status))
1214             return src->status;
1215
1216         mask = compositor->pattern_to_surface (dst, &composite->mask_pattern.base, TRUE,
1217                                                extents, &composite->mask_sample_area,
1218                                                &mask_x, &mask_y);
1219         if (unlikely (mask->status)) {
1220             cairo_surface_destroy (src);
1221             return mask->status;
1222         }
1223
1224         compositor->composite (dst, op, src, mask,
1225                                extents->x + src_x,  extents->y + src_y,
1226                                extents->x + mask_x, extents->y + mask_y,
1227                                extents->x - dst_x,  extents->y - dst_y,
1228                                extents->width,      extents->height);
1229
1230         cairo_surface_destroy (mask);
1231         cairo_surface_destroy (src);
1232     } else {
1233         src = compositor->pattern_to_surface (dst, &composite->mask_pattern.base, FALSE,
1234                                               extents, &composite->mask_sample_area,
1235                                               &src_x, &src_y);
1236         if (unlikely (src->status))
1237             return src->status;
1238
1239         compositor->composite (dst, op, src, NULL,
1240                                extents->x + src_x,  extents->y + src_y,
1241                                0, 0,
1242                                extents->x - dst_x,  extents->y - dst_y,
1243                                extents->width,      extents->height);
1244
1245         cairo_surface_destroy (src);
1246     }
1247
1248     return CAIRO_STATUS_SUCCESS;
1249 }
1250
1251 static cairo_int_status_t
1252 _cairo_mask_compositor_mask (const cairo_compositor_t *_compositor,
1253                              cairo_composite_rectangles_t *extents)
1254 {
1255     const cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
1256     cairo_int_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
1257
1258     status = compositor->check_composite (extents);
1259     if (unlikely (status))
1260         return status;
1261
1262     if (extents->mask_pattern.base.type == CAIRO_PATTERN_TYPE_SOLID &&
1263         extents->clip->path == NULL &&
1264         _cairo_clip_is_region (extents->clip)) {
1265         status = clip_and_composite (compositor,
1266                                      composite_opacity_boxes,
1267                                      composite_opacity_boxes,
1268                                      &extents->mask_pattern.solid,
1269                                      extents, need_unbounded_clip (extents));
1270     } else {
1271         status = clip_and_composite (compositor,
1272                                      composite_mask,
1273                                      extents->clip->path == NULL ? composite_mask_clip_boxes : NULL,
1274                                      extents,
1275                                      extents, need_bounded_clip (extents));
1276     }
1277
1278     return status;
1279 }
1280
1281 static cairo_int_status_t
1282 _cairo_mask_compositor_stroke (const cairo_compositor_t *_compositor,
1283                                cairo_composite_rectangles_t *extents,
1284                                const cairo_path_fixed_t *path,
1285                                const cairo_stroke_style_t       *style,
1286                                const cairo_matrix_t     *ctm,
1287                                const cairo_matrix_t     *ctm_inverse,
1288                                double            tolerance,
1289                                cairo_antialias_t         antialias)
1290 {
1291     const cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
1292     cairo_surface_t *mask;
1293     cairo_surface_pattern_t pattern;
1294     cairo_int_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
1295
1296     status = compositor->check_composite (extents);
1297     if (unlikely (status))
1298         return status;
1299
1300     if (_cairo_path_fixed_stroke_is_rectilinear (path)) {
1301         cairo_boxes_t boxes;
1302
1303         _cairo_boxes_init_with_clip (&boxes, extents->clip);
1304         status = _cairo_path_fixed_stroke_rectilinear_to_boxes (path,
1305                                                                 style,
1306                                                                 ctm,
1307                                                                 antialias,
1308                                                                 &boxes);
1309         if (likely (status == CAIRO_INT_STATUS_SUCCESS))
1310             status = clip_and_composite_boxes (compositor, extents, &boxes);
1311         _cairo_boxes_fini (&boxes);
1312     }
1313
1314
1315     if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
1316         mask = cairo_surface_create_similar_image (extents->surface,
1317                                                    CAIRO_FORMAT_A8,
1318                                                    extents->bounded.width,
1319                                                    extents->bounded.height);
1320         if (unlikely (mask->status)) {
1321             status = mask->status;
1322             cairo_surface_destroy (mask);
1323             return status;
1324         }
1325
1326         status = _cairo_surface_offset_stroke (mask,
1327                                                extents->bounded.x,
1328                                                extents->bounded.y,
1329                                                CAIRO_OPERATOR_ADD,
1330                                                &_cairo_pattern_white.base,
1331                                                path, style, ctm, ctm_inverse,
1332                                                tolerance, antialias,
1333                                                extents->clip);
1334         if (unlikely (status)) {
1335             cairo_surface_destroy (mask);
1336             return status;
1337         }
1338
1339         _cairo_pattern_init_for_surface (&pattern, mask);
1340         cairo_surface_destroy (mask);
1341
1342         cairo_matrix_init_translate (&pattern.base.matrix,
1343                                      -extents->bounded.x,
1344                                      -extents->bounded.y);
1345         pattern.base.filter = CAIRO_FILTER_NEAREST;
1346         pattern.base.extend = CAIRO_EXTEND_NONE;
1347         status = _cairo_surface_mask (extents->surface,
1348                                       extents->op,
1349                                       &extents->source_pattern.base,
1350                                       &pattern.base,
1351                                       extents->clip);
1352         _cairo_pattern_fini (&pattern.base);
1353     }
1354
1355     return status;
1356 }
1357
1358 static cairo_int_status_t
1359 _cairo_mask_compositor_fill (const cairo_compositor_t *_compositor,
1360                              cairo_composite_rectangles_t *extents,
1361                              const cairo_path_fixed_t   *path,
1362                              cairo_fill_rule_t   fill_rule,
1363                              double                      tolerance,
1364                              cairo_antialias_t   antialias)
1365 {
1366     const cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
1367     cairo_surface_t *mask;
1368     cairo_surface_pattern_t pattern;
1369     cairo_int_status_t status = CAIRO_INT_STATUS_UNSUPPORTED;
1370
1371     status = compositor->check_composite (extents);
1372     if (unlikely (status))
1373         return status;
1374
1375     if (_cairo_path_fixed_fill_is_rectilinear (path)) {
1376         cairo_boxes_t boxes;
1377
1378         _cairo_boxes_init_with_clip (&boxes, extents->clip);
1379         status = _cairo_path_fixed_fill_rectilinear_to_boxes (path,
1380                                                               fill_rule,
1381                                                               antialias,
1382                                                               &boxes);
1383         if (likely (status == CAIRO_INT_STATUS_SUCCESS))
1384             status = clip_and_composite_boxes (compositor, extents, &boxes);
1385         _cairo_boxes_fini (&boxes);
1386     }
1387
1388     if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
1389         mask = cairo_surface_create_similar_image (extents->surface,
1390                                                    CAIRO_FORMAT_A8,
1391                                                    extents->bounded.width,
1392                                                    extents->bounded.height);
1393         if (unlikely (mask->status)) {
1394             status = mask->status;
1395             cairo_surface_destroy (mask);
1396             return status;
1397         }
1398
1399         status = _cairo_surface_offset_fill (mask,
1400                                              extents->bounded.x,
1401                                              extents->bounded.y,
1402                                              CAIRO_OPERATOR_ADD,
1403                                              &_cairo_pattern_white.base,
1404                                              path, fill_rule, tolerance, antialias,
1405                                              extents->clip);
1406         if (unlikely (status)) {
1407             cairo_surface_destroy (mask);
1408             return status;
1409         }
1410
1411         _cairo_pattern_init_for_surface (&pattern, mask);
1412         cairo_surface_destroy (mask);
1413
1414         cairo_matrix_init_translate (&pattern.base.matrix,
1415                                      -extents->bounded.x,
1416                                      -extents->bounded.y);
1417         pattern.base.filter = CAIRO_FILTER_NEAREST;
1418         pattern.base.extend = CAIRO_EXTEND_NONE;
1419         status = _cairo_surface_mask (extents->surface,
1420                                       extents->op,
1421                                       &extents->source_pattern.base,
1422                                       &pattern.base,
1423                                       extents->clip);
1424         _cairo_pattern_fini (&pattern.base);
1425     }
1426
1427     return status;
1428 }
1429
1430 static cairo_int_status_t
1431 _cairo_mask_compositor_glyphs (const cairo_compositor_t *_compositor,
1432                                cairo_composite_rectangles_t *extents,
1433                                cairo_scaled_font_t      *scaled_font,
1434                                cairo_glyph_t            *glyphs,
1435                                int                       num_glyphs,
1436                                cairo_bool_t              overlap)
1437 {
1438     const cairo_mask_compositor_t *compositor = (cairo_mask_compositor_t*)_compositor;
1439     cairo_surface_t *mask;
1440     cairo_surface_pattern_t pattern;
1441     cairo_int_status_t status;
1442
1443     status = compositor->check_composite (extents);
1444     if (unlikely (status))
1445         return CAIRO_INT_STATUS_UNSUPPORTED;
1446
1447     mask = cairo_surface_create_similar_image (extents->surface,
1448                                                CAIRO_FORMAT_A8,
1449                                                extents->bounded.width,
1450                                                extents->bounded.height);
1451     if (unlikely (mask->status)) {
1452         status = mask->status;
1453         cairo_surface_destroy (mask);
1454         return status;
1455     }
1456
1457     status = _cairo_surface_offset_glyphs (mask,
1458                                            extents->bounded.x,
1459                                            extents->bounded.y,
1460                                            CAIRO_OPERATOR_ADD,
1461                                            &_cairo_pattern_white.base,
1462                                            scaled_font, glyphs, num_glyphs,
1463                                            extents->clip);
1464     if (unlikely (status)) {
1465         cairo_surface_destroy (mask);
1466         return status;
1467     }
1468
1469     _cairo_pattern_init_for_surface (&pattern, mask);
1470     cairo_surface_destroy (mask);
1471
1472     cairo_matrix_init_translate (&pattern.base.matrix,
1473                                  -extents->bounded.x,
1474                                  -extents->bounded.y);
1475     pattern.base.filter = CAIRO_FILTER_NEAREST;
1476     pattern.base.extend = CAIRO_EXTEND_NONE;
1477     status = _cairo_surface_mask (extents->surface,
1478                                   extents->op,
1479                                   &extents->source_pattern.base,
1480                                   &pattern.base,
1481                                   extents->clip);
1482     _cairo_pattern_fini (&pattern.base);
1483
1484     return status;
1485 }
1486
1487 void
1488 _cairo_mask_compositor_init (cairo_mask_compositor_t *compositor,
1489                              const cairo_compositor_t *delegate)
1490 {
1491     compositor->base.delegate = delegate;
1492
1493     compositor->base.paint = _cairo_mask_compositor_paint;
1494     compositor->base.mask  = _cairo_mask_compositor_mask;
1495     compositor->base.fill  = _cairo_mask_compositor_fill;
1496     compositor->base.stroke = _cairo_mask_compositor_stroke;
1497     compositor->base.glyphs = _cairo_mask_compositor_glyphs;
1498 }