Coverage Report

Created: 2025-11-16 09:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/workdir/UnpackedTarball/cairo/src/cairo-mono-scan-converter.c
Line
Count
Source
1
/* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2
/*
3
 * Copyright (c) 2011  Intel Corporation
4
 *
5
 * Permission is hereby granted, free of charge, to any person
6
 * obtaining a copy of this software and associated documentation
7
 * files (the "Software"), to deal in the Software without
8
 * restriction, including without limitation the rights to use,
9
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the
11
 * Software is furnished to do so, subject to the following
12
 * conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be
15
 * included in all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24
 * OTHER DEALINGS IN THE SOFTWARE.
25
 */
26
#include "cairoint.h"
27
#include "cairo-spans-private.h"
28
#include "cairo-error-private.h"
29
30
#include <stdlib.h>
31
#include <string.h>
32
#include <limits.h>
33
34
struct quorem {
35
    int32_t quo;
36
    int32_t rem;
37
};
38
39
struct edge {
40
    struct edge *next, *prev;
41
42
    int32_t height_left;
43
    int32_t dir;
44
    int32_t vertical;
45
46
    int32_t dy;
47
    struct quorem x;
48
    struct quorem dxdy;
49
};
50
51
/* A collection of sorted and vertically clipped edges of the polygon.
52
 * Edges are moved from the polygon to an active list while scan
53
 * converting. */
54
struct polygon {
55
    /* The vertical clip extents. */
56
    int32_t ymin, ymax;
57
58
    int num_edges;
59
    struct edge *edges;
60
61
    /* Array of edges all starting in the same bucket.  An edge is put
62
     * into bucket EDGE_BUCKET_INDEX(edge->ytop, polygon->ymin) when
63
     * it is added to the polygon. */
64
    struct edge **y_buckets;
65
66
    struct edge *y_buckets_embedded[64];
67
    struct edge edges_embedded[32];
68
};
69
70
struct mono_scan_converter {
71
    struct polygon polygon[1];
72
73
    /* Leftmost edge on the current scan line. */
74
    struct edge head, tail;
75
    int is_vertical;
76
77
    cairo_half_open_span_t *spans;
78
    cairo_half_open_span_t spans_embedded[64];
79
    int num_spans;
80
81
    /* Clip box. */
82
    int32_t xmin, xmax;
83
    int32_t ymin, ymax;
84
};
85
86
6.87M
#define I(x) _cairo_fixed_integer_round_down(x)
87
88
/* Compute the floored division (x*a)/b. Assumes / and % perform symmetric
89
 * division. */
90
static struct quorem
91
floored_muldivrem(int x, int a, int b)
92
21.8k
{
93
21.8k
    struct quorem qr;
94
21.8k
    long long xa = (long long)x*a;
95
21.8k
    qr.quo = xa/b;
96
21.8k
    qr.rem = xa%b;
97
21.8k
    if ((xa>=0) != (b>=0) && qr.rem) {
98
10.8k
  qr.quo -= 1;
99
10.8k
  qr.rem += b;
100
10.8k
    }
101
21.8k
    return qr;
102
21.8k
}
103
104
static cairo_status_t
105
polygon_init (struct polygon *polygon, int ymin, int ymax)
106
184k
{
107
184k
    unsigned h = ymax - ymin + 1;
108
109
184k
    polygon->y_buckets = polygon->y_buckets_embedded;
110
184k
    if (h > ARRAY_LENGTH (polygon->y_buckets_embedded)) {
111
0
  polygon->y_buckets = _cairo_malloc_ab (h, sizeof (struct edge *));
112
0
  if (unlikely (NULL == polygon->y_buckets))
113
0
      return _cairo_error (CAIRO_STATUS_NO_MEMORY);
114
0
    }
115
184k
    memset (polygon->y_buckets, 0, h * sizeof (struct edge *));
116
184k
    polygon->y_buckets[h-1] = (void *)-1;
117
118
184k
    polygon->ymin = ymin;
119
184k
    polygon->ymax = ymax;
120
184k
    return CAIRO_STATUS_SUCCESS;
121
184k
}
122
123
static void
124
polygon_fini (struct polygon *polygon)
125
184k
{
126
184k
    if (polygon->y_buckets != polygon->y_buckets_embedded)
127
0
  free (polygon->y_buckets);
128
129
184k
    if (polygon->edges != polygon->edges_embedded)
130
189
  free (polygon->edges);
131
184k
}
132
133
static void
134
_polygon_insert_edge_into_its_y_bucket(struct polygon *polygon,
135
               struct edge *e,
136
               int y)
