1
/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2
/* glitter-paths - polygon scan converter
3
 *
4
 * Copyright (c) 2008  M Joonas Pihlaja
5
 * Copyright (c) 2007  David Turner
6
 *
7
 * Permission is hereby granted, free of charge, to any person
8
 * obtaining a copy of this software and associated documentation
9
 * files (the "Software"), to deal in the Software without
10
 * restriction, including without limitation the rights to use,
11
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the
13
 * Software is furnished to do so, subject to the following
14
 * conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be
17
 * included in all copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26
 * OTHER DEALINGS IN THE SOFTWARE.
27
 */
28
/* This is the Glitter paths scan converter incorporated into cairo.
29
 * The source is from commit 734c53237a867a773640bd5b64816249fa1730f8
30
 * of
31
 *
32
 *   https://gitweb.freedesktop.org/?p=users/joonas/glitter-paths
33
 */
34
/* Glitter-paths is a stand alone polygon rasteriser derived from
35
 * David Turner's reimplementation of Tor Anderssons's 15x17
36
 * supersampling rasteriser from the Apparition graphics library.  The
37
 * main new feature here is cheaply choosing per-scan line between
38
 * doing fully analytical coverage computation for an entire row at a
39
 * time vs. using a supersampling approach.
40
 *
41
 * David Turner's code can be found at
42
 *
43
 *   http://david.freetype.org/rasterizer-shootout/raster-comparison-20070813.tar.bz2
44
 *
45
 * In particular this file incorporates large parts of ftgrays_tor10.h
46
 * from raster-comparison-20070813.tar.bz2
47
 */
48
/* Overview
49
 *
50
 * A scan converter's basic purpose to take polygon edges and convert
51
 * them into an RLE compressed A8 mask.  This one works in two phases:
52
 * gathering edges and generating spans.
53
 *
54
 * 1) As the user feeds the scan converter edges they are vertically
55
 * clipped and bucketted into a _polygon_ data structure.  The edges
56
 * are also snapped from the user's coordinates to the subpixel grid
57
 * coordinates used during scan conversion.
58
 *
59
 *     user
60
 *      |
61
 *      | edges
62
 *      V
63
 *    polygon buckets
64
 *
65
 * 2) Generating spans works by performing a vertical sweep of pixel
66
 * rows from top to bottom and maintaining an _active_list_ of edges
67
 * that intersect the row.  From the active list the fill rule
68
 * determines which edges are the left and right edges of the start of
69
 * each span, and their contribution is then accumulated into a pixel
70
 * coverage list (_cell_list_) as coverage deltas.  Once the coverage
71
 * deltas of all edges are known we can form spans of constant pixel
72
 * coverage by summing the deltas during a traversal of the cell list.
73
 * At the end of a pixel row the cell list is sent to a coverage
74
 * blitter for rendering to some target surface.
75
 *
76
 * The pixel coverages are computed by either supersampling the row
77
 * and box filtering a mono rasterisation, or by computing the exact
78
 * coverages of edges in the active list.  The supersampling method is
79
 * used whenever some edge starts or stops within the row or there are
80
 * edge intersections in the row.
81
 *
82
 *   polygon bucket for       \
83
 *   current pixel row        |
84
 *      |                     |
85
 *      | activate new edges  |  Repeat GRID_Y times if we
86
 *      V                     \  are supersampling this row,
87
 *   active list              /  or just once if we're computing
88
 *      |                     |  analytical coverage.
89
 *      | coverage deltas     |
90
 *      V                     |
91
 *   pixel coverage list     /
92
 *      |
93
 *      V
94
 *   coverage blitter
95
 */
96
#include "cairoint.h"
97
#include "cairo-spans-private.h"
98
#include "cairo-error-private.h"
99

            
100
#include <assert.h>
101
#include <stdlib.h>
102
#include <string.h>
103
#include <limits.h>
104
#include <setjmp.h>
105

            
106
/* The input coordinate scale and the rasterisation grid scales. */
107
#define GLITTER_INPUT_BITS CAIRO_FIXED_FRAC_BITS
108
#define GRID_X_BITS CAIRO_FIXED_FRAC_BITS
109
#define GRID_Y 15
110

            
111
/* Set glitter up to use a cairo span renderer to do the coverage
112
 * blitting. */
113
struct pool;
114
struct cell_list;
115

            
116
/*-------------------------------------------------------------------------
117
 * glitter-paths.h
118
 */
119

            
120
/* "Input scaled" numbers are fixed precision reals with multiplier
121
 * 2**GLITTER_INPUT_BITS.  Input coordinates are given to glitter as
122
 * pixel scaled numbers.  These get converted to the internal grid
123
 * scaled numbers as soon as possible. Internal overflow is possible
124
 * if GRID_X/Y inside glitter-paths.c is larger than
125
 * 1<<GLITTER_INPUT_BITS. */
126
#ifndef GLITTER_INPUT_BITS
127
#  define GLITTER_INPUT_BITS 8
128
#endif
129
#define GLITTER_INPUT_SCALE (1<<GLITTER_INPUT_BITS)
130
typedef int glitter_input_scaled_t;
131

            
132
/* Opaque type for scan converting. */
133
typedef struct glitter_scan_converter glitter_scan_converter_t;
134

            
135
/*-------------------------------------------------------------------------
136
 * glitter-paths.c: Implementation internal types
137
 */
138
#include <stdlib.h>
139
#include <string.h>
140
#include <limits.h>
141

            
142
/* All polygon coordinates are snapped onto a subsample grid. "Grid
143
 * scaled" numbers are fixed precision reals with multiplier GRID_X or
144
 * GRID_Y. */
145
typedef int grid_scaled_t;
146
typedef int grid_scaled_x_t;
147
typedef int grid_scaled_y_t;
148

            
149
/* Default x/y scale factors.
150
 *  You can either define GRID_X/Y_BITS to get a power-of-two scale
151
 *  or define GRID_X/Y separately. */
152
#if !defined(GRID_X) && !defined(GRID_X_BITS)
153
#  define GRID_X_BITS 8
154
#endif
155
#if !defined(GRID_Y) && !defined(GRID_Y_BITS)
156
#  define GRID_Y 15
157
#endif
158

            
159
/* Use GRID_X/Y_BITS to define GRID_X/Y if they're available. */
160
#ifdef GRID_X_BITS
161
#  define GRID_X (1 << GRID_X_BITS)
162
#endif
163
#ifdef GRID_Y_BITS
164
#  define GRID_Y (1 << GRID_Y_BITS)
165
#endif
166

            
167
/* The GRID_X_TO_INT_FRAC macro splits a grid scaled coordinate into
168
 * integer and fractional parts. The integer part is floored. */
169
#if defined(GRID_X_TO_INT_FRAC)
170
  /* do nothing */
171
#elif defined(GRID_X_BITS)
172
#  define GRID_X_TO_INT_FRAC(x, i, f) \
173
	_GRID_TO_INT_FRAC_shift(x, i, f, GRID_X_BITS)
174
#else
175
#  define GRID_X_TO_INT_FRAC(x, i, f) \
176
	_GRID_TO_INT_FRAC_general(x, i, f, GRID_X)
177
#endif
178

            
179
#define _GRID_TO_INT_FRAC_general(t, i, f, m) do {	\
180
    (i) = (t) / (m);					\
181
    (f) = (t) % (m);					\
182
    if ((f) < 0) {					\
183
	--(i);						\
184
	(f) += (m);					\
185
    }							\
186
} while (0)
187

            
188
#define _GRID_TO_INT_FRAC_shift(t, i, f, b) do {	\
189
    (f) = (t) & ((1 << (b)) - 1);			\
190
    (i) = (t) >> (b);					\
