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
#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
242004
floored_muldivrem(int x, int a, int b)
92
{
93
    struct quorem qr;
94
242004
    long long xa = (long long)x*a;
95
242004
    qr.quo = xa/b;
96
242004
    qr.rem = xa%b;
97
242004
    if ((xa>=0) != (b>=0) && qr.rem) {
98
27525
	qr.quo -= 1;
99
27525
	qr.rem += b;
100
    }
101
242004
    return qr;
102
}
103

            
104
static cairo_status_t
105
786
polygon_init (struct polygon *polygon, int ymin, int ymax)
106
{
107
786
    unsigned h = ymax - ymin + 1;
108

            
109
786
    polygon->y_buckets = polygon->y_buckets_embedded;
110
786
    if (h > ARRAY_LENGTH (polygon->y_buckets_embedded)) {
111
156
	polygon->y_buckets = _cairo_malloc_ab (h, sizeof (struct edge *));
112
156
	if (unlikely (NULL == polygon->y_buckets))
113
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
114
    }
115
786
    memset (polygon->y_buckets, 0, h * sizeof (struct edge *));
116
786
    polygon->y_buckets[h-1] = (void *)-1;
117

            
118
786
    polygon->ymin = ymin;
119
786
    polygon->ymax = ymax;
120
786
    return CAIRO_STATUS_SUCCESS;
121
}
122

            
123
static void
124
786
polygon_fini (struct polygon *polygon)
125
{
126
786
    if (polygon->y_buckets != polygon->y_buckets_embedded)
127
156
	free (polygon->y_buckets);
128

            
129
786
    if (polygon->edges != polygon->edges_embedded)
130
294
	free (polygon->edges);
131
786
}
132

            
133
static void
134
174375
_polygon_insert_edge_into_its_y_bucket(struct polygon *polygon,
135
				       struct edge *e,
136
				       int y)
137
{
138
174375
    struct edge **ptail = &polygon->y_buckets[y - polygon->ymin];
139
174375
    if (*ptail)
140
156603
	(*ptail)->prev = e;
141
174375
    e->next = *ptail;
142
174375
    e->prev = NULL;
143
174375
    *ptail = e;
144
174375
}
145

            
146
inline static void
147
347103
polygon_add_edge (struct polygon *polygon,
148
		  const cairo_edge_t *edge)
149
{
150
    struct edge *e;
151
    cairo_fixed_t dx;
152
    cairo_fixed_t dy;
153
    int y, ytop, ybot;
154
347103
    int ymin = polygon->ymin;
155
347103
    int ymax = polygon->ymax;
156

            
157
347103
    y = I(edge->top);
158
347103
    ytop = MAX(y, ymin);
159

            
160
347103
    y = I(edge->bottom);
161
347103
    ybot = MIN(y, ymax);
162

            
163
347103
    if (ybot <= ytop)
164
172728
	return;
165

            
166
174375
    e = polygon->edges + polygon->num_edges++;
167
174375
    e->height_left = ybot - ytop;
168
174375
    e->dir = edge->dir;
169

            
170
174375
    dx = edge->line.p2.x - edge->line.p1.x;
171
174375
    dy = edge->line.p2.y - edge->line.p1.y;
172

            
173
174375
    if (dx == 0) {
174
53373
	e->vertical = TRUE;
175
53373
	e->x.quo = edge->line.p1.x;
176
53373
	e->x.rem = 0;
177
53373
	e->dxdy.quo = 0;
178
53373
	e->dxdy.rem = 0;
179
53373
	e->dy = 0;
180
    } else {
181
121002
	e->vertical = FALSE;
182
121002
	e->dxdy = floored_muldivrem (dx, CAIRO_FIXED_ONE, dy);
183
121002
	e->dy = dy;
184

            
185
121002
	e->x = floored_muldivrem (ytop * CAIRO_FIXED_ONE + CAIRO_FIXED_FRAC_MASK/2 - edge->line.p1.y,
186
				  dx, dy);
187
121002
	e->x.quo += edge->line.p1.x;
188
    }
189
174375
    e->x.rem -= dy;
190

            
191
174375
    _polygon_insert_edge_into_its_y_bucket (polygon, e, ytop);
192
}
193

            
194
static struct edge *
195
92016
merge_sorted_edges (struct edge *head_a, struct edge *head_b)
196
{
197
    struct edge *head, **next, *prev;
198
    int32_t x;
199

            
200
92016
    prev = head_a->prev;
201
92016
    next = &head;
202
92016
    if (head_a->x.quo <= head_b->x.quo) {
203
36744
	head = head_a;
204
    } else {
205
55272
	head = head_b;
206
55272
	head_b->prev = prev;
207
55272
	goto start_with_b;
208
    }
209

            
210
    do {
211
46989
	x = head_b->x.quo;
212
156075
	while (head_a != NULL && head_a->x.quo <= x) {
213
109086
	    prev = head_a;
214
109086
	    next = &head_a->next;
215
109086
	    head_a = head_a->next;
216
	}
217

            
218
46989
	head_b->prev = prev;
219
46989
	*next = head_b;
220
46989
	if (head_a == NULL)
221
30465
	    return head;
222

            
223
16524
start_with_b:
224
71796
	x = head_a->x.quo;
225
629367
	while (head_b != NULL && head_b->x.quo <= x) {
226
557571
	    prev = head_b;
227
557571
	    next = &head_b->next;
228
557571
	    head_b = head_b->next;
229
	}
230

            
231
71796
	head_a->prev = prev;
232
71796
	*next = head_a;
233
71796
	if (head_b == NULL)
234
61551
	    return head;
235
    } while (1);
236
}
237

            
238
static struct edge *
239
92016
sort_edges (struct edge *list,
240
	    unsigned int level,
241
	    struct edge **head_out)
