Rev 1892 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 1892 | Rev 3959 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright © 2004 Carl Worth |
2 | * Copyright © 2004 Carl Worth |
3 | * Copyright © 2006 Red Hat, Inc. |
3 | * Copyright © 2006 Red Hat, Inc. |
4 | * Copyright © 2007 David Turner |
4 | * Copyright © 2007 David Turner |
5 | * Copyright © 2008 M Joonas Pihlaja |
5 | * Copyright © 2008 M Joonas Pihlaja |
6 | * Copyright © 2008 Chris Wilson |
6 | * Copyright © 2008 Chris Wilson |
7 | * Copyright © 2009 Intel Corporation |
7 | * Copyright © 2009 Intel Corporation |
8 | * |
8 | * |
9 | * This library is free software; you can redistribute it and/or |
9 | * This library is free software; you can redistribute it and/or |
10 | * modify it either under the terms of the GNU Lesser General Public |
10 | * modify it either under the terms of the GNU Lesser General Public |
11 | * License version 2.1 as published by the Free Software Foundation |
11 | * License version 2.1 as published by the Free Software Foundation |
12 | * (the "LGPL") or, at your option, under the terms of the Mozilla |
12 | * (the "LGPL") or, at your option, under the terms of the Mozilla |
13 | * Public License Version 1.1 (the "MPL"). If you do not alter this |
13 | * Public License Version 1.1 (the "MPL"). If you do not alter this |
14 | * notice, a recipient may use your version of this file under either |
14 | * notice, a recipient may use your version of this file under either |
15 | * the MPL or the LGPL. |
15 | * the MPL or the LGPL. |
16 | * |
16 | * |
17 | * You should have received a copy of the LGPL along with this library |
17 | * You should have received a copy of the LGPL along with this library |
18 | * in the file COPYING-LGPL-2.1; if not, write to the Free Software |
18 | * in the file COPYING-LGPL-2.1; if not, write to the Free Software |
19 | * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA |
19 | * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA |
20 | * You should have received a copy of the MPL along with this library |
20 | * You should have received a copy of the MPL along with this library |
21 | * in the file COPYING-MPL-1.1 |
21 | * in the file COPYING-MPL-1.1 |
22 | * |
22 | * |
23 | * The contents of this file are subject to the Mozilla Public License |
23 | * The contents of this file are subject to the Mozilla Public License |
24 | * Version 1.1 (the "License"); you may not use this file except in |
24 | * Version 1.1 (the "License"); you may not use this file except in |
25 | * compliance with the License. You may obtain a copy of the License at |
25 | * compliance with the License. You may obtain a copy of the License at |
26 | * http://www.mozilla.org/MPL/ |
26 | * http://www.mozilla.org/MPL/ |
27 | * |
27 | * |
28 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY |
28 | * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY |
29 | * OF ANY KIND, either express or implied. See the LGPL or the MPL for |
29 | * OF ANY KIND, either express or implied. See the LGPL or the MPL for |
30 | * the specific language governing rights and limitations. |
30 | * the specific language governing rights and limitations. |
31 | * |
31 | * |
32 | * The Original Code is the cairo graphics library. |
32 | * The Original Code is the cairo graphics library. |
33 | * |
33 | * |
34 | * The Initial Developer of the Original Code is Carl Worth |
34 | * The Initial Developer of the Original Code is Carl Worth |
35 | * |
35 | * |
36 | * Contributor(s): |
36 | * Contributor(s): |
37 | * Carl D. Worth |
37 | * Carl D. Worth |
38 | * M Joonas Pihlaja |
38 | * M Joonas Pihlaja |
39 | * Chris Wilson |
39 | * Chris Wilson |
40 | */ |
40 | */ |
41 | 41 | ||
42 | /* Provide definitions for standalone compilation */ |
42 | /* Provide definitions for standalone compilation */ |
43 | #include "cairoint.h" |
43 | #include "cairoint.h" |
44 | 44 | ||
45 | #include "cairo-error-private.h" |
45 | #include "cairo-error-private.h" |
46 | #include "cairo-list-private.h" |
46 | #include "cairo-list-inline.h" |
47 | #include "cairo-freelist-private.h" |
47 | #include "cairo-freelist-private.h" |
48 | #include "cairo-combsort-private.h" |
48 | #include "cairo-combsort-inline.h" |
49 | 49 | ||
50 | #include |
50 | #include |
51 | 51 | ||
52 | #define STEP_X CAIRO_FIXED_ONE |
52 | #define STEP_X CAIRO_FIXED_ONE |
53 | #define STEP_Y CAIRO_FIXED_ONE |
53 | #define STEP_Y CAIRO_FIXED_ONE |
54 | #define UNROLL3(x) x x x |
54 | #define UNROLL3(x) x x x |
55 | 55 | ||
56 | #define STEP_XY (2*STEP_X*STEP_Y) /* Unit area in the step. */ |
56 | #define STEP_XY (2*STEP_X*STEP_Y) /* Unit area in the step. */ |
57 | #define AREA_TO_ALPHA(c) (((c)*255 + STEP_XY/2) / STEP_XY) |
57 | #define AREA_TO_ALPHA(c) (((c)*255 + STEP_XY/2) / STEP_XY) |
58 | 58 | ||
59 | typedef struct _cairo_bo_intersect_ordinate { |
59 | typedef struct _cairo_bo_intersect_ordinate { |
60 | int32_t ordinate; |
60 | int32_t ordinate; |
61 | enum { EXACT, INEXACT } exactness; |
61 | enum { EXACT, INEXACT } exactness; |
62 | } cairo_bo_intersect_ordinate_t; |
62 | } cairo_bo_intersect_ordinate_t; |
63 | 63 | ||
64 | typedef struct _cairo_bo_intersect_point { |
64 | typedef struct _cairo_bo_intersect_point { |
65 | cairo_bo_intersect_ordinate_t x; |
65 | cairo_bo_intersect_ordinate_t x; |
66 | cairo_bo_intersect_ordinate_t y; |
66 | cairo_bo_intersect_ordinate_t y; |
67 | } cairo_bo_intersect_point_t; |
67 | } cairo_bo_intersect_point_t; |
68 | 68 | ||
69 | struct quorem { |
69 | struct quorem { |
70 | cairo_fixed_t quo; |
70 | cairo_fixed_t quo; |
71 | cairo_fixed_t rem; |
71 | cairo_fixed_t rem; |
72 | }; |
72 | }; |
73 | 73 | ||
74 | struct run { |
74 | struct run { |
75 | struct run *next; |
75 | struct run *next; |
76 | int sign; |
76 | int sign; |
77 | cairo_fixed_t y; |
77 | cairo_fixed_t y; |
78 | }; |
78 | }; |
79 | 79 | ||
80 | typedef struct edge { |
80 | typedef struct edge { |
81 | cairo_list_t link; |
81 | cairo_list_t link; |
82 | 82 | ||
83 | cairo_edge_t edge; |
83 | cairo_edge_t edge; |
84 | 84 | ||
85 | /* Current x coordinate and advancement. |
85 | /* Current x coordinate and advancement. |
86 | * Initialised to the x coordinate of the top of the |
86 | * Initialised to the x coordinate of the top of the |
87 | * edge. The quotient is in cairo_fixed_t units and the |
87 | * edge. The quotient is in cairo_fixed_t units and the |
88 | * remainder is mod dy in cairo_fixed_t units. |
88 | * remainder is mod dy in cairo_fixed_t units. |
89 | */ |
89 | */ |
90 | cairo_fixed_t dy; |
90 | cairo_fixed_t dy; |
91 | struct quorem x; |
91 | struct quorem x; |
92 | struct quorem dxdy; |
92 | struct quorem dxdy; |
93 | struct quorem dxdy_full; |
93 | struct quorem dxdy_full; |
94 | 94 | ||
95 | cairo_bool_t vertical; |
95 | cairo_bool_t vertical; |
96 | unsigned int flags; |
96 | unsigned int flags; |
97 | 97 | ||
98 | int current_sign; |
98 | int current_sign; |
99 | struct run *runs; |
99 | struct run *runs; |
100 | } edge_t; |
100 | } edge_t; |
101 | 101 | ||
102 | enum { |
102 | enum { |
103 | START = 0x1, |
103 | START = 0x1, |
104 | STOP = 0x2, |
104 | STOP = 0x2, |
105 | }; |
105 | }; |
106 | 106 | ||
107 | /* the parent is always given by index/2 */ |
107 | /* the parent is always given by index/2 */ |
108 | #define PQ_PARENT_INDEX(i) ((i) >> 1) |
108 | #define PQ_PARENT_INDEX(i) ((i) >> 1) |
109 | #define PQ_FIRST_ENTRY 1 |
109 | #define PQ_FIRST_ENTRY 1 |
110 | 110 | ||
111 | /* left and right children are index * 2 and (index * 2) +1 respectively */ |
111 | /* left and right children are index * 2 and (index * 2) +1 respectively */ |
112 | #define PQ_LEFT_CHILD_INDEX(i) ((i) << 1) |
112 | #define PQ_LEFT_CHILD_INDEX(i) ((i) << 1) |
113 | 113 | ||
114 | typedef enum { |
114 | typedef enum { |
115 | EVENT_TYPE_STOP, |
115 | EVENT_TYPE_STOP, |
116 | EVENT_TYPE_INTERSECTION, |
116 | EVENT_TYPE_INTERSECTION, |
117 | EVENT_TYPE_START |
117 | EVENT_TYPE_START |
118 | } event_type_t; |
118 | } event_type_t; |
119 | 119 | ||
120 | typedef struct _event { |
120 | typedef struct _event { |
121 | cairo_fixed_t y; |
121 | cairo_fixed_t y; |
122 | event_type_t type; |
122 | event_type_t type; |
123 | } event_t; |
123 | } event_t; |
124 | 124 | ||
125 | typedef struct _start_event { |
125 | typedef struct _start_event { |
126 | cairo_fixed_t y; |
126 | cairo_fixed_t y; |
127 | event_type_t type; |
127 | event_type_t type; |
128 | edge_t *edge; |
128 | edge_t *edge; |
129 | } start_event_t; |
129 | } start_event_t; |
130 | 130 | ||
131 | typedef struct _queue_event { |
131 | typedef struct _queue_event { |
132 | cairo_fixed_t y; |
132 | cairo_fixed_t y; |
133 | event_type_t type; |
133 | event_type_t type; |
134 | edge_t *e1; |
134 | edge_t *e1; |
135 | edge_t *e2; |
135 | edge_t *e2; |
136 | } queue_event_t; |
136 | } queue_event_t; |
137 | 137 | ||
138 | typedef struct _pqueue { |
138 | typedef struct _pqueue { |
139 | int size, max_size; |
139 | int size, max_size; |
140 | 140 | ||
141 | event_t **elements; |
141 | event_t **elements; |
142 | event_t *elements_embedded[1024]; |
142 | event_t *elements_embedded[1024]; |
143 | } pqueue_t; |
143 | } pqueue_t; |
144 | 144 | ||
145 | struct cell { |
145 | struct cell { |
146 | struct cell *prev; |
146 | struct cell *prev; |
147 | struct cell *next; |
147 | struct cell *next; |
148 | int x; |
148 | int x; |
149 | int uncovered_area; |
149 | int uncovered_area; |
150 | int covered_height; |
150 | int covered_height; |
151 | }; |
151 | }; |
152 | 152 | ||
153 | typedef struct _sweep_line { |
153 | typedef struct _sweep_line { |
154 | cairo_list_t active; |
154 | cairo_list_t active; |
155 | cairo_list_t stopped; |
155 | cairo_list_t stopped; |
156 | cairo_list_t *insert_cursor; |
156 | cairo_list_t *insert_cursor; |
157 | cairo_bool_t is_vertical; |
157 | cairo_bool_t is_vertical; |
158 | 158 | ||
159 | cairo_fixed_t current_row; |
159 | cairo_fixed_t current_row; |
160 | cairo_fixed_t current_subrow; |
160 | cairo_fixed_t current_subrow; |
161 | 161 | ||
162 | struct coverage { |
162 | struct coverage { |
163 | struct cell head; |
163 | struct cell head; |
164 | struct cell tail; |
164 | struct cell tail; |
165 | 165 | ||
166 | struct cell *cursor; |
166 | struct cell *cursor; |
167 | int count; |
167 | int count; |
168 | 168 | ||
169 | cairo_freepool_t pool; |
169 | cairo_freepool_t pool; |
170 | } coverage; |
170 | } coverage; |
171 | 171 | ||
172 | struct event_queue { |
172 | struct event_queue { |
173 | pqueue_t pq; |
173 | pqueue_t pq; |
174 | event_t **start_events; |
174 | event_t **start_events; |
175 | 175 | ||
176 | cairo_freepool_t pool; |
176 | cairo_freepool_t pool; |
177 | } queue; |
177 | } queue; |
178 | 178 | ||
179 | cairo_freepool_t runs; |
179 | cairo_freepool_t runs; |
180 | 180 | ||
181 | jmp_buf unwind; |
181 | jmp_buf unwind; |
182 | } sweep_line_t; |
182 | } sweep_line_t; |
183 | 183 | ||
184 | cairo_always_inline static struct quorem |
184 | cairo_always_inline static struct quorem |
185 | floored_divrem (int a, int b) |
185 | floored_divrem (int a, int b) |
186 | { |
186 | { |
187 | struct quorem qr; |
187 | struct quorem qr; |
188 | qr.quo = a/b; |
188 | qr.quo = a/b; |
189 | qr.rem = a%b; |
189 | qr.rem = a%b; |
190 | if ((a^b)<0 && qr.rem) { |
190 | if ((a^b)<0 && qr.rem) { |
191 | qr.quo--; |
191 | qr.quo--; |
192 | qr.rem += b; |
192 | qr.rem += b; |
193 | } |
193 | } |
194 | return qr; |
194 | return qr; |
195 | } |
195 | } |
196 | 196 | ||
197 | static struct quorem |
197 | static struct quorem |
198 | floored_muldivrem(int x, int a, int b) |
198 | floored_muldivrem(int x, int a, int b) |
199 | { |
199 | { |
200 | struct quorem qr; |
200 | struct quorem qr; |
201 | long long xa = (long long)x*a; |
201 | long long xa = (long long)x*a; |
202 | qr.quo = xa/b; |
202 | qr.quo = xa/b; |
203 | qr.rem = xa%b; |
203 | qr.rem = xa%b; |
204 | if ((xa>=0) != (b>=0) && qr.rem) { |
204 | if ((xa>=0) != (b>=0) && qr.rem) { |
205 | qr.quo--; |
205 | qr.quo--; |
206 | qr.rem += b; |
206 | qr.rem += b; |
207 | } |
207 | } |
208 | return qr; |
208 | return qr; |
209 | } |
209 | } |
210 | 210 | ||
211 | static cairo_fixed_t |
211 | static cairo_fixed_t |
212 | line_compute_intersection_x_for_y (const cairo_line_t *line, |
212 | line_compute_intersection_x_for_y (const cairo_line_t *line, |
213 | cairo_fixed_t y) |
213 | cairo_fixed_t y) |
214 | { |
214 | { |
215 | cairo_fixed_t x, dy; |
215 | cairo_fixed_t x, dy; |
216 | 216 | ||
217 | if (y == line->p1.y) |
217 | if (y == line->p1.y) |
218 | return line->p1.x; |
218 | return line->p1.x; |
219 | if (y == line->p2.y) |
219 | if (y == line->p2.y) |
220 | return line->p2.x; |
220 | return line->p2.x; |
221 | 221 | ||
222 | x = line->p1.x; |
222 | x = line->p1.x; |
223 | dy = line->p2.y - line->p1.y; |
223 | dy = line->p2.y - line->p1.y; |
224 | if (dy != 0) { |
224 | if (dy != 0) { |
225 | x += _cairo_fixed_mul_div_floor (y - line->p1.y, |
225 | x += _cairo_fixed_mul_div_floor (y - line->p1.y, |
226 | line->p2.x - line->p1.x, |
226 | line->p2.x - line->p1.x, |
227 | dy); |
227 | dy); |
228 | } |
228 | } |
229 | 229 | ||
230 | return x; |
230 | return x; |
231 | } |
231 | } |
232 | 232 | ||
233 | /* |
233 | /* |
234 | * We need to compare the x-coordinates of a pair of lines for a particular y, |
234 | * We need to compare the x-coordinates of a pair of lines for a particular y, |
235 | * without loss of precision. |
235 | * without loss of precision. |
236 | * |
236 | * |
237 | * The x-coordinate along an edge for a given y is: |
237 | * The x-coordinate along an edge for a given y is: |
238 | * X = A_x + (Y - A_y) * A_dx / A_dy |
238 | * X = A_x + (Y - A_y) * A_dx / A_dy |
239 | * |
239 | * |
240 | * So the inequality we wish to test is: |
240 | * So the inequality we wish to test is: |
241 | * A_x + (Y - A_y) * A_dx / A_dy ∘ B_x + (Y - B_y) * B_dx / B_dy, |
241 | * A_x + (Y - A_y) * A_dx / A_dy ∘ B_x + (Y - B_y) * B_dx / B_dy, |
242 | * where ∘ is our inequality operator. |
242 | * where ∘ is our inequality operator. |
243 | * |
243 | * |
244 | * By construction, we know that A_dy and B_dy (and (Y - A_y), (Y - B_y)) are |
244 | * By construction, we know that A_dy and B_dy (and (Y - A_y), (Y - B_y)) are |
245 | * all positive, so we can rearrange it thus without causing a sign change: |
245 | * all positive, so we can rearrange it thus without causing a sign change: |
246 | * A_dy * B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx * A_dy |
246 | * A_dy * B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx * A_dy |
247 | * - (Y - A_y) * A_dx * B_dy |
247 | * - (Y - A_y) * A_dx * B_dy |
248 | * |
248 | * |
249 | * Given the assumption that all the deltas fit within 32 bits, we can compute |
249 | * Given the assumption that all the deltas fit within 32 bits, we can compute |
250 | * this comparison directly using 128 bit arithmetic. For certain, but common, |
250 | * this comparison directly using 128 bit arithmetic. For certain, but common, |
251 | * input we can reduce this down to a single 32 bit compare by inspecting the |
251 | * input we can reduce this down to a single 32 bit compare by inspecting the |
252 | * deltas. |
252 | * deltas. |
253 | * |
253 | * |
254 | * (And put the burden of the work on developing fast 128 bit ops, which are |
254 | * (And put the burden of the work on developing fast 128 bit ops, which are |
255 | * required throughout the tessellator.) |
255 | * required throughout the tessellator.) |
256 | * |
256 | * |
257 | * See the similar discussion for _slope_compare(). |
257 | * See the similar discussion for _slope_compare(). |
258 | */ |
258 | */ |
259 | static int |
259 | static int |
260 | edges_compare_x_for_y_general (const cairo_edge_t *a, |
260 | edges_compare_x_for_y_general (const cairo_edge_t *a, |
261 | const cairo_edge_t *b, |
261 | const cairo_edge_t *b, |
262 | int32_t y) |
262 | int32_t y) |
263 | { |
263 | { |
264 | /* XXX: We're assuming here that dx and dy will still fit in 32 |
264 | /* XXX: We're assuming here that dx and dy will still fit in 32 |
265 | * bits. That's not true in general as there could be overflow. We |
265 | * bits. That's not true in general as there could be overflow. We |
266 | * should prevent that before the tessellation algorithm |
266 | * should prevent that before the tessellation algorithm |
267 | * begins. |
267 | * begins. |
268 | */ |
268 | */ |
269 | int32_t dx; |
269 | int32_t dx; |
270 | int32_t adx, ady; |
270 | int32_t adx, ady; |
271 | int32_t bdx, bdy; |
271 | int32_t bdx, bdy; |
272 | enum { |
272 | enum { |
273 | HAVE_NONE = 0x0, |
273 | HAVE_NONE = 0x0, |
274 | HAVE_DX = 0x1, |
274 | HAVE_DX = 0x1, |
275 | HAVE_ADX = 0x2, |
275 | HAVE_ADX = 0x2, |
276 | HAVE_DX_ADX = HAVE_DX | HAVE_ADX, |
276 | HAVE_DX_ADX = HAVE_DX | HAVE_ADX, |
277 | HAVE_BDX = 0x4, |
277 | HAVE_BDX = 0x4, |
278 | HAVE_DX_BDX = HAVE_DX | HAVE_BDX, |
278 | HAVE_DX_BDX = HAVE_DX | HAVE_BDX, |
279 | HAVE_ADX_BDX = HAVE_ADX | HAVE_BDX, |
279 | HAVE_ADX_BDX = HAVE_ADX | HAVE_BDX, |
280 | HAVE_ALL = HAVE_DX | HAVE_ADX | HAVE_BDX |
280 | HAVE_ALL = HAVE_DX | HAVE_ADX | HAVE_BDX |
281 | } have_dx_adx_bdx = HAVE_ALL; |
281 | } have_dx_adx_bdx = HAVE_ALL; |
282 | 282 | ||
283 | /* don't bother solving for abscissa if the edges' bounding boxes |
283 | /* don't bother solving for abscissa if the edges' bounding boxes |
284 | * can be used to order them. */ |
284 | * can be used to order them. */ |
285 | { |
285 | { |
286 | int32_t amin, amax; |
286 | int32_t amin, amax; |
287 | int32_t bmin, bmax; |
287 | int32_t bmin, bmax; |
288 | if (a->line.p1.x < a->line.p2.x) { |
288 | if (a->line.p1.x < a->line.p2.x) { |
289 | amin = a->line.p1.x; |
289 | amin = a->line.p1.x; |
290 | amax = a->line.p2.x; |
290 | amax = a->line.p2.x; |
291 | } else { |
291 | } else { |
292 | amin = a->line.p2.x; |
292 | amin = a->line.p2.x; |
293 | amax = a->line.p1.x; |
293 | amax = a->line.p1.x; |
294 | } |
294 | } |
295 | if (b->line.p1.x < b->line.p2.x) { |
295 | if (b->line.p1.x < b->line.p2.x) { |
296 | bmin = b->line.p1.x; |
296 | bmin = b->line.p1.x; |
297 | bmax = b->line.p2.x; |
297 | bmax = b->line.p2.x; |
298 | } else { |
298 | } else { |
299 | bmin = b->line.p2.x; |
299 | bmin = b->line.p2.x; |
300 | bmax = b->line.p1.x; |
300 | bmax = b->line.p1.x; |
301 | } |
301 | } |
302 | if (amax < bmin) return -1; |
302 | if (amax < bmin) return -1; |
303 | if (amin > bmax) return +1; |
303 | if (amin > bmax) return +1; |
304 | } |
304 | } |
305 | 305 | ||
306 | ady = a->line.p2.y - a->line.p1.y; |
306 | ady = a->line.p2.y - a->line.p1.y; |
307 | adx = a->line.p2.x - a->line.p1.x; |
307 | adx = a->line.p2.x - a->line.p1.x; |
308 | if (adx == 0) |
308 | if (adx == 0) |
309 | have_dx_adx_bdx &= ~HAVE_ADX; |
309 | have_dx_adx_bdx &= ~HAVE_ADX; |
310 | 310 | ||
311 | bdy = b->line.p2.y - b->line.p1.y; |
311 | bdy = b->line.p2.y - b->line.p1.y; |
312 | bdx = b->line.p2.x - b->line.p1.x; |
312 | bdx = b->line.p2.x - b->line.p1.x; |
313 | if (bdx == 0) |
313 | if (bdx == 0) |
314 | have_dx_adx_bdx &= ~HAVE_BDX; |
314 | have_dx_adx_bdx &= ~HAVE_BDX; |
315 | 315 | ||
316 | dx = a->line.p1.x - b->line.p1.x; |
316 | dx = a->line.p1.x - b->line.p1.x; |
317 | if (dx == 0) |
317 | if (dx == 0) |
318 | have_dx_adx_bdx &= ~HAVE_DX; |
318 | have_dx_adx_bdx &= ~HAVE_DX; |
319 | 319 | ||
320 | #define L _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (ady, bdy), dx) |
320 | #define L _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (ady, bdy), dx) |
321 | #define A _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (adx, bdy), y - a->line.p1.y) |
321 | #define A _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (adx, bdy), y - a->line.p1.y) |
322 | #define B _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (bdx, ady), y - b->line.p1.y) |
322 | #define B _cairo_int64x32_128_mul (_cairo_int32x32_64_mul (bdx, ady), y - b->line.p1.y) |
323 | switch (have_dx_adx_bdx) { |
323 | switch (have_dx_adx_bdx) { |
324 | default: |
324 | default: |
325 | case HAVE_NONE: |
325 | case HAVE_NONE: |
326 | return 0; |
326 | return 0; |
327 | case HAVE_DX: |
327 | case HAVE_DX: |
328 | /* A_dy * B_dy * (A_x - B_x) ∘ 0 */ |
328 | /* A_dy * B_dy * (A_x - B_x) ∘ 0 */ |
329 | return dx; /* ady * bdy is positive definite */ |
329 | return dx; /* ady * bdy is positive definite */ |
330 | case HAVE_ADX: |
330 | case HAVE_ADX: |
331 | /* 0 ∘ - (Y - A_y) * A_dx * B_dy */ |
331 | /* 0 ∘ - (Y - A_y) * A_dx * B_dy */ |
332 | return adx; /* bdy * (y - a->top.y) is positive definite */ |
332 | return adx; /* bdy * (y - a->top.y) is positive definite */ |
333 | case HAVE_BDX: |
333 | case HAVE_BDX: |
334 | /* 0 ∘ (Y - B_y) * B_dx * A_dy */ |
334 | /* 0 ∘ (Y - B_y) * B_dx * A_dy */ |
335 | return -bdx; /* ady * (y - b->top.y) is positive definite */ |
335 | return -bdx; /* ady * (y - b->top.y) is positive definite */ |
336 | case HAVE_ADX_BDX: |
336 | case HAVE_ADX_BDX: |
337 | /* 0 ∘ (Y - B_y) * B_dx * A_dy - (Y - A_y) * A_dx * B_dy */ |
337 | /* 0 ∘ (Y - B_y) * B_dx * A_dy - (Y - A_y) * A_dx * B_dy */ |
338 | if ((adx ^ bdx) < 0) { |
338 | if ((adx ^ bdx) < 0) { |
339 | return adx; |
339 | return adx; |
340 | } else if (a->line.p1.y == b->line.p1.y) { /* common origin */ |
340 | } else if (a->line.p1.y == b->line.p1.y) { /* common origin */ |
341 | cairo_int64_t adx_bdy, bdx_ady; |
341 | cairo_int64_t adx_bdy, bdx_ady; |
342 | 342 | ||
343 | /* ∴ A_dx * B_dy ∘ B_dx * A_dy */ |
343 | /* ∴ A_dx * B_dy ∘ B_dx * A_dy */ |
344 | 344 | ||
345 | adx_bdy = _cairo_int32x32_64_mul (adx, bdy); |
345 | adx_bdy = _cairo_int32x32_64_mul (adx, bdy); |
346 | bdx_ady = _cairo_int32x32_64_mul (bdx, ady); |
346 | bdx_ady = _cairo_int32x32_64_mul (bdx, ady); |
347 | 347 | ||
348 | return _cairo_int64_cmp (adx_bdy, bdx_ady); |
348 | return _cairo_int64_cmp (adx_bdy, bdx_ady); |
349 | } else |
349 | } else |
350 | return _cairo_int128_cmp (A, B); |
350 | return _cairo_int128_cmp (A, B); |
351 | case HAVE_DX_ADX: |
351 | case HAVE_DX_ADX: |
352 | /* A_dy * (A_x - B_x) ∘ - (Y - A_y) * A_dx */ |
352 | /* A_dy * (A_x - B_x) ∘ - (Y - A_y) * A_dx */ |
353 | if ((-adx ^ dx) < 0) { |
353 | if ((-adx ^ dx) < 0) { |
354 | return dx; |
354 | return dx; |
355 | } else { |
355 | } else { |
356 | cairo_int64_t ady_dx, dy_adx; |
356 | cairo_int64_t ady_dx, dy_adx; |
357 | 357 | ||
358 | ady_dx = _cairo_int32x32_64_mul (ady, dx); |
358 | ady_dx = _cairo_int32x32_64_mul (ady, dx); |
359 | dy_adx = _cairo_int32x32_64_mul (a->line.p1.y - y, adx); |
359 | dy_adx = _cairo_int32x32_64_mul (a->line.p1.y - y, adx); |
360 | 360 | ||
361 | return _cairo_int64_cmp (ady_dx, dy_adx); |
361 | return _cairo_int64_cmp (ady_dx, dy_adx); |
362 | } |
362 | } |
363 | case HAVE_DX_BDX: |
363 | case HAVE_DX_BDX: |
364 | /* B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx */ |
364 | /* B_dy * (A_x - B_x) ∘ (Y - B_y) * B_dx */ |
365 | if ((bdx ^ dx) < 0) { |
365 | if ((bdx ^ dx) < 0) { |
366 | return dx; |
366 | return dx; |
367 | } else { |
367 | } else { |
368 | cairo_int64_t bdy_dx, dy_bdx; |
368 | cairo_int64_t bdy_dx, dy_bdx; |
369 | 369 | ||
370 | bdy_dx = _cairo_int32x32_64_mul (bdy, dx); |
370 | bdy_dx = _cairo_int32x32_64_mul (bdy, dx); |
371 | dy_bdx = _cairo_int32x32_64_mul (y - b->line.p1.y, bdx); |
371 | dy_bdx = _cairo_int32x32_64_mul (y - b->line.p1.y, bdx); |
372 | 372 | ||
373 | return _cairo_int64_cmp (bdy_dx, dy_bdx); |
373 | return _cairo_int64_cmp (bdy_dx, dy_bdx); |
374 | } |
374 | } |
375 | case HAVE_ALL: |
375 | case HAVE_ALL: |
376 | /* XXX try comparing (a->line.p2.x - b->line.p2.x) et al */ |
376 | /* XXX try comparing (a->line.p2.x - b->line.p2.x) et al */ |
377 | return _cairo_int128_cmp (L, _cairo_int128_sub (B, A)); |
377 | return _cairo_int128_cmp (L, _cairo_int128_sub (B, A)); |
378 | } |
378 | } |
379 | #undef B |
379 | #undef B |
380 | #undef A |
380 | #undef A |
381 | #undef L |
381 | #undef L |
382 | } |
382 | } |
383 | 383 | ||
384 | /* |
384 | /* |
385 | * We need to compare the x-coordinate of a line for a particular y wrt to a |
385 | * We need to compare the x-coordinate of a line for a particular y wrt to a |
386 | * given x, without loss of precision. |
386 | * given x, without loss of precision. |
387 | * |
387 | * |
388 | * The x-coordinate along an edge for a given y is: |
388 | * The x-coordinate along an edge for a given y is: |
389 | * X = A_x + (Y - A_y) * A_dx / A_dy |
389 | * X = A_x + (Y - A_y) * A_dx / A_dy |
390 | * |
390 | * |
391 | * So the inequality we wish to test is: |
391 | * So the inequality we wish to test is: |
392 | * A_x + (Y - A_y) * A_dx / A_dy ∘ X |
392 | * A_x + (Y - A_y) * A_dx / A_dy ∘ X |
393 | * where ∘ is our inequality operator. |
393 | * where ∘ is our inequality operator. |
394 | * |
394 | * |
395 | * By construction, we know that A_dy (and (Y - A_y)) are |
395 | * By construction, we know that A_dy (and (Y - A_y)) are |
396 | * all positive, so we can rearrange it thus without causing a sign change: |
396 | * all positive, so we can rearrange it thus without causing a sign change: |
397 | * (Y - A_y) * A_dx ∘ (X - A_x) * A_dy |
397 | * (Y - A_y) * A_dx ∘ (X - A_x) * A_dy |
398 | * |
398 | * |
399 | * Given the assumption that all the deltas fit within 32 bits, we can compute |
399 | * Given the assumption that all the deltas fit within 32 bits, we can compute |
400 | * this comparison directly using 64 bit arithmetic. |
400 | * this comparison directly using 64 bit arithmetic. |
401 | * |
401 | * |
402 | * See the similar discussion for _slope_compare() and |
402 | * See the similar discussion for _slope_compare() and |
403 | * edges_compare_x_for_y_general(). |
403 | * edges_compare_x_for_y_general(). |
404 | */ |
404 | */ |
405 | static int |
405 | static int |
406 | edge_compare_for_y_against_x (const cairo_edge_t *a, |
406 | edge_compare_for_y_against_x (const cairo_edge_t *a, |
407 | int32_t y, |
407 | int32_t y, |
408 | int32_t x) |
408 | int32_t x) |
409 | { |
409 | { |
410 | int32_t adx, ady; |
410 | int32_t adx, ady; |
411 | int32_t dx, dy; |
411 | int32_t dx, dy; |
412 | cairo_int64_t L, R; |
412 | cairo_int64_t L, R; |
413 | 413 | ||
414 | if (a->line.p1.x <= a->line.p2.x) { |
414 | if (a->line.p1.x <= a->line.p2.x) { |
415 | if (x < a->line.p1.x) |
415 | if (x < a->line.p1.x) |
416 | return 1; |
416 | return 1; |
417 | if (x > a->line.p2.x) |
417 | if (x > a->line.p2.x) |
418 | return -1; |
418 | return -1; |
419 | } else { |
419 | } else { |
420 | if (x < a->line.p2.x) |
420 | if (x < a->line.p2.x) |
421 | return 1; |
421 | return 1; |
422 | if (x > a->line.p1.x) |
422 | if (x > a->line.p1.x) |
423 | return -1; |
423 | return -1; |
424 | } |
424 | } |
425 | 425 | ||
426 | adx = a->line.p2.x - a->line.p1.x; |
426 | adx = a->line.p2.x - a->line.p1.x; |
427 | dx = x - a->line.p1.x; |
427 | dx = x - a->line.p1.x; |
428 | 428 | ||
429 | if (adx == 0) |
429 | if (adx == 0) |
430 | return -dx; |
430 | return -dx; |
431 | if (dx == 0 || (adx ^ dx) < 0) |
431 | if (dx == 0 || (adx ^ dx) < 0) |
432 | return adx; |
432 | return adx; |
433 | 433 | ||
434 | dy = y - a->line.p1.y; |
434 | dy = y - a->line.p1.y; |
435 | ady = a->line.p2.y - a->line.p1.y; |
435 | ady = a->line.p2.y - a->line.p1.y; |
436 | 436 | ||
437 | L = _cairo_int32x32_64_mul (dy, adx); |
437 | L = _cairo_int32x32_64_mul (dy, adx); |
438 | R = _cairo_int32x32_64_mul (dx, ady); |
438 | R = _cairo_int32x32_64_mul (dx, ady); |
439 | 439 | ||
440 | return _cairo_int64_cmp (L, R); |
440 | return _cairo_int64_cmp (L, R); |
441 | } |
441 | } |
442 | 442 | ||
443 | static int |
443 | static int |
444 | edges_compare_x_for_y (const cairo_edge_t *a, |
444 | edges_compare_x_for_y (const cairo_edge_t *a, |
445 | const cairo_edge_t *b, |
445 | const cairo_edge_t *b, |
446 | int32_t y) |
446 | int32_t y) |
447 | { |
447 | { |
448 | /* If the sweep-line is currently on an end-point of a line, |
448 | /* If the sweep-line is currently on an end-point of a line, |
449 | * then we know its precise x value (and considering that we often need to |
449 | * then we know its precise x value (and considering that we often need to |
450 | * compare events at end-points, this happens frequently enough to warrant |
450 | * compare events at end-points, this happens frequently enough to warrant |
451 | * special casing). |
451 | * special casing). |
452 | */ |
452 | */ |
453 | enum { |
453 | enum { |
454 | HAVE_NEITHER = 0x0, |
454 | HAVE_NEITHER = 0x0, |
455 | HAVE_AX = 0x1, |
455 | HAVE_AX = 0x1, |
456 | HAVE_BX = 0x2, |
456 | HAVE_BX = 0x2, |
457 | HAVE_BOTH = HAVE_AX | HAVE_BX |
457 | HAVE_BOTH = HAVE_AX | HAVE_BX |
458 | } have_ax_bx = HAVE_BOTH; |
458 | } have_ax_bx = HAVE_BOTH; |
459 | int32_t ax, bx; |
459 | int32_t ax, bx; |
460 | 460 | ||
461 | /* XXX given we have x and dx? */ |
461 | /* XXX given we have x and dx? */ |
462 | 462 | ||
463 | if (y == a->line.p1.y) |
463 | if (y == a->line.p1.y) |
464 | ax = a->line.p1.x; |
464 | ax = a->line.p1.x; |
465 | else if (y == a->line.p2.y) |
465 | else if (y == a->line.p2.y) |
466 | ax = a->line.p2.x; |
466 | ax = a->line.p2.x; |
467 | else |
467 | else |
468 | have_ax_bx &= ~HAVE_AX; |
468 | have_ax_bx &= ~HAVE_AX; |
469 | 469 | ||
470 | if (y == b->line.p1.y) |
470 | if (y == b->line.p1.y) |
471 | bx = b->line.p1.x; |
471 | bx = b->line.p1.x; |
472 | else if (y == b->line.p2.y) |
472 | else if (y == b->line.p2.y) |
473 | bx = b->line.p2.x; |
473 | bx = b->line.p2.x; |
474 | else |
474 | else |
475 | have_ax_bx &= ~HAVE_BX; |
475 | have_ax_bx &= ~HAVE_BX; |
476 | 476 | ||
477 | switch (have_ax_bx) { |
477 | switch (have_ax_bx) { |
478 | default: |
478 | default: |
479 | case HAVE_NEITHER: |
479 | case HAVE_NEITHER: |
480 | return edges_compare_x_for_y_general (a, b, y); |
480 | return edges_compare_x_for_y_general (a, b, y); |
481 | case HAVE_AX: |
481 | case HAVE_AX: |
482 | return -edge_compare_for_y_against_x (b, y, ax); |
482 | return -edge_compare_for_y_against_x (b, y, ax); |
483 | case HAVE_BX: |
483 | case HAVE_BX: |
484 | return edge_compare_for_y_against_x (a, y, bx); |
484 | return edge_compare_for_y_against_x (a, y, bx); |
485 | case HAVE_BOTH: |
485 | case HAVE_BOTH: |
486 | return ax - bx; |
486 | return ax - bx; |
487 | } |
487 | } |
488 | } |
488 | } |
489 | 489 | ||
490 | static inline int |
490 | static inline int |
491 | slope_compare (const edge_t *a, |
491 | slope_compare (const edge_t *a, |
492 | const edge_t *b) |
492 | const edge_t *b) |
493 | { |
493 | { |
494 | cairo_int64_t L, R; |
494 | cairo_int64_t L, R; |
495 | int cmp; |
495 | int cmp; |
496 | 496 | ||
497 | cmp = a->dxdy.quo - b->dxdy.quo; |
497 | cmp = a->dxdy.quo - b->dxdy.quo; |
498 | if (cmp) |
498 | if (cmp) |
499 | return cmp; |
499 | return cmp; |
500 | 500 | ||
501 | if (a->dxdy.rem == 0) |
501 | if (a->dxdy.rem == 0) |
502 | return -b->dxdy.rem; |
502 | return -b->dxdy.rem; |
503 | if (b->dxdy.rem == 0) |
503 | if (b->dxdy.rem == 0) |
504 | return a->dxdy.rem; |
504 | return a->dxdy.rem; |
505 | 505 | ||
506 | L = _cairo_int32x32_64_mul (b->dy, a->dxdy.rem); |
506 | L = _cairo_int32x32_64_mul (b->dy, a->dxdy.rem); |
507 | R = _cairo_int32x32_64_mul (a->dy, b->dxdy.rem); |
507 | R = _cairo_int32x32_64_mul (a->dy, b->dxdy.rem); |
508 | return _cairo_int64_cmp (L, R); |
508 | return _cairo_int64_cmp (L, R); |
509 | } |
509 | } |
510 | 510 | ||
511 | static inline int |
511 | static inline int |
512 | line_equal (const cairo_line_t *a, const cairo_line_t *b) |
512 | line_equal (const cairo_line_t *a, const cairo_line_t *b) |
513 | { |
513 | { |
514 | return a->p1.x == b->p1.x && a->p1.y == b->p1.y && |
514 | return a->p1.x == b->p1.x && a->p1.y == b->p1.y && |
515 | a->p2.x == b->p2.x && a->p2.y == b->p2.y; |
515 | a->p2.x == b->p2.x && a->p2.y == b->p2.y; |
516 | } |
516 | } |
517 | 517 | ||
518 | static inline int |
518 | static inline int |
519 | sweep_line_compare_edges (const edge_t *a, |
519 | sweep_line_compare_edges (const edge_t *a, |
520 | const edge_t *b, |
520 | const edge_t *b, |
521 | cairo_fixed_t y) |
521 | cairo_fixed_t y) |
522 | { |
522 | { |
523 | int cmp; |
523 | int cmp; |
524 | 524 | ||
525 | if (line_equal (&a->edge.line, &b->edge.line)) |
525 | if (line_equal (&a->edge.line, &b->edge.line)) |
526 | return 0; |
526 | return 0; |
527 | 527 | ||
528 | cmp = edges_compare_x_for_y (&a->edge, &b->edge, y); |
528 | cmp = edges_compare_x_for_y (&a->edge, &b->edge, y); |
529 | if (cmp) |
529 | if (cmp) |
530 | return cmp; |
530 | return cmp; |
531 | 531 | ||
532 | return slope_compare (a, b); |
532 | return slope_compare (a, b); |
533 | } |
533 | } |
534 | 534 | ||
535 | static inline cairo_int64_t |
535 | static inline cairo_int64_t |
536 | det32_64 (int32_t a, int32_t b, |
536 | det32_64 (int32_t a, int32_t b, |
537 | int32_t c, int32_t d) |
537 | int32_t c, int32_t d) |
538 | { |
538 | { |
539 | /* det = a * d - b * c */ |
539 | /* det = a * d - b * c */ |
540 | return _cairo_int64_sub (_cairo_int32x32_64_mul (a, d), |
540 | return _cairo_int64_sub (_cairo_int32x32_64_mul (a, d), |
541 | _cairo_int32x32_64_mul (b, c)); |
541 | _cairo_int32x32_64_mul (b, c)); |
542 | } |
542 | } |
543 | 543 | ||
544 | static inline cairo_int128_t |
544 | static inline cairo_int128_t |
545 | det64x32_128 (cairo_int64_t a, int32_t b, |
545 | det64x32_128 (cairo_int64_t a, int32_t b, |
546 | cairo_int64_t c, int32_t d) |
546 | cairo_int64_t c, int32_t d) |
547 | { |
547 | { |
548 | /* det = a * d - b * c */ |
548 | /* det = a * d - b * c */ |
549 | return _cairo_int128_sub (_cairo_int64x32_128_mul (a, d), |
549 | return _cairo_int128_sub (_cairo_int64x32_128_mul (a, d), |
550 | _cairo_int64x32_128_mul (c, b)); |
550 | _cairo_int64x32_128_mul (c, b)); |
551 | } |
551 | } |
552 | 552 | ||
553 | /* Compute the intersection of two lines as defined by two edges. The |
553 | /* Compute the intersection of two lines as defined by two edges. The |
554 | * result is provided as a coordinate pair of 128-bit integers. |
554 | * result is provided as a coordinate pair of 128-bit integers. |
555 | * |
555 | * |
556 | * Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection or |
556 | * Returns %CAIRO_BO_STATUS_INTERSECTION if there is an intersection or |
557 | * %CAIRO_BO_STATUS_PARALLEL if the two lines are exactly parallel. |
557 | * %CAIRO_BO_STATUS_PARALLEL if the two lines are exactly parallel. |
558 | */ |
558 | */ |
559 | static cairo_bool_t |
559 | static cairo_bool_t |
560 | intersect_lines (const edge_t *a, const edge_t *b, |
560 | intersect_lines (const edge_t *a, const edge_t *b, |
561 | cairo_bo_intersect_point_t *intersection) |
561 | cairo_bo_intersect_point_t *intersection) |
562 | { |
562 | { |
563 | cairo_int64_t a_det, b_det; |
563 | cairo_int64_t a_det, b_det; |
564 | 564 | ||
565 | /* XXX: We're assuming here that dx and dy will still fit in 32 |
565 | /* XXX: We're assuming here that dx and dy will still fit in 32 |
566 | * bits. That's not true in general as there could be overflow. We |
566 | * bits. That's not true in general as there could be overflow. We |
567 | * should prevent that before the tessellation algorithm begins. |
567 | * should prevent that before the tessellation algorithm begins. |
568 | * What we're doing to mitigate this is to perform clamping in |
568 | * What we're doing to mitigate this is to perform clamping in |
569 | * cairo_bo_tessellate_polygon(). |
569 | * cairo_bo_tessellate_polygon(). |
570 | */ |
570 | */ |
571 | int32_t dx1 = a->edge.line.p1.x - a->edge.line.p2.x; |
571 | int32_t dx1 = a->edge.line.p1.x - a->edge.line.p2.x; |
572 | int32_t dy1 = a->edge.line.p1.y - a->edge.line.p2.y; |
572 | int32_t dy1 = a->edge.line.p1.y - a->edge.line.p2.y; |
573 | 573 | ||
574 | int32_t dx2 = b->edge.line.p1.x - b->edge.line.p2.x; |
574 | int32_t dx2 = b->edge.line.p1.x - b->edge.line.p2.x; |
575 | int32_t dy2 = b->edge.line.p1.y - b->edge.line.p2.y; |
575 | int32_t dy2 = b->edge.line.p1.y - b->edge.line.p2.y; |
576 | 576 | ||
577 | cairo_int64_t den_det; |
577 | cairo_int64_t den_det; |
578 | cairo_int64_t R; |
578 | cairo_int64_t R; |
579 | cairo_quorem64_t qr; |
579 | cairo_quorem64_t qr; |
580 | 580 | ||
581 | den_det = det32_64 (dx1, dy1, dx2, dy2); |
581 | den_det = det32_64 (dx1, dy1, dx2, dy2); |
582 | 582 | ||
583 | /* Q: Can we determine that the lines do not intersect (within range) |
583 | /* Q: Can we determine that the lines do not intersect (within range) |
584 | * much more cheaply than computing the intersection point i.e. by |
584 | * much more cheaply than computing the intersection point i.e. by |
585 | * avoiding the division? |
585 | * avoiding the division? |
586 | * |
586 | * |
587 | * X = ax + t * adx = bx + s * bdx; |
587 | * X = ax + t * adx = bx + s * bdx; |
588 | * Y = ay + t * ady = by + s * bdy; |
588 | * Y = ay + t * ady = by + s * bdy; |
589 | * ∴ t * (ady*bdx - bdy*adx) = bdx * (by - ay) + bdy * (ax - bx) |
589 | * ∴ t * (ady*bdx - bdy*adx) = bdx * (by - ay) + bdy * (ax - bx) |
590 | * => t * L = R |
590 | * => t * L = R |
591 | * |
591 | * |
592 | * Therefore we can reject any intersection (under the criteria for |
592 | * Therefore we can reject any intersection (under the criteria for |
593 | * valid intersection events) if: |
593 | * valid intersection events) if: |
594 | * L^R < 0 => t < 0, or |
594 | * L^R < 0 => t < 0, or |
595 | * L |
595 | * L |
596 | * |
596 | * |
597 | * (where top/bottom must at least extend to the line endpoints). |
597 | * (where top/bottom must at least extend to the line endpoints). |
598 | * |
598 | * |
599 | * A similar substitution can be performed for s, yielding: |
599 | * A similar substitution can be performed for s, yielding: |
600 | * s * (ady*bdx - bdy*adx) = ady * (ax - bx) - adx * (ay - by) |
600 | * s * (ady*bdx - bdy*adx) = ady * (ax - bx) - adx * (ay - by) |
601 | */ |
601 | */ |
602 | R = det32_64 (dx2, dy2, |
602 | R = det32_64 (dx2, dy2, |
603 | b->edge.line.p1.x - a->edge.line.p1.x, |
603 | b->edge.line.p1.x - a->edge.line.p1.x, |
604 | b->edge.line.p1.y - a->edge.line.p1.y); |
604 | b->edge.line.p1.y - a->edge.line.p1.y); |
605 | if (_cairo_int64_negative (den_det)) { |
605 | if (_cairo_int64_negative (den_det)) { |
606 | if (_cairo_int64_ge (den_det, R)) |
606 | if (_cairo_int64_ge (den_det, R)) |
607 | return FALSE; |
607 | return FALSE; |
608 | } else { |
608 | } else { |
609 | if (_cairo_int64_le (den_det, R)) |
609 | if (_cairo_int64_le (den_det, R)) |
610 | return FALSE; |
610 | return FALSE; |
611 | } |
611 | } |
612 | 612 | ||
613 | R = det32_64 (dy1, dx1, |
613 | R = det32_64 (dy1, dx1, |
614 | a->edge.line.p1.y - b->edge.line.p1.y, |
614 | a->edge.line.p1.y - b->edge.line.p1.y, |
615 | a->edge.line.p1.x - b->edge.line.p1.x); |
615 | a->edge.line.p1.x - b->edge.line.p1.x); |
616 | if (_cairo_int64_negative (den_det)) { |
616 | if (_cairo_int64_negative (den_det)) { |
617 | if (_cairo_int64_ge (den_det, R)) |
617 | if (_cairo_int64_ge (den_det, R)) |
618 | return FALSE; |
618 | return FALSE; |
619 | } else { |
619 | } else { |
620 | if (_cairo_int64_le (den_det, R)) |
620 | if (_cairo_int64_le (den_det, R)) |
621 | return FALSE; |
621 | return FALSE; |
622 | } |
622 | } |
623 | 623 | ||
624 | /* We now know that the two lines should intersect within range. */ |
624 | /* We now know that the two lines should intersect within range. */ |
625 | 625 | ||
626 | a_det = det32_64 (a->edge.line.p1.x, a->edge.line.p1.y, |
626 | a_det = det32_64 (a->edge.line.p1.x, a->edge.line.p1.y, |
627 | a->edge.line.p2.x, a->edge.line.p2.y); |
627 | a->edge.line.p2.x, a->edge.line.p2.y); |
628 | b_det = det32_64 (b->edge.line.p1.x, b->edge.line.p1.y, |
628 | b_det = det32_64 (b->edge.line.p1.x, b->edge.line.p1.y, |
629 | b->edge.line.p2.x, b->edge.line.p2.y); |
629 | b->edge.line.p2.x, b->edge.line.p2.y); |
630 | 630 | ||
631 | /* x = det (a_det, dx1, b_det, dx2) / den_det */ |
631 | /* x = det (a_det, dx1, b_det, dx2) / den_det */ |
632 | qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dx1, |
632 | qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dx1, |
633 | b_det, dx2), |
633 | b_det, dx2), |
634 | den_det); |
634 | den_det); |
635 | if (_cairo_int64_eq (qr.rem, den_det)) |
635 | if (_cairo_int64_eq (qr.rem, den_det)) |
636 | return FALSE; |
636 | return FALSE; |
637 | #if 0 |
637 | #if 0 |
638 | intersection->x.exactness = _cairo_int64_is_zero (qr.rem) ? EXACT : INEXACT; |
638 | intersection->x.exactness = _cairo_int64_is_zero (qr.rem) ? EXACT : INEXACT; |
639 | #else |
639 | #else |
640 | intersection->x.exactness = EXACT; |
640 | intersection->x.exactness = EXACT; |
641 | if (! _cairo_int64_is_zero (qr.rem)) { |
641 | if (! _cairo_int64_is_zero (qr.rem)) { |
642 | if (_cairo_int64_negative (den_det) ^ _cairo_int64_negative (qr.rem)) |
642 | if (_cairo_int64_negative (den_det) ^ _cairo_int64_negative (qr.rem)) |
643 | qr.rem = _cairo_int64_negate (qr.rem); |
643 | qr.rem = _cairo_int64_negate (qr.rem); |
644 | qr.rem = _cairo_int64_mul (qr.rem, _cairo_int32_to_int64 (2)); |
644 | qr.rem = _cairo_int64_mul (qr.rem, _cairo_int32_to_int64 (2)); |
645 | if (_cairo_int64_ge (qr.rem, den_det)) { |
645 | if (_cairo_int64_ge (qr.rem, den_det)) { |
646 | qr.quo = _cairo_int64_add (qr.quo, |
646 | qr.quo = _cairo_int64_add (qr.quo, |
647 | _cairo_int32_to_int64 (_cairo_int64_negative (qr.quo) ? -1 : 1)); |
647 | _cairo_int32_to_int64 (_cairo_int64_negative (qr.quo) ? -1 : 1)); |
648 | } else |
648 | } else |
649 | intersection->x.exactness = INEXACT; |
649 | intersection->x.exactness = INEXACT; |
650 | } |
650 | } |
651 | #endif |
651 | #endif |
652 | intersection->x.ordinate = _cairo_int64_to_int32 (qr.quo); |
652 | intersection->x.ordinate = _cairo_int64_to_int32 (qr.quo); |
653 | 653 | ||
654 | /* y = det (a_det, dy1, b_det, dy2) / den_det */ |
654 | /* y = det (a_det, dy1, b_det, dy2) / den_det */ |
655 | qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dy1, |
655 | qr = _cairo_int_96by64_32x64_divrem (det64x32_128 (a_det, dy1, |
656 | b_det, dy2), |
656 | b_det, dy2), |
657 | den_det); |
657 | den_det); |
658 | if (_cairo_int64_eq (qr.rem, den_det)) |
658 | if (_cairo_int64_eq (qr.rem, den_det)) |
659 | return FALSE; |
659 | return FALSE; |
660 | #if 0 |
660 | #if 0 |
661 | intersection->y.exactness = _cairo_int64_is_zero (qr.rem) ? EXACT : INEXACT; |
661 | intersection->y.exactness = _cairo_int64_is_zero (qr.rem) ? EXACT : INEXACT; |
662 | #else |
662 | #else |
663 | intersection->y.exactness = EXACT; |
663 | intersection->y.exactness = EXACT; |
664 | if (! _cairo_int64_is_zero (qr.rem)) { |
664 | if (! _cairo_int64_is_zero (qr.rem)) { |
665 | /* compute ceiling away from zero */ |
665 | /* compute ceiling away from zero */ |
666 | qr.quo = _cairo_int64_add (qr.quo, |
666 | qr.quo = _cairo_int64_add (qr.quo, |
667 | _cairo_int32_to_int64 (_cairo_int64_negative (qr.quo) ? -1 : 1)); |
667 | _cairo_int32_to_int64 (_cairo_int64_negative (qr.quo) ? -1 : 1)); |
668 | intersection->y.exactness = INEXACT; |
668 | intersection->y.exactness = INEXACT; |
669 | } |
669 | } |
670 | #endif |
670 | #endif |
671 | intersection->y.ordinate = _cairo_int64_to_int32 (qr.quo); |
671 | intersection->y.ordinate = _cairo_int64_to_int32 (qr.quo); |
672 | 672 | ||
673 | return TRUE; |
673 | return TRUE; |
674 | } |
674 | } |
675 | 675 | ||
676 | static int |
676 | static int |
677 | bo_intersect_ordinate_32_compare (int32_t a, int32_t b, int exactness) |
677 | bo_intersect_ordinate_32_compare (int32_t a, int32_t b, int exactness) |
678 | { |
678 | { |
679 | int cmp; |
679 | int cmp; |
680 | 680 | ||
681 | /* First compare the quotient */ |
681 | /* First compare the quotient */ |
682 | cmp = a - b; |
682 | cmp = a - b; |
683 | if (cmp) |
683 | if (cmp) |
684 | return cmp; |
684 | return cmp; |
685 | 685 | ||
686 | /* With quotient identical, if remainder is 0 then compare equal */ |
686 | /* With quotient identical, if remainder is 0 then compare equal */ |
687 | /* Otherwise, the non-zero remainder makes a > b */ |
687 | /* Otherwise, the non-zero remainder makes a > b */ |
688 | return -(INEXACT == exactness); |
688 | return -(INEXACT == exactness); |
689 | } |
689 | } |
690 | 690 | ||
691 | /* Does the given edge contain the given point. The point must already |
691 | /* Does the given edge contain the given point. The point must already |
692 | * be known to be contained within the line determined by the edge, |
692 | * be known to be contained within the line determined by the edge, |
693 | * (most likely the point results from an intersection of this edge |
693 | * (most likely the point results from an intersection of this edge |
694 | * with another). |
694 | * with another). |
695 | * |
695 | * |
696 | * If we had exact arithmetic, then this function would simply be a |
696 | * If we had exact arithmetic, then this function would simply be a |
697 | * matter of examining whether the y value of the point lies within |
697 | * matter of examining whether the y value of the point lies within |
698 | * the range of y values of the edge. But since intersection points |
698 | * the range of y values of the edge. But since intersection points |
699 | * are not exact due to being rounded to the nearest integer within |
699 | * are not exact due to being rounded to the nearest integer within |
700 | * the available precision, we must also examine the x value of the |
700 | * the available precision, we must also examine the x value of the |
701 | * point. |
701 | * point. |
702 | * |
702 | * |
703 | * The definition of "contains" here is that the given intersection |
703 | * The definition of "contains" here is that the given intersection |
704 | * point will be seen by the sweep line after the start event for the |
704 | * point will be seen by the sweep line after the start event for the |
705 | * given edge and before the stop event for the edge. See the comments |
705 | * given edge and before the stop event for the edge. See the comments |
706 | * in the implementation for more details. |
706 | * in the implementation for more details. |
707 | */ |
707 | */ |
708 | static cairo_bool_t |
708 | static cairo_bool_t |
709 | bo_edge_contains_intersect_point (const edge_t *edge, |
709 | bo_edge_contains_intersect_point (const edge_t *edge, |
710 | cairo_bo_intersect_point_t *point) |
710 | cairo_bo_intersect_point_t *point) |
711 | { |
711 | { |
712 | int cmp_top, cmp_bottom; |
712 | int cmp_top, cmp_bottom; |
713 | 713 | ||
714 | /* XXX: When running the actual algorithm, we don't actually need to |
714 | /* XXX: When running the actual algorithm, we don't actually need to |
715 | * compare against edge->top at all here, since any intersection above |
715 | * compare against edge->top at all here, since any intersection above |
716 | * top is eliminated early via a slope comparison. We're leaving these |
716 | * top is eliminated early via a slope comparison. We're leaving these |
717 | * here for now only for the sake of the quadratic-time intersection |
717 | * here for now only for the sake of the quadratic-time intersection |
718 | * finder which needs them. |
718 | * finder which needs them. |
719 | */ |
719 | */ |
720 | 720 | ||
721 | cmp_top = bo_intersect_ordinate_32_compare (point->y.ordinate, |
721 | cmp_top = bo_intersect_ordinate_32_compare (point->y.ordinate, |
722 | edge->edge.top, |
722 | edge->edge.top, |
723 | point->y.exactness); |
723 | point->y.exactness); |
724 | if (cmp_top < 0) |
724 | if (cmp_top < 0) |
725 | return FALSE; |
725 | return FALSE; |
726 | 726 | ||
727 | cmp_bottom = bo_intersect_ordinate_32_compare (point->y.ordinate, |
727 | cmp_bottom = bo_intersect_ordinate_32_compare (point->y.ordinate, |
728 | edge->edge.bottom, |
728 | edge->edge.bottom, |
729 | point->y.exactness); |
729 | point->y.exactness); |
730 | if (cmp_bottom > 0) |
730 | if (cmp_bottom > 0) |
731 | return FALSE; |
731 | return FALSE; |
732 | 732 | ||
733 | if (cmp_top > 0 && cmp_bottom < 0) |
733 | if (cmp_top > 0 && cmp_bottom < 0) |
734 | return TRUE; |
734 | return TRUE; |
735 | 735 | ||
736 | /* At this stage, the point lies on the same y value as either |
736 | /* At this stage, the point lies on the same y value as either |
737 | * edge->top or edge->bottom, so we have to examine the x value in |
737 | * edge->top or edge->bottom, so we have to examine the x value in |
738 | * order to properly determine containment. */ |
738 | * order to properly determine containment. */ |
739 | 739 | ||
740 | /* If the y value of the point is the same as the y value of the |
740 | /* If the y value of the point is the same as the y value of the |
741 | * top of the edge, then the x value of the point must be greater |
741 | * top of the edge, then the x value of the point must be greater |
742 | * to be considered as inside the edge. Similarly, if the y value |
742 | * to be considered as inside the edge. Similarly, if the y value |
743 | * of the point is the same as the y value of the bottom of the |
743 | * of the point is the same as the y value of the bottom of the |
744 | * edge, then the x value of the point must be less to be |
744 | * edge, then the x value of the point must be less to be |
745 | * considered as inside. */ |
745 | * considered as inside. */ |
746 | 746 | ||
747 | if (cmp_top == 0) { |
747 | if (cmp_top == 0) { |
748 | cairo_fixed_t top_x; |
748 | cairo_fixed_t top_x; |
749 | 749 | ||
750 | top_x = line_compute_intersection_x_for_y (&edge->edge.line, |
750 | top_x = line_compute_intersection_x_for_y (&edge->edge.line, |
751 | edge->edge.top); |
751 | edge->edge.top); |
752 | return bo_intersect_ordinate_32_compare (top_x, point->x.ordinate, point->x.exactness) < 0; |
752 | return bo_intersect_ordinate_32_compare (top_x, point->x.ordinate, point->x.exactness) < 0; |
753 | } else { /* cmp_bottom == 0 */ |
753 | } else { /* cmp_bottom == 0 */ |
754 | cairo_fixed_t bot_x; |
754 | cairo_fixed_t bot_x; |
755 | 755 | ||
756 | bot_x = line_compute_intersection_x_for_y (&edge->edge.line, |
756 | bot_x = line_compute_intersection_x_for_y (&edge->edge.line, |
757 | edge->edge.bottom); |
757 | edge->edge.bottom); |
758 | return bo_intersect_ordinate_32_compare (point->x.ordinate, bot_x, point->x.exactness) < 0; |
758 | return bo_intersect_ordinate_32_compare (point->x.ordinate, bot_x, point->x.exactness) < 0; |
759 | } |
759 | } |
760 | } |
760 | } |
761 | 761 | ||
762 | static cairo_bool_t |
762 | static cairo_bool_t |
763 | edge_intersect (const edge_t *a, |
763 | edge_intersect (const edge_t *a, |
764 | const edge_t *b, |
764 | const edge_t *b, |
765 | cairo_point_t *intersection) |
765 | cairo_point_t *intersection) |
766 | { |
766 | { |
767 | cairo_bo_intersect_point_t quorem; |
767 | cairo_bo_intersect_point_t quorem; |
768 | 768 | ||
769 | if (! intersect_lines (a, b, &quorem)) |
769 | if (! intersect_lines (a, b, &quorem)) |
770 | return FALSE; |
770 | return FALSE; |
771 | 771 | ||
772 | if (a->edge.top != a->edge.line.p1.y || a->edge.bottom != a->edge.line.p2.y) { |
772 | if (a->edge.top != a->edge.line.p1.y || a->edge.bottom != a->edge.line.p2.y) { |
773 | if (! bo_edge_contains_intersect_point (a, &quorem)) |
773 | if (! bo_edge_contains_intersect_point (a, &quorem)) |
774 | return FALSE; |
774 | return FALSE; |
775 | } |
775 | } |
776 | 776 | ||
777 | if (b->edge.top != b->edge.line.p1.y || b->edge.bottom != b->edge.line.p2.y) { |
777 | if (b->edge.top != b->edge.line.p1.y || b->edge.bottom != b->edge.line.p2.y) { |
778 | if (! bo_edge_contains_intersect_point (b, &quorem)) |
778 | if (! bo_edge_contains_intersect_point (b, &quorem)) |
779 | return FALSE; |
779 | return FALSE; |
780 | } |
780 | } |
781 | 781 | ||
782 | /* Now that we've correctly compared the intersection point and |
782 | /* Now that we've correctly compared the intersection point and |
783 | * determined that it lies within the edge, then we know that we |
783 | * determined that it lies within the edge, then we know that we |
784 | * no longer need any more bits of storage for the intersection |
784 | * no longer need any more bits of storage for the intersection |
785 | * than we do for our edge coordinates. We also no longer need the |
785 | * than we do for our edge coordinates. We also no longer need the |
786 | * remainder from the division. */ |
786 | * remainder from the division. */ |
787 | intersection->x = quorem.x.ordinate; |
787 | intersection->x = quorem.x.ordinate; |
788 | intersection->y = quorem.y.ordinate; |
788 | intersection->y = quorem.y.ordinate; |
789 | 789 | ||
790 | return TRUE; |
790 | return TRUE; |
791 | } |
791 | } |
792 | 792 | ||
793 | static inline int |
793 | static inline int |
794 | event_compare (const event_t *a, const event_t *b) |
794 | event_compare (const event_t *a, const event_t *b) |
795 | { |
795 | { |
796 | return a->y - b->y; |
796 | return a->y - b->y; |
797 | } |
797 | } |
798 | 798 | ||
799 | static void |
799 | static void |
800 | pqueue_init (pqueue_t *pq) |
800 | pqueue_init (pqueue_t *pq) |
801 | { |
801 | { |
802 | pq->max_size = ARRAY_LENGTH (pq->elements_embedded); |
802 | pq->max_size = ARRAY_LENGTH (pq->elements_embedded); |
803 | pq->size = 0; |
803 | pq->size = 0; |
804 | 804 | ||
805 | pq->elements = pq->elements_embedded; |
805 | pq->elements = pq->elements_embedded; |
806 | } |
806 | } |
807 | 807 | ||
808 | static void |
808 | static void |
809 | pqueue_fini (pqueue_t *pq) |
809 | pqueue_fini (pqueue_t *pq) |
810 | { |
810 | { |
811 | if (pq->elements != pq->elements_embedded) |
811 | if (pq->elements != pq->elements_embedded) |
812 | free (pq->elements); |
812 | free (pq->elements); |
813 | } |
813 | } |
814 | 814 | ||
815 | static cairo_bool_t |
815 | static cairo_bool_t |
816 | pqueue_grow (pqueue_t *pq) |
816 | pqueue_grow (pqueue_t *pq) |
817 | { |
817 | { |
818 | event_t **new_elements; |
818 | event_t **new_elements; |
819 | pq->max_size *= 2; |
819 | pq->max_size *= 2; |
820 | 820 | ||
821 | if (pq->elements == pq->elements_embedded) { |
821 | if (pq->elements == pq->elements_embedded) { |
822 | new_elements = _cairo_malloc_ab (pq->max_size, |
822 | new_elements = _cairo_malloc_ab (pq->max_size, |
823 | sizeof (event_t *)); |
823 | sizeof (event_t *)); |
824 | if (unlikely (new_elements == NULL)) |
824 | if (unlikely (new_elements == NULL)) |
825 | return FALSE; |
825 | return FALSE; |
826 | 826 | ||
827 | memcpy (new_elements, pq->elements_embedded, |
827 | memcpy (new_elements, pq->elements_embedded, |
828 | sizeof (pq->elements_embedded)); |
828 | sizeof (pq->elements_embedded)); |
829 | } else { |
829 | } else { |
830 | new_elements = _cairo_realloc_ab (pq->elements, |
830 | new_elements = _cairo_realloc_ab (pq->elements, |
831 | pq->max_size, |
831 | pq->max_size, |
832 | sizeof (event_t *)); |
832 | sizeof (event_t *)); |
833 | if (unlikely (new_elements == NULL)) |
833 | if (unlikely (new_elements == NULL)) |
834 | return FALSE; |
834 | return FALSE; |
835 | } |
835 | } |
836 | 836 | ||
837 | pq->elements = new_elements; |
837 | pq->elements = new_elements; |
838 | return TRUE; |
838 | return TRUE; |
839 | } |
839 | } |
840 | 840 | ||
841 | static inline void |
841 | static inline void |
842 | pqueue_push (sweep_line_t *sweep_line, event_t *event) |
842 | pqueue_push (sweep_line_t *sweep_line, event_t *event) |
843 | { |
843 | { |
844 | event_t **elements; |
844 | event_t **elements; |
845 | int i, parent; |
845 | int i, parent; |
846 | 846 | ||
847 | if (unlikely (sweep_line->queue.pq.size + 1 == sweep_line->queue.pq.max_size)) { |
847 | if (unlikely (sweep_line->queue.pq.size + 1 == sweep_line->queue.pq.max_size)) { |
848 | if (unlikely (! pqueue_grow (&sweep_line->queue.pq))) { |
848 | if (unlikely (! pqueue_grow (&sweep_line->queue.pq))) { |
849 | longjmp (sweep_line->unwind, |
849 | longjmp (sweep_line->unwind, |
850 | _cairo_error (CAIRO_STATUS_NO_MEMORY)); |
850 | _cairo_error (CAIRO_STATUS_NO_MEMORY)); |
851 | } |
851 | } |
852 | } |
852 | } |
853 | 853 | ||
854 | elements = sweep_line->queue.pq.elements; |
854 | elements = sweep_line->queue.pq.elements; |
855 | for (i = ++sweep_line->queue.pq.size; |
855 | for (i = ++sweep_line->queue.pq.size; |
856 | i != PQ_FIRST_ENTRY && |
856 | i != PQ_FIRST_ENTRY && |
857 | event_compare (event, |
857 | event_compare (event, |
858 | elements[parent = PQ_PARENT_INDEX (i)]) < 0; |
858 | elements[parent = PQ_PARENT_INDEX (i)]) < 0; |
859 | i = parent) |
859 | i = parent) |
860 | { |
860 | { |
861 | elements[i] = elements[parent]; |
861 | elements[i] = elements[parent]; |
862 | } |
862 | } |
863 | 863 | ||
864 | elements[i] = event; |
864 | elements[i] = event; |
865 | } |
865 | } |
866 | 866 | ||
867 | static inline void |
867 | static inline void |
868 | pqueue_pop (pqueue_t *pq) |
868 | pqueue_pop (pqueue_t *pq) |
869 | { |
869 | { |
870 | event_t **elements = pq->elements; |
870 | event_t **elements = pq->elements; |
871 | event_t *tail; |
871 | event_t *tail; |
872 | int child, i; |
872 | int child, i; |
873 | 873 | ||
874 | tail = elements[pq->size--]; |
874 | tail = elements[pq->size--]; |
875 | if (pq->size == 0) { |
875 | if (pq->size == 0) { |
876 | elements[PQ_FIRST_ENTRY] = NULL; |
876 | elements[PQ_FIRST_ENTRY] = NULL; |
877 | return; |
877 | return; |
878 | } |
878 | } |
879 | 879 | ||
880 | for (i = PQ_FIRST_ENTRY; |
880 | for (i = PQ_FIRST_ENTRY; |
881 | (child = PQ_LEFT_CHILD_INDEX (i)) <= pq->size; |
881 | (child = PQ_LEFT_CHILD_INDEX (i)) <= pq->size; |
882 | i = child) |
882 | i = child) |
883 | { |
883 | { |
884 | if (child != pq->size && |
884 | if (child != pq->size && |
885 | event_compare (elements[child+1], |
885 | event_compare (elements[child+1], |
886 | elements[child]) < 0) |
886 | elements[child]) < 0) |
887 | { |
887 | { |
888 | child++; |
888 | child++; |
889 | } |
889 | } |
890 | 890 | ||
891 | if (event_compare (elements[child], tail) >= 0) |
891 | if (event_compare (elements[child], tail) >= 0) |
892 | break; |
892 | break; |
893 | 893 | ||
894 | elements[i] = elements[child]; |
894 | elements[i] = elements[child]; |
895 | } |
895 | } |
896 | elements[i] = tail; |
896 | elements[i] = tail; |
897 | } |
897 | } |
898 | 898 | ||
899 | static inline void |
899 | static inline void |
900 | event_insert (sweep_line_t *sweep_line, |
900 | event_insert (sweep_line_t *sweep_line, |
901 | event_type_t type, |
901 | event_type_t type, |
902 | edge_t *e1, |
902 | edge_t *e1, |
903 | edge_t *e2, |
903 | edge_t *e2, |
904 | cairo_fixed_t y) |
904 | cairo_fixed_t y) |
905 | { |
905 | { |
906 | queue_event_t *event; |
906 | queue_event_t *event; |
907 | 907 | ||
908 | event = _cairo_freepool_alloc (&sweep_line->queue.pool); |
908 | event = _cairo_freepool_alloc (&sweep_line->queue.pool); |
909 | if (unlikely (event == NULL)) { |
909 | if (unlikely (event == NULL)) { |
910 | longjmp (sweep_line->unwind, |
910 | longjmp (sweep_line->unwind, |
911 | _cairo_error (CAIRO_STATUS_NO_MEMORY)); |
911 | _cairo_error (CAIRO_STATUS_NO_MEMORY)); |
912 | } |
912 | } |
913 | 913 | ||
914 | event->y = y; |
914 | event->y = y; |
915 | event->type = type; |
915 | event->type = type; |
916 | event->e1 = e1; |
916 | event->e1 = e1; |
917 | event->e2 = e2; |
917 | event->e2 = e2; |
918 | 918 | ||
919 | pqueue_push (sweep_line, (event_t *) event); |
919 | pqueue_push (sweep_line, (event_t *) event); |
920 | } |
920 | } |
921 | 921 | ||
922 | static void |
922 | static void |
923 | event_delete (sweep_line_t *sweep_line, |
923 | event_delete (sweep_line_t *sweep_line, |
924 | event_t *event) |
924 | event_t *event) |
925 | { |
925 | { |
926 | _cairo_freepool_free (&sweep_line->queue.pool, event); |
926 | _cairo_freepool_free (&sweep_line->queue.pool, event); |
927 | } |
927 | } |
928 | 928 | ||
929 | static inline event_t * |
929 | static inline event_t * |
930 | event_next (sweep_line_t *sweep_line) |
930 | event_next (sweep_line_t *sweep_line) |
931 | { |
931 | { |
932 | event_t *event, *cmp; |
932 | event_t *event, *cmp; |
933 | 933 | ||
934 | event = sweep_line->queue.pq.elements[PQ_FIRST_ENTRY]; |
934 | event = sweep_line->queue.pq.elements[PQ_FIRST_ENTRY]; |
935 | cmp = *sweep_line->queue.start_events; |
935 | cmp = *sweep_line->queue.start_events; |
936 | if (event == NULL || |
936 | if (event == NULL || |
937 | (cmp != NULL && event_compare (cmp, event) < 0)) |
937 | (cmp != NULL && event_compare (cmp, event) < 0)) |
938 | { |
938 | { |
939 | event = cmp; |
939 | event = cmp; |
940 | sweep_line->queue.start_events++; |
940 | sweep_line->queue.start_events++; |
941 | } |
941 | } |
942 | else |
942 | else |
943 | { |
943 | { |
944 | pqueue_pop (&sweep_line->queue.pq); |
944 | pqueue_pop (&sweep_line->queue.pq); |
945 | } |
945 | } |
946 | 946 | ||
947 | return event; |
947 | return event; |
948 | } |
948 | } |
949 | 949 | ||
950 | CAIRO_COMBSORT_DECLARE (start_event_sort, event_t *, event_compare) |
950 | CAIRO_COMBSORT_DECLARE (start_event_sort, event_t *, event_compare) |
951 | 951 | ||
952 | static inline void |
952 | static inline void |
953 | event_insert_stop (sweep_line_t *sweep_line, |
953 | event_insert_stop (sweep_line_t *sweep_line, |
954 | edge_t *edge) |
954 | edge_t *edge) |
955 | { |
955 | { |
956 | event_insert (sweep_line, |
956 | event_insert (sweep_line, |
957 | EVENT_TYPE_STOP, |
957 | EVENT_TYPE_STOP, |
958 | edge, NULL, |
958 | edge, NULL, |
959 | edge->edge.bottom); |
959 | edge->edge.bottom); |
960 | } |
960 | } |
961 | 961 | ||
962 | static inline void |
962 | static inline void |
963 | event_insert_if_intersect_below_current_y (sweep_line_t *sweep_line, |
963 | event_insert_if_intersect_below_current_y (sweep_line_t *sweep_line, |
964 | edge_t *left, |
964 | edge_t *left, |
965 | edge_t *right) |
965 | edge_t *right) |
966 | { |
966 | { |
967 | cairo_point_t intersection; |
967 | cairo_point_t intersection; |
968 | 968 | ||
969 | /* start points intersect */ |
969 | /* start points intersect */ |
970 | if (left->edge.line.p1.x == right->edge.line.p1.x && |
970 | if (left->edge.line.p1.x == right->edge.line.p1.x && |
971 | left->edge.line.p1.y == right->edge.line.p1.y) |
971 | left->edge.line.p1.y == right->edge.line.p1.y) |
972 | { |
972 | { |
973 | return; |
973 | return; |
974 | } |
974 | } |
975 | 975 | ||
976 | /* end points intersect, process DELETE events first */ |
976 | /* end points intersect, process DELETE events first */ |
977 | if (left->edge.line.p2.x == right->edge.line.p2.x && |
977 | if (left->edge.line.p2.x == right->edge.line.p2.x && |
978 | left->edge.line.p2.y == right->edge.line.p2.y) |
978 | left->edge.line.p2.y == right->edge.line.p2.y) |
979 | { |
979 | { |
980 | return; |
980 | return; |
981 | } |
981 | } |
982 | 982 | ||
983 | if (slope_compare (left, right) <= 0) |
983 | if (slope_compare (left, right) <= 0) |
984 | return; |
984 | return; |
985 | 985 | ||
986 | if (! edge_intersect (left, right, &intersection)) |
986 | if (! edge_intersect (left, right, &intersection)) |
987 | return; |
987 | return; |
988 | 988 | ||
989 | event_insert (sweep_line, |
989 | event_insert (sweep_line, |
990 | EVENT_TYPE_INTERSECTION, |
990 | EVENT_TYPE_INTERSECTION, |
991 | left, right, |
991 | left, right, |
992 | intersection.y); |
992 | intersection.y); |
993 | } |
993 | } |
994 | 994 | ||
995 | static inline edge_t * |
995 | static inline edge_t * |
996 | link_to_edge (cairo_list_t *link) |
996 | link_to_edge (cairo_list_t *link) |
997 | { |
997 | { |
998 | return (edge_t *) link; |
998 | return (edge_t *) link; |
999 | } |
999 | } |
1000 | 1000 | ||
1001 | static void |
1001 | static void |
1002 | sweep_line_insert (sweep_line_t *sweep_line, |
1002 | sweep_line_insert (sweep_line_t *sweep_line, |
1003 | edge_t *edge) |
1003 | edge_t *edge) |
1004 | { |
1004 | { |
1005 | cairo_list_t *pos; |
1005 | cairo_list_t *pos; |
1006 | cairo_fixed_t y = sweep_line->current_subrow; |
1006 | cairo_fixed_t y = sweep_line->current_subrow; |
1007 | 1007 | ||
1008 | pos = sweep_line->insert_cursor; |
1008 | pos = sweep_line->insert_cursor; |
1009 | if (pos == &sweep_line->active) |
1009 | if (pos == &sweep_line->active) |
1010 | pos = sweep_line->active.next; |
1010 | pos = sweep_line->active.next; |
1011 | if (pos != &sweep_line->active) { |
1011 | if (pos != &sweep_line->active) { |
1012 | int cmp; |
1012 | int cmp; |
1013 | 1013 | ||
1014 | cmp = sweep_line_compare_edges (link_to_edge (pos), |
1014 | cmp = sweep_line_compare_edges (link_to_edge (pos), |
1015 | edge, |
1015 | edge, |
1016 | y); |
1016 | y); |
1017 | if (cmp < 0) { |
1017 | if (cmp < 0) { |
1018 | while (pos->next != &sweep_line->active && |
1018 | while (pos->next != &sweep_line->active && |
1019 | sweep_line_compare_edges (link_to_edge (pos->next), |
1019 | sweep_line_compare_edges (link_to_edge (pos->next), |
1020 | edge, |
1020 | edge, |
1021 | y) < 0) |
1021 | y) < 0) |
1022 | { |
1022 | { |
1023 | pos = pos->next; |
1023 | pos = pos->next; |
1024 | } |
1024 | } |
1025 | } else if (cmp > 0) { |
1025 | } else if (cmp > 0) { |
1026 | do { |
1026 | do { |
1027 | pos = pos->prev; |
1027 | pos = pos->prev; |
1028 | } while (pos != &sweep_line->active && |
1028 | } while (pos != &sweep_line->active && |
1029 | sweep_line_compare_edges (link_to_edge (pos), |
1029 | sweep_line_compare_edges (link_to_edge (pos), |
1030 | edge, |
1030 | edge, |
1031 | y) > 0); |
1031 | y) > 0); |
1032 | } |
1032 | } |
1033 | } |
1033 | } |
1034 | cairo_list_add (&edge->link, pos); |
1034 | cairo_list_add (&edge->link, pos); |
1035 | sweep_line->insert_cursor = &edge->link; |
1035 | sweep_line->insert_cursor = &edge->link; |
1036 | } |
1036 | } |
1037 | 1037 | ||
1038 | inline static void |
1038 | inline static void |
1039 | coverage_rewind (struct coverage *cells) |
1039 | coverage_rewind (struct coverage *cells) |
1040 | { |
1040 | { |
1041 | cells->cursor = &cells->head; |
1041 | cells->cursor = &cells->head; |
1042 | } |
1042 | } |
1043 | 1043 | ||
1044 | static void |
1044 | static void |
1045 | coverage_init (struct coverage *cells) |
1045 | coverage_init (struct coverage *cells) |
1046 | { |
1046 | { |
1047 | _cairo_freepool_init (&cells->pool, |
1047 | _cairo_freepool_init (&cells->pool, |
1048 | sizeof (struct cell)); |
1048 | sizeof (struct cell)); |
1049 | cells->head.prev = NULL; |
1049 | cells->head.prev = NULL; |
1050 | cells->head.next = &cells->tail; |
1050 | cells->head.next = &cells->tail; |
1051 | cells->head.x = INT_MIN; |
1051 | cells->head.x = INT_MIN; |
1052 | cells->tail.prev = &cells->head; |
1052 | cells->tail.prev = &cells->head; |
1053 | cells->tail.next = NULL; |
1053 | cells->tail.next = NULL; |
1054 | cells->tail.x = INT_MAX; |
1054 | cells->tail.x = INT_MAX; |
1055 | cells->count = 0; |
1055 | cells->count = 0; |
1056 | coverage_rewind (cells); |
1056 | coverage_rewind (cells); |
1057 | } |
1057 | } |
1058 | 1058 | ||
1059 | static void |
1059 | static void |
1060 | coverage_fini (struct coverage *cells) |
1060 | coverage_fini (struct coverage *cells) |
1061 | { |
1061 | { |
1062 | _cairo_freepool_fini (&cells->pool); |
1062 | _cairo_freepool_fini (&cells->pool); |
1063 | } |
1063 | } |
1064 | 1064 | ||
1065 | inline static void |
1065 | inline static void |
1066 | coverage_reset (struct coverage *cells) |
1066 | coverage_reset (struct coverage *cells) |
1067 | { |
1067 | { |
1068 | cells->head.next = &cells->tail; |
1068 | cells->head.next = &cells->tail; |
1069 | cells->tail.prev = &cells->head; |
1069 | cells->tail.prev = &cells->head; |
1070 | cells->count = 0; |
1070 | cells->count = 0; |
1071 | _cairo_freepool_reset (&cells->pool); |
1071 | _cairo_freepool_reset (&cells->pool); |
1072 | coverage_rewind (cells); |
1072 | coverage_rewind (cells); |
1073 | } |
1073 | } |
1074 | 1074 | ||
1075 | inline static struct cell * |
1075 | static struct cell * |
1076 | coverage_alloc (sweep_line_t *sweep_line, |
1076 | coverage_alloc (sweep_line_t *sweep_line, |
1077 | struct cell *tail, |
1077 | struct cell *tail, |
1078 | int x) |
1078 | int x) |
1079 | { |
1079 | { |
1080 | struct cell *cell; |
1080 | struct cell *cell; |
1081 | 1081 | ||
1082 | cell = _cairo_freepool_alloc (&sweep_line->coverage.pool); |
1082 | cell = _cairo_freepool_alloc (&sweep_line->coverage.pool); |
1083 | if (unlikely (NULL == cell)) { |
1083 | if (unlikely (NULL == cell)) { |
1084 | longjmp (sweep_line->unwind, |
1084 | longjmp (sweep_line->unwind, |
1085 | _cairo_error (CAIRO_STATUS_NO_MEMORY)); |
1085 | _cairo_error (CAIRO_STATUS_NO_MEMORY)); |
1086 | } |
1086 | } |
1087 | 1087 | ||
1088 | tail->prev->next = cell; |
1088 | tail->prev->next = cell; |
1089 | cell->prev = tail->prev; |
1089 | cell->prev = tail->prev; |
1090 | cell->next = tail; |
1090 | cell->next = tail; |
1091 | tail->prev = cell; |
1091 | tail->prev = cell; |
1092 | cell->x = x; |
1092 | cell->x = x; |
1093 | cell->uncovered_area = 0; |
1093 | cell->uncovered_area = 0; |
1094 | cell->covered_height = 0; |
1094 | cell->covered_height = 0; |
1095 | sweep_line->coverage.count++; |
1095 | sweep_line->coverage.count++; |
1096 | return cell; |
1096 | return cell; |
1097 | } |
1097 | } |
1098 | 1098 | ||
1099 | inline static struct cell * |
1099 | inline static struct cell * |
1100 | coverage_find (sweep_line_t *sweep_line, int x) |
1100 | coverage_find (sweep_line_t *sweep_line, int x) |
1101 | { |
1101 | { |
1102 | struct cell *cell; |
1102 | struct cell *cell; |
1103 | 1103 | ||
1104 | cell = sweep_line->coverage.cursor; |
1104 | cell = sweep_line->coverage.cursor; |
1105 | if (unlikely (cell->x > x)) { |
1105 | if (unlikely (cell->x > x)) { |
1106 | do { |
1106 | do { |
1107 | if (cell->prev->x < x) |
1107 | if (cell->prev->x < x) |
1108 | break; |
1108 | break; |
1109 | cell = cell->prev; |
1109 | cell = cell->prev; |
1110 | } while (TRUE); |
1110 | } while (TRUE); |
1111 | } else { |
1111 | } else { |
1112 | if (cell->x == x) |
1112 | if (cell->x == x) |
1113 | return cell; |
1113 | return cell; |
1114 | 1114 | ||
1115 | do { |
1115 | do { |
1116 | UNROLL3({ |
1116 | UNROLL3({ |
1117 | cell = cell->next; |
1117 | cell = cell->next; |
1118 | if (cell->x >= x) |
1118 | if (cell->x >= x) |
1119 | break; |
1119 | break; |
1120 | }); |
1120 | }); |
1121 | } while (TRUE); |
1121 | } while (TRUE); |
1122 | } |
1122 | } |
1123 | 1123 | ||
1124 | if (cell->x != x) |
1124 | if (cell->x != x) |
1125 | cell = coverage_alloc (sweep_line, cell, x); |
1125 | cell = coverage_alloc (sweep_line, cell, x); |
1126 | 1126 | ||
1127 | return sweep_line->coverage.cursor = cell; |
1127 | return sweep_line->coverage.cursor = cell; |
1128 | } |
1128 | } |
1129 | 1129 | ||
1130 | static void |
1130 | static void |
1131 | coverage_render_cells (sweep_line_t *sweep_line, |
1131 | coverage_render_cells (sweep_line_t *sweep_line, |
1132 | cairo_fixed_t left, cairo_fixed_t right, |
1132 | cairo_fixed_t left, cairo_fixed_t right, |
1133 | cairo_fixed_t y1, cairo_fixed_t y2, |
1133 | cairo_fixed_t y1, cairo_fixed_t y2, |
1134 | int sign) |
1134 | int sign) |
1135 | { |
1135 | { |
1136 | int fx1, fx2; |
1136 | int fx1, fx2; |
1137 | int ix1, ix2; |
1137 | int ix1, ix2; |
1138 | int dx, dy; |
1138 | int dx, dy; |
1139 | 1139 | ||
1140 | /* Orient the edge left-to-right. */ |
1140 | /* Orient the edge left-to-right. */ |
1141 | dx = right - left; |
1141 | dx = right - left; |
1142 | if (dx >= 0) { |
1142 | if (dx >= 0) { |
1143 | ix1 = _cairo_fixed_integer_part (left); |
1143 | ix1 = _cairo_fixed_integer_part (left); |
1144 | fx1 = _cairo_fixed_fractional_part (left); |
1144 | fx1 = _cairo_fixed_fractional_part (left); |
1145 | 1145 | ||
1146 | ix2 = _cairo_fixed_integer_part (right); |
1146 | ix2 = _cairo_fixed_integer_part (right); |
1147 | fx2 = _cairo_fixed_fractional_part (right); |
1147 | fx2 = _cairo_fixed_fractional_part (right); |
1148 | 1148 | ||
1149 | dy = y2 - y1; |
1149 | dy = y2 - y1; |
1150 | } else { |
1150 | } else { |
1151 | ix1 = _cairo_fixed_integer_part (right); |
1151 | ix1 = _cairo_fixed_integer_part (right); |
1152 | fx1 = _cairo_fixed_fractional_part (right); |
1152 | fx1 = _cairo_fixed_fractional_part (right); |
1153 | 1153 | ||
1154 | ix2 = _cairo_fixed_integer_part (left); |
1154 | ix2 = _cairo_fixed_integer_part (left); |
1155 | fx2 = _cairo_fixed_fractional_part (left); |
1155 | fx2 = _cairo_fixed_fractional_part (left); |
1156 | 1156 | ||
1157 | dx = -dx; |
1157 | dx = -dx; |
1158 | sign = -sign; |
1158 | sign = -sign; |
1159 | dy = y1 - y2; |
1159 | dy = y1 - y2; |
1160 | y1 = y2 - dy; |
1160 | y1 = y2 - dy; |
1161 | y2 = y1 + dy; |
1161 | y2 = y1 + dy; |
1162 | } |
1162 | } |
1163 | 1163 | ||
1164 | /* Add coverage for all pixels [ix1,ix2] on this row crossed |
1164 | /* Add coverage for all pixels [ix1,ix2] on this row crossed |
1165 | * by the edge. */ |
1165 | * by the edge. */ |
1166 | { |
1166 | { |
1167 | struct quorem y = floored_divrem ((STEP_X - fx1)*dy, dx); |
1167 | struct quorem y = floored_divrem ((STEP_X - fx1)*dy, dx); |
1168 | struct cell *cell; |
1168 | struct cell *cell; |
1169 | 1169 | ||
1170 | cell = sweep_line->coverage.cursor; |
1170 | cell = sweep_line->coverage.cursor; |
1171 | if (cell->x != ix1) { |
1171 | if (cell->x != ix1) { |
1172 | if (unlikely (cell->x > ix1)) { |
1172 | if (unlikely (cell->x > ix1)) { |
1173 | do { |
1173 | do { |
1174 | if (cell->prev->x < ix1) |
1174 | if (cell->prev->x < ix1) |
1175 | break; |
1175 | break; |
1176 | cell = cell->prev; |
1176 | cell = cell->prev; |
1177 | } while (TRUE); |
1177 | } while (TRUE); |
1178 | } else do { |
1178 | } else do { |
1179 | UNROLL3({ |
1179 | UNROLL3({ |
1180 | if (cell->x >= ix1) |
1180 | if (cell->x >= ix1) |
1181 | break; |
1181 | break; |
1182 | cell = cell->next; |
1182 | cell = cell->next; |
1183 | }); |
1183 | }); |
1184 | } while (TRUE); |
1184 | } while (TRUE); |
1185 | 1185 | ||
1186 | if (cell->x != ix1) |
1186 | if (cell->x != ix1) |
1187 | cell = coverage_alloc (sweep_line, cell, ix1); |
1187 | cell = coverage_alloc (sweep_line, cell, ix1); |
1188 | } |
1188 | } |
1189 | 1189 | ||
1190 | cell->uncovered_area += sign * y.quo * (STEP_X + fx1); |
1190 | cell->uncovered_area += sign * y.quo * (STEP_X + fx1); |
1191 | cell->covered_height += sign * y.quo; |
1191 | cell->covered_height += sign * y.quo; |
1192 | y.quo += y1; |
1192 | y.quo += y1; |
1193 | 1193 | ||
1194 | cell = cell->next; |
1194 | cell = cell->next; |
1195 | if (cell->x != ++ix1) |
1195 | if (cell->x != ++ix1) |
1196 | cell = coverage_alloc (sweep_line, cell, ix1); |
1196 | cell = coverage_alloc (sweep_line, cell, ix1); |
1197 | if (ix1 < ix2) { |
1197 | if (ix1 < ix2) { |
1198 | struct quorem dydx_full = floored_divrem (STEP_X*dy, dx); |
1198 | struct quorem dydx_full = floored_divrem (STEP_X*dy, dx); |
1199 | 1199 | ||
1200 | do { |
1200 | do { |
1201 | cairo_fixed_t y_skip = dydx_full.quo; |
1201 | cairo_fixed_t y_skip = dydx_full.quo; |
1202 | y.rem += dydx_full.rem; |
1202 | y.rem += dydx_full.rem; |
1203 | if (y.rem >= dx) { |
1203 | if (y.rem >= dx) { |
1204 | ++y_skip; |
1204 | ++y_skip; |
1205 | y.rem -= dx; |
1205 | y.rem -= dx; |
1206 | } |
1206 | } |
1207 | 1207 | ||
1208 | y.quo += y_skip; |
1208 | y.quo += y_skip; |
1209 | 1209 | ||
1210 | y_skip *= sign; |
1210 | y_skip *= sign; |
1211 | cell->covered_height += y_skip; |
1211 | cell->covered_height += y_skip; |
1212 | cell->uncovered_area += y_skip*STEP_X; |
1212 | cell->uncovered_area += y_skip*STEP_X; |
1213 | 1213 | ||
1214 | cell = cell->next; |
1214 | cell = cell->next; |
1215 | if (cell->x != ++ix1) |
1215 | if (cell->x != ++ix1) |
1216 | cell = coverage_alloc (sweep_line, cell, ix1); |
1216 | cell = coverage_alloc (sweep_line, cell, ix1); |
1217 | } while (ix1 != ix2); |
1217 | } while (ix1 != ix2); |
1218 | } |
1218 | } |
1219 | cell->uncovered_area += sign*(y2 - y.quo)*fx2; |
1219 | cell->uncovered_area += sign*(y2 - y.quo)*fx2; |
1220 | cell->covered_height += sign*(y2 - y.quo); |
1220 | cell->covered_height += sign*(y2 - y.quo); |
1221 | sweep_line->coverage.cursor = cell; |
1221 | sweep_line->coverage.cursor = cell; |
1222 | } |
1222 | } |
1223 | } |
1223 | } |
1224 | 1224 | ||
1225 | inline static void |
1225 | inline static void |
1226 | full_inc_edge (edge_t *edge) |
1226 | full_inc_edge (edge_t *edge) |
1227 | { |
1227 | { |
1228 | edge->x.quo += edge->dxdy_full.quo; |
1228 | edge->x.quo += edge->dxdy_full.quo; |
1229 | edge->x.rem += edge->dxdy_full.rem; |
1229 | edge->x.rem += edge->dxdy_full.rem; |
1230 | if (edge->x.rem >= 0) { |
1230 | if (edge->x.rem >= 0) { |
1231 | ++edge->x.quo; |
1231 | ++edge->x.quo; |
1232 | edge->x.rem -= edge->dy; |
1232 | edge->x.rem -= edge->dy; |
1233 | } |
1233 | } |
1234 | } |
1234 | } |
1235 | 1235 | ||
1236 | static void |
1236 | static void |
1237 | full_add_edge (sweep_line_t *sweep_line, edge_t *edge, int sign) |
1237 | full_add_edge (sweep_line_t *sweep_line, edge_t *edge, int sign) |
1238 | { |
1238 | { |
1239 | struct cell *cell; |
1239 | struct cell *cell; |
1240 | cairo_fixed_t x1, x2; |
1240 | cairo_fixed_t x1, x2; |
1241 | int ix1, ix2; |
1241 | int ix1, ix2; |
1242 | int frac; |
1242 | int frac; |
1243 | 1243 | ||
1244 | edge->current_sign = sign; |
1244 | edge->current_sign = sign; |
1245 | 1245 | ||
1246 | ix1 = _cairo_fixed_integer_part (edge->x.quo); |
1246 | ix1 = _cairo_fixed_integer_part (edge->x.quo); |
1247 | 1247 | ||
1248 | if (edge->vertical) { |
1248 | if (edge->vertical) { |
1249 | frac = _cairo_fixed_fractional_part (edge->x.quo); |
1249 | frac = _cairo_fixed_fractional_part (edge->x.quo); |
1250 | cell = coverage_find (sweep_line, ix1); |
1250 | cell = coverage_find (sweep_line, ix1); |
1251 | cell->covered_height += sign * STEP_Y; |
1251 | cell->covered_height += sign * STEP_Y; |
1252 | cell->uncovered_area += sign * 2 * frac * STEP_Y; |
1252 | cell->uncovered_area += sign * 2 * frac * STEP_Y; |
1253 | return; |
1253 | return; |
1254 | } |
1254 | } |
1255 | 1255 | ||
1256 | x1 = edge->x.quo; |
1256 | x1 = edge->x.quo; |
1257 | full_inc_edge (edge); |
1257 | full_inc_edge (edge); |
1258 | x2 = edge->x.quo; |
1258 | x2 = edge->x.quo; |
1259 | 1259 | ||
1260 | ix2 = _cairo_fixed_integer_part (edge->x.quo); |
1260 | ix2 = _cairo_fixed_integer_part (edge->x.quo); |
1261 | 1261 | ||
1262 | /* Edge is entirely within a column? */ |
1262 | /* Edge is entirely within a column? */ |
1263 | if (likely (ix1 == ix2)) { |
1263 | if (likely (ix1 == ix2)) { |
1264 | frac = _cairo_fixed_fractional_part (x1) + |
1264 | frac = _cairo_fixed_fractional_part (x1) + |
1265 | _cairo_fixed_fractional_part (x2); |
1265 | _cairo_fixed_fractional_part (x2); |
1266 | cell = coverage_find (sweep_line, ix1); |
1266 | cell = coverage_find (sweep_line, ix1); |
1267 | cell->covered_height += sign * STEP_Y; |
1267 | cell->covered_height += sign * STEP_Y; |
1268 | cell->uncovered_area += sign * frac * STEP_Y; |
1268 | cell->uncovered_area += sign * frac * STEP_Y; |
1269 | return; |
1269 | return; |
1270 | } |
1270 | } |
1271 | 1271 | ||
1272 | coverage_render_cells (sweep_line, x1, x2, 0, STEP_Y, sign); |
1272 | coverage_render_cells (sweep_line, x1, x2, 0, STEP_Y, sign); |
1273 | } |
1273 | } |
1274 | 1274 | ||
1275 | static void |
1275 | static void |
1276 | full_nonzero (sweep_line_t *sweep_line) |
1276 | full_nonzero (sweep_line_t *sweep_line) |
1277 | { |
1277 | { |
1278 | cairo_list_t *pos; |
1278 | cairo_list_t *pos; |
1279 | 1279 | ||
1280 | sweep_line->is_vertical = TRUE; |
1280 | sweep_line->is_vertical = TRUE; |
1281 | pos = sweep_line->active.next; |
1281 | pos = sweep_line->active.next; |
1282 | do { |
1282 | do { |
1283 | edge_t *left = link_to_edge (pos), *right; |
1283 | edge_t *left = link_to_edge (pos), *right; |
1284 | int winding = left->edge.dir; |
1284 | int winding = left->edge.dir; |
1285 | 1285 | ||
1286 | sweep_line->is_vertical &= left->vertical; |
1286 | sweep_line->is_vertical &= left->vertical; |
1287 | 1287 | ||
1288 | pos = left->link.next; |
1288 | pos = left->link.next; |
1289 | do { |
1289 | do { |
1290 | if (unlikely (pos == &sweep_line->active)) { |
1290 | if (unlikely (pos == &sweep_line->active)) { |
1291 | full_add_edge (sweep_line, left, +1); |
1291 | full_add_edge (sweep_line, left, +1); |
1292 | return; |
1292 | return; |
1293 | } |
1293 | } |
1294 | 1294 | ||
1295 | right = link_to_edge (pos); |
1295 | right = link_to_edge (pos); |
1296 | pos = pos->next; |
1296 | pos = pos->next; |
1297 | sweep_line->is_vertical &= right->vertical; |
1297 | sweep_line->is_vertical &= right->vertical; |
1298 | 1298 | ||
1299 | winding += right->edge.dir; |
1299 | winding += right->edge.dir; |
1300 | if (0 == winding) { |
1300 | if (0 == winding) { |
1301 | if (pos == &sweep_line->active || |
1301 | if (pos == &sweep_line->active || |
1302 | link_to_edge (pos)->x.quo != right->x.quo) |
1302 | link_to_edge (pos)->x.quo != right->x.quo) |
1303 | { |
1303 | { |
1304 | break; |
1304 | break; |
1305 | } |
1305 | } |
1306 | } |
1306 | } |
1307 | 1307 | ||
1308 | if (! right->vertical) |
1308 | if (! right->vertical) |
1309 | full_inc_edge (right); |
1309 | full_inc_edge (right); |
1310 | } while (TRUE); |
1310 | } while (TRUE); |
1311 | 1311 | ||
1312 | full_add_edge (sweep_line, left, +1); |
1312 | full_add_edge (sweep_line, left, +1); |
1313 | full_add_edge (sweep_line, right, -1); |
1313 | full_add_edge (sweep_line, right, -1); |
1314 | } while (pos != &sweep_line->active); |
1314 | } while (pos != &sweep_line->active); |
1315 | } |
1315 | } |
1316 | 1316 | ||
1317 | static void |
1317 | static void |
1318 | full_evenodd (sweep_line_t *sweep_line) |
1318 | full_evenodd (sweep_line_t *sweep_line) |
1319 | { |
1319 | { |
1320 | cairo_list_t *pos; |
1320 | cairo_list_t *pos; |
1321 | 1321 | ||
1322 | sweep_line->is_vertical = TRUE; |
1322 | sweep_line->is_vertical = TRUE; |
1323 | pos = sweep_line->active.next; |
1323 | pos = sweep_line->active.next; |
1324 | do { |
1324 | do { |
1325 | edge_t *left = link_to_edge (pos), *right; |
1325 | edge_t *left = link_to_edge (pos), *right; |
1326 | int winding = 0; |
1326 | int winding = 0; |
1327 | 1327 | ||
1328 | sweep_line->is_vertical &= left->vertical; |
1328 | sweep_line->is_vertical &= left->vertical; |
1329 | 1329 | ||
1330 | pos = left->link.next; |
1330 | pos = left->link.next; |
1331 | do { |
1331 | do { |
1332 | if (pos == &sweep_line->active) { |
1332 | if (pos == &sweep_line->active) { |
1333 | full_add_edge (sweep_line, left, +1); |
1333 | full_add_edge (sweep_line, left, +1); |
1334 | return; |
1334 | return; |
1335 | } |
1335 | } |
1336 | 1336 | ||
1337 | right = link_to_edge (pos); |
1337 | right = link_to_edge (pos); |
1338 | pos = pos->next; |
1338 | pos = pos->next; |
1339 | sweep_line->is_vertical &= right->vertical; |
1339 | sweep_line->is_vertical &= right->vertical; |
1340 | 1340 | ||
1341 | if (++winding & 1) { |
1341 | if (++winding & 1) { |
1342 | if (pos == &sweep_line->active || |
1342 | if (pos == &sweep_line->active || |
1343 | link_to_edge (pos)->x.quo != right->x.quo) |
1343 | link_to_edge (pos)->x.quo != right->x.quo) |
1344 | { |
1344 | { |
1345 | break; |
1345 | break; |
1346 | } |
1346 | } |
1347 | } |
1347 | } |
1348 | 1348 | ||
1349 | if (! right->vertical) |
1349 | if (! right->vertical) |
1350 | full_inc_edge (right); |
1350 | full_inc_edge (right); |
1351 | } while (TRUE); |
1351 | } while (TRUE); |
1352 | 1352 | ||
1353 | full_add_edge (sweep_line, left, +1); |
1353 | full_add_edge (sweep_line, left, +1); |
1354 | full_add_edge (sweep_line, right, -1); |
1354 | full_add_edge (sweep_line, right, -1); |
1355 | } while (pos != &sweep_line->active); |
1355 | } while (pos != &sweep_line->active); |
1356 | } |
1356 | } |
1357 | 1357 | ||
1358 | static void |
1358 | static void |
1359 | render_rows (cairo_botor_scan_converter_t *self, |
1359 | render_rows (cairo_botor_scan_converter_t *self, |
1360 | sweep_line_t *sweep_line, |
1360 | sweep_line_t *sweep_line, |
1361 | int y, int height, |
1361 | int y, int height, |
1362 | cairo_span_renderer_t *renderer) |
1362 | cairo_span_renderer_t *renderer) |
1363 | { |
1363 | { |
1364 | cairo_half_open_span_t spans_stack[CAIRO_STACK_ARRAY_LENGTH (cairo_half_open_span_t)]; |
1364 | cairo_half_open_span_t spans_stack[CAIRO_STACK_ARRAY_LENGTH (cairo_half_open_span_t)]; |
1365 | cairo_half_open_span_t *spans = spans_stack; |
1365 | cairo_half_open_span_t *spans = spans_stack; |
1366 | struct cell *cell; |
1366 | struct cell *cell; |
1367 | int prev_x, cover; |
1367 | int prev_x, cover; |
1368 | int num_spans; |
1368 | int num_spans; |
1369 | cairo_status_t status; |
1369 | cairo_status_t status; |
1370 | 1370 | ||
1371 | if (unlikely (sweep_line->coverage.count == 0)) { |
1371 | if (unlikely (sweep_line->coverage.count == 0)) { |
1372 | status = renderer->render_rows (renderer, y, height, NULL, 0); |
1372 | status = renderer->render_rows (renderer, y, height, NULL, 0); |
1373 | if (unlikely (status)) |
1373 | if (unlikely (status)) |
1374 | longjmp (sweep_line->unwind, status); |
1374 | longjmp (sweep_line->unwind, status); |
1375 | return; |
1375 | return; |
1376 | } |
1376 | } |
1377 | 1377 | ||
1378 | /* Allocate enough spans for the row. */ |
1378 | /* Allocate enough spans for the row. */ |
1379 | 1379 | ||
1380 | num_spans = 2*sweep_line->coverage.count+2; |
1380 | num_spans = 2*sweep_line->coverage.count+2; |
1381 | if (unlikely (num_spans > ARRAY_LENGTH (spans_stack))) { |
1381 | if (unlikely (num_spans > ARRAY_LENGTH (spans_stack))) { |
1382 | spans = _cairo_malloc_ab (num_spans, sizeof (cairo_half_open_span_t)); |
1382 | spans = _cairo_malloc_ab (num_spans, sizeof (cairo_half_open_span_t)); |
1383 | if (unlikely (spans == NULL)) { |
1383 | if (unlikely (spans == NULL)) { |
1384 | longjmp (sweep_line->unwind, |
1384 | longjmp (sweep_line->unwind, |
1385 | _cairo_error (CAIRO_STATUS_NO_MEMORY)); |
1385 | _cairo_error (CAIRO_STATUS_NO_MEMORY)); |
1386 | } |
1386 | } |
1387 | } |
1387 | } |
1388 | 1388 | ||
1389 | /* Form the spans from the coverage and areas. */ |
1389 | /* Form the spans from the coverage and areas. */ |
1390 | num_spans = 0; |
1390 | num_spans = 0; |
1391 | prev_x = self->xmin; |
1391 | prev_x = self->xmin; |
1392 | cover = 0; |
1392 | cover = 0; |
1393 | cell = sweep_line->coverage.head.next; |
1393 | cell = sweep_line->coverage.head.next; |
1394 | do { |
1394 | do { |
1395 | int x = cell->x; |
1395 | int x = cell->x; |
1396 | int area; |
1396 | int area; |
1397 | 1397 | ||
1398 | if (x > prev_x) { |
1398 | if (x > prev_x) { |
1399 | spans[num_spans].x = prev_x; |
1399 | spans[num_spans].x = prev_x; |
- | 1400 | spans[num_spans].inverse = 0; |
|
1400 | spans[num_spans].coverage = AREA_TO_ALPHA (cover); |
1401 | spans[num_spans].coverage = AREA_TO_ALPHA (cover); |
1401 | ++num_spans; |
1402 | ++num_spans; |
1402 | } |
1403 | } |
1403 | 1404 | ||
1404 | cover += cell->covered_height*STEP_X*2; |
1405 | cover += cell->covered_height*STEP_X*2; |
1405 | area = cover - cell->uncovered_area; |
1406 | area = cover - cell->uncovered_area; |
1406 | 1407 | ||
1407 | spans[num_spans].x = x; |
1408 | spans[num_spans].x = x; |
1408 | spans[num_spans].coverage = AREA_TO_ALPHA (area); |
1409 | spans[num_spans].coverage = AREA_TO_ALPHA (area); |
1409 | ++num_spans; |
1410 | ++num_spans; |
1410 | 1411 | ||
1411 | prev_x = x + 1; |
1412 | prev_x = x + 1; |
1412 | } while ((cell = cell->next) != &sweep_line->coverage.tail); |
1413 | } while ((cell = cell->next) != &sweep_line->coverage.tail); |
1413 | 1414 | ||
1414 | if (prev_x <= self->xmax) { |
1415 | if (prev_x <= self->xmax) { |
1415 | spans[num_spans].x = prev_x; |
1416 | spans[num_spans].x = prev_x; |
- | 1417 | spans[num_spans].inverse = 0; |
|
1416 | spans[num_spans].coverage = AREA_TO_ALPHA (cover); |
1418 | spans[num_spans].coverage = AREA_TO_ALPHA (cover); |
1417 | ++num_spans; |
1419 | ++num_spans; |
1418 | } |
1420 | } |
1419 | 1421 | ||
1420 | if (cover && prev_x < self->xmax) { |
1422 | if (cover && prev_x < self->xmax) { |
1421 | spans[num_spans].x = self->xmax; |
1423 | spans[num_spans].x = self->xmax; |
- | 1424 | spans[num_spans].inverse = 1; |
|
1422 | spans[num_spans].coverage = 0; |
1425 | spans[num_spans].coverage = 0; |
1423 | ++num_spans; |
1426 | ++num_spans; |
1424 | } |
1427 | } |
1425 | 1428 | ||
1426 | status = renderer->render_rows (renderer, y, height, spans, num_spans); |
1429 | status = renderer->render_rows (renderer, y, height, spans, num_spans); |
1427 | 1430 | ||
1428 | if (unlikely (spans != spans_stack)) |
1431 | if (unlikely (spans != spans_stack)) |
1429 | free (spans); |
1432 | free (spans); |
1430 | 1433 | ||
1431 | coverage_reset (&sweep_line->coverage); |
1434 | coverage_reset (&sweep_line->coverage); |
1432 | 1435 | ||
1433 | if (unlikely (status)) |
1436 | if (unlikely (status)) |
1434 | longjmp (sweep_line->unwind, status); |
1437 | longjmp (sweep_line->unwind, status); |
1435 | } |
1438 | } |
1436 | 1439 | ||
1437 | static void |
1440 | static void |
1438 | full_repeat (sweep_line_t *sweep) |
1441 | full_repeat (sweep_line_t *sweep) |
1439 | { |
1442 | { |
1440 | edge_t *edge; |
1443 | edge_t *edge; |
1441 | 1444 | ||
1442 | cairo_list_foreach_entry (edge, edge_t, &sweep->active, link) { |
1445 | cairo_list_foreach_entry (edge, edge_t, &sweep->active, link) { |
1443 | if (edge->current_sign) |
1446 | if (edge->current_sign) |
1444 | full_add_edge (sweep, edge, edge->current_sign); |
1447 | full_add_edge (sweep, edge, edge->current_sign); |
1445 | else if (! edge->vertical) |
1448 | else if (! edge->vertical) |
1446 | full_inc_edge (edge); |
1449 | full_inc_edge (edge); |
1447 | } |
1450 | } |
1448 | } |
1451 | } |
1449 | 1452 | ||
1450 | static void |
1453 | static void |
1451 | full_reset (sweep_line_t *sweep) |
1454 | full_reset (sweep_line_t *sweep) |
1452 | { |
1455 | { |
1453 | edge_t *edge; |
1456 | edge_t *edge; |
1454 | 1457 | ||
1455 | cairo_list_foreach_entry (edge, edge_t, &sweep->active, link) |
1458 | cairo_list_foreach_entry (edge, edge_t, &sweep->active, link) |
1456 | edge->current_sign = 0; |
1459 | edge->current_sign = 0; |
1457 | } |
1460 | } |
1458 | 1461 | ||
1459 | static void |
1462 | static void |
1460 | full_step (cairo_botor_scan_converter_t *self, |
1463 | full_step (cairo_botor_scan_converter_t *self, |
1461 | sweep_line_t *sweep_line, |
1464 | sweep_line_t *sweep_line, |
1462 | cairo_fixed_t row, |
1465 | cairo_fixed_t row, |
1463 | cairo_span_renderer_t *renderer) |
1466 | cairo_span_renderer_t *renderer) |
1464 | { |
1467 | { |
1465 | int top, bottom; |
1468 | int top, bottom; |
1466 | 1469 | ||
1467 | top = _cairo_fixed_integer_part (sweep_line->current_row); |
1470 | top = _cairo_fixed_integer_part (sweep_line->current_row); |
1468 | bottom = _cairo_fixed_integer_part (row); |
1471 | bottom = _cairo_fixed_integer_part (row); |
1469 | if (cairo_list_is_empty (&sweep_line->active)) { |
1472 | if (cairo_list_is_empty (&sweep_line->active)) { |
1470 | cairo_status_t status; |
1473 | cairo_status_t status; |
1471 | 1474 | ||
1472 | status = renderer->render_rows (renderer, top, bottom - top, NULL, 0); |
1475 | status = renderer->render_rows (renderer, top, bottom - top, NULL, 0); |
1473 | if (unlikely (status)) |
1476 | if (unlikely (status)) |
1474 | longjmp (sweep_line->unwind, status); |
1477 | longjmp (sweep_line->unwind, status); |
1475 | 1478 | ||
1476 | return; |
1479 | return; |
1477 | } |
1480 | } |
1478 | 1481 | ||
1479 | if (self->fill_rule == CAIRO_FILL_RULE_WINDING) |
1482 | if (self->fill_rule == CAIRO_FILL_RULE_WINDING) |
1480 | full_nonzero (sweep_line); |
1483 | full_nonzero (sweep_line); |
1481 | else |
1484 | else |
1482 | full_evenodd (sweep_line); |
1485 | full_evenodd (sweep_line); |
1483 | 1486 | ||
1484 | if (sweep_line->is_vertical || bottom == top + 1) { |
1487 | if (sweep_line->is_vertical || bottom == top + 1) { |
1485 | render_rows (self, sweep_line, top, bottom - top, renderer); |
1488 | render_rows (self, sweep_line, top, bottom - top, renderer); |
1486 | full_reset (sweep_line); |
1489 | full_reset (sweep_line); |
1487 | return; |
1490 | return; |
1488 | } |
1491 | } |
1489 | 1492 | ||
1490 | render_rows (self, sweep_line, top++, 1, renderer); |
1493 | render_rows (self, sweep_line, top++, 1, renderer); |
1491 | do { |
1494 | do { |
1492 | full_repeat (sweep_line); |
1495 | full_repeat (sweep_line); |
1493 | render_rows (self, sweep_line, top, 1, renderer); |
1496 | render_rows (self, sweep_line, top, 1, renderer); |
1494 | } while (++top != bottom); |
1497 | } while (++top != bottom); |
1495 | 1498 | ||
1496 | full_reset (sweep_line); |
1499 | full_reset (sweep_line); |
1497 | } |
1500 | } |
1498 | 1501 | ||
1499 | cairo_always_inline static void |
1502 | cairo_always_inline static void |
1500 | sub_inc_edge (edge_t *edge, |
1503 | sub_inc_edge (edge_t *edge, |
1501 | cairo_fixed_t height) |
1504 | cairo_fixed_t height) |
1502 | { |
1505 | { |
1503 | if (height == 1) { |
1506 | if (height == 1) { |
1504 | edge->x.quo += edge->dxdy.quo; |
1507 | edge->x.quo += edge->dxdy.quo; |
1505 | edge->x.rem += edge->dxdy.rem; |
1508 | edge->x.rem += edge->dxdy.rem; |
1506 | if (edge->x.rem >= 0) { |
1509 | if (edge->x.rem >= 0) { |
1507 | ++edge->x.quo; |
1510 | ++edge->x.quo; |
1508 | edge->x.rem -= edge->dy; |
1511 | edge->x.rem -= edge->dy; |
1509 | } |
1512 | } |
1510 | } else { |
1513 | } else { |
1511 | edge->x.quo += height * edge->dxdy.quo; |
1514 | edge->x.quo += height * edge->dxdy.quo; |
1512 | edge->x.rem += height * edge->dxdy.rem; |
1515 | edge->x.rem += height * edge->dxdy.rem; |
1513 | if (edge->x.rem >= 0) { |
1516 | if (edge->x.rem >= 0) { |
1514 | int carry = edge->x.rem / edge->dy + 1; |
1517 | int carry = edge->x.rem / edge->dy + 1; |
1515 | edge->x.quo += carry; |
1518 | edge->x.quo += carry; |
1516 | edge->x.rem -= carry * edge->dy; |
1519 | edge->x.rem -= carry * edge->dy; |
1517 | } |
1520 | } |
1518 | } |
1521 | } |
1519 | } |
1522 | } |
1520 | 1523 | ||
1521 | static void |
1524 | static void |
1522 | sub_add_run (sweep_line_t *sweep_line, edge_t *edge, int y, int sign) |
1525 | sub_add_run (sweep_line_t *sweep_line, edge_t *edge, int y, int sign) |
1523 | { |
1526 | { |
1524 | struct run *run; |
1527 | struct run *run; |
1525 | 1528 | ||
1526 | run = _cairo_freepool_alloc (&sweep_line->runs); |
1529 | run = _cairo_freepool_alloc (&sweep_line->runs); |
1527 | if (unlikely (run == NULL)) |
1530 | if (unlikely (run == NULL)) |
1528 | longjmp (sweep_line->unwind, _cairo_error (CAIRO_STATUS_NO_MEMORY)); |
1531 | longjmp (sweep_line->unwind, _cairo_error (CAIRO_STATUS_NO_MEMORY)); |
1529 | 1532 | ||
1530 | run->y = y; |
1533 | run->y = y; |
1531 | run->sign = sign; |
1534 | run->sign = sign; |
1532 | run->next = edge->runs; |
1535 | run->next = edge->runs; |
1533 | edge->runs = run; |
1536 | edge->runs = run; |
1534 | 1537 | ||
1535 | edge->current_sign = sign; |
1538 | edge->current_sign = sign; |
1536 | } |
1539 | } |
1537 | 1540 | ||
1538 | inline static cairo_bool_t |
1541 | inline static cairo_bool_t |
1539 | edges_coincident (edge_t *left, edge_t *right, cairo_fixed_t y) |
1542 | edges_coincident (edge_t *left, edge_t *right, cairo_fixed_t y) |
1540 | { |
1543 | { |
1541 | /* XXX is compare_x_for_y() worth executing during sub steps? */ |
1544 | /* XXX is compare_x_for_y() worth executing during sub steps? */ |
1542 | return line_equal (&left->edge.line, &right->edge.line); |
1545 | return line_equal (&left->edge.line, &right->edge.line); |
1543 | //edges_compare_x_for_y (&left->edge, &right->edge, y) >= 0; |
1546 | //edges_compare_x_for_y (&left->edge, &right->edge, y) >= 0; |
1544 | } |
1547 | } |
1545 | 1548 | ||
1546 | static void |
1549 | static void |
1547 | sub_nonzero (sweep_line_t *sweep_line) |
1550 | sub_nonzero (sweep_line_t *sweep_line) |
1548 | { |
1551 | { |
1549 | cairo_fixed_t y = sweep_line->current_subrow; |
1552 | cairo_fixed_t y = sweep_line->current_subrow; |
1550 | cairo_fixed_t fy = _cairo_fixed_fractional_part (y); |
1553 | cairo_fixed_t fy = _cairo_fixed_fractional_part (y); |
1551 | cairo_list_t *pos; |
1554 | cairo_list_t *pos; |
1552 | 1555 | ||
1553 | pos = sweep_line->active.next; |
1556 | pos = sweep_line->active.next; |
1554 | do { |
1557 | do { |
1555 | edge_t *left = link_to_edge (pos), *right; |
1558 | edge_t *left = link_to_edge (pos), *right; |
1556 | int winding = left->edge.dir; |
1559 | int winding = left->edge.dir; |
1557 | 1560 | ||
1558 | pos = left->link.next; |
1561 | pos = left->link.next; |
1559 | do { |
1562 | do { |
1560 | if (unlikely (pos == &sweep_line->active)) { |
1563 | if (unlikely (pos == &sweep_line->active)) { |
1561 | if (left->current_sign != +1) |
1564 | if (left->current_sign != +1) |
1562 | sub_add_run (sweep_line, left, fy, +1); |
1565 | sub_add_run (sweep_line, left, fy, +1); |
1563 | return; |
1566 | return; |
1564 | } |
1567 | } |
1565 | 1568 | ||
1566 | right = link_to_edge (pos); |
1569 | right = link_to_edge (pos); |
1567 | pos = pos->next; |
1570 | pos = pos->next; |
1568 | 1571 | ||
1569 | winding += right->edge.dir; |
1572 | winding += right->edge.dir; |
1570 | if (0 == winding) { |
1573 | if (0 == winding) { |
1571 | if (pos == &sweep_line->active || |
1574 | if (pos == &sweep_line->active || |
1572 | ! edges_coincident (right, link_to_edge (pos), y)) |
1575 | ! edges_coincident (right, link_to_edge (pos), y)) |
1573 | { |
1576 | { |
1574 | break; |
1577 | break; |
1575 | } |
1578 | } |
1576 | } |
1579 | } |
1577 | 1580 | ||
1578 | if (right->current_sign) |
1581 | if (right->current_sign) |
1579 | sub_add_run (sweep_line, right, fy, 0); |
1582 | sub_add_run (sweep_line, right, fy, 0); |
1580 | } while (TRUE); |
1583 | } while (TRUE); |
1581 | 1584 | ||
1582 | if (left->current_sign != +1) |
1585 | if (left->current_sign != +1) |
1583 | sub_add_run (sweep_line, left, fy, +1); |
1586 | sub_add_run (sweep_line, left, fy, +1); |
1584 | if (right->current_sign != -1) |
1587 | if (right->current_sign != -1) |
1585 | sub_add_run (sweep_line, right, fy, -1); |
1588 | sub_add_run (sweep_line, right, fy, -1); |
1586 | } while (pos != &sweep_line->active); |
1589 | } while (pos != &sweep_line->active); |
1587 | } |
1590 | } |
1588 | 1591 | ||
1589 | static void |
1592 | static void |
1590 | sub_evenodd (sweep_line_t *sweep_line) |
1593 | sub_evenodd (sweep_line_t *sweep_line) |
1591 | { |
1594 | { |
1592 | cairo_fixed_t y = sweep_line->current_subrow; |
1595 | cairo_fixed_t y = sweep_line->current_subrow; |
1593 | cairo_fixed_t fy = _cairo_fixed_fractional_part (y); |
1596 | cairo_fixed_t fy = _cairo_fixed_fractional_part (y); |
1594 | cairo_list_t *pos; |
1597 | cairo_list_t *pos; |
1595 | 1598 | ||
1596 | pos = sweep_line->active.next; |
1599 | pos = sweep_line->active.next; |
1597 | do { |
1600 | do { |
1598 | edge_t *left = link_to_edge (pos), *right; |
1601 | edge_t *left = link_to_edge (pos), *right; |
1599 | int winding = 0; |
1602 | int winding = 0; |
1600 | 1603 | ||
1601 | pos = left->link.next; |
1604 | pos = left->link.next; |
1602 | do { |
1605 | do { |
1603 | if (unlikely (pos == &sweep_line->active)) { |
1606 | if (unlikely (pos == &sweep_line->active)) { |
1604 | if (left->current_sign != +1) |
1607 | if (left->current_sign != +1) |
1605 | sub_add_run (sweep_line, left, fy, +1); |
1608 | sub_add_run (sweep_line, left, fy, +1); |
1606 | return; |
1609 | return; |
1607 | } |
1610 | } |
1608 | 1611 | ||
1609 | right = link_to_edge (pos); |
1612 | right = link_to_edge (pos); |
1610 | pos = pos->next; |
1613 | pos = pos->next; |
1611 | 1614 | ||
1612 | if (++winding & 1) { |
1615 | if (++winding & 1) { |
1613 | if (pos == &sweep_line->active || |
1616 | if (pos == &sweep_line->active || |
1614 | ! edges_coincident (right, link_to_edge (pos), y)) |
1617 | ! edges_coincident (right, link_to_edge (pos), y)) |
1615 | { |
1618 | { |
1616 | break; |
1619 | break; |
1617 | } |
1620 | } |
1618 | } |
1621 | } |
1619 | 1622 | ||
1620 | if (right->current_sign) |
1623 | if (right->current_sign) |
1621 | sub_add_run (sweep_line, right, fy, 0); |
1624 | sub_add_run (sweep_line, right, fy, 0); |
1622 | } while (TRUE); |
1625 | } while (TRUE); |
1623 | 1626 | ||
1624 | if (left->current_sign != +1) |
1627 | if (left->current_sign != +1) |
1625 | sub_add_run (sweep_line, left, fy, +1); |
1628 | sub_add_run (sweep_line, left, fy, +1); |
1626 | if (right->current_sign != -1) |
1629 | if (right->current_sign != -1) |
1627 | sub_add_run (sweep_line, right, fy, -1); |
1630 | sub_add_run (sweep_line, right, fy, -1); |
1628 | } while (pos != &sweep_line->active); |
1631 | } while (pos != &sweep_line->active); |
1629 | } |
1632 | } |
1630 | 1633 | ||
1631 | cairo_always_inline static void |
1634 | cairo_always_inline static void |
1632 | sub_step (cairo_botor_scan_converter_t *self, |
1635 | sub_step (cairo_botor_scan_converter_t *self, |
1633 | sweep_line_t *sweep_line) |
1636 | sweep_line_t *sweep_line) |
1634 | { |
1637 | { |
1635 | if (cairo_list_is_empty (&sweep_line->active)) |
1638 | if (cairo_list_is_empty (&sweep_line->active)) |
1636 | return; |
1639 | return; |
1637 | 1640 | ||
1638 | if (self->fill_rule == CAIRO_FILL_RULE_WINDING) |
1641 | if (self->fill_rule == CAIRO_FILL_RULE_WINDING) |
1639 | sub_nonzero (sweep_line); |
1642 | sub_nonzero (sweep_line); |
1640 | else |
1643 | else |
1641 | sub_evenodd (sweep_line); |
1644 | sub_evenodd (sweep_line); |
1642 | } |
1645 | } |
1643 | 1646 | ||
1644 | static void |
1647 | static void |
1645 | coverage_render_runs (sweep_line_t *sweep, edge_t *edge, |
1648 | coverage_render_runs (sweep_line_t *sweep, edge_t *edge, |
1646 | cairo_fixed_t y1, cairo_fixed_t y2) |
1649 | cairo_fixed_t y1, cairo_fixed_t y2) |
1647 | { |
1650 | { |
1648 | struct run tail; |
1651 | struct run tail; |
1649 | struct run *run = &tail; |
1652 | struct run *run = &tail; |
1650 | 1653 | ||
1651 | tail.next = NULL; |
1654 | tail.next = NULL; |
1652 | tail.y = y2; |
1655 | tail.y = y2; |
1653 | 1656 | ||
1654 | /* Order the runs top->bottom */ |
1657 | /* Order the runs top->bottom */ |
1655 | while (edge->runs) { |
1658 | while (edge->runs) { |
1656 | struct run *r; |
1659 | struct run *r; |
1657 | 1660 | ||
1658 | r = edge->runs; |
1661 | r = edge->runs; |
1659 | edge->runs = r->next; |
1662 | edge->runs = r->next; |
1660 | r->next = run; |
1663 | r->next = run; |
1661 | run = r; |
1664 | run = r; |
1662 | } |
1665 | } |
1663 | 1666 | ||
1664 | if (run->y > y1) |
1667 | if (run->y > y1) |
1665 | sub_inc_edge (edge, run->y - y1); |
1668 | sub_inc_edge (edge, run->y - y1); |
1666 | 1669 | ||
1667 | do { |
1670 | do { |
1668 | cairo_fixed_t x1, x2; |
1671 | cairo_fixed_t x1, x2; |
1669 | 1672 | ||
1670 | y1 = run->y; |
1673 | y1 = run->y; |
1671 | y2 = run->next->y; |
1674 | y2 = run->next->y; |
1672 | 1675 | ||
1673 | x1 = edge->x.quo; |
1676 | x1 = edge->x.quo; |
1674 | if (y2 - y1 == STEP_Y) |
1677 | if (y2 - y1 == STEP_Y) |
1675 | full_inc_edge (edge); |
1678 | full_inc_edge (edge); |
1676 | else |
1679 | else |
1677 | sub_inc_edge (edge, y2 - y1); |
1680 | sub_inc_edge (edge, y2 - y1); |
1678 | x2 = edge->x.quo; |
1681 | x2 = edge->x.quo; |
1679 | 1682 | ||
1680 | if (run->sign) { |
1683 | if (run->sign) { |
1681 | int ix1, ix2; |
1684 | int ix1, ix2; |
1682 | 1685 | ||
1683 | ix1 = _cairo_fixed_integer_part (x1); |
1686 | ix1 = _cairo_fixed_integer_part (x1); |
1684 | ix2 = _cairo_fixed_integer_part (x2); |
1687 | ix2 = _cairo_fixed_integer_part (x2); |
1685 | 1688 | ||
1686 | /* Edge is entirely within a column? */ |
1689 | /* Edge is entirely within a column? */ |
1687 | if (likely (ix1 == ix2)) { |
1690 | if (likely (ix1 == ix2)) { |
1688 | struct cell *cell; |
1691 | struct cell *cell; |
1689 | int frac; |
1692 | int frac; |
1690 | 1693 | ||
1691 | frac = _cairo_fixed_fractional_part (x1) + |
1694 | frac = _cairo_fixed_fractional_part (x1) + |
1692 | _cairo_fixed_fractional_part (x2); |
1695 | _cairo_fixed_fractional_part (x2); |
1693 | cell = coverage_find (sweep, ix1); |
1696 | cell = coverage_find (sweep, ix1); |
1694 | cell->covered_height += run->sign * (y2 - y1); |
1697 | cell->covered_height += run->sign * (y2 - y1); |
1695 | cell->uncovered_area += run->sign * (y2 - y1) * frac; |
1698 | cell->uncovered_area += run->sign * (y2 - y1) * frac; |
1696 | } else { |
1699 | } else { |
1697 | coverage_render_cells (sweep, x1, x2, y1, y2, run->sign); |
1700 | coverage_render_cells (sweep, x1, x2, y1, y2, run->sign); |
1698 | } |
1701 | } |
1699 | } |
1702 | } |
1700 | 1703 | ||
1701 | run = run->next; |
1704 | run = run->next; |
1702 | } while (run->next != NULL); |
1705 | } while (run->next != NULL); |
1703 | } |
1706 | } |
1704 | 1707 | ||
1705 | static void |
1708 | static void |
1706 | coverage_render_vertical_runs (sweep_line_t *sweep, edge_t *edge, cairo_fixed_t y2) |
1709 | coverage_render_vertical_runs (sweep_line_t *sweep, edge_t *edge, cairo_fixed_t y2) |
1707 | { |
1710 | { |
1708 | struct cell *cell; |
1711 | struct cell *cell; |
1709 | struct run *run; |
1712 | struct run *run; |
1710 | int height = 0; |
1713 | int height = 0; |
1711 | 1714 | ||
1712 | for (run = edge->runs; run != NULL; run = run->next) { |
1715 | for (run = edge->runs; run != NULL; run = run->next) { |
1713 | if (run->sign) |
1716 | if (run->sign) |
1714 | height += run->sign * (y2 - run->y); |
1717 | height += run->sign * (y2 - run->y); |
1715 | y2 = run->y; |
1718 | y2 = run->y; |
1716 | } |
1719 | } |
1717 | 1720 | ||
1718 | cell = coverage_find (sweep, _cairo_fixed_integer_part (edge->x.quo)); |
1721 | cell = coverage_find (sweep, _cairo_fixed_integer_part (edge->x.quo)); |
1719 | cell->covered_height += height; |
1722 | cell->covered_height += height; |
1720 | cell->uncovered_area += 2 * _cairo_fixed_fractional_part (edge->x.quo) * height; |
1723 | cell->uncovered_area += 2 * _cairo_fixed_fractional_part (edge->x.quo) * height; |
1721 | } |
1724 | } |
1722 | 1725 | ||
1723 | cairo_always_inline static void |
1726 | cairo_always_inline static void |
1724 | sub_emit (cairo_botor_scan_converter_t *self, |
1727 | sub_emit (cairo_botor_scan_converter_t *self, |
1725 | sweep_line_t *sweep, |
1728 | sweep_line_t *sweep, |
1726 | cairo_span_renderer_t *renderer) |
1729 | cairo_span_renderer_t *renderer) |
1727 | { |
1730 | { |
1728 | edge_t *edge; |
1731 | edge_t *edge; |
1729 | 1732 | ||
1730 | sub_step (self, sweep); |
1733 | sub_step (self, sweep); |
1731 | 1734 | ||
1732 | /* convert the runs into coverages */ |
1735 | /* convert the runs into coverages */ |
1733 | 1736 | ||
1734 | cairo_list_foreach_entry (edge, edge_t, &sweep->active, link) { |
1737 | cairo_list_foreach_entry (edge, edge_t, &sweep->active, link) { |
1735 | if (edge->runs == NULL) { |
1738 | if (edge->runs == NULL) { |
1736 | if (! edge->vertical) { |
1739 | if (! edge->vertical) { |
1737 | if (edge->flags & START) { |
1740 | if (edge->flags & START) { |
1738 | sub_inc_edge (edge, |
1741 | sub_inc_edge (edge, |
1739 | STEP_Y - _cairo_fixed_fractional_part (edge->edge.top)); |
1742 | STEP_Y - _cairo_fixed_fractional_part (edge->edge.top)); |
1740 | edge->flags &= ~START; |
1743 | edge->flags &= ~START; |
1741 | } else |
1744 | } else |
1742 | full_inc_edge (edge); |
1745 | full_inc_edge (edge); |
1743 | } |
1746 | } |
1744 | } else { |
1747 | } else { |
1745 | if (edge->vertical) { |
1748 | if (edge->vertical) { |
1746 | coverage_render_vertical_runs (sweep, edge, STEP_Y); |
1749 | coverage_render_vertical_runs (sweep, edge, STEP_Y); |
1747 | } else { |
1750 | } else { |
1748 | int y1 = 0; |
1751 | int y1 = 0; |
1749 | if (edge->flags & START) { |
1752 | if (edge->flags & START) { |
1750 | y1 = _cairo_fixed_fractional_part (edge->edge.top); |
1753 | y1 = _cairo_fixed_fractional_part (edge->edge.top); |
1751 | edge->flags &= ~START; |
1754 | edge->flags &= ~START; |
1752 | } |
1755 | } |
1753 | coverage_render_runs (sweep, edge, y1, STEP_Y); |
1756 | coverage_render_runs (sweep, edge, y1, STEP_Y); |
1754 | } |
1757 | } |
1755 | } |
1758 | } |
1756 | edge->current_sign = 0; |
1759 | edge->current_sign = 0; |
1757 | edge->runs = NULL; |
1760 | edge->runs = NULL; |
1758 | } |
1761 | } |
1759 | 1762 | ||
1760 | cairo_list_foreach_entry (edge, edge_t, &sweep->stopped, link) { |
1763 | cairo_list_foreach_entry (edge, edge_t, &sweep->stopped, link) { |
1761 | int y2 = _cairo_fixed_fractional_part (edge->edge.bottom); |
1764 | int y2 = _cairo_fixed_fractional_part (edge->edge.bottom); |
1762 | if (edge->vertical) { |
1765 | if (edge->vertical) { |
1763 | coverage_render_vertical_runs (sweep, edge, y2); |
1766 | coverage_render_vertical_runs (sweep, edge, y2); |
1764 | } else { |
1767 | } else { |
1765 | int y1 = 0; |
1768 | int y1 = 0; |
1766 | if (edge->flags & START) |
1769 | if (edge->flags & START) |
1767 | y1 = _cairo_fixed_fractional_part (edge->edge.top); |
1770 | y1 = _cairo_fixed_fractional_part (edge->edge.top); |
1768 | coverage_render_runs (sweep, edge, y1, y2); |
1771 | coverage_render_runs (sweep, edge, y1, y2); |
1769 | } |
1772 | } |
1770 | } |
1773 | } |
1771 | cairo_list_init (&sweep->stopped); |
1774 | cairo_list_init (&sweep->stopped); |
1772 | 1775 | ||
1773 | _cairo_freepool_reset (&sweep->runs); |
1776 | _cairo_freepool_reset (&sweep->runs); |
1774 | 1777 | ||
1775 | render_rows (self, sweep, |
1778 | render_rows (self, sweep, |
1776 | _cairo_fixed_integer_part (sweep->current_row), 1, |
1779 | _cairo_fixed_integer_part (sweep->current_row), 1, |
1777 | renderer); |
1780 | renderer); |
1778 | } |
1781 | } |
1779 | 1782 | ||
1780 | static void |
1783 | static void |
1781 | sweep_line_init (sweep_line_t *sweep_line, |
1784 | sweep_line_init (sweep_line_t *sweep_line, |
1782 | event_t **start_events, |
1785 | event_t **start_events, |
1783 | int num_events) |
1786 | int num_events) |
1784 | { |
1787 | { |
1785 | cairo_list_init (&sweep_line->active); |
1788 | cairo_list_init (&sweep_line->active); |
1786 | cairo_list_init (&sweep_line->stopped); |
1789 | cairo_list_init (&sweep_line->stopped); |
1787 | sweep_line->insert_cursor = &sweep_line->active; |
1790 | sweep_line->insert_cursor = &sweep_line->active; |
1788 | 1791 | ||
1789 | sweep_line->current_row = INT32_MIN; |
1792 | sweep_line->current_row = INT32_MIN; |
1790 | sweep_line->current_subrow = INT32_MIN; |
1793 | sweep_line->current_subrow = INT32_MIN; |
1791 | 1794 | ||
1792 | coverage_init (&sweep_line->coverage); |
1795 | coverage_init (&sweep_line->coverage); |
1793 | _cairo_freepool_init (&sweep_line->runs, sizeof (struct run)); |
1796 | _cairo_freepool_init (&sweep_line->runs, sizeof (struct run)); |
1794 | 1797 | ||
1795 | start_event_sort (start_events, num_events); |
1798 | start_event_sort (start_events, num_events); |
1796 | start_events[num_events] = NULL; |
1799 | start_events[num_events] = NULL; |
1797 | 1800 | ||
1798 | sweep_line->queue.start_events = start_events; |
1801 | sweep_line->queue.start_events = start_events; |
1799 | 1802 | ||
1800 | _cairo_freepool_init (&sweep_line->queue.pool, |
1803 | _cairo_freepool_init (&sweep_line->queue.pool, |
1801 | sizeof (queue_event_t)); |
1804 | sizeof (queue_event_t)); |
1802 | pqueue_init (&sweep_line->queue.pq); |
1805 | pqueue_init (&sweep_line->queue.pq); |
1803 | sweep_line->queue.pq.elements[PQ_FIRST_ENTRY] = NULL; |
1806 | sweep_line->queue.pq.elements[PQ_FIRST_ENTRY] = NULL; |
1804 | } |
1807 | } |
1805 | 1808 | ||
1806 | static void |
1809 | static void |
1807 | sweep_line_delete (sweep_line_t *sweep_line, |
1810 | sweep_line_delete (sweep_line_t *sweep_line, |
1808 | edge_t *edge) |
1811 | edge_t *edge) |
1809 | { |
1812 | { |
1810 | if (sweep_line->insert_cursor == &edge->link) |
1813 | if (sweep_line->insert_cursor == &edge->link) |
1811 | sweep_line->insert_cursor = edge->link.prev; |
1814 | sweep_line->insert_cursor = edge->link.prev; |
1812 | 1815 | ||
1813 | cairo_list_del (&edge->link); |
1816 | cairo_list_del (&edge->link); |
1814 | if (edge->runs) |
1817 | if (edge->runs) |
1815 | cairo_list_add_tail (&edge->link, &sweep_line->stopped); |
1818 | cairo_list_add_tail (&edge->link, &sweep_line->stopped); |
1816 | edge->flags |= STOP; |
1819 | edge->flags |= STOP; |
1817 | } |
1820 | } |
1818 | 1821 | ||
1819 | static void |
1822 | static void |
1820 | sweep_line_swap (sweep_line_t *sweep_line, |
1823 | sweep_line_swap (sweep_line_t *sweep_line, |
1821 | edge_t *left, |
1824 | edge_t *left, |
1822 | edge_t *right) |
1825 | edge_t *right) |
1823 | { |
1826 | { |
1824 | right->link.prev = left->link.prev; |
1827 | right->link.prev = left->link.prev; |
1825 | left->link.next = right->link.next; |
1828 | left->link.next = right->link.next; |
1826 | right->link.next = &left->link; |
1829 | right->link.next = &left->link; |
1827 | left->link.prev = &right->link; |
1830 | left->link.prev = &right->link; |
1828 | left->link.next->prev = &left->link; |
1831 | left->link.next->prev = &left->link; |
1829 | right->link.prev->next = &right->link; |
1832 | right->link.prev->next = &right->link; |
1830 | } |
1833 | } |
1831 | 1834 | ||
1832 | static void |
1835 | static void |
1833 | sweep_line_fini (sweep_line_t *sweep_line) |
1836 | sweep_line_fini (sweep_line_t *sweep_line) |
1834 | { |
1837 | { |
1835 | pqueue_fini (&sweep_line->queue.pq); |
1838 | pqueue_fini (&sweep_line->queue.pq); |
1836 | _cairo_freepool_fini (&sweep_line->queue.pool); |
1839 | _cairo_freepool_fini (&sweep_line->queue.pool); |
1837 | coverage_fini (&sweep_line->coverage); |
1840 | coverage_fini (&sweep_line->coverage); |
1838 | _cairo_freepool_fini (&sweep_line->runs); |
1841 | _cairo_freepool_fini (&sweep_line->runs); |
1839 | } |
1842 | } |
1840 | 1843 | ||
1841 | static cairo_status_t |
1844 | static cairo_status_t |
1842 | botor_generate (cairo_botor_scan_converter_t *self, |
1845 | botor_generate (cairo_botor_scan_converter_t *self, |
1843 | event_t **start_events, |
1846 | event_t **start_events, |
1844 | cairo_span_renderer_t *renderer) |
1847 | cairo_span_renderer_t *renderer) |
1845 | { |
1848 | { |
1846 | cairo_status_t status; |
1849 | cairo_status_t status; |
1847 | sweep_line_t sweep_line; |
1850 | sweep_line_t sweep_line; |
1848 | cairo_fixed_t ybot; |
1851 | cairo_fixed_t ybot; |
1849 | event_t *event; |
1852 | event_t *event; |
1850 | cairo_list_t *left, *right; |
1853 | cairo_list_t *left, *right; |
1851 | edge_t *e1, *e2; |
1854 | edge_t *e1, *e2; |
1852 | int bottom; |
1855 | int bottom; |
1853 | 1856 | ||
1854 | sweep_line_init (&sweep_line, start_events, self->num_edges); |
1857 | sweep_line_init (&sweep_line, start_events, self->num_edges); |
1855 | if ((status = setjmp (sweep_line.unwind))) |
1858 | if ((status = setjmp (sweep_line.unwind))) |
1856 | goto unwind; |
1859 | goto unwind; |
1857 | 1860 | ||
1858 | ybot = self->extents.p2.y; |
1861 | ybot = self->extents.p2.y; |
1859 | sweep_line.current_subrow = self->extents.p1.y; |
1862 | sweep_line.current_subrow = self->extents.p1.y; |
1860 | sweep_line.current_row = _cairo_fixed_floor (self->extents.p1.y); |
1863 | sweep_line.current_row = _cairo_fixed_floor (self->extents.p1.y); |
1861 | event = *sweep_line.queue.start_events++; |
1864 | event = *sweep_line.queue.start_events++; |
1862 | do { |
1865 | do { |
1863 | /* Can we process a full step in one go? */ |
1866 | /* Can we process a full step in one go? */ |
1864 | if (event->y >= sweep_line.current_row + STEP_Y) { |
1867 | if (event->y >= sweep_line.current_row + STEP_Y) { |
1865 | bottom = _cairo_fixed_floor (event->y); |
1868 | bottom = _cairo_fixed_floor (event->y); |
1866 | full_step (self, &sweep_line, bottom, renderer); |
1869 | full_step (self, &sweep_line, bottom, renderer); |
1867 | sweep_line.current_row = bottom; |
1870 | sweep_line.current_row = bottom; |
1868 | sweep_line.current_subrow = bottom; |
1871 | sweep_line.current_subrow = bottom; |
1869 | } |
1872 | } |
1870 | 1873 | ||
1871 | do { |
1874 | do { |
1872 | if (event->y > sweep_line.current_subrow) { |
1875 | if (event->y > sweep_line.current_subrow) { |
1873 | sub_step (self, &sweep_line); |
1876 | sub_step (self, &sweep_line); |
1874 | sweep_line.current_subrow = event->y; |
1877 | sweep_line.current_subrow = event->y; |
1875 | } |
1878 | } |
1876 | 1879 | ||
1877 | do { |
1880 | do { |
1878 | /* Update the active list using Bentley-Ottmann */ |
1881 | /* Update the active list using Bentley-Ottmann */ |
1879 | switch (event->type) { |
1882 | switch (event->type) { |
1880 | case EVENT_TYPE_START: |
1883 | case EVENT_TYPE_START: |
1881 | e1 = ((start_event_t *) event)->edge; |
1884 | e1 = ((start_event_t *) event)->edge; |
1882 | 1885 | ||
1883 | sweep_line_insert (&sweep_line, e1); |
1886 | sweep_line_insert (&sweep_line, e1); |
1884 | event_insert_stop (&sweep_line, e1); |
1887 | event_insert_stop (&sweep_line, e1); |
1885 | 1888 | ||
1886 | left = e1->link.prev; |
1889 | left = e1->link.prev; |
1887 | right = e1->link.next; |
1890 | right = e1->link.next; |
1888 | 1891 | ||
1889 | if (left != &sweep_line.active) { |
1892 | if (left != &sweep_line.active) { |
1890 | event_insert_if_intersect_below_current_y (&sweep_line, |
1893 | event_insert_if_intersect_below_current_y (&sweep_line, |
1891 | link_to_edge (left), e1); |
1894 | link_to_edge (left), e1); |
1892 | } |
1895 | } |
1893 | 1896 | ||
1894 | if (right != &sweep_line.active) { |
1897 | if (right != &sweep_line.active) { |
1895 | event_insert_if_intersect_below_current_y (&sweep_line, |
1898 | event_insert_if_intersect_below_current_y (&sweep_line, |
1896 | e1, link_to_edge (right)); |
1899 | e1, link_to_edge (right)); |
1897 | } |
1900 | } |
1898 | 1901 | ||
1899 | break; |
1902 | break; |
1900 | 1903 | ||
1901 | case EVENT_TYPE_STOP: |
1904 | case EVENT_TYPE_STOP: |
1902 | e1 = ((queue_event_t *) event)->e1; |
1905 | e1 = ((queue_event_t *) event)->e1; |
1903 | event_delete (&sweep_line, event); |
1906 | event_delete (&sweep_line, event); |
1904 | 1907 | ||
1905 | left = e1->link.prev; |
1908 | left = e1->link.prev; |
1906 | right = e1->link.next; |
1909 | right = e1->link.next; |
1907 | 1910 | ||
1908 | sweep_line_delete (&sweep_line, e1); |
1911 | sweep_line_delete (&sweep_line, e1); |
1909 | 1912 | ||
1910 | if (left != &sweep_line.active && |
1913 | if (left != &sweep_line.active && |
1911 | right != &sweep_line.active) |
1914 | right != &sweep_line.active) |
1912 | { |
1915 | { |
1913 | event_insert_if_intersect_below_current_y (&sweep_line, |
1916 | event_insert_if_intersect_below_current_y (&sweep_line, |
1914 | link_to_edge (left), |
1917 | link_to_edge (left), |
1915 | link_to_edge (right)); |
1918 | link_to_edge (right)); |
1916 | } |
1919 | } |
1917 | 1920 | ||
1918 | break; |
1921 | break; |
1919 | 1922 | ||
1920 | case EVENT_TYPE_INTERSECTION: |
1923 | case EVENT_TYPE_INTERSECTION: |
1921 | e1 = ((queue_event_t *) event)->e1; |
1924 | e1 = ((queue_event_t *) event)->e1; |
1922 | e2 = ((queue_event_t *) event)->e2; |
1925 | e2 = ((queue_event_t *) event)->e2; |
1923 | 1926 | ||
1924 | event_delete (&sweep_line, event); |
1927 | event_delete (&sweep_line, event); |
1925 | if (e1->flags & STOP) |
1928 | if (e1->flags & STOP) |
1926 | break; |
1929 | break; |
1927 | if (e2->flags & STOP) |
1930 | if (e2->flags & STOP) |
1928 | break; |
1931 | break; |
1929 | 1932 | ||
1930 | /* skip this intersection if its edges are not adjacent */ |
1933 | /* skip this intersection if its edges are not adjacent */ |
1931 | if (&e2->link != e1->link.next) |
1934 | if (&e2->link != e1->link.next) |
1932 | break; |
1935 | break; |
1933 | 1936 | ||
1934 | left = e1->link.prev; |
1937 | left = e1->link.prev; |
1935 | right = e2->link.next; |
1938 | right = e2->link.next; |
1936 | 1939 | ||
1937 | sweep_line_swap (&sweep_line, e1, e2); |
1940 | sweep_line_swap (&sweep_line, e1, e2); |
1938 | 1941 | ||
1939 | /* after the swap e2 is left of e1 */ |
1942 | /* after the swap e2 is left of e1 */ |
1940 | if (left != &sweep_line.active) { |
1943 | if (left != &sweep_line.active) { |
1941 | event_insert_if_intersect_below_current_y (&sweep_line, |
1944 | event_insert_if_intersect_below_current_y (&sweep_line, |
1942 | link_to_edge (left), e2); |
1945 | link_to_edge (left), e2); |
1943 | } |
1946 | } |
1944 | 1947 | ||
1945 | if (right != &sweep_line.active) { |
1948 | if (right != &sweep_line.active) { |
1946 | event_insert_if_intersect_below_current_y (&sweep_line, |
1949 | event_insert_if_intersect_below_current_y (&sweep_line, |
1947 | e1, link_to_edge (right)); |
1950 | e1, link_to_edge (right)); |
1948 | } |
1951 | } |
1949 | 1952 | ||
1950 | break; |
1953 | break; |
1951 | } |
1954 | } |
1952 | 1955 | ||
1953 | event = event_next (&sweep_line); |
1956 | event = event_next (&sweep_line); |
1954 | if (event == NULL) |
1957 | if (event == NULL) |
1955 | goto end; |
1958 | goto end; |
1956 | } while (event->y == sweep_line.current_subrow); |
1959 | } while (event->y == sweep_line.current_subrow); |
1957 | } while (event->y < sweep_line.current_row + STEP_Y); |
1960 | } while (event->y < sweep_line.current_row + STEP_Y); |
1958 | 1961 | ||
1959 | bottom = sweep_line.current_row + STEP_Y; |
1962 | bottom = sweep_line.current_row + STEP_Y; |
1960 | sub_emit (self, &sweep_line, renderer); |
1963 | sub_emit (self, &sweep_line, renderer); |
1961 | sweep_line.current_subrow = bottom; |
1964 | sweep_line.current_subrow = bottom; |
1962 | sweep_line.current_row = sweep_line.current_subrow; |
1965 | sweep_line.current_row = sweep_line.current_subrow; |
1963 | } while (TRUE); |
1966 | } while (TRUE); |
1964 | 1967 | ||
1965 | end: |
1968 | end: |
1966 | /* flush any partial spans */ |
1969 | /* flush any partial spans */ |
1967 | if (sweep_line.current_subrow != sweep_line.current_row) { |
1970 | if (sweep_line.current_subrow != sweep_line.current_row) { |
1968 | sub_emit (self, &sweep_line, renderer); |
1971 | sub_emit (self, &sweep_line, renderer); |
1969 | sweep_line.current_row += STEP_Y; |
1972 | sweep_line.current_row += STEP_Y; |
1970 | sweep_line.current_subrow = sweep_line.current_row; |
1973 | sweep_line.current_subrow = sweep_line.current_row; |
1971 | } |
1974 | } |
1972 | /* clear the rest */ |
1975 | /* clear the rest */ |
1973 | if (sweep_line.current_subrow < ybot) { |
1976 | if (sweep_line.current_subrow < ybot) { |
1974 | bottom = _cairo_fixed_integer_part (sweep_line.current_row); |
1977 | bottom = _cairo_fixed_integer_part (sweep_line.current_row); |
1975 | status = renderer->render_rows (renderer, |
1978 | status = renderer->render_rows (renderer, |
1976 | bottom, _cairo_fixed_integer_ceil (ybot) - bottom, |
1979 | bottom, _cairo_fixed_integer_ceil (ybot) - bottom, |
1977 | NULL, 0); |
1980 | NULL, 0); |
1978 | } |
1981 | } |
1979 | 1982 | ||
1980 | unwind: |
1983 | unwind: |
1981 | sweep_line_fini (&sweep_line); |
1984 | sweep_line_fini (&sweep_line); |
1982 | 1985 | ||
1983 | return status; |
1986 | return status; |
1984 | } |
1987 | } |
1985 | 1988 | ||
1986 | static cairo_status_t |
1989 | static cairo_status_t |
1987 | _cairo_botor_scan_converter_generate (void *converter, |
1990 | _cairo_botor_scan_converter_generate (void *converter, |
1988 | cairo_span_renderer_t *renderer) |
1991 | cairo_span_renderer_t *renderer) |
1989 | { |
1992 | { |
1990 | cairo_botor_scan_converter_t *self = converter; |
1993 | cairo_botor_scan_converter_t *self = converter; |
1991 | start_event_t stack_events[CAIRO_STACK_ARRAY_LENGTH (start_event_t)]; |
1994 | start_event_t stack_events[CAIRO_STACK_ARRAY_LENGTH (start_event_t)]; |
1992 | start_event_t *events; |
1995 | start_event_t *events; |
1993 | event_t *stack_event_ptrs[ARRAY_LENGTH (stack_events) + 1]; |
1996 | event_t *stack_event_ptrs[ARRAY_LENGTH (stack_events) + 1]; |
1994 | event_t **event_ptrs; |
1997 | event_t **event_ptrs; |
1995 | struct _cairo_botor_scan_converter_chunk *chunk; |
1998 | struct _cairo_botor_scan_converter_chunk *chunk; |
1996 | cairo_status_t status; |
1999 | cairo_status_t status; |
1997 | int num_events; |
2000 | int num_events; |
1998 | int i, j; |
2001 | int i, j; |
1999 | 2002 | ||
2000 | num_events = self->num_edges; |
2003 | num_events = self->num_edges; |
2001 | if (unlikely (0 == num_events)) { |
2004 | if (unlikely (0 == num_events)) { |
2002 | return renderer->render_rows (renderer, |
2005 | return renderer->render_rows (renderer, |
2003 | _cairo_fixed_integer_floor (self->extents.p1.y), |
2006 | _cairo_fixed_integer_floor (self->extents.p1.y), |
2004 | _cairo_fixed_integer_ceil (self->extents.p2.y) - |
2007 | _cairo_fixed_integer_ceil (self->extents.p2.y) - |
2005 | _cairo_fixed_integer_floor (self->extents.p1.y), |
2008 | _cairo_fixed_integer_floor (self->extents.p1.y), |
2006 | NULL, 0); |
2009 | NULL, 0); |
2007 | } |
2010 | } |
2008 | 2011 | ||
2009 | events = stack_events; |
2012 | events = stack_events; |
2010 | event_ptrs = stack_event_ptrs; |
2013 | event_ptrs = stack_event_ptrs; |
2011 | if (unlikely (num_events >= ARRAY_LENGTH (stack_events))) { |
2014 | if (unlikely (num_events >= ARRAY_LENGTH (stack_events))) { |
2012 | events = _cairo_malloc_ab_plus_c (num_events, |
2015 | events = _cairo_malloc_ab_plus_c (num_events, |
2013 | sizeof (start_event_t) + sizeof (event_t *), |
2016 | sizeof (start_event_t) + sizeof (event_t *), |
2014 | sizeof (event_t *)); |
2017 | sizeof (event_t *)); |
2015 | if (unlikely (events == NULL)) |
2018 | if (unlikely (events == NULL)) |
2016 | return _cairo_error (CAIRO_STATUS_NO_MEMORY); |
2019 | return _cairo_error (CAIRO_STATUS_NO_MEMORY); |
2017 | 2020 | ||
2018 | event_ptrs = (event_t **) (events + num_events); |
2021 | event_ptrs = (event_t **) (events + num_events); |
2019 | } |
2022 | } |
2020 | 2023 | ||
2021 | j = 0; |
2024 | j = 0; |
2022 | for (chunk = &self->chunks; chunk != NULL; chunk = chunk->next) { |
2025 | for (chunk = &self->chunks; chunk != NULL; chunk = chunk->next) { |
2023 | edge_t *edge; |
2026 | edge_t *edge; |
2024 | 2027 | ||
2025 | edge = chunk->base; |
2028 | edge = chunk->base; |
2026 | for (i = 0; i < chunk->count; i++) { |
2029 | for (i = 0; i < chunk->count; i++) { |
2027 | event_ptrs[j] = (event_t *) &events[j]; |
2030 | event_ptrs[j] = (event_t *) &events[j]; |
2028 | 2031 | ||
2029 | events[j].y = edge->edge.top; |
2032 | events[j].y = edge->edge.top; |
2030 | events[j].type = EVENT_TYPE_START; |
2033 | events[j].type = EVENT_TYPE_START; |
2031 | events[j].edge = edge; |
2034 | events[j].edge = edge; |
2032 | 2035 | ||
2033 | edge++, j++; |
2036 | edge++, j++; |
2034 | } |
2037 | } |
2035 | } |
2038 | } |
2036 | 2039 | ||
2037 | status = botor_generate (self, event_ptrs, renderer); |
2040 | status = botor_generate (self, event_ptrs, renderer); |
2038 | 2041 | ||
2039 | if (events != stack_events) |
2042 | if (events != stack_events) |
2040 | free (events); |
2043 | free (events); |
2041 | 2044 | ||
2042 | return status; |
2045 | return status; |
2043 | } |
2046 | } |
2044 | 2047 | ||
2045 | static edge_t * |
2048 | static edge_t * |
2046 | botor_allocate_edge (cairo_botor_scan_converter_t *self) |
2049 | botor_allocate_edge (cairo_botor_scan_converter_t *self) |
2047 | { |
2050 | { |
2048 | struct _cairo_botor_scan_converter_chunk *chunk; |
2051 | struct _cairo_botor_scan_converter_chunk *chunk; |
2049 | 2052 | ||
2050 | chunk = self->tail; |
2053 | chunk = self->tail; |
2051 | if (chunk->count == chunk->size) { |
2054 | if (chunk->count == chunk->size) { |
2052 | int size; |
2055 | int size; |
2053 | 2056 | ||
2054 | size = chunk->size * 2; |
2057 | size = chunk->size * 2; |
2055 | chunk->next = _cairo_malloc_ab_plus_c (size, |
2058 | chunk->next = _cairo_malloc_ab_plus_c (size, |
2056 | sizeof (edge_t), |
2059 | sizeof (edge_t), |
2057 | sizeof (struct _cairo_botor_scan_converter_chunk)); |
2060 | sizeof (struct _cairo_botor_scan_converter_chunk)); |
2058 | if (unlikely (chunk->next == NULL)) |
2061 | if (unlikely (chunk->next == NULL)) |
2059 | return NULL; |
2062 | return NULL; |
2060 | 2063 | ||
2061 | chunk = chunk->next; |
2064 | chunk = chunk->next; |
2062 | chunk->next = NULL; |
2065 | chunk->next = NULL; |
2063 | chunk->count = 0; |
2066 | chunk->count = 0; |
2064 | chunk->size = size; |
2067 | chunk->size = size; |
2065 | chunk->base = chunk + 1; |
2068 | chunk->base = chunk + 1; |
2066 | self->tail = chunk; |
2069 | self->tail = chunk; |
2067 | } |
2070 | } |
2068 | 2071 | ||
2069 | return (edge_t *) chunk->base + chunk->count++; |
2072 | return (edge_t *) chunk->base + chunk->count++; |
2070 | } |
2073 | } |
2071 | 2074 | ||
2072 | static cairo_status_t |
2075 | static cairo_status_t |
2073 | botor_add_edge (cairo_botor_scan_converter_t *self, |
2076 | botor_add_edge (cairo_botor_scan_converter_t *self, |
2074 | const cairo_edge_t *edge) |
2077 | const cairo_edge_t *edge) |
2075 | { |
2078 | { |
2076 | edge_t *e; |
2079 | edge_t *e; |
2077 | cairo_fixed_t dx, dy; |
2080 | cairo_fixed_t dx, dy; |
2078 | 2081 | ||
2079 | e = botor_allocate_edge (self); |
2082 | e = botor_allocate_edge (self); |
2080 | if (unlikely (e == NULL)) |
2083 | if (unlikely (e == NULL)) |
2081 | return _cairo_error (CAIRO_STATUS_NO_MEMORY); |
2084 | return _cairo_error (CAIRO_STATUS_NO_MEMORY); |
2082 | 2085 | ||
2083 | cairo_list_init (&e->link); |
2086 | cairo_list_init (&e->link); |
2084 | e->edge = *edge; |
2087 | e->edge = *edge; |
2085 | 2088 | ||
2086 | dx = edge->line.p2.x - edge->line.p1.x; |
2089 | dx = edge->line.p2.x - edge->line.p1.x; |
2087 | dy = edge->line.p2.y - edge->line.p1.y; |
2090 | dy = edge->line.p2.y - edge->line.p1.y; |
2088 | e->dy = dy; |
2091 | e->dy = dy; |
2089 | 2092 | ||
2090 | if (dx == 0) { |
2093 | if (dx == 0) { |
2091 | e->vertical = TRUE; |
2094 | e->vertical = TRUE; |
2092 | e->x.quo = edge->line.p1.x; |
2095 | e->x.quo = edge->line.p1.x; |
2093 | e->x.rem = 0; |
2096 | e->x.rem = 0; |
2094 | e->dxdy.quo = 0; |
2097 | e->dxdy.quo = 0; |
2095 | e->dxdy.rem = 0; |
2098 | e->dxdy.rem = 0; |
2096 | e->dxdy_full.quo = 0; |
2099 | e->dxdy_full.quo = 0; |
2097 | e->dxdy_full.rem = 0; |
2100 | e->dxdy_full.rem = 0; |
2098 | } else { |
2101 | } else { |
2099 | e->vertical = FALSE; |
2102 | e->vertical = FALSE; |
2100 | e->dxdy = floored_divrem (dx, dy); |
2103 | e->dxdy = floored_divrem (dx, dy); |
2101 | if (edge->top == edge->line.p1.y) { |
2104 | if (edge->top == edge->line.p1.y) { |
2102 | e->x.quo = edge->line.p1.x; |
2105 | e->x.quo = edge->line.p1.x; |
2103 | e->x.rem = 0; |
2106 | e->x.rem = 0; |
2104 | } else { |
2107 | } else { |
2105 | e->x = floored_muldivrem (edge->top - edge->line.p1.y, |
2108 | e->x = floored_muldivrem (edge->top - edge->line.p1.y, |
2106 | dx, dy); |
2109 | dx, dy); |
2107 | e->x.quo += edge->line.p1.x; |
2110 | e->x.quo += edge->line.p1.x; |
2108 | } |
2111 | } |
2109 | 2112 | ||
2110 | if (_cairo_fixed_integer_part (edge->bottom) - _cairo_fixed_integer_part (edge->top) > 1) { |
2113 | if (_cairo_fixed_integer_part (edge->bottom) - _cairo_fixed_integer_part (edge->top) > 1) { |
2111 | e->dxdy_full = floored_muldivrem (STEP_Y, dx, dy); |
2114 | e->dxdy_full = floored_muldivrem (STEP_Y, dx, dy); |
2112 | } else { |
2115 | } else { |
2113 | e->dxdy_full.quo = 0; |
2116 | e->dxdy_full.quo = 0; |
2114 | e->dxdy_full.rem = 0; |
2117 | e->dxdy_full.rem = 0; |
2115 | } |
2118 | } |
2116 | } |
2119 | } |
2117 | 2120 | ||
2118 | e->x.rem = -e->dy; |
2121 | e->x.rem = -e->dy; |
2119 | e->current_sign = 0; |
2122 | e->current_sign = 0; |
2120 | e->runs = NULL; |
2123 | e->runs = NULL; |
2121 | e->flags = START; |
2124 | e->flags = START; |
2122 | 2125 | ||
2123 | self->num_edges++; |
2126 | self->num_edges++; |
2124 | 2127 | ||
2125 | return CAIRO_STATUS_SUCCESS; |
2128 | return CAIRO_STATUS_SUCCESS; |
2126 | } |
2129 | } |
2127 | - | ||
2128 | static cairo_status_t |
- | |
2129 | _cairo_botor_scan_converter_add_edge (void *converter, |
- | |
2130 | const cairo_point_t *p1, |
- | |
2131 | const cairo_point_t *p2, |
- | |
2132 | int top, int bottom, |
- | |
2133 | int dir) |
- | |
2134 | { |
- | |
2135 | cairo_botor_scan_converter_t *self = converter; |
- | |
2136 | cairo_edge_t edge; |
- | |
2137 | - | ||
2138 | edge.line.p1 = *p1; |
- | |
2139 | edge.line.p2 = *p2; |
- | |
2140 | edge.top = top; |
- | |
2141 | edge.bottom = bottom; |
- | |
2142 | edge.dir = dir; |
- | |
2143 | - | ||
2144 | return botor_add_edge (self, &edge); |
- | |
2145 | } |
- | |
2146 | - | ||
2147 | static cairo_status_t |
- | |
2148 | _cairo_botor_scan_converter_add_polygon (void *converter, |
- | |
2149 | const cairo_polygon_t *polygon) |
- | |
2150 | { |
- | |
2151 | cairo_botor_scan_converter_t *self = converter; |
- | |
2152 | cairo_status_t status; |
- | |
2153 | int i; |
- | |
2154 | - | ||
2155 | for (i = 0; i < polygon->num_edges; i++) { |
- | |
2156 | status = botor_add_edge (self, &polygon->edges[i]); |
- | |
2157 | if (unlikely (status)) |
- | |
2158 | return status; |
- | |
2159 | } |
- | |
2160 | - | ||
2161 | return CAIRO_STATUS_SUCCESS; |
- | |
2162 | } |
- | |
2163 | 2130 | ||
2164 | static void |
2131 | static void |
2165 | _cairo_botor_scan_converter_destroy (void *converter) |
2132 | _cairo_botor_scan_converter_destroy (void *converter) |
2166 | { |
2133 | { |
2167 | cairo_botor_scan_converter_t *self = converter; |
2134 | cairo_botor_scan_converter_t *self = converter; |
2168 | struct _cairo_botor_scan_converter_chunk *chunk, *next; |
2135 | struct _cairo_botor_scan_converter_chunk *chunk, *next; |
2169 | 2136 | ||
2170 | for (chunk = self->chunks.next; chunk != NULL; chunk = next) { |
2137 | for (chunk = self->chunks.next; chunk != NULL; chunk = next) { |
2171 | next = chunk->next; |
2138 | next = chunk->next; |
2172 | free (chunk); |
2139 | free (chunk); |
2173 | } |
2140 | } |
2174 | } |
2141 | } |
2175 | 2142 | ||
2176 | void |
2143 | void |
2177 | _cairo_botor_scan_converter_init (cairo_botor_scan_converter_t *self, |
2144 | _cairo_botor_scan_converter_init (cairo_botor_scan_converter_t *self, |
2178 | const cairo_box_t *extents, |
2145 | const cairo_box_t *extents, |
2179 | cairo_fill_rule_t fill_rule) |
2146 | cairo_fill_rule_t fill_rule) |
2180 | { |
2147 | { |
2181 | self->base.destroy = _cairo_botor_scan_converter_destroy; |
2148 | self->base.destroy = _cairo_botor_scan_converter_destroy; |
2182 | self->base.add_edge = _cairo_botor_scan_converter_add_edge; |
- | |
2183 | self->base.add_polygon = _cairo_botor_scan_converter_add_polygon; |
- | |
2184 | self->base.generate = _cairo_botor_scan_converter_generate; |
2149 | self->base.generate = _cairo_botor_scan_converter_generate; |
2185 | 2150 | ||
2186 | self->extents = *extents; |
2151 | self->extents = *extents; |
2187 | self->fill_rule = fill_rule; |
2152 | self->fill_rule = fill_rule; |
2188 | 2153 | ||
2189 | self->xmin = _cairo_fixed_integer_floor (extents->p1.x); |
2154 | self->xmin = _cairo_fixed_integer_floor (extents->p1.x); |
2190 | self->xmax = _cairo_fixed_integer_ceil (extents->p2.x); |
2155 | self->xmax = _cairo_fixed_integer_ceil (extents->p2.x); |
2191 | 2156 | ||
2192 | self->chunks.base = self->buf; |
2157 | self->chunks.base = self->buf; |
2193 | self->chunks.next = NULL; |
2158 | self->chunks.next = NULL; |
2194 | self->chunks.count = 0; |
2159 | self->chunks.count = 0; |
2195 | self->chunks.size = sizeof (self->buf) / sizeof (edge_t); |
2160 | self->chunks.size = sizeof (self->buf) / sizeof (edge_t); |
2196 | self->tail = &self->chunks; |
2161 | self->tail = &self->chunks; |
2197 | 2162 | ||
2198 | self->num_edges = 0; |
2163 | self->num_edges = 0; |
2199 | }>>>>>=>>>>>>=>>>=>>>>>>>>>>>=>>>>>>>0>><> |
2164 | }>>>>=>>>>>>=>>>=>>>>>>>>>>>=>>>>>>>0>><> |