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
#include "cairoint.h"
43

            
44
#include "cairo-box-inline.h"
45
#include "cairo-boxes-private.h"
46
#include "cairo-clip-inline.h"
47
#include "cairo-clip-private.h"
48
#include "cairo-composite-rectangles-private.h"
49
#include "cairo-compositor-private.h"
50
#include "cairo-error-private.h"
51
#include "cairo-image-surface-private.h"
52
#include "cairo-pattern-inline.h"
53
#include "cairo-paginated-private.h"
54
#include "cairo-recording-surface-inline.h"
55
#include "cairo-surface-subsurface-private.h"
56
#include "cairo-surface-snapshot-inline.h"
57
#include "cairo-surface-observer-private.h"
58
#include "cairo-region-private.h"
59
#include "cairo-spans-private.h"
60
#include "cairo-traps-private.h"
61
#include "cairo-tristrip-private.h"
62

            
63
typedef cairo_int_status_t
64
(*draw_func_t) (const cairo_traps_compositor_t *compositor,
65
		cairo_surface_t			*dst,
66
		void				*closure,
67
		cairo_operator_t		 op,
68
		cairo_surface_t		*src,
69
		int				 src_x,
70
		int				 src_y,
71
		int				 dst_x,
72
		int				 dst_y,
73
		const cairo_rectangle_int_t	*extents,
74
		cairo_clip_t			*clip);
75

            
76
465
static void do_unaligned_row(void (*blt)(void *closure,
77
					 int16_t x, int16_t y,
78
					 int16_t w, int16_t h,
79
					 uint16_t coverage),
80
			     void *closure,
81
			     const cairo_box_t *b,
82
			     int tx, int y, int h,
83
			     uint16_t coverage)
84
{
85
465
    int x1 = _cairo_fixed_integer_part (b->p1.x) - tx;
86
465
    int x2 = _cairo_fixed_integer_part (b->p2.x) - tx;
87
465
    if (x2 > x1) {
88
465
	if (! _cairo_fixed_is_integer (b->p1.x)) {
89
	    blt(closure, x1, y, 1, h,
90
		coverage * (256 - _cairo_fixed_fractional_part (b->p1.x)));
91
	    x1++;
92
	}
93

            
94
465
	if (x2 > x1)
95
465
	    blt(closure, x1, y, x2-x1, h, (coverage << 8) - (coverage >> 8));
96

            
97
465
	if (! _cairo_fixed_is_integer (b->p2.x))
98
	    blt(closure, x2, y, 1, h,
99
		coverage * _cairo_fixed_fractional_part (b->p2.x));
100
    } else
101
	blt(closure, x1, y, 1, h,
102
	    coverage * (b->p2.x - b->p1.x));
103
465
}
104

            
105
465
static void do_unaligned_box(void (*blt)(void *closure,
106
					 int16_t x, int16_t y,
107
					 int16_t w, int16_t h,
108
					 uint16_t coverage),
109
			     void *closure,
110
			     const cairo_box_t *b, int tx, int ty)
111
{
112
465
    int y1 = _cairo_fixed_integer_part (b->p1.y) - ty;
113
465
    int y2 = _cairo_fixed_integer_part (b->p2.y) - ty;
114
465
    if (y2 > y1) {
115
465
	if (! _cairo_fixed_is_integer (b->p1.y)) {
116
	    do_unaligned_row(blt, closure, b, tx, y1, 1,
117
			     256 - _cairo_fixed_fractional_part (b->p1.y));
118
	    y1++;
119
	}
120

            
121
465
	if (y2 > y1)
122
465
	    do_unaligned_row(blt, closure, b, tx, y1, y2-y1, 256);
123

            
124
465
	if (! _cairo_fixed_is_integer (b->p2.y))
125
	    do_unaligned_row(blt, closure, b, tx, y2, 1,
126
			     _cairo_fixed_fractional_part (b->p2.y));
127
    } else
128
	do_unaligned_row(blt, closure, b, tx, y1, 1,
129
			 b->p2.y - b->p1.y);
130
465
}
131

            
132
struct blt_in {
133
    const cairo_traps_compositor_t *compositor;
134
    cairo_surface_t *dst;
135
    cairo_boxes_t boxes;
136
};
137

            
138
static void blt_in(void *closure,
139
		   int16_t x, int16_t y,
140
		   int16_t w, int16_t h,
141
		   uint16_t coverage)
142
{
143
    struct blt_in *info = closure;
144
    cairo_color_t color;
145

            
146
    if (CAIRO_ALPHA_SHORT_IS_OPAQUE (coverage))
147
	return;
148

            
149
    _cairo_box_from_integers (&info->boxes.chunks.base[0], x, y, w, h);
150

            
151
    _cairo_color_init_rgba (&color, 0, 0, 0, coverage / (double) 0xffff);
152
    info->compositor->fill_boxes (info->dst,
153
				  CAIRO_OPERATOR_IN, &color,
154
				  &info->boxes);
155
}
156

            
157
static void
158
27
add_rect_with_offset (cairo_boxes_t *boxes, int x1, int y1, int x2, int y2, int dx, int dy)
159
{
160
    cairo_box_t box;
161
    cairo_int_status_t status;
162

            
163
27
    box.p1.x = _cairo_fixed_from_int (x1 - dx);
164
27
    box.p1.y = _cairo_fixed_from_int (y1 - dy);
165
27
    box.p2.x = _cairo_fixed_from_int (x2 - dx);
166
27
    box.p2.y = _cairo_fixed_from_int (y2 - dy);
167

            
168
27
    status = _cairo_boxes_add (boxes, CAIRO_ANTIALIAS_DEFAULT, &box);
169
27
    assert (status == CAIRO_INT_STATUS_SUCCESS);
170
27
}
171

            
172
static cairo_int_status_t
173
30
combine_clip_as_traps (const cairo_traps_compositor_t *compositor,
174
		       cairo_surface_t *mask,
175
		       const cairo_clip_t *clip,
176
		       const cairo_rectangle_int_t *extents)
177
{
178
    cairo_polygon_t polygon;
179
    cairo_fill_rule_t fill_rule;
180
    cairo_antialias_t antialias;
181
    cairo_traps_t traps;
182
    cairo_surface_t *src;
183
    cairo_box_t box;
184
    cairo_rectangle_int_t fixup;
185
    int src_x, src_y;
186
    cairo_int_status_t status;
187

            
188
    TRACE ((stderr, "%s\n", __FUNCTION__));
189

            
190
30
    status = _cairo_clip_get_polygon (clip, &polygon,
191
				      &fill_rule, &antialias);
192
30
    if (status)
193
	return status;
194

            
195
30
    _cairo_traps_init (&traps);
196
30
    status = _cairo_bentley_ottmann_tessellate_polygon (&traps,
197
							&polygon,
198
							fill_rule);
199
30
    _cairo_polygon_fini (&polygon);
200
30
    if (unlikely (status))
201
	return status;
202

            
203
30
    src = compositor->pattern_to_surface (mask, NULL, FALSE,
204
					  extents, NULL,
205
					  &src_x, &src_y);
206
30
    if (unlikely (src->status)) {
207
	_cairo_traps_fini (&traps);
208
	return src->status;
209
    }
210

            
211
30
    status = compositor->composite_traps (mask, CAIRO_OPERATOR_IN, src,
212
					  src_x, src_y,
213
30
					  extents->x, extents->y,
214
					  extents,
215
					  antialias, &traps);
216

            
217
30
    _cairo_traps_extents (&traps, &box);
218
30
    _cairo_box_round_to_rectangle (&box, &fixup);
219
30
    _cairo_traps_fini (&traps);
220
30
    cairo_surface_destroy (src);
221

            
222
30
    if (unlikely (status))
223
	return status;
224

            
225
30
    if (! _cairo_rectangle_intersect (&fixup, extents))
226
	return CAIRO_STATUS_SUCCESS;
227

            
228
30
    if (fixup.width < extents->width || fixup.height < extents->height) {
229
	cairo_boxes_t clear;
230

            
231
27
	_cairo_boxes_init (&clear);
232

            
233
	/* top */
234
27
	if (fixup.y != extents->y) {
235
	    add_rect_with_offset (&clear,
236
				  extents->x, extents->y,
237
				  extents->x + extents->width,
238
				  fixup.y,
239
				  extents->x, extents->y);
240
	}
241
	/* left */
242
27
	if (fixup.x != extents->x) {
243
	    add_rect_with_offset (&clear,
244
				  extents->x, fixup.y,
245
				  fixup.x,
246
				  fixup.y + fixup.height,
247
				  extents->x, extents->y);
248
	}
249
	/* right */
250
27
	if (fixup.x + fixup.width != extents->x + extents->width) {
251
27
	    add_rect_with_offset (&clear,
252
27
				  fixup.x + fixup.width,
253
				  fixup.y,
254
27
				  extents->x + extents->width,
255
27
				  fixup.y + fixup.height,
256
27
				  extents->x, extents->y);
257
	}
258
	/* bottom */
259
27
	if (fixup.y + fixup.height != extents->y + extents->height) {
260
	    add_rect_with_offset (&clear,
261
				  extents->x,
262
				  fixup.y + fixup.height,
263
				  extents->x + extents->width,
264
				  extents->y + extents->height,
265
				  extents->x, extents->y);
266
	}
267

            
268
27
	status = compositor->fill_boxes (mask,
269
					 CAIRO_OPERATOR_CLEAR,
270
					 CAIRO_COLOR_TRANSPARENT,
271
					 &clear);
272

            
273
27
	_cairo_boxes_fini (&clear);
274
    }
275

            
276
30
    return status;
277
}
278

            
279
static cairo_status_t
280
372
__clip_to_surface (const cairo_traps_compositor_t *compositor,
281
		   const cairo_composite_rectangles_t *composite,
282
		   const cairo_rectangle_int_t *extents,
283
		   cairo_surface_t **surface)
