Coverage Report

Created: 2026-11-30 06:27

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cairo/src/cairo-tor-scan-converter.c
Line
Count
Source
1
/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2
/* glitter-paths - polygon scan converter
3
 *
4
 * Copyright (c) 2008  M Joonas Pihlaja
5
 * Copyright (c) 2007  David Turner
6
 *
7
 * Permission is hereby granted, free of charge, to any person
8
 * obtaining a copy of this software and associated documentation
9
 * files (the "Software"), to deal in the Software without
10
 * restriction, including without limitation the rights to use,
11
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
12
 * copies of the Software, and to permit persons to whom the
13
 * Software is furnished to do so, subject to the following
14
 * conditions:
15
 *
16
 * The above copyright notice and this permission notice shall be
17
 * included in all copies or substantial portions of the Software.
18
 *
19
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
21
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
23
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
24
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
26
 * OTHER DEALINGS IN THE SOFTWARE.
27
 */
28
/* This is the Glitter paths scan converter incorporated into cairo.
29
 * The source is from commit 734c53237a867a773640bd5b64816249fa1730f8
30
 * of
31
 *
32
 *   https://gitweb.freedesktop.org/?p=users/joonas/glitter-paths
33
 */
34
/* Glitter-paths is a stand alone polygon rasteriser derived from
35
 * David Turner's reimplementation of Tor Anderssons's 15x17
36
 * supersampling rasteriser from the Apparition graphics library.  The
37
 * main new feature here is cheaply choosing per-scan line between
38
 * doing fully analytical coverage computation for an entire row at a
39
 * time vs. using a supersampling approach.
40
 *
41
 * David Turner's code can be found at
42
 *
43
 *   http://david.freetype.org/rasterizer-shootout/raster-comparison-20070813.tar.bz2
44
 *
45
 * In particular this file incorporates large parts of ftgrays_tor10.h
46
 * from raster-comparison-20070813.tar.bz2
47
 */
48
/* Overview
49
 *
50
 * A scan converter's basic purpose to take polygon edges and convert
51
 * them into an RLE compressed A8 mask.  This one works in two phases:
52
 * gathering edges and generating spans.
53
 *
54
 * 1) As the user feeds the scan converter edges they are vertically
55
 * clipped and bucketted into a _polygon_ data structure.  The edges
56
 * are also snapped from the user's coordinates to the subpixel grid
57
 * coordinates used during scan conversion.
58
 *
59
 *     user
60
 *      |
61
 *      | edges
62
 *      V
63
 *    polygon buckets
64
 *
65
 * 2) Generating spans works by performing a vertical sweep of pixel
66
 * rows from top to bottom and maintaining an _active_list_ of edges
67
 * that intersect the row.  From the active list the fill rule
68
 * determines which edges are the left and right edges of the start of
69
 * each span, and their contribution is then accumulated into a pixel
70
 * coverage list (_cell_list_) as coverage deltas.  Once the coverage
71
 * deltas of all edges are known we can form spans of constant pixel
72
 * coverage by summing the deltas during a traversal of the cell list.
73
 * At the end of a pixel row the cell list is sent to a coverage
74
 * blitter for rendering to some target surface.
75
 *
76
 * The pixel coverages are computed by either supersampling the row
77
 * and box filtering a mono rasterisation, or by computing the exact
78
 * coverages of edges in the active list.  The supersampling method is
79
 * used whenever some edge starts or stops within the row or there are
80
 * edge intersections in the row.
81
 *
82
 *   polygon bucket for       \
83
 *   current pixel row        |
84
 *      |                     |
85
 *      | activate new edges  |  Repeat GRID_Y times if we
86
 *      V                     \  are supersampling this row,
87
 *   active list              /  or just once if we're computing
88
 *      |                     |  analytical coverage.
89
 *      | coverage deltas     |
90
 *      V                     |
91
 *   pixel coverage list     /
92
 *      |
93
 *      V
94
 *   coverage blitter
95
 */
96
#include "cairoint.h"
97
#include "cairo-spans-private.h"
98
#include "cairo-error-private.h"
99
100
#include <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
364k
#define GLITTER_STATUS_SUCCESS CAIRO_STATUS_SUCCESS
113
0
#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
105M
#define GLITTER_INPUT_BITS CAIRO_FIXED_FRAC_BITS
118
119M
#define GRID_X_BITS CAIRO_FIXED_FRAC_BITS
119
246M
#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
/*-------------------------------------------------------------------------
127
 * glitter-paths.h
128
 */
129
130
/* "Input scaled" numbers are fixed precision reals with multiplier
131
 * 2**GLITTER_INPUT_BITS.  Input coordinates are given to glitter as
132
 * pixel scaled numbers.  These get converted to the internal grid
133
 * scaled numbers as soon as possible. Internal overflow is possible
134
 * if GRID_X/Y inside glitter-paths.c is larger than
135
 * 1<<GLITTER_INPUT_BITS. */
136
#ifndef GLITTER_INPUT_BITS
137
#  define GLITTER_INPUT_BITS 8
138
#endif
139
#define GLITTER_INPUT_SCALE (1<<GLITTER_INPUT_BITS)
140
typedef int glitter_input_scaled_t;
141
142
#if !GLITTER_HAVE_STATUS_T
143
typedef enum {
144
    GLITTER_STATUS_SUCCESS = 0,
145
    GLITTER_STATUS_NO_MEMORY
146
} glitter_status_t;
147
#endif
148
149
#ifndef I
150
# define I /*static*/
151
#endif
152
153
/* Opaque type for scan converting. */
154
typedef struct glitter_scan_converter glitter_scan_converter_t;
155
156
/* Reset a scan converter to accept polygon edges and set the clip box
157
 * in pixels.  Allocates O(ymax-ymin) bytes of memory.  The clip box
158
 * is set to integer pixel coordinates xmin <= x < xmax, ymin <= y <
159
 * ymax. */
160
I glitter_status_t
161
glitter_scan_converter_reset(
162
    glitter_scan_converter_t *converter,
163
    int xmin, int ymin,
164
    int xmax, int ymax);
165
166
/* Render the polygon in the scan converter to the given A8 format
167
 * image raster.  Only the pixels accessible as pixels[y*stride+x] for
168
 * x,y inside the clip box are written to, where xmin <= x < xmax,
169
 * ymin <= y < ymax.  The image is assumed to be clear on input.
170
 *
171
 * If nonzero_fill is true then the interior of the polygon is
172
 * computed with the non-zero fill rule.  Otherwise the even-odd fill
173
 * rule is used.
174
 *
175
 * The scan converter must be reset or destroyed after this call. */
176
177
/*-------------------------------------------------------------------------
178
 * glitter-paths.c: Implementation internal types
179
 */
180
#include <stdlib.h>
181
#include <string.h>
182
#include <limits.h>
183
184
/* All polygon coordinates are snapped onto a subsample grid. "Grid
185
 * scaled" numbers are fixed precision reals with multiplier GRID_X or
186
 * GRID_Y. */
187
typedef int grid_scaled_t;
188
typedef int grid_scaled_x_t;
189
typedef int grid_scaled_y_t;
190
191
/* Default x/y scale factors.
192
 *  You can either define GRID_X/Y_BITS to get a power-of-two scale
193
 *  or define GRID_X/Y separately. */
194
#if !defined(GRID_X) && !defined(GRID_X_BITS)
195
#  define GRID_X_BITS 8
196
#endif
197
#if !defined(GRID_Y) && !defined(GRID_Y_BITS)
198
#  define GRID_Y 15
199
#endif
200
201
/* Use GRID_X/Y_BITS to define GRID_X/Y if they're available. */
202
#ifdef GRID_X_BITS
203
119M
#  define GRID_X (1 << GRID_X_BITS)
204
#endif
205
#ifdef GRID_Y_BITS
206
#  define GRID_Y (1 << GRID_Y_BITS)
207
#endif
208
209
/* The GRID_X_TO_INT_FRAC macro splits a grid scaled coordinate into
210
 * integer and fractional parts. The integer part is floored. */
211
#if defined(GRID_X_TO_INT_FRAC)
212
  /* do nothing */
213
#elif defined(GRID_X_BITS)
214
#  define GRID_X_TO_INT_FRAC(x, i, f) \
215
231M
  _GRID_TO_INT_FRAC_shift(x, i, f, GRID_X_BITS)
216
#else
217
#  define GRID_X_TO_INT_FRAC(x, i, f) \
218
  _GRID_TO_INT_FRAC_general(x, i, f, GRID_X)
219
#endif
220
221
#define _GRID_TO_INT_FRAC_general(t, i, f, m) do {  \
222
    (i) = (t) / (m);          \
223
    (f) = (t) % (m);          \
224
    if ((f) < 0) {          \
225
  --(i);            \
226
  (f) += (m);         \
227
    }             \
228
} while (0)
229
230
231M
#define _GRID_TO_INT_FRAC_shift(t, i, f, b) do { \
231
231M
    (f) = (t) & ((1 << (b)) - 1);     \
232
231M
    (i) = (t) >> (b);         \
233
231M
} while (0)
234
235
/* A grid area is a real in [0,1] scaled by 2*GRID_X*GRID_Y.  We want
236
 * to be able to represent exactly areas of subpixel trapezoids whose
237
 * vertices are given in grid scaled coordinates.  The scale factor
238
 * comes from needing to accurately represent the area 0.5*dx*dy of a
239
 * triangle with base dx and height dy in grid scaled numbers. */