191
} while (0)
192

            
193
/* A grid area is a real in [0,1] scaled by 2*GRID_X*GRID_Y.  We want
194
 * to be able to represent exactly areas of subpixel trapezoids whose
195
 * vertices are given in grid scaled coordinates.  The scale factor
196
 * comes from needing to accurately represent the area 0.5*dx*dy of a
197
 * triangle with base dx and height dy in grid scaled numbers. */
198
typedef int grid_area_t;
199
#define GRID_XY (2*GRID_X*GRID_Y) /* Unit area on the grid. */
200

            
201
/* GRID_AREA_TO_ALPHA(area): map [0,GRID_XY] to [0,255]. */
202
#if GRID_XY == 510
203
#  define GRID_AREA_TO_ALPHA(c)	  (((c)+1) >> 1)
204
#elif GRID_XY == 255
205
#  define  GRID_AREA_TO_ALPHA(c)  (c)
206
#elif GRID_XY == 64
207
#  define  GRID_AREA_TO_ALPHA(c)  (((c) << 2) | -(((c) & 0x40) >> 6))
208
#elif GRID_XY == 128
209
#  define  GRID_AREA_TO_ALPHA(c)  ((((c) << 1) | -((c) >> 7)) & 255)
210
#elif GRID_XY == 256
211
#  define  GRID_AREA_TO_ALPHA(c)  (((c) | -((c) >> 8)) & 255)
212
#elif GRID_XY == 15
213
#  define  GRID_AREA_TO_ALPHA(c)  (((c) << 4) + (c))
214
#elif GRID_XY == 2*256*15
215
#  define  GRID_AREA_TO_ALPHA(c)  (((c) + ((c)<<4) + 256) >> 9)
216
#else
217
#  define  GRID_AREA_TO_ALPHA(c)  (((c)*255 + GRID_XY/2) / GRID_XY)
218
#endif
219

            
220
#define UNROLL3(x) x x x
221

            
222
struct quorem {
223
    int32_t quo;
224
    int32_t rem;
225
};
226

            
227
/* Header for a chunk of memory in a memory pool. */
228
struct _pool_chunk {
229
    /* # bytes used in this chunk. */
230
    size_t size;
231

            
232
    /* # bytes total in this chunk */
233
    size_t capacity;
234

            
235
    /* Pointer to the previous chunk or %NULL if this is the sentinel
236
     * chunk in the pool header. */
237
    struct _pool_chunk *prev_chunk;
238

            
239
    /* Actual data starts here.	 Well aligned for pointers. */
240
};
241

            
242
/* A memory pool.  This is supposed to be embedded on the stack or
243
 * within some other structure.	 It may optionally be followed by an
244
 * embedded array from which requests are fulfilled until
245
 * malloc needs to be called to allocate a first real chunk. */
246
struct pool {
247
    /* Chunk we're allocating from. */
248
    struct _pool_chunk *current;
249

            
250
    jmp_buf *jmp;
251

            
252
    /* Free list of previously allocated chunks.  All have >= default
253
     * capacity. */
254
    struct _pool_chunk *first_free;
255

            
256
    /* The default capacity of a chunk. */
257
    size_t default_capacity;
258

            
259
    /* Header for the sentinel chunk.  Directly following the pool
260
     * struct should be some space for embedded elements from which
261
     * the sentinel chunk allocates from. */
262
    struct _pool_chunk sentinel[1];
263
};
264

            
265
/* A polygon edge. */
266
struct edge {
267
    /* Next in y-bucket or active list. */
268
    struct edge *next;
269

            
270
    /* Current x coordinate while the edge is on the active
271
     * list. Initialised to the x coordinate of the top of the
272
     * edge. The quotient is in grid_scaled_x_t units and the
273
     * remainder is mod dy in grid_scaled_y_t units.*/
274
    struct quorem x;
275

            
276
    /* Advance of the current x when moving down a subsample line. */
277
    struct quorem dxdy;
278

            
279
    /* Advance of the current x when moving down a full pixel
280
     * row. Only initialised when the height of the edge is large
281
     * enough that there's a chance the edge could be stepped by a
282
     * full row's worth of subsample rows at a time. */
283
    struct quorem dxdy_full;
284

            
285
    /* The clipped y of the top of the edge. */
286
    grid_scaled_y_t ytop;
287

            
288
    /* y2-y1 after orienting the edge downwards.  */
289
    grid_scaled_y_t dy;
290

            
291
    /* Number of subsample rows remaining to scan convert of this
292
     * edge. */
293
    grid_scaled_y_t height_left;
294

            
295
    /* Original sign of the edge: +1 for downwards, -1 for upwards
296
     * edges.  */
297
    int dir;
298
    int vertical;
299
    int clip;
300
};
301

            
302
/* Number of subsample rows per y-bucket. Must be GRID_Y. */
303
#define EDGE_Y_BUCKET_HEIGHT GRID_Y
304

            
305
#define EDGE_Y_BUCKET_INDEX(y, ymin) (((y) - (ymin))/EDGE_Y_BUCKET_HEIGHT)
306

            
307
/* A collection of sorted and vertically clipped edges of the polygon.
308
 * Edges are moved from the polygon to an active list while scan
309
 * converting. */
310
struct polygon {
311
    /* The vertical clip extents. */
312
    grid_scaled_y_t ymin, ymax;
313

            
314
    /* Array of edges all starting in the same bucket.	An edge is put
315
     * into bucket EDGE_BUCKET_INDEX(edge->ytop, polygon->ymin) when
316
     * it is added to the polygon. */
317
    struct edge **y_buckets;
318
    struct edge *y_buckets_embedded[64];
319

            
320
    struct {
321
	struct pool base[1];
322
	struct edge embedded[32];
323
    } edge_pool;
324
};
325

            
326
/* A cell records the effect on pixel coverage of polygon edges
327
 * passing through a pixel.  It contains two accumulators of pixel
328
 * coverage.
329
 *
330
 * Consider the effects of a polygon edge on the coverage of a pixel
331
 * it intersects and that of the following one.  The coverage of the
332
 * following pixel is the height of the edge multiplied by the width
333
 * of the pixel, and the coverage of the pixel itself is the area of
334
 * the trapezoid formed by the edge and the right side of the pixel.
335
 *
336
 * +-----------------------+-----------------------+
337
 * |                       |                       |
338
 * |                       |                       |
339
 * |_______________________|_______________________|
340
 * |   \...................|.......................|\
341
 * |    \..................|.......................| |
342
 * |     \.................|.......................| |
343
 * |      \....covered.....|.......................| |
344
 * |       \....area.......|.......................| } covered height
345
 * |        \..............|.......................| |
346
 * |uncovered\.............|.......................| |
347
 * |  area    \............|.......................| |
348
 * |___________\...........|.......................|/
349
 * |                       |                       |
350
 * |                       |                       |
351
 * |                       |                       |
352
 * +-----------------------+-----------------------+
353
 *
354
 * Since the coverage of the following pixel will always be a multiple
355
 * of the width of the pixel, we can store the height of the covered
356
 * area instead.  The coverage of the pixel itself is the total
357
 * coverage minus the area of the uncovered area to the left of the
358
 * edge.  As it's faster to compute the uncovered area we only store
359
 * that and subtract it from the total coverage later when forming
360
 * spans to blit.
361
 *
362
 * The heights and areas are signed, with left edges of the polygon
363
 * having positive sign and right edges having negative sign.  When
364
 * two edges intersect they swap their left/rightness so their
365
 * contribution above and below the intersection point must be
366
 * computed separately. */
367
struct cell {
368
    struct cell		*next;
369
    int			 x;
370
    grid_area_t		 uncovered_area;
371
    grid_scaled_y_t	 covered_height;
372
    grid_scaled_y_t	 clipped_height;
373
};
374

            
375
/* A cell list represents the scan line sparsely as cells ordered by
376
 * ascending x.  It is geared towards scanning the cells in order
377
 * using an internal cursor. */
