1
/*
2
 * Copyright © 2004 Carl Worth
3
 * Copyright © 2006 Red Hat, Inc.
4
 * Copyright © 2008 Chris Wilson
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it either under the terms of the GNU Lesser General Public
8
 * License version 2.1 as published by the Free Software Foundation
9
 * (the "LGPL") or, at your option, under the terms of the Mozilla
10
 * Public License Version 1.1 (the "MPL"). If you do not alter this
11
 * notice, a recipient may use your version of this file under either
12
 * the MPL or the LGPL.
13
 *
14
 * You should have received a copy of the LGPL along with this library
15
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
16
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
17
 * You should have received a copy of the MPL along with this library
18
 * in the file COPYING-MPL-1.1
19
 *
20
 * The contents of this file are subject to the Mozilla Public License
21
 * Version 1.1 (the "License"); you may not use this file except in
22
 * compliance with the License. You may obtain a copy of the License at
23
 * http://www.mozilla.org/MPL/
24
 *
25
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
26
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
27
 * the specific language governing rights and limitations.
28
 *
29
 * The Original Code is the cairo graphics library.
30
 *
31
 * The Initial Developer of the Original Code is Carl Worth
32
 *
33
 * Contributor(s):
34
 *	Carl D. Worth <cworth@cworth.org>
35
 *	Chris Wilson <chris@chris-wilson.co.uk>
36
 */
37

            
38
/* Provide definitions for standalone compilation */
39
#include "cairoint.h"
40

            
41
#include "cairo-error-private.h"
42
#include "cairo-freelist-private.h"
43
#include "cairo-combsort-inline.h"
44

            
45

            
46
typedef struct _cairo_bo_intersect_ordinate {
47
    int32_t ordinate;
48
    enum { EXCESS = -1, EXACT = 0, DEFAULT = 1 } approx;
49
} cairo_bo_intersect_ordinate_t;
50

            
51
typedef struct _cairo_bo_intersect_point {
52
    cairo_bo_intersect_ordinate_t x;
53
    cairo_bo_intersect_ordinate_t y;
54
} cairo_bo_intersect_point_t;
55

            
56
typedef struct _cairo_bo_edge cairo_bo_edge_t;
57

            
58
typedef struct _cairo_bo_deferred {
59
    cairo_bo_edge_t *other;
60
    int32_t top;
61
} cairo_bo_deferred_t;
62

            
63
struct _cairo_bo_edge {
64
    int a_or_b;
65
    cairo_edge_t edge;
66
    cairo_bo_edge_t *prev;
67
    cairo_bo_edge_t *next;
68
    cairo_bo_deferred_t deferred;
69
};
70

            
71
/* the parent is always given by index/2 */
72
#define PQ_PARENT_INDEX(i) ((i) >> 1)
73
#define PQ_FIRST_ENTRY 1
74

            
75
/* left and right children are index * 2 and (index * 2) +1 respectively */
76
#define PQ_LEFT_CHILD_INDEX(i) ((i) << 1)
77

            
78
typedef enum {
79
    CAIRO_BO_EVENT_TYPE_STOP = -1,
80
    CAIRO_BO_EVENT_TYPE_INTERSECTION,
81
    CAIRO_BO_EVENT_TYPE_START
82
} cairo_bo_event_type_t;
83

            
84
typedef struct _cairo_bo_event {
85
    cairo_bo_event_type_t type;
86
    cairo_bo_intersect_point_t point;
87
} cairo_bo_event_t;
88

            
89
typedef struct _cairo_bo_start_event {
90
    cairo_bo_event_type_t type;
91
    cairo_bo_intersect_point_t point;
92
    cairo_bo_edge_t edge;
93
} cairo_bo_start_event_t;
94

            
95
typedef struct _cairo_bo_queue_event {
96
    cairo_bo_event_type_t type;
97
    cairo_bo_intersect_point_t point;
98
    cairo_bo_edge_t *e1;
99
    cairo_bo_edge_t *e2;
100
} cairo_bo_queue_event_t;
101

            
102
typedef struct _pqueue {
103
    int size, max_size;
104

            
105
    cairo_bo_event_t **elements;
106
    cairo_bo_event_t *elements_embedded[1024];
107
} pqueue_t;
108

            
109
typedef struct _cairo_bo_event_queue {
110
    cairo_freepool_t pool;
111
    pqueue_t pqueue;
112
    cairo_bo_event_t **start_events;
113
} cairo_bo_event_queue_t;
114

            
115
typedef struct _cairo_bo_sweep_line {
116
    cairo_bo_edge_t *head;
117
    int32_t current_y;
118
    cairo_bo_edge_t *current_edge;
119
} cairo_bo_sweep_line_t;
120

            
121
static cairo_fixed_t
122
306960
_line_compute_intersection_x_for_y (const cairo_line_t *line,
123
				    cairo_fixed_t y)
124
{
125
    cairo_fixed_t x, dy;
126

            
127
306960
    if (y == line->p1.y)
128
117396
	return line->p1.x;
129
189564
    if (y == line->p2.y)
130
117219
	return line->p2.x;
131

            
132
72345
    x = line->p1.x;
133
72345
    dy = line->p2.y - line->p1.y;
134
72345
    if (dy != 0) {
135
72345
	x += _cairo_fixed_mul_div_floor (y - line->p1.y,
136
72345
					 line->p2.x - line->p1.x,
137
					 dy);
138
    }
139

            
140
72345
    return x;
141
}
142

            
143
static inline int
144
10339086
_cairo_bo_point32_compare (cairo_bo_intersect_point_t const *a,
145
			   cairo_bo_intersect_point_t const *b)
146
{
147
    int cmp;
148

            
149
10339086
    cmp = a->y.ordinate - b->y.ordinate;
150
10339086
    if (cmp)
151
9359190
	return cmp;
152

            
153
979896
    cmp = a->y.approx - b->y.approx;
154
979896
    if (cmp)
155
32595
	return cmp;
156

            
157
947301
    return a->x.ordinate - b->x.ordinate;
158
}
159

            
160
/* Compare the slope of a to the slope of b, returning 1, 0, -1 if the
161
 * slope a is respectively greater than, equal to, or less than the
162
 * slope of b.
163
 *
164
 * For each edge, consider the direction vector formed from:
165
 *
166
 *	top -> bottom
167
 *
168
 * which is:
169
 *
170
 *	(dx, dy) = (line.p2.x - line.p1.x, line.p2.y - line.p1.y)
171
 *
172
 * We then define the slope of each edge as dx/dy, (which is the
173
 * inverse of the slope typically used in math instruction). We never
174
 * compute a slope directly as the value approaches infinity, but we
175
 * can derive a slope comparison without division as follows, (where
176
 * the ? represents our compare operator).
177
 *
178
 * 1.	   slope(a) ? slope(b)
179
 * 2.	    adx/ady ? bdx/bdy
180
 * 3.	(adx * bdy) ? (bdx * ady)
181
 *
182
 * Note that from step 2 to step 3 there is no change needed in the
183
 * sign of the result since both ady and bdy are guaranteed to be
184
 * greater than or equal to 0.
185
 *
186
 * When using this slope comparison to sort edges, some care is needed
187
 * when interpreting the results. Since the slope compare operates on
188
 * distance vectors from top to bottom it gives a correct left to
189
 * right sort for edges that have a common top point, (such as two
190
 * edges with start events at the same location). On the other hand,
191
 * the sense of the result will be exactly reversed for two edges that
192
 * have a common stop point.
193
 */