284
{
285
    cairo_surface_t *mask;
286
    cairo_polygon_t polygon;
287
    cairo_fill_rule_t fill_rule;
288
    cairo_antialias_t antialias;
289
    cairo_traps_t traps;
290
    cairo_boxes_t clear;
291
    cairo_surface_t *src;
292
    int src_x, src_y;
293
    cairo_int_status_t status;
294

            
295
    TRACE ((stderr, "%s\n", __FUNCTION__));
296

            
297
372
    status = _cairo_clip_get_polygon (composite->clip, &polygon,
298
				      &fill_rule, &antialias);
299
372
    if (status)
300
	return status;
301

            
302
372
    _cairo_traps_init (&traps);
303
372
    status = _cairo_bentley_ottmann_tessellate_polygon (&traps,
304
							&polygon,
305
							fill_rule);
306
372
    _cairo_polygon_fini (&polygon);
307
372
    if (unlikely (status))
308
	return status;
309

            
310
372
    mask = _cairo_surface_create_scratch (composite->surface,
311
					  CAIRO_CONTENT_ALPHA,
312
372
					  extents->width,
313
372
					  extents->height,
314
					  NULL);
315
372
    if (unlikely (mask->status)) {
316
	_cairo_traps_fini (&traps);
317
	return status;
318
    }
319

            
320
372
    src = compositor->pattern_to_surface (mask, NULL, FALSE,
321
					  extents, NULL,
322
					  &src_x, &src_y);
323
372
    if (unlikely (status = src->status))
324
	goto error;
325

            
326
372
    status = compositor->acquire (mask);
327
372
    if (unlikely (status))
328
	goto error;
329

            
330
372
    _cairo_boxes_init_from_rectangle (&clear,
331
				      0, 0,
332
372
				      extents->width,
333
372
				      extents->height);
334
372
    status = compositor->fill_boxes (mask,
335
				     CAIRO_OPERATOR_CLEAR,
336
				     CAIRO_COLOR_TRANSPARENT,
337
				     &clear);
338
372
    if (unlikely (status))
339
	goto error_release;
340

            
341
372
    status = compositor->composite_traps (mask, CAIRO_OPERATOR_ADD, src,
342
					  src_x, src_y,
343
372
					  extents->x, extents->y,
344
					  extents,
345
					  antialias, &traps);
346
372
    if (unlikely (status))
347
	goto error_release;
348

            
349
372
    compositor->release (mask);
350
372
    *surface = mask;
351
372
out:
352
372
    cairo_surface_destroy (src);
353
372
    _cairo_traps_fini (&traps);
354
372
    return status;
355

            
356
error_release:
357
    compositor->release (mask);
358
error:
359
    cairo_surface_destroy (mask);
360
    goto out;
361
}
362

            
363
static cairo_surface_t *
364
372
traps_get_clip_surface (const cairo_traps_compositor_t *compositor,
365
			const cairo_composite_rectangles_t *composite,
366
			const cairo_rectangle_int_t *extents)
367
{
368
372
    cairo_surface_t *surface = NULL;
369
    cairo_int_status_t status;
370

            
371
    TRACE ((stderr, "%s\n", __FUNCTION__));
372

            
373
372
    status = __clip_to_surface (compositor, composite, extents, &surface);
374
372
    if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
375
	surface = _cairo_surface_create_scratch (composite->surface,
376
						 CAIRO_CONTENT_ALPHA,
377
						 extents->width,
378
						 extents->height,
379
						 CAIRO_COLOR_WHITE);
380
	if (unlikely (surface->status))
381
	    return surface;
382

            
383
	status = _cairo_clip_combine_with_surface (composite->clip, surface,
384
						   extents->x, extents->y);
385
    }
386
372
    if (unlikely (status)) {
387
	cairo_surface_destroy (surface);
388
	surface = _cairo_surface_create_in_error (status);
389
    }
390

            
391
372
    return surface;
392
}
393

            
394
12
static void blt_unaligned_boxes(const cairo_traps_compositor_t *compositor,
395
				cairo_surface_t *surface,
396
				int dx, int dy,
397
				cairo_box_t *boxes,
398
				int num_boxes)
399
{
400
    struct blt_in info;
401
    int i;
402

            
403
12
    info.compositor = compositor;
404
12
    info.dst = surface;
405
12
    _cairo_boxes_init (&info.boxes);
406
12
    info.boxes.num_boxes = 1;
407
24
    for (i = 0; i < num_boxes; i++) {
408
12
	cairo_box_t *b = &boxes[i];
409

            
410
24
	if (! _cairo_fixed_is_integer (b->p1.x) ||
411
24
	    ! _cairo_fixed_is_integer (b->p1.y) ||
412
24
	    ! _cairo_fixed_is_integer (b->p2.x) ||
413
12
	    ! _cairo_fixed_is_integer (b->p2.y))
414
	{
415
	    do_unaligned_box(blt_in, &info, b, dx, dy);
416
	}
417
    }
418
12
}
419

            
420
static cairo_surface_t *
421
402
create_composite_mask (const cairo_traps_compositor_t *compositor,
422
		       cairo_surface_t		*dst,
423
		       void			*draw_closure,
424
		       draw_func_t		 draw_func,
425
		       draw_func_t		 mask_func,
426
		       const cairo_composite_rectangles_t *extents)
427
{
428
    cairo_surface_t *surface, *src;
429
    cairo_int_status_t status;
430
    int src_x, src_y;
431

            
432
    TRACE ((stderr, "%s\n", __FUNCTION__));
433

            
434
402
    surface = _cairo_surface_create_scratch (dst, CAIRO_CONTENT_ALPHA,
435
402
					     extents->bounded.width,
436
402
					     extents->bounded.height,
437
					     NULL);
438
402
    if (unlikely (surface->status))
439
	return surface;
440

            
441
402
    src = compositor->pattern_to_surface (surface,
442
					  &_cairo_pattern_white.base,
443
					  FALSE,
444
					  &extents->bounded,
445
					  &extents->bounded,
446
					  &src_x, &src_y);
447
402
    if (unlikely (src->status)) {
448
	cairo_surface_destroy (surface);
449
	return src;
450
    }
451

            
452
402
    status = compositor->acquire (surface);
453
402
    if (unlikely (status)) {
454
	cairo_surface_destroy (src);
455
	cairo_surface_destroy (surface);
456
	return _cairo_surface_create_in_error (status);
457
    }
458

            
459
402
    if (!surface->is_clear) {
460
	cairo_boxes_t clear;
461

            
462
	_cairo_boxes_init_from_rectangle (&clear,
463
					  0, 0,
464
					  extents->bounded.width,
465
					  extents->bounded.height);
466
	status = compositor->fill_boxes (surface,
467
					 CAIRO_OPERATOR_CLEAR,
468
					 CAIRO_COLOR_TRANSPARENT,
469
					 &clear);
470
	if (unlikely (status))
471
	    goto error;
472

            
473
	surface->is_clear = TRUE;
474
    }
475

            
476
402
    if (mask_func) {
477
360
	status = mask_func (compositor, surface, draw_closure,
478
			    CAIRO_OPERATOR_SOURCE, src, src_x, src_y,
479
360
			    extents->bounded.x, extents->bounded.y,
480
360
			    &extents->bounded, extents->clip);
481
360
	if (likely (status == CAIRO_INT_STATUS_SUCCESS)) {
482
360
	    surface->is_clear = FALSE;
483
360
	    goto out;
484
	}
485
	if (unlikely (status != CAIRO_INT_STATUS_UNSUPPORTED))
486
	    goto error;
487
    }
488

            
489
    /* Is it worth setting the clip region here? */
490
42
    status = draw_func (compositor, surface, draw_closure,
491
			CAIRO_OPERATOR_ADD, src, src_x, src_y,
492
42
			extents->bounded.x, extents->bounded.y,
493
			&extents->bounded, NULL);
494
42
    if (unlikely (status))
495
	goto error;
496

            
497
42
    surface->is_clear = FALSE;
498
42
    if (extents->clip->path != NULL) {
499
30
	status = combine_clip_as_traps (compositor, surface,
500
30
					extents->clip, &extents->bounded);
501
30
	if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
502
	    status = _cairo_clip_combine_with_surface (extents->clip, surface,
503
						       extents->bounded.x,
504
						       extents->bounded.y);
505
	}
506
30
	if (unlikely (status))
507
	    goto error;
508
12
    } else if (extents->clip->boxes) {
509
12
	blt_unaligned_boxes(compositor, surface,
510
12
			    extents->bounded.x, extents->bounded.y,
511
12
			    extents->clip->boxes, extents->clip->num_boxes);
512

            
513
    }
514

            
515
out:
516
402
    compositor->release (surface);
517
402
    cairo_surface_destroy (src);
518
402
    return surface;
519

            
520
error:
521
    compositor->release (surface);
522
    if (status != CAIRO_INT_STATUS_NOTHING_TO_DO) {
523
	cairo_surface_destroy (surface);
524
	surface = _cairo_surface_create_in_error (status);
525
    }
526
    cairo_surface_destroy (src);
527
    return surface;
528
}
529

            
530
/* Handles compositing with a clip surface when the operator allows
531
 * us to combine the clip with the mask
532
 */
533
static cairo_status_t
534
27
clip_and_composite_with_mask (const cairo_traps_compositor_t *compositor,
535
			      const cairo_composite_rectangles_t*extents,
536
			      draw_func_t		 draw_func,
537
			      draw_func_t		 mask_func,
538
			      void			*draw_closure,
539
			      cairo_operator_t		 op,
540
			      cairo_surface_t	*src,
541
			      int src_x, int src_y)
542
{
543
27
    cairo_surface_t *dst = extents->surface;
544
    cairo_surface_t *mask;
545

            
546
    TRACE ((stderr, "%s\n", __FUNCTION__));
547

            
548
27
    mask = create_composite_mask (compositor, dst, draw_closure,
549
				  draw_func, mask_func,
550
				  extents);
551
27
    if (unlikely (mask->status))
552
	return mask->status;
553

            
554
27
    if (mask->is_clear)
555
	goto skip;
556

            
557
27
    if (src != NULL || dst->content != CAIRO_CONTENT_ALPHA) {
558
27
	compositor->composite (dst, op, src, mask,
559
27
			       extents->bounded.x + src_x,
560
27
			       extents->bounded.y + src_y,
561
			       0, 0,
562
27
			       extents->bounded.x,      extents->bounded.y,
563
27
			       extents->bounded.width,  extents->bounded.height);
564
    } else {
565
	compositor->composite (dst, op, mask, NULL,
566
			       0, 0,
567
			       0, 0,
568
			       extents->bounded.x,      extents->bounded.y,
569
			       extents->bounded.width,  extents->bounded.height);
570
    }
571

            
572
27
skip:
573
27
    cairo_surface_destroy (mask);
574
27
    return CAIRO_STATUS_SUCCESS;
575
}
576

            
577
/* Handles compositing with a clip surface when we have to do the operation
578
 * in two pieces and combine them together.
579
 */
580
static cairo_status_t
581
162
clip_and_composite_combine (const cairo_traps_compositor_t *compositor,
582
			    const cairo_composite_rectangles_t*extents,
583
			    draw_func_t		 draw_func,
584
			    void			*draw_closure,
585
			    cairo_operator_t		 op,
586
			    cairo_surface_t	*src,
587
			    int src_x, int src_y)