137
1.93M
{
138
1.93M
    struct edge **ptail = &polygon->y_buckets[y - polygon->ymin];
139
1.93M
    if (*ptail)
140
1.74M
  (*ptail)->prev = e;
141
1.93M
    e->next = *ptail;
142
1.93M
    e->prev = NULL;
143
1.93M
    *ptail = e;
144
1.93M
}
145
146
inline static void
147
polygon_add_edge (struct polygon *polygon,
148
      const cairo_edge_t *edge)
149
1.99M
{
150
1.99M
    struct edge *e;
151
1.99M
    cairo_fixed_t dx;
152
1.99M
    cairo_fixed_t dy;
153
1.99M
    int y, ytop, ybot;
154
1.99M
    int ymin = polygon->ymin;
155
1.99M
    int ymax = polygon->ymax;
156
157
1.99M
    y = I(edge->top);
158
1.99M
    ytop = MAX(y, ymin);
159
160
1.99M
    y = I(edge->bottom);
161
1.99M
    ybot = MIN(y, ymax);
162
163
1.99M
    if (ybot <= ytop)
164
57.6k
  return;
165
166
1.93M
    e = polygon->edges + polygon->num_edges++;
167
1.93M
    e->height_left = ybot - ytop;
168
1.93M
    e->dir = edge->dir;
169
170
1.93M
    dx = edge->line.p2.x - edge->line.p1.x;
171
1.93M
    dy = edge->line.p2.y - edge->line.p1.y;
172
173
1.93M
    if (dx == 0) {
174
1.92M
  e->vertical = TRUE;
175
1.92M
  e->x.quo = edge->line.p1.x;
176
1.92M
  e->x.rem = 0;
177
1.92M
  e->dxdy.quo = 0;
178
1.92M
  e->dxdy.rem = 0;
179
1.92M
  e->dy = 0;
180
1.92M
    } else {
181
10.9k
  e->vertical = FALSE;
182
10.9k
  e->dxdy = floored_muldivrem (dx, CAIRO_FIXED_ONE, dy);
183
10.9k
  e->dy = dy;
184
185
10.9k
  e->x = floored_muldivrem (ytop * CAIRO_FIXED_ONE + CAIRO_FIXED_FRAC_MASK/2 - edge->line.p1.y,
186
10.9k
          dx, dy);
187
10.9k
  e->x.quo += edge->line.p1.x;
188
10.9k
    }
189
1.93M
    e->x.rem -= dy;
190
191
1.93M
    _polygon_insert_edge_into_its_y_bucket (polygon, e, ytop);
192
1.93M
}
193
194
static struct edge *
195
merge_sorted_edges (struct edge *head_a, struct edge *head_b)
196
966k
{
197
966k
    struct edge *head, **next, *prev;
198
966k
    int32_t x;
199
200
966k
    prev = head_a->prev;
201
966k
    next = &head;
202
966k
    if (head_a->x.quo <= head_b->x.quo) {
203
773k
  head = head_a;
204
773k
    } else {
205
193k
  head = head_b;
206
193k
  head_b->prev = prev;
207
193k
  goto start_with_b;
208
193k
    }
209
210
775k
    do {
211
775k
  x = head_b->x.quo;
212
7.92M
  while (head_a != NULL && head_a->x.quo <= x) {
213
7.15M
      prev = head_a;
214
7.15M
      next = &head_a->next;
215
7.15M
      head_a = head_a->next;
216
7.15M
  }
217
218
775k
  head_b->prev = prev;
219
775k
  *next = head_b;
220
775k
  if (head_a == NULL)
221
596k
      return head;
222
223
372k
start_with_b:
224
372k
  x = head_a->x.quo;
225
5.20M
  while (head_b != NULL && head_b->x.quo <= x) {
226
4.82M
      prev = head_b;
227
4.82M
      next = &head_b->next;
228
4.82M
      head_b = head_b->next;
229
4.82M
  }
230
231
372k
  head_a->prev = prev;
232
372k
  *next = head_a;
233
372k
  if (head_b == NULL)
234
369k
      return head;
235
372k
    } while (1);
236
773k
}
237
238
static struct edge *
239
sort_edges (struct edge *list,
240
      unsigned int level,
241
      struct edge **head_out)
