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 <assert.h>
101 #include <stdlib.h>
102 #include <string.h>
103 #include <limits.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
121 /* Set glitter up to use a cairo span renderer to do the coverage
122 * blitting. */
123 struct pool;
124 struct cell_list;
125
126 static glitter_status_t
127 blit_with_span_renderer(
128 struct cell_list *coverages,
129 cairo_span_renderer_t *span_renderer,
130 struct pool *span_pool,
131 int y,
132 int height,
133 int xmin,
134 int xmax);
135
136 static glitter_status_t
137 blit_empty_with_span_renderer (cairo_span_renderer_t *renderer, int y, int height);
138
139 #define GLITTER_BLIT_COVERAGES_ARGS \
140 cairo_span_renderer_t *span_renderer, \
141 struct pool *span_pool
142
143 #define GLITTER_BLIT_COVERAGES(cells, y, height,xmin, xmax) do { \
144 cairo_status_t status = blit_with_span_renderer (cells, \
145 span_renderer, \
146 span_pool, \
147 y, height, \
148 xmin, xmax); \
149 if (unlikely (status)) \
150 return status; \
151 } while (0)
152
153 #define GLITTER_BLIT_COVERAGES_EMPTY(y, height, xmin, xmax) do { \
154 cairo_status_t status = blit_empty_with_span_renderer (span_renderer, y, height); \
155 if (unlikely (status)) \
156 return status; \
157 } while (0)
158
159 /*-------------------------------------------------------------------------
160 * glitter-paths.h
161 */
162
163 /* "Input scaled" numbers are fixed precision reals with multiplier
164 * 2**GLITTER_INPUT_BITS. Input coordinates are given to glitter as
165 * pixel scaled numbers. These get converted to the internal grid
166 * scaled numbers as soon as possible. Internal overflow is possible
167 * if GRID_X/Y inside glitter-paths.c is larger than
168 * 1<<GLITTER_INPUT_BITS. */
169 #ifndef GLITTER_INPUT_BITS
170 # define GLITTER_INPUT_BITS 8
171 #endif
172 #define GLITTER_INPUT_SCALE (1<<GLITTER_INPUT_BITS)
173 typedef int glitter_input_scaled_t;
174
175 #if !GLITTER_HAVE_STATUS_T
176 typedef enum {
177 GLITTER_STATUS_SUCCESS = 0,
178 GLITTER_STATUS_NO_MEMORY
179 } glitter_status_t;
180 #endif
181
182 #ifndef I
183 # define I /*static*/
184 #endif
185
186 /* Opaque type for scan converting. */
187 typedef struct glitter_scan_converter glitter_scan_converter_t;
188
189 /* Reset a scan converter to accept polygon edges and set the clip box
190 * in pixels. Allocates O(ymax-ymin) bytes of memory. The clip box
191 * is set to integer pixel coordinates xmin <= x < xmax, ymin <= y <
192 * ymax. */
193 I glitter_status_t
194 glitter_scan_converter_reset(
195 glitter_scan_converter_t *converter,
196 int xmin, int ymin,
197 int xmax, int ymax);
198
199 /* Add a new polygon edge from pixel (x1,y1) to (x2,y2) to the scan
200 * converter. The coordinates represent pixel positions scaled by
201 * 2**GLITTER_PIXEL_BITS. If this function fails then the scan
202 * converter should be reset or destroyed. Dir must be +1 or -1,
203 * with the latter reversing the orientation of the edge. */
204 I glitter_status_t
205 glitter_scan_converter_add_edge (glitter_scan_converter_t *converter,
206 const cairo_edge_t *edge);
207
208 /* Render the polygon in the scan converter to the given A8 format
209 * image raster. Only the pixels accessible as pixels[y*stride+x] for
210 * x,y inside the clip box are written to, where xmin <= x < xmax,
211 * ymin <= y < ymax. The image is assumed to be clear on input.
212 *
213 * If nonzero_fill is true then the interior of the polygon is
214 * computed with the non-zero fill rule. Otherwise the even-odd fill
215 * rule is used.
216 *
217 * The scan converter must be reset or destroyed after this call. */
218 #ifndef GLITTER_BLIT_COVERAGES_ARGS
219 # define GLITTER_BLIT_COVERAGES_ARGS unsigned char *raster_pixels, long raster_stride
220 #endif
221 I glitter_status_t
222 glitter_scan_converter_render(
223 glitter_scan_converter_t *converter,
224 int nonzero_fill,
225 GLITTER_BLIT_COVERAGES_ARGS);
226
227 /*-------------------------------------------------------------------------
228 * glitter-paths.c: Implementation internal types
229 */
230 #include <stdlib.h>
231 #include <string.h>
232 #include <limits.h>
233
234 /* All polygon coordinates are snapped onto a subsample grid. "Grid
235 * scaled" numbers are fixed precision reals with multiplier GRID_X or
236 * GRID_Y. */
237 typedef int grid_scaled_t;
238 typedef int grid_scaled_x_t;
239 typedef int grid_scaled_y_t;
240
241 /* Default x/y scale factors.
242 * You can either define GRID_X/Y_BITS to get a power-of-two scale
243 * or define GRID_X/Y separately. */
244 #if !defined(GRID_X) && !defined(GRID_X_BITS)
245 # define GRID_X_BITS 8
246 #endif
247 #if !defined(GRID_Y) && !defined(GRID_Y_BITS)
248 # define GRID_Y 15
249 #endif
250
251 /* Use GRID_X/Y_BITS to define GRID_X/Y if they're available. */
252 #ifdef GRID_X_BITS
253 # define GRID_X (1 << GRID_X_BITS)
254 #endif
255 #ifdef GRID_Y_BITS
256 # define GRID_Y (1 << GRID_Y_BITS)
257 #endif
258
259 /* The GRID_X_TO_INT_FRAC macro splits a grid scaled coordinate into
260 * integer and fractional parts. The integer part is floored. */
261 #if defined(GRID_X_TO_INT_FRAC)
262 /* do nothing */
263 #elif defined(GRID_X_BITS)
264 # define GRID_X_TO_INT_FRAC(x, i, f) \
265 _GRID_TO_INT_FRAC_shift(x, i, f, GRID_X_BITS)
266 #else
267 # define GRID_X_TO_INT_FRAC(x, i, f) \
268 _GRID_TO_INT_FRAC_general(x, i, f, GRID_X)
269 #endif
270
271 #define _GRID_TO_INT_FRAC_general(t, i, f, m) do { \
272 (i) = (t) / (m); \
273 (f) = (t) % (m); \
274 if ((f) < 0) { \
275 --(i); \
276 (f) += (m); \
277 } \
278 } while (0)
279
280 #define _GRID_TO_INT_FRAC_shift(t, i, f, b) do { \
281 (f) = (t) & ((1 << (b)) - 1); \
282 (i) = (t) >> (b); \
283 } while (0)
284
285 /* A grid area is a real in [0,1] scaled by 2*GRID_X*GRID_Y. We want
286 * to be able to represent exactly areas of subpixel trapezoids whose
287 * vertices are given in grid scaled coordinates. The scale factor
288 * comes from needing to accurately represent the area 0.5*dx*dy of a
289 * triangle with base dx and height dy in grid scaled numbers. */
290 typedef int grid_area_t;
291 #define GRID_XY (2*GRID_X*GRID_Y) /* Unit area on the grid. */
292
293 /* GRID_AREA_TO_ALPHA(area): map [0,GRID_XY] to [0,255]. */
294 #if GRID_XY == 510
295 # define GRID_AREA_TO_ALPHA(c) (((c)+1) >> 1)
296 #elif GRID_XY == 255
297 # define GRID_AREA_TO_ALPHA(c) (c)
298 #elif GRID_XY == 64
299 # define GRID_AREA_TO_ALPHA(c) (((c) << 2) | -(((c) & 0x40) >> 6))
300 #elif GRID_XY == 128
301 # define GRID_AREA_TO_ALPHA(c) ((((c) << 1) | -((c) >> 7)) & 255)
302 #elif GRID_XY == 256
303 # define GRID_AREA_TO_ALPHA(c) (((c) | -((c) >> 8)) & 255)
304 #elif GRID_XY == 15
305 # define GRID_AREA_TO_ALPHA(c) (((c) << 4) + (c))
306 #elif GRID_XY == 2*256*15
307 # define GRID_AREA_TO_ALPHA(c) (((c) + ((c)<<4) + 256) >> 9)
308 #else
309 # define GRID_AREA_TO_ALPHA(c) (((c)*255 + GRID_XY/2) / GRID_XY)
310 #endif
311
312 #define UNROLL3(x) x x x
313
314 struct quorem {
315 int32_t quo;
316 int32_t rem;
317 };
318
319 /* Header for a chunk of memory in a memory pool. */
320 struct _pool_chunk {
321 /* # bytes used in this chunk. */
322 size_t size;
323
324 /* # bytes total in this chunk */
325 size_t capacity;
326
327 /* Pointer to the previous chunk or %NULL if this is the sentinel
328 * chunk in the pool header. */
329 struct _pool_chunk *prev_chunk;
330
331 /* Actual data starts here. Well aligned for pointers. */
332 };
333
334 /* A memory pool. This is supposed to be embedded on the stack or
335 * within some other structure. It may optionally be followed by an
336 * embedded array from which requests are fulfilled until
337 * malloc needs to be called to allocate a first real chunk. */
338 struct pool {
339 /* Chunk we're allocating from. */
340 struct _pool_chunk *current;
341
342 /* Free list of previously allocated chunks. All have >= default
343 * capacity. */
344 struct _pool_chunk *first_free;
345
346 /* The default capacity of a chunk. */
347 size_t default_capacity;
348
349 /* Header for the sentinel chunk. Directly following the pool
350 * struct should be some space for embedded elements from which
351 * the sentinel chunk allocates from. */
352 struct _pool_chunk sentinel[1];
353 };
354
355 /* A polygon edge. */
356 struct edge {
357 /* Next in y-bucket or active list. */
358 struct edge *next;
359
360 /* Current x coordinate while the edge is on the active
361 * list. Initialised to the x coordinate of the top of the
362 * edge. The quotient is in grid_scaled_x_t units and the
363 * remainder is mod dy in grid_scaled_y_t units.*/
364 struct quorem x;
365
366 /* Advance of the current x when moving down a subsample line. */
367 struct quorem dxdy;
368
369 /* Advance of the current x when moving down a full pixel
370 * row. Only initialised when the height of the edge is large
371 * enough that there's a chance the edge could be stepped by a
372 * full row's worth of subsample rows at a time. */
373 struct quorem dxdy_full;
374
375 /* The clipped y of the top of the edge. */
376 grid_scaled_y_t ytop;
377
378 /* y2-y1 after orienting the edge downwards. */
379 grid_scaled_y_t dy;
380
381 /* Number of subsample rows remaining to scan convert of this
382 * edge. */
383 grid_scaled_y_t height_left;
384
385 /* Original sign of the edge: +1 for downwards, -1 for upwards
386 * edges. */
387 int dir;
388 int vertical;
389 };
390
391 /* Number of subsample rows per y-bucket. Must be GRID_Y. */
392 #define EDGE_Y_BUCKET_HEIGHT GRID_Y
393
394 #define EDGE_Y_BUCKET_INDEX(y, ymin) (((y) - (ymin))/EDGE_Y_BUCKET_HEIGHT)
395
396 struct bucket {
397 /* Unsorted list of edges starting within this bucket. */
398 struct edge *edges;
399
400 /* Set to non-zero if there are edges starting strictly within the
401 * bucket. */
402 unsigned have_inside_edges;
403 };
404
405 /* A collection of sorted and vertically clipped edges of the polygon.
406 * Edges are moved from the polygon to an active list while scan
407 * converting. */
408 struct polygon {
409 /* The clip extents. */
410 grid_scaled_x_t xmin, xmax;
411 grid_scaled_y_t ymin, ymax;
412
413 /* Array of edges all starting in the same bucket. An edge is put
414 * into bucket EDGE_BUCKET_INDEX(edge->ytop, polygon->ymin) when
415 * it is added to the polygon. */
416 struct bucket *y_buckets;
417 struct bucket y_buckets_embedded[64];
418
419 struct {
420 struct pool base[1];
421 struct edge embedded[32];
422 } edge_pool;
423 };
424
425 /* A cell records the effect on pixel coverage of polygon edges
426 * passing through a pixel. It contains two accumulators of pixel
427 * coverage.
428 *
429 * Consider the effects of a polygon edge on the coverage of a pixel
430 * it intersects and that of the following one. The coverage of the
431 * following pixel is the height of the edge multiplied by the width
432 * of the pixel, and the coverage of the pixel itself is the area of
433 * the trapezoid formed by the edge and the right side of the pixel.
434 *
435 * +-----------------------+-----------------------+
436 * | | |
437 * | | |
438 * |_______________________|_______________________|
439 * | \...................|.......................|\
440 * | \..................|.......................| |
441 * | \.................|.......................| |
442 * | \....covered.....|.......................| |
443 * | \....area.......|.......................| } covered height
444 * | \..............|.......................| |
445 * |uncovered\.............|.......................| |
446 * | area \............|.......................| |
447 * |___________\...........|.......................|/
448 * | | |
449 * | | |
450 * | | |
451 * +-----------------------+-----------------------+
452 *
453 * Since the coverage of the following pixel will always be a multiple
454 * of the width of the pixel, we can store the height of the covered
455 * area instead. The coverage of the pixel itself is the total
456 * coverage minus the area of the uncovered area to the left of the
457 * edge. As it's faster to compute the uncovered area we only store
458 * that and subtract it from the total coverage later when forming
459 * spans to blit.
460 *
461 * The heights and areas are signed, with left edges of the polygon
462 * having positive sign and right edges having negative sign. When
463 * two edges intersect they swap their left/rightness so their
464 * contribution above and below the intersection point must be
465 * computed separately. */
466 struct cell {
467 struct cell *next;
468 int x;
469 grid_area_t uncovered_area;
470 grid_scaled_y_t covered_height;
471 };
472
473 /* A cell list represents the scan line sparsely as cells ordered by
474 * ascending x. It is geared towards scanning the cells in order
475 * using an internal cursor. */
476 struct cell_list {
477 /* Points to the left-most cell in the scan line. */
478 struct cell *head;
479 /* Sentinel node */
480 struct cell tail;
481
482 /* Cursor state for iterating through the cell list. Points to
483 * a pointer to the current cell: either &cell_list->head or the next
484 * field of the previous cell. */
485 struct cell **cursor;
486
487 /* Cells in the cell list are owned by the cell list and are
488 * allocated from this pool. */
489 struct {
490 struct pool base[1];
491 struct cell embedded[32];
492 } cell_pool;
493 };
494
495 struct cell_pair {
496 struct cell *cell1;
497 struct cell *cell2;
498 };
499
500 /* The active list contains edges in the current scan line ordered by
501 * the x-coordinate of the intercept of the edge and the scan line. */
502 struct active_list {
503 /* Leftmost edge on the current scan line. */
504 struct edge *head;
505
506 /* A lower bound on the height of the active edges is used to
507 * estimate how soon some active edge ends. We can't advance the
508 * scan conversion by a full pixel row if an edge ends somewhere
509 * within it. */
510 grid_scaled_y_t min_height;
511 };
512
513 struct glitter_scan_converter {
514 struct polygon polygon[1];
515 struct active_list active[1];
516 struct cell_list coverages[1];
517
518 /* Clip box. */
519 grid_scaled_x_t xmin, xmax;
520 grid_scaled_y_t ymin, ymax;
521 };
522
523 /* Compute the floored division a/b. Assumes / and % perform symmetric
524 * division. */
525 inline static struct quorem
floored_divrem(int a,int b)526 floored_divrem(int a, int b)
527 {
528 struct quorem qr;
529 qr.quo = a/b;
530 qr.rem = a%b;
531 if ((a^b)<0 && qr.rem) {
532 qr.quo -= 1;
533 qr.rem += b;
534 }
535 return qr;
536 }
537
538 /* Compute the floored division (x*a)/b. Assumes / and % perform symmetric
539 * division. */
540 static struct quorem
floored_muldivrem(int x,int a,int b)541 floored_muldivrem(int x, int a, int b)
542 {
543 struct quorem qr;
544 long long xa = (long long)x*a;
545 qr.quo = xa/b;
546 qr.rem = xa%b;
547 if ((xa>=0) != (b>=0) && qr.rem) {
548 qr.quo -= 1;
549 qr.rem += b;
550 }
551 return qr;
552 }
553
554 static void
_pool_chunk_init(struct _pool_chunk * p,struct _pool_chunk * prev_chunk,size_t capacity)555 _pool_chunk_init(
556 struct _pool_chunk *p,
557 struct _pool_chunk *prev_chunk,
558 size_t capacity)
559 {
560 p->prev_chunk = prev_chunk;
561 p->size = 0;
562 p->capacity = capacity;
563 }
564
565 static struct _pool_chunk *
_pool_chunk_create(struct _pool_chunk * prev_chunk,size_t size)566 _pool_chunk_create(
567 struct _pool_chunk *prev_chunk,
568 size_t size)
569 {
570 struct _pool_chunk *p;
571 size_t size_with_head = size + sizeof(struct _pool_chunk);
572 if (size_with_head < size)
573 return NULL;
574 p = malloc(size_with_head);
575 if (p)
576 _pool_chunk_init(p, prev_chunk, size);
577 return p;
578 }
579
580 static void
pool_init(struct pool * pool,size_t default_capacity,size_t embedded_capacity)581 pool_init(
582 struct pool *pool,
583 size_t default_capacity,
584 size_t embedded_capacity)
585 {
586 pool->current = pool->sentinel;
587 pool->first_free = NULL;
588 pool->default_capacity = default_capacity;
589 _pool_chunk_init(pool->sentinel, NULL, embedded_capacity);
590 }
591
592 static void
pool_fini(struct pool * pool)593 pool_fini(struct pool *pool)
594 {
595 struct _pool_chunk *p = pool->current;
596 do {
597 while (NULL != p) {
598 struct _pool_chunk *prev = p->prev_chunk;
599 if (p != pool->sentinel)
600 free(p);
601 p = prev;
602 }
603 p = pool->first_free;
604 pool->first_free = NULL;
605 } while (NULL != p);
606 pool_init(pool, 0, 0);
607 }
608
609 /* Satisfy an allocation by first allocating a new large enough chunk
610 * and adding it to the head of the pool's chunk list. This function
611 * is called as a fallback if pool_alloc() couldn't do a quick
612 * allocation from the current chunk in the pool. */
613 static void *
_pool_alloc_from_new_chunk(struct pool * pool,size_t size)614 _pool_alloc_from_new_chunk(
615 struct pool *pool,
616 size_t size)
617 {
618 struct _pool_chunk *chunk;
619 void *obj;
620 size_t capacity;
621
622 /* If the allocation is smaller than the default chunk size then
623 * try getting a chunk off the free list. Force alloc of a new
624 * chunk for large requests. */
625 capacity = size;
626 chunk = NULL;
627 if (size < pool->default_capacity) {
628 capacity = pool->default_capacity;
629 chunk = pool->first_free;
630 if (chunk) {
631 pool->first_free = chunk->prev_chunk;
632 _pool_chunk_init(chunk, pool->current, chunk->capacity);
633 }
634 }
635
636 if (NULL == chunk) {
637 chunk = _pool_chunk_create (pool->current, capacity);
638 if (unlikely (NULL == chunk))
639 return NULL;
640 }
641 pool->current = chunk;
642
643 obj = ((unsigned char*)chunk + sizeof(*chunk) + chunk->size);
644 chunk->size += size;
645 return obj;
646 }
647
648 /* Allocate size bytes from the pool. The first allocated address
649 * returned from a pool is aligned to sizeof(void*). Subsequent
650 * addresses will maintain alignment as long as multiples of void* are
651 * allocated. Returns the address of a new memory area or %NULL on
652 * allocation failures. The pool retains ownership of the returned
653 * memory. */
654 inline static void *
pool_alloc(struct pool * pool,size_t size)655 pool_alloc (struct pool *pool, size_t size)
656 {
657 struct _pool_chunk *chunk = pool->current;
658
659 if (size <= chunk->capacity - chunk->size) {
660 void *obj = ((unsigned char*)chunk + sizeof(*chunk) + chunk->size);
661 chunk->size += size;
662 return obj;
663 } else {
664 return _pool_alloc_from_new_chunk(pool, size);
665 }
666 }
667
668 /* Relinquish all pool_alloced memory back to the pool. */
669 static void
pool_reset(struct pool * pool)670 pool_reset (struct pool *pool)
671 {
672 /* Transfer all used chunks to the chunk free list. */
673 struct _pool_chunk *chunk = pool->current;
674 if (chunk != pool->sentinel) {
675 while (chunk->prev_chunk != pool->sentinel) {
676 chunk = chunk->prev_chunk;
677 }
678 chunk->prev_chunk = pool->first_free;
679 pool->first_free = pool->current;
680 }
681 /* Reset the sentinel as the current chunk. */
682 pool->current = pool->sentinel;
683 pool->sentinel->size = 0;
684 }
685
686 /* Rewinds the cell list's cursor to the beginning. After rewinding
687 * we're good to cell_list_find() the cell any x coordinate. */
688 inline static void
cell_list_rewind(struct cell_list * cells)689 cell_list_rewind (struct cell_list *cells)
690 {
691 cells->cursor = &cells->head;
692 }
693
694 /* Rewind the cell list if its cursor has been advanced past x. */
695 inline static void
cell_list_maybe_rewind(struct cell_list * cells,int x)696 cell_list_maybe_rewind (struct cell_list *cells, int x)
697 {
698 struct cell *tail = *cells->cursor;
699 if (tail->x > x)
700 cell_list_rewind (cells);
701 }
702
703 static void
cell_list_init(struct cell_list * cells)704 cell_list_init(struct cell_list *cells)
705 {
706 pool_init(cells->cell_pool.base,
707 256*sizeof(struct cell),
708 sizeof(cells->cell_pool.embedded));
709 cells->tail.next = NULL;
710 cells->tail.x = INT_MAX;
711 cells->head = &cells->tail;
712 cell_list_rewind (cells);
713 }
714
715 static void
cell_list_fini(struct cell_list * cells)716 cell_list_fini(struct cell_list *cells)
717 {
718 pool_fini (cells->cell_pool.base);
719 }
720
721 /* Empty the cell list. This is called at the start of every pixel
722 * row. */
723 inline static void
cell_list_reset(struct cell_list * cells)724 cell_list_reset (struct cell_list *cells)
725 {
726 cell_list_rewind (cells);
727 cells->head = &cells->tail;
728 pool_reset (cells->cell_pool.base);
729 }
730
731 static struct cell *
cell_list_alloc(struct cell_list * cells,struct cell ** cursor,struct cell * tail,int x)732 cell_list_alloc (struct cell_list *cells,
733 struct cell **cursor,
734 struct cell *tail,
735 int x)
736 {
737 struct cell *cell;
738
739 cell = pool_alloc (cells->cell_pool.base, sizeof (struct cell));
740 if (unlikely (NULL == cell))
741 return NULL;
742
743 *cursor = cell;
744 cell->next = tail;
745 cell->x = x;
746 cell->uncovered_area = 0;
747 cell->covered_height = 0;
748 return cell;
749 }
750
751 /* Find a cell at the given x-coordinate. Returns %NULL if a new cell
752 * needed to be allocated but couldn't be. Cells must be found with
753 * non-decreasing x-coordinate until the cell list is rewound using
754 * cell_list_rewind(). Ownership of the returned cell is retained by
755 * the cell list. */
756 inline static struct cell *
cell_list_find(struct cell_list * cells,int x)757 cell_list_find (struct cell_list *cells, int x)
758 {
759 struct cell **cursor = cells->cursor;
760 struct cell *tail;
761
762 while (1) {
763 UNROLL3({
764 tail = *cursor;
765 if (tail->x >= x) {
766 break;
767 }
768 cursor = &tail->next;
769 });
770 }
771 cells->cursor = cursor;
772
773 if (tail->x == x)
774 return tail;
775
776 return cell_list_alloc (cells, cursor, tail, x);
777 }
778
779 /* Find two cells at x1 and x2. This is exactly equivalent
780 * to
781 *
782 * pair.cell1 = cell_list_find(cells, x1);
783 * pair.cell2 = cell_list_find(cells, x2);
784 *
785 * except with less function call overhead. */
786 inline static struct cell_pair
cell_list_find_pair(struct cell_list * cells,int x1,int x2)787 cell_list_find_pair(struct cell_list *cells, int x1, int x2)
788 {
789 struct cell_pair pair;
790 struct cell **cursor = cells->cursor;
791 struct cell *cell1;
792 struct cell *cell2;
793 struct cell *newcell;
794
795 /* Find first cell at x1. */
796 while (1) {
797 UNROLL3({
798 cell1 = *cursor;
799 if (cell1->x > x1)
800 break;
801
802 if (cell1->x == x1)
803 goto found_first;
804
805 cursor = &cell1->next;
806 });
807 }
808
809 /* New first cell at x1. */
810 newcell = pool_alloc (cells->cell_pool.base,
811 sizeof (struct cell));
812 if (likely (NULL != newcell)) {
813 *cursor = newcell;
814 newcell->next = cell1;
815 newcell->x = x1;
816 newcell->uncovered_area = 0;
817 newcell->covered_height = 0;
818 }
819 cell1 = newcell;
820 found_first:
821
822 /* Find second cell at x2. */
823 while (1) {
824 UNROLL3({
825 cell2 = *cursor;
826 if (cell2->x > x2)
827 break;
828 if (cell2->x == x2)
829 goto found_second;
830 cursor = &cell2->next;
831 });
832 }
833
834 /* New second cell at x2. */
835 newcell = pool_alloc (cells->cell_pool.base,
836 sizeof (struct cell));
837 if (likely (NULL != newcell)) {
838 *cursor = newcell;
839 newcell->next = cell2;
840 newcell->x = x2;
841 newcell->uncovered_area = 0;
842 newcell->covered_height = 0;
843 }
844 cell2 = newcell;
845 found_second:
846
847 cells->cursor = cursor;
848 pair.cell1 = cell1;
849 pair.cell2 = cell2;
850 return pair;
851 }
852
853 /* Add an unbounded subpixel span covering subpixels >= x to the
854 * coverage cells. */
855 static glitter_status_t
cell_list_add_unbounded_subspan(struct cell_list * cells,grid_scaled_x_t x)856 cell_list_add_unbounded_subspan (struct cell_list *cells,
857 grid_scaled_x_t x)
858 {
859 struct cell *cell;
860 int ix, fx;
861
862 GRID_X_TO_INT_FRAC(x, ix, fx);
863
864 cell = cell_list_find (cells, ix);
865 if (likely (cell != NULL)) {
866 cell->uncovered_area += 2*fx;
867 cell->covered_height++;
868 return GLITTER_STATUS_SUCCESS;
869 }
870
871 return GLITTER_STATUS_NO_MEMORY;
872 }
873
874 /* Add a subpixel span covering [x1, x2) to the coverage cells. */
875 inline static glitter_status_t
cell_list_add_subspan(struct cell_list * cells,grid_scaled_x_t x1,grid_scaled_x_t x2)876 cell_list_add_subspan(
877 struct cell_list *cells,
878 grid_scaled_x_t x1,
879 grid_scaled_x_t x2)
880 {
881 int ix1, fx1;
882 int ix2, fx2;
883
884 GRID_X_TO_INT_FRAC(x1, ix1, fx1);
885 GRID_X_TO_INT_FRAC(x2, ix2, fx2);
886
887 if (ix1 != ix2) {
888 struct cell_pair p;
889 p = cell_list_find_pair(cells, ix1, ix2);
890 if (likely (p.cell1 != NULL && p.cell2 != NULL)) {
891 p.cell1->uncovered_area += 2*fx1;
892 ++p.cell1->covered_height;
893 p.cell2->uncovered_area -= 2*fx2;
894 --p.cell2->covered_height;
895 return GLITTER_STATUS_SUCCESS;
896 }
897 } else {
898 struct cell *cell = cell_list_find(cells, ix1);
899 if (likely (cell != NULL)) {
900 cell->uncovered_area += 2*(fx1-fx2);
901 return GLITTER_STATUS_SUCCESS;
902 }
903 }
904 return GLITTER_STATUS_NO_MEMORY;
905 }
906
907 /* Adds the analytical coverage of an edge crossing the current pixel
908 * row to the coverage cells and advances the edge's x position to the
909 * following row.
910 *
911 * This function is only called when we know that during this pixel row:
912 *
913 * 1) The relative order of all edges on the active list doesn't
914 * change. In particular, no edges intersect within this row to pixel
915 * precision.
916 *
917 * 2) No new edges start in this row.
918 *
919 * 3) No existing edges end mid-row.
920 *
921 * This function depends on being called with all edges from the
922 * active list in the order they appear on the list (i.e. with
923 * non-decreasing x-coordinate.) */
924 static glitter_status_t
cell_list_render_edge(struct cell_list * cells,struct edge * edge,int sign)925 cell_list_render_edge(
926 struct cell_list *cells,
927 struct edge *edge,
928 int sign)
929 {
930 grid_scaled_y_t y1, y2, dy;
931 grid_scaled_x_t dx;
932 int ix1, ix2;
933 grid_scaled_x_t fx1, fx2;
934
935 struct quorem x1 = edge->x;
936 struct quorem x2 = x1;
937
938 if (! edge->vertical) {
939 x2.quo += edge->dxdy_full.quo;
940 x2.rem += edge->dxdy_full.rem;
941 if (x2.rem >= 0) {
942 ++x2.quo;
943 x2.rem -= edge->dy;
944 }
945
946 edge->x = x2;
947 }
948
949 GRID_X_TO_INT_FRAC(x1.quo, ix1, fx1);
950 GRID_X_TO_INT_FRAC(x2.quo, ix2, fx2);
951
952 /* Edge is entirely within a column? */
953 if (ix1 == ix2) {
954 /* We always know that ix1 is >= the cell list cursor in this
955 * case due to the no-intersections precondition. */
956 struct cell *cell = cell_list_find(cells, ix1);
957 if (unlikely (NULL == cell))
958 return GLITTER_STATUS_NO_MEMORY;
959
960 cell->covered_height += sign*GRID_Y;
961 cell->uncovered_area += sign*(fx1 + fx2)*GRID_Y;
962 return GLITTER_STATUS_SUCCESS;
963 }
964
965 /* Orient the edge left-to-right. */
966 dx = x2.quo - x1.quo;
967 if (dx >= 0) {
968 y1 = 0;
969 y2 = GRID_Y;
970 } else {
971 int tmp;
972 tmp = ix1; ix1 = ix2; ix2 = tmp;
973 tmp = fx1; fx1 = fx2; fx2 = tmp;
974 dx = -dx;
975 sign = -sign;
976 y1 = GRID_Y;
977 y2 = 0;
978 }
979 dy = y2 - y1;
980
981 /* Add coverage for all pixels [ix1,ix2] on this row crossed
982 * by the edge. */
983 {
984 struct cell_pair pair;
985 struct quorem y = floored_divrem((GRID_X - fx1)*dy, dx);
986
987 /* When rendering a previous edge on the active list we may
988 * advance the cell list cursor past the leftmost pixel of the
989 * current edge even though the two edges don't intersect.
990 * e.g. consider two edges going down and rightwards:
991 *
992 * --\_+---\_+-----+-----+----
993 * \_ \_ | |
994 * | \_ | \_ | |
995 * | \_| \_| |
996 * | \_ \_ |
997 * ----+-----+-\---+-\---+----
998 *
999 * The left edge touches cells past the starting cell of the
1000 * right edge. Fortunately such cases are rare.
1001 *
1002 * The rewinding is never necessary if the current edge stays
1003 * within a single column because we've checked before calling
1004 * this function that the active list order won't change. */
1005 cell_list_maybe_rewind(cells, ix1);
1006
1007 pair = cell_list_find_pair(cells, ix1, ix1+1);
1008 if (unlikely (!pair.cell1 || !pair.cell2))
1009 return GLITTER_STATUS_NO_MEMORY;
1010
1011 pair.cell1->uncovered_area += sign*y.quo*(GRID_X + fx1);
1012 pair.cell1->covered_height += sign*y.quo;
1013 y.quo += y1;
1014
1015 if (ix1+1 < ix2) {
1016 struct quorem dydx_full = floored_divrem(GRID_X*dy, dx);
1017 struct cell *cell = pair.cell2;
1018
1019 ++ix1;
1020 do {
1021 grid_scaled_y_t y_skip = dydx_full.quo;
1022 y.rem += dydx_full.rem;
1023 if (y.rem >= dx) {
1024 ++y_skip;
1025 y.rem -= dx;
1026 }
1027
1028 y.quo += y_skip;
1029
1030 y_skip *= sign;
1031 cell->uncovered_area += y_skip*GRID_X;
1032 cell->covered_height += y_skip;
1033
1034 ++ix1;
1035 cell = cell_list_find(cells, ix1);
1036 if (unlikely (NULL == cell))
1037 return GLITTER_STATUS_NO_MEMORY;
1038 } while (ix1 != ix2);
1039
1040 pair.cell2 = cell;
1041 }
1042 pair.cell2->uncovered_area += sign*(y2 - y.quo)*fx2;
1043 pair.cell2->covered_height += sign*(y2 - y.quo);
1044 }
1045
1046 return GLITTER_STATUS_SUCCESS;
1047 }
1048
1049 static void
polygon_init(struct polygon * polygon)1050 polygon_init (struct polygon *polygon)
1051 {
1052 polygon->ymin = polygon->ymax = 0;
1053 polygon->xmin = polygon->xmax = 0;
1054 polygon->y_buckets = polygon->y_buckets_embedded;
1055 pool_init (polygon->edge_pool.base,
1056 8192 - sizeof (struct _pool_chunk),
1057 sizeof (polygon->edge_pool.embedded));
1058 }
1059
1060 static void
polygon_fini(struct polygon * polygon)1061 polygon_fini (struct polygon *polygon)
1062 {
1063 if (polygon->y_buckets != polygon->y_buckets_embedded)
1064 free (polygon->y_buckets);
1065
1066 pool_fini (polygon->edge_pool.base);
1067 }
1068
1069 /* Empties the polygon of all edges. The polygon is then prepared to
1070 * receive new edges and clip them to the vertical range
1071 * [ymin,ymax). */
1072 static glitter_status_t
polygon_reset(struct polygon * polygon,grid_scaled_x_t xmin,grid_scaled_x_t xmax,grid_scaled_y_t ymin,grid_scaled_y_t ymax)1073 polygon_reset (struct polygon *polygon,
1074 grid_scaled_x_t xmin,
1075 grid_scaled_x_t xmax,
1076 grid_scaled_y_t ymin,
1077 grid_scaled_y_t ymax)
1078 {
1079 unsigned h = ymax - ymin;
1080 unsigned num_buckets = EDGE_Y_BUCKET_INDEX(ymax + EDGE_Y_BUCKET_HEIGHT-1,
1081 ymin);
1082
1083 pool_reset(polygon->edge_pool.base);
1084
1085 if (unlikely (h > 0x7FFFFFFFU - EDGE_Y_BUCKET_HEIGHT))
1086 goto bail_no_mem; /* even if you could, you wouldn't want to. */
1087
1088 if (polygon->y_buckets != polygon->y_buckets_embedded)
1089 free (polygon->y_buckets);
1090
1091 polygon->y_buckets = polygon->y_buckets_embedded;
1092 if (num_buckets > ARRAY_LENGTH (polygon->y_buckets_embedded)) {
1093 polygon->y_buckets = _cairo_malloc_ab (num_buckets,
1094 sizeof (struct bucket));
1095 if (unlikely (NULL == polygon->y_buckets))
1096 goto bail_no_mem;
1097 }
1098 memset (polygon->y_buckets, 0, num_buckets * sizeof (struct bucket));
1099
1100 polygon->ymin = ymin;
1101 polygon->ymax = ymax;
1102 polygon->xmin = xmin;
1103 polygon->xmax = xmax;
1104 return GLITTER_STATUS_SUCCESS;
1105
1106 bail_no_mem:
1107 polygon->ymin = 0;
1108 polygon->ymax = 0;
1109 return GLITTER_STATUS_NO_MEMORY;
1110 }
1111
1112 static void
_polygon_insert_edge_into_its_y_bucket(struct polygon * polygon,struct edge * e)1113 _polygon_insert_edge_into_its_y_bucket(
1114 struct polygon *polygon,
1115 struct edge *e)
1116 {
1117 unsigned j = e->ytop - polygon->ymin;
1118 unsigned ix = j / EDGE_Y_BUCKET_HEIGHT;
1119 unsigned offset = j % EDGE_Y_BUCKET_HEIGHT;
1120 struct edge **ptail = &polygon->y_buckets[ix].edges;
1121 e->next = *ptail;
1122 *ptail = e;
1123 polygon->y_buckets[ix].have_inside_edges |= offset;
1124 }
1125
1126 inline static glitter_status_t
polygon_add_edge(struct polygon * polygon,const cairo_edge_t * edge)1127 polygon_add_edge (struct polygon *polygon,
1128 const cairo_edge_t *edge)
1129 {
1130 struct edge *e;
1131 grid_scaled_x_t dx;
1132 grid_scaled_y_t dy;
1133 grid_scaled_y_t ytop, ybot;
1134 grid_scaled_y_t ymin = polygon->ymin;
1135 grid_scaled_y_t ymax = polygon->ymax;
1136
1137 assert (edge->bottom > edge->top);
1138
1139 if (unlikely (edge->top >= ymax || edge->bottom <= ymin))
1140 return GLITTER_STATUS_SUCCESS;
1141
1142 e = pool_alloc (polygon->edge_pool.base, sizeof (struct edge));
1143 if (unlikely (NULL == e))
1144 return GLITTER_STATUS_NO_MEMORY;
1145
1146 dx = edge->line.p2.x - edge->line.p1.x;
1147 dy = edge->line.p2.y - edge->line.p1.y;
1148 e->dy = dy;
1149 e->dir = edge->dir;
1150
1151 ytop = edge->top >= ymin ? edge->top : ymin;
1152 ybot = edge->bottom <= ymax ? edge->bottom : ymax;
1153 e->ytop = ytop;
1154 e->height_left = ybot - ytop;
1155
1156 if (dx == 0) {
1157 e->vertical = TRUE;
1158 e->x.quo = edge->line.p1.x;
1159 e->x.rem = 0;
1160 e->dxdy.quo = 0;
1161 e->dxdy.rem = 0;
1162 e->dxdy_full.quo = 0;
1163 e->dxdy_full.rem = 0;
1164
1165 /* Drop edges to the right of the clip extents. */
1166 if (e->x.quo >= polygon->xmax)
1167 return GLITTER_STATUS_SUCCESS;
1168
1169 /* Offset vertical edges at the left side of the clip extents
1170 * to just shy of the left side. We depend on this when
1171 * checking for possible intersections within the clip
1172 * rectangle. */
1173 if (e->x.quo <= polygon->xmin) {
1174 e->x.quo = polygon->xmin - 1;
1175 }
1176 } else {
1177 e->vertical = FALSE;
1178 e->dxdy = floored_divrem (dx, dy);
1179 if (ytop == edge->line.p1.y) {
1180 e->x.quo = edge->line.p1.x;
1181 e->x.rem = 0;
1182 } else {
1183 e->x = floored_muldivrem (ytop - edge->line.p1.y, dx, dy);
1184 e->x.quo += edge->line.p1.x;
1185 }
1186
1187 if (e->x.quo >= polygon->xmax && e->dxdy.quo >= 0)
1188 return GLITTER_STATUS_SUCCESS;
1189
1190 if (e->height_left >= GRID_Y) {
1191 e->dxdy_full = floored_muldivrem (GRID_Y, dx, dy);
1192 } else {
1193 e->dxdy_full.quo = 0;
1194 e->dxdy_full.rem = 0;
1195 }
1196 }
1197
1198 _polygon_insert_edge_into_its_y_bucket (polygon, e);
1199
1200 e->x.rem -= dy; /* Bias the remainder for faster
1201 * edge advancement. */
1202 return GLITTER_STATUS_SUCCESS;
1203 }
1204
1205 static void
active_list_reset(struct active_list * active)1206 active_list_reset (struct active_list *active)
1207 {
1208 active->head = NULL;
1209 active->min_height = 0;
1210 }
1211
1212 static void
active_list_init(struct active_list * active)1213 active_list_init(struct active_list *active)
1214 {
1215 active_list_reset(active);
1216 }
1217
1218 /*
1219 * Merge two sorted edge lists.
1220 * Input:
1221 * - head_a: The head of the first list.
1222 * - head_b: The head of the second list; head_b cannot be NULL.
1223 * Output:
1224 * Returns the head of the merged list.
1225 *
1226 * Implementation notes:
1227 * To make it fast (in particular, to reduce to an insertion sort whenever
1228 * one of the two input lists only has a single element) we iterate through
1229 * a list until its head becomes greater than the head of the other list,
1230 * then we switch their roles. As soon as one of the two lists is empty, we
1231 * just attach the other one to the current list and exit.
1232 * Writes to memory are only needed to "switch" lists (as it also requires
1233 * attaching to the output list the list which we will be iterating next) and
1234 * to attach the last non-empty list.
1235 */
1236 static struct edge *
merge_sorted_edges(struct edge * head_a,struct edge * head_b)1237 merge_sorted_edges (struct edge *head_a, struct edge *head_b)
1238 {
1239 struct edge *head, **next;
1240
1241 head = head_a;
1242 next = &head;
1243
1244 while (1) {
1245 while (head_a != NULL && head_a->x.quo <= head_b->x.quo) {
1246 next = &head_a->next;
1247 head_a = head_a->next;
1248 }
1249
1250 *next = head_b;
1251 if (head_a == NULL)
1252 return head;
1253
1254 while (head_b != NULL && head_b->x.quo <= head_a->x.quo) {
1255 next = &head_b->next;
1256 head_b = head_b->next;
1257 }
1258
1259 *next = head_a;
1260 if (head_b == NULL)
1261 return head;
1262 }
1263 }
1264
1265 /*
1266 * Sort (part of) a list.
1267 * Input:
1268 * - list: The list to be sorted; list cannot be NULL.
1269 * - limit: Recursion limit.
1270 * Output:
1271 * - head_out: The head of the sorted list containing the first 2^(level+1) elements of the
1272 * input list; if the input list has fewer elements, head_out be a sorted list
1273 * containing all the elements of the input list.
1274 * Returns the head of the list of unprocessed elements (NULL if the sorted list contains
1275 * all the elements of the input list).
1276 *
1277 * Implementation notes:
1278 * Special case single element list, unroll/inline the sorting of the first two elements.
1279 * Some tail recursion is used since we iterate on the bottom-up solution of the problem
1280 * (we start with a small sorted list and keep merging other lists of the same size to it).
1281 */
1282 static struct edge *
sort_edges(struct edge * list,unsigned int level,struct edge ** head_out)1283 sort_edges (struct edge *list,
1284 unsigned int level,
1285 struct edge **head_out)
1286 {
1287 struct edge *head_other, *remaining;
1288 unsigned int i;
1289
1290 head_other = list->next;
1291
1292 /* Single element list -> return */
1293 if (head_other == NULL) {
1294 *head_out = list;
1295 return NULL;
1296 }
1297
1298 /* Unroll the first iteration of the following loop (halves the number of calls to merge_sorted_edges):
1299 * - Initialize remaining to be the list containing the elements after the second in the input list.
1300 * - Initialize *head_out to be the sorted list containing the first two element.
1301 */
1302 remaining = head_other->next;
1303 if (list->x.quo <= head_other->x.quo) {
1304 *head_out = list;
1305 /* list->next = head_other; */ /* The input list is already like this. */
1306 head_other->next = NULL;
1307 } else {
1308 *head_out = head_other;
1309 head_other->next = list;
1310 list->next = NULL;
1311 }
1312
1313 for (i = 0; i < level && remaining; i++) {
1314 /* Extract a sorted list of the same size as *head_out
1315 * (2^(i+1) elements) from the list of remaining elements. */
1316 remaining = sort_edges (remaining, i, &head_other);
1317 *head_out = merge_sorted_edges (*head_out, head_other);
1318 }
1319
1320 /* *head_out now contains (at most) 2^(level+1) elements. */
1321
1322 return remaining;
1323 }
1324
1325 /* Test if the edges on the active list can be safely advanced by a
1326 * full row without intersections or any edges ending. */
1327 inline static int
active_list_can_step_full_row(struct active_list * active,grid_scaled_x_t xmin)1328 active_list_can_step_full_row (struct active_list *active,
1329 grid_scaled_x_t xmin)
1330 {
1331 const struct edge *e;
1332 grid_scaled_x_t prev_x = INT_MIN;
1333
1334 /* Recomputes the minimum height of all edges on the active
1335 * list if we have been dropping edges. */
1336 if (active->min_height <= 0) {
1337 int min_height = INT_MAX;
1338
1339 e = active->head;
1340 while (NULL != e) {
1341 if (e->height_left < min_height)
1342 min_height = e->height_left;
1343 e = e->next;
1344 }
1345
1346 active->min_height = min_height;
1347 }
1348
1349 if (active->min_height < GRID_Y)
1350 return 0;
1351
1352 /* Check for intersections as no edges end during the next row. */
1353 e = active->head;
1354 while (NULL != e) {
1355 struct quorem x = e->x;
1356
1357 if (! e->vertical) {
1358 x.quo += e->dxdy_full.quo;
1359 x.rem += e->dxdy_full.rem;
1360 if (x.rem >= 0)
1361 ++x.quo;
1362 }
1363
1364 /* There's may be an intersection if the edge sort order might
1365 * change. */
1366 if (x.quo <= prev_x) {
1367 /* Ignore intersections to the left of the clip extents.
1368 * This assumes that all vertical edges on or at the left
1369 * side of the clip rectangle have been shifted slightly
1370 * to the left in polygon_add_edge(). */
1371 if (prev_x >= xmin || x.quo >= xmin || e->x.quo >= xmin)
1372 return 0;
1373 }
1374 else {
1375 prev_x = x.quo;
1376 }
1377 e = e->next;
1378 }
1379
1380 return 1;
1381 }
1382
1383 /* Merges edges on the given subpixel row from the polygon to the
1384 * active_list. */
1385 inline static void
active_list_merge_edges_from_polygon(struct active_list * active,grid_scaled_y_t y,struct polygon * polygon)1386 active_list_merge_edges_from_polygon(
1387 struct active_list *active,
1388 grid_scaled_y_t y,
1389 struct polygon *polygon)
1390 {
1391 /* Split off the edges on the current subrow and merge them into
1392 * the active list. */
1393 unsigned ix = EDGE_Y_BUCKET_INDEX(y, polygon->ymin);
1394 int min_height = active->min_height;
1395 struct edge *subrow_edges = NULL;
1396 struct edge **ptail = &polygon->y_buckets[ix].edges;
1397
1398 while (1) {
1399 struct edge *tail = *ptail;
1400 if (NULL == tail) break;
1401
1402 if (y == tail->ytop) {
1403 *ptail = tail->next;
1404 tail->next = subrow_edges;
1405 subrow_edges = tail;
1406 if (tail->height_left < min_height)
1407 min_height = tail->height_left;
1408 } else {
1409 ptail = &tail->next;
1410 }
1411 }
1412 if (subrow_edges) {
1413 sort_edges (subrow_edges, UINT_MAX, &subrow_edges);
1414 active->head = merge_sorted_edges (active->head, subrow_edges);
1415 active->min_height = min_height;
1416 }
1417 }
1418
1419 /* Advance the edges on the active list by one subsample row by
1420 * updating their x positions. Drop edges from the list that end. */
1421 inline static void
active_list_substep_edges(struct active_list * active)1422 active_list_substep_edges(
1423 struct active_list *active)
1424 {
1425 struct edge **cursor = &active->head;
1426 grid_scaled_x_t prev_x = INT_MIN;
1427 struct edge *unsorted = NULL;
1428
1429 while (1) {
1430 struct edge *edge;
1431
1432 UNROLL3({
1433 edge = *cursor;
1434 if (NULL == edge)
1435 break;
1436
1437 if (0 != --edge->height_left) {
1438 edge->x.quo += edge->dxdy.quo;
1439 edge->x.rem += edge->dxdy.rem;
1440 if (edge->x.rem >= 0) {
1441 ++edge->x.quo;
1442 edge->x.rem -= edge->dy;
1443 }
1444
1445 if (edge->x.quo < prev_x) {
1446 *cursor = edge->next;
1447 edge->next = unsorted;
1448 unsorted = edge;
1449 } else {
1450 prev_x = edge->x.quo;
1451 cursor = &edge->next;
1452 }
1453
1454 } else {
1455 *cursor = edge->next;
1456 }
1457 });
1458 }
1459
1460 if (unsorted) {
1461 sort_edges (unsorted, UINT_MAX, &unsorted);
1462 active->head = merge_sorted_edges (active->head, unsorted);
1463 }
1464 }
1465
1466 inline static glitter_status_t
apply_nonzero_fill_rule_for_subrow(struct active_list * active,struct cell_list * coverages)1467 apply_nonzero_fill_rule_for_subrow (struct active_list *active,
1468 struct cell_list *coverages)
1469 {
1470 struct edge *edge = active->head;
1471 int winding = 0;
1472 int xstart;
1473 int xend;
1474 int status;
1475
1476 cell_list_rewind (coverages);
1477
1478 while (NULL != edge) {
1479 xstart = edge->x.quo;
1480 winding = edge->dir;
1481 while (1) {
1482 edge = edge->next;
1483 if (NULL == edge)
1484 return cell_list_add_unbounded_subspan (coverages, xstart);
1485
1486 winding += edge->dir;
1487 if (0 == winding) {
1488 if (edge->next == NULL || edge->next->x.quo != edge->x.quo)
1489 break;
1490 }
1491 }
1492
1493 xend = edge->x.quo;
1494 status = cell_list_add_subspan (coverages, xstart, xend);
1495 if (unlikely (status))
1496 return status;
1497
1498 edge = edge->next;
1499 }
1500
1501 return GLITTER_STATUS_SUCCESS;
1502 }
1503
1504 static glitter_status_t
apply_evenodd_fill_rule_for_subrow(struct active_list * active,struct cell_list * coverages)1505 apply_evenodd_fill_rule_for_subrow (struct active_list *active,
1506 struct cell_list *coverages)
1507 {
1508 struct edge *edge = active->head;
1509 int xstart;
1510 int xend;
1511 int status;
1512
1513 cell_list_rewind (coverages);
1514
1515 while (NULL != edge) {
1516 xstart = edge->x.quo;
1517
1518 while (1) {
1519 edge = edge->next;
1520 if (NULL == edge)
1521 return cell_list_add_unbounded_subspan (coverages, xstart);
1522
1523 if (edge->next == NULL || edge->next->x.quo != edge->x.quo)
1524 break;
1525
1526 edge = edge->next;
1527 }
1528
1529 xend = edge->x.quo;
1530 status = cell_list_add_subspan (coverages, xstart, xend);
1531 if (unlikely (status))
1532 return status;
1533
1534 edge = edge->next;
1535 }
1536
1537 return GLITTER_STATUS_SUCCESS;
1538 }
1539
1540 static glitter_status_t
apply_nonzero_fill_rule_and_step_edges(struct active_list * active,struct cell_list * coverages)1541 apply_nonzero_fill_rule_and_step_edges (struct active_list *active,
1542 struct cell_list *coverages)
1543 {
1544 struct edge **cursor = &active->head;
1545 struct edge *left_edge;
1546 int status;
1547
1548 left_edge = *cursor;
1549 while (NULL != left_edge) {
1550 struct edge *right_edge;
1551 int winding = left_edge->dir;
1552
1553 left_edge->height_left -= GRID_Y;
1554 if (left_edge->height_left)
1555 cursor = &left_edge->next;
1556 else
1557 *cursor = left_edge->next;
1558
1559 while (1) {
1560 right_edge = *cursor;
1561 if (NULL == right_edge)
1562 return cell_list_render_edge (coverages, left_edge, +1);
1563
1564 right_edge->height_left -= GRID_Y;
1565 if (right_edge->height_left)
1566 cursor = &right_edge->next;
1567 else
1568 *cursor = right_edge->next;
1569
1570 winding += right_edge->dir;
1571 if (0 == winding) {
1572 if (right_edge->next == NULL ||
1573 right_edge->next->x.quo != right_edge->x.quo)
1574 {
1575 break;
1576 }
1577 }
1578
1579 if (! right_edge->vertical) {
1580 right_edge->x.quo += right_edge->dxdy_full.quo;
1581 right_edge->x.rem += right_edge->dxdy_full.rem;
1582 if (right_edge->x.rem >= 0) {
1583 ++right_edge->x.quo;
1584 right_edge->x.rem -= right_edge->dy;
1585 }
1586 }
1587 }
1588
1589 status = cell_list_render_edge (coverages, left_edge, +1);
1590 if (unlikely (status))
1591 return status;
1592
1593 status = cell_list_render_edge (coverages, right_edge, -1);
1594 if (unlikely (status))
1595 return status;
1596
1597 left_edge = *cursor;
1598 }
1599
1600 return GLITTER_STATUS_SUCCESS;
1601 }
1602
1603 static glitter_status_t
apply_evenodd_fill_rule_and_step_edges(struct active_list * active,struct cell_list * coverages)1604 apply_evenodd_fill_rule_and_step_edges (struct active_list *active,
1605 struct cell_list *coverages)
1606 {
1607 struct edge **cursor = &active->head;
1608 struct edge *left_edge;
1609 int status;
1610
1611 left_edge = *cursor;
1612 while (NULL != left_edge) {
1613 struct edge *right_edge;
1614 int winding = left_edge->dir;
1615
1616 left_edge->height_left -= GRID_Y;
1617 if (left_edge->height_left)
1618 cursor = &left_edge->next;
1619 else
1620 *cursor = left_edge->next;
1621
1622 while (1) {
1623 right_edge = *cursor;
1624 if (NULL == right_edge)
1625 return cell_list_render_edge (coverages, left_edge, +1);
1626
1627 right_edge->height_left -= GRID_Y;
1628 if (right_edge->height_left)
1629 cursor = &right_edge->next;
1630 else
1631 *cursor = right_edge->next;
1632
1633 winding += right_edge->dir;
1634 if ((winding & 1) == 0) {
1635 if (right_edge->next == NULL ||
1636 right_edge->next->x.quo != right_edge->x.quo)
1637 {
1638 break;
1639 }
1640 }
1641
1642 if (! right_edge->vertical) {
1643 right_edge->x.quo += right_edge->dxdy_full.quo;
1644 right_edge->x.rem += right_edge->dxdy_full.rem;
1645 if (right_edge->x.rem >= 0) {
1646 ++right_edge->x.quo;
1647 right_edge->x.rem -= right_edge->dy;
1648 }
1649 }
1650 }
1651
1652 status = cell_list_render_edge (coverages, left_edge, +1);
1653 if (unlikely (status))
1654 return status;
1655
1656 status = cell_list_render_edge (coverages, right_edge, -1);
1657 if (unlikely (status))
1658 return status;
1659
1660 left_edge = *cursor;
1661 }
1662
1663 return GLITTER_STATUS_SUCCESS;
1664 }
1665
1666 /* If the user hasn't configured a coverage blitter, use a default one
1667 * that blits spans directly to an A8 raster. */
1668 #ifndef GLITTER_BLIT_COVERAGES
1669
1670 inline static void
blit_span(unsigned char * row_pixels,int x,unsigned len,grid_area_t coverage)1671 blit_span(
1672 unsigned char *row_pixels,
1673 int x, unsigned len,
1674 grid_area_t coverage)
1675 {
1676 int alpha = GRID_AREA_TO_ALPHA(coverage);
1677 if (1 == len) {
1678 row_pixels[x] = alpha;
1679 }
1680 else {
1681 memset(row_pixels + x, alpha, len);
1682 }
1683 }
1684
1685 #define GLITTER_BLIT_COVERAGES(coverages, y, height, xmin, xmax) \
1686 do { \
1687 int __y = y; \
1688 int __h = height; \
1689 do { \
1690 blit_cells(coverages, raster_pixels + (__y)*raster_stride, xmin, xmax); \
1691 } while (--__h); \
1692 } while (0)
1693
1694 static void
blit_cells(struct cell_list * cells,unsigned char * row_pixels,int xmin,int xmax)1695 blit_cells(
1696 struct cell_list *cells,
1697 unsigned char *row_pixels,
1698 int xmin, int xmax)
1699 {
1700 struct cell *cell = cells->head;
1701 int prev_x = xmin;
1702 int coverage = 0;
1703 if (NULL == cell)
1704 return;
1705
1706 while (NULL != cell && cell->x < xmin) {
1707 coverage += cell->covered_height;
1708 cell = cell->next;
1709 }
1710 coverage *= GRID_X*2;
1711
1712 for (; NULL != cell; cell = cell->next) {
1713 int x = cell->x;
1714 int area;
1715 if (x >= xmax)
1716 break;
1717 if (x > prev_x && 0 != coverage) {
1718 blit_span(row_pixels, prev_x, x - prev_x, coverage);
1719 }
1720
1721 coverage += cell->covered_height * GRID_X*2;
1722 area = coverage - cell->uncovered_area;
1723 if (area) {
1724 blit_span(row_pixels, x, 1, area);
1725 }
1726 prev_x = x+1;
1727 }
1728
1729 if (0 != coverage && prev_x < xmax) {
1730 blit_span(row_pixels, prev_x, xmax - prev_x, coverage);
1731 }
1732 }
1733 #endif /* GLITTER_BLIT_COVERAGES */
1734
1735 static void
_glitter_scan_converter_init(glitter_scan_converter_t * converter)1736 _glitter_scan_converter_init(glitter_scan_converter_t *converter)
1737 {
1738 polygon_init(converter->polygon);
1739 active_list_init(converter->active);
1740 cell_list_init(converter->coverages);
1741 converter->xmin=0;
1742 converter->ymin=0;
1743 converter->xmax=0;
1744 converter->ymax=0;
1745 }
1746
1747 static void
_glitter_scan_converter_fini(glitter_scan_converter_t * converter)1748 _glitter_scan_converter_fini(glitter_scan_converter_t *converter)
1749 {
1750 polygon_fini(converter->polygon);
1751 cell_list_fini(converter->coverages);
1752 converter->xmin=0;
1753 converter->ymin=0;
1754 converter->xmax=0;
1755 converter->ymax=0;
1756 }
1757
1758 static grid_scaled_t
int_to_grid_scaled(int i,int scale)1759 int_to_grid_scaled(int i, int scale)
1760 {
1761 /* Clamp to max/min representable scaled number. */
1762 if (i >= 0) {
1763 if (i >= INT_MAX/scale)
1764 i = INT_MAX/scale;
1765 }
1766 else {
1767 if (i <= INT_MIN/scale)
1768 i = INT_MIN/scale;
1769 }
1770 return i*scale;
1771 }
1772
1773 #define int_to_grid_scaled_x(x) int_to_grid_scaled((x), GRID_X)
1774 #define int_to_grid_scaled_y(x) int_to_grid_scaled((x), GRID_Y)
1775
1776 I glitter_status_t
glitter_scan_converter_reset(glitter_scan_converter_t * converter,int xmin,int ymin,int xmax,int ymax)1777 glitter_scan_converter_reset(
1778 glitter_scan_converter_t *converter,
1779 int xmin, int ymin,
1780 int xmax, int ymax)
1781 {
1782 glitter_status_t status;
1783
1784 converter->xmin = 0; converter->xmax = 0;
1785 converter->ymin = 0; converter->ymax = 0;
1786
1787 xmin = int_to_grid_scaled_x(xmin);
1788 ymin = int_to_grid_scaled_y(ymin);
1789 xmax = int_to_grid_scaled_x(xmax);
1790 ymax = int_to_grid_scaled_y(ymax);
1791
1792 active_list_reset(converter->active);
1793 cell_list_reset(converter->coverages);
1794 status = polygon_reset(converter->polygon, xmin, xmax, ymin, ymax);
1795 if (status)
1796 return status;
1797
1798 converter->xmin = xmin;
1799 converter->xmax = xmax;
1800 converter->ymin = ymin;
1801 converter->ymax = ymax;
1802 return GLITTER_STATUS_SUCCESS;
1803 }
1804
1805 /* INPUT_TO_GRID_X/Y (in_coord, out_grid_scaled, grid_scale)
1806 * These macros convert an input coordinate in the client's
1807 * device space to the rasterisation grid.
1808 */
1809 /* Gah.. this bit of ugly defines INPUT_TO_GRID_X/Y so as to use
1810 * shifts if possible, and something saneish if not.
1811 */
1812 #if !defined(INPUT_TO_GRID_Y) && defined(GRID_Y_BITS) && GRID_Y_BITS <= GLITTER_INPUT_BITS
1813 # define INPUT_TO_GRID_Y(in, out) (out) = (in) >> (GLITTER_INPUT_BITS - GRID_Y_BITS)
1814 #else
1815 # define INPUT_TO_GRID_Y(in, out) INPUT_TO_GRID_general(in, out, GRID_Y)
1816 #endif
1817
1818 #if !defined(INPUT_TO_GRID_X) && defined(GRID_X_BITS) && GRID_X_BITS <= GLITTER_INPUT_BITS
1819 # define INPUT_TO_GRID_X(in, out) (out) = (in) >> (GLITTER_INPUT_BITS - GRID_X_BITS)
1820 #else
1821 # define INPUT_TO_GRID_X(in, out) INPUT_TO_GRID_general(in, out, GRID_X)
1822 #endif
1823
1824 #define INPUT_TO_GRID_general(in, out, grid_scale) do { \
1825 long long tmp__ = (long long)(grid_scale) * (in); \
1826 tmp__ >>= GLITTER_INPUT_BITS; \
1827 (out) = tmp__; \
1828 } while (0)
1829
1830 I glitter_status_t
glitter_scan_converter_add_edge(glitter_scan_converter_t * converter,const cairo_edge_t * edge)1831 glitter_scan_converter_add_edge (glitter_scan_converter_t *converter,
1832 const cairo_edge_t *edge)
1833 {
1834 cairo_edge_t e;
1835
1836 INPUT_TO_GRID_Y (edge->top, e.top);
1837 INPUT_TO_GRID_Y (edge->bottom, e.bottom);
1838 if (e.top >= e.bottom)
1839 return GLITTER_STATUS_SUCCESS;
1840
1841 /* XXX: possible overflows if GRID_X/Y > 2**GLITTER_INPUT_BITS */
1842 INPUT_TO_GRID_Y (edge->line.p1.y, e.line.p1.y);
1843 INPUT_TO_GRID_Y (edge->line.p2.y, e.line.p2.y);
1844 if (e.line.p1.y == e.line.p2.y)
1845 return GLITTER_STATUS_SUCCESS;
1846
1847 INPUT_TO_GRID_X (edge->line.p1.x, e.line.p1.x);
1848 INPUT_TO_GRID_X (edge->line.p2.x, e.line.p2.x);
1849
1850 e.dir = edge->dir;
1851
1852 return polygon_add_edge (converter->polygon, &e);
1853 }
1854
1855 #ifndef GLITTER_BLIT_COVERAGES_BEGIN
1856 # define GLITTER_BLIT_COVERAGES_BEGIN
1857 #endif
1858
1859 #ifndef GLITTER_BLIT_COVERAGES_END
1860 # define GLITTER_BLIT_COVERAGES_END
1861 #endif
1862
1863 #ifndef GLITTER_BLIT_COVERAGES_EMPTY
1864 # define GLITTER_BLIT_COVERAGES_EMPTY(y0, y1, xmin, xmax)
1865 #endif
1866
1867 static cairo_bool_t
active_list_is_vertical(struct active_list * active)1868 active_list_is_vertical (struct active_list *active)
1869 {
1870 struct edge *e;
1871
1872 for (e = active->head; e != NULL; e = e->next) {
1873 if (! e->vertical)
1874 return FALSE;
1875 }
1876
1877 return TRUE;
1878 }
1879
1880 static void
step_edges(struct active_list * active,int count)1881 step_edges (struct active_list *active, int count)
1882 {
1883 struct edge **cursor = &active->head;
1884 struct edge *edge;
1885
1886 for (edge = *cursor; edge != NULL; edge = *cursor) {
1887 edge->height_left -= GRID_Y * count;
1888 if (edge->height_left)
1889 cursor = &edge->next;
1890 else
1891 *cursor = edge->next;
1892 }
1893 }
1894
1895 I glitter_status_t
glitter_scan_converter_render(glitter_scan_converter_t * converter,int nonzero_fill,GLITTER_BLIT_COVERAGES_ARGS)1896 glitter_scan_converter_render(
1897 glitter_scan_converter_t *converter,
1898 int nonzero_fill,
1899 GLITTER_BLIT_COVERAGES_ARGS)
1900 {
1901 int i, j;
1902 int ymax_i = converter->ymax / GRID_Y;
1903 int ymin_i = converter->ymin / GRID_Y;
1904 int xmin_i, xmax_i;
1905 grid_scaled_x_t xmin = converter->xmin;
1906 int h = ymax_i - ymin_i;
1907 struct polygon *polygon = converter->polygon;
1908 struct cell_list *coverages = converter->coverages;
1909 struct active_list *active = converter->active;
1910
1911 xmin_i = converter->xmin / GRID_X;
1912 xmax_i = converter->xmax / GRID_X;
1913 if (xmin_i >= xmax_i)
1914 return GLITTER_STATUS_SUCCESS;
1915
1916 /* Let the coverage blitter initialise itself. */
1917 GLITTER_BLIT_COVERAGES_BEGIN;
1918
1919 /* Render each pixel row. */
1920 for (i = 0; i < h; i = j) {
1921 int do_full_step = 0;
1922 glitter_status_t status = 0;
1923
1924 j = i + 1;
1925
1926 /* Determine if we can ignore this row or use the full pixel
1927 * stepper. */
1928 if (polygon->y_buckets[i].edges == NULL) {
1929 if (! active->head) {
1930 for (; j < h && ! polygon->y_buckets[j].edges; j++)
1931 ;
1932 GLITTER_BLIT_COVERAGES_EMPTY (i+ymin_i, j-i, xmin_i, xmax_i);
1933 continue;
1934 }
1935 do_full_step = active_list_can_step_full_row (active, xmin);
1936 }
1937 else if (! polygon->y_buckets[i].have_inside_edges) {
1938 grid_scaled_y_t y = (i+ymin_i)*GRID_Y;
1939 active_list_merge_edges_from_polygon (active, y, polygon);
1940 do_full_step = active_list_can_step_full_row (active, xmin);
1941 }
1942
1943 if (do_full_step) {
1944 /* Step by a full pixel row's worth. */
1945 if (nonzero_fill) {
1946 status = apply_nonzero_fill_rule_and_step_edges (active,
1947 coverages);
1948 } else {
1949 status = apply_evenodd_fill_rule_and_step_edges (active,
1950 coverages);
1951 }
1952
1953 if (active_list_is_vertical (active)) {
1954 while (j < h &&
1955 polygon->y_buckets[j].edges == NULL &&
1956 active->min_height >= 2*GRID_Y)
1957 {
1958 active->min_height -= GRID_Y;
1959 j++;
1960 }
1961 if (j != i + 1)
1962 step_edges (active, j - (i + 1));
1963 }
1964 } else {
1965 /* Supersample this row. */
1966 grid_scaled_y_t suby;
1967 for (suby = 0; suby < GRID_Y; suby++) {
1968 grid_scaled_y_t y = (i+ymin_i)*GRID_Y + suby;
1969
1970 active_list_merge_edges_from_polygon (active, y, polygon);
1971
1972 if (nonzero_fill) {
1973 status |= apply_nonzero_fill_rule_for_subrow (active,
1974 coverages);
1975 } else {
1976 status |= apply_evenodd_fill_rule_for_subrow (active,
1977 coverages);
1978 }
1979
1980 active_list_substep_edges(active);
1981 }
1982 }
1983
1984 if (unlikely (status))
1985 return status;
1986
1987 GLITTER_BLIT_COVERAGES(coverages, i+ymin_i, j-i, xmin_i, xmax_i);
1988 cell_list_reset (coverages);
1989
1990 if (! active->head)
1991 active->min_height = INT_MAX;
1992 else
1993 active->min_height -= GRID_Y;
1994 }
1995
1996 /* Clean up the coverage blitter. */
1997 GLITTER_BLIT_COVERAGES_END;
1998
1999 return GLITTER_STATUS_SUCCESS;
2000 }
2001
2002 /*-------------------------------------------------------------------------
2003 * cairo specific implementation: the coverage blitter and
2004 * scan converter subclass. */
2005
2006 static glitter_status_t
blit_with_span_renderer(struct cell_list * cells,cairo_span_renderer_t * renderer,struct pool * span_pool,int y,int height,int xmin,int xmax)2007 blit_with_span_renderer (struct cell_list *cells,
2008 cairo_span_renderer_t *renderer,
2009 struct pool *span_pool,
2010 int y, int height,
2011 int xmin, int xmax)
2012 {
2013 struct cell *cell = cells->head;
2014 int prev_x = xmin;
2015 int cover = 0;
2016 cairo_half_open_span_t *spans;
2017 unsigned num_spans;
2018
2019 if (cell == NULL)
2020 return blit_empty_with_span_renderer (renderer, y, height);
2021
2022 /* Skip cells to the left of the clip region. */
2023 while (cell != NULL && cell->x < xmin) {
2024 cover += cell->covered_height;
2025 cell = cell->next;
2026 }
2027 cover *= GRID_X*2;
2028
2029 /* Count number of cells remaining. */
2030 {
2031 struct cell *next = cell;
2032 num_spans = 1;
2033 while (next != NULL) {
2034 next = next->next;
2035 ++num_spans;
2036 }
2037 num_spans = 2*num_spans;
2038 }
2039
2040 /* Allocate enough spans for the row. */
2041 pool_reset (span_pool);
2042 spans = pool_alloc (span_pool, sizeof(spans[0])*num_spans);
2043 if (unlikely (spans == NULL))
2044 return GLITTER_STATUS_NO_MEMORY;
2045
2046 num_spans = 0;
2047
2048 /* Form the spans from the coverages and areas. */
2049 for (; cell != NULL; cell = cell->next) {
2050 int x = cell->x;
2051 int area;
2052
2053 if (x >= xmax)
2054 break;
2055
2056 if (x > prev_x) {
2057 spans[num_spans].x = prev_x;
2058 spans[num_spans].coverage = GRID_AREA_TO_ALPHA (cover);
2059 ++num_spans;
2060 }
2061
2062 cover += cell->covered_height*GRID_X*2;
2063 area = cover - cell->uncovered_area;
2064
2065 spans[num_spans].x = x;
2066 spans[num_spans].coverage = GRID_AREA_TO_ALPHA (area);
2067 ++num_spans;
2068
2069 prev_x = x+1;
2070 }
2071
2072 if (prev_x <= xmax) {
2073 spans[num_spans].x = prev_x;
2074 spans[num_spans].coverage = GRID_AREA_TO_ALPHA (cover);
2075 ++num_spans;
2076 }
2077
2078 if (prev_x < xmax && cover) {
2079 spans[num_spans].x = xmax;
2080 spans[num_spans].coverage = 0;
2081 ++num_spans;
2082 }
2083
2084 /* Dump them into the renderer. */
2085 return renderer->render_rows (renderer, y, height, spans, num_spans);
2086 }
2087
2088 static glitter_status_t
blit_empty_with_span_renderer(cairo_span_renderer_t * renderer,int y,int height)2089 blit_empty_with_span_renderer (cairo_span_renderer_t *renderer, int y, int height)
2090 {
2091 return renderer->render_rows (renderer, y, height, NULL, 0);
2092 }
2093
2094 struct _cairo_tor_scan_converter {
2095 cairo_scan_converter_t base;
2096
2097 glitter_scan_converter_t converter[1];
2098 cairo_fill_rule_t fill_rule;
2099
2100 struct {
2101 struct pool base[1];
2102 cairo_half_open_span_t embedded[32];
2103 } span_pool;
2104 };
2105
2106 typedef struct _cairo_tor_scan_converter cairo_tor_scan_converter_t;
2107
2108 static void
_cairo_tor_scan_converter_destroy(void * converter)2109 _cairo_tor_scan_converter_destroy (void *converter)
2110 {
2111 cairo_tor_scan_converter_t *self = converter;
2112 if (self == NULL) {
2113 return;
2114 }
2115 _glitter_scan_converter_fini (self->converter);
2116 pool_fini (self->span_pool.base);
2117 free(self);
2118 }
2119
2120 static cairo_status_t
_cairo_tor_scan_converter_add_edge(void * converter,const cairo_point_t * p1,const cairo_point_t * p2,int top,int bottom,int dir)2121 _cairo_tor_scan_converter_add_edge (void *converter,
2122 const cairo_point_t *p1,
2123 const cairo_point_t *p2,
2124 int top, int bottom,
2125 int dir)
2126 {
2127 cairo_tor_scan_converter_t *self = converter;
2128 cairo_status_t status;
2129 cairo_edge_t edge;
2130
2131 edge.line.p1 = *p1;
2132 edge.line.p2 = *p2;
2133 edge.top = top;
2134 edge.bottom = bottom;
2135 edge.dir = dir;
2136
2137 status = glitter_scan_converter_add_edge (self->converter, &edge);
2138 if (unlikely (status))
2139 return _cairo_scan_converter_set_error (self, _cairo_error (status));
2140
2141 return CAIRO_STATUS_SUCCESS;
2142 }
2143
2144 static cairo_status_t
_cairo_tor_scan_converter_add_polygon(void * converter,const cairo_polygon_t * polygon)2145 _cairo_tor_scan_converter_add_polygon (void *converter,
2146 const cairo_polygon_t *polygon)
2147 {
2148 cairo_tor_scan_converter_t *self = converter;
2149 cairo_status_t status;
2150 int i;
2151
2152 for (i = 0; i < polygon->num_edges; i++) {
2153 status = glitter_scan_converter_add_edge (self->converter,
2154 &polygon->edges[i]);
2155 if (unlikely (status)) {
2156 return _cairo_scan_converter_set_error (self,
2157 _cairo_error (status));
2158 }
2159 }
2160
2161 return CAIRO_STATUS_SUCCESS;
2162 }
2163
2164 static cairo_status_t
_cairo_tor_scan_converter_generate(void * converter,cairo_span_renderer_t * renderer)2165 _cairo_tor_scan_converter_generate (void *converter,
2166 cairo_span_renderer_t *renderer)
2167 {
2168 cairo_tor_scan_converter_t *self = converter;
2169 cairo_status_t status;
2170
2171 status = glitter_scan_converter_render (self->converter,
2172 self->fill_rule == CAIRO_FILL_RULE_WINDING,
2173 renderer,
2174 self->span_pool.base);
2175 if (unlikely (status))
2176 return _cairo_scan_converter_set_error (self, _cairo_error (status));
2177
2178 return CAIRO_STATUS_SUCCESS;
2179 }
2180
2181 cairo_scan_converter_t *
_cairo_tor_scan_converter_create(int xmin,int ymin,int xmax,int ymax,cairo_fill_rule_t fill_rule)2182 _cairo_tor_scan_converter_create (int xmin,
2183 int ymin,
2184 int xmax,
2185 int ymax,
2186 cairo_fill_rule_t fill_rule)
2187 {
2188 cairo_tor_scan_converter_t *self;
2189 cairo_status_t status;
2190
2191 self = calloc (1, sizeof(struct _cairo_tor_scan_converter));
2192 if (unlikely (self == NULL)) {
2193 status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
2194 goto bail_nomem;
2195 }
2196
2197 self->base.destroy = _cairo_tor_scan_converter_destroy;
2198 self->base.add_edge = _cairo_tor_scan_converter_add_edge;
2199 self->base.add_polygon = _cairo_tor_scan_converter_add_polygon;
2200 self->base.generate = _cairo_tor_scan_converter_generate;
2201
2202 pool_init (self->span_pool.base,
2203 250 * sizeof(self->span_pool.embedded[0]),
2204 sizeof(self->span_pool.embedded));
2205
2206 _glitter_scan_converter_init (self->converter);
2207 status = glitter_scan_converter_reset (self->converter,
2208 xmin, ymin, xmax, ymax);
2209 if (unlikely (status))
2210 goto bail;
2211
2212 self->fill_rule = fill_rule;
2213
2214 return &self->base;
2215
2216 bail:
2217 self->base.destroy(&self->base);
2218 bail_nomem:
2219 return _cairo_scan_converter_create_in_error (status);
2220 }
2221