240
#define GRID_XY (2*GRID_X*GRID_Y) /* Unit area on the grid. */
241
242
/* GRID_AREA_TO_ALPHA(area): map [0,GRID_XY] to [0,255]. */
243
#if GRID_XY == 510
244
#  define GRID_AREA_TO_ALPHA(c)   (((c)+1) >> 1)
245
#elif GRID_XY == 255
246
#  define  GRID_AREA_TO_ALPHA(c)  (c)
247
#elif GRID_XY == 64
248
#  define  GRID_AREA_TO_ALPHA(c)  (((c) << 2) | -(((c) & 0x40) >> 6))
249
#elif GRID_XY == 128
250
#  define  GRID_AREA_TO_ALPHA(c)  ((((c) << 1) | -((c) >> 7)) & 255)
251
#elif GRID_XY == 256
252
#  define  GRID_AREA_TO_ALPHA(c)  (((c) | -((c) >> 8)) & 255)
253
#elif GRID_XY == 15
254
#  define  GRID_AREA_TO_ALPHA(c)  (((c) << 4) + (c))
255
#elif GRID_XY == 2*256*15
256
89.1M
#  define  GRID_AREA_TO_ALPHA(c)  (((c) + ((c)<<4) + 256) >> 9)
257
#else
258
#  define  GRID_AREA_TO_ALPHA(c)  (((c)*255 + GRID_XY/2) / GRID_XY)
259
#endif
260
261
291M
#define UNROLL3(x) x x x
262
263
struct quorem {
264
    int32_t quo;
265
    int64_t rem;
266
};
267
268
/* Header for a chunk of memory in a memory pool. */
269
struct _pool_chunk {
270
    /* # bytes used in this chunk. */
271
    size_t size;
272
273
    /* # bytes total in this chunk */
274
    size_t capacity;
275
276
    /* Pointer to the previous chunk or %NULL if this is the sentinel
277
     * chunk in the pool header. */
278
    struct _pool_chunk *prev_chunk;
279
280
    /* Actual data starts here. Well aligned even for 64 bit types. */
281
    int64_t data;
282
};
283
284
/* The int64_t data member of _pool_chunk just exists to enforce alignment,
285
 * it shouldn't be included in the allocated size for the struct. */
286
#define SIZEOF_POOL_CHUNK (sizeof(struct _pool_chunk) - sizeof(int64_t))
287
288
/* A memory pool.  This is supposed to be embedded on the stack or
289
 * within some other structure.  It may optionally be followed by an
290
 * embedded array from which requests are fulfilled until
291
 * malloc needs to be called to allocate a first real chunk. */
292
struct pool {
293
    /* Chunk we're allocating from. */
294
    struct _pool_chunk *current;
295
296
    jmp_buf *jmp;
297
298
    /* Free list of previously allocated chunks.  All have >= default
299
     * capacity. */
300
    struct _pool_chunk *first_free;
301
302
    /* The default capacity of a chunk. */
303
    size_t default_capacity;
304
305
    /* Header for the sentinel chunk.  Directly following the pool
306
     * struct should be some space for embedded elements from which
307
     * the sentinel chunk allocates from. This is expressed as a char
308
     * array so that the 'int64_t data' member of _pool_chunk isn't
309
     * included. This way embedding struct pool in other structs works
310
     * without wasting space. */
311
    char sentinel[SIZEOF_POOL_CHUNK];
312
};
313
314
/* A polygon edge. */
315
struct edge {
316
    /* Next in y-bucket or active list. */
317
    struct edge *next, *prev;
318
319
    /* The clipped y of the top of the edge. */
320
    grid_scaled_y_t ytop;
321
322
    /* Number of subsample rows remaining to scan convert of this
323
     * edge. */
324
    grid_scaled_y_t height_left;
325
326
    /* Original sign of the edge: +1 for downwards, -1 for upwards
327
     * edges.  */
328
    int dir;
329
    int cell;
330
331
    /* Current x coordinate while the edge is on the active
332
     * list. Initialised to the x coordinate of the top of the
333
     * edge. The quotient is in grid_scaled_x_t units and the
334
     * remainder is mod dy in grid_scaled_y_t units.*/
335
    struct quorem x;
336
337
    /* Advance of the current x when moving down a subsample line. */
338
    struct quorem dxdy;
339
340
    /* Advance of the current x when moving down a full pixel
341
     * row. Only initialised when the height of the edge is large
342
     * enough that there's a chance the edge could be stepped by a
343
     * full row's worth of subsample rows at a time. */
344
    struct quorem dxdy_full;
345
346
    /* y2-y1 after orienting the edge downwards.  */
347
    int64_t dy;
348
};
349
350
12.6M
#define EDGE_Y_BUCKET_INDEX(y, ymin) (((y) - (ymin))/GRID_Y)
351
352
/* A collection of sorted and vertically clipped edges of the polygon.
353
 * Edges are moved from the polygon to an active list while scan
354
 * converting. */
355
struct polygon {
356
    /* The vertical clip extents. */
357
    grid_scaled_y_t ymin, ymax;
358
359
    /* Array of edges all starting in the same bucket.  An edge is put
360
     * into bucket EDGE_BUCKET_INDEX(edge->ytop, polygon->ymin) when
361
     * it is added to the polygon. */
362
    struct edge **y_buckets;
363
    struct edge *y_buckets_embedded[64];
364
365
    struct {
366
  struct pool base[1];
367
  struct edge embedded[32];
368
    } edge_pool;
369
};
370
371
/* A cell records the effect on pixel coverage of polygon edges
372
 * passing through a pixel.  It contains two accumulators of pixel
373
 * coverage.
374
 *
375
 * Consider the effects of a polygon edge on the coverage of a pixel
376
 * it intersects and that of the following one.  The coverage of the
377
 * following pixel is the height of the edge multiplied by the width
378
 * of the pixel, and the coverage of the pixel itself is the area of
379
 * the trapezoid formed by the edge and the right side of the pixel.
380
 *
381
 * +-----------------------+-----------------------+
382
 * |                       |                       |
383
 * |                       |                       |
384
 * |_______________________|_______________________|
385
 * |   \...................|.......................|\
386
 * |    \..................|.......................| |
387
 * |     \.................|.......................| |
388
 * |      \....covered.....|.......................| |
389
 * |       \....area.......|.......................| } covered height
390
 * |        \..............|.......................| |
391
 * |uncovered\.............|.......................| |
392
 * |  area    \............|.......................| |
393
 * |___________\...........|.......................|/
394
 * |                       |                       |
395
 * |                       |                       |
396
 * |                       |                       |
397
 * +-----------------------+-----------------------+
398
 *
399
 * Since the coverage of the following pixel will always be a multiple
400
 * of the width of the pixel, we can store the height of the covered
401
 * area instead.  The coverage of the pixel itself is the total
402
 * coverage minus the area of the uncovered area to the left of the
403
 * edge.  As it's faster to compute the uncovered area we only store
404
 * that and subtract it from the total coverage later when forming
405
 * spans to blit.
406
 *
407
 * The heights and areas are signed, with left edges of the polygon
408
 * having positive sign and right edges having negative sign.  When
409
 * two edges intersect they swap their left/rightness so their
410
 * contribution above and below the intersection point must be
411
 * computed separately. */
412
struct cell {
413
    struct cell   *next;
414
    int      x;
415
    int16_t    uncovered_area;
416
    int16_t    covered_height;
417
};
418
419
/* A cell list represents the scan line sparsely as cells ordered by
420
 * ascending x.  It is geared towards scanning the cells in order
421
 * using an internal cursor. */
422
struct cell_list {
423
    /* Sentinel nodes */
424
    struct cell head, tail;
425
426
    /* Cursor state for iterating through the cell list. */
427
    struct cell *cursor, *rewind;
428
429
    /* Cells in the cell list are owned by the cell list and are
430
     * allocated from this pool.  */
431
    struct {
432
  struct pool base[1];
433
  struct cell embedded[32];
434
    } cell_pool;
435
};
436
437
struct cell_pair {
438
    struct cell *cell1;
439
    struct cell *cell2;
440
};
441
442
/* The active list contains edges in the current scan line ordered by
443
 * the x-coordinate of the intercept of the edge and the scan line. */
444
struct active_list {
445
    /* Leftmost edge on the current scan line. */
446
    struct edge head, tail;
447
448
    /* A lower bound on the height of the active edges is used to
449
     * estimate how soon some active edge ends.  We can't advance the
450
     * scan conversion by a full pixel row if an edge ends somewhere
451
     * within it. */
452
    grid_scaled_y_t min_height;
453
    int is_vertical;
454
};
455
456
struct glitter_scan_converter {
457
    struct polygon  polygon[1];
458
    struct active_list  active[1];
459
    struct cell_list  coverages[1];
460
461
    cairo_half_open_span_t *spans;
462
    cairo_half_open_span_t spans_embedded[64];
463
464
    /* Clip box. */
465
    grid_scaled_x_t xmin, xmax;
466
    grid_scaled_y_t ymin, ymax;
467
};
468
469
static struct _pool_chunk *
470
_pool_chunk_init(
471
    struct _pool_chunk *p,
472
    struct _pool_chunk *prev_chunk,
473
    size_t capacity)
474
573k
{
475
573k
    p->prev_chunk = prev_chunk;
476
573k
    p->size = 0;
477
573k
    p->capacity = capacity;
478
573k
    return p;
479
573k
}
480
481
static struct _pool_chunk *
482
_pool_chunk_create(struct pool *pool, size_t size)
483
142k
{
484
142k
    struct _pool_chunk *p;
485
486
142k
    p = _cairo_malloc (SIZEOF_POOL_CHUNK + size);
487
142k
    if (unlikely (NULL == p))
488
0
  longjmp (*pool->jmp, _cairo_error (CAIRO_STATUS_NO_MEMORY));
489
490
142k
    return _pool_chunk_init(p, pool->current, size);
491
142k
}
492
493
static void
494
pool_init(struct pool *pool,
495
    jmp_buf *jmp,
496
    size_t default_capacity,
497
    size_t embedded_capacity)