588
{
589
162
    cairo_surface_t *dst = extents->surface;
590
    cairo_surface_t *tmp, *clip;
591
    cairo_status_t status;
592

            
593
    TRACE ((stderr, "%s\n", __FUNCTION__));
594

            
595
162
    tmp = _cairo_surface_create_scratch (dst, dst->content,
596
162
					 extents->bounded.width,
597
162
					 extents->bounded.height,
598
					 NULL);
599
162
    if (unlikely (tmp->status))
600
	return tmp->status;
601

            
602
162
    status = compositor->acquire (tmp);
603
162
    if (unlikely (status)) {
604
	cairo_surface_destroy (tmp);
605
	return status;
606
    }
607

            
608
162
    compositor->composite (tmp,
609
162
			   dst->is_clear ? CAIRO_OPERATOR_CLEAR : CAIRO_OPERATOR_SOURCE,
610
			   dst, NULL,
611
162
			   extents->bounded.x,      extents->bounded.y,
612
			   0, 0,
613
			   0, 0,
614
162
			   extents->bounded.width,  extents->bounded.height);
615

            
616
162
    status = draw_func (compositor, tmp, draw_closure, op,
617
			src, src_x, src_y,
618
162
			extents->bounded.x, extents->bounded.y,
619
			&extents->bounded, NULL);
620

            
621
162
    if (unlikely (status))
622
	goto cleanup;
623

            
624
162
    clip = traps_get_clip_surface (compositor, extents, &extents->bounded);
625
162
    if (unlikely ((status = clip->status)))
626
	goto cleanup;
627

            
628
162
    if (dst->is_clear) {
629
	compositor->composite (dst, CAIRO_OPERATOR_SOURCE, tmp, clip,
630
			       0, 0,
631
			       0, 0,
632
			       extents->bounded.x,      extents->bounded.y,
633
			       extents->bounded.width,  extents->bounded.height);
634
    } else {
635
162
	compositor->lerp (dst, tmp, clip,
636
			  0, 0,
637
			  0,0,
638
162
			  extents->bounded.x,     extents->bounded.y,
639
162
			  extents->bounded.width, extents->bounded.height);
640
    }
641
162
    cairo_surface_destroy (clip);
642

            
643
162
cleanup:
644
162
    compositor->release (tmp);
645
162
    cairo_surface_destroy (tmp);
646

            
647
162
    return status;
648
}
649

            
650
/* Handles compositing for %CAIRO_OPERATOR_SOURCE, which is special; it's
651
 * defined as (src IN mask IN clip) ADD (dst OUT (mask IN clip))
652
 */
653
static cairo_status_t
654
375
clip_and_composite_source (const cairo_traps_compositor_t	*compositor,
655
			   cairo_surface_t			*dst,
656
			   draw_func_t				 draw_func,
657
			   draw_func_t				 mask_func,
658
			   void					*draw_closure,
659
			   cairo_surface_t		*src,
660
			   int src_x,
661
			   int src_y,
662
			   const cairo_composite_rectangles_t	*extents)
663
{
664
    cairo_surface_t *mask;
665

            
666
    TRACE ((stderr, "%s\n", __FUNCTION__));
667

            
668
    /* Create a surface that is mask IN clip */
669
375
    mask = create_composite_mask (compositor, dst, draw_closure,
670
				  draw_func, mask_func,
671
				  extents);
672
375
    if (unlikely (mask->status))
673
	return mask->status;
674

            
675
375
    if (mask->is_clear)
676
	goto skip;
677

            
678
375
    if (dst->is_clear) {
679
3
	compositor->composite (dst, CAIRO_OPERATOR_SOURCE, src, mask,
680
3
			       extents->bounded.x + src_x, extents->bounded.y + src_y,
681
			       0, 0,
682
3
			       extents->bounded.x,      extents->bounded.y,
683
3
			       extents->bounded.width,  extents->bounded.height);
684
    } else {
685
372
	compositor->lerp (dst, src, mask,
686
372
			  extents->bounded.x + src_x, extents->bounded.y + src_y,
687
			  0, 0,
688
372
			  extents->bounded.x,     extents->bounded.y,
689
372
			  extents->bounded.width, extents->bounded.height);
690
    }
691

            
692
375
skip:
693
375
    cairo_surface_destroy (mask);
694

            
695
375
    return CAIRO_STATUS_SUCCESS;
696
}
697

            
698
static cairo_bool_t
699
93
can_reduce_alpha_op (cairo_operator_t op)
700
{
701
93
    int iop = op;
702
93
    switch (iop) {
703
93
    case CAIRO_OPERATOR_OVER:
704
    case CAIRO_OPERATOR_SOURCE:
705
    case CAIRO_OPERATOR_ADD:
706
93
	return TRUE;
707
    default:
708
	return FALSE;
709
    }
710
}
711

            
712
static cairo_bool_t
713
69177
reduce_alpha_op (cairo_composite_rectangles_t *extents)
714
{
715
69177
    cairo_surface_t *dst = extents->surface;
716
69177
    cairo_operator_t op = extents->op;
717
69177
    const cairo_pattern_t *pattern = &extents->source_pattern.base;
718
69294
    return dst->is_clear &&
719
213
	   dst->content == CAIRO_CONTENT_ALPHA &&
720
69390
	   _cairo_pattern_is_opaque_solid (pattern) &&
721
93
	   can_reduce_alpha_op (op);
722
}
723

            
724
static cairo_status_t
725
210
fixup_unbounded_with_mask (const cairo_traps_compositor_t *compositor,
726
			   const cairo_composite_rectangles_t *extents)
727
{
728
210
    cairo_surface_t *dst = extents->surface;
729
    cairo_surface_t *mask;
730

            
731
    TRACE ((stderr, "%s\n", __FUNCTION__));
732

            
733
    /* XXX can we avoid querying the clip surface again? */
734
210
    mask = traps_get_clip_surface (compositor, extents, &extents->unbounded);
735
210
    if (unlikely (mask->status))
736
	return mask->status;
737

            
738
    /* top */
739
210
    if (extents->bounded.y != extents->unbounded.y) {
740
153
	int x = extents->unbounded.x;
741
153
	int y = extents->unbounded.y;
742
153
	int width = extents->unbounded.width;
743
153
	int height = extents->bounded.y - y;
744

            
745
153
	compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
746
			       0, 0,
747
			       0, 0,
748
			       x, y,
749
			       width, height);
750
    }
751

            
752
    /* left */
753
210
    if (extents->bounded.x != extents->unbounded.x) {
754
153
	int x = extents->unbounded.x;
755
153
	int y = extents->bounded.y;
756
153
	int width = extents->bounded.x - x;
757
153
	int height = extents->bounded.height;
758

            
759
153
	compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
760
153
			       0, y - extents->unbounded.y,
761
			       0, 0,
762
			       x, y,
763
			       width, height);
764
    }
765

            
766
    /* right */
767
210
    if (extents->bounded.x + extents->bounded.width != extents->unbounded.x + extents->unbounded.width) {
768
177
	int x = extents->bounded.x + extents->bounded.width;
769
177
	int y = extents->bounded.y;
770
177
	int width = extents->unbounded.x + extents->unbounded.width - x;
771
177
	int height = extents->bounded.height;
772

            
773
177
	compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
774
177
			       x - extents->unbounded.x, y - extents->unbounded.y,
775
			       0, 0,
776
			       x, y,
777
			       width, height);
778
    }
779

            
780
    /* bottom */
781
210
    if (extents->bounded.y + extents->bounded.height != extents->unbounded.y + extents->unbounded.height) {
782
189
	int x = extents->unbounded.x;
783
189
	int y = extents->bounded.y + extents->bounded.height;
784
189
	int width = extents->unbounded.width;
785
189
	int height = extents->unbounded.y + extents->unbounded.height - y;
786

            
787
189
	compositor->composite (dst, CAIRO_OPERATOR_DEST_OUT, mask, NULL,
788
189
			       0, y - extents->unbounded.y,
789
			       0, 0,
790
			       x, y,
791
			       width, height);
792
    }
793

            
794
210
    cairo_surface_destroy (mask);
795

            
796
210
    return CAIRO_STATUS_SUCCESS;
797
}
798

            
799
static void
800
60
add_rect (cairo_boxes_t *boxes, int x1, int y1, int x2, int y2)
801
{
802
    cairo_box_t box;
803
    cairo_int_status_t status;
804

            
805
60
    box.p1.x = _cairo_fixed_from_int (x1);
806
60
    box.p1.y = _cairo_fixed_from_int (y1);
807
60
    box.p2.x = _cairo_fixed_from_int (x2);
808
60
    box.p2.y = _cairo_fixed_from_int (y2);
809

            
810
60
    status = _cairo_boxes_add (boxes, CAIRO_ANTIALIAS_DEFAULT, &box);
811
60
    assert (status == CAIRO_INT_STATUS_SUCCESS);
812
60
}
813

            
814
static cairo_status_t
815
27
fixup_unbounded (const cairo_traps_compositor_t *compositor,
816
		 cairo_composite_rectangles_t *extents,
817
		 cairo_boxes_t *boxes)