194
static inline int
195
4007982
_slope_compare (const cairo_bo_edge_t *a,
196
		const cairo_bo_edge_t *b)
197
{
198
    /* XXX: We're assuming here that dx and dy will still fit in 32
199
     * bits. That's not true in general as there could be overflow. We
200
     * should prevent that before the tessellation algorithm
201
     * begins.
202
     */
203
4007982
    int32_t adx = a->edge.line.p2.x - a->edge.line.p1.x;
204
4007982
    int32_t bdx = b->edge.line.p2.x - b->edge.line.p1.x;
205

            
206
    /* Since the dy's are all positive by construction we can fast
207
     * path several common cases.
208
     */
209

            
210
    /* First check for vertical lines. */
211
4007982
    if (adx == 0)
212
235122
	return -bdx;
213
3772860
    if (bdx == 0)
214
280995
	return adx;
215

            
216
    /* Then where the two edges point in different directions wrt x. */
217
3491865
    if ((adx ^ bdx) < 0)
218
1473246
	return adx;
219

            
220
    /* Finally we actually need to do the general comparison. */
221
    {
222
2018619
	int32_t ady = a->edge.line.p2.y - a->edge.line.p1.y;
223
2018619
	int32_t bdy = b->edge.line.p2.y - b->edge.line.p1.y;
224
2018619
	cairo_int64_t adx_bdy = _cairo_int32x32_64_mul (adx, bdy);
225
2018619
	cairo_int64_t bdx_ady = _cairo_int32x32_64_mul (bdx, ady);
226

            
227
2018619
	return _cairo_int64_cmp (adx_bdy, bdx_ady);
228
    }
229
}
230

            
231
/*
232
 * We need to compare the x-coordinates of a pair of lines for a particular y,
233
 * without loss of precision.
234
 *
235
 * The x-coordinate along an edge for a given y is:
236
 *   X = A_x + (Y - A_y) * A_dx / A_dy
237
 *
238
 * So the inequality we wish to test is:
239
 *   A_x + (Y - A_y) * A_dx / A_dy ∘ B_x + (Y - B_y) * B_dx / B_dy,
240
 * where ∘ is our inequality operator.
241
 *
242
 * By construction, we know that A_dy and B_dy (and (Y - A_y), (Y - B_y)) are
243
 * all positive, so we can rearrange it thus without causing a sign change:
244
 *   A_dy * B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx * A_dy
245
 *                                 - (Y - A_y) * A_dx * B_dy
246
 *
247
 * Given the assumption that all the deltas fit within 32 bits, we can compute
248
 * this comparison directly using 128 bit arithmetic. For certain, but common,
249
 * input we can reduce this down to a single 32 bit compare by inspecting the
250
 * deltas.
251
 *
252
 * (And put the burden of the work on developing fast 128 bit ops, which are
253
 * required throughout the tessellator.)
254
 *
255
 * See the similar discussion for _slope_compare().
256
 */
257
static int
258
374496
edges_compare_x_for_y_general (const cairo_bo_edge_t *a,
259
			       const cairo_bo_edge_t *b,
260
			       int32_t y)
261
{
262
    /* XXX: We're assuming here that dx and dy will still fit in 32
263
     * bits. That's not true in general as there could be overflow. We
264
     * should prevent that before the tessellation algorithm
265
     * begins.
266
     */
267
    int32_t dx;
268
    int32_t adx, ady;
269
    int32_t bdx, bdy;
270
    enum {
271
       HAVE_NONE    = 0x0,
272
       HAVE_DX      = 0x1,
273
       HAVE_ADX     = 0x2,
274
       HAVE_DX_ADX  = HAVE_DX | HAVE_ADX,
275
       HAVE_BDX     = 0x4,
276
       HAVE_DX_BDX  = HAVE_DX | HAVE_BDX,
277
       HAVE_ADX_BDX = HAVE_ADX | HAVE_BDX,
278
       HAVE_ALL     = HAVE_DX | HAVE_ADX | HAVE_BDX
279
374496
    } have_dx_adx_bdx = HAVE_ALL;
280

            
281
    /* don't bother solving for abscissa if the edges' bounding boxes
282
     * can be used to order them. */
283
    {
284
           int32_t amin, amax;
285
           int32_t bmin, bmax;
286
374496
           if (a->edge.line.p1.x < a->edge.line.p2.x) {
287
187776
                   amin = a->edge.line.p1.x;
288
187776
                   amax = a->edge.line.p2.x;
289
           } else {
290
186720
                   amin = a->edge.line.p2.x;
291
186720
                   amax = a->edge.line.p1.x;
292
           }
293
374496
           if (b->edge.line.p1.x < b->edge.line.p2.x) {
294
92328
                   bmin = b->edge.line.p1.x;
295
92328
                   bmax = b->edge.line.p2.x;
296
           } else {
297
282168
                   bmin = b->edge.line.p2.x;
298
282168
                   bmax = b->edge.line.p1.x;
299
           }
300
374496
           if (amax < bmin) return -1;
301
288048
           if (amin > bmax) return +1;
302
    }
303

            
304
178410
    ady = a->edge.line.p2.y - a->edge.line.p1.y;
305
178410
    adx = a->edge.line.p2.x - a->edge.line.p1.x;
306
178410
    if (adx == 0)
307
7533
	have_dx_adx_bdx &= ~HAVE_ADX;
308

            
309
178410
    bdy = b->edge.line.p2.y - b->edge.line.p1.y;
310
178410
    bdx = b->edge.line.p2.x - b->edge.line.p1.x;
311
178410
    if (bdx == 0)
312
52137
	have_dx_adx_bdx &= ~HAVE_BDX;
313

            
314
178410
    dx = a->edge.line.p1.x - b->edge.line.p1.x;
315
178410
    if (dx == 0)
316
1779
	have_dx_adx_bdx &= ~HAVE_DX;
317

            
318
#define L _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (ady, bdy), dx)
319
#define A _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (adx, bdy), y - a->edge.line.p1.y)
320
#define B _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (bdx, ady), y - b->edge.line.p1.y)
321
178410
    switch (have_dx_adx_bdx) {
322
174
    default:
323
    case HAVE_NONE:
324
174
	return 0;
325
    case HAVE_DX:
326
	/* A_dy * B_dy * (A_x - B_x) ∘ 0 */
327
	return dx; /* ady * bdy is positive definite */
328
822
    case HAVE_ADX:
329
	/* 0 ∘  - (Y - A_y) * A_dx * B_dy */
330
822
	return adx; /* bdy * (y - a->top.y) is positive definite */
331
60
    case HAVE_BDX:
332
	/* 0 ∘ (Y - B_y) * B_dx * A_dy */
333
60
	return -bdx; /* ady * (y - b->top.y) is positive definite */
334
723
    case HAVE_ADX_BDX:
335
	/*  0 ∘ (Y - B_y) * B_dx * A_dy - (Y - A_y) * A_dx * B_dy */
336
723
	if ((adx ^ bdx) < 0) {
337
348
	    return adx;
338
375
	} else if (a->edge.line.p1.y == b->edge.line.p1.y) { /* common origin */
339
	    cairo_int64_t adx_bdy, bdx_ady;
340

            
341
	    /* ∴ A_dx * B_dy ∘ B_dx * A_dy */
342

            
343
363
	    adx_bdy = _cairo_int32x32_64_mul (adx, bdy);
344
363
	    bdx_ady = _cairo_int32x32_64_mul (bdx, ady);
345

            
346
363
	    return _cairo_int64_cmp (adx_bdy, bdx_ady);
347
	} else
348
12
	    return _cairo_int128_cmp (A, B);
349
51141
    case HAVE_DX_ADX:
350
	/* A_dy * (A_x - B_x) ∘ - (Y - A_y) * A_dx */
351
51141
	if ((-adx ^ dx) < 0) {
352
	    return dx;
353
	} else {
354
	    cairo_int64_t ady_dx, dy_adx;
355

            
356
51141
	    ady_dx = _cairo_int32x32_64_mul (ady, dx);
357
51141
	    dy_adx = _cairo_int32x32_64_mul (a->edge.line.p1.y - y, adx);
358

            
359
51141
	    return _cairo_int64_cmp (ady_dx, dy_adx);
360
	}
361
7299
    case HAVE_DX_BDX:
362
	/* B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx */
363
7299
	if ((bdx ^ dx) < 0) {
364
	    return dx;
365
	} else {
366
	    cairo_int64_t bdy_dx, dy_bdx;
367

            
368
7299
	    bdy_dx = _cairo_int32x32_64_mul (bdy, dx);
369
7299
	    dy_bdx = _cairo_int32x32_64_mul (y - b->edge.line.p1.y, bdx);
370

            
371
7299
	    return _cairo_int64_cmp (bdy_dx, dy_bdx);
372
	}
373
118191
    case HAVE_ALL:
374
	/* XXX try comparing (a->edge.line.p2.x - b->edge.line.p2.x) et al */
375
118191
	return _cairo_int128_cmp (L, _cairo_int128_sub (B, A));
376
    }
377
#undef B
378
#undef A
379
#undef L
380
}
381

            
382
/*
383
 * We need to compare the x-coordinate of a line for a particular y wrt to a
384
 * given x, without loss of precision.
385
 *
386
 * The x-coordinate along an edge for a given y is:
387
 *   X = A_x + (Y - A_y) * A_dx / A_dy
388
 *
389
 * So the inequality we wish to test is:
390
 *   A_x + (Y - A_y) * A_dx / A_dy ∘ X
391
 * where ∘ is our inequality operator.
392
 *
393
 * By construction, we know that A_dy (and (Y - A_y)) are
394
 * all positive, so we can rearrange it thus without causing a sign change:
395
 *   (Y - A_y) * A_dx ∘ (X - A_x) * A_dy
396
 *
397
 * Given the assumption that all the deltas fit within 32 bits, we can compute
398
 * this comparison directly using 64 bit arithmetic.
399
 *
400
 * See the similar discussion for _slope_compare() and
401
 * edges_compare_x_for_y_general().
402
 */