242
966k
{
243
966k
    struct edge *head_other, *remaining;
244
966k
    unsigned int i;
245
246
966k
    head_other = list->next;
247
248
966k
    if (head_other == NULL) {
249
0
  *head_out = list;
250
0
  return NULL;
251
0
    }
252
253
966k
    remaining = head_other->next;
254
966k
    if (list->x.quo <= head_other->x.quo) {
255
875k
  *head_out = list;
256
875k
  head_other->next = NULL;
257
875k
    } else {
258
91.4k
  *head_out = head_other;
259
91.4k
  head_other->prev = list->prev;
260
91.4k
  head_other->next = list;
261
91.4k
  list->prev = head_other;
262
91.4k
  list->next = NULL;
263
91.4k
    }
264
265
1.74M
    for (i = 0; i < level && remaining; i++) {
266
781k
  remaining = sort_edges (remaining, i, &head_other);
267
781k
  *head_out = merge_sorted_edges (*head_out, head_other);
268
781k
    }
269
270
966k
    return remaining;
271
966k
}
272
273
static struct edge *
274
merge_unsorted_edges (struct edge *head, struct edge *unsorted)
275
184k
{
276
184k
    sort_edges (unsorted, UINT_MAX, &unsorted);
277
184k
    return merge_sorted_edges (head, unsorted);
278
184k
}
279
280
inline static void
281
active_list_merge_edges (struct mono_scan_converter *c, struct edge *edges)
282
184k
{
283
184k
    struct edge *e;
284
285
1.16M
    for (e = edges; c->is_vertical && e; e = e->next)
286
983k
  c->is_vertical = e->vertical;
287
288
184k
    c->head.next = merge_unsorted_edges (c->head.next, edges);
289
184k
}
290
291
inline static void
292
add_span (struct mono_scan_converter *c, int x1, int x2)
293
184k
{
294
184k
    int n;
295
296
184k
    if (x1 < c->xmin)
297
215
  x1 = c->xmin;
298
184k
    if (x2 > c->xmax)
299
35
  x2 = c->xmax;
300
184k
    if (x2 <= x1)
301
2.16k
  return;
302
303
182k
    n = c->num_spans++;
304
182k
    c->spans[n].x = x1;
305
182k
    c->spans[n].coverage = 255;
306
307
182k
    n = c->num_spans++;
308
182k
    c->spans[n].x = x2;
309
182k
    c->spans[n].coverage = 0;
310
182k
}
311
312
inline static void
313
row (struct mono_scan_converter *c, unsigned int mask)
314
184k
{
315
184k
    struct edge *edge = c->head.next;
316
184k
    int xstart = INT_MIN, prev_x = INT_MIN;
317
184k
    int winding = 0;
318
319
184k
    c->num_spans = 0;
320
2.11M
    while (&c->tail != edge) {
321
1.93M
  struct edge *next = edge->next;
322
1.93M
  int xend = I(edge->x.quo);
323
324
1.93M
  if (--edge->height_left) {
325
0
      if (!edge->vertical) {
326
0
    edge->x.quo += edge->dxdy.quo;
327
0
    edge->x.rem += edge->dxdy.rem;
328
0
    if (edge->x.rem >= 0) {
329
0
        ++edge->x.quo;
330
0
        edge->x.rem -= edge->dy;
331
0
    }
332
0
      }
333
334
0
      if (edge->x.quo < prev_x) {
335
0
    struct edge *pos = edge->prev;
336
0
    pos->next = next;
337
0
    next->prev = pos;
338
0
    do {
339
0
        pos = pos->prev;
340
0
    } while (edge->x.quo < pos->x.quo);
341
0
    pos->next->prev = edge;
342
0
    edge->next = pos->next;
343
0
    edge->prev = pos;
344
0
    pos->next = edge;
345
0
      } else
346
0
    prev_x = edge->x.quo;
347
1.93M
  } else {
348
1.93M
      edge->prev->next = next;
349
1.93M
      next->prev = edge->prev;
350
1.93M
  }
351
352
1.93M
  winding += edge->dir;
353
1.93M
  if ((winding & mask) == 0) {
354
963k
      if (I(next->x.quo) > xend + 1) {
355
184k
    add_span (c, xstart, xend);
356
184k
    xstart = INT_MIN;
357
184k
      }
358
969k
  } else if (xstart == INT_MIN)
359
184k
      xstart = xend;
360
361
1.93M
  edge = next;
362
1.93M
    }
363
184k
}
364
365
static cairo_status_t
366
_mono_scan_converter_init(struct mono_scan_converter *c,
367
        int xmin, int ymin,
368
        int xmax, int ymax)