242
{
243
    struct edge *head_other, *remaining;
244
    unsigned int i;
245

            
246
92016
    head_other = list->next;
247

            
248
92016
    if (head_other == NULL) {
249
9657
	*head_out = list;
250
9657
	return NULL;
251
    }
252

            
253
82359
    remaining = head_other->next;
254
82359
    if (list->x.quo <= head_other->x.quo) {
255
6987
	*head_out = list;
256
6987
	head_other->next = NULL;
257
    } else {
258
75372
	*head_out = head_other;
259
75372
	head_other->prev = list->prev;
260
75372
	head_other->next = list;
261
75372
	list->prev = head_other;
262
75372
	list->next = NULL;
263
    }
264

            
265
156603
    for (i = 0; i < level && remaining; i++) {
266
74244
	remaining = sort_edges (remaining, i, &head_other);
267
74244
	*head_out = merge_sorted_edges (*head_out, head_other);
268
    }
269

            
270
82359
    return remaining;
271
}
272

            
273
static struct edge *
274
17772
merge_unsorted_edges (struct edge *head, struct edge *unsorted)
275
{
276
17772
    sort_edges (unsorted, UINT_MAX, &unsorted);
277
17772
    return merge_sorted_edges (head, unsorted);
278
}
279

            
280
inline static void
281
17772
active_list_merge_edges (struct mono_scan_converter *c, struct edge *edges)
282
{
283
    struct edge *e;
284

            
285
24504
    for (e = edges; c->is_vertical && e; e = e->next)
286
6732
	c->is_vertical = e->vertical;
287

            
288
17772
    c->head.next = merge_unsorted_edges (c->head.next, edges);
289
17772
}
290

            
291
inline static void
292
78480
add_span (struct mono_scan_converter *c, int x1, int x2)
293
{
294
    int n;
295

            
296
78480
    if (x1 < c->xmin)
297
	x1 = c->xmin;
298
78480
    if (x2 > c->xmax)
299
	x2 = c->xmax;
300
78480
    if (x2 <= x1)
301
13434
	return;
302

            
303
65046
    n = c->num_spans++;
304
65046
    c->spans[n].x = x1;
305
65046
    c->spans[n].coverage = 255;
306

            
307
65046
    n = c->num_spans++;
308
65046
    c->spans[n].x = x2;
309
65046
    c->spans[n].coverage = 0;
310
}
311

            
312
inline static void
313
31380
row (struct mono_scan_converter *c, unsigned int mask)
314
{
315
31380
    struct edge *edge = c->head.next;
316
31380
    int xstart = INT_MIN, prev_x = INT_MIN;
317
31380
    int winding = 0;
318

            
319
31380
    c->num_spans = 0;
320
259620
    while (&c->tail != edge) {
321
228240
	struct edge *next = edge->next;
322
228240
	int xend = I(edge->x.quo);
323

            
324
228240
	if (--edge->height_left) {
325
53865
	    if (!edge->vertical) {
326
50634
		edge->x.quo += edge->dxdy.quo;
327
50634
		edge->x.rem += edge->dxdy.rem;
328
50634
		if (edge->x.rem >= 0) {
329
20091
		    ++edge->x.quo;
330
20091
		    edge->x.rem -= edge->dy;
331
		}
332
	    }
333

            
334
53865
	    if (edge->x.quo < prev_x) {
335
75
		struct edge *pos = edge->prev;
336
75
		pos->next = next;
337
75
		next->prev = pos;
338
		do {
339
75
		    pos = pos->prev;
340
75
		} while (edge->x.quo < pos->x.quo);
341
75
		pos->next->prev = edge;
342
75
		edge->next = pos->next;
343
75
		edge->prev = pos;
344
75
		pos->next = edge;
345
	    } else
346
53790
		prev_x = edge->x.quo;
347
	} else {
348
174375
	    edge->prev->next = next;
349
174375
	    next->prev = edge->prev;
350
	}
351

            
352
228240
	winding += edge->dir;
353
228240
	if ((winding & mask) == 0) {
354
112572
	    if (I(next->x.quo) > xend + 1) {
355
78480
		add_span (c, xstart, xend);
356
78480
		xstart = INT_MIN;
357
	    }
358
115668
	} else if (xstart == INT_MIN)
359
78480
	    xstart = xend;
360

            
361
228240
	edge = next;
362
    }
363
31380
}
364

            
365
static cairo_status_t
366
786
_mono_scan_converter_init(struct mono_scan_converter *c,
367
			  int xmin, int ymin,
368
			  int xmax, int ymax)