818
{
819
27
    cairo_surface_t *dst = extents->surface;
820
    cairo_boxes_t clear, tmp;
821
    cairo_box_t box;
822
    cairo_int_status_t status;
823

            
824
    TRACE ((stderr, "%s\n", __FUNCTION__));
825

            
826
27
    if (extents->bounded.width  == extents->unbounded.width &&
827
9
	extents->bounded.height == extents->unbounded.height)
828
    {
829
9
	return CAIRO_STATUS_SUCCESS;
830
    }
831

            
832
18
    assert (extents->clip->path == NULL);
833

            
834
    /* subtract the drawn boxes from the unbounded area */
835
18
    _cairo_boxes_init (&clear);
836

            
837
18
    box.p1.x = _cairo_fixed_from_int (extents->unbounded.x + extents->unbounded.width);
838
18
    box.p1.y = _cairo_fixed_from_int (extents->unbounded.y);
839
18
    box.p2.x = _cairo_fixed_from_int (extents->unbounded.x);
840
18
    box.p2.y = _cairo_fixed_from_int (extents->unbounded.y + extents->unbounded.height);
841

            
842
18
    if (boxes == NULL) {
843
18
	if (extents->bounded.width == 0 || extents->bounded.height == 0) {
844
	    goto empty;
845
	} else {
846
	    /* top */
847
18
	    if (extents->bounded.y != extents->unbounded.y) {
848
18
		add_rect (&clear,
849
			  extents->unbounded.x, extents->unbounded.y,
850
18
			  extents->unbounded.x + extents->unbounded.width,
851
			  extents->bounded.y);
852
	    }
853
	    /* left */
854
18
	    if (extents->bounded.x != extents->unbounded.x) {
855
18
		add_rect (&clear,
856
			  extents->unbounded.x, extents->bounded.y,
857
			  extents->bounded.x,
858
18
			  extents->bounded.y + extents->bounded.height);
859
	    }
860
	    /* right */
861
18
	    if (extents->bounded.x + extents->bounded.width != extents->unbounded.x + extents->unbounded.width) {
862
6
		add_rect (&clear,
863
6
			  extents->bounded.x + extents->bounded.width,
864
			  extents->bounded.y,
865
6
			  extents->unbounded.x + extents->unbounded.width,
866
6
			  extents->bounded.y + extents->bounded.height);
867
	    }
868
	    /* bottom */
869
18
	    if (extents->bounded.y + extents->bounded.height != extents->unbounded.y + extents->unbounded.height) {
870
18
		add_rect (&clear,
871
			  extents->unbounded.x,
872
18
			  extents->bounded.y + extents->bounded.height,
873
18
			  extents->unbounded.x + extents->unbounded.width,
874
18
			  extents->unbounded.y + extents->unbounded.height);
875
	    }
876
	}
877
    } else if (boxes->num_boxes) {
878
	_cairo_boxes_init (&tmp);
879

            
880
	assert (boxes->is_pixel_aligned);
881

            
882
	status = _cairo_boxes_add (&tmp, CAIRO_ANTIALIAS_DEFAULT, &box);
883
	assert (status == CAIRO_INT_STATUS_SUCCESS);
884

            
885
	tmp.chunks.next = &boxes->chunks;
886
	tmp.num_boxes += boxes->num_boxes;
887

            
888
	status = _cairo_bentley_ottmann_tessellate_boxes (&tmp,
889
							  CAIRO_FILL_RULE_WINDING,
890
							  &clear);
891
	tmp.chunks.next = NULL;
892
	if (unlikely (status))
893
	    goto error;
894
    } else {
895
empty:
896
	box.p1.x = _cairo_fixed_from_int (extents->unbounded.x);
897
	box.p2.x = _cairo_fixed_from_int (extents->unbounded.x + extents->unbounded.width);
898

            
899
	status = _cairo_boxes_add (&clear, CAIRO_ANTIALIAS_DEFAULT, &box);
900
	assert (status == CAIRO_INT_STATUS_SUCCESS);
901
    }
902

            
903
    /* Now intersect with the clip boxes */
904
18
    if (extents->clip->num_boxes) {
905
18
	_cairo_boxes_init_for_array (&tmp,
906
18
				     extents->clip->boxes,
907
18
				     extents->clip->num_boxes);
908
18
	status = _cairo_boxes_intersect (&clear, &tmp, &clear);
909
18
	if (unlikely (status))
910
	    goto error;
911
    }
912

            
913
18
    status = compositor->fill_boxes (dst,
914
				     CAIRO_OPERATOR_CLEAR,
915
				     CAIRO_COLOR_TRANSPARENT,
916
				     &clear);
917

            
918
18
error:
919
18
    _cairo_boxes_fini (&clear);
920
18
    return status;
921
}
922

            
923
enum {
924
    NEED_CLIP_REGION = 0x1,
925
    NEED_CLIP_SURFACE = 0x2,
926
    FORCE_CLIP_REGION = 0x4,
927
};
928

            
929
static cairo_bool_t
930
68985
need_bounded_clip (cairo_composite_rectangles_t *extents)
931
{
932
68985
    unsigned int flags = 0;
933

            
934
68985
    if (extents->clip->num_boxes > 1 ||
935
68943
	extents->mask.width > extents->unbounded.width ||
936
2061
	extents->mask.height > extents->unbounded.height)
937
    {
938
66972
	flags |= NEED_CLIP_REGION;
939
    }
940

            
941
68985
    if (extents->clip->num_boxes > 1 ||
942
68943
	extents->mask.width > extents->bounded.width ||
943
2061
	extents->mask.height > extents->bounded.height)
944
    {
945
66972
	flags |= FORCE_CLIP_REGION;
946
    }
947

            
948
68985
    if (! _cairo_clip_is_region (extents->clip))
949
60
	flags |= NEED_CLIP_SURFACE;
950

            
951
68985
    return flags;
952
}
953

            
954
static cairo_bool_t
955
198
need_unbounded_clip (cairo_composite_rectangles_t *extents)
956
{
957
198
    unsigned int flags = 0;
958
198
    if (! extents->is_bounded) {
959
198
	flags |= NEED_CLIP_REGION;
960
198
	if (! _cairo_clip_is_region (extents->clip))
961
186
	    flags |= NEED_CLIP_SURFACE;
962
    }
963
198
    if (extents->clip->path != NULL)
964
141
	flags |= NEED_CLIP_SURFACE;
965
198
    return flags;
966
}
967

            
968
static cairo_status_t
969
69177
clip_and_composite (const cairo_traps_compositor_t *compositor,
970
		    cairo_composite_rectangles_t *extents,
971
		    draw_func_t		 draw_func,
972
		    draw_func_t		 mask_func,
973
		    void		*draw_closure,
974
		    unsigned int need_clip)