378
struct cell_list {
379
    /* Sentinel nodes */
380
    struct cell head, tail;
381

            
382
    /* Cursor state for iterating through the cell list. */
383
    struct cell *cursor;
384

            
385
    /* Cells in the cell list are owned by the cell list and are
386
     * allocated from this pool.  */
387
    struct {
388
	struct pool base[1];
389
	struct cell embedded[32];
390
    } cell_pool;
391
};
392

            
393
struct cell_pair {
394
    struct cell *cell1;
395
    struct cell *cell2;
396
};
397

            
398
/* The active list contains edges in the current scan line ordered by
399
 * the x-coordinate of the intercept of the edge and the scan line. */
400
struct active_list {
401
    /* Leftmost edge on the current scan line. */
402
    struct edge *head;
403

            
404
    /* A lower bound on the height of the active edges is used to
405
     * estimate how soon some active edge ends.	 We can't advance the
406
     * scan conversion by a full pixel row if an edge ends somewhere
407
     * within it. */
408
    grid_scaled_y_t min_height;
409
};
410

            
411
struct glitter_scan_converter {
412
    struct polygon	polygon[1];
413
    struct active_list	active[1];
414
    struct cell_list	coverages[1];
415

            
416
    /* Clip box. */
417
    grid_scaled_y_t ymin, ymax;
418
};
419

            
420
/* Compute the floored division a/b. Assumes / and % perform symmetric
421
 * division. */
422
inline static struct quorem
423
floored_divrem(int a, int b)
424
{
425
    struct quorem qr;
426
    qr.quo = a/b;
427
    qr.rem = a%b;
428
    if ((a^b)<0 && qr.rem) {
429
	qr.quo -= 1;
430
	qr.rem += b;
431
    }
432
    return qr;
433
}
434

            
435
/* Compute the floored division (x*a)/b. Assumes / and % perform symmetric
436
 * division. */
437
static struct quorem
438
floored_muldivrem(int x, int a, int b)
439
{
440
    struct quorem qr;
441
    long long xa = (long long)x*a;
442
    qr.quo = xa/b;
443
    qr.rem = xa%b;
444
    if ((xa>=0) != (b>=0) && qr.rem) {
445
	qr.quo -= 1;
446
	qr.rem += b;
447
    }
448
    return qr;
449
}
450

            
451
static struct _pool_chunk *
452
_pool_chunk_init(
453
    struct _pool_chunk *p,
454
    struct _pool_chunk *prev_chunk,
455
    size_t capacity)
456
{
457
    p->prev_chunk = prev_chunk;
458
    p->size = 0;
459
    p->capacity = capacity;
460
    return p;
461
}
462

            
463
static struct _pool_chunk *
464
_pool_chunk_create(struct pool *pool, size_t size)
465
{
466
    struct _pool_chunk *p;
467

            
468
    p = _cairo_malloc (size + sizeof(struct _pool_chunk));
469
    if (unlikely (NULL == p))
470
	longjmp (*pool->jmp, _cairo_error (CAIRO_STATUS_NO_MEMORY));
471

            
472
    return _pool_chunk_init(p, pool->current, size);
473
}
474

            
475
static void
476
pool_init(struct pool *pool,
477
	  jmp_buf *jmp,
478
	  size_t default_capacity,
479
	  size_t embedded_capacity)
480
{
481
    pool->jmp = jmp;
482
    pool->current = pool->sentinel;
483
    pool->first_free = NULL;
484
    pool->default_capacity = default_capacity;
485
    _pool_chunk_init(pool->sentinel, NULL, embedded_capacity);
486
}
487

            
488
static void
489
pool_fini(struct pool *pool)
490
{
491
    struct _pool_chunk *p = pool->current;
492
    do {
493
	while (NULL != p) {
494
	    struct _pool_chunk *prev = p->prev_chunk;
495
	    if (p != pool->sentinel)
496
		free(p);
497
	    p = prev;
498
	}
499
	p = pool->first_free;
500
	pool->first_free = NULL;
501
    } while (NULL != p);
502
}
503

            
504
/* Satisfy an allocation by first allocating a new large enough chunk
505
 * and adding it to the head of the pool's chunk list. This function
506
 * is called as a fallback if pool_alloc() couldn't do a quick
507
 * allocation from the current chunk in the pool. */
508
static void *
509
_pool_alloc_from_new_chunk(
510
    struct pool *pool,
511
    size_t size)
512
{
513
    struct _pool_chunk *chunk;
514
    void *obj;
515
    size_t capacity;
516

            
517
    /* If the allocation is smaller than the default chunk size then
518
     * try getting a chunk off the free list.  Force alloc of a new
519
     * chunk for large requests. */
520
    capacity = size;
521
    chunk = NULL;
522
    if (size < pool->default_capacity) {
523
	capacity = pool->default_capacity;
524
	chunk = pool->first_free;
525
	if (chunk) {
526
	    pool->first_free = chunk->prev_chunk;
527
	    _pool_chunk_init(chunk, pool->current, chunk->capacity);
528
	}
529
    }
530

            
531
    if (NULL == chunk)
532
	chunk = _pool_chunk_create (pool, capacity);
533
    pool->current = chunk;
534

            
535
    obj = ((unsigned char*)chunk + sizeof(*chunk) + chunk->size);
536
    chunk->size += size;
537
    return obj;
538
}
539

            
540
/* Allocate size bytes from the pool.  The first allocated address
541
 * returned from a pool is aligned to sizeof(void*).  Subsequent
542
 * addresses will maintain alignment as long as multiples of void* are
543
 * allocated.  Returns the address of a new memory area or %NULL on
544
 * allocation failures.	 The pool retains ownership of the returned
545
 * memory. */
546
inline static void *
547
pool_alloc (struct pool *pool, size_t size)
548
{
549
    struct _pool_chunk *chunk = pool->current;
550

            
551
    if (size <= chunk->capacity - chunk->size) {
552
	void *obj = ((unsigned char*)chunk + sizeof(*chunk) + chunk->size);
553
	chunk->size += size;
554
	return obj;
555
    } else {
556
	return _pool_alloc_from_new_chunk(pool, size);
557
    }
558
}
559

            
560
/* Relinquish all pool_alloced memory back to the pool. */
561
static void
562
pool_reset (struct pool *pool)
563
{
564
    /* Transfer all used chunks to the chunk free list. */
565
    struct _pool_chunk *chunk = pool->current;
566
    if (chunk != pool->sentinel) {
567
	while (chunk->prev_chunk != pool->sentinel) {
568
	    chunk = chunk->prev_chunk;
569
	}
570
	chunk->prev_chunk = pool->first_free;
571
	pool->first_free = pool->current;
572
    }
573
    /* Reset the sentinel as the current chunk. */
574
    pool->current = pool->sentinel;
575
    pool->sentinel->size = 0;
576
}
577

            
578
/* Rewinds the cell list's cursor to the beginning.  After rewinding
579
 * we're good to cell_list_find() the cell any x coordinate. */
580
inline static void
581
cell_list_rewind (struct cell_list *cells)
582
{
583
    cells->cursor = &cells->head;
584
}
585

            
586
/* Rewind the cell list if its cursor has been advanced past x. */
587
inline static void
588
cell_list_maybe_rewind (struct cell_list *cells, int x)
589
{
590
    struct cell *tail = cells->cursor;
591
    if (tail->x > x)
592
	cell_list_rewind (cells);
593
}
594

            
595
static void
596
cell_list_init(struct cell_list *cells, jmp_buf *jmp)
597
{
598
    pool_init(cells->cell_pool.base, jmp,
599
	      256*sizeof(struct cell),
600
	      sizeof(cells->cell_pool.embedded));
601
    cells->tail.next = NULL;
602
    cells->tail.x = INT_MAX;
603
    cells->head.x = INT_MIN;
604
    cells->head.next = &cells->tail;
605
    cell_list_rewind (cells);
606
}
607

            
608
static void
609
cell_list_fini(struct cell_list *cells)
610
{
611
    pool_fini (cells->cell_pool.base);
612
}
613

            
614
/* Empty the cell list.  This is called at the start of every pixel
615
 * row. */
