2f930306e18f5dc50fe8b40ba4c5e7e876b163ab
[framework/graphics/cairo.git] / src / cairo-tor22-scan-converter.c
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  *   http://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 <stdlib.h>
101 #include <string.h>
102 #include <limits.h>
103 #include <setjmp.h>
104
105 /*-------------------------------------------------------------------------
106  * cairo specific config
107  */
108 #define I static
109
110 /* Prefer cairo's status type. */
111 #define GLITTER_HAVE_STATUS_T 1
112 #define GLITTER_STATUS_SUCCESS CAIRO_STATUS_SUCCESS
113 #define GLITTER_STATUS_NO_MEMORY CAIRO_STATUS_NO_MEMORY
114 typedef cairo_status_t glitter_status_t;
115
116 /* The input coordinate scale and the rasterisation grid scales. */
117 #define GLITTER_INPUT_BITS CAIRO_FIXED_FRAC_BITS
118 //#define GRID_X_BITS CAIRO_FIXED_FRAC_BITS
119 //#define GRID_Y 15
120 #define GRID_X_BITS 2
121 #define GRID_Y_BITS 2
122
123 /* Set glitter up to use a cairo span renderer to do the coverage
124  * blitting. */
125 struct pool;
126 struct cell_list;
127
128 /*-------------------------------------------------------------------------
129  * glitter-paths.h
130  */
131
132 /* "Input scaled" numbers are fixed precision reals with multiplier
133  * 2**GLITTER_INPUT_BITS.  Input coordinates are given to glitter as
134  * pixel scaled numbers.  These get converted to the internal grid
135  * scaled numbers as soon as possible. Internal overflow is possible
136  * if GRID_X/Y inside glitter-paths.c is larger than
137  * 1<<GLITTER_INPUT_BITS. */
138 #ifndef GLITTER_INPUT_BITS
139 #  define GLITTER_INPUT_BITS 8
140 #endif
141 #define GLITTER_INPUT_SCALE (1<<GLITTER_INPUT_BITS)
142 typedef int glitter_input_scaled_t;
143
144 #if !GLITTER_HAVE_STATUS_T
145 typedef enum {
146     GLITTER_STATUS_SUCCESS = 0,
147     GLITTER_STATUS_NO_MEMORY
148 } glitter_status_t;
149 #endif
150
151 #ifndef I
152 # define I /*static*/
153 #endif
154
155 /* Opaque type for scan converting. */
156 typedef struct glitter_scan_converter glitter_scan_converter_t;
157
158 /* Reset a scan converter to accept polygon edges and set the clip box
159  * in pixels.  Allocates O(ymax-ymin) bytes of memory.  The clip box
160  * is set to integer pixel coordinates xmin <= x < xmax, ymin <= y <
161  * ymax. */
162 I glitter_status_t
163 glitter_scan_converter_reset(
164     glitter_scan_converter_t *converter,
165     int xmin, int ymin,
166     int xmax, int ymax);
167
168 /* Render the polygon in the scan converter to the given A8 format
169  * image raster.  Only the pixels accessible as pixels[y*stride+x] for
170  * x,y inside the clip box are written to, where xmin <= x < xmax,
171  * ymin <= y < ymax.  The image is assumed to be clear on input.
172  *
173  * If nonzero_fill is true then the interior of the polygon is
174  * computed with the non-zero fill rule.  Otherwise the even-odd fill
175  * rule is used.
176  *
177  * The scan converter must be reset or destroyed after this call. */
178
179 /*-------------------------------------------------------------------------
180  * glitter-paths.c: Implementation internal types
181  */
182 #include <stdlib.h>
183 #include <string.h>
184 #include <limits.h>
185
186 /* All polygon coordinates are snapped onto a subsample grid. "Grid
187  * scaled" numbers are fixed precision reals with multiplier GRID_X or
188  * GRID_Y. */
189 typedef int grid_scaled_t;
190 typedef int grid_scaled_x_t;
191 typedef int grid_scaled_y_t;
192
193 /* Default x/y scale factors.
194  *  You can either define GRID_X/Y_BITS to get a power-of-two scale
195  *  or define GRID_X/Y separately. */
196 #if !defined(GRID_X) && !defined(GRID_X_BITS)
197 #  define GRID_X_BITS 8
198 #endif
199 #if !defined(GRID_Y) && !defined(GRID_Y_BITS)
200 #  define GRID_Y 15
201 #endif
202
203 /* Use GRID_X/Y_BITS to define GRID_X/Y if they're available. */
204 #ifdef GRID_X_BITS
205 #  define GRID_X (1 << GRID_X_BITS)
206 #endif
207 #ifdef GRID_Y_BITS
208 #  define GRID_Y (1 << GRID_Y_BITS)
209 #endif
210
211 /* The GRID_X_TO_INT_FRAC macro splits a grid scaled coordinate into
212  * integer and fractional parts. The integer part is floored. */
213 #if defined(GRID_X_TO_INT_FRAC)
214   /* do nothing */
215 #elif defined(GRID_X_BITS)
216 #  define GRID_X_TO_INT_FRAC(x, i, f) \
217         _GRID_TO_INT_FRAC_shift(x, i, f, GRID_X_BITS)
218 #else
219 #  define GRID_X_TO_INT_FRAC(x, i, f) \
220         _GRID_TO_INT_FRAC_general(x, i, f, GRID_X)
221 #endif
222
223 #define _GRID_TO_INT_FRAC_general(t, i, f, m) do {      \
224     (i) = (t) / (m);                                    \
225     (f) = (t) % (m);                                    \
226     if ((f) < 0) {                                      \
227         --(i);                                          \
228         (f) += (m);                                     \
229     }                                                   \
230 } while (0)
231
232 #define _GRID_TO_INT_FRAC_shift(t, i, f, b) do {        \
233     (f) = (t) & ((1 << (b)) - 1);                       \
234     (i) = (t) >> (b);                                   \
235 } while (0)
236
237 /* A grid area is a real in [0,1] scaled by 2*GRID_X*GRID_Y.  We want
238  * to be able to represent exactly areas of subpixel trapezoids whose
239  * vertices are given in grid scaled coordinates.  The scale factor
240  * comes from needing to accurately represent the area 0.5*dx*dy of a
241  * triangle with base dx and height dy in grid scaled numbers. */
242 #define GRID_XY (2*GRID_X*GRID_Y) /* Unit area on the grid. */
243
244 /* GRID_AREA_TO_ALPHA(area): map [0,GRID_XY] to [0,255]. */
245 #if GRID_XY == 510
246 #  define GRID_AREA_TO_ALPHA(c)   (((c)+1) >> 1)
247 #elif GRID_XY == 255
248 #  define  GRID_AREA_TO_ALPHA(c)  (c)
249 #elif GRID_XY == 64
250 #  define  GRID_AREA_TO_ALPHA(c)  (((c) << 2) | -(((c) & 0x40) >> 6))
251 #elif GRID_XY == 128
252 #  define  GRID_AREA_TO_ALPHA(c)  ((((c) << 1) | -((c) >> 7)) & 255)
253 #elif GRID_XY == 256
254 #  define  GRID_AREA_TO_ALPHA(c)  (((c) | -((c) >> 8)) & 255)
255 #elif GRID_XY == 15
256 #  define  GRID_AREA_TO_ALPHA(c)  (((c) << 4) + (c))
257 #elif GRID_XY == 2*256*15
258 #  define  GRID_AREA_TO_ALPHA(c)  (((c) + ((c)<<4) + 256) >> 9)
259 #else
260 #  define  GRID_AREA_TO_ALPHA(c)  (((c)*255 + GRID_XY/2) / GRID_XY)
261 #endif
262
263 #define UNROLL3(x) x x x
264
265 struct quorem {
266     int32_t quo;
267     int32_t rem;
268 };
269
270 /* Header for a chunk of memory in a memory pool. */
271 struct _pool_chunk {
272     /* # bytes used in this chunk. */
273     size_t size;
274
275     /* # bytes total in this chunk */
276     size_t capacity;
277
278     /* Pointer to the previous chunk or %NULL if this is the sentinel
279      * chunk in the pool header. */
280     struct _pool_chunk *prev_chunk;
281
282     /* Actual data starts here.  Well aligned for pointers. */
283 };
284
285 /* A memory pool.  This is supposed to be embedded on the stack or
286  * within some other structure.  It may optionally be followed by an
287  * embedded array from which requests are fulfilled until
288  * malloc needs to be called to allocate a first real chunk. */
289 struct pool {
290     /* Chunk we're allocating from. */
291     struct _pool_chunk *current;
292
293     jmp_buf *jmp;
294
295     /* Free list of previously allocated chunks.  All have >= default
296      * capacity. */
297     struct _pool_chunk *first_free;
298
299     /* The default capacity of a chunk. */
300     size_t default_capacity;
301
302     /* Header for the sentinel chunk.  Directly following the pool
303      * struct should be some space for embedded elements from which
304      * the sentinel chunk allocates from. */
305     struct _pool_chunk sentinel[1];
306 };
307
308 /* A polygon edge. */
309 struct edge {
310     /* Next in y-bucket or active list. */
311     struct edge *next, *prev;
312
313     /* Number of subsample rows remaining to scan convert of this
314      * edge. */
315     grid_scaled_y_t height_left;
316
317     /* Original sign of the edge: +1 for downwards, -1 for upwards
318      * edges.  */
319     int dir;
320     int vertical;
321
322     /* Current x coordinate while the edge is on the active
323      * list. Initialised to the x coordinate of the top of the
324      * edge. The quotient is in grid_scaled_x_t units and the
325      * remainder is mod dy in grid_scaled_y_t units.*/
326     struct quorem x;
327
328     /* Advance of the current x when moving down a subsample line. */
329     struct quorem dxdy;
330
331     /* The clipped y of the top of the edge. */
332     grid_scaled_y_t ytop;
333
334     /* y2-y1 after orienting the edge downwards.  */
335     grid_scaled_y_t dy;
336 };
337
338 #define EDGE_Y_BUCKET_INDEX(y, ymin) (((y) - (ymin))/GRID_Y)
339
340 /* A collection of sorted and vertically clipped edges of the polygon.
341  * Edges are moved from the polygon to an active list while scan
342  * converting. */
343 struct polygon {
344     /* The vertical clip extents. */
345     grid_scaled_y_t ymin, ymax;
346
347     /* Array of edges all starting in the same bucket.  An edge is put
348      * into bucket EDGE_BUCKET_INDEX(edge->ytop, polygon->ymin) when
349      * it is added to the polygon. */
350     struct edge **y_buckets;
351     struct edge *y_buckets_embedded[64];
352
353     struct {
354         struct pool base[1];
355         struct edge embedded[32];
356     } edge_pool;
357 };
358
359 /* A cell records the effect on pixel coverage of polygon edges
360  * passing through a pixel.  It contains two accumulators of pixel
361  * coverage.
362  *
363  * Consider the effects of a polygon edge on the coverage of a pixel
364  * it intersects and that of the following one.  The coverage of the
365  * following pixel is the height of the edge multiplied by the width
366  * of the pixel, and the coverage of the pixel itself is the area of
367  * the trapezoid formed by the edge and the right side of the pixel.
368  *
369  * +-----------------------+-----------------------+
370  * |                       |                       |
371  * |                       |                       |
372  * |_______________________|_______________________|
373  * |   \...................|.......................|\
374  * |    \..................|.......................| |
375  * |     \.................|.......................| |
376  * |      \....covered.....|.......................| |
377  * |       \....area.......|.......................| } covered height
378  * |        \..............|.......................| |
379  * |uncovered\.............|.......................| |
380  * |  area    \............|.......................| |
381  * |___________\...........|.......................|/
382  * |                       |                       |
383  * |                       |                       |
384  * |                       |                       |
385  * +-----------------------+-----------------------+
386  *
387  * Since the coverage of the following pixel will always be a multiple
388  * of the width of the pixel, we can store the height of the covered
389  * area instead.  The coverage of the pixel itself is the total
390  * coverage minus the area of the uncovered area to the left of the
391  * edge.  As it's faster to compute the uncovered area we only store
392  * that and subtract it from the total coverage later when forming
393  * spans to blit.
394  *
395  * The heights and areas are signed, with left edges of the polygon
396  * having positive sign and right edges having negative sign.  When
397  * two edges intersect they swap their left/rightness so their
398  * contribution above and below the intersection point must be
399  * computed separately. */
400 struct cell {
401     struct cell         *next;
402     int                  x;
403     int16_t              uncovered_area;
404     int16_t              covered_height;
405 };
406
407 /* A cell list represents the scan line sparsely as cells ordered by
408  * ascending x.  It is geared towards scanning the cells in order
409  * using an internal cursor. */
410 struct cell_list {
411     /* Sentinel nodes */
412     struct cell head, tail;
413
414     /* Cursor state for iterating through the cell list. */
415     struct cell *cursor, *rewind;
416
417     /* Cells in the cell list are owned by the cell list and are
418      * allocated from this pool.  */
419     struct {
420         struct pool base[1];
421         struct cell embedded[32];
422     } cell_pool;
423 };
424
425 struct cell_pair {
426     struct cell *cell1;
427     struct cell *cell2;
428 };
429
430 /* The active list contains edges in the current scan line ordered by
431  * the x-coordinate of the intercept of the edge and the scan line. */
432 struct active_list {
433     /* Leftmost edge on the current scan line. */
434     struct edge head, tail;
435
436     /* A lower bound on the height of the active edges is used to
437      * estimate how soon some active edge ends.  We can't advance the
438      * scan conversion by a full pixel row if an edge ends somewhere
439      * within it. */
440     grid_scaled_y_t min_height;
441     int is_vertical;
442 };
443
444 struct glitter_scan_converter {
445     struct polygon      polygon[1];
446     struct active_list  active[1];
447     struct cell_list    coverages[1];
448
449     cairo_half_open_span_t *spans;
450     cairo_half_open_span_t spans_embedded[64];
451
452     /* Clip box. */
453     grid_scaled_x_t xmin, xmax;
454     grid_scaled_y_t ymin, ymax;
455 };
456
457 /* Compute the floored division a/b. Assumes / and % perform symmetric
458  * division. */
459 inline static struct quorem
460 floored_divrem(int a, int b)
461 {
462     struct quorem qr;
463     qr.quo = a/b;
464     qr.rem = a%b;
465     if ((a^b)<0 && qr.rem) {
466         qr.quo -= 1;
467         qr.rem += b;
468     }
469     return qr;
470 }
471
472 /* Compute the floored division (x*a)/b. Assumes / and % perform symmetric
473  * division. */
474 static struct quorem
475 floored_muldivrem(int x, int a, int b)
476 {
477     struct quorem qr;
478     long long xa = (long long)x*a;
479     qr.quo = xa/b;
480     qr.rem = xa%b;
481     if ((xa>=0) != (b>=0) && qr.rem) {
482         qr.quo -= 1;
483         qr.rem += b;
484     }
485     return qr;
486 }
487
488 static struct _pool_chunk *
489 _pool_chunk_init(
490     struct _pool_chunk *p,
491     struct _pool_chunk *prev_chunk,
492     size_t capacity)
493 {
494     p->prev_chunk = prev_chunk;
495     p->size = 0;
496     p->capacity = capacity;
497     return p;
498 }
499
500 static struct _pool_chunk *
501 _pool_chunk_create(struct pool *pool, size_t size)
502 {
503     struct _pool_chunk *p;
504
505     p = malloc(size + sizeof(struct _pool_chunk));
506     if (unlikely (NULL == p))
507         longjmp (*pool->jmp, _cairo_error (CAIRO_STATUS_NO_MEMORY));
508
509     return _pool_chunk_init(p, pool->current, size);
510 }
511
512 static void
513 pool_init(struct pool *pool,
514           jmp_buf *jmp,
515           size_t default_capacity,
516           size_t embedded_capacity)
517 {
518     pool->jmp = jmp;
519     pool->current = pool->sentinel;
520     pool->first_free = NULL;
521     pool->default_capacity = default_capacity;
522     _pool_chunk_init(pool->sentinel, NULL, embedded_capacity);
523 }
524
525 static void
526 pool_fini(struct pool *pool)
527 {
528     struct _pool_chunk *p = pool->current;
529     do {
530         while (NULL != p) {
531             struct _pool_chunk *prev = p->prev_chunk;
532             if (p != pool->sentinel)
533                 free(p);
534             p = prev;
535         }
536         p = pool->first_free;
537         pool->first_free = NULL;
538     } while (NULL != p);
539 }
540
541 /* Satisfy an allocation by first allocating a new large enough chunk
542  * and adding it to the head of the pool's chunk list. This function
543  * is called as a fallback if pool_alloc() couldn't do a quick
544  * allocation from the current chunk in the pool. */
545 static void *
546 _pool_alloc_from_new_chunk(
547     struct pool *pool,
548     size_t size)
549 {
550     struct _pool_chunk *chunk;
551     void *obj;
552     size_t capacity;
553
554     /* If the allocation is smaller than the default chunk size then
555      * try getting a chunk off the free list.  Force alloc of a new
556      * chunk for large requests. */
557     capacity = size;
558     chunk = NULL;
559     if (size < pool->default_capacity) {
560         capacity = pool->default_capacity;
561         chunk = pool->first_free;
562         if (chunk) {
563             pool->first_free = chunk->prev_chunk;
564             _pool_chunk_init(chunk, pool->current, chunk->capacity);
565         }
566     }
567
568     if (NULL == chunk)
569         chunk = _pool_chunk_create (pool, capacity);
570     pool->current = chunk;
571
572     obj = ((unsigned char*)chunk + sizeof(*chunk) + chunk->size);
573     chunk->size += size;
574     return obj;
575 }
576
577 /* Allocate size bytes from the pool.  The first allocated address
578  * returned from a pool is aligned to sizeof(void*).  Subsequent
579  * addresses will maintain alignment as long as multiples of void* are
580  * allocated.  Returns the address of a new memory area or %NULL on
581  * allocation failures.  The pool retains ownership of the returned
582  * memory. */
583 inline static void *
584 pool_alloc (struct pool *pool, size_t size)
585 {
586     struct _pool_chunk *chunk = pool->current;
587
588     if (size <= chunk->capacity - chunk->size) {
589         void *obj = ((unsigned char*)chunk + sizeof(*chunk) + chunk->size);
590         chunk->size += size;
591         return obj;
592     } else {
593         return _pool_alloc_from_new_chunk(pool, size);
594     }
595 }
596
597 /* Relinquish all pool_alloced memory back to the pool. */
598 static void
599 pool_reset (struct pool *pool)
600 {
601     /* Transfer all used chunks to the chunk free list. */
602     struct _pool_chunk *chunk = pool->current;
603     if (chunk != pool->sentinel) {
604         while (chunk->prev_chunk != pool->sentinel) {
605             chunk = chunk->prev_chunk;
606         }
607         chunk->prev_chunk = pool->first_free;
608         pool->first_free = pool->current;
609     }
610     /* Reset the sentinel as the current chunk. */
611     pool->current = pool->sentinel;
612     pool->sentinel->size = 0;
613 }
614
615 /* Rewinds the cell list's cursor to the beginning.  After rewinding
616  * we're good to cell_list_find() the cell any x coordinate. */
617 inline static void
618 cell_list_rewind (struct cell_list *cells)
619 {
620     cells->cursor = &cells->head;
621 }
622
623 inline static void
624 cell_list_maybe_rewind (struct cell_list *cells, int x)
625 {
626     if (x < cells->cursor->x) {
627         cells->cursor = cells->rewind;
628         if (x < cells->cursor->x)
629             cells->cursor = &cells->head;
630     }
631 }
632
633 inline static void
634 cell_list_set_rewind (struct cell_list *cells)
635 {
636     cells->rewind = cells->cursor;
637 }
638
639 static void
640 cell_list_init(struct cell_list *cells, jmp_buf *jmp)
641 {
642     pool_init(cells->cell_pool.base, jmp,
643               256*sizeof(struct cell),
644               sizeof(cells->cell_pool.embedded));
645     cells->tail.next = NULL;
646     cells->tail.x = INT_MAX;
647     cells->head.x = INT_MIN;
648     cells->head.next = &cells->tail;
649     cell_list_rewind (cells);
650 }
651
652 static void
653 cell_list_fini(struct cell_list *cells)
654 {
655     pool_fini (cells->cell_pool.base);
656 }
657
658 /* Empty the cell list.  This is called at the start of every pixel
659  * row. */
660 inline static void
661 cell_list_reset (struct cell_list *cells)
662 {
663     cell_list_rewind (cells);
664     cells->head.next = &cells->tail;
665     pool_reset (cells->cell_pool.base);
666 }
667
668 inline static struct cell *
669 cell_list_alloc (struct cell_list *cells,
670                  struct cell *tail,
671                  int x)
672 {
673     struct cell *cell;
674
675     cell = pool_alloc (cells->cell_pool.base, sizeof (struct cell));
676     cell->next = tail->next;
677     tail->next = cell;
678     cell->x = x;
679     *(uint32_t *)&cell->uncovered_area = 0;
680
681     return cell;
682 }
683
684 /* Find a cell at the given x-coordinate.  Returns %NULL if a new cell
685  * needed to be allocated but couldn't be.  Cells must be found with
686  * non-decreasing x-coordinate until the cell list is rewound using
687  * cell_list_rewind(). Ownership of the returned cell is retained by
688  * the cell list. */
689 inline static struct cell *
690 cell_list_find (struct cell_list *cells, int x)
691 {
692     struct cell *tail = cells->cursor;
693
694     if (tail->x == x)
695         return tail;
696
697     while (1) {
698         UNROLL3({
699                 if (tail->next->x > x)
700                         break;
701                 tail = tail->next;
702         });
703     }
704
705     if (tail->x != x)
706         tail = cell_list_alloc (cells, tail, x);
707     return cells->cursor = tail;
708
709 }
710
711 /* Find two cells at x1 and x2.  This is exactly equivalent
712  * to
713  *
714  *   pair.cell1 = cell_list_find(cells, x1);
715  *   pair.cell2 = cell_list_find(cells, x2);
716  *
717  * except with less function call overhead. */
718 inline static struct cell_pair
719 cell_list_find_pair(struct cell_list *cells, int x1, int x2)
720 {
721     struct cell_pair pair;
722
723     pair.cell1 = cells->cursor;
724     while (1) {
725         UNROLL3({
726                 if (pair.cell1->next->x > x1)
727                         break;
728                 pair.cell1 = pair.cell1->next;
729         });
730     }
731     if (pair.cell1->x != x1)
732         pair.cell1 = cell_list_alloc (cells, pair.cell1, x1);
733
734     pair.cell2 = pair.cell1;
735     while (1) {
736         UNROLL3({
737                 if (pair.cell2->next->x > x2)
738                         break;
739                 pair.cell2 = pair.cell2->next;
740         });
741     }
742     if (pair.cell2->x != x2)
743         pair.cell2 = cell_list_alloc (cells, pair.cell2, x2);
744
745     cells->cursor = pair.cell2;
746     return pair;
747 }
748
749 /* Add a subpixel span covering [x1, x2) to the coverage cells. */
750 inline static void
751 cell_list_add_subspan(struct cell_list *cells,
752                       grid_scaled_x_t x1,
753                       grid_scaled_x_t x2)
754 {
755     int ix1, fx1;
756     int ix2, fx2;
757
758     if (x1 == x2)
759         return;
760
761     GRID_X_TO_INT_FRAC(x1, ix1, fx1);
762     GRID_X_TO_INT_FRAC(x2, ix2, fx2);
763
764     if (ix1 != ix2) {
765         struct cell_pair p;
766         p = cell_list_find_pair(cells, ix1, ix2);
767         p.cell1->uncovered_area += 2*fx1;
768         ++p.cell1->covered_height;
769         p.cell2->uncovered_area -= 2*fx2;
770         --p.cell2->covered_height;
771     } else {
772         struct cell *cell = cell_list_find(cells, ix1);
773         cell->uncovered_area += 2*(fx1-fx2);
774     }
775 }
776
777 /* Adds the analytical coverage of an edge crossing the current pixel
778  * row to the coverage cells and advances the edge's x position to the
779  * following row.
780  *
781  * This function is only called when we know that during this pixel row:
782  *
783  * 1) The relative order of all edges on the active list doesn't
784  * change.  In particular, no edges intersect within this row to pixel
785  * precision.
786  *
787  * 2) No new edges start in this row.
788  *
789  * 3) No existing edges end mid-row.
790  *
791  * This function depends on being called with all edges from the
792  * active list in the order they appear on the list (i.e. with
793  * non-decreasing x-coordinate.)  */
794 static void
795 cell_list_render_edge(struct cell_list *cells,
796                       struct edge *edge,
797                       int sign)
798 {
799     grid_scaled_x_t fx;
800     struct cell *cell;
801     int ix;
802
803     GRID_X_TO_INT_FRAC(edge->x.quo, ix, fx);
804
805     /* We always know that ix1 is >= the cell list cursor in this
806      * case due to the no-intersections precondition.  */
807     cell = cell_list_find(cells, ix);
808     cell->covered_height += sign*GRID_Y;
809     cell->uncovered_area += sign*2*fx*GRID_Y;
810 }
811
812 static void
813 polygon_init (struct polygon *polygon, jmp_buf *jmp)
814 {
815     polygon->ymin = polygon->ymax = 0;
816     polygon->y_buckets = polygon->y_buckets_embedded;
817     pool_init (polygon->edge_pool.base, jmp,
818                8192 - sizeof (struct _pool_chunk),
819                sizeof (polygon->edge_pool.embedded));
820 }
821
822 static void
823 polygon_fini (struct polygon *polygon)
824 {
825     if (polygon->y_buckets != polygon->y_buckets_embedded)
826         free (polygon->y_buckets);
827
828     pool_fini (polygon->edge_pool.base);
829 }
830
831 /* Empties the polygon of all edges. The polygon is then prepared to
832  * receive new edges and clip them to the vertical range
833  * [ymin,ymax). */
834 static glitter_status_t
835 polygon_reset (struct polygon *polygon,
836                grid_scaled_y_t ymin,
837                grid_scaled_y_t ymax)
838 {
839     unsigned h = ymax - ymin;
840     unsigned num_buckets = EDGE_Y_BUCKET_INDEX(ymax + GRID_Y-1, ymin);
841
842     pool_reset(polygon->edge_pool.base);
843
844     if (unlikely (h > 0x7FFFFFFFU - GRID_Y))
845         goto bail_no_mem; /* even if you could, you wouldn't want to. */
846
847     if (polygon->y_buckets != polygon->y_buckets_embedded)
848         free (polygon->y_buckets);
849
850     polygon->y_buckets =  polygon->y_buckets_embedded;
851     if (num_buckets > ARRAY_LENGTH (polygon->y_buckets_embedded)) {
852         polygon->y_buckets = _cairo_malloc_ab (num_buckets,
853                                                sizeof (struct edge *));
854         if (unlikely (NULL == polygon->y_buckets))
855             goto bail_no_mem;
856     }
857     memset (polygon->y_buckets, 0, num_buckets * sizeof (struct edge *));
858
859     polygon->ymin = ymin;
860     polygon->ymax = ymax;
861     return GLITTER_STATUS_SUCCESS;
862
863 bail_no_mem:
864     polygon->ymin = 0;
865     polygon->ymax = 0;
866     return GLITTER_STATUS_NO_MEMORY;
867 }
868
869 static void
870 _polygon_insert_edge_into_its_y_bucket(struct polygon *polygon,
871                                        struct edge *e)
872 {
873     unsigned ix = EDGE_Y_BUCKET_INDEX(e->ytop, polygon->ymin);
874     struct edge **ptail = &polygon->y_buckets[ix];
875     e->next = *ptail;
876     *ptail = e;
877 }
878
879 inline static void
880 polygon_add_edge (struct polygon *polygon,
881                   const cairo_edge_t *edge)
882 {
883     struct edge *e;
884     grid_scaled_x_t dx;
885     grid_scaled_y_t dy;
886     grid_scaled_y_t ytop, ybot;
887     grid_scaled_y_t ymin = polygon->ymin;
888     grid_scaled_y_t ymax = polygon->ymax;
889
890     if (unlikely (edge->top >= ymax || edge->bottom <= ymin))
891         return;
892
893     e = pool_alloc (polygon->edge_pool.base, sizeof (struct edge));
894
895     dx = edge->line.p2.x - edge->line.p1.x;
896     dy = edge->line.p2.y - edge->line.p1.y;
897     e->dy = dy;
898     e->dir = edge->dir;
899
900     ytop = edge->top >= ymin ? edge->top : ymin;
901     ybot = edge->bottom <= ymax ? edge->bottom : ymax;
902     e->ytop = ytop;
903     e->height_left = ybot - ytop;
904
905     if (dx == 0) {
906         e->vertical = TRUE;
907         e->x.quo = edge->line.p1.x;
908         e->x.rem = 0;
909         e->dxdy.quo = 0;
910         e->dxdy.rem = 0;
911     } else {
912         e->vertical = FALSE;
913         e->dxdy = floored_divrem (dx, dy);
914         if (ytop == edge->line.p1.y) {
915             e->x.quo = edge->line.p1.x;
916             e->x.rem = 0;
917         } else {
918             e->x = floored_muldivrem (ytop - edge->line.p1.y, dx, dy);
919             e->x.quo += edge->line.p1.x;
920         }
921     }
922
923     _polygon_insert_edge_into_its_y_bucket (polygon, e);
924
925     e->x.rem -= dy;             /* Bias the remainder for faster
926                                  * edge advancement. */
927 }
928
929 static void
930 active_list_reset (struct active_list *active)
931 {
932     active->head.vertical = 1;
933     active->head.height_left = INT_MAX;
934     active->head.x.quo = INT_MIN;
935     active->head.prev = NULL;
936     active->head.next = &active->tail;
937     active->tail.prev = &active->head;
938     active->tail.next = NULL;
939     active->tail.x.quo = INT_MAX;
940     active->tail.height_left = INT_MAX;
941     active->tail.vertical = 1;
942     active->min_height = 0;
943     active->is_vertical = 1;
944 }
945
946 static void
947 active_list_init(struct active_list *active)
948 {
949     active_list_reset(active);
950 }
951
952 /*
953  * Merge two sorted edge lists.
954  * Input:
955  *  - head_a: The head of the first list.
956  *  - head_b: The head of the second list; head_b cannot be NULL.
957  * Output:
958  * Returns the head of the merged list.
959  *
960  * Implementation notes:
961  * To make it fast (in particular, to reduce to an insertion sort whenever
962  * one of the two input lists only has a single element) we iterate through
963  * a list until its head becomes greater than the head of the other list,
964  * then we switch their roles. As soon as one of the two lists is empty, we
965  * just attach the other one to the current list and exit.
966  * Writes to memory are only needed to "switch" lists (as it also requires
967  * attaching to the output list the list which we will be iterating next) and
968  * to attach the last non-empty list.
969  */
970 static struct edge *
971 merge_sorted_edges (struct edge *head_a, struct edge *head_b)
972 {
973     struct edge *head, **next, *prev;
974     int32_t x;
975
976     prev = head_a->prev;
977     next = &head;
978     if (head_a->x.quo <= head_b->x.quo) {
979         head = head_a;
980     } else {
981         head = head_b;
982         head_b->prev = prev;
983         goto start_with_b;
984     }
985
986     do {
987         x = head_b->x.quo;
988         while (head_a != NULL && head_a->x.quo <= x) {
989             prev = head_a;
990             next = &head_a->next;
991             head_a = head_a->next;
992         }
993
994         head_b->prev = prev;
995         *next = head_b;
996         if (head_a == NULL)
997             return head;
998
999 start_with_b:
1000         x = head_a->x.quo;
1001         while (head_b != NULL && head_b->x.quo <= x) {
1002             prev = head_b;
1003             next = &head_b->next;
1004             head_b = head_b->next;
1005         }
1006
1007         head_a->prev = prev;
1008         *next = head_a;
1009         if (head_b == NULL)
1010             return head;
1011     } while (1);
1012 }
1013
1014 /*
1015  * Sort (part of) a list.
1016  * Input:
1017  *  - list: The list to be sorted; list cannot be NULL.
1018  *  - limit: Recursion limit.
1019  * Output:
1020  *  - head_out: The head of the sorted list containing the first 2^(level+1) elements of the
1021  *              input list; if the input list has fewer elements, head_out be a sorted list
1022  *              containing all the elements of the input list.
1023  * Returns the head of the list of unprocessed elements (NULL if the sorted list contains
1024  * all the elements of the input list).
1025  *
1026  * Implementation notes:
1027  * Special case single element list, unroll/inline the sorting of the first two elements.
1028  * Some tail recursion is used since we iterate on the bottom-up solution of the problem
1029  * (we start with a small sorted list and keep merging other lists of the same size to it).
1030  */
1031 static struct edge *
1032 sort_edges (struct edge *list,
1033             unsigned int level,
1034             struct edge **head_out)
1035 {
1036     struct edge *head_other, *remaining;
1037     unsigned int i;
1038
1039     head_other = list->next;
1040
1041     if (head_other == NULL) {
1042         *head_out = list;
1043         return NULL;
1044     }
1045
1046     remaining = head_other->next;
1047     if (list->x.quo <= head_other->x.quo) {
1048         *head_out = list;
1049         head_other->next = NULL;
1050     } else {
1051         *head_out = head_other;
1052         head_other->prev = list->prev;
1053         head_other->next = list;
1054         list->prev = head_other;
1055         list->next = NULL;
1056     }
1057
1058     for (i = 0; i < level && remaining; i++) {
1059         remaining = sort_edges (remaining, i, &head_other);
1060         *head_out = merge_sorted_edges (*head_out, head_other);
1061     }
1062
1063     return remaining;
1064 }
1065
1066 static struct edge *
1067 merge_unsorted_edges (struct edge *head, struct edge *unsorted)
1068 {
1069     sort_edges (unsorted, UINT_MAX, &unsorted);
1070     return merge_sorted_edges (head, unsorted);
1071 }
1072
1073 /* Test if the edges on the active list can be safely advanced by a
1074  * full row without intersections or any edges ending. */
1075 inline static int
1076 can_do_full_row (struct active_list *active)
1077 {
1078     const struct edge *e;
1079
1080     /* Recomputes the minimum height of all edges on the active
1081      * list if we have been dropping edges. */
1082     if (active->min_height <= 0) {
1083         int min_height = INT_MAX;
1084         int is_vertical = 1;
1085
1086         e = active->head.next;
1087         while (NULL != e) {
1088             if (e->height_left < min_height)
1089                 min_height = e->height_left;
1090             is_vertical &= e->vertical;
1091             e = e->next;
1092         }
1093
1094         active->is_vertical = is_vertical;
1095         active->min_height = min_height;
1096     }
1097
1098     if (active->min_height < GRID_Y)
1099         return 0;
1100
1101     return active->is_vertical;
1102 }
1103
1104 /* Merges edges on the given subpixel row from the polygon to the
1105  * active_list. */
1106 inline static void
1107 active_list_merge_edges_from_bucket(struct active_list *active,
1108                                     struct edge *edges)
1109 {
1110     active->head.next = merge_unsorted_edges (active->head.next, edges);
1111 }
1112
1113 inline static void
1114 polygon_fill_buckets (struct active_list *active,
1115                       struct edge *edge,
1116                       int y,
1117                       struct edge **buckets)
1118 {
1119     grid_scaled_y_t min_height = active->min_height;
1120     int is_vertical = active->is_vertical;
1121
1122     while (edge) {
1123         struct edge *next = edge->next;
1124         int suby = edge->ytop - y;
1125         if (buckets[suby])
1126             buckets[suby]->prev = edge;
1127         edge->next = buckets[suby];
1128         edge->prev = NULL;
1129         buckets[suby] = edge;
1130         if (edge->height_left < min_height)
1131             min_height = edge->height_left;
1132         is_vertical &= edge->vertical;
1133         edge = next;
1134     }
1135
1136     active->is_vertical = is_vertical;
1137     active->min_height = min_height;
1138 }
1139
1140 inline static void
1141 sub_row (struct active_list *active,
1142          struct cell_list *coverages,
1143          unsigned int mask)
1144 {
1145     struct edge *edge = active->head.next;
1146     int xstart = INT_MIN, prev_x = INT_MIN;
1147     int winding = 0;
1148
1149     cell_list_rewind (coverages);
1150
1151     while (&active->tail != edge) {
1152         struct edge *next = edge->next;
1153         int xend = edge->x.quo;
1154
1155         if (--edge->height_left) {
1156             edge->x.quo += edge->dxdy.quo;
1157             edge->x.rem += edge->dxdy.rem;
1158             if (edge->x.rem >= 0) {
1159                 ++edge->x.quo;
1160                 edge->x.rem -= edge->dy;
1161             }
1162
1163             if (edge->x.quo < prev_x) {
1164                 struct edge *pos = edge->prev;
1165                 pos->next = next;
1166                 next->prev = pos;
1167                 do {
1168                     pos = pos->prev;
1169                 } while (edge->x.quo < pos->x.quo);
1170                 pos->next->prev = edge;
1171                 edge->next = pos->next;
1172                 edge->prev = pos;
1173                 pos->next = edge;
1174             } else
1175                 prev_x = edge->x.quo;
1176         } else {
1177             edge->prev->next = next;
1178             next->prev = edge->prev;
1179         }
1180
1181         winding += edge->dir;
1182         if ((winding & mask) == 0) {
1183             if (next->x.quo != xend) {
1184                 cell_list_add_subspan (coverages, xstart, xend);
1185                 xstart = INT_MIN;
1186             }
1187         } else if (xstart == INT_MIN)
1188             xstart = xend;
1189
1190         edge = next;
1191     }
1192 }
1193
1194 inline static void dec (struct edge *e, int h)
1195 {
1196     e->height_left -= h;
1197     if (e->height_left == 0) {
1198         e->prev->next = e->next;
1199         e->next->prev = e->prev;
1200     }
1201 }
1202
1203 static void
1204 full_row (struct active_list *active,
1205           struct cell_list *coverages,
1206           unsigned int mask)
1207 {
1208     struct edge *left = active->head.next;
1209
1210     while (&active->tail != left) {
1211         struct edge *right;
1212         int winding;
1213
1214         dec (left, GRID_Y);
1215
1216         winding = left->dir;
1217         right = left->next;
1218         do {
1219             dec (right, GRID_Y);
1220
1221             winding += right->dir;
1222             if ((winding & mask) == 0 && right->next->x.quo != right->x.quo)
1223                 break;
1224
1225             right = right->next;
1226         } while (1);
1227
1228         cell_list_set_rewind (coverages);
1229         cell_list_render_edge (coverages, left, +1);
1230         cell_list_render_edge (coverages, right, -1);
1231
1232         left = right->next;
1233     }
1234 }
1235
1236 static void
1237 _glitter_scan_converter_init(glitter_scan_converter_t *converter, jmp_buf *jmp)
1238 {
1239     polygon_init(converter->polygon, jmp);
1240     active_list_init(converter->active);
1241     cell_list_init(converter->coverages, jmp);
1242     converter->xmin=0;
1243     converter->ymin=0;
1244     converter->xmax=0;
1245     converter->ymax=0;
1246 }
1247
1248 static void
1249 _glitter_scan_converter_fini(glitter_scan_converter_t *self)
1250 {
1251     if (self->spans != self->spans_embedded)
1252         free (self->spans);
1253
1254     polygon_fini(self->polygon);
1255     cell_list_fini(self->coverages);
1256
1257     self->xmin=0;
1258     self->ymin=0;
1259     self->xmax=0;
1260     self->ymax=0;
1261 }
1262
1263 static grid_scaled_t
1264 int_to_grid_scaled(int i, int scale)
1265 {
1266     /* Clamp to max/min representable scaled number. */
1267     if (i >= 0) {
1268         if (i >= INT_MAX/scale)
1269             i = INT_MAX/scale;
1270     }
1271     else {
1272         if (i <= INT_MIN/scale)
1273             i = INT_MIN/scale;
1274     }
1275     return i*scale;
1276 }
1277
1278 #define int_to_grid_scaled_x(x) int_to_grid_scaled((x), GRID_X)
1279 #define int_to_grid_scaled_y(x) int_to_grid_scaled((x), GRID_Y)
1280
1281 I glitter_status_t
1282 glitter_scan_converter_reset(
1283                              glitter_scan_converter_t *converter,
1284                              int xmin, int ymin,
1285                              int xmax, int ymax)
1286 {
1287     glitter_status_t status;
1288
1289     converter->xmin = 0; converter->xmax = 0;
1290     converter->ymin = 0; converter->ymax = 0;
1291
1292     if (xmax - xmin > ARRAY_LENGTH(converter->spans_embedded)) {
1293         converter->spans = _cairo_malloc_ab (xmax - xmin,
1294                                              sizeof (cairo_half_open_span_t));
1295         if (unlikely (converter->spans == NULL))
1296             return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1297     } else
1298         converter->spans = converter->spans_embedded;
1299
1300     xmin = int_to_grid_scaled_x(xmin);
1301     ymin = int_to_grid_scaled_y(ymin);
1302     xmax = int_to_grid_scaled_x(xmax);
1303     ymax = int_to_grid_scaled_y(ymax);
1304
1305     active_list_reset(converter->active);
1306     cell_list_reset(converter->coverages);
1307     status = polygon_reset(converter->polygon, ymin, ymax);
1308     if (status)
1309         return status;
1310
1311     converter->xmin = xmin;
1312     converter->xmax = xmax;
1313     converter->ymin = ymin;
1314     converter->ymax = ymax;
1315     return GLITTER_STATUS_SUCCESS;
1316 }
1317
1318 /* INPUT_TO_GRID_X/Y (in_coord, out_grid_scaled, grid_scale)
1319  *   These macros convert an input coordinate in the client's
1320  *   device space to the rasterisation grid.
1321  */
1322 /* Gah.. this bit of ugly defines INPUT_TO_GRID_X/Y so as to use
1323  * shifts if possible, and something saneish if not.
1324  */
1325 #if !defined(INPUT_TO_GRID_Y) && defined(GRID_Y_BITS) && GRID_Y_BITS <= GLITTER_INPUT_BITS
1326 #  define INPUT_TO_GRID_Y(in, out) (out) = (in) >> (GLITTER_INPUT_BITS - GRID_Y_BITS)
1327 #else
1328 #  define INPUT_TO_GRID_Y(in, out) INPUT_TO_GRID_general(in, out, GRID_Y)
1329 #endif
1330
1331 #if !defined(INPUT_TO_GRID_X) && defined(GRID_X_BITS) && GRID_X_BITS <= GLITTER_INPUT_BITS
1332 #  define INPUT_TO_GRID_X(in, out) (out) = (in) >> (GLITTER_INPUT_BITS - GRID_X_BITS)
1333 #else
1334 #  define INPUT_TO_GRID_X(in, out) INPUT_TO_GRID_general(in, out, GRID_X)
1335 #endif
1336
1337 #define INPUT_TO_GRID_general(in, out, grid_scale) do {         \
1338     long long tmp__ = (long long)(grid_scale) * (in);   \
1339     tmp__ >>= GLITTER_INPUT_BITS;                               \
1340     (out) = tmp__;                                              \
1341 } while (0)
1342
1343 /* Add a new polygon edge from pixel (x1,y1) to (x2,y2) to the scan
1344  * converter.  The coordinates represent pixel positions scaled by
1345  * 2**GLITTER_PIXEL_BITS.  If this function fails then the scan
1346  * converter should be reset or destroyed.  Dir must be +1 or -1,
1347  * with the latter reversing the orientation of the edge. */
1348 I void
1349 glitter_scan_converter_add_edge (glitter_scan_converter_t *converter,
1350                                  const cairo_edge_t *edge)
1351 {
1352     cairo_edge_t e;
1353
1354     INPUT_TO_GRID_Y (edge->top, e.top);
1355     INPUT_TO_GRID_Y (edge->bottom, e.bottom);
1356     if (e.top >= e.bottom)
1357         return;
1358
1359     /* XXX: possible overflows if GRID_X/Y > 2**GLITTER_INPUT_BITS */
1360     INPUT_TO_GRID_Y (edge->line.p1.y, e.line.p1.y);
1361     INPUT_TO_GRID_Y (edge->line.p2.y, e.line.p2.y);
1362     if (e.line.p1.y == e.line.p2.y)
1363         return;
1364
1365     INPUT_TO_GRID_X (edge->line.p1.x, e.line.p1.x);
1366     INPUT_TO_GRID_X (edge->line.p2.x, e.line.p2.x);
1367
1368     e.dir = edge->dir;
1369
1370     polygon_add_edge (converter->polygon, &e);
1371 }
1372
1373 static void
1374 step_edges (struct active_list *active, int count)
1375 {
1376     struct edge *edge;
1377
1378     count *= GRID_Y;
1379     for (edge = active->head.next; edge != &active->tail; edge = edge->next) {
1380         edge->height_left -= count;
1381         if (! edge->height_left) {
1382             edge->prev->next = edge->next;
1383             edge->next->prev = edge->prev;
1384         }
1385     }
1386 }
1387
1388 static glitter_status_t
1389 blit_a8 (struct cell_list *cells,
1390          cairo_span_renderer_t *renderer,
1391          cairo_half_open_span_t *spans,
1392          int y, int height,
1393          int xmin, int xmax)
1394 {
1395     struct cell *cell = cells->head.next;
1396     int prev_x = xmin, last_x = -1;
1397     int16_t cover = 0, last_cover = 0;
1398     unsigned num_spans;
1399
1400     if (cell == &cells->tail)
1401         return CAIRO_STATUS_SUCCESS;
1402
1403     /* Skip cells to the left of the clip region. */
1404     while (cell->x < xmin) {
1405         cover += cell->covered_height;
1406         cell = cell->next;
1407     }
1408     cover *= GRID_X*2;
1409
1410     /* Form the spans from the coverages and areas. */
1411     num_spans = 0;
1412     for (; cell->x < xmax; cell = cell->next) {
1413         int x = cell->x;
1414         int16_t area;
1415
1416         if (x > prev_x && cover != last_cover) {
1417             spans[num_spans].x = prev_x;
1418             spans[num_spans].coverage = GRID_AREA_TO_ALPHA (cover);
1419             last_cover = cover;
1420             last_x = prev_x;
1421             ++num_spans;
1422         }
1423
1424         cover += cell->covered_height*GRID_X*2;
1425         area = cover - cell->uncovered_area;
1426
1427         if (area != last_cover) {
1428             spans[num_spans].x = x;
1429             spans[num_spans].coverage = GRID_AREA_TO_ALPHA (area);
1430             last_cover = area;
1431             last_x = x;
1432             ++num_spans;
1433         }
1434
1435         prev_x = x+1;
1436     }
1437
1438     if (prev_x <= xmax && cover != last_cover) {
1439         spans[num_spans].x = prev_x;
1440         spans[num_spans].coverage = GRID_AREA_TO_ALPHA (cover);
1441         last_cover = cover;
1442         last_x = prev_x;
1443         ++num_spans;
1444     }
1445
1446     if (last_x < xmax && last_cover) {
1447         spans[num_spans].x = xmax;
1448         spans[num_spans].coverage = 0;
1449         ++num_spans;
1450     }
1451
1452     /* Dump them into the renderer. */
1453     return renderer->render_rows (renderer, y, height, spans, num_spans);
1454 }
1455
1456 #define GRID_AREA_TO_A1(A)  ((GRID_AREA_TO_ALPHA (A) > 127) ? 255 : 0)
1457 static glitter_status_t
1458 blit_a1 (struct cell_list *cells,
1459          cairo_span_renderer_t *renderer,
1460          cairo_half_open_span_t *spans,
1461          int y, int height,
1462          int xmin, int xmax)
1463 {
1464     struct cell *cell = cells->head.next;
1465     int prev_x = xmin, last_x = -1;
1466     int16_t cover = 0;
1467     uint8_t coverage, last_cover = 0;
1468     unsigned num_spans;
1469
1470     if (cell == &cells->tail)
1471         return CAIRO_STATUS_SUCCESS;
1472
1473     /* Skip cells to the left of the clip region. */
1474     while (cell->x < xmin) {
1475         cover += cell->covered_height;
1476         cell = cell->next;
1477     }
1478     cover *= GRID_X*2;
1479
1480     /* Form the spans from the coverages and areas. */
1481     num_spans = 0;
1482     for (; cell->x < xmax; cell = cell->next) {
1483         int x = cell->x;
1484         int16_t area;
1485
1486         coverage = GRID_AREA_TO_A1 (cover);
1487         if (x > prev_x && coverage != last_cover) {
1488             last_x = spans[num_spans].x = prev_x;
1489             last_cover = spans[num_spans].coverage = coverage;
1490             ++num_spans;
1491         }
1492
1493         cover += cell->covered_height*GRID_X*2;
1494         area = cover - cell->uncovered_area;
1495
1496         coverage = GRID_AREA_TO_A1 (area);
1497         if (coverage != last_cover) {
1498             last_x = spans[num_spans].x = x;
1499             last_cover = spans[num_spans].coverage = coverage;
1500             ++num_spans;
1501         }
1502
1503         prev_x = x+1;
1504     }
1505
1506     coverage = GRID_AREA_TO_A1 (cover);
1507     if (prev_x <= xmax && coverage != last_cover) {
1508         last_x = spans[num_spans].x = prev_x;
1509         last_cover = spans[num_spans].coverage = coverage;
1510         ++num_spans;
1511     }
1512
1513     if (last_x < xmax && last_cover) {
1514         spans[num_spans].x = xmax;
1515         spans[num_spans].coverage = 0;
1516         ++num_spans;
1517     }
1518     if (num_spans == 1)
1519         return CAIRO_STATUS_SUCCESS;
1520
1521     /* Dump them into the renderer. */
1522     return renderer->render_rows (renderer, y, height, spans, num_spans);
1523 }
1524
1525
1526 I void
1527 glitter_scan_converter_render(glitter_scan_converter_t *converter,
1528                               unsigned int winding_mask,
1529                               int antialias,
1530                               cairo_span_renderer_t *renderer)
1531 {
1532     int i, j;
1533     int ymax_i = converter->ymax / GRID_Y;
1534     int ymin_i = converter->ymin / GRID_Y;
1535     int xmin_i, xmax_i;
1536     int h = ymax_i - ymin_i;
1537     struct polygon *polygon = converter->polygon;
1538     struct cell_list *coverages = converter->coverages;
1539     struct active_list *active = converter->active;
1540     struct edge *buckets[GRID_Y] = { 0 };
1541
1542     xmin_i = converter->xmin / GRID_X;
1543     xmax_i = converter->xmax / GRID_X;
1544     if (xmin_i >= xmax_i)
1545         return;
1546
1547     /* Render each pixel row. */
1548     for (i = 0; i < h; i = j) {
1549         int do_full_row = 0;
1550
1551         j = i + 1;
1552
1553         /* Determine if we can ignore this row or use the full pixel
1554          * stepper. */
1555         if (! polygon->y_buckets[i]) {
1556             if (active->head.next == &active->tail) {
1557                 active->min_height = INT_MAX;
1558                 active->is_vertical = 1;
1559                 for (; j < h && ! polygon->y_buckets[j]; j++)
1560                     ;
1561                 continue;
1562             }
1563
1564             do_full_row = can_do_full_row (active);
1565         }
1566
1567         if (do_full_row) {
1568             /* Step by a full pixel row's worth. */
1569             full_row (active, coverages, winding_mask);
1570
1571             if (active->is_vertical) {
1572                 while (j < h &&
1573                        polygon->y_buckets[j] == NULL &&
1574                        active->min_height >= 2*GRID_Y)
1575                 {
1576                     active->min_height -= GRID_Y;
1577                     j++;
1578                 }
1579                 if (j != i + 1)
1580                     step_edges (active, j - (i + 1));
1581             }
1582         } else {
1583             int sub;
1584
1585             polygon_fill_buckets (active,
1586                                   polygon->y_buckets[i],
1587                                   (i+ymin_i)*GRID_Y,
1588                                   buckets);
1589
1590             /* Subsample this row. */
1591             for (sub = 0; sub < GRID_Y; sub++) {
1592                 if (buckets[sub]) {
1593                     active_list_merge_edges_from_bucket (active, buckets[sub]);
1594                     buckets[sub] = NULL;
1595                 }
1596
1597                 sub_row (active, coverages, winding_mask);
1598             }
1599         }
1600
1601         if (antialias)
1602             blit_a8 (coverages, renderer, converter->spans,
1603                      i+ymin_i, j-i, xmin_i, xmax_i);
1604         else
1605             blit_a1 (coverages, renderer, converter->spans,
1606                      i+ymin_i, j-i, xmin_i, xmax_i);
1607         cell_list_reset (coverages);
1608
1609         active->min_height -= GRID_Y;
1610     }
1611 }
1612
1613 struct _cairo_tor22_scan_converter {
1614     cairo_scan_converter_t base;
1615
1616     glitter_scan_converter_t converter[1];
1617     cairo_fill_rule_t fill_rule;
1618     cairo_antialias_t antialias;
1619
1620     jmp_buf jmp;
1621 };
1622
1623 typedef struct _cairo_tor22_scan_converter cairo_tor22_scan_converter_t;
1624
1625 static void
1626 _cairo_tor22_scan_converter_destroy (void *converter)
1627 {
1628     cairo_tor22_scan_converter_t *self = converter;
1629     if (self == NULL) {
1630         return;
1631     }
1632     _glitter_scan_converter_fini (self->converter);
1633     free(self);
1634 }
1635
1636 cairo_status_t
1637 _cairo_tor22_scan_converter_add_polygon (void           *converter,
1638                                        const cairo_polygon_t *polygon)
1639 {
1640     cairo_tor22_scan_converter_t *self = converter;
1641     int i;
1642
1643 #if 0
1644     FILE *file = fopen ("polygon.txt", "w");
1645     _cairo_debug_print_polygon (file, polygon);
1646     fclose (file);
1647 #endif
1648
1649     for (i = 0; i < polygon->num_edges; i++)
1650          glitter_scan_converter_add_edge (self->converter, &polygon->edges[i]);
1651
1652     return CAIRO_STATUS_SUCCESS;
1653 }
1654
1655 static cairo_status_t
1656 _cairo_tor22_scan_converter_generate (void                      *converter,
1657                                     cairo_span_renderer_t       *renderer)
1658 {
1659     cairo_tor22_scan_converter_t *self = converter;
1660     cairo_status_t status;
1661
1662     if ((status = setjmp (self->jmp)))
1663         return _cairo_scan_converter_set_error (self, _cairo_error (status));
1664
1665     glitter_scan_converter_render (self->converter,
1666                                    self->fill_rule == CAIRO_FILL_RULE_WINDING ? ~0 : 1,
1667                                    self->antialias != CAIRO_ANTIALIAS_NONE,
1668                                    renderer);
1669     return CAIRO_STATUS_SUCCESS;
1670 }
1671
1672 cairo_scan_converter_t *
1673 _cairo_tor22_scan_converter_create (int                 xmin,
1674                                   int                   ymin,
1675                                   int                   xmax,
1676                                   int                   ymax,
1677                                   cairo_fill_rule_t     fill_rule,
1678                                   cairo_antialias_t     antialias)
1679 {
1680     cairo_tor22_scan_converter_t *self;
1681     cairo_status_t status;
1682
1683     self = malloc (sizeof(struct _cairo_tor22_scan_converter));
1684     if (unlikely (self == NULL)) {
1685         status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1686         goto bail_nomem;
1687     }
1688
1689     self->base.destroy = _cairo_tor22_scan_converter_destroy;
1690     self->base.generate = _cairo_tor22_scan_converter_generate;
1691
1692     _glitter_scan_converter_init (self->converter, &self->jmp);
1693     status = glitter_scan_converter_reset (self->converter,
1694                                            xmin, ymin, xmax, ymax);
1695     if (unlikely (status))
1696         goto bail;
1697
1698     self->fill_rule = fill_rule;
1699     self->antialias = antialias;
1700
1701     return &self->base;
1702
1703  bail:
1704     self->base.destroy(&self->base);
1705  bail_nomem:
1706     return _cairo_scan_converter_create_in_error (status);
1707 }