369
{
370
    cairo_status_t status;
371
    int max_num_spans;
372

            
373
786
    status = polygon_init (c->polygon, ymin, ymax);
374
786
    if  (unlikely (status))
375
	return status;
376

            
377
786
    max_num_spans = xmax - xmin + 1;
378
786
    if (max_num_spans > ARRAY_LENGTH(c->spans_embedded)) {
379
258
	c->spans = _cairo_malloc_ab (max_num_spans,
380
				     sizeof (cairo_half_open_span_t));
381
258
	if (unlikely (c->spans == NULL)) {
382
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
383
	}
384
    } else
385
528
	c->spans = c->spans_embedded;
386

            
387
786
    c->xmin = xmin;
388
786
    c->xmax = xmax;
389
786
    c->ymin = ymin;
390
786
    c->ymax = ymax;
391

            
392
786
    c->head.vertical = 1;
393
786
    c->head.height_left = INT_MAX;
394
786
    c->head.x.quo = _cairo_fixed_from_int (_cairo_fixed_integer_part (INT_MIN));
395
786
    c->head.prev = NULL;
396
786
    c->head.next = &c->tail;
397
786
    c->tail.prev = &c->head;
398
786
    c->tail.next = NULL;
399
786
    c->tail.x.quo = _cairo_fixed_from_int (_cairo_fixed_integer_part (INT_MAX));
400
786
    c->tail.height_left = INT_MAX;
401
786
    c->tail.vertical = 1;
402

            
403
786
    c->is_vertical = 1;
404
786
    return CAIRO_STATUS_SUCCESS;
405
}
406

            
407
static void
408
786
_mono_scan_converter_fini(struct mono_scan_converter *self)
409
{
410
786
    if (self->spans != self->spans_embedded)
411
258
	free (self->spans);
412

            
413
786
    polygon_fini(self->polygon);
414
786
}
415

            
416
static cairo_status_t
417
786
mono_scan_converter_allocate_edges(struct mono_scan_converter *c,
418
				   int num_edges)
419

            
420
{
421
786
    c->polygon->num_edges = 0;
422
786
    c->polygon->edges = c->polygon->edges_embedded;
423
786
    if (num_edges > ARRAY_LENGTH (c->polygon->edges_embedded)) {
424
294
	c->polygon->edges = _cairo_malloc_ab (num_edges, sizeof (struct edge));
425
294
	if (unlikely (c->polygon->edges == NULL))
426
	    return _cairo_error (CAIRO_STATUS_NO_MEMORY);
427
    }
428

            
429
786
    return CAIRO_STATUS_SUCCESS;
430
}
431

            
432
static void
433
347103
mono_scan_converter_add_edge (struct mono_scan_converter *c,
434
			      const cairo_edge_t *edge)
435
{
436
347103
    polygon_add_edge (c->polygon, edge);
437
347103
}
438

            
439
static void
440
72
step_edges (struct mono_scan_converter *c, int count)
441
{
442
    struct edge *edge;
443

            
444
222
    for (edge = c->head.next; edge != &c->tail; edge = edge->next) {
445
150
	edge->height_left -= count;
446
150
	if (! edge->height_left) {
447
	    edge->prev->next = edge->next;
448
	    edge->next->prev = edge->prev;
449
	}
450
    }
451
72
}
452

            
453
static cairo_status_t
454
786
mono_scan_converter_render(struct mono_scan_converter *c,
455
			   unsigned int winding_mask,
456
			   cairo_span_renderer_t *renderer)