498
364k
{
499
364k
    pool->jmp = jmp;
500
364k
    pool->current = (void*) pool->sentinel;
501
364k
    pool->first_free = NULL;
502
364k
    pool->default_capacity = default_capacity;
503
364k
    _pool_chunk_init(pool->current, NULL, embedded_capacity);
504
364k
}
505
506
static void
507
pool_fini(struct pool *pool)
508
364k
{
509
364k
    struct _pool_chunk *p = pool->current;
510
367k
    do {
511
874k
  while (NULL != p) {
512
507k
      struct _pool_chunk *prev = p->prev_chunk;
513
507k
      if (p != (void *) pool->sentinel)
514
142k
    free(p);
515
507k
      p = prev;
516
507k
  }
517
367k
  p = pool->first_free;
518
367k
  pool->first_free = NULL;
519
367k
    } while (NULL != p);
520
364k
}
521
522
/* Satisfy an allocation by first allocating a new large enough chunk
523
 * and adding it to the head of the pool's chunk list. This function
524
 * is called as a fallback if pool_alloc() couldn't do a quick
525
 * allocation from the current chunk in the pool. */
526
static void *
527
_pool_alloc_from_new_chunk(
528
    struct pool *pool,
529
    size_t size)
530
208k
{
531
208k
    struct _pool_chunk *chunk;
532
208k
    void *obj;
533
208k
    size_t capacity;
534
535
    /* If the allocation is smaller than the default chunk size then
536
     * try getting a chunk off the free list.  Force alloc of a new
537
     * chunk for large requests. */
538
208k
    capacity = size;
539
208k
    chunk = NULL;
540
208k
    if (size < pool->default_capacity) {
541
208k
  capacity = pool->default_capacity;
542
208k
  chunk = pool->first_free;
543
208k
  if (chunk) {
544
65.7k
      pool->first_free = chunk->prev_chunk;
545
65.7k
      _pool_chunk_init(chunk, pool->current, chunk->capacity);
546
65.7k
  }
547
208k
    }
548
549
208k
    if (NULL == chunk)
550
142k
  chunk = _pool_chunk_create (pool, capacity);
551
208k
    pool->current = chunk;
552
553
208k
    obj = ((unsigned char*)&chunk->data + chunk->size);
554
208k
    chunk->size += size;
555
208k
    return obj;
556
208k
}
557
558
/* Allocate size bytes from the pool.  The first allocated address
559
 * returned from a pool is aligned to 8 bytes.  Subsequent
560
 * addresses will maintain alignment as long as multiples of 8 are
561
 * allocated.  Returns the address of a new memory area or %NULL on
562
 * allocation failures.  The pool retains ownership of the returned
563
 * memory. */
564
inline static void *
565
pool_alloc (struct pool *pool, size_t size)
566
73.1M
{
567
73.1M
    struct _pool_chunk *chunk = pool->current;
568
569
73.1M
    if (size <= chunk->capacity - chunk->size) {
570
72.9M
  void *obj = ((unsigned char*)&chunk->data + chunk->size);
571
72.9M
  chunk->size += size;
572
72.9M
  return obj;
573
72.9M
    } else {
574
208k
  return _pool_alloc_from_new_chunk(pool, size);
575
208k
    }
576
73.1M
}
577
578
/* Relinquish all pool_alloced memory back to the pool. */
579
static void
580
pool_reset (struct pool *pool)
581
13.0M
{
582
    /* Transfer all used chunks to the chunk free list. */
583
13.0M
    struct _pool_chunk *chunk = pool->current;
584
13.0M
    if (chunk != (void *) pool->sentinel) {
585
68.8k
  while (chunk->prev_chunk != (void *) pool->sentinel) {
586
1.60k
      chunk = chunk->prev_chunk;
587
1.60k
  }
588
67.2k
  chunk->prev_chunk = pool->first_free;
589
67.2k
  pool->first_free = pool->current;
590
67.2k
    }
591
    /* Reset the sentinel as the current chunk. */
592
13.0M
    pool->current = (void *) pool->sentinel;
593
13.0M
    pool->current->size = 0;
594
13.0M
}
595
596
/* Rewinds the cell list's cursor to the beginning.  After rewinding
597
 * we're good to cell_list_find() the cell any x coordinate. */
598
inline static void
599
cell_list_rewind (struct cell_list *cells)
600
61.4M
{
601
61.4M
    cells->cursor = &cells->head;
602
61.4M
}
603
604
inline static void
605
cell_list_maybe_rewind (struct cell_list *cells, int x)
606
23.4M
{
607
23.4M
    if (x < cells->cursor->x) {
608
500k
  cells->cursor = cells->rewind;
609
500k
  if (x < cells->cursor->x)
610
11.4k
      cells->cursor = &cells->head;
611
500k
    }
612
23.4M
}
613
614
inline static void
615
cell_list_set_rewind (struct cell_list *cells)
616
11.7M
{
617
11.7M
    cells->rewind = cells->cursor;
618
11.7M
}
619
620
static void
621
cell_list_init(struct cell_list *cells, jmp_buf *jmp)
622
182k
{
623
182k
    pool_init(cells->cell_pool.base, jmp,
624
182k
        256*sizeof(struct cell),
625
182k
        sizeof(cells->cell_pool.embedded));
626
182k
    cells->tail.next = NULL;
627
182k
    cells->tail.x = INT_MAX;
628
182k
    cells->head.x = INT_MIN;
629
182k
    cells->head.next = &cells->tail;
630
182k
    cell_list_rewind (cells);
631
182k
}
632
633
static void
634
cell_list_fini(struct cell_list *cells)
635
182k
{
636
182k
    pool_fini (cells->cell_pool.base);
637
182k
}
638
639
/* Empty the cell list.  This is called at the start of every pixel
640
 * row. */
641
inline static void
642
cell_list_reset (struct cell_list *cells)
643
12.9M
{
644
12.9M
    cell_list_rewind (cells);
645
12.9M
    cells->head.next = &cells->tail;
646
12.9M
    pool_reset (cells->cell_pool.base);
647
12.9M
}
648
649
inline static struct cell *
650
cell_list_alloc (struct cell_list *cells,
651
     struct cell *tail,
652
     int x)
653
60.6M
{
654
60.6M
    struct cell *cell;
655
656
60.6M
    cell = pool_alloc (cells->cell_pool.base, sizeof (struct cell));
657
60.6M
    cell->next = tail->next;
658
60.6M
    tail->next = cell;
659
60.6M
    cell->x = x;
660
60.6M
    *(uint32_t *)&cell->uncovered_area = 0;
661
662
60.6M
    return cell;
663
60.6M
}
664
665
/* Find a cell at the given x-coordinate.  Returns %NULL if a new cell
666
 * needed to be allocated but couldn't be.  Cells must be found with
667
 * non-decreasing x-coordinate until the cell list is rewound using
668
 * cell_list_rewind(). Ownership of the returned cell is retained by
669
 * the cell list. */
670
inline static struct cell *
671
cell_list_find (struct cell_list *cells, int x)
672
23.7M
{
673
23.7M
    struct cell *tail = cells->cursor;
674
675
23.7M
    if (tail->x == x)
676
1.29M
  return tail;
677
678
23.0M
    while (1) {
679
23.0M
  UNROLL3({
680
13.4M
    if (tail->next->x > x)
681
13.4M
      break;
682
13.4M
    tail = tail->next;
683
13.4M
  });
684
13.4M
    }
685
686
22.4M
    if (tail->x != x)
687
12.7M
  tail = cell_list_alloc (cells, tail, x);
688
22.4M
    return cells->cursor = tail;
689
690
23.7M
}
691
692
/* Find two cells at x1 and x2.  This is exactly equivalent
693
 * to
694
 *
695
 *   pair.cell1 = cell_list_find(cells, x1);
696
 *   pair.cell2 = cell_list_find(cells, x2);
697
 *
698
 * except with less function call overhead. */
699
inline static struct cell_pair
700
cell_list_find_pair(struct cell_list *cells, int x1, int x2)
701
94.8M
{
702
94.8M
    struct cell_pair pair;
703
704
94.8M
    pair.cell1 = cells->cursor;
705
100M
    while (1) {
706
100M
  UNROLL3({
707
98.3M
    if (pair.cell1->next->x > x1)
708
98.3M
      break;
709
98.3M
    pair.cell1 = pair.cell1->next;
710
98.3M
  });
711
98.3M
    }
712
94.8M
    if (pair.cell1->x != x1)
713
23.7M
  pair.cell1 = cell_list_alloc (cells, pair.cell1, x1);
714
715
94.8M
    pair.cell2 = pair.cell1;
716
104M
    while (1) {
717
111M
  UNROLL3({
718
111M
    if (pair.cell2->next->x > x2)
719
111M
      break;
720
111M
    pair.cell2 = pair.cell2->next;
721
111M
  });
722
111M
    }
723
94.8M
    if (pair.cell2->x != x2)
724
24.2M
  pair.cell2 = cell_list_alloc (cells, pair.cell2, x2);
725
726
94.8M
    cells->cursor = pair.cell2;
727
94.8M
    return pair;
728
94.8M
}
729
730
/* Add a subpixel span covering [x1, x2) to the coverage cells. */
731
inline static void
732
cell_list_add_subspan(struct cell_list *cells,
733
          grid_scaled_x_t x1,
734
          grid_scaled_x_t x2)