403
static int
404
1236936
edge_compare_for_y_against_x (const cairo_bo_edge_t *a,
405
			      int32_t y,
406
			      int32_t x)
407
{
408
    int32_t adx, ady;
409
    int32_t dx, dy;
410
    cairo_int64_t L, R;
411

            
412
1236936
    if (x < a->edge.line.p1.x && x < a->edge.line.p2.x)
413
401769
	return 1;
414
835167
    if (x > a->edge.line.p1.x && x > a->edge.line.p2.x)
415
349284
	return -1;
416

            
417
485883
    adx = a->edge.line.p2.x - a->edge.line.p1.x;
418
485883
    dx = x - a->edge.line.p1.x;
419

            
420
485883
    if (adx == 0)
421
1347
	return -dx;
422
484536
    if (dx == 0 || (adx ^ dx) < 0)
423
627
	return adx;
424

            
425
483909
    dy = y - a->edge.line.p1.y;
426
483909
    ady = a->edge.line.p2.y - a->edge.line.p1.y;
427

            
428
483909
    L = _cairo_int32x32_64_mul (dy, adx);
429
483909
    R = _cairo_int32x32_64_mul (dx, ady);
430

            
431
483909
    return _cairo_int64_cmp (L, R);
432
}
433

            
434
static int
435
1603236
edges_compare_x_for_y (const cairo_bo_edge_t *a,
436
		       const cairo_bo_edge_t *b,
437
		       int32_t y)
438
{
439
    /* If the sweep-line is currently on an end-point of a line,
440
     * then we know its precise x value (and considering that we often need to
441
     * compare events at end-points, this happens frequently enough to warrant
442
     * special casing).
443
     */
444
    enum {
445
       HAVE_NEITHER = 0x0,
446
       HAVE_AX      = 0x1,
447
       HAVE_BX      = 0x2,
448
       HAVE_BOTH    = HAVE_AX | HAVE_BX
449
1603236
    } have_ax_bx = HAVE_BOTH;
450
1603236
    int32_t ax = 0, bx = 0;
451

            
452
1603236
    if (y == a->edge.line.p1.y)
453
60102
	ax = a->edge.line.p1.x;
454
1543134
    else if (y == a->edge.line.p2.y)
455
36987
	ax = a->edge.line.p2.x;
456
    else
457
1506147
	have_ax_bx &= ~HAVE_AX;
458

            
459
1603236
    if (y == b->edge.line.p1.y)
460
1221363
	bx = b->edge.line.p1.x;
461
381873
    else if (y == b->edge.line.p2.y)
462
	bx = b->edge.line.p2.x;
463
    else
464
381873
	have_ax_bx &= ~HAVE_BX;
465

            
466
1603236
    switch (have_ax_bx) {
467
374496
    default:
468
    case HAVE_NEITHER:
469
374496
	return edges_compare_x_for_y_general (a, b, y);
470
7377
    case HAVE_AX:
471
7377
	return -edge_compare_for_y_against_x (b, y, ax);
472
1131651
    case HAVE_BX:
473
1131651
	return edge_compare_for_y_against_x (a, y, bx);
474
89712
    case HAVE_BOTH:
475
89712
	return ax - bx;
476
    }
477
}
478

            
479
static inline int
480
5690628
_line_equal (const cairo_line_t *a, const cairo_line_t *b)
481
{
482
124710
    return a->p1.x == b->p1.x && a->p1.y == b->p1.y &&
483
5815338
           a->p2.x == b->p2.x && a->p2.y == b->p2.y;
484
}
485

            
486
static int
487
1617927
_cairo_bo_sweep_line_compare_edges (cairo_bo_sweep_line_t	*sweep_line,
488
				    const cairo_bo_edge_t	*a,
489
				    const cairo_bo_edge_t	*b)
490
{
491
    int cmp;
492

            
493
    /* compare the edges if not identical */
494
1617927
    if (! _line_equal (&a->edge.line, &b->edge.line)) {
495
1603236
	cmp = edges_compare_x_for_y (a, b, sweep_line->current_y);
496
1603236
	if (cmp)
497
1593246
	    return cmp;
498

            
499
	/* The two edges intersect exactly at y, so fall back on slope
500
	 * comparison. We know that this compare_edges function will be
501
	 * called only when starting a new edge, (not when stopping an
502
	 * edge), so we don't have to worry about conditionally inverting
503
	 * the sense of _slope_compare. */
504
9990
	cmp = _slope_compare (a, b);
505
9990
	if (cmp)
506
9486
	    return cmp;
507
    }
508

            
509
    /* We've got two collinear edges now. */
510
15195
    return b->edge.bottom - a->edge.bottom;
511
}
512

            
513
static inline cairo_int64_t
514
2106576
det32_64 (int32_t a, int32_t b,
515
	  int32_t c, int32_t d)