457
{
458
786
    struct polygon *polygon = c->polygon;
459
786
    int i, j, h = c->ymax - c->ymin;
460
    cairo_status_t status;
461

            
462
32166
    for (i = 0; i < h; i = j) {
463
31380
	j = i + 1;
464

            
465
31380
	if (polygon->y_buckets[i])
466
17772
	    active_list_merge_edges (c, polygon->y_buckets[i]);
467

            
468
31380
	if (c->is_vertical) {
469
	    int min_height;
470
	    struct edge *e;
471

            
472
1428
	    e = c->head.next;
473
1428
	    min_height = e->height_left;
474
2364
	    while (e != &c->tail) {
475
936
		if (e->height_left < min_height)
476
18
		    min_height = e->height_left;
477
936
		e = e->next;
478
	    }
479

            
480
2064
	    while (--min_height >= 1 && polygon->y_buckets[j] == NULL)
481
636
		j++;
482
1428
	    if (j != i + 1)
483
72
		step_edges (c, j - (i + 1));
484
	}
485

            
486
31380
	row (c, winding_mask);
487
31380
	if (c->num_spans) {
488
29346
	    status = renderer->render_rows (renderer, c->ymin+i, j-i,
489
29346
					    c->spans, c->num_spans);
490
29346
	    if (unlikely (status))
491
		return status;
492
	}
493

            
494
	/* XXX recompute after dropping edges? */
495
31380
	if (c->head.next == &c->tail)
496
7119
	    c->is_vertical = 1;
497
    }
498

            
499
786
    return CAIRO_STATUS_SUCCESS;
500
}
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
786
_cairo_mono_scan_converter_destroy (void *converter)
513
{
514
786
    cairo_mono_scan_converter_t *self = converter;
515
786
    _mono_scan_converter_fini (self->converter);
516
786
    free(self);
517
786
}
518

            
519
cairo_status_t
520
786
_cairo_mono_scan_converter_add_polygon (void		*converter,
521
				       const cairo_polygon_t *polygon)
522
{
523
786
    cairo_mono_scan_converter_t *self = converter;
524
    cairo_status_t status;
525
    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
786
    status = mono_scan_converter_allocate_edges (self->converter,
534
786
						 polygon->num_edges);
535
786
    if (unlikely (status))
536
	return status;
537

            
538
347889
    for (i = 0; i < polygon->num_edges; i++)
539
347103
	 mono_scan_converter_add_edge (self->converter, &polygon->edges[i]);
540

            
541
786
    return CAIRO_STATUS_SUCCESS;
542
}
543

            
544
static cairo_status_t
545
786
_cairo_mono_scan_converter_generate (void			*converter,
546
				    cairo_span_renderer_t	*renderer)
547
{
548
786
    cairo_mono_scan_converter_t *self = converter;
549

            
550
786
    return mono_scan_converter_render (self->converter,
551
786
				       self->fill_rule == CAIRO_FILL_RULE_WINDING ? ~0 : 1,
552
				       renderer);
553
}
554

            
555
cairo_scan_converter_t *
556
786
_cairo_mono_scan_converter_create (int			xmin,
557
				  int			ymin,
558
				  int			xmax,
559
				  int			ymax,
560
				  cairo_fill_rule_t	fill_rule)
561
{
562
    cairo_mono_scan_converter_t *self;
563
    cairo_status_t status;
564

            
565
786
    self = _cairo_calloc (sizeof(struct _cairo_mono_scan_converter));
566
786
    if (unlikely (self == NULL)) {
567
	status = _cairo_error (CAIRO_STATUS_NO_MEMORY);
568
	goto bail_nomem;
569
    }
570

            
571
786
    self->base.destroy = _cairo_mono_scan_converter_destroy;
572
786
    self->base.generate = _cairo_mono_scan_converter_generate;
573

            
574
786
    status = _mono_scan_converter_init (self->converter,
575
					xmin, ymin, xmax, ymax);
576
786
    if (unlikely (status))
577
	goto bail;
578

            
579
786
    self->fill_rule = fill_rule;
580

            
581
786
    return &self->base;
582

            
583
 bail:
584
    self->base.destroy(&self->base);
585
 bail_nomem:
586
    return _cairo_scan_converter_create_in_error (status);
587
}