735
93.3M
{
736
93.3M
    int ix1, fx1;
737
93.3M
    int ix2, fx2;
738
739
93.3M
    if (x1 == x2)
740
1.28M
  return;
741
742
92.0M
    GRID_X_TO_INT_FRAC(x1, ix1, fx1);
743
92.0M
    GRID_X_TO_INT_FRAC(x2, ix2, fx2);
744
745
92.0M
    if (ix1 != ix2) {
746
81.2M
  struct cell_pair p;
747
81.2M
  p = cell_list_find_pair(cells, ix1, ix2);
748
81.2M
  p.cell1->uncovered_area += 2*fx1;
749
81.2M
  ++p.cell1->covered_height;
750
81.2M
  p.cell2->uncovered_area -= 2*fx2;
751
81.2M
  --p.cell2->covered_height;
752
81.2M
    } else {
753
10.7M
  struct cell *cell = cell_list_find(cells, ix1);
754
10.7M
  cell->uncovered_area += 2*(fx1-fx2);
755
10.7M
    }
756
92.0M
}
757
758
inline static void full_step (struct edge *e)
759
29.4M
{
760
29.4M
    if (e->dy == 0)
761
6.08M
  return;
762
763
23.4M
    e->x.quo += e->dxdy_full.quo;
764
23.4M
    e->x.rem += e->dxdy_full.rem;
765
23.4M
    if (e->x.rem < 0) {
766
3.72M
  e->x.quo--;
767
3.72M
  e->x.rem += e->dy;
768
19.6M
    } else if (e->x.rem >= e->dy) {
769
8.75M
  ++e->x.quo;
770
8.75M
  e->x.rem -= e->dy;
771
8.75M
    }
772
773
23.4M
    e->cell = e->x.quo + (e->x.rem >= e->dy/2);
774
23.4M
}
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
23.4M
{
799
23.4M
    struct quorem x1, x2;
800
23.4M
    grid_scaled_x_t fx1, fx2;
801
23.4M
    int ix1, ix2;
802
803
23.4M
    x1 = edge->x;
804
23.4M
    full_step (edge);
805
23.4M
    x2 = edge->x;
806
807
    /* Step back from the sample location (half-subrow) to the pixel origin */
808
23.4M
    if (edge->dy) {
809
21.0M
  x1.quo -= edge->dxdy.quo / 2;
810
21.0M
  x1.rem -= edge->dxdy.rem / 2;
811
21.0M
  if (x1.rem < 0) {
812
2.89M
      --x1.quo;
813
2.89M
      x1.rem += edge->dy;
814
18.1M
  } else if (x1.rem >= edge->dy) {
815
1.85M
      ++x1.quo;
816
1.85M
      x1.rem -= edge->dy;
817
1.85M
  }
818
819
21.0M
  x2.quo -= edge->dxdy.quo / 2;
820
21.0M
  x2.rem -= edge->dxdy.rem / 2;
821
21.0M
  if (x2.rem < 0) {
822
2.65M
      --x2.quo;
823
2.65M
      x2.rem += edge->dy;
824
18.3M
  } else if (x2.rem >= edge->dy) {
825
1.73M
      ++x2.quo;
826
1.73M
      x2.rem -= edge->dy;
827
1.73M
  }
828
21.0M
    }
829
830
23.4M
    GRID_X_TO_INT_FRAC(x1.quo, ix1, fx1);
831
23.4M
    GRID_X_TO_INT_FRAC(x2.quo, ix2, fx2);
832
833
23.4M
    cell_list_maybe_rewind(cells, MIN(ix1, ix2));
834
835
    /* Edge is entirely within a column? */
836
23.4M
    if (ix1 == ix2) {
837
  /* We always know that ix1 is >= the cell list cursor in this
838
   * case due to the no-intersections precondition.  */
839
9.87M
  struct cell *cell = cell_list_find(cells, ix1);
840
9.87M
  cell->covered_height += sign*GRID_Y;
841
9.87M
  cell->uncovered_area += sign*(fx1 + fx2)*GRID_Y;
842
9.87M
  return;
843
9.87M
    }
844
845
    /* Orient the edge left-to-right. */
846
13.5M
    if (ix2 < ix1) {
847
3.54M
  struct quorem tx;
848
3.54M
  int t;
849
850
3.54M
  t = ix1;
851
3.54M
  ix1 = ix2;
852
3.54M
  ix2 = t;
853
854
3.54M
  t = fx1;
855
3.54M
  fx1 = fx2;
856
3.54M
  fx2 = t;
857
858
3.54M
  tx = x1;
859
3.54M
  x1 = x2;
860
3.54M
  x2 = tx;
861
3.54M
    }
862
863
    /* Add coverage for all pixels [ix1,ix2] on this row crossed
864
     * by the edge. */
865
13.5M
    {
866
13.5M
  struct cell_pair pair;
867
13.5M
  struct quorem y;
868
13.5M
  int64_t tmp, dx;
869
13.5M
  int y_last;
870
871
13.5M
  dx = (x2.quo - x1.quo) * edge->dy + (x2.rem - x1.rem);
872
873
13.5M
  tmp = (ix1 + 1) * GRID_X * edge->dy;
874
13.5M
  tmp -= x1.quo * edge->dy + x1.rem;
875
13.5M
  tmp *= GRID_Y;
876
877
13.5M
  y.quo = tmp / dx;
878
13.5M
  y.rem = tmp % dx;
879
880
  /* When rendering a previous edge on the active list we may
881
   * advance the cell list cursor past the leftmost pixel of the
882
   * current edge even though the two edges don't intersect.
883
   * e.g. consider two edges going down and rightwards:
884
   *
885
   *  --\_+---\_+-----+-----+----
886
   *      \_    \_    |     |
887
   *      | \_  | \_  |     |
888
   *      |   \_|   \_|     |
889
   *      |     \_    \_    |
890
   *  ----+-----+-\---+-\---+----
891
   *
892
   * The left edge touches cells past the starting cell of the
893
   * right edge.  Fortunately such cases are rare.
894
   */
895
896
13.5M
  pair = cell_list_find_pair(cells, ix1, ix1+1);
897
13.5M
  pair.cell1->uncovered_area += sign*y.quo*(GRID_X + fx1);
898
13.5M
  pair.cell1->covered_height += sign*y.quo;
899
13.5M
  y_last = y.quo;
900
901
13.5M
  if (ix1+1 < ix2) {
902
2.02M
      struct cell *cell = pair.cell2;
903
2.02M
      struct quorem dydx_full;
904
905
2.02M
      dydx_full.quo = GRID_Y * GRID_X * edge->dy / dx;
906
2.02M
      dydx_full.rem = GRID_Y * GRID_X * edge->dy % dx;
907
908
2.02M
      ++ix1;
909
3.12M
      do {
910
3.12M
    y.quo += dydx_full.quo;
911
3.12M
    y.rem += dydx_full.rem;
912
3.12M
    if (y.rem >= dx) {
913
1.08M
        y.quo++;
914
1.08M
        y.rem -= dx;
915
1.08M
    }
916
917
3.12M
    cell->uncovered_area += sign*(y.quo - y_last)*GRID_X;
918
3.12M
    cell->covered_height += sign*(y.quo - y_last);
919
3.12M
    y_last = y.quo;
920
921
3.12M
    ++ix1;
922
3.12M
    cell = cell_list_find(cells, ix1);
923
3.12M
      } while (ix1 != ix2);
924
925
2.02M
      pair.cell2 = cell;
926
2.02M
  }
927
13.5M
  pair.cell2->uncovered_area += sign*(GRID_Y - y_last)*fx2;
928
13.5M
  pair.cell2->covered_height += sign*(GRID_Y - y_last);
929
13.5M
    }
930
13.5M
}
931
932
static void
933
polygon_init (struct polygon *polygon, jmp_buf *jmp)
934
182k
{
935
182k
    polygon->ymin = polygon->ymax = 0;
936
182k
    polygon->y_buckets = polygon->y_buckets_embedded;
937
182k
    pool_init (polygon->edge_pool.base, jmp,
938
182k
         8192 - sizeof (struct _pool_chunk),
939
182k
         sizeof (polygon->edge_pool.embedded));
940
182k
}
941
942
static void
943
polygon_fini (struct polygon *polygon)
944
182k
{
945
182k
    if (polygon->y_buckets != polygon->y_buckets_embedded)
946
127k
  free (polygon->y_buckets);
947
948
182k
    pool_fini (polygon->edge_pool.base);
949
182k
}
950
951
/* Empties the polygon of all edges. The polygon is then prepared to
952
 * receive new edges and clip them to the vertical range
953
 * [ymin,ymax). */
954
static glitter_status_t
955
polygon_reset (struct polygon *polygon,
956
         grid_scaled_y_t ymin,
957
         grid_scaled_y_t ymax)
958
182k
{
959
182k
    unsigned h = ymax - ymin;
960
182k
    unsigned num_buckets = EDGE_Y_BUCKET_INDEX(ymax + GRID_Y-1, ymin);
961
962
182k
    pool_reset(polygon->edge_pool.base);
963
964
182k
    if (unlikely (h > 0x7FFFFFFFU - GRID_Y))
965
0
  goto bail_no_mem; /* even if you could, you wouldn't want to. */
966
967
182k
    if (polygon->y_buckets != polygon->y_buckets_embedded)
968
0
  free (polygon->y_buckets);
969
970
182k
    polygon->y_buckets =  polygon->y_buckets_embedded;
971
182k
    if (num_buckets > ARRAY_LENGTH (polygon->y_buckets_embedded)) {
972
127k
  polygon->y_buckets = _cairo_malloc_ab (num_buckets,
973
127k
                 sizeof (struct edge *));
974
127k
  if (unlikely (NULL == polygon->y_buckets))
975
0
      goto bail_no_mem;
976
127k
    }
977
182k
    memset (polygon->y_buckets, 0, num_buckets * sizeof (struct edge *));
978
979
182k
    polygon->ymin = ymin;
980
182k
    polygon->ymax = ymax;
981
182k
    return GLITTER_STATUS_SUCCESS;
982
983
0
bail_no_mem:
984
0
    polygon->ymin = 0;
985
0
    polygon->ymax = 0;
986
0
    return GLITTER_STATUS_NO_MEMORY;
987
182k
}
988
989
static void
990
_polygon_insert_edge_into_its_y_bucket(struct polygon *polygon,
991
               struct edge *e)