516
{
517
    /* det = a * d - b * c */
518
2106576
    return _cairo_int64_sub (_cairo_int32x32_64_mul (a, d),
519
			     _cairo_int32x32_64_mul (b, c));
520
}
521

            
522
static inline cairo_int128_t
523
652842
det64x32_128 (cairo_int64_t a, int32_t       b,
524
	      cairo_int64_t c, int32_t       d)
525
{
526
    /* det = a * d - b * c */
527
652842
    return _cairo_int128_sub (_cairo_int64x32_128_mul (a, d),
528
			      _cairo_int64x32_128_mul (c, b));
529
}
530

            
531
static inline cairo_bo_intersect_ordinate_t
532
652842
round_to_nearest (cairo_quorem64_t d,
533
		  cairo_int64_t    den)
534
{
535
    cairo_bo_intersect_ordinate_t ordinate;
536
652842
    int32_t quo = d.quo;
537
652842
    cairo_int64_t drem_2 = _cairo_int64_mul (d.rem, _cairo_int32_to_int64 (2));
538

            
539
    /* assert (! _cairo_int64_negative (den));*/
540

            
541
652842
    if (_cairo_int64_lt (drem_2, _cairo_int64_negate (den))) {
542
	quo -= 1;
543
	drem_2 = _cairo_int64_negate (drem_2);
544
652842
    } else if (_cairo_int64_le (den, drem_2)) {
545
260655
	quo += 1;
546
260655
	drem_2 = _cairo_int64_negate (drem_2);
547
    }
548

            
549
652842
    ordinate.ordinate = quo;
550
652842
    ordinate.approx = _cairo_int64_is_zero (drem_2) ? EXACT : _cairo_int64_negative (drem_2) ? EXCESS : DEFAULT;
551

            
552
652842
    return ordinate;
553
}
554

            
555
/* Compute the intersection of two lines as defined by two edges. The
556
 * result is provided as a coordinate pair of 128-bit integers.
557
 *
558
 * Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection or
559
 * %CAIRO_BO_STATUS_PARALLEL if the two lines are exactly parallel.
560
 */
561
static cairo_bool_t
562
538425
intersect_lines (cairo_bo_edge_t		*a,
563
		 cairo_bo_edge_t		*b,
564
		 cairo_bo_intersect_point_t	*intersection)
565
{
566
    cairo_int64_t a_det, b_det;
567

            
568
    /* XXX: We're assuming here that dx and dy will still fit in 32
569
     * bits. That's not true in general as there could be overflow. We
570
     * should prevent that before the tessellation algorithm begins.
571
     * What we're doing to mitigate this is to perform clamping in
572
     * cairo_bo_tessellate_polygon().
573
     */
574
538425
    int32_t dx1 = a->edge.line.p1.x - a->edge.line.p2.x;
575
538425
    int32_t dy1 = a->edge.line.p1.y - a->edge.line.p2.y;
576

            
577
538425
    int32_t dx2 = b->edge.line.p1.x - b->edge.line.p2.x;
578
538425
    int32_t dy2 = b->edge.line.p1.y - b->edge.line.p2.y;
579

            
580
    cairo_int64_t den_det;
581
    cairo_int64_t R;
582
    cairo_quorem64_t qr;
583

            
584
538425
    den_det = det32_64 (dx1, dy1, dx2, dy2);
585

            
586
     /* Q: Can we determine that the lines do not intersect (within range)
587
      * much more cheaply than computing the intersection point i.e. by
588
      * avoiding the division?
589
      *
590
      *   X = ax + t * adx = bx + s * bdx;
591
      *   Y = ay + t * ady = by + s * bdy;
592
      *   ∴ t * (ady*bdx - bdy*adx) = bdx * (by - ay) + bdy * (ax - bx)
593
      *   => t * L = R
594
      *
595
      * Therefore we can reject any intersection (under the criteria for
596
      * valid intersection events) if:
597
      *   L^R < 0 => t < 0, or
598
      *   L<R => t > 1
599
      *
600
      * (where top/bottom must at least extend to the line endpoints).
601
      *
602
      * A similar substitution can be performed for s, yielding:
603
      *   s * (ady*bdx - bdy*adx) = ady * (ax - bx) - adx * (ay - by)
604
      */
605
538425
    R = det32_64 (dx2, dy2,
606
538425
		  b->edge.line.p1.x - a->edge.line.p1.x,
607
538425
		  b->edge.line.p1.y - a->edge.line.p1.y);
608
538425
	if (_cairo_int64_le (den_det, R))
609
161541
	    return FALSE;
610

            
611
376884
    R = det32_64 (dy1, dx1,
612
376884
		  a->edge.line.p1.y - b->edge.line.p1.y,
613
376884
		  a->edge.line.p1.x - b->edge.line.p1.x);
614
376884
	if (_cairo_int64_le (den_det, R))
615
50463
	    return FALSE;
616

            
617
    /* We now know that the two lines should intersect within range. */
618

            
619
326421
    a_det = det32_64 (a->edge.line.p1.x, a->edge.line.p1.y,
620
		      a->edge.line.p2.x, a->edge.line.p2.y);
621
326421
    b_det = det32_64 (b->edge.line.p1.x, b->edge.line.p1.y,
622
		      b->edge.line.p2.x, b->edge.line.p2.y);
623

            
624
    /* x = det (a_det, dx1, b_det, dx2) / den_det */
625
326421
    qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dx1,
626
						       b_det, dx2),
627
					 den_det);
628
326421
    if (_cairo_int64_eq (qr.rem, den_det))
629
	return FALSE;
630

            
631
326421
    intersection->x = round_to_nearest (qr, den_det);
632

            
633
    /* y = det (a_det, dy1, b_det, dy2) / den_det */
634
326421
    qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dy1,
635
						       b_det, dy2),
636
					 den_det);
637
326421
    if (_cairo_int64_eq (qr.rem, den_det))
638
	return FALSE;
639

            
640
326421
    intersection->y = round_to_nearest (qr, den_det);
641

            
642
326421
    return TRUE;
643
}
644

            
645
static int
646
636414
_cairo_bo_intersect_ordinate_32_compare (cairo_bo_intersect_ordinate_t	a,
647
					 int32_t			b)
648
{
649
    /* First compare the quotient */
650
636414
    if (a.ordinate > b)
651
15273
	return +1;
652
621141
    if (a.ordinate < b)
653
611961
	return -1;
654

            
655
9180
    return a.approx; /* == EXCESS ? -1 : a.approx == EXACT ? 0 : 1;*/
656
}
657

            
658
/* Does the given edge contain the given point. The point must already
659
 * be known to be contained within the line determined by the edge,
660
 * (most likely the point results from an intersection of this edge
661
 * with another).
662
 *
663
 * If we had exact arithmetic, then this function would simply be a
664
 * matter of examining whether the y value of the point lies within
665
 * the range of y values of the edge. But since intersection points
666
 * are not exact due to being rounded to the nearest integer within
667
 * the available precision, we must also examine the x value of the
668
 * point.
669
 *
670
 * The definition of "contains" here is that the given intersection
671
 * point will be seen by the sweep line after the start event for the
672
 * given edge and before the stop event for the edge. See the comments
673
 * in the implementation for more details.
674
 */