369
184k
{
370
184k
    cairo_status_t status;
371
184k
    int max_num_spans;
372
373
184k
    status = polygon_init (c->polygon, ymin, ymax);
374
184k
    if  (unlikely (status))
375
0
  return status;
376
377
184k
    max_num_spans = xmax - xmin + 1;
378
184k
    if (max_num_spans > ARRAY_LENGTH(c->spans_embedded)) {
379
0
  c->spans = _cairo_malloc_ab (max_num_spans,
380
0
             sizeof (cairo_half_open_span_t));
381
0
  if (unlikely (c->spans == NULL)) {
382
0
      return _cairo_error (CAIRO_STATUS_NO_MEMORY);
383
0
  }
384
0
    } else
385
184k
  c->spans = c->spans_embedded;
386
387
184k
    c->xmin = xmin;
388
184k
    c->xmax = xmax;
389
184k
    c->ymin = ymin;
390
184k
    c->ymax = ymax;
391
392
184k
    c->head.vertical = 1;
393
184k
    c->head.height_left = INT_MAX;
394
184k
    c->head.x.quo = _cairo_fixed_from_int (_cairo_fixed_integer_part (INT_MIN));
395
184k
    c->head.prev = NULL;
396
184k
    c->head.next = &c->tail;
397
184k
    c->tail.prev = &c->head;
398
184k
    c->tail.next = NULL;
399
184k
    c->tail.x.quo = _cairo_fixed_from_int (_cairo_fixed_integer_part (INT_MAX));
400
184k
    c->tail.height_left = INT_MAX;
401
184k
    c->tail.vertical = 1;
402
403
184k
    c->is_vertical = 1;
404
184k
    return CAIRO_STATUS_SUCCESS;
405
184k
}
406
407
static void
408
_mono_scan_converter_fini(struct mono_scan_converter *self)
409
184k
{
410
184k
    if (self->spans != self->spans_embedded)
411
0
  free (self->spans);
412
413
184k
    polygon_fini(self->polygon);
414
184k
}
415
416
static cairo_status_t
417
mono_scan_converter_allocate_edges(struct mono_scan_converter *c,
418
           int num_edges)
419
420
184k
{
421
184k
    c->polygon->num_edges = 0;
422
184k
    c->polygon->edges = c->polygon->edges_embedded;
423
184k
    if (num_edges > ARRAY_LENGTH (c->polygon->edges_embedded)) {
424
189
  c->polygon->edges = _cairo_malloc_ab (num_edges, sizeof (struct edge));
425
189
  if (unlikely (c->polygon->edges == NULL))
426
0
      return _cairo_error (CAIRO_STATUS_NO_MEMORY);
427
189
    }
428
429
184k
    return CAIRO_STATUS_SUCCESS;
430
184k
}
431
432
static void
433
mono_scan_converter_add_edge (struct mono_scan_converter *c,
434
            const cairo_edge_t *edge)
435
1.99M
{
436
1.99M
    polygon_add_edge (c->polygon, edge);
437
1.99M
}
438
439
static void
440
step_edges (struct mono_scan_converter *c, int count)
441
0
{
442
0
    struct edge *edge;
443
444
0
    for (edge = c->head.next; edge != &c->tail; edge = edge->next) {
445
0
  edge->height_left -= count;
446
0
  if (! edge->height_left) {
447
0
      edge->prev->next = edge->next;
448
0
      edge->next->prev = edge->prev;
449
0
  }
450
0
    }
451
0
}
452
453
static cairo_status_t
454
mono_scan_converter_render(struct mono_scan_converter *c,
455
         unsigned int winding_mask,
456
         cairo_span_renderer_t *renderer)