975
{
976
69177
    cairo_surface_t *dst = extents->surface;
977
69177
    cairo_operator_t op = extents->op;
978
69177
    cairo_pattern_t *source = &extents->source_pattern.base;
979
    cairo_surface_t *src;
980
    int src_x, src_y;
981
69177
    cairo_region_t *clip_region = NULL;
982
69177
    cairo_status_t status = CAIRO_STATUS_SUCCESS;
983

            
984
    TRACE ((stderr, "%s\n", __FUNCTION__));
985

            
986
69177
    if (reduce_alpha_op (extents)) {
987
93
	op = CAIRO_OPERATOR_ADD;
988
93
	source = NULL;
989
    }
990

            
991
69177
    if (op == CAIRO_OPERATOR_CLEAR) {
992
9
	op = CAIRO_OPERATOR_DEST_OUT;
993
9
	source = NULL;
994
    }
995

            
996
69177
    compositor->acquire (dst);
997

            
998
69177
    if (need_clip & NEED_CLIP_REGION) {
999
	const cairo_rectangle_int_t *limit;
67167
	if ((need_clip & FORCE_CLIP_REGION) == 0)
111
	    limit = &extents->unbounded;
	else
67056
	    limit = &extents->destination;
67167
	clip_region = _cairo_clip_get_region (extents->clip);
134334
	if (clip_region != NULL &&
67167
	    cairo_region_contains_rectangle (clip_region,
					     limit) == CAIRO_REGION_OVERLAP_IN)
32511
	    clip_region = NULL;
67167
	if (clip_region != NULL) {
34656
	    status = compositor->set_clip_region (dst, clip_region);
34656
	    if (unlikely (status)) {
		compositor->release (dst);
		return status;
	    }
	}
    }
69177
    if (extents->bounded.width == 0 || extents->bounded.height == 0)
48
	goto skip;
69129
    src = compositor->pattern_to_surface (dst, source, FALSE,
69129
					  &extents->bounded,
69129
					  &extents->source_sample_area,
					  &src_x, &src_y);
69129
    if (unlikely (status = src->status))
	goto error;
69129
    if (op == CAIRO_OPERATOR_SOURCE) {
375
	status = clip_and_composite_source (compositor, dst,
					    draw_func, mask_func, draw_closure,
					    src, src_x, src_y,
					    extents);
    } else {
68754
	if (need_clip & NEED_CLIP_SURFACE) {
189
	    if (extents->is_bounded) {
27
		status = clip_and_composite_with_mask (compositor, extents,
						       draw_func, mask_func,
						       draw_closure,
						       op, src, src_x, src_y);
	    } else {
162
		status = clip_and_composite_combine (compositor, extents,
						     draw_func, draw_closure,
						     op, src, src_x, src_y);
	    }
	} else {
68565
	    status = draw_func (compositor,
				dst, draw_closure,
				op, src, src_x, src_y,
				0, 0,
68565
				&extents->bounded,
				extents->clip);
	}
    }
69129
    cairo_surface_destroy (src);
69177
skip:
69177
    if (status == CAIRO_STATUS_SUCCESS && ! extents->is_bounded) {
237
	if (need_clip & NEED_CLIP_SURFACE)
210
	    status = fixup_unbounded_with_mask (compositor, extents);
	else
27
	    status = fixup_unbounded (compositor, extents, NULL);
    }
68940
error:
69177
    if (clip_region)
34656
	compositor->set_clip_region (dst, NULL);
69177
    compositor->release (dst);
69177
    return status;
}
/* meta-ops */
typedef struct {
    cairo_traps_t traps;
    cairo_antialias_t antialias;
} composite_traps_info_t;
static cairo_int_status_t
90
composite_traps (const cairo_traps_compositor_t *compositor,
		 cairo_surface_t		*dst,
		 void				 *closure,
		 cairo_operator_t		 op,
		 cairo_surface_t		*src,
		 int src_x, int src_y,
		 int dst_x, int dst_y,
		 const cairo_rectangle_int_t *extents,
		 cairo_clip_t			*clip)
{
90
    composite_traps_info_t *info = closure;
    TRACE ((stderr, "%s\n", __FUNCTION__));
90
    return compositor->composite_traps (dst, op, src,
					src_x - dst_x, src_y - dst_y,
					dst_x, dst_y,
					extents,
					info->antialias, &info->traps);
}
typedef struct {
    cairo_tristrip_t strip;
    cairo_antialias_t antialias;
} composite_tristrip_info_t;
static cairo_int_status_t
composite_tristrip (const cairo_traps_compositor_t *compositor,
		    cairo_surface_t		*dst,
		    void				 *closure,
		    cairo_operator_t		 op,
		    cairo_surface_t		*src,
		    int src_x, int src_y,
		    int dst_x, int dst_y,
		    const cairo_rectangle_int_t *extents,
		    cairo_clip_t			*clip)
{
    composite_tristrip_info_t *info = closure;
    TRACE ((stderr, "%s\n", __FUNCTION__));
    return compositor->composite_tristrip (dst, op, src,
					   src_x - dst_x, src_y - dst_y,
					   dst_x, dst_y,
					   extents,
					   info->antialias, &info->strip);
}
static cairo_bool_t
24
is_recording_pattern (const cairo_pattern_t *pattern)
{
    cairo_surface_t *surface;
24
    if (pattern->type != CAIRO_PATTERN_TYPE_SURFACE)
24
	return FALSE;
    surface = ((const cairo_surface_pattern_t *) pattern)->surface;
    surface = _cairo_surface_get_source (surface, NULL);
    return _cairo_surface_is_recording (surface);
}
static cairo_surface_t *
recording_pattern_get_surface (const cairo_pattern_t *pattern)
{
    cairo_surface_t *surface;
    surface = ((const cairo_surface_pattern_t *) pattern)->surface;
    return _cairo_surface_get_source (surface, NULL);
}
static cairo_bool_t
24
recording_pattern_contains_sample (const cairo_pattern_t *pattern,
				   const cairo_rectangle_int_t *sample)
{
    cairo_recording_surface_t *surface;
24
    if (! is_recording_pattern (pattern))
24
	return FALSE;
    if (pattern->extend == CAIRO_EXTEND_NONE)
	return TRUE;
    surface = (cairo_recording_surface_t *) recording_pattern_get_surface (pattern);
    if (surface->unbounded)
	return TRUE;
    return _cairo_rectangle_contains_rectangle (&surface->extents, sample);
}
static cairo_bool_t
33
op_reduces_to_source (cairo_composite_rectangles_t *extents)
{
33
    if (extents->op == CAIRO_OPERATOR_SOURCE)
24
	return TRUE;
9
    if (extents->surface->is_clear)
3
	return extents->op == CAIRO_OPERATOR_OVER || extents->op == CAIRO_OPERATOR_ADD;
6
    return FALSE;
}
static cairo_status_t
111
composite_aligned_boxes (const cairo_traps_compositor_t *compositor,
			 cairo_composite_rectangles_t *extents,
			 cairo_boxes_t *boxes)
{
111
    cairo_surface_t *dst = extents->surface;
111
    cairo_operator_t op = extents->op;
111
    cairo_bool_t need_clip_mask = ! _cairo_clip_is_region (extents->clip);
    cairo_bool_t op_is_source;
    cairo_status_t status;
    TRACE ((stderr, "%s\n", __FUNCTION__));
111
    if (need_clip_mask &&
81
	(! extents->is_bounded || extents->op == CAIRO_OPERATOR_SOURCE))
    {
81
	return CAIRO_INT_STATUS_UNSUPPORTED;
    }
30
    op_is_source = op_reduces_to_source (extents);
    /* Are we just copying a recording surface? */
54
    if (! need_clip_mask && op_is_source &&
24
	recording_pattern_contains_sample (&extents->source_pattern.base,
24
					   &extents->source_sample_area))
    {
	cairo_clip_t *recording_clip;
	const cairo_pattern_t *source = &extents->source_pattern.base;
	const cairo_matrix_t *m;
	cairo_matrix_t matrix;
	/* XXX could also do tiling repeat modes... */
	/* first clear the area about to be overwritten */
	if (! dst->is_clear) {
	    status = compositor->acquire (dst);
	    if (unlikely (status))
		return status;
	    status = compositor->fill_boxes (dst,
					     CAIRO_OPERATOR_CLEAR,
					     CAIRO_COLOR_TRANSPARENT,
					     boxes);
	    compositor->release (dst);
	    if (unlikely (status))
		return status;
	}
	m = &source->matrix;
	if (_cairo_surface_has_device_transform (dst)) {
	    cairo_matrix_multiply (&matrix,
				   &source->matrix,
				   &dst->device_transform);
	    m = &matrix;
	}
	recording_clip = _cairo_clip_from_boxes (boxes);
	status = _cairo_recording_surface_replay_with_clip (recording_pattern_get_surface (source),
							    m, dst, recording_clip);
	_cairo_clip_destroy (recording_clip);
	return status;
    }
30
    status = compositor->acquire (dst);
30
    if (unlikely (status))
	return status;
30
    if (! need_clip_mask &&
24
	(op == CAIRO_OPERATOR_CLEAR ||
24
	 extents->source_pattern.base.type == CAIRO_PATTERN_TYPE_SOLID))
30
    {
	const cairo_color_t *color;
30
	if (op == CAIRO_OPERATOR_CLEAR) {
6
	    color = CAIRO_COLOR_TRANSPARENT;
	} else {
24
	    color = &((cairo_solid_pattern_t *) &extents->source_pattern)->color;
24
	    if (op_is_source)
24
		op = CAIRO_OPERATOR_SOURCE;
	}
30
	status = compositor->fill_boxes (dst, op, color, boxes);
    }
    else
    {
	cairo_surface_t *src, *mask = NULL;
	cairo_pattern_t *source = &extents->source_pattern.base;
	int src_x, src_y;
	int mask_x = 0, mask_y = 0;
	if (need_clip_mask) {
	    mask = traps_get_clip_surface (compositor,
					   extents, &extents->bounded);
	    if (unlikely (mask->status))
		return mask->status;
	    mask_x = -extents->bounded.x;
	    mask_y = -extents->bounded.y;
	    if (op == CAIRO_OPERATOR_CLEAR) {
		source = NULL;
		op = CAIRO_OPERATOR_DEST_OUT;
	    }
	} else if (op_is_source)
	    op = CAIRO_OPERATOR_SOURCE;
	src = compositor->pattern_to_surface (dst, source, FALSE,
					      &extents->bounded,
					      &extents->source_sample_area,
					      &src_x, &src_y);
	if (likely (src->status == CAIRO_STATUS_SUCCESS)) {
	    status = compositor->composite_boxes (dst, op, src, mask,
						  src_x, src_y,
						  mask_x, mask_y,
						  0, 0,
						  boxes, &extents->bounded);
	    cairo_surface_destroy (src);
	} else
	    status = src->status;
	cairo_surface_destroy (mask);
    }
30
    if (status == CAIRO_STATUS_SUCCESS && ! extents->is_bounded)
	status = fixup_unbounded (compositor, extents, boxes);
30
    compositor->release (dst);
30
    return status;
}
static cairo_status_t
3
upload_boxes (const cairo_traps_compositor_t *compositor,
	      cairo_composite_rectangles_t *extents,
	      cairo_boxes_t *boxes)
{
3
    cairo_surface_t *dst = extents->surface;
3
    const cairo_pattern_t *source = &extents->source_pattern.base;
    cairo_surface_t *src;
    cairo_rectangle_int_t limit;
    cairo_int_status_t status;
    int tx, ty;
    TRACE ((stderr, "%s\n", __FUNCTION__));
3
    src = _cairo_pattern_get_source((cairo_surface_pattern_t *)source,
				    &limit);
3
    if (!(src->type == CAIRO_SURFACE_TYPE_IMAGE || src->type == dst->type))
	return CAIRO_INT_STATUS_UNSUPPORTED;
3
    if (! _cairo_matrix_is_integer_translation (&source->matrix, &tx, &ty))
	return CAIRO_INT_STATUS_UNSUPPORTED;
    /* Check that the data is entirely within the image */
3
    if (extents->bounded.x + tx < limit.x || extents->bounded.y + ty < limit.y)
	return CAIRO_INT_STATUS_UNSUPPORTED;
3
    if (extents->bounded.x + extents->bounded.width  + tx > limit.x + limit.width ||
3
	extents->bounded.y + extents->bounded.height + ty > limit.y + limit.height)
	return CAIRO_INT_STATUS_UNSUPPORTED;
3
    tx += limit.x;
3
    ty += limit.y;
3
    if (src->type == CAIRO_SURFACE_TYPE_IMAGE)
	status = compositor->draw_image_boxes (dst,
					       (cairo_image_surface_t *)src,
					       boxes, tx, ty);
    else
3
	status = compositor->copy_boxes (dst, src, boxes, &extents->bounded,
					 tx, ty);
3
    return status;
}
static cairo_int_status_t
135
trim_extents_to_traps (cairo_composite_rectangles_t *extents,
		       cairo_traps_t *traps)
{
    cairo_box_t box;
135
    _cairo_traps_extents (traps, &box);
135
    return _cairo_composite_rectangles_intersect_mask_extents (extents, &box);
}
static cairo_int_status_t
trim_extents_to_tristrip (cairo_composite_rectangles_t *extents,
			  cairo_tristrip_t *strip)
{
    cairo_box_t box;
    _cairo_tristrip_extents (strip, &box);
    return _cairo_composite_rectangles_intersect_mask_extents (extents, &box);
}
static cairo_int_status_t
144
trim_extents_to_boxes (cairo_composite_rectangles_t *extents,
		       cairo_boxes_t *boxes)
{
    cairo_box_t box;
144
    _cairo_boxes_extents (boxes, &box);
144
    return _cairo_composite_rectangles_intersect_mask_extents (extents, &box);
}
static cairo_int_status_t
129
boxes_for_traps (cairo_boxes_t *boxes,
		 cairo_traps_t *traps,
		 cairo_antialias_t antialias)
{
    int i, j;
    /* first check that the traps are rectilinear */
129
    if (antialias == CAIRO_ANTIALIAS_NONE) {
	for (i = 0; i < traps->num_traps; i++) {
	    const cairo_trapezoid_t *t = &traps->traps[i];
	    if (_cairo_fixed_integer_round_down (t->left.p1.x) !=
		_cairo_fixed_integer_round_down (t->left.p2.x) ||
		_cairo_fixed_integer_round_down (t->right.p1.x) !=
		_cairo_fixed_integer_round_down (t->right.p2.x))
	    {
		return CAIRO_INT_STATUS_UNSUPPORTED;
	    }
	}
    } else {
132
	for (i = 0; i < traps->num_traps; i++) {
84
	    const cairo_trapezoid_t *t = &traps->traps[i];
84
	    if (t->left.p1.x != t->left.p2.x || t->right.p1.x != t->right.p2.x)
81
		return CAIRO_INT_STATUS_UNSUPPORTED;
	}
    }
48
    _cairo_boxes_init (boxes);
48
    boxes->chunks.base  = (cairo_box_t *) traps->traps;
48
    boxes->chunks.size  = traps->num_traps;
48
    if (antialias != CAIRO_ANTIALIAS_NONE) {
48
	for (i = j = 0; i < traps->num_traps; i++) {
	    /* Note the traps and boxes alias so we need to take the local copies first. */
	    cairo_fixed_t x1 = traps->traps[i].left.p1.x;
	    cairo_fixed_t x2 = traps->traps[i].right.p1.x;
	    cairo_fixed_t y1 = traps->traps[i].top;
	    cairo_fixed_t y2 = traps->traps[i].bottom;
	    if (x1 == x2 || y1 == y2)
		    continue;
	    boxes->chunks.base[j].p1.x = x1;
	    boxes->chunks.base[j].p1.y = y1;
	    boxes->chunks.base[j].p2.x = x2;
	    boxes->chunks.base[j].p2.y = y2;
	    j++;
	    if (boxes->is_pixel_aligned) {
		boxes->is_pixel_aligned =
		    _cairo_fixed_is_integer (x1) && _cairo_fixed_is_integer (y1) &&
		    _cairo_fixed_is_integer (x2) && _cairo_fixed_is_integer (y2);
	    }
	}
    } else {
	boxes->is_pixel_aligned = TRUE;
	for (i = j = 0; i < traps->num_traps; i++) {
	    /* Note the traps and boxes alias so we need to take the local copies first. */
	    cairo_fixed_t x1 = traps->traps[i].left.p1.x;
	    cairo_fixed_t x2 = traps->traps[i].right.p1.x;
	    cairo_fixed_t y1 = traps->traps[i].top;
	    cairo_fixed_t y2 = traps->traps[i].bottom;
	    /* round down here to match Pixman's behavior when using traps. */
	    boxes->chunks.base[j].p1.x = _cairo_fixed_round_down (x1);
	    boxes->chunks.base[j].p1.y = _cairo_fixed_round_down (y1);
	    boxes->chunks.base[j].p2.x = _cairo_fixed_round_down (x2);
	    boxes->chunks.base[j].p2.y = _cairo_fixed_round_down (y2);
	    j += (boxes->chunks.base[j].p1.x != boxes->chunks.base[j].p2.x &&
		  boxes->chunks.base[j].p1.y != boxes->chunks.base[j].p2.y);
	}
    }
48
    boxes->chunks.count = j;
48
    boxes->num_boxes    = j;
48
    return CAIRO_INT_STATUS_SUCCESS;
}
static cairo_status_t
clip_and_composite_boxes (const cairo_traps_compositor_t *compositor,
			  cairo_composite_rectangles_t *extents,
			  cairo_boxes_t *boxes);