675
static cairo_bool_t
676
636414
_cairo_bo_edge_contains_intersect_point (cairo_bo_edge_t		*edge,
677
					 cairo_bo_intersect_point_t	*point)
678
{
679
636414
    return _cairo_bo_intersect_ordinate_32_compare (point->y,
680
636414
						    edge->edge.bottom) < 0;
681
}
682

            
683
/* Compute the intersection of two edges. The result is provided as a
684
 * coordinate pair of 128-bit integers.
685
 *
686
 * Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection
687
 * that is within both edges, %CAIRO_BO_STATUS_NO_INTERSECTION if the
688
 * intersection of the lines defined by the edges occurs outside of
689
 * one or both edges, and %CAIRO_BO_STATUS_PARALLEL if the two edges
690
 * are exactly parallel.
691
 *
692
 * Note that when determining if a candidate intersection is "inside"
693
 * an edge, we consider both the infinitesimal shortening and the
694
 * infinitesimal tilt rules described by John Hobby. Specifically, if
695
 * the intersection is exactly the same as an edge point, it is
696
 * effectively outside (no intersection is returned). Also, if the
697
 * intersection point has the same
698
 */
699
static cairo_bool_t
700
538425
_cairo_bo_edge_intersect (cairo_bo_edge_t	*a,
701
			  cairo_bo_edge_t	*b,
702
			  cairo_bo_intersect_point_t *intersection)
703
{
704
538425
    if (! intersect_lines (a, b, intersection))
705
212004
	return FALSE;
706

            
707
326421
    if (! _cairo_bo_edge_contains_intersect_point (a, intersection))
708
16428
	return FALSE;
709

            
710
309993
    if (! _cairo_bo_edge_contains_intersect_point (b, intersection))
711
4260
	return FALSE;
712

            
713
305733
    return TRUE;
714
}
715

            
716
static inline int
717
10339086
cairo_bo_event_compare (const cairo_bo_event_t *a,
718
			const cairo_bo_event_t *b)
719
{
720
    int cmp;
721

            
722
10339086
    cmp = _cairo_bo_point32_compare (&a->point, &b->point);
723
10339086
    if (cmp)
724
10069872
	return cmp;
725

            
726
269214
    cmp = a->type - b->type;
727
269214
    if (cmp)
728
120759
	return cmp;
729

            
730
148455
    return a < b ? -1 : a == b ? 0 : 1;
731
}
732

            
733
static inline void
734
606
_pqueue_init (pqueue_t *pq)
735
{
736
606
    pq->max_size = ARRAY_LENGTH (pq->elements_embedded);
737
606
    pq->size = 0;
738

            
739
606
    pq->elements = pq->elements_embedded;
740
606
}
741

            
742
static inline void
743
606
_pqueue_fini (pqueue_t *pq)
744
{
745
606
    if (pq->elements != pq->elements_embedded)
746
	free (pq->elements);
747
606
}
748

            
749
static cairo_status_t
750
_pqueue_grow (pqueue_t *pq)
751
{
752
    cairo_bo_event_t **new_elements;
753
    pq->max_size *= 2;
754

            
755
    if (pq->elements == pq->elements_embedded) {
756
	new_elements = _cairo_malloc_ab (pq->max_size,
757
					 sizeof (cairo_bo_event_t *));
758
	if (unlikely (new_elements == NULL))
759
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
760

            
761
	memcpy (new_elements, pq->elements_embedded,
762
		sizeof (pq->elements_embedded));
763
    } else {
764
	new_elements = _cairo_realloc_ab (pq->elements,
765
					  pq->max_size,
766
					  sizeof (cairo_bo_event_t *));
767
	if (unlikely (new_elements == NULL))
768
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
769
    }
770

            
771
    pq->elements = new_elements;
772
    return CAIRO_STATUS_SUCCESS;
773
}
774

            
775
static inline cairo_status_t
776
459213
_pqueue_push (pqueue_t *pq, cairo_bo_event_t *event)
777
{
778
    cairo_bo_event_t **elements;
779
    int i, parent;
780

            
781
459213
    if (unlikely (pq->size + 1 == pq->max_size)) {
782
	cairo_status_t status;
783

            
784
	status = _pqueue_grow (pq);
785
	if (unlikely (status))
786
	    return status;
787
    }
788

            
789
459213
    elements = pq->elements;
790

            
791
459213
    for (i = ++pq->size;
792
2345424
	 i != PQ_FIRST_ENTRY &&
793
1158252
	 cairo_bo_event_compare (event,
794
1158252
				 elements[parent = PQ_PARENT_INDEX (i)]) < 0;
795
727959
	 i = parent)
796
    {
797
727959
	elements[i] = elements[parent];
798
    }
799

            
800
459213
    elements[i] = event;
801

            
802
459213
    return CAIRO_STATUS_SUCCESS;
803
}
804

            
805
static inline void
806
459213
_pqueue_pop (pqueue_t *pq)
807
{
808
459213
    cairo_bo_event_t **elements = pq->elements;
809
    cairo_bo_event_t *tail;
810
    int child, i;
811

            
812
459213
    tail = elements[pq->size--];
813
459213
    if (pq->size == 0) {
814
606
	elements[PQ_FIRST_ENTRY] = NULL;
815
606
	return;
816
    }
817

            
818
458607
    for (i = PQ_FIRST_ENTRY;
819
2649588
	 (child = PQ_LEFT_CHILD_INDEX (i)) <= pq->size;
820
2190981
	 i = child)
821
    {
822
4703349
	if (child != pq->size &&
823
2312301
	    cairo_bo_event_compare (elements[child+1],
824
2312301
				    elements[child]) < 0)
825
	{
826
1098903
	    child++;
827
	}
828

            
829
2391048
	if (cairo_bo_event_compare (elements[child], tail) >= 0)
830
200067
	    break;
831

            
832
2190981
	elements[i] = elements[child];
833
    }
834
458607
    elements[i] = tail;
835
}
836

            
837
static inline cairo_status_t
838
459213
_cairo_bo_event_queue_insert (cairo_bo_event_queue_t	*queue,
839
			      cairo_bo_event_type_t	 type,
840
			      cairo_bo_edge_t		*e1,
841
			      cairo_bo_edge_t		*e2,
842
			      const cairo_bo_intersect_point_t  *point)
843
{
844
    cairo_bo_queue_event_t *event;
845

            
846
459213
    event = _cairo_freepool_alloc (&queue->pool);
847
459213
    if (unlikely (event == NULL))
848
	return _cairo_error (CAIRO_STATUS_NO_MEMORY);
849

            
850
459213
    event->type = type;
851
459213
    event->e1 = e1;
852
459213
    event->e2 = e2;
853
459213
    event->point = *point;
854

            
855
459213
    return _pqueue_push (&queue->pqueue, (cairo_bo_event_t *) event);
856
}
857

            
858
static void
859
459213
_cairo_bo_event_queue_delete (cairo_bo_event_queue_t *queue,
860
			      cairo_bo_event_t	     *event)