616
inline static void
617
cell_list_reset (struct cell_list *cells)
618
{
619
    cell_list_rewind (cells);
620
    cells->head.next = &cells->tail;
621
    pool_reset (cells->cell_pool.base);
622
}
623

            
624
static struct cell *
625
cell_list_alloc (struct cell_list *cells,
626
		 struct cell *tail,
627
		 int x)
628
{
629
    struct cell *cell;
630

            
631
    cell = pool_alloc (cells->cell_pool.base, sizeof (struct cell));
632
    cell->next = tail->next;
633
    tail->next = cell;
634
    cell->x = x;
635
    cell->uncovered_area = 0;
636
    cell->covered_height = 0;
637
    cell->clipped_height = 0;
638
    return cell;
639
}
640

            
641
/* Find a cell at the given x-coordinate.  Returns %NULL if a new cell
642
 * needed to be allocated but couldn't be.  Cells must be found with
643
 * non-decreasing x-coordinate until the cell list is rewound using
644
 * cell_list_rewind(). Ownership of the returned cell is retained by
645
 * the cell list. */
646
inline static struct cell *
647
cell_list_find (struct cell_list *cells, int x)
648
{
649
    struct cell *tail = cells->cursor;
650

            
651
    while (1) {
652
	UNROLL3({
653
	    if (tail->next->x > x)
654
		break;
655
	    tail = tail->next;
656
	});
657
    }
658

            
659
    if (tail->x != x)
660
	tail = cell_list_alloc (cells, tail, x);
661
    return cells->cursor = tail;
662

            
663
}
664

            
665
/* Find two cells at x1 and x2.	 This is exactly equivalent
666
 * to
667
 *
668
 *   pair.cell1 = cell_list_find(cells, x1);
669
 *   pair.cell2 = cell_list_find(cells, x2);
670
 *
671
 * except with less function call overhead. */
672
inline static struct cell_pair
673
cell_list_find_pair(struct cell_list *cells, int x1, int x2)
674
{
675
    struct cell_pair pair;
676

            
677
    pair.cell1 = cells->cursor;
678
    while (1) {
679
	UNROLL3({
680
	    if (pair.cell1->next->x > x1)
681
		break;
682
	    pair.cell1 = pair.cell1->next;
683
	});
684
    }
685
    if (pair.cell1->x != x1) {
686
	struct cell *cell = pool_alloc (cells->cell_pool.base,
687
					sizeof (struct cell));
688
	cell->x = x1;
689
	cell->uncovered_area = 0;
690
	cell->covered_height = 0;
691
	cell->clipped_height = 0;
692
	cell->next = pair.cell1->next;
693
	pair.cell1->next = cell;
694
	pair.cell1 = cell;
695
    }
696

            
697
    pair.cell2 = pair.cell1;
698
    while (1) {
699
	UNROLL3({
700
	    if (pair.cell2->next->x > x2)
701
		break;
702
	    pair.cell2 = pair.cell2->next;
703
	});
704
    }
705
    if (pair.cell2->x != x2) {
706
	struct cell *cell = pool_alloc (cells->cell_pool.base,
707
					sizeof (struct cell));
708
	cell->uncovered_area = 0;
709
	cell->covered_height = 0;
710
	cell->clipped_height = 0;
711
	cell->x = x2;
712
	cell->next = pair.cell2->next;
713
	pair.cell2->next = cell;
714
	pair.cell2 = cell;
715
    }
716

            
717
    cells->cursor = pair.cell2;
718
    return pair;
719
}
720

            
721
/* Add a subpixel span covering [x1, x2) to the coverage cells. */
722
inline static void
723
cell_list_add_subspan(struct cell_list *cells,
724
		      grid_scaled_x_t x1,
725
		      grid_scaled_x_t x2)
726
{
727
    int ix1, fx1;
728
    int ix2, fx2;
729

            
730
    GRID_X_TO_INT_FRAC(x1, ix1, fx1);
731
    GRID_X_TO_INT_FRAC(x2, ix2, fx2);
732

            
733
    if (ix1 != ix2) {
734
	struct cell_pair p;
735
	p = cell_list_find_pair(cells, ix1, ix2);
736
	p.cell1->uncovered_area += 2*fx1;
737
	++p.cell1->covered_height;
738
	p.cell2->uncovered_area -= 2*fx2;
739
	--p.cell2->covered_height;
740
    } else {
741
	struct cell *cell = cell_list_find(cells, ix1);
742
	cell->uncovered_area += 2*(fx1-fx2);
743
    }
744
}
745

            
746
/* Adds the analytical coverage of an edge crossing the current pixel
747
 * row to the coverage cells and advances the edge's x position to the
748
 * following row.
749
 *
750
 * This function is only called when we know that during this pixel row:
751
 *
752
 * 1) The relative order of all edges on the active list doesn't
753
 * change.  In particular, no edges intersect within this row to pixel
754
 * precision.
755
 *
756
 * 2) No new edges start in this row.
757
 *
758
 * 3) No existing edges end mid-row.
759
 *
760
 * This function depends on being called with all edges from the
761
 * active list in the order they appear on the list (i.e. with
762
 * non-decreasing x-coordinate.)  */
763
static void
764
cell_list_render_edge(
765
    struct cell_list *cells,
766
    struct edge *edge,
767
    int sign)