static cairo_status_t
129
clip_and_composite_polygon (const cairo_traps_compositor_t *compositor,
			    cairo_composite_rectangles_t *extents,
			    cairo_polygon_t *polygon,
			    cairo_antialias_t antialias,
			    cairo_fill_rule_t fill_rule,
			    cairo_bool_t curvy)
{
    composite_traps_info_t traps;
129
    cairo_surface_t *dst = extents->surface;
129
    cairo_bool_t clip_surface = ! _cairo_clip_is_region (extents->clip);
    cairo_int_status_t status;
    TRACE ((stderr, "%s\n", __FUNCTION__));
129
    if (polygon->num_edges == 0) {
	status = CAIRO_INT_STATUS_SUCCESS;
	if (! extents->is_bounded) {
	    cairo_region_t *clip_region = _cairo_clip_get_region (extents->clip);
	    if (clip_region &&
		cairo_region_contains_rectangle (clip_region,
						 &extents->unbounded) == CAIRO_REGION_OVERLAP_IN)
		clip_region = NULL;
	    if (clip_region != NULL) {
		status = compositor->set_clip_region (dst, clip_region);
		if (unlikely (status))
		    return status;
	    }
	    if (clip_surface)
		status = fixup_unbounded_with_mask (compositor, extents);
	    else
		status = fixup_unbounded (compositor, extents, NULL);
	    if (clip_region != NULL)
		compositor->set_clip_region (dst, NULL);
	}
	return status;
    }
129
    if (extents->clip->path != NULL && extents->is_bounded) {
	cairo_polygon_t clipper;
	cairo_fill_rule_t clipper_fill_rule;
	cairo_antialias_t clipper_antialias;
	status = _cairo_clip_get_polygon (extents->clip,
					  &clipper,
					  &clipper_fill_rule,
					  &clipper_antialias);
	if (likely (status == CAIRO_INT_STATUS_SUCCESS)) {
	    if (clipper_antialias == antialias) {
		status = _cairo_polygon_intersect (polygon, fill_rule,
						   &clipper, clipper_fill_rule);
		if (likely (status == CAIRO_INT_STATUS_SUCCESS)) {
		    cairo_clip_t * clip = _cairo_clip_copy_region (extents->clip);
		    _cairo_clip_destroy (extents->clip);
		    extents->clip = clip;
		    fill_rule = CAIRO_FILL_RULE_WINDING;
		}
		_cairo_polygon_fini (&clipper);
	    }
	}
    }
129
    if (antialias == CAIRO_ANTIALIAS_NONE && curvy) {
	cairo_boxes_t boxes;
	_cairo_boxes_init (&boxes);
	status = _cairo_rasterise_polygon_to_boxes (polygon, fill_rule, &boxes);
	if (likely (status == CAIRO_INT_STATUS_SUCCESS)) {
	    assert (boxes.is_pixel_aligned);
	    status = clip_and_composite_boxes (compositor, extents, &boxes);
	}
	_cairo_boxes_fini (&boxes);
	if ((status != CAIRO_INT_STATUS_UNSUPPORTED))
	    return status;
    }
129
    _cairo_traps_init (&traps.traps);
129
    if (antialias == CAIRO_ANTIALIAS_NONE && curvy) {
	status = _cairo_rasterise_polygon_to_traps (polygon, fill_rule, antialias, &traps.traps);
    } else {
129
	status = _cairo_bentley_ottmann_tessellate_polygon (&traps.traps, polygon, fill_rule);
    }
129
    if (unlikely (status))
	goto CLEANUP_TRAPS;
129
    status = trim_extents_to_traps (extents, &traps.traps);
129
    if (unlikely (status))
	goto CLEANUP_TRAPS;
    /* Use a fast path if the trapezoids consist of a set of boxes.  */
129
    status = CAIRO_INT_STATUS_UNSUPPORTED;
    if (1) {
	cairo_boxes_t boxes;
129
	status = boxes_for_traps (&boxes, &traps.traps, antialias);
129
	if (status == CAIRO_INT_STATUS_SUCCESS) {
48
	    status = clip_and_composite_boxes (compositor, extents, &boxes);
	    /* XXX need to reconstruct the traps! */
48
	    assert (status != CAIRO_INT_STATUS_UNSUPPORTED);
	}
    }
129
    if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
	/* Otherwise render the trapezoids to a mask and composite in the usual
	 * fashion.
	 */
81
	unsigned int flags = 0;
	/* For unbounded operations, the X11 server will estimate the
	 * affected rectangle and apply the operation to that. However,
	 * there are cases where this is an overestimate (e.g. the
	 * clip-fill-{eo,nz}-unbounded test).
	 *
	 * The clip will trim that overestimate to our expectations.
	 */
81
	if (! extents->is_bounded)
81
	    flags |= FORCE_CLIP_REGION;
81
	traps.antialias = antialias;
81
	status = clip_and_composite (compositor, extents,
				     composite_traps, NULL, &traps,
81
				     need_unbounded_clip (extents) | flags);
    }
48
CLEANUP_TRAPS:
129
    _cairo_traps_fini (&traps.traps);