457
184k
{
458
184k
    struct polygon *polygon = c->polygon;
459
184k
    int i, j, h = c->ymax - c->ymin;
460
184k
    cairo_status_t status;
461
462
369k
    for (i = 0; i < h; i = j) {
463
184k
  j = i + 1;
464
465
184k
  if (polygon->y_buckets[i])
466
184k
      active_list_merge_edges (c, polygon->y_buckets[i]);
467
468
184k
  if (c->is_vertical) {
469
175k
      int min_height;
470
175k
      struct edge *e;
471
472
175k
      e = c->head.next;
473
175k
      min_height = e->height_left;
474
912k
      while (e != &c->tail) {
475
736k
    if (e->height_left < min_height)
476
0
        min_height = e->height_left;
477
736k
    e = e->next;
478
736k
      }
479
480
175k
      while (--min_height >= 1 && polygon->y_buckets[j] == NULL)
481
0
    j++;
482
175k
      if (j != i + 1)
483
0
    step_edges (c, j - (i + 1));
484
175k
  }
485
486
184k
  row (c, winding_mask);
487
184k
  if (c->num_spans) {
488
182k
      status = renderer->render_rows (renderer, c->ymin+i, j-i,
489
182k
              c->spans, c->num_spans);
490
182k
      if (unlikely (status))
491
0
    return status;
492
182k
  }
493
494
  /* XXX recompute after dropping edges? */
495
184k
  if (c->head.next == &c->tail)
496
184k
      c->is_vertical = 1;
497
184k
    }
498
499
184k
    return CAIRO_STATUS_SUCCESS;
500
184k
}
501
502
struct _cairo_mono_scan_converter {
503
    cairo_scan_converter_t base;
504
505
    struct mono_scan_converter converter[1];
506
    cairo_fill_rule_t fill_rule;
507
};
508
509
typedef struct _cairo_mono_scan_converter cairo_mono_scan_converter_t;
510
511
static void
512
_cairo_mono_scan_converter_destroy (void *converter)
513
184k
{
514
184k
    cairo_mono_scan_converter_t *self = converter;
515
184k
    _mono_scan_converter_fini (self->converter);
516
184k
    free(self);
517
184k
}
518
519
cairo_status_t
520
_cairo_mono_scan_converter_add_polygon (void    *converter,
521
               const cairo_polygon_t *polygon)
522
184k
{
523
184k
    cairo_mono_scan_converter_t *self = converter;
524
184k
    cairo_status_t status;
525
184k
    int i;
526
527
#if 0
528
    FILE *file = fopen ("polygon.txt", "w");
529
    _cairo_debug_print_polygon (file, polygon);
530
    fclose (file);
531
#endif
532
533
184k
    status = mono_scan_converter_allocate_edges (self->converter,
534
184k
             polygon->num_edges);
535
184k
    if (unlikely (status))
536
0
  return status;
537
538
2.17M
    for (i = 0; i < polygon->num_edges; i++)
539
1.99M
   mono_scan_converter_add_edge (self->converter, &polygon->edges[i]);
540
541
184k
    return CAIRO_STATUS_SUCCESS;
542
184k
}
543
544
static cairo_status_t
545
_cairo_mono_scan_converter_generate (void     *converter,
546
            cairo_span_renderer_t *renderer)
547
184k
{
548
184k
    cairo_mono_scan_converter_t *self = converter;
549
550
184k
    return mono_scan_converter_render (self->converter,
551
184k
               self->fill_rule == CAIRO_FILL_RULE_WINDING ? ~0 : 1,
552
184k
               renderer);
553
184k
}
554
555
cairo_scan_converter_t *
556
_cairo_mono_scan_converter_create (int      xmin,
557
          int     ymin,
558
          int     xmax,
559
          int     ymax,
560
          cairo_fill_rule_t fill_rule)
561
184k
{
562
184k
    cairo_mono_scan_converter_t *self;
563
184k
    cairo_status_t status;
564
565
184k
    self = _cairo_calloc (sizeof(struct _cairo_mono_scan_converter));
566
184k
    if (unlikely (self == NULL)) {
567
0
  status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
568
0
  goto bail_nomem;
569
0
    }
570
571
184k
    self->base.destroy = _cairo_mono_scan_converter_destroy;
572
184k
    self->base.generate = _cairo_mono_scan_converter_generate;
573
574
184k
    status = _mono_scan_converter_init (self->converter,
575
184k
          xmin, ymin, xmax, ymax);
576
184k
    if (unlikely (status))
577
0
  goto bail;
578
579
184k
    self->fill_rule = fill_rule;
580
581
184k
    return &self->base;
582
583
0
 bail:
584
0
    self->base.destroy(&self->base);
585
0
 bail_nomem:
586
0
    return _cairo_scan_converter_create_in_error (status);
587
0
}