768
{
769
    grid_scaled_y_t y1, y2, dy;
770
    grid_scaled_x_t dx;
771
    int ix1, ix2;
772
    grid_scaled_x_t fx1, fx2;
773

            
774
    struct quorem x1 = edge->x;
775
    struct quorem x2 = x1;
776

            
777
    if (! edge->vertical) {
778
	x2.quo += edge->dxdy_full.quo;
779
	x2.rem += edge->dxdy_full.rem;
780
	if (x2.rem >= 0) {
781
	    ++x2.quo;
782
	    x2.rem -= edge->dy;
783
	}
784

            
785
	edge->x = x2;
786
    }
787

            
788
    GRID_X_TO_INT_FRAC(x1.quo, ix1, fx1);
789
    GRID_X_TO_INT_FRAC(x2.quo, ix2, fx2);
790

            
791
    /* Edge is entirely within a column? */
792
    if (ix1 == ix2) {
793
	/* We always know that ix1 is >= the cell list cursor in this
794
	 * case due to the no-intersections precondition.  */
795
	struct cell *cell = cell_list_find(cells, ix1);
796
	cell->covered_height += sign*GRID_Y;
797
	cell->uncovered_area += sign*(fx1 + fx2)*GRID_Y;
798
	return;
799
    }
800

            
801
    /* Orient the edge left-to-right. */
802
    dx = x2.quo - x1.quo;
803
    if (dx >= 0) {
804
	y1 = 0;
805
	y2 = GRID_Y;
806
    } else {
807
	int tmp;
808
	tmp = ix1; ix1 = ix2; ix2 = tmp;
809
	tmp = fx1; fx1 = fx2; fx2 = tmp;
810
	dx = -dx;
811
	sign = -sign;
812
	y1 = GRID_Y;
813
	y2 = 0;
814
    }
815
    dy = y2 - y1;
816

            
817
    /* Add coverage for all pixels [ix1,ix2] on this row crossed
818
     * by the edge. */
819
    {
820
	struct cell_pair pair;
821
	struct quorem y = floored_divrem((GRID_X - fx1)*dy, dx);
822

            
823
	/* When rendering a previous edge on the active list we may
824
	 * advance the cell list cursor past the leftmost pixel of the
825
	 * current edge even though the two edges don't intersect.
826
	 * e.g. consider two edges going down and rightwards:
827
	 *
828
	 *  --\_+---\_+-----+-----+----
829
	 *      \_    \_    |     |
830
	 *      | \_  | \_  |     |
831
	 *      |   \_|   \_|     |
832
	 *      |     \_    \_    |
833
	 *  ----+-----+-\---+-\---+----
834
	 *
835
	 * The left edge touches cells past the starting cell of the
836
	 * right edge.  Fortunately such cases are rare.
837
	 *
838
	 * The rewinding is never necessary if the current edge stays
839
	 * within a single column because we've checked before calling
840
	 * this function that the active list order won't change. */
841
	cell_list_maybe_rewind(cells, ix1);
842

            
843
	pair = cell_list_find_pair(cells, ix1, ix1+1);
844
	pair.cell1->uncovered_area += sign*y.quo*(GRID_X + fx1);
845
	pair.cell1->covered_height += sign*y.quo;
846
	y.quo += y1;
847

            
848
	if (ix1+1 < ix2) {
849
	    struct quorem dydx_full = floored_divrem(GRID_X*dy, dx);
850
	    struct cell *cell = pair.cell2;
851

            
852
	    ++ix1;
853
	    do {
854
		grid_scaled_y_t y_skip = dydx_full.quo;
855
		y.rem += dydx_full.rem;
856
		if (y.rem >= dx) {
857
		    ++y_skip;
858
		    y.rem -= dx;
859
		}
860

            
861
		y.quo += y_skip;
862

            
863
		y_skip *= sign;
864
		cell->uncovered_area += y_skip*GRID_X;
865
		cell->covered_height += y_skip;
866

            
867
		++ix1;
868
		cell = cell_list_find(cells, ix1);
869
	    } while (ix1 != ix2);
870

            
871
	    pair.cell2 = cell;
872
	}
873
	pair.cell2->uncovered_area += sign*(y2 - y.quo)*fx2;
874
	pair.cell2->covered_height += sign*(y2 - y.quo);
875
    }
876
}
877

            
878
static void
879
polygon_init (struct polygon *polygon, jmp_buf *jmp)
880
{
881
    polygon->ymin = polygon->ymax = 0;
882
    polygon->y_buckets = polygon->y_buckets_embedded;
883
    pool_init (polygon->edge_pool.base, jmp,
884
	       8192 - sizeof (struct _pool_chunk),
885
	       sizeof (polygon->edge_pool.embedded));
886
}
887

            
888
static void
889
polygon_fini (struct polygon *polygon)
890
{
891
    if (polygon->y_buckets != polygon->y_buckets_embedded)
892
	free (polygon->y_buckets);
893

            
894
    pool_fini (polygon->edge_pool.base);
895
}
896

            
897
/* Empties the polygon of all edges. The polygon is then prepared to
898
 * receive new edges and clip them to the vertical range
899
 * [ymin,ymax). */
900
static cairo_status_t
901
polygon_reset (struct polygon *polygon,
902
	       grid_scaled_y_t ymin,
903
	       grid_scaled_y_t ymax)
904
{
905
    unsigned h = ymax - ymin;
906
    unsigned num_buckets = EDGE_Y_BUCKET_INDEX(ymax + EDGE_Y_BUCKET_HEIGHT-1,
907
					       ymin);
908

            
909
    pool_reset(polygon->edge_pool.base);
910

            
911
    if (unlikely (h > 0x7FFFFFFFU - EDGE_Y_BUCKET_HEIGHT))
912
	goto bail_no_mem; /* even if you could, you wouldn't want to. */
913

            
914
    if (polygon->y_buckets != polygon->y_buckets_embedded)
915
	free (polygon->y_buckets);
916

            
917
    polygon->y_buckets =  polygon->y_buckets_embedded;
918
    if (num_buckets > ARRAY_LENGTH (polygon->y_buckets_embedded)) {
919
	polygon->y_buckets = _cairo_malloc_ab (num_buckets,
920
					       sizeof (struct edge *));
921
	if (unlikely (NULL == polygon->y_buckets))
922
	    goto bail_no_mem;
923
    }
924
    memset (polygon->y_buckets, 0, num_buckets * sizeof (struct edge *));
925

            
926
    polygon->ymin = ymin;
927
    polygon->ymax = ymax;
928
    return CAIRO_STATUS_SUCCESS;
929

            
930
 bail_no_mem:
931
    polygon->ymin = 0;
932
    polygon->ymax = 0;
933
    return CAIRO_STATUS_NO_MEMORY;
934
}
935

            
936
static void
937
_polygon_insert_edge_into_its_y_bucket(
938
    struct polygon *polygon,
939
    struct edge *e)
940
{
941
    unsigned ix = EDGE_Y_BUCKET_INDEX(e->ytop, polygon->ymin);
942
    struct edge **ptail = &polygon->y_buckets[ix];
943
    e->next = *ptail;
944
    *ptail = e;
945
}
946

            
947
inline static void
948
polygon_add_edge (struct polygon *polygon,
949
		  const cairo_edge_t *edge,
950
		  int clip)
951
{
952
    struct edge *e;
953
    grid_scaled_x_t dx;
954
    grid_scaled_y_t dy;
955
    grid_scaled_y_t ytop, ybot;
956
    grid_scaled_y_t ymin = polygon->ymin;
957
    grid_scaled_y_t ymax = polygon->ymax;
958

            
959
    assert (edge->bottom > edge->top);
960

            
961
    if (unlikely (edge->top >= ymax || edge->bottom <= ymin))
962
	return;
963

            
964
    e = pool_alloc (polygon->edge_pool.base, sizeof (struct edge));
965

            
966
    dx = edge->line.p2.x - edge->line.p1.x;
967
    dy = edge->line.p2.y - edge->line.p1.y;
968
    e->dy = dy;
969
    e->dir = edge->dir;
970
    e->clip = clip;
971

            
972
    ytop = edge->top >= ymin ? edge->top : ymin;
973
    ybot = edge->bottom <= ymax ? edge->bottom : ymax;
974
    e->ytop = ytop;
975
    e->height_left = ybot - ytop;
976

            
977
    if (dx == 0) {
978
	e->vertical = TRUE;
979
	e->x.quo = edge->line.p1.x;
980
	e->x.rem = 0;
981
	e->dxdy.quo = 0;
982
	e->dxdy.rem = 0;
983
	e->dxdy_full.quo = 0;
984
	e->dxdy_full.rem = 0;
985
    } else {
986
	e->vertical = FALSE;
987
	e->dxdy = floored_divrem (dx, dy);
988
	if (ytop == edge->line.p1.y) {
989
	    e->x.quo = edge->line.p1.x;
990
	    e->x.rem = 0;
991
	} else {
992
	    e->x = floored_muldivrem (ytop - edge->line.p1.y, dx, dy);
993
	    e->x.quo += edge->line.p1.x;
994
	}
995

            
996
	if (e->height_left >= GRID_Y) {
997
	    e->dxdy_full = floored_muldivrem (GRID_Y, dx, dy);
998
	} else {
999
	    e->dxdy_full.quo = 0;
	    e->dxdy_full.rem = 0;
	}
    }
    _polygon_insert_edge_into_its_y_bucket (polygon, e);
    e->x.rem -= dy;		/* Bias the remainder for faster
				 * edge advancement. */
}
static void
active_list_reset (struct active_list *active)
{
    active->head = NULL;
    active->min_height = 0;
}
static void
active_list_init(struct active_list *active)
{
    active_list_reset(active);
}
/*
 * Merge two sorted edge lists.
 * Input:
 *  - head_a: The head of the first list.
 *  - head_b: The head of the second list; head_b cannot be NULL.
 * Output:
 * Returns the head of the merged list.
 *
 * Implementation notes:
 * To make it fast (in particular, to reduce to an insertion sort whenever
 * one of the two input lists only has a single element) we iterate through
 * a list until its head becomes greater than the head of the other list,
 * then we switch their roles. As soon as one of the two lists is empty, we
 * just attach the other one to the current list and exit.
 * Writes to memory are only needed to "switch" lists (as it also requires
 * attaching to the output list the list which we will be iterating next) and
 * to attach the last non-empty list.
 */