129
    return status;
}
struct composite_opacity_info {
    const cairo_traps_compositor_t *compositor;
    uint8_t op;
    cairo_surface_t *dst;
    cairo_surface_t *src;
    int src_x, src_y;
    double opacity;
};
static void composite_opacity(void *closure,
			      int16_t x, int16_t y,
			      int16_t w, int16_t h,
			      uint16_t coverage)
{
    struct composite_opacity_info *info = closure;
    const cairo_traps_compositor_t *compositor = info->compositor;
    cairo_surface_t *mask;
    int mask_x, mask_y;
    cairo_color_t color;
    cairo_solid_pattern_t solid;
    _cairo_color_init_rgba (&color, 0, 0, 0, info->opacity * coverage);
    _cairo_pattern_init_solid (&solid, &color);
    mask = compositor->pattern_to_surface (info->dst, &solid.base, TRUE,
					   &_cairo_unbounded_rectangle,
					   &_cairo_unbounded_rectangle,
					   &mask_x, &mask_y);
    if (likely (mask->status == CAIRO_STATUS_SUCCESS)) {
	if (info->src) {
	    compositor->composite (info->dst, info->op, info->src, mask,
				   x + info->src_x,  y + info->src_y,
				   mask_x,           mask_y,
				   x,                y,
				   w,                h);
	} else {
	    compositor->composite (info->dst, info->op, mask, NULL,
				   mask_x,            mask_y,
				   0,                 0,
				   x,                 y,
				   w,                 h);
	}
    }
    cairo_surface_destroy (mask);
}
static cairo_int_status_t
composite_opacity_boxes (const cairo_traps_compositor_t *compositor,
			 cairo_surface_t		*dst,
			 void				*closure,
			 cairo_operator_t		 op,
			 cairo_surface_t		*src,
			 int				 src_x,
			 int				 src_y,
			 int				 dst_x,
			 int				 dst_y,
			 const cairo_rectangle_int_t	*extents,
			 cairo_clip_t			*clip)
{
    const cairo_solid_pattern_t *mask = closure;
    struct composite_opacity_info info;
    int i;
    TRACE ((stderr, "%s\n", __FUNCTION__));
    info.compositor = compositor;
    info.op = op;
    info.dst = dst;
    info.src = src;
    info.src_x = src_x;
    info.src_y = src_y;
    info.opacity = mask->color.alpha / (double) 0xffff;
    /* XXX for lots of boxes create a clip region for the fully opaque areas */
    for (i = 0; i < clip->num_boxes; i++)
	do_unaligned_box(composite_opacity, &info,
			 &clip->boxes[i], dst_x, dst_y);
    return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
63
composite_boxes (const cairo_traps_compositor_t *compositor,
		 cairo_surface_t		*dst,
		 void				*closure,
		 cairo_operator_t		 op,
		 cairo_surface_t		*src,
		 int				 src_x,
		 int				 src_y,
		 int				 dst_x,
		 int				 dst_y,
		 const cairo_rectangle_int_t	*extents,
		 cairo_clip_t			*clip)
{
    cairo_traps_t traps;
    cairo_status_t status;
    TRACE ((stderr, "%s\n", __FUNCTION__));
63
    status = _cairo_traps_init_boxes (&traps, closure);
63
    if (unlikely (status))
	return status;
63
    status = compositor->composite_traps (dst, op, src,
					  src_x - dst_x, src_y - dst_y,
					  dst_x, dst_y,
					  extents,
					  CAIRO_ANTIALIAS_DEFAULT, &traps);
63
    _cairo_traps_fini (&traps);
63
    return status;
}
static cairo_status_t
144
clip_and_composite_boxes (const cairo_traps_compositor_t *compositor,
			  cairo_composite_rectangles_t *extents,
			  cairo_boxes_t *boxes)
{
    cairo_int_status_t status;
    TRACE ((stderr, "%s\n", __FUNCTION__));
144
    if (boxes->num_boxes == 0 && extents->is_bounded)
	return CAIRO_STATUS_SUCCESS;
144
    status = trim_extents_to_boxes (extents, boxes);
144
    if (unlikely (status))
	return status;
144
    if (boxes->is_pixel_aligned && extents->clip->path == NULL &&
51
	extents->source_pattern.base.type == CAIRO_PATTERN_TYPE_SURFACE &&
3
	(op_reduces_to_source (extents) ||
	 (extents->op == CAIRO_OPERATOR_OVER &&
	  (extents->source_pattern.surface.surface->content & CAIRO_CONTENT_ALPHA) == 0)))
    {
3
	status = upload_boxes (compositor, extents, boxes);
3
	if (status != CAIRO_INT_STATUS_UNSUPPORTED)
3
	    return status;
    }
    /* Can we reduce drawing through a clip-mask to simply drawing the clip? */
141
    if (extents->clip->path != NULL && extents->is_bounded) {
	cairo_polygon_t polygon;
	cairo_fill_rule_t fill_rule;
	cairo_antialias_t antialias;
	cairo_clip_t *clip;
	clip = _cairo_clip_copy (extents->clip);
	clip = _cairo_clip_intersect_boxes (clip, boxes);
	if (_cairo_clip_is_all_clipped (clip))
	    return CAIRO_INT_STATUS_NOTHING_TO_DO;
	status = _cairo_clip_get_polygon (clip, &polygon,
					  &fill_rule, &antialias);
	_cairo_clip_path_destroy (clip->path);
	clip->path = NULL;
	if (likely (status == CAIRO_INT_STATUS_SUCCESS)) {
	    cairo_clip_t *saved_clip = extents->clip;
	    extents->clip = clip;
	    status = clip_and_composite_polygon (compositor, extents, &polygon,
						 antialias, fill_rule, FALSE);
	    clip = extents->clip;
	    extents->clip = saved_clip;
	    _cairo_polygon_fini (&polygon);
	}
	_cairo_clip_destroy (clip);
	if (status != CAIRO_INT_STATUS_UNSUPPORTED)
	    return status;
    }
    /* Use a fast path if the boxes are pixel aligned (or nearly aligned!) */
141
    if (boxes->is_pixel_aligned) {
111
	status = composite_aligned_boxes (compositor, extents, boxes);
111
	if (status != CAIRO_INT_STATUS_UNSUPPORTED)
30
	    return status;
    }
111
    return clip_and_composite (compositor, extents,
			       composite_boxes, NULL, boxes,
111
			       need_unbounded_clip (extents));
}
static cairo_int_status_t
3
composite_traps_as_boxes (const cairo_traps_compositor_t *compositor,
			  cairo_composite_rectangles_t *extents,
			  composite_traps_info_t *info)
{
    cairo_boxes_t boxes;
    TRACE ((stderr, "%s\n", __FUNCTION__));
3
    if (! _cairo_traps_to_boxes (&info->traps, info->antialias, &boxes))
3
	return CAIRO_INT_STATUS_UNSUPPORTED;
    return clip_and_composite_boxes (compositor, extents, &boxes);
}
static cairo_int_status_t
6
clip_and_composite_traps (const cairo_traps_compositor_t *compositor,
			  cairo_composite_rectangles_t *extents,
			  composite_traps_info_t *info,
			  unsigned flags)
{
    cairo_int_status_t status;
    TRACE ((stderr, "%s\n", __FUNCTION__));
6
    status = trim_extents_to_traps (extents, &info->traps);
6
    if (unlikely (status != CAIRO_INT_STATUS_SUCCESS))
	return status;
6
    status = CAIRO_INT_STATUS_UNSUPPORTED;
6
    if ((flags & FORCE_CLIP_REGION) == 0)
3
	status = composite_traps_as_boxes (compositor, extents, info);
6
    if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
	/* For unbounded operations, the X11 server will estimate the
	 * affected rectangle and apply the operation to that. However,
	 * there are cases where this is an overestimate (e.g. the
	 * clip-fill-{eo,nz}-unbounded test).
	 *
	 * The clip will trim that overestimate to our expectations.
	 */
6
	if (! extents->is_bounded)
6
	    flags |= FORCE_CLIP_REGION;
6
	status = clip_and_composite (compositor, extents,
				     composite_traps, NULL, info,
6
				     need_unbounded_clip (extents) | flags);
    }
6
    return status;
}
static cairo_int_status_t
clip_and_composite_tristrip (const cairo_traps_compositor_t *compositor,
			     cairo_composite_rectangles_t *extents,
			     composite_tristrip_info_t *info)
{
    cairo_int_status_t status;
    unsigned int flags = 0;
    TRACE ((stderr, "%s\n", __FUNCTION__));
    status = trim_extents_to_tristrip (extents, &info->strip);
    if (unlikely (status != CAIRO_INT_STATUS_SUCCESS))
	return status;
    if (! extents->is_bounded)
	flags |= FORCE_CLIP_REGION;
    status = clip_and_composite (compositor, extents,
				 composite_tristrip, NULL, info,
				 need_unbounded_clip (extents) | flags);
    return status;
}
struct composite_mask {
    cairo_surface_t *mask;
    int mask_x, mask_y;
};
static cairo_int_status_t
12
composite_mask (const cairo_traps_compositor_t *compositor,
		cairo_surface_t			*dst,
		void				*closure,
		cairo_operator_t		 op,
		cairo_surface_t			*src,
		int				 src_x,
		int				 src_y,
		int				 dst_x,
		int				 dst_y,
		const cairo_rectangle_int_t	*extents,
		cairo_clip_t			*clip)
{
12
    struct composite_mask *data = closure;
    TRACE ((stderr, "%s\n", __FUNCTION__));
12
    if (src != NULL) {
12
	compositor->composite (dst, op, src, data->mask,
12
			       extents->x + src_x, extents->y + src_y,
12
			       extents->x + data->mask_x, extents->y + data->mask_y,
12
			       extents->x - dst_x,  extents->y - dst_y,
12
			       extents->width,      extents->height);
    } else {
	compositor->composite (dst, op, data->mask, NULL,
			       extents->x + data->mask_x, extents->y + data->mask_y,
			       0, 0,
			       extents->x - dst_x,  extents->y - dst_y,
			       extents->width,      extents->height);
    }
12
    return CAIRO_STATUS_SUCCESS;
}
struct composite_box_info {
    const cairo_traps_compositor_t *compositor;
    cairo_surface_t *dst;
    cairo_surface_t *src;
    int src_x, src_y;
    uint8_t op;
};
465
static void composite_box(void *closure,
			  int16_t x, int16_t y,
			  int16_t w, int16_t h,
			  uint16_t coverage)
{
465
    struct composite_box_info *info = closure;
465
    const cairo_traps_compositor_t *compositor = info->compositor;
    TRACE ((stderr, "%s\n", __FUNCTION__));
465
    if (! CAIRO_ALPHA_SHORT_IS_OPAQUE (coverage)) {
	cairo_surface_t *mask;
	cairo_color_t color;
	cairo_solid_pattern_t solid;
	int mask_x, mask_y;
	_cairo_color_init_rgba (&color, 0, 0, 0, coverage / (double)0xffff);
	_cairo_pattern_init_solid (&solid, &color);
	mask = compositor->pattern_to_surface (info->dst, &solid.base, FALSE,
					       &_cairo_unbounded_rectangle,
					       &_cairo_unbounded_rectangle,
					       &mask_x, &mask_y);
	if (likely (mask->status == CAIRO_STATUS_SUCCESS)) {
	    compositor->composite (info->dst, info->op, info->src, mask,
				   x + info->src_x,  y + info->src_y,
				   mask_x,           mask_y,
				   x,                y,
				   w,                h);
	}
	cairo_surface_destroy (mask);
    } else {
465
	compositor->composite (info->dst, info->op, info->src, NULL,
465
			       x + info->src_x,  y + info->src_y,
			       0,                0,
			       x,                y,
			       w,                h);
    }
465
}
static cairo_int_status_t
357
composite_mask_clip_boxes (const cairo_traps_compositor_t *compositor,
			   cairo_surface_t		*dst,
			   void				*closure,
			   cairo_operator_t		 op,
			   cairo_surface_t		*src,
			   int				 src_x,
			   int				 src_y,
			   int				 dst_x,
			   int				 dst_y,
			   const cairo_rectangle_int_t	*extents,
			   cairo_clip_t			*clip)
{
357
    struct composite_mask *data = closure;
    struct composite_box_info info;
    int i;
    TRACE ((stderr, "%s\n", __FUNCTION__));
357
    info.compositor = compositor;
357
    info.op = CAIRO_OPERATOR_SOURCE;
357
    info.dst = dst;
357
    info.src = data->mask;
357
    info.src_x = data->mask_x;
357
    info.src_y = data->mask_y;
357
    info.src_x += dst_x;
357
    info.src_y += dst_y;
822
    for (i = 0; i < clip->num_boxes; i++)
465
	do_unaligned_box(composite_box, &info, &clip->boxes[i], dst_x, dst_y);
357
    return CAIRO_STATUS_SUCCESS;
}
static cairo_int_status_t
3
composite_mask_clip (const cairo_traps_compositor_t *compositor,
		     cairo_surface_t			*dst,
		     void				*closure,
		     cairo_operator_t			 op,
		     cairo_surface_t			*src,
		     int				 src_x,
		     int				 src_y,
		     int				 dst_x,
		     int				 dst_y,
		     const cairo_rectangle_int_t	*extents,
		     cairo_clip_t			*clip)
{
3
    struct composite_mask *data = closure;
    cairo_polygon_t polygon;
    cairo_fill_rule_t fill_rule;
    composite_traps_info_t info;
    cairo_status_t status;
    TRACE ((stderr, "%s\n", __FUNCTION__));
3
    status = _cairo_clip_get_polygon (clip, &polygon,
				      &fill_rule, &info.antialias);
3
    if (unlikely (status))
	return status;
3
    _cairo_traps_init (&info.traps);
3
    status = _cairo_bentley_ottmann_tessellate_polygon (&info.traps,
							&polygon,
							fill_rule);
3
    _cairo_polygon_fini (&polygon);
3
    if (unlikely (status))
	return status;
3
    status = composite_traps (compositor, dst, &info,
			      CAIRO_OPERATOR_SOURCE,
			      data->mask,
3
			      data->mask_x + dst_x, data->mask_y + dst_y,
			      dst_x, dst_y,
			      extents, NULL);
3
    _cairo_traps_fini (&info.traps);
3
    return status;
}
/* high-level compositor interface */
static cairo_int_status_t
9
_cairo_traps_compositor_paint (const cairo_compositor_t *_compositor,
			       cairo_composite_rectangles_t *extents)
{
9
    cairo_traps_compositor_t *compositor = (cairo_traps_compositor_t*)_compositor;
    cairo_boxes_t boxes;
    cairo_int_status_t status;
    TRACE ((stderr, "%s\n", __FUNCTION__));
9
    status = compositor->check_composite (extents);
9
    if (unlikely (status))
	return status;
9
     _cairo_clip_steal_boxes (extents->clip, &boxes);
9
     status = clip_and_composite_boxes (compositor, extents, &boxes);
9
     _cairo_clip_unsteal_boxes (extents->clip, &boxes);
9
    return status;
}
static cairo_int_status_t
372
_cairo_traps_compositor_mask (const cairo_compositor_t *_compositor,
			      cairo_composite_rectangles_t *extents)
{
372
    const cairo_traps_compositor_t *compositor = (cairo_traps_compositor_t*)_compositor;
    cairo_int_status_t status;
    TRACE ((stderr, "%s\n", __FUNCTION__));
372
    status = compositor->check_composite (extents);
372
    if (unlikely (status))
	return status;
372
    if (extents->mask_pattern.base.type == CAIRO_PATTERN_TYPE_SOLID &&
	extents->clip->path == NULL) {
	status = clip_and_composite (compositor, extents,
				     composite_opacity_boxes,
				     composite_opacity_boxes,
				     &extents->mask_pattern,
				     need_unbounded_clip (extents));
    } else {
	struct composite_mask data;
744
	data.mask = compositor->pattern_to_surface (extents->surface,
372
						    &extents->mask_pattern.base,
						    TRUE,
372
						    &extents->bounded,
372
						    &extents->mask_sample_area,
						    &data.mask_x,
						    &data.mask_y);
372
	if (unlikely (data.mask->status))
	    return data.mask->status;
372
	status = clip_and_composite (compositor, extents,
				     composite_mask,
372
				     extents->clip->path ? composite_mask_clip : composite_mask_clip_boxes,
372
				     &data, need_bounded_clip (extents));
372
	cairo_surface_destroy (data.mask);
    }
372
    return status;
}
static cairo_int_status_t
6
_cairo_traps_compositor_stroke (const cairo_compositor_t *_compositor,
				cairo_composite_rectangles_t *extents,
				const cairo_path_fixed_t *path,
				const cairo_stroke_style_t *style,
				const cairo_matrix_t	*ctm,
				const cairo_matrix_t	*ctm_inverse,
				double			 tolerance,
				cairo_antialias_t	 antialias)
{
6
    const cairo_traps_compositor_t *compositor = (cairo_traps_compositor_t *)_compositor;
    cairo_int_status_t status;
    TRACE ((stderr, "%s\n", __FUNCTION__));
6
    status = compositor->check_composite (extents);
6
    if (unlikely (status))
	return status;
6
    status = CAIRO_INT_STATUS_UNSUPPORTED;
6
    if (_cairo_path_fixed_stroke_is_rectilinear (path)) {
	cairo_boxes_t boxes;
	_cairo_boxes_init_with_clip (&boxes, extents->clip);
	status = _cairo_path_fixed_stroke_rectilinear_to_boxes (path,
								style,
								ctm,
								antialias,
								&boxes);
	if (likely (status == CAIRO_INT_STATUS_SUCCESS))
	    status = clip_and_composite_boxes (compositor, extents, &boxes);
	_cairo_boxes_fini (&boxes);
    }
    if (status == CAIRO_INT_STATUS_UNSUPPORTED && 0 &&
	_cairo_clip_is_region (extents->clip)) /* XXX */
    {
	composite_tristrip_info_t info;
	info.antialias = antialias;
	_cairo_tristrip_init_with_clip (&info.strip, extents->clip);
	status = _cairo_path_fixed_stroke_to_tristrip (path, style,
						       ctm, ctm_inverse,
						       tolerance,
						       &info.strip);
	if (likely (status == CAIRO_INT_STATUS_SUCCESS))
	    status = clip_and_composite_tristrip (compositor, extents, &info);
	_cairo_tristrip_fini (&info.strip);
    }
6
    if (status == CAIRO_INT_STATUS_UNSUPPORTED &&
6
	path->has_curve_to && antialias == CAIRO_ANTIALIAS_NONE) {
	cairo_polygon_t polygon;
	_cairo_polygon_init_with_clip (&polygon, extents->clip);
	status = _cairo_path_fixed_stroke_to_polygon (path, style,
						      ctm, ctm_inverse,
						      tolerance,
						      &polygon);
	if (likely (status == CAIRO_INT_STATUS_SUCCESS))
	    status = clip_and_composite_polygon (compositor,
						 extents, &polygon,
						 CAIRO_ANTIALIAS_NONE,
						 CAIRO_FILL_RULE_WINDING,
						 TRUE);
	_cairo_polygon_fini (&polygon);
    }
6
    if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
	cairo_int_status_t (*func) (const cairo_path_fixed_t	*path,
				    const cairo_stroke_style_t	*stroke_style,
				    const cairo_matrix_t	*ctm,
				    const cairo_matrix_t	*ctm_inverse,
				    double			 tolerance,
				    cairo_traps_t		*traps);
	composite_traps_info_t info;
	unsigned flags;
6
	if (antialias == CAIRO_ANTIALIAS_BEST || antialias == CAIRO_ANTIALIAS_GOOD) {
	    func = _cairo_path_fixed_stroke_polygon_to_traps;
	    flags = 0;
	} else {
6
	    func = _cairo_path_fixed_stroke_to_traps;
6
	    flags = need_bounded_clip (extents) & ~NEED_CLIP_SURFACE;
	}
6
	info.antialias = antialias;
6
	_cairo_traps_init_with_clip (&info.traps, extents->clip);
6
	status = func (path, style, ctm, ctm_inverse, tolerance, &info.traps);
6
	if (likely (status == CAIRO_INT_STATUS_SUCCESS))
6
	    status = clip_and_composite_traps (compositor, extents, &info, flags);
6
	_cairo_traps_fini (&info.traps);
    }