992
12.4M
{
993
12.4M
    unsigned ix = EDGE_Y_BUCKET_INDEX(e->ytop, polygon->ymin);
994
12.4M
    struct edge **ptail = &polygon->y_buckets[ix];
995
12.4M
    e->next = *ptail;
996
12.4M
    *ptail = e;
997
12.4M
}
998
999
static void
1000
active_list_reset (struct active_list *active)
1001
364k
{
1002
364k
    active->head.height_left = INT_MAX;
1003
364k
    active->head.dy = 0;
1004
364k
    active->head.cell = INT_MIN;
1005
364k
    active->head.prev = NULL;
1006
364k
    active->head.next = &active->tail;
1007
364k
    active->tail.prev = &active->head;
1008
364k
    active->tail.next = NULL;
1009
364k
    active->tail.cell = INT_MAX;
1010
364k
    active->tail.height_left = INT_MAX;
1011
364k
    active->tail.dy = 0;
1012
364k
    active->min_height = 0;
1013
364k
    active->is_vertical = 1;
1014
364k
}
1015
1016
static void
1017
active_list_init(struct active_list *active)
1018
182k
{
1019
182k
    active_list_reset(active);
1020
182k
}
1021
1022
/*
1023
 * Merge two sorted edge lists.
1024
 * Input:
1025
 *  - head_a: The head of the first list.
1026
 *  - head_b: The head of the second list; head_b cannot be NULL.
1027
 * Output:
1028
 * Returns the head of the merged list.
1029
 *
1030
 * Implementation notes:
1031
 * To make it fast (in particular, to reduce to an insertion sort whenever
1032
 * one of the two input lists only has a single element) we iterate through
1033
 * a list until its head becomes greater than the head of the other list,
1034
 * then we switch their roles. As soon as one of the two lists is empty, we
1035
 * just attach the other one to the current list and exit.
1036
 * Writes to memory are only needed to "switch" lists (as it also requires
1037
 * attaching to the output list the list which we will be iterating next) and
1038
 * to attach the last non-empty list.
1039
 */
1040
static struct edge *
1041
merge_sorted_edges (struct edge *head_a, struct edge *head_b)
1042
6.76M
{
1043
6.76M
    struct edge *head, **next, *prev;
1044
6.76M
    int32_t x;
1045
1046
6.76M
    prev = head_a->prev;
1047
6.76M
    next = &head;
1048
6.76M
    if (head_a->cell <= head_b->cell) {
1049
3.51M
  head = head_a;
1050
3.51M
    } else {
1051
3.24M
  head = head_b;
1052
3.24M
  head_b->prev = prev;
1053
3.24M
  goto start_with_b;
1054
3.24M
    }
1055
1056
4.41M
    do {
1057
4.41M
  x = head_b->cell;
1058
77.6M
  while (head_a != NULL && head_a->cell <= x) {
1059
73.2M
      prev = head_a;
1060
73.2M
      next = &head_a->next;
1061
73.2M
      head_a = head_a->next;
1062
73.2M
  }
1063
1064
4.41M
  head_b->prev = prev;
1065
4.41M
  *next = head_b;
1066
4.41M
  if (head_a == NULL)
1067
1.06M
      return head;
1068
1069
6.58M
start_with_b:
1070
6.58M
  x = head_a->cell;
1071
31.0M
  while (head_b != NULL && head_b->cell <= x) {
1072
24.4M
      prev = head_b;
1073
24.4M
      next = &head_b->next;
1074
24.4M
      head_b = head_b->next;
1075
24.4M
  }
1076
1077
6.58M
  head_a->prev = prev;
1078
6.58M
  *next = head_a;
1079
6.58M
  if (head_b == NULL)
1080
5.69M
      return head;
1081
6.58M
    } while (1);
1082
3.51M
}
1083
1084
/*
1085
 * Sort (part of) a list.
1086
 * Input:
1087
 *  - list: The list to be sorted; list cannot be NULL.
1088
 *  - limit: Recursion limit.
1089
 * Output:
1090
 *  - head_out: The head of the sorted list containing the first 2^(level+1) elements of the
1091
 *              input list; if the input list has fewer elements, head_out be a sorted list
1092
 *              containing all the elements of the input list.
1093
 * Returns the head of the list of unprocessed elements (NULL if the sorted list contains
1094
 * all the elements of the input list).
1095
 *
1096
 * Implementation notes:
1097
 * Special case single element list, unroll/inline the sorting of the first two elements.
1098
 * Some tail recursion is used since we iterate on the bottom-up solution of the problem
1099
 * (we start with a small sorted list and keep merging other lists of the same size to it).
1100
 */
1101
static struct edge *
1102
sort_edges (struct edge *list,
1103
      unsigned int level,
1104
      struct edge **head_out)
1105
6.76M
{
1106
6.76M
    struct edge *head_other, *remaining;
1107
6.76M
    unsigned int i;
1108
1109
6.76M
    head_other = list->next;
1110
1111
6.76M
    if (head_other == NULL) {
1112
1.05M
  *head_out = list;
1113
1.05M
  return NULL;
1114
1.05M
    }
1115
1116
5.70M
    remaining = head_other->next;
1117
5.70M
    if (list->cell <= head_other->cell) {
1118
3.90M
  *head_out = list;
1119
3.90M
  head_other->next = NULL;
1120
3.90M
    } else {
1121
1.80M
  *head_out = head_other;
1122
1.80M
  head_other->prev = list->prev;
1123
1.80M
  head_other->next = list;
1124
1.80M
  list->prev = head_other;
1125
1.80M
  list->next = NULL;
1126
1.80M
    }
1127
1128
8.13M
    for (i = 0; i < level && remaining; i++) {
1129
2.42M
  remaining = sort_edges (remaining, i, &head_other);
1130
2.42M
  *head_out = merge_sorted_edges (*head_out, head_other);
1131
2.42M
    }
1132
1133
5.70M
    return remaining;
1134
6.76M
}
1135
1136
 static struct edge *
1137
merge_unsorted_edges (struct edge *head, struct edge *unsorted)
1138
4.33M
{
1139
4.33M
    sort_edges (unsorted, UINT_MAX, &unsorted);
1140
4.33M
    return merge_sorted_edges (head, unsorted);
1141
4.33M
}
1142
1143
/* Test if the edges on the active list can be safely advanced by a
1144
 * full row without intersections or any edges ending. */
1145
inline static int
1146
can_do_full_row (struct active_list *active)
1147
9.62M
{
1148
9.62M
    const struct edge *e;
1149
9.62M
    int prev_x = INT_MIN;
1150
1151
    /* Recomputes the minimum height of all edges on the active
1152
     * list if we have been dropping edges. */
1153
9.62M
    if (active->min_height <= 0) {
1154
2.48M
  int min_height = INT_MAX;
1155
2.48M
  int is_vertical = 1;
1156
1157
2.48M
  e = active->head.next;
1158
14.9M
  while (NULL != e) {
1159
12.4M
      if (e->height_left < min_height)
1160
3.05M
    min_height = e->height_left;
1161
12.4M
      is_vertical &= e->dy == 0;
1162
12.4M
      e = e->next;
1163
12.4M
  }
1164
1165
2.48M
  active->is_vertical = is_vertical;
1166
2.48M
  active->min_height = min_height;
1167
2.48M
    }
1168
1169
9.62M
    if (active->min_height < GRID_Y)
1170
94.8k
  return 0;
1171
1172
    /* Check for intersections as no edges end during the next row. */
1173
39.4M
    for (e = active->head.next; e != &active->tail; e = e->next) {
1174
29.9M
  int cell;
1175
1176
29.9M
  if (e->dy) {
1177
23.7M
      struct quorem x = e->x;
1178
23.7M
      x.quo += e->dxdy_full.quo;
1179
23.7M
      x.rem += e->dxdy_full.rem;
1180
23.7M
      if (x.rem < 0) {
1181
3.82M
    x.quo--;
1182
3.82M
    x.rem += e->dy;
1183
19.9M
      } else if (x.rem >= e->dy) {
1184
8.86M
    x.quo++;
1185
8.86M
    x.rem -= e->dy;
1186
8.86M
      }
1187
23.7M
      cell = x.quo + (x.rem >= e->dy/2);
1188
23.7M
  } else
1189
6.14M
      cell = e->cell;
1190
1191
29.9M
  if (cell < prev_x)
1192
19.6k
      return 0;
1193
1194
29.9M
  prev_x = cell;
1195
29.9M
    }
1196
1197
9.50M
    return 1;
1198
9.52M
}
1199
1200
/* Merges edges on the given subpixel row from the polygon to the
1201
 * active_list. */
1202
inline static void
1203
active_list_merge_edges_from_bucket(struct active_list *active,
1204
            struct edge *edges)
1205
4.33M
{
1206
4.33M
    active->head.next = merge_unsorted_edges (active->head.next, edges);
1207
4.33M
}
1208
1209
inline static int
1210
polygon_fill_buckets (struct active_list *active,
1211
          struct edge *edge,
1212
          int y,
1213
          struct edge **buckets)