static struct edge *
merge_sorted_edges (struct edge *head_a, struct edge *head_b)
{
    struct edge *head, **next;
    int32_t x;
    if (head_a == NULL)
	return head_b;
    next = &head;
    if (head_a->x.quo <= head_b->x.quo) {
	head = head_a;
    } else {
	head = head_b;
	goto start_with_b;
    }
    do {
	x = head_b->x.quo;
	while (head_a != NULL && head_a->x.quo <= x) {
	    next = &head_a->next;
	    head_a = head_a->next;
	}
	*next = head_b;
	if (head_a == NULL)
	    return head;
start_with_b:
	x = head_a->x.quo;
	while (head_b != NULL && head_b->x.quo <= x) {
	    next = &head_b->next;
	    head_b = head_b->next;
	}
	*next = head_a;
	if (head_b == NULL)
	    return head;
    } while (1);
}
/*
 * Sort (part of) a list.
 * Input:
 *  - list: The list to be sorted; list cannot be NULL.
 *  - limit: Recursion limit.
 * Output:
 *  - head_out: The head of the sorted list containing the first 2^(level+1) elements of the
 *              input list; if the input list has fewer elements, head_out be a sorted list
 *              containing all the elements of the input list.
 * Returns the head of the list of unprocessed elements (NULL if the sorted list contains
 * all the elements of the input list).
 *
 * Implementation notes:
 * Special case single element list, unroll/inline the sorting of the first two elements.
 * Some tail recursion is used since we iterate on the bottom-up solution of the problem
 * (we start with a small sorted list and keep merging other lists of the same size to it).
 */
static struct edge *
sort_edges (struct edge  *list,
	    unsigned int  level,
	    struct edge **head_out)
{
    struct edge *head_other, *remaining;
    unsigned int i;
    head_other = list->next;
    /* Single element list -> return */
    if (head_other == NULL) {
	*head_out = list;
	return NULL;
    }
    /* Unroll the first iteration of the following loop (halves the number of calls to merge_sorted_edges):
     *  - Initialize remaining to be the list containing the elements after the second in the input list.
     *  - Initialize *head_out to be the sorted list containing the first two element.
     */
    remaining = head_other->next;
    if (list->x.quo <= head_other->x.quo) {
	*head_out = list;
	/* list->next = head_other; */ /* The input list is already like this. */
	head_other->next = NULL;
    } else {
	*head_out = head_other;
	head_other->next = list;
	list->next = NULL;
    }
    for (i = 0; i < level && remaining; i++) {
	/* Extract a sorted list of the same size as *head_out
	 * (2^(i+1) elements) from the list of remaining elements. */
	remaining = sort_edges (remaining, i, &head_other);
	*head_out = merge_sorted_edges (*head_out, head_other);
    }
    /* *head_out now contains (at most) 2^(level+1) elements. */
    return remaining;
}
/* Test if the edges on the active list can be safely advanced by a
 * full row without intersections or any edges ending. */
inline static int
active_list_can_step_full_row (struct active_list *active)
{
    const struct edge *e;
    int prev_x = INT_MIN;
    /* Recomputes the minimum height of all edges on the active
     * list if we have been dropping edges. */
    if (active->min_height <= 0) {
	int min_height = INT_MAX;
	e = active->head;
	while (NULL != e) {
	    if (e->height_left < min_height)
		min_height = e->height_left;
	    e = e->next;
	}
	active->min_height = min_height;
    }
    if (active->min_height < GRID_Y)
	return 0;
    /* Check for intersections as no edges end during the next row. */
    e = active->head;
    while (NULL != e) {
	struct quorem x = e->x;
	if (! e->vertical) {
	    x.quo += e->dxdy_full.quo;
	    x.rem += e->dxdy_full.rem;
	    if (x.rem >= 0)
		++x.quo;
	}
	if (x.quo <= prev_x)
	    return 0;
	prev_x = x.quo;
	e = e->next;
    }
    return 1;
}
/* Merges edges on the given subpixel row from the polygon to the
 * active_list. */
inline static void
active_list_merge_edges_from_polygon(struct active_list *active,
				     struct edge **ptail,
				     grid_scaled_y_t y,
				     struct polygon *polygon)
{
    /* Split off the edges on the current subrow and merge them into
     * the active list. */
    int min_height = active->min_height;
    struct edge *subrow_edges = NULL;
    struct edge *tail = *ptail;
    do {
	struct edge *next = tail->next;
	if (y == tail->ytop) {
	    tail->next = subrow_edges;
	    subrow_edges = tail;
	    if (tail->height_left < min_height)
		min_height = tail->height_left;
	    *ptail = next;
	} else
	    ptail = &tail->next;
	tail = next;
    } while (tail);
    if (subrow_edges) {
	sort_edges (subrow_edges, UINT_MAX, &subrow_edges);
	active->head = merge_sorted_edges (active->head, subrow_edges);
	active->min_height = min_height;
    }
}
/* Advance the edges on the active list by one subsample row by
 * updating their x positions.  Drop edges from the list that end. */