861
{
862
459213
    _cairo_freepool_free (&queue->pool, event);
863
459213
}
864

            
865
static cairo_bo_event_t *
866
613299
_cairo_bo_event_dequeue (cairo_bo_event_queue_t *event_queue)
867
{
868
    cairo_bo_event_t *event, *cmp;
869

            
870
613299
    event = event_queue->pqueue.elements[PQ_FIRST_ENTRY];
871
613299
    cmp = *event_queue->start_events;
872
613299
    if (event == NULL ||
873
606657
	(cmp != NULL && cairo_bo_event_compare (cmp, event) < 0))
874
    {
875
154086
	event = cmp;
876
154086
	event_queue->start_events++;
877
    }
878
    else
879
    {
880
459213
	_pqueue_pop (&event_queue->pqueue);
881
    }
882

            
883
613299
    return event;
884
}
885

            
886
3876471
CAIRO_COMBSORT_DECLARE (_cairo_bo_event_queue_sort,
887
			cairo_bo_event_t *,
888
			cairo_bo_event_compare)
889

            
890
static void
891
606
_cairo_bo_event_queue_init (cairo_bo_event_queue_t	 *event_queue,
892
			    cairo_bo_event_t		**start_events,
893
			    int				  num_events)
894
{
895
606
    _cairo_bo_event_queue_sort (start_events, num_events);
896
606
    start_events[num_events] = NULL;
897

            
898
606
    event_queue->start_events = start_events;
899

            
900
606
    _cairo_freepool_init (&event_queue->pool,
901
			  sizeof (cairo_bo_queue_event_t));
902
606
    _pqueue_init (&event_queue->pqueue);
903
606
    event_queue->pqueue.elements[PQ_FIRST_ENTRY] = NULL;
904
606
}
905

            
906
static cairo_status_t
907
153480
event_queue_insert_stop (cairo_bo_event_queue_t	*event_queue,
908
			 cairo_bo_edge_t		*edge)
909
{
910
    cairo_bo_intersect_point_t point;
911

            
912
153480
    point.y.ordinate = edge->edge.bottom;
913
153480
    point.y.approx   = EXACT;
914
153480
    point.x.ordinate = _line_compute_intersection_x_for_y (&edge->edge.line,
915
							   point.y.ordinate);
916
153480
    point.x.approx   = EXACT;
917

            
918
153480
    return _cairo_bo_event_queue_insert (event_queue,
919
					 CAIRO_BO_EVENT_TYPE_STOP,
920
					 edge, NULL,
921
					 &point);
922
}
923

            
924
static void
925
606
_cairo_bo_event_queue_fini (cairo_bo_event_queue_t *event_queue)
926
{
927
606
    _pqueue_fini (&event_queue->pqueue);
928
606
    _cairo_freepool_fini (&event_queue->pool);
929
606
}
930

            
931
static inline cairo_status_t
932
931644
event_queue_insert_if_intersect_below_current_y (cairo_bo_event_queue_t	*event_queue,
933
						 cairo_bo_edge_t	*left,
934
						 cairo_bo_edge_t *right)
935
{
936
    cairo_bo_intersect_point_t intersection;
937

            
938
931644
    if (_line_equal (&left->edge.line, &right->edge.line))
939
20091
	return CAIRO_STATUS_SUCCESS;
940

            
941
    /* The names "left" and "right" here are correct descriptions of
942
     * the order of the two edges within the active edge list. So if a
943
     * slope comparison also puts left less than right, then we know
944
     * that the intersection of these two segments has already
945
     * occurred before the current sweep line position. */
946
911553
    if (_slope_compare (left, right) <= 0)
947
373128
	return CAIRO_STATUS_SUCCESS;
948

            
949
538425
    if (! _cairo_bo_edge_intersect (left, right, &intersection))
950
232692
	return CAIRO_STATUS_SUCCESS;
951

            
952
305733
    return _cairo_bo_event_queue_insert (event_queue,
953
					 CAIRO_BO_EVENT_TYPE_INTERSECTION,
954
					 left, right,
955
					 &intersection);
956
}
957

            
958
static void
959
606
_cairo_bo_sweep_line_init (cairo_bo_sweep_line_t *sweep_line)
960
{
961
606
    sweep_line->head = NULL;
962
606
    sweep_line->current_y = INT32_MIN;
963
606
    sweep_line->current_edge = NULL;
964
606
}
965

            
966
static cairo_status_t
967
153480
sweep_line_insert (cairo_bo_sweep_line_t	*sweep_line,
968
		   cairo_bo_edge_t		*edge)