1214
12.8M
{
1215
12.8M
    grid_scaled_y_t min_height = active->min_height;
1216
12.8M
    int is_vertical = active->is_vertical;
1217
12.8M
    int max_suby = 0;
1218
1219
25.3M
    while (edge) {
1220
12.4M
  struct edge *next = edge->next;
1221
12.4M
  int suby = edge->ytop - y;
1222
12.4M
  if (buckets[suby])
1223
8.13M
      buckets[suby]->prev = edge;
1224
12.4M
  edge->next = buckets[suby];
1225
12.4M
  edge->prev = NULL;
1226
12.4M
  buckets[suby] = edge;
1227
12.4M
  if (edge->height_left < min_height)
1228
172k
      min_height = edge->height_left;
1229
12.4M
  is_vertical &= edge->dy == 0;
1230
12.4M
  edge = next;
1231
12.4M
  if (suby > max_suby)
1232
3.39M
    max_suby = suby;
1233
12.4M
    }
1234
1235
12.8M
    active->is_vertical = is_vertical;
1236
12.8M
    active->min_height = min_height;
1237
1238
12.8M
    return max_suby;
1239
12.8M
}
1240
1241
static void step (struct edge *edge)
1242
386M
{
1243
386M
    if (edge->dy == 0)
1244
50.9M
  return;
1245
1246
335M
    edge->x.quo += edge->dxdy.quo;
1247
335M
    edge->x.rem += edge->dxdy.rem;
1248
335M
    if (edge->x.rem < 0) {
1249
89.3M
  --edge->x.quo;
1250
89.3M
  edge->x.rem += edge->dy;
1251
246M
    } else if (edge->x.rem >= edge->dy) {
1252
81.7M
  ++edge->x.quo;
1253
81.7M
  edge->x.rem -= edge->dy;
1254
81.7M
    }
1255
1256
335M
    edge->cell = edge->x.quo + (edge->x.rem >= edge->dy/2);
1257
335M
}
1258
1259
inline static void
1260
sub_row (struct active_list *active,
1261
   struct cell_list *coverages,
1262
   unsigned int mask)
1263
48.3M
{
1264
48.3M
    struct edge *edge = active->head.next;
1265
48.3M
    int xstart = INT_MIN, prev_x = INT_MIN;
1266
48.3M
    int winding = 0;
1267
1268
48.3M
    cell_list_rewind (coverages);
1269
1270
447M
    while (&active->tail != edge) {
1271
398M
  struct edge *next = edge->next;
1272
398M
  int xend = edge->cell;
1273
1274
398M
  if (--edge->height_left) {
1275
386M
      step (edge);
1276
1277
386M
      if (edge->cell < prev_x) {
1278
2.04M
    struct edge *pos = edge->prev;
1279
2.04M
    pos->next = next;
1280
2.04M
    next->prev = pos;
1281
186M
    do {
1282
186M
        pos = pos->prev;
1283
186M
    } while (edge->cell < pos->cell);
1284
2.04M
    pos->next->prev = edge;
1285
2.04M
    edge->next = pos->next;
1286
2.04M
    edge->prev = pos;
1287
2.04M
    pos->next = edge;
1288
2.04M
      } else
1289
384M
    prev_x = edge->cell;
1290
386M
      active->min_height = -1;
1291
386M
  } else {
1292
11.9M
      edge->prev->next = next;
1293
11.9M
      next->prev = edge->prev;
1294
11.9M
  }
1295
1296
398M
  winding += edge->dir;
1297
398M
  if ((winding & mask) == 0) {
1298
95.5M
      if (next->cell != xend) {
1299
93.3M
    cell_list_add_subspan (coverages, xstart, xend);
1300
93.3M
    xstart = INT_MIN;
1301
93.3M
      }
1302
303M
  } else if (xstart == INT_MIN)
1303
93.3M
      xstart = xend;
1304
1305
398M
  edge = next;
1306
398M
    }
1307
48.3M
}
1308
1309
inline static void dec (struct active_list *a, struct edge *e, int h)
1310
29.4M
{
1311
29.4M
    e->height_left -= h;
1312
29.4M
    if (e->height_left == 0) {
1313
426k
  e->prev->next = e->next;
1314
426k
  e->next->prev = e->prev;
1315
426k
  a->min_height = -1;
1316
426k
    }
1317
29.4M
}
1318
1319
static void
1320
full_row (struct active_list *active,
1321
    struct cell_list *coverages,
1322
    unsigned int mask)
1323
9.50M
{
1324
9.50M
    struct edge *left = active->head.next;
1325
1326
21.2M
    while (&active->tail != left) {
1327
11.7M
  struct edge *right;
1328
11.7M
  int winding;
1329
1330
11.7M
  dec (active, left, GRID_Y);
1331
1332
11.7M
  winding = left->dir;
1333
11.7M
  right = left->next;
1334
17.7M
  do {
1335
17.7M
      dec (active, right, GRID_Y);
1336
1337
17.7M
      winding += right->dir;
1338
17.7M
      if ((winding & mask) == 0 && right->next->cell != right->cell)
1339
11.7M
    break;
1340
1341
6.04M
      full_step (right);
1342
1343
6.04M
      right = right->next;
1344
6.04M
  } while (1);
1345
1346
11.7M
  cell_list_set_rewind (coverages);
1347
11.7M
  cell_list_render_edge (coverages, left, +1);
1348
11.7M
  cell_list_render_edge (coverages, right, -1);
1349
1350
11.7M
  left = right->next;
1351
11.7M
    }
1352
9.50M
}
1353
1354
static void
1355
_glitter_scan_converter_init(glitter_scan_converter_t *converter, jmp_buf *jmp)
1356
182k
{
1357
182k
    polygon_init(converter->polygon, jmp);
1358
182k
    active_list_init(converter->active);
1359
182k
    cell_list_init(converter->coverages, jmp);
1360
182k
    converter->xmin=0;
1361
182k
    converter->ymin=0;
1362
182k
    converter->xmax=0;
1363
182k
    converter->ymax=0;
1364
182k
}
1365
1366
static void
1367
_glitter_scan_converter_fini(glitter_scan_converter_t *self)
1368
182k
{
1369
182k
    if (self->spans != self->spans_embedded)
1370
160k
  free (self->spans);
1371
1372
182k
    polygon_fini(self->polygon);
1373
182k
    cell_list_fini(self->coverages);
1374
1375
182k
    self->xmin=0;
1376
182k
    self->ymin=0;
1377
182k
    self->xmax=0;
1378
182k
    self->ymax=0;
1379
182k
}
1380
1381
static grid_scaled_t
1382
int_to_grid_scaled(int i, int scale)
1383
729k
{
1384
    /* Clamp to max/min representable scaled number. */
1385
729k
    if (i >= 0) {
1386
729k
  if (i >= INT_MAX/scale)
1387
0
      i = INT_MAX/scale;
1388
729k
    }
1389
0
    else {
1390
0
  if (i <= INT_MIN/scale)
1391
0
      i = INT_MIN/scale;
1392
0
    }
1393
729k
    return i*scale;
1394
729k
}
1395
1396
364k
#define int_to_grid_scaled_x(x) int_to_grid_scaled((x), GRID_X)
1397
364k
#define int_to_grid_scaled_y(x) int_to_grid_scaled((x), GRID_Y)
1398
1399
I glitter_status_t
1400
glitter_scan_converter_reset(
1401
           glitter_scan_converter_t *converter,
1402
           int xmin, int ymin,
1403
           int xmax, int ymax)
1404
182k
{
1405
182k
    glitter_status_t status;
1406
182k
    int max_num_spans;
1407
1408
182k
    converter->xmin = 0; converter->xmax = 0;
1409
182k
    converter->ymin = 0; converter->ymax = 0;
1410
1411
182k
    max_num_spans = xmax - xmin + 1;
1412
1413
182k
    if (max_num_spans > ARRAY_LENGTH(converter->spans_embedded)) {
1414
160k
  converter->spans = _cairo_malloc_ab (max_num_spans,
1415
160k
               sizeof (cairo_half_open_span_t));
1416
160k
  if (unlikely (converter->spans == NULL))
1417
0
      return _cairo_error (CAIRO_STATUS_NO_MEMORY);
1418
160k
    } else
1419
21.5k
  converter->spans = converter->spans_embedded;
1420
1421
182k
    xmin = int_to_grid_scaled_x(xmin);
1422
182k
    ymin = int_to_grid_scaled_y(ymin);
1423
182k
    xmax = int_to_grid_scaled_x(xmax);
1424
182k
    ymax = int_to_grid_scaled_y(ymax);
1425
1426
182k
    active_list_reset(converter->active);
1427
182k
    cell_list_reset(converter->coverages);
1428
182k
    status = polygon_reset(converter->polygon, ymin, ymax);
1429
182k
    if (status)
1430
0
  return status;
1431
1432
182k
    converter->xmin = xmin;
1433
182k
    converter->xmax = xmax;
1434
182k
    converter->ymin = ymin;
1435
182k
    converter->ymax = ymax;
1436
182k
    return GLITTER_STATUS_SUCCESS;
1437
182k
}
1438
1439
/* INPUT_TO_GRID_X/Y (in_coord, out_grid_scaled, grid_scale)
1440
 *   These macros convert an input coordinate in the client's
1441
 *   device space to the rasterisation grid.
1442
 */
1443
/* Gah.. this bit of ugly defines INPUT_TO_GRID_X/Y so as to use
1444
 * shifts if possible, and something saneish if not.
1445
 */
1446
#if !defined(INPUT_TO_GRID_Y) && defined(GRID_Y_BITS) && GRID_Y_BITS <= GLITTER_INPUT_BITS
1447
#  define INPUT_TO_GRID_Y(in, out) (out) = (in) >> (GLITTER_INPUT_BITS - GRID_Y_BITS)
1448
#else
1449
25.2M
#  define INPUT_TO_GRID_Y(in, out) INPUT_TO_GRID_general(in, out, GRID_Y)
1450
#endif
1451
1452
#if !defined(INPUT_TO_GRID_X) && defined(GRID_X_BITS) && GRID_X_BITS <= GLITTER_INPUT_BITS
1453
#  define INPUT_TO_GRID_X(in, out) (out) = (in) >> (GLITTER_INPUT_BITS - GRID_X_BITS)
1454
#else
1455
#  define INPUT_TO_GRID_X(in, out) INPUT_TO_GRID_general(in, out, GRID_X)
1456
#endif
1457
1458
25.2M
#define INPUT_TO_GRID_general(in, out, grid_scale) do {   \
1459
25.2M
    long long tmp__ = (long long)(grid_scale) * (in); \
1460
25.2M
    tmp__ += 1 << (GLITTER_INPUT_BITS-1);      \
1461
25.2M
    tmp__ >>= GLITTER_INPUT_BITS;       \