inline static void
active_list_substep_edges(struct active_list *active)
{
    struct edge **cursor = &active->head;
    grid_scaled_x_t prev_x = INT_MIN;
    struct edge *unsorted = NULL;
    struct edge *edge = *cursor;
    do {
	UNROLL3({
	    struct edge *next;
	    if (NULL == edge)
		break;
	    next = edge->next;
	    if (--edge->height_left) {
		edge->x.quo += edge->dxdy.quo;
		edge->x.rem += edge->dxdy.rem;
		if (edge->x.rem >= 0) {
		    ++edge->x.quo;
		    edge->x.rem -= edge->dy;
		}
		if (edge->x.quo < prev_x) {
		    *cursor = next;
		    edge->next = unsorted;
		    unsorted = edge;
		} else {
		    prev_x = edge->x.quo;
		    cursor = &edge->next;
		}
	    } else {
		 *cursor = next;
	    }
	    edge = next;
	})
    } while (1);
    if (unsorted) {
	sort_edges (unsorted, UINT_MAX, &unsorted);
	active->head = merge_sorted_edges (active->head, unsorted);
    }
}
inline static void
apply_nonzero_fill_rule_for_subrow (struct active_list *active,
				    struct cell_list *coverages)
{
    struct edge *edge = active->head;
    int winding = 0;
    int xstart;
    int xend;
    cell_list_rewind (coverages);
    while (NULL != edge) {
	xstart = edge->x.quo;
	winding = edge->dir;
	while (1) {
	    edge = edge->next;
	    if (NULL == edge) {
		ASSERT_NOT_REACHED;
		return;
	    }
	    winding += edge->dir;
	    if (0 == winding) {
		if (edge->next == NULL || edge->next->x.quo != edge->x.quo)
		    break;
	    }
	}
	xend = edge->x.quo;
	cell_list_add_subspan (coverages, xstart, xend);
	edge = edge->next;
    }
}
static void
apply_evenodd_fill_rule_for_subrow (struct active_list *active,
				    struct cell_list *coverages)
{
    struct edge *edge = active->head;
    int xstart;
    int xend;
    cell_list_rewind (coverages);
    while (NULL != edge) {
	xstart = edge->x.quo;
	while (1) {
	    edge = edge->next;
	    if (NULL == edge) {
		ASSERT_NOT_REACHED;
		return;
	    }
	    if (edge->next == NULL || edge->next->x.quo != edge->x.quo)
		break;
	    edge = edge->next;
	}
	xend = edge->x.quo;
	cell_list_add_subspan (coverages, xstart, xend);
	edge = edge->next;
    }
}
static void
apply_nonzero_fill_rule_and_step_edges (struct active_list *active,
					struct cell_list *coverages)
{
    struct edge **cursor = &active->head;
    struct edge *left_edge;
    left_edge = *cursor;
    while (NULL != left_edge) {
	struct edge *right_edge;
	int winding = left_edge->dir;
	left_edge->height_left -= GRID_Y;
	if (left_edge->height_left)
	    cursor = &left_edge->next;
	else
	    *cursor = left_edge->next;
	while (1) {
	    right_edge = *cursor;
	    if (NULL == right_edge) {
		cell_list_render_edge (coverages, left_edge, +1);
		return;
	    }
	    right_edge->height_left -= GRID_Y;
	    if (right_edge->height_left)
		cursor = &right_edge->next;
	    else
		*cursor = right_edge->next;
	    winding += right_edge->dir;
	    if (0 == winding) {
		if (right_edge->next == NULL ||
		    right_edge->next->x.quo != right_edge->x.quo)
		{
		    break;
		}
	    }
	    if (! right_edge->vertical) {
		right_edge->x.quo += right_edge->dxdy_full.quo;
		right_edge->x.rem += right_edge->dxdy_full.rem;
		if (right_edge->x.rem >= 0) {
		    ++right_edge->x.quo;
		    right_edge->x.rem -= right_edge->dy;
		}
	    }
	}
	cell_list_render_edge (coverages, left_edge, +1);
	cell_list_render_edge (coverages, right_edge, -1);
	left_edge = *cursor;
    }
}
static void
apply_evenodd_fill_rule_and_step_edges (struct active_list *active,
					struct cell_list *coverages)
{
    struct edge **cursor = &active->head;
    struct edge *left_edge;
    left_edge = *cursor;
    while (NULL != left_edge) {
	struct edge *right_edge;
	left_edge->height_left -= GRID_Y;
	if (left_edge->height_left)
	    cursor = &left_edge->next;
	else
	    *cursor = left_edge->next;
	while (1) {
	    right_edge = *cursor;
	    if (NULL == right_edge) {
		cell_list_render_edge (coverages, left_edge, +1);
		return;
	    }
	    right_edge->height_left -= GRID_Y;
	    if (right_edge->height_left)
		cursor = &right_edge->next;
	    else
		*cursor = right_edge->next;
	    if (right_edge->next == NULL ||
		right_edge->next->x.quo != right_edge->x.quo)
	    {
		break;
	    }
	    if (! right_edge->vertical) {
		right_edge->x.quo += right_edge->dxdy_full.quo;
		right_edge->x.rem += right_edge->dxdy_full.rem;
		if (right_edge->x.rem >= 0) {
		    ++right_edge->x.quo;
		    right_edge->x.rem -= right_edge->dy;
		}
	    }
	}
	cell_list_render_edge (coverages, left_edge, +1);
	cell_list_render_edge (coverages, right_edge, -1);
	left_edge = *cursor;
    }
}
static void
_glitter_scan_converter_init(glitter_scan_converter_t *converter, jmp_buf *jmp)
{
    polygon_init(converter->polygon, jmp);
    active_list_init(converter->active);
    cell_list_init(converter->coverages, jmp);
    converter->ymin=0;
    converter->ymax=0;
}
static void
_glitter_scan_converter_fini(glitter_scan_converter_t *converter)
{
    polygon_fini(converter->polygon);
    cell_list_fini(converter->coverages);
    converter->ymin=0;
    converter->ymax=0;
}
static grid_scaled_t
int_to_grid_scaled(int i, int scale)
{
    /* Clamp to max/min representable scaled number. */
    if (i >= 0) {
	if (i >= INT_MAX/scale)
	    i = INT_MAX/scale;
    }
    else {
	if (i <= INT_MIN/scale)
	    i = INT_MIN/scale;
    }
    return i*scale;
}
#define int_to_grid_scaled_x(x) int_to_grid_scaled((x), GRID_X)
#define int_to_grid_scaled_y(x) int_to_grid_scaled((x), GRID_Y)
static cairo_status_t
glitter_scan_converter_reset(glitter_scan_converter_t *converter,
			     int ymin, int ymax)
{
    cairo_status_t status;
    converter->ymin = 0;
    converter->ymax = 0;
    ymin = int_to_grid_scaled_y(ymin);
    ymax = int_to_grid_scaled_y(ymax);
    active_list_reset(converter->active);
    cell_list_reset(converter->coverages);
    status = polygon_reset(converter->polygon, ymin, ymax);
    if (status)
	return status;
    converter->ymin = ymin;
    converter->ymax = ymax;
    return CAIRO_STATUS_SUCCESS;
}
/* INPUT_TO_GRID_X/Y (in_coord, out_grid_scaled, grid_scale)
 *   These macros convert an input coordinate in the client's
 *   device space to the rasterisation grid.
 */
/* Gah.. this bit of ugly defines INPUT_TO_GRID_X/Y so as to use
 * shifts if possible, and something saneish if not.
 */