969
{
970
153480
    if (sweep_line->current_edge != NULL) {
971
	cairo_bo_edge_t *prev, *next;
972
	int cmp;
973

            
974
152874
	cmp = _cairo_bo_sweep_line_compare_edges (sweep_line,
975
152874
						  sweep_line->current_edge,
976
						  edge);
977
152874
	if (cmp < 0) {
978
104769
	    prev = sweep_line->current_edge;
979
104769
	    next = prev->next;
980
1483653
	    while (next != NULL &&
981
736242
		   _cairo_bo_sweep_line_compare_edges (sweep_line,
982
						       next, edge) < 0)
983
	    {
984
642642
		prev = next, next = prev->next;
985
	    }
986

            
987
104769
	    prev->next = edge;
988
104769
	    edge->prev = prev;
989
104769
	    edge->next = next;
990
104769
	    if (next != NULL)
991
93600
		next->prev = edge;
992
48105
	} else if (cmp > 0) {
993
47772
	    next = sweep_line->current_edge;
994
47772
	    prev = next->prev;
995
1464582
	    while (prev != NULL &&
996
728811
		   _cairo_bo_sweep_line_compare_edges (sweep_line,
997
						       prev, edge) > 0)
998
	    {
999
687999
		next = prev, prev = next->prev;
	    }
47772
	    next->prev = edge;
47772
	    edge->next = next;
47772
	    edge->prev = prev;
47772
	    if (prev != NULL)
40812
		prev->next = edge;
	    else
6960
		sweep_line->head = edge;
	} else {
333
	    prev = sweep_line->current_edge;
333
	    edge->prev = prev;
333
	    edge->next = prev->next;
333
	    if (prev->next != NULL)
216
		prev->next->prev = edge;
333
	    prev->next = edge;
	}
    } else {
606
	sweep_line->head = edge;
    }
153480
    sweep_line->current_edge = edge;
153480
    return CAIRO_STATUS_SUCCESS;
}
static void
153480
_cairo_bo_sweep_line_delete (cairo_bo_sweep_line_t	*sweep_line,
			     cairo_bo_edge_t	*edge)
{
153480
    if (edge->prev != NULL)
144327
	edge->prev->next = edge->next;
    else
9153
	sweep_line->head = edge->next;
153480
    if (edge->next != NULL)
142941
	edge->next->prev = edge->prev;
153480
    if (sweep_line->current_edge == edge)
6075
	sweep_line->current_edge = edge->prev ? edge->prev : edge->next;
153480
}
static void
256032
_cairo_bo_sweep_line_swap (cairo_bo_sweep_line_t	*sweep_line,
			   cairo_bo_edge_t		*left,
			   cairo_bo_edge_t		*right)
{
256032
    if (left->prev != NULL)
255102
	left->prev->next = right;
    else
930
	sweep_line->head = right;
256032
    if (right->next != NULL)
254646
	right->next->prev = left;
256032
    left->next = right->next;
256032
    right->next = left;
256032
    right->prev = left->prev;
256032
    left->prev = right;
256032
}
static inline cairo_bool_t
3141057
edges_colinear (const cairo_bo_edge_t *a, const cairo_bo_edge_t *b)
{
3141057
    if (_line_equal (&a->edge.line, &b->edge.line))
54618
	return TRUE;
3086439
    if (_slope_compare (a, b))
2984157
	return FALSE;
    /* The choice of y is not truly arbitrary since we must guarantee that it
     * is greater than the start of either line.
     */
102282
    if (a->edge.line.p1.y == b->edge.line.p1.y) {
4374
	return a->edge.line.p1.x == b->edge.line.p1.x;
97908
    } else if (a->edge.line.p1.y < b->edge.line.p1.y) {
52398
	return edge_compare_for_y_against_x (b,
52398
					     a->edge.line.p1.y,
52398
					     a->edge.line.p1.x) == 0;
    } else {
45510
	return edge_compare_for_y_against_x (a,
45510
					     b->edge.line.p1.y,
45510
					     b->edge.line.p1.x) == 0;
    }
}
static void
146124
edges_end (cairo_bo_edge_t	*left,
	   int32_t		 bot,
	   cairo_polygon_t	*polygon)
{
146124
    cairo_bo_deferred_t *l = &left->deferred;
146124
    cairo_bo_edge_t *right = l->other;
146124
    assert(right->deferred.other == NULL);
146124
    if (likely (l->top < bot)) {
146124
	_cairo_polygon_add_line (polygon, &left->edge.line, l->top, bot, 1);
146124
	_cairo_polygon_add_line (polygon, &right->edge.line, l->top, bot, -1);
    }
146124
    l->other = NULL;
146124
}
static inline void
2886444
edges_start_or_continue (cairo_bo_edge_t	*left,
			 cairo_bo_edge_t	*right,
			 int			 top,
			 cairo_polygon_t	*polygon)
{
2886444
    assert (right != NULL);
2886444
    assert (right->deferred.other == NULL);
2886444
    if (left->deferred.other == right)
2735067
	return;
151377
    if (left->deferred.other != NULL) {
62061
	if (edges_colinear (left->deferred.other, right)) {
1518
	    cairo_bo_edge_t *old = left->deferred.other;
	    /* continuation on right, extend right to cover both */
1518
	    assert (old->deferred.other == NULL);
1518
	    assert (old->edge.line.p2.y > old->edge.line.p1.y);
1518
	    if (old->edge.line.p1.y < right->edge.line.p1.y)
105
		right->edge.line.p1 = old->edge.line.p1;
1518
	    if (old->edge.line.p2.y > right->edge.line.p2.y)
		right->edge.line.p2 = old->edge.line.p2;
1518
	    left->deferred.other = right;
1518
	    return;
	}
60543
	edges_end (left, top, polygon);
    }
149859
    if (! edges_colinear (left, right)) {
146124
	left->deferred.top = top;
146124
	left->deferred.other = right;
    }
}
#define is_zero(w) ((w)[0] == 0 || (w)[1] == 0)
static inline void
260220
active_edges (cairo_bo_edge_t		*left,
	      int32_t			 top,
	      cairo_polygon_t	        *polygon)
{
	cairo_bo_edge_t *right;
260220
	int winding[2] = {0, 0};
	/* Yes, this is naive. Consider this a placeholder. */
3146664
	while (left != NULL) {
3138321
	    assert (is_zero (winding));
	    do {
14235354
		winding[left->a_or_b] += left->edge.dir;
14235354
		if (! is_zero (winding))
2886444
		    break;
11348910
		if unlikely ((left->deferred.other))
2388
		    edges_end (left, top, polygon);
11348910
		left = left->next;
11348910
		if (! left)
251877
		    return;
	    } while (1);
2886444
	    right = left->next;
	    do {
6423348
		if unlikely ((right->deferred.other))
279
		    edges_end (right, top, polygon);
6423348
		winding[right->a_or_b] += right->edge.dir;
6423348
		if (is_zero (winding)) {
5866011
		    if (right->next == NULL ||
2929137
			! edges_colinear (right, right->next))
			break;
		}
3536904
		right = right->next;
	    } while (1);
2886444
	    edges_start_or_continue (left, right, top, polygon);
2886444
	    left = right->next;
	}
}
static cairo_status_t
606
intersection_sweep (cairo_bo_event_t   **start_events,
		    int			 num_events,
		    cairo_polygon_t	*polygon)
{
606
    cairo_status_t status = CAIRO_STATUS_SUCCESS; /* silence compiler */
    cairo_bo_event_queue_t event_queue;
    cairo_bo_sweep_line_t sweep_line;
    cairo_bo_event_t *event;
    cairo_bo_edge_t *left, *right;
    cairo_bo_edge_t *e1, *e2;
606
    _cairo_bo_event_queue_init (&event_queue, start_events, num_events);
606
    _cairo_bo_sweep_line_init (&sweep_line);
613905
    while ((event = _cairo_bo_event_dequeue (&event_queue))) {
612693
	if (event->point.y.ordinate != sweep_line.current_y) {
260220
	    active_edges (sweep_line.head,
			  sweep_line.current_y,
			  polygon);
260220
	    sweep_line.current_y = event->point.y.ordinate;
	}
612693
	switch (event->type) {
153480
	case CAIRO_BO_EVENT_TYPE_START:
153480
	    e1 = &((cairo_bo_start_event_t *) event)->edge;
153480
	    status = sweep_line_insert (&sweep_line, e1);
153480
	    if (unlikely (status))
		goto unwind;
153480
	    status = event_queue_insert_stop (&event_queue, e1);
153480
	    if (unlikely (status))
		goto unwind;
153480
	    left = e1->prev;
153480
	    right = e1->next;
153480
	    if (left != NULL) {
145914
		status = event_queue_insert_if_intersect_below_current_y (&event_queue, left, e1);
145914
		if (unlikely (status))
		    goto unwind;
	    }
153480
	    if (right != NULL) {
141588
		status = event_queue_insert_if_intersect_below_current_y (&event_queue, e1, right);
141588
		if (unlikely (status))
		    goto unwind;
	    }
153480
	    break;
153480
	case CAIRO_BO_EVENT_TYPE_STOP:
153480
	    e1 = ((cairo_bo_queue_event_t *) event)->e1;
153480
	    _cairo_bo_event_queue_delete (&event_queue, event);
153480
	    if (e1->deferred.other)
24117
		edges_end (e1, sweep_line.current_y, polygon);
153480
	    left = e1->prev;
153480
	    right = e1->next;
153480
	    _cairo_bo_sweep_line_delete (&sweep_line, e1);
153480
	    if (left != NULL && right != NULL) {
134394
		status = event_queue_insert_if_intersect_below_current_y (&event_queue, left, right);
134394
		if (unlikely (status))
		    goto unwind;
	    }
153480
	    break;
305733
	case CAIRO_BO_EVENT_TYPE_INTERSECTION:
305733
	    e1 = ((cairo_bo_queue_event_t *) event)->e1;
305733
	    e2 = ((cairo_bo_queue_event_t *) event)->e2;
305733
	    _cairo_bo_event_queue_delete (&event_queue, event);
	    /* skip this intersection if its edges are not adjacent */
305733
	    if (e2 != e1->next)
49701
		break;
256032
	    if (e1->deferred.other)
29115
		edges_end (e1, sweep_line.current_y, polygon);
256032
	    if (e2->deferred.other)
29682
		edges_end (e2, sweep_line.current_y, polygon);
256032
	    left = e1->prev;
256032
	    right = e2->next;
256032
	    _cairo_bo_sweep_line_swap (&sweep_line, e1, e2);
	    /* after the swap e2 is left of e1 */
256032
	    if (left != NULL) {
255102
		status = event_queue_insert_if_intersect_below_current_y (&event_queue, left, e2);
255102
		if (unlikely (status))
		    goto unwind;
	    }
256032
	    if (right != NULL) {
254646
		status = event_queue_insert_if_intersect_below_current_y (&event_queue, e1, right);
254646
		if (unlikely (status))
		    goto unwind;
	    }
256032
	    break;
	}
    }
606
 unwind:
606
    _cairo_bo_event_queue_fini (&event_queue);
606
    return status;
}
cairo_status_t
621
_cairo_polygon_intersect (cairo_polygon_t *a, int winding_a,
			  cairo_polygon_t *b, int winding_b)
{
    cairo_status_t status;
    cairo_bo_start_event_t stack_events[CAIRO_STACK_ARRAY_LENGTH (cairo_bo_start_event_t)];
    cairo_bo_start_event_t *events;
    cairo_bo_event_t *stack_event_ptrs[ARRAY_LENGTH (stack_events) + 1];
    cairo_bo_event_t **event_ptrs;
    int num_events;
    int i, j;
    /* XXX lazy */
621
    if (winding_a != CAIRO_FILL_RULE_WINDING) {
72
	status = _cairo_polygon_reduce (a, winding_a);
72
	if (unlikely (status))
	    return status;
    }
621
    if (winding_b != CAIRO_FILL_RULE_WINDING) {
	status = _cairo_polygon_reduce (b, winding_b);
	if (unlikely (status))
	    return status;
    }
621
    if (unlikely (0 == a->num_edges))
12
	return CAIRO_STATUS_SUCCESS;
609
    if (unlikely (0 == b->num_edges)) {
3
	a->num_edges = 0;
3
	return CAIRO_STATUS_SUCCESS;
    }
606
    events = stack_events;
606
    event_ptrs = stack_event_ptrs;
606
    num_events = a->num_edges + b->num_edges;
606
    if (num_events > ARRAY_LENGTH (stack_events)) {
213
	events = _cairo_malloc_ab_plus_c (num_events,
					  sizeof (cairo_bo_start_event_t) +
					  sizeof (cairo_bo_event_t *),
					  sizeof (cairo_bo_event_t *));
213
	if (unlikely (events == NULL))
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
213
	event_ptrs = (cairo_bo_event_t **) (events + num_events);
    }
606
    j = 0;
87492
    for (i = 0; i < a->num_edges; i++) {
86886
	event_ptrs[j] = (cairo_bo_event_t *) &events[j];
86886
	events[j].type = CAIRO_BO_EVENT_TYPE_START;
86886
	events[j].point.y.ordinate = a->edges[i].top;
86886
	events[j].point.y.approx = EXACT;
173772
	events[j].point.x.ordinate =
86886
	    _line_compute_intersection_x_for_y (&a->edges[i].line,
86886
						events[j].point.y.ordinate);
86886
	events[j].point.x.approx = EXACT;
86886
	events[j].edge.a_or_b = 0;
86886
	events[j].edge.edge = a->edges[i];
86886
	events[j].edge.deferred.other = NULL;
86886
	events[j].edge.prev = NULL;
86886
	events[j].edge.next = NULL;
86886
	j++;
    }
67200
    for (i = 0; i < b->num_edges; i++) {
66594
	event_ptrs[j] = (cairo_bo_event_t *) &events[j];
66594
	events[j].type = CAIRO_BO_EVENT_TYPE_START;
66594
	events[j].point.y.ordinate = b->edges[i].top;
66594
	events[j].point.y.approx = EXACT;
133188
	events[j].point.x.ordinate =
66594
	    _line_compute_intersection_x_for_y (&b->edges[i].line,
66594
						events[j].point.y.ordinate);
66594
	events[j].point.x.approx = EXACT;
66594
	events[j].edge.a_or_b = 1;
66594
	events[j].edge.edge = b->edges[i];
66594
	events[j].edge.deferred.other = NULL;
66594
	events[j].edge.prev = NULL;
66594
	events[j].edge.next = NULL;
66594
	j++;
    }
606
    assert (j == num_events);
#if 0
    {
	FILE *file = fopen ("clip_a.txt", "w");
	_cairo_debug_print_polygon (file, a);
	fclose (file);
    }
    {
	FILE *file = fopen ("clip_b.txt", "w");
	_cairo_debug_print_polygon (file, b);
	fclose (file);
    }
#endif
606
    a->num_edges = 0;
606
    status = intersection_sweep (event_ptrs, num_events, a);
606
    if (events != stack_events)
213
	free (events);
#if 0
    {
	FILE *file = fopen ("clip_result.txt", "w");
	_cairo_debug_print_polygon (file, a);
	fclose (file);
    }
#endif
606
    return status;
}
cairo_status_t
162
_cairo_polygon_intersect_with_boxes (cairo_polygon_t *polygon,
				     cairo_fill_rule_t *winding,
				     cairo_box_t *boxes,
				     int num_boxes)
{
    cairo_polygon_t b;
    cairo_status_t status;
    int n;
162
    if (num_boxes == 0) {
	polygon->num_edges = 0;
	return CAIRO_STATUS_SUCCESS;
    }
4788
    for (n = 0; n < num_boxes; n++) {
4626
	if (polygon->extents.p1.x >= boxes[n].p1.x &&
333
	    polygon->extents.p2.x <= boxes[n].p2.x &&
144
	    polygon->extents.p1.y >= boxes[n].p1.y &&
69
	    polygon->extents.p2.y <= boxes[n].p2.y)
	{
	    return CAIRO_STATUS_SUCCESS;
	}
    }
162
    _cairo_polygon_init (&b, NULL, 0);
4788
    for (n = 0; n < num_boxes; n++) {
4626
	if (boxes[n].p2.x > polygon->extents.p1.x &&
4605
	    boxes[n].p1.x < polygon->extents.p2.x &&
4599
	    boxes[n].p2.y > polygon->extents.p1.y &&
4599
	    boxes[n].p1.y < polygon->extents.p2.y)
	{
	    cairo_point_t p1, p2;
4593
	    p1.y = boxes[n].p1.y;
4593
	    p2.y = boxes[n].p2.y;
4593
	    p2.x = p1.x = boxes[n].p1.x;
4593
	    _cairo_polygon_add_external_edge (&b, &p1, &p2);
4593
	    p2.x = p1.x = boxes[n].p2.x;
4593
	    _cairo_polygon_add_external_edge (&b, &p2, &p1);
	}
    }
162
    status = _cairo_polygon_intersect (polygon, *winding,
				       &b, CAIRO_FILL_RULE_WINDING);
162
    _cairo_polygon_fini (&b);
162
    *winding = CAIRO_FILL_RULE_WINDING;
162
    return status;
}