1462
25.2M
    (out) = tmp__;            \
1463
25.2M
} while (0)
1464
1465
inline static void
1466
polygon_add_edge (struct polygon *polygon,
1467
      const cairo_edge_t *edge)
1468
12.6M
{
1469
12.6M
    struct edge *e;
1470
12.6M
    grid_scaled_y_t ytop, ybot;
1471
12.6M
    const cairo_point_t *p1, *p2;
1472
1473
12.6M
    INPUT_TO_GRID_Y (edge->top, ytop);
1474
12.6M
    if (ytop < polygon->ymin)
1475
0
      ytop = polygon->ymin;
1476
1477
12.6M
    INPUT_TO_GRID_Y (edge->bottom, ybot);
1478
12.6M
    if (ybot > polygon->ymax)
1479
0
      ybot = polygon->ymax;
1480
1481
12.6M
    if (ybot <= ytop)
1482
137k
      return;
1483
1484
12.4M
    e = pool_alloc (polygon->edge_pool.base, sizeof (struct edge));
1485
1486
12.4M
    e->ytop = ytop;
1487
12.4M
    e->height_left = ybot - ytop;
1488
12.4M
    if (edge->line.p2.y > edge->line.p1.y) {
1489
12.4M
      e->dir = edge->dir;
1490
12.4M
      p1 = &edge->line.p1;
1491
12.4M
      p2 = &edge->line.p2;
1492
12.4M
    } else {
1493
0
      e->dir = -edge->dir;
1494
0
      p1 = &edge->line.p2;
1495
0
      p2 = &edge->line.p1;
1496
0
    }
1497
1498
12.4M
    if (p2->x == p1->x) {
1499
846k
  e->cell = p1->x;
1500
846k
  e->x.quo = p1->x;
1501
846k
  e->x.rem = 0;
1502
846k
  e->dxdy.quo = e->dxdy.rem = 0;
1503
846k
  e->dxdy_full.quo = e->dxdy_full.rem = 0;
1504
846k
  e->dy = 0;
1505
11.6M
    } else {
1506
11.6M
  int64_t Ex, Ey, tmp;
1507
1508
11.6M
  Ex = (int64_t)(p2->x - p1->x) * GRID_X;
1509
11.6M
  Ey = (int64_t)(p2->y - p1->y) * GRID_Y * (2 << GLITTER_INPUT_BITS);
1510
1511
11.6M
  e->dxdy.quo = Ex * (2 << GLITTER_INPUT_BITS) / Ey;
1512
11.6M
  e->dxdy.rem = Ex * (2 << GLITTER_INPUT_BITS) % Ey;
1513
1514
11.6M
  tmp = (int64_t)(2*ytop + 1) << GLITTER_INPUT_BITS;
1515
11.6M
  tmp -= (int64_t)p1->y * GRID_Y * 2;
1516
11.6M
  tmp *= Ex;
1517
11.6M
  e->x.quo = tmp / Ey;
1518
11.6M
  e->x.rem = tmp % Ey;
1519
1520
11.6M
#if GRID_X_BITS == GLITTER_INPUT_BITS
1521
11.6M
  e->x.quo += p1->x;
1522
#else
1523
  tmp = (int64_t)p1->x * GRID_X;
1524
  e->x.quo += tmp >> GLITTER_INPUT_BITS;
1525
  e->x.rem += ((tmp & ((1 << GLITTER_INPUT_BITS) - 1)) * Ey) / (1 << GLITTER_INPUT_BITS);
1526
#endif
1527
1528
11.6M
  if (e->x.rem < 0) {
1529
5.59M
    e->x.quo--;
1530
5.59M
    e->x.rem += Ey;
1531
6.03M
  } else  if (e->x.rem >= Ey) {
1532
0
    e->x.quo++;
1533
0
    e->x.rem -= Ey;
1534
0
  }
1535
1536
11.6M
  if (e->height_left >= GRID_Y) {
1537
8.66M
      tmp = Ex * (2 * GRID_Y << GLITTER_INPUT_BITS);
1538
8.66M
      e->dxdy_full.quo = tmp / Ey;
1539
8.66M
      e->dxdy_full.rem = tmp % Ey;
1540
8.66M
  } else
1541
2.95M
      e->dxdy_full.quo = e->dxdy_full.rem = 0;
1542
1543
11.6M
  e->cell = e->x.quo + (e->x.rem >= Ey/2);
1544
11.6M
  e->dy = Ey;
1545
11.6M
    }
1546
1547
12.4M
    _polygon_insert_edge_into_its_y_bucket (polygon, e);
1548
12.4M
}
1549
1550
/* Add a new polygon edge from pixel (x1,y1) to (x2,y2) to the scan
1551
 * converter.  The coordinates represent pixel positions scaled by
1552
 * 2**GLITTER_PIXEL_BITS.  If this function fails then the scan
1553
 * converter should be reset or destroyed.  Dir must be +1 or -1,
1554
 * with the latter reversing the orientation of the edge. */
1555
I void
1556
glitter_scan_converter_add_edge (glitter_scan_converter_t *converter,
1557
         const cairo_edge_t *edge)
1558
12.6M
{
1559
12.6M
    polygon_add_edge (converter->polygon, edge);
1560
12.6M
}
1561
1562
static void
1563
step_edges (struct active_list *active, int count)
1564
90.0k
{
1565
90.0k
    struct edge *edge;
1566
1567
90.0k
    count *= GRID_Y;
1568
517k
    for (edge = active->head.next; edge != &active->tail; edge = edge->next) {
1569
427k
  edge->height_left -= count;
1570
427k
  if (! edge->height_left) {
1571
58.9k
      edge->prev->next = edge->next;
1572
58.9k
      edge->next->prev = edge->prev;
1573
58.9k
      active->min_height = -1;
1574
58.9k
  }
1575
427k
    }
1576
90.0k
}
1577
1578
static glitter_status_t
1579
blit_a8 (struct cell_list *cells,
1580
   cairo_span_renderer_t *renderer,
1581
   cairo_half_open_span_t *spans,
1582
   int y, int height,
1583
   int xmin, int xmax)
1584
12.7M
{
1585
12.7M
    struct cell *cell = cells->head.next;
1586
12.7M
    int prev_x = xmin, last_x = -1;
1587
12.7M
    int16_t cover = 0, last_cover = 0;
1588
12.7M
    unsigned num_spans;
1589
1590
12.7M
    if (cell == &cells->tail)
1591
18.9k
  return CAIRO_STATUS_SUCCESS;
1592
1593
    /* Skip cells to the left of the clip region. */
1594
12.7M
    while (cell->x < xmin) {
1595
751
  cover += cell->covered_height;
1596
751
  cell = cell->next;
1597
751
    }
1598
12.7M
    cover *= GRID_X*2;
1599
1600
    /* Form the spans from the coverages and areas. */
1601
12.7M
    num_spans = 0;
1602
73.2M
    for (; cell->x < xmax; cell = cell->next) {
1603
60.5M
  int x = cell->x;
1604
60.5M
  int16_t area;
1605
1606
60.5M
  if (x > prev_x && cover != last_cover) {
1607
17.9M
      spans[num_spans].x = prev_x;
1608
17.9M
      spans[num_spans].coverage = GRID_AREA_TO_ALPHA (cover);
1609
17.9M
      last_cover = cover;
1610
17.9M
      last_x = prev_x;
1611
17.9M
      ++num_spans;
1612
17.9M
  }
1613
1614
60.5M
  cover += cell->covered_height*GRID_X*2;
1615
60.5M
  area = cover - cell->uncovered_area;
1616
1617
60.5M
  if (area != last_cover) {
1618
58.7M
      spans[num_spans].x = x;
1619
58.7M
      spans[num_spans].coverage = GRID_AREA_TO_ALPHA (area);
1620
58.7M
      last_cover = area;
1621
58.7M
      last_x = x;
1622
58.7M
      ++num_spans;
1623
58.7M
  }
1624
1625
60.5M
  prev_x = x+1;
1626
60.5M
    }
1627
1628
12.7M
    if (prev_x <= xmax && cover != last_cover) {
1629
12.3M
  spans[num_spans].x = prev_x;
1630
12.3M
  spans[num_spans].coverage = GRID_AREA_TO_ALPHA (cover);
1631
12.3M
  last_cover = cover;
1632
12.3M
  last_x = prev_x;
1633
12.3M
  ++num_spans;
1634
12.3M
    }
1635
1636
12.7M
    if (last_x < xmax && last_cover) {
1637
123k
  spans[num_spans].x = xmax;
1638
123k
  spans[num_spans].coverage = 0;
1639
123k
  ++num_spans;
1640
123k
    }
1641
1642
    /* Dump them into the renderer. */
1643
12.7M
    return renderer->render_rows (renderer, y, height, spans, num_spans);
1644
12.7M
}
1645
1646
0
#define GRID_AREA_TO_A1(A)  ((GRID_AREA_TO_ALPHA (A) > 127) ? 255 : 0)
1647
static glitter_status_t
1648
blit_a1 (struct cell_list *cells,
1649
   cairo_span_renderer_t *renderer,
1650
   cairo_half_open_span_t *spans,
1651
   int y, int height,
1652
   int xmin, int xmax)