#if !defined(INPUT_TO_GRID_Y) && defined(GRID_Y_BITS) && GRID_Y_BITS <= GLITTER_INPUT_BITS
#  define INPUT_TO_GRID_Y(in, out) (out) = (in) >> (GLITTER_INPUT_BITS - GRID_Y_BITS)
#else
#  define INPUT_TO_GRID_Y(in, out) INPUT_TO_GRID_general(in, out, GRID_Y)
#endif
#if !defined(INPUT_TO_GRID_X) && defined(GRID_X_BITS) && GRID_X_BITS <= GLITTER_INPUT_BITS
#  define INPUT_TO_GRID_X(in, out) (out) = (in) >> (GLITTER_INPUT_BITS - GRID_X_BITS)
#else
#  define INPUT_TO_GRID_X(in, out) INPUT_TO_GRID_general(in, out, GRID_X)
#endif
#define INPUT_TO_GRID_general(in, out, grid_scale) do {		\
	long long tmp__ = (long long)(grid_scale) * (in);	\
	tmp__ >>= GLITTER_INPUT_BITS;				\
	(out) = tmp__;						\
} while (0)
static void
glitter_scan_converter_add_edge (glitter_scan_converter_t *converter,
				 const cairo_edge_t *edge,
				 int clip)
{
    cairo_edge_t e;
    INPUT_TO_GRID_Y (edge->top, e.top);
    INPUT_TO_GRID_Y (edge->bottom, e.bottom);
    if (e.top >= e.bottom)
	return;
    /* XXX: possible overflows if GRID_X/Y > 2**GLITTER_INPUT_BITS */
    INPUT_TO_GRID_Y (edge->line.p1.y, e.line.p1.y);
    INPUT_TO_GRID_Y (edge->line.p2.y, e.line.p2.y);
    if (e.line.p1.y == e.line.p2.y)
	return;
    INPUT_TO_GRID_X (edge->line.p1.x, e.line.p1.x);
    INPUT_TO_GRID_X (edge->line.p2.x, e.line.p2.x);
    e.dir = edge->dir;
    polygon_add_edge (converter->polygon, &e, clip);
}
static cairo_bool_t
active_list_is_vertical (struct active_list *active)
{
    struct edge *e;
    for (e = active->head; e != NULL; e = e->next) {
	if (! e->vertical)
	    return FALSE;
    }
    return TRUE;
}
static void
step_edges (struct active_list *active, int count)
{
    struct edge **cursor = &active->head;
    struct edge *edge;
    for (edge = *cursor; edge != NULL; edge = *cursor) {
	edge->height_left -= GRID_Y * count;
	if (edge->height_left)
	    cursor = &edge->next;
	else
	    *cursor = edge->next;
    }
}
static cairo_status_t
blit_coverages (struct cell_list *cells,
		cairo_span_renderer_t *renderer,
		struct pool *span_pool,
		int y, int height)
{
    struct cell *cell = cells->head.next;
    int prev_x = -1;
    int cover = 0, last_cover = 0;
    int clip = 0;
    cairo_half_open_span_t *spans;
    unsigned num_spans;
    assert (cell != &cells->tail);
    /* Count number of cells remaining. */
    {
	struct cell *next = cell;
	num_spans = 2;
	while (next->next) {
	    next = next->next;
	    ++num_spans;
	}
	num_spans = 2*num_spans;
    }
    /* Allocate enough spans for the row. */
    pool_reset (span_pool);
    spans = pool_alloc (span_pool, sizeof(spans[0])*num_spans);
    num_spans = 0;
    /* Form the spans from the coverages and areas. */
    for (; cell->next; cell = cell->next) {
	int x = cell->x;
	int area;
	if (x > prev_x && cover != last_cover) {
	    spans[num_spans].x = prev_x;
	    spans[num_spans].coverage = GRID_AREA_TO_ALPHA (cover);
	    spans[num_spans].inverse = 0;
	    last_cover = cover;
	    ++num_spans;
	}
	cover += cell->covered_height*GRID_X*2;
	clip += cell->covered_height*GRID_X*2;
	area = cover - cell->uncovered_area;
	if (area != last_cover) {
	    spans[num_spans].x = x;
	    spans[num_spans].coverage = GRID_AREA_TO_ALPHA (area);
	    spans[num_spans].inverse = 0;
	    last_cover = area;
	    ++num_spans;
	}
	prev_x = x+1;
    }
    /* Dump them into the renderer. */
    return renderer->render_rows (renderer, y, height, spans, num_spans);
}
static void
glitter_scan_converter_render(glitter_scan_converter_t *converter,
			      int nonzero_fill,
			      cairo_span_renderer_t *span_renderer,
			      struct pool *span_pool)
{
    int i, j;
    int ymax_i = converter->ymax / GRID_Y;
    int ymin_i = converter->ymin / GRID_Y;
    int h = ymax_i - ymin_i;
    struct polygon *polygon = converter->polygon;
    struct cell_list *coverages = converter->coverages;
    struct active_list *active = converter->active;
    /* Render each pixel row. */
    for (i = 0; i < h; i = j) {
	int do_full_step = 0;
	j = i + 1;
	/* Determine if we can ignore this row or use the full pixel
	 * stepper. */
	if (GRID_Y == EDGE_Y_BUCKET_HEIGHT && ! polygon->y_buckets[i]) {
	    if (! active->head) {
		for (; j < h && ! polygon->y_buckets[j]; j++)
		    ;
		continue;
	    }
	    do_full_step = active_list_can_step_full_row (active);
	}
	if (do_full_step) {
	    /* Step by a full pixel row's worth. */
	    if (nonzero_fill)
		apply_nonzero_fill_rule_and_step_edges (active, coverages);
	    else
		apply_evenodd_fill_rule_and_step_edges (active, coverages);
	    if (active_list_is_vertical (active)) {
		while (j < h &&
		       polygon->y_buckets[j] == NULL &&
		       active->min_height >= 2*GRID_Y)
		{
		    active->min_height -= GRID_Y;
		    j++;
		}
		if (j != i + 1)
		    step_edges (active, j - (i + 1));
	    }
	} else {
	    grid_scaled_y_t suby;
	    /* Subsample this row. */
	    for (suby = 0; suby < GRID_Y; suby++) {
		grid_scaled_y_t y = (i+ymin_i)*GRID_Y + suby;
		if (polygon->y_buckets[i]) {
		    active_list_merge_edges_from_polygon (active,
							  &polygon->y_buckets[i], y,
							  polygon);
		}
		if (nonzero_fill)
		    apply_nonzero_fill_rule_for_subrow (active, coverages);
		else
		    apply_evenodd_fill_rule_for_subrow (active, coverages);
		active_list_substep_edges(active);
	    }
	}
	blit_coverages (coverages, span_renderer, span_pool, i+ymin_i, j -i);
	cell_list_reset (coverages);
	if (! active->head)
	    active->min_height = INT_MAX;
	else
	    active->min_height -= GRID_Y;
    }
}
struct _cairo_clip_tor_scan_converter {
    cairo_scan_converter_t base;
    glitter_scan_converter_t converter[1];
    cairo_fill_rule_t fill_rule;
    cairo_antialias_t antialias;
    cairo_fill_rule_t clip_fill_rule;
    cairo_antialias_t clip_antialias;
    jmp_buf jmp;
    struct {
	struct pool base[1];
	cairo_half_open_span_t embedded[32];
    } span_pool;
};
typedef struct _cairo_clip_tor_scan_converter cairo_clip_tor_scan_converter_t;
static void
_cairo_clip_tor_scan_converter_destroy (void *converter)
{
    cairo_clip_tor_scan_converter_t *self = converter;
    if (self == NULL) {
	return;
    }
    _glitter_scan_converter_fini (self->converter);
    pool_fini (self->span_pool.base);
    free(self);
}
static cairo_status_t
_cairo_clip_tor_scan_converter_generate (void			*converter,
				    cairo_span_renderer_t	*renderer)
{
    cairo_clip_tor_scan_converter_t *self = converter;
    cairo_status_t status;
    if ((status = setjmp (self->jmp)))
	return _cairo_scan_converter_set_error (self, _cairo_error (status));
    glitter_scan_converter_render (self->converter,
				   self->fill_rule == CAIRO_FILL_RULE_WINDING,
				   renderer,
				   self->span_pool.base);
    return CAIRO_STATUS_SUCCESS;
}
cairo_scan_converter_t *
_cairo_clip_tor_scan_converter_create (cairo_clip_t *clip,
				       cairo_polygon_t *polygon,
				       cairo_fill_rule_t fill_rule,
				       cairo_antialias_t antialias)
{
    cairo_clip_tor_scan_converter_t *self;
    cairo_polygon_t clipper;
    cairo_status_t status;
    int i;
    self = _cairo_calloc (sizeof(struct _cairo_clip_tor_scan_converter));
    if (unlikely (self == NULL)) {
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
	goto bail_nomem;
    }
    self->base.destroy = _cairo_clip_tor_scan_converter_destroy;
    self->base.generate = _cairo_clip_tor_scan_converter_generate;
    pool_init (self->span_pool.base, &self->jmp,
	       250 * sizeof(self->span_pool.embedded[0]),
	       sizeof(self->span_pool.embedded));
    _glitter_scan_converter_init (self->converter, &self->jmp);
    status = glitter_scan_converter_reset (self->converter,
					   clip->extents.y,
					   clip->extents.y + clip->extents.height);
    if (unlikely (status))
	goto bail;
    self->fill_rule = fill_rule;
    self->antialias = antialias;
    for (i = 0; i < polygon->num_edges; i++)
	 glitter_scan_converter_add_edge (self->converter,
					  &polygon->edges[i],
					  FALSE);
    status = _cairo_clip_get_polygon (clip,
				      &clipper,
				      &self->clip_fill_rule,
				      &self->clip_antialias);
    if (unlikely (status))
	goto bail;
    for (i = 0; i < clipper.num_edges; i++)
	 glitter_scan_converter_add_edge (self->converter,
					  &clipper.edges[i],
					  TRUE);
    _cairo_polygon_fini (&clipper);
    return &self->base;
 bail:
    self->base.destroy(&self->base);
 bail_nomem:
    return _cairo_scan_converter_create_in_error (status);
}