6
    return status;
}
static cairo_int_status_t
216
_cairo_traps_compositor_fill (const cairo_compositor_t *_compositor,
			      cairo_composite_rectangles_t *extents,
			      const cairo_path_fixed_t	*path,
			      cairo_fill_rule_t		 fill_rule,
			      double			 tolerance,
			      cairo_antialias_t		 antialias)
{
216
    const cairo_traps_compositor_t *compositor = (cairo_traps_compositor_t *)_compositor;
    cairo_int_status_t status;
    TRACE ((stderr, "%s\n", __FUNCTION__));
216
    status = compositor->check_composite (extents);
216
    if (unlikely (status))
	return status;
216
    status = CAIRO_INT_STATUS_UNSUPPORTED;
216
    if (_cairo_path_fixed_fill_is_rectilinear (path)) {
	cairo_boxes_t boxes;
87
	_cairo_boxes_init_with_clip (&boxes, extents->clip);
87
	status = _cairo_path_fixed_fill_rectilinear_to_boxes (path,
							      fill_rule,
							      antialias,
							      &boxes);
87
	if (likely (status == CAIRO_INT_STATUS_SUCCESS))
87
	    status = clip_and_composite_boxes (compositor, extents, &boxes);
87
	_cairo_boxes_fini (&boxes);
    }
216
    if (status == CAIRO_INT_STATUS_UNSUPPORTED) {
	cairo_polygon_t polygon;
#if 0
	if (extents->mask.width  > extents->unbounded.width ||
	    extents->mask.height > extents->unbounded.height)
	{
	    cairo_box_t limits;
	    _cairo_box_from_rectangle (&limits, &extents->unbounded);
	    _cairo_polygon_init (&polygon, &limits, 1);
	}
	else
	{
	    _cairo_polygon_init (&polygon, NULL, 0);
	}
	status = _cairo_path_fixed_fill_to_polygon (path, tolerance, &polygon);
	if (likely (status == CAIRO_INT_STATUS_SUCCESS)) {
	    status = _cairo_polygon_intersect_with_boxes (&polygon, &fill_rule,
							  extents->clip->boxes,
							  extents->clip->num_boxes);
	}
#else
129
	_cairo_polygon_init_with_clip (&polygon, extents->clip);
129
	status = _cairo_path_fixed_fill_to_polygon (path, tolerance, &polygon);
#endif
129
	if (likely (status == CAIRO_INT_STATUS_SUCCESS)) {
129
	    status = clip_and_composite_polygon (compositor, extents, &polygon,
129
						 antialias, fill_rule, path->has_curve_to);
	}
129
	_cairo_polygon_fini (&polygon);
    }
216
    return status;
}
static cairo_int_status_t
68607
composite_glyphs (const cairo_traps_compositor_t *compositor,
		  cairo_surface_t	*dst,
		  void *closure,
		  cairo_operator_t	 op,
		  cairo_surface_t	*src,
		  int src_x, int src_y,
		  int dst_x, int dst_y,
		  const cairo_rectangle_int_t *extents,
		  cairo_clip_t		*clip)
{
68607
    cairo_composite_glyphs_info_t *info = closure;
    TRACE ((stderr, "%s\n", __FUNCTION__));
68607
    if (op == CAIRO_OPERATOR_ADD && (dst->content & CAIRO_CONTENT_COLOR) == 0)
135
	info->use_mask = 0;
68607
    return compositor->composite_glyphs (dst, op, src,
					 src_x, src_y,
					 dst_x, dst_y,
					 info);
}
static cairo_int_status_t
68607
_cairo_traps_compositor_glyphs (const cairo_compositor_t	*_compositor,
				cairo_composite_rectangles_t	*extents,
				cairo_scaled_font_t		*scaled_font,
				cairo_glyph_t			*glyphs,
				int				 num_glyphs,
				cairo_bool_t			 overlap)
{
68607
    const cairo_traps_compositor_t *compositor = (cairo_traps_compositor_t *)_compositor;
    cairo_int_status_t status;
    TRACE ((stderr, "%s\n", __FUNCTION__));
68607
    status = compositor->check_composite (extents);
68607
    if (unlikely (status))
	return status;
68607
    _cairo_scaled_font_freeze_cache (scaled_font);
68607
    status = compositor->check_composite_glyphs (extents,
						 scaled_font, glyphs,
						 &num_glyphs);
68607
    if (likely (status == CAIRO_INT_STATUS_SUCCESS)) {
	cairo_composite_glyphs_info_t info;
68607
	info.font = scaled_font;
68607
	info.glyphs = glyphs;
68607
	info.num_glyphs = num_glyphs;
68607
	info.use_mask = overlap || ! extents->is_bounded;
68607
	info.extents = extents->bounded;
68607
	status = clip_and_composite (compositor, extents,
				     composite_glyphs, NULL, &info,
68607
				     need_bounded_clip (extents) | FORCE_CLIP_REGION);
    }
68607
    _cairo_scaled_font_thaw_cache (scaled_font);
68607
    return status;
}
void
1746
_cairo_traps_compositor_init (cairo_traps_compositor_t *compositor,
			      const cairo_compositor_t  *delegate)
{
1746
    compositor->base.delegate = delegate;
1746
    compositor->base.paint = _cairo_traps_compositor_paint;
1746
    compositor->base.mask = _cairo_traps_compositor_mask;
1746
    compositor->base.fill = _cairo_traps_compositor_fill;
1746
    compositor->base.stroke = _cairo_traps_compositor_stroke;
1746
    compositor->base.glyphs = _cairo_traps_compositor_glyphs;
1746
}