1653
0
{
1654
0
    struct cell *cell = cells->head.next;
1655
0
    int prev_x = xmin, last_x = -1;
1656
0
    int16_t cover = 0;
1657
0
    uint8_t coverage, last_cover = 0;
1658
0
    unsigned num_spans;
1659
1660
0
    if (cell == &cells->tail)
1661
0
  return CAIRO_STATUS_SUCCESS;
1662
1663
    /* Skip cells to the left of the clip region. */
1664
0
    while (cell->x < xmin) {
1665
0
  cover += cell->covered_height;
1666
0
  cell = cell->next;
1667
0
    }
1668
0
    cover *= GRID_X*2;
1669
1670
    /* Form the spans from the coverages and areas. */
1671
0
    num_spans = 0;
1672
0
    for (; cell->x < xmax; cell = cell->next) {
1673
0
  int x = cell->x;
1674
0
  int16_t area;
1675
1676
0
  coverage = GRID_AREA_TO_A1 (cover);
1677
0
  if (x > prev_x && coverage != last_cover) {
1678
0
      last_x = spans[num_spans].x = prev_x;
1679
0
      last_cover = spans[num_spans].coverage = coverage;
1680
0
      ++num_spans;
1681
0
  }
1682
1683
0
  cover += cell->covered_height*GRID_X*2;
1684
0
  area = cover - cell->uncovered_area;
1685
1686
0
  coverage = GRID_AREA_TO_A1 (area);
1687
0
  if (coverage != last_cover) {
1688
0
      last_x = spans[num_spans].x = x;
1689
0
      last_cover = spans[num_spans].coverage = coverage;
1690
0
      ++num_spans;
1691
0
  }
1692
1693
0
  prev_x = x+1;
1694
0
    }
1695
1696
0
    coverage = GRID_AREA_TO_A1 (cover);
1697
0
    if (prev_x <= xmax && coverage != last_cover) {
1698
0
  last_x = spans[num_spans].x = prev_x;
1699
0
  last_cover = spans[num_spans].coverage = coverage;
1700
0
  ++num_spans;
1701
0
    }
1702
1703
0
    if (last_x < xmax && last_cover) {
1704
0
  spans[num_spans].x = xmax;
1705
0
  spans[num_spans].coverage = 0;
1706
0
  ++num_spans;
1707
0
    }
1708
0
    if (num_spans == 1)
1709
0
  return CAIRO_STATUS_SUCCESS;
1710
1711
    /* Dump them into the renderer. */
1712
0
    return renderer->render_rows (renderer, y, height, spans, num_spans);
1713
0
}
1714
1715
1716
I void
1717
glitter_scan_converter_render(glitter_scan_converter_t *converter,
1718
            unsigned int winding_mask,
1719
            int antialias,
1720
            cairo_span_renderer_t *renderer)
1721
182k
{
1722
182k
    int i, j;
1723
182k
    int ymax_i = converter->ymax / GRID_Y;
1724
182k
    int ymin_i = converter->ymin / GRID_Y;
1725
182k
    int xmin_i, xmax_i;
1726
182k
    int h = ymax_i - ymin_i;
1727
182k
    struct polygon *polygon = converter->polygon;
1728
182k
    struct cell_list *coverages = converter->coverages;
1729
182k
    struct active_list *active = converter->active;
1730
182k
    struct edge *buckets[GRID_Y] = { 0 };
1731
1732
182k
    xmin_i = converter->xmin / GRID_X;
1733
182k
    xmax_i = converter->xmax / GRID_X;
1734
182k
    if (xmin_i >= xmax_i)
1735
0
  return;
1736
1737
    /* Render each pixel row. */
1738
13.0M
    for (i = 0; i < h; i = j) {
1739
12.8M
  int do_full_row = 0;
1740
1741
12.8M
  j = i + 1;
1742
1743
  /* Determine if we can ignore this row or use the full pixel
1744
   * stepper. */
1745
12.8M
  if (polygon_fill_buckets (active,
1746
12.8M
          polygon->y_buckets[i],
1747
12.8M
          (i+ymin_i)*GRID_Y,
1748
12.8M
          buckets) == 0) {
1749
9.72M
      if (buckets[0]) {
1750
215k
    active_list_merge_edges_from_bucket (active, buckets[0]);
1751
215k
    buckets[0] = NULL;
1752
215k
      }
1753
1754
9.72M
      if (active->head.next == &active->tail) {
1755
103k
    active->min_height = INT_MAX;
1756
103k
    active->is_vertical = 1;
1757
3.09M
    for (; j < h && ! polygon->y_buckets[j]; j++)
1758
2.99M
        ;
1759
103k
    continue;
1760
103k
      }
1761
1762
9.62M
      do_full_row = can_do_full_row (active);
1763
9.62M
  }
1764
1765
12.7M
  if (do_full_row) {
1766
      /* Step by a full pixel row's worth. */
1767
9.50M
      full_row (active, coverages, winding_mask);
1768
1769
9.50M
      if (active->is_vertical) {
1770
4.35M
    while (j < h &&
1771
4.34M
           polygon->y_buckets[j] == NULL &&
1772
4.26M
           active->min_height >= 2*GRID_Y)
1773
4.25M
    {
1774
4.25M
        active->min_height -= GRID_Y;
1775
4.25M
        j++;
1776
4.25M
    }
1777
99.7k
    if (j != i + 1)
1778
90.0k
        step_edges (active, j - (i + 1));
1779
99.7k
      }
1780
9.50M
  } else {
1781
3.22M
      int sub;
1782
1783
      /* Subsample this row. */
1784
51.5M
      for (sub = 0; sub < GRID_Y; sub++) {
1785
48.3M
    if (buckets[sub]) {
1786
4.12M
        active_list_merge_edges_from_bucket (active, buckets[sub]);
1787
4.12M
        buckets[sub] = NULL;
1788
4.12M
    }
1789
48.3M
    sub_row (active, coverages, winding_mask);
1790
48.3M
      }
1791
3.22M
  }
1792
1793
12.7M
  if (antialias)
1794
12.7M
      blit_a8 (coverages, renderer, converter->spans,
1795
12.7M
         i+ymin_i, j-i, xmin_i, xmax_i);
1796
0
  else
1797
0
      blit_a1 (coverages, renderer, converter->spans,
1798
0
         i+ymin_i, j-i, xmin_i, xmax_i);
1799
12.7M
  cell_list_reset (coverages);
1800
1801
12.7M
  active->min_height -= GRID_Y;
1802
12.7M
    }
1803
182k
}
1804
1805
struct _cairo_tor_scan_converter {
1806
    cairo_scan_converter_t base;
1807
1808
    glitter_scan_converter_t converter[1];
1809
    cairo_fill_rule_t fill_rule;
1810
    cairo_antialias_t antialias;
1811
1812
    jmp_buf jmp;
1813
};
1814
1815
typedef struct _cairo_tor_scan_converter cairo_tor_scan_converter_t;
1816
1817
static void
1818
_cairo_tor_scan_converter_destroy (void *converter)
1819
182k
{
1820
182k
    cairo_tor_scan_converter_t *self = converter;
1821
182k
    if (self == NULL) {
1822
0
  return;
1823
0
    }
1824
182k
    _glitter_scan_converter_fini (self->converter);
1825
182k
    free(self);
1826
182k
}
1827
1828
cairo_status_t
1829
_cairo_tor_scan_converter_add_polygon (void   *converter,
1830
               const cairo_polygon_t *polygon)
1831
182k
{
1832
182k
    cairo_tor_scan_converter_t *self = converter;
1833
182k
    cairo_status_t status;
1834
182k
    int i;
1835
1836
#if 0
1837
    FILE *file = fopen ("polygon.txt", "w");
1838
    _cairo_debug_print_polygon (file, polygon);
1839
    fclose (file);
1840
#endif
1841
1842
182k
    if ((status = setjmp (self->jmp)))
1843
0
  return _cairo_scan_converter_set_error (self, _cairo_error (status));
1844
1845
12.7M
    for (i = 0; i < polygon->num_edges; i++)
1846
12.6M
   glitter_scan_converter_add_edge (self->converter, &polygon->edges[i]);
1847
1848
182k
    return CAIRO_STATUS_SUCCESS;
1849
182k
}
1850
1851
static cairo_status_t
1852
_cairo_tor_scan_converter_generate (void      *converter,
1853
            cairo_span_renderer_t *renderer)
1854
182k
{
1855
182k
    cairo_tor_scan_converter_t *self = converter;
1856
182k
    cairo_status_t status;
1857
1858
182k
    if ((status = setjmp (self->jmp)))
1859
0
  return _cairo_scan_converter_set_error (self, _cairo_error (status));
1860
1861
182k
    glitter_scan_converter_render (self->converter,
1862
182k
           self->fill_rule == CAIRO_FILL_RULE_WINDING ? ~0 : 1,
1863
182k
           self->antialias != CAIRO_ANTIALIAS_NONE,
1864
182k
           renderer);
1865
182k
    return CAIRO_STATUS_SUCCESS;
1866
182k
}
1867
1868
cairo_scan_converter_t *
1869
_cairo_tor_scan_converter_create (int     xmin,
1870
          int     ymin,
1871
          int     xmax,
1872
          int     ymax,
1873
          cairo_fill_rule_t fill_rule,
1874
          cairo_antialias_t antialias)
1875
182k
{
1876
182k
    cairo_tor_scan_converter_t *self;
1877
182k
    cairo_status_t status;
1878
1879
182k
    self = _cairo_calloc (sizeof(struct _cairo_tor_scan_converter));
1880
182k
    if (unlikely (self == NULL)) {
1881
0
  status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
1882
0
  goto bail_nomem;
1883
0
    }
1884
1885
182k
    self->base.destroy = _cairo_tor_scan_converter_destroy;
1886
182k
    self->base.generate = _cairo_tor_scan_converter_generate;
1887
1888
182k
    _glitter_scan_converter_init (self->converter, &self->jmp);
1889
182k
    status = glitter_scan_converter_reset (self->converter,
1890
182k
             xmin, ymin, xmax, ymax);
1891
182k
    if (unlikely (status))
1892
0
  goto bail;
1893
1894
182k
    self->fill_rule = fill_rule;
1895
182k
    self->antialias = antialias;
1896
1897
182k
    return &self->base;
1898
1899
0
 bail:
1900
0
    self->base.destroy(&self->base);
1901
0
 bail_nomem:
1902
0
    return _cairo_scan_converter_create_in_error (status);
1903
0
}