1 /* -*- Mode: c; tab-width: 8; c-basic-offset: 4; indent-tabs-mode: t; -*- */
2 /* cairo - a vector graphics library with display and print output
4 * Copyright (c) 2008 M Joonas Pihlaja
6 * Permission is hereby granted, free of charge, to any person
7 * obtaining a copy of this software and associated documentation
8 * files (the "Software"), to deal in the Software without
9 * restriction, including without limitation the rights to use,
10 * copy, modify, merge, publish, distribute, sublicense, and/or sell
11 * copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
20 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
22 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
23 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
24 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
25 * OTHER DEALINGS IN THE SOFTWARE.
28 #include "cairo-perf.h"
30 #define MAX_SEGMENTS 2560
33 PIXALIGN, /* pixel aligned path */
34 NONALIGN /* unaligned path. */
38 RECTCLOSE, /* keeps the path rectilinear */
39 DIAGCLOSE /* forces a diagonal */
43 draw_spiral (cairo_t *cr,
44 cairo_fill_rule_t fill_rule,
47 int width, int height, int loops)
51 double x[MAX_SEGMENTS];
52 double y[MAX_SEGMENTS];
54 int side = width < height ? width : height;
56 assert(5*(side/step/2+1)+2 < MAX_SEGMENTS);
58 #define L(x_,y_) (x[n] = (x_), y[n] = (y_), n++)
59 #define M(x_,y_) L(x_,y_)
60 #define v(t) L(x[n-1], y[n-1] + (t))
61 #define h(t) L(x[n-1] + (t), y[n-1])
64 case PIXALIGN: M(0,0); break;
65 case NONALIGN: M(0.1415926, 0.7182818); break;
68 while (side >= step && side >= 0) {
78 case RECTCLOSE: L(x[n-1],y[0]); break;
79 case DIAGCLOSE: L(x[0],y[0]); break;
82 assert(n < MAX_SEGMENTS);
85 cairo_set_source_rgb (cr, 0, 0, 0);
88 cairo_translate (cr, 1, 1);
89 cairo_set_fill_rule (cr, fill_rule);
90 cairo_set_source_rgb (cr, 1, 0, 0);
93 cairo_move_to (cr, x[0], y[0]);
94 for (i = 1; i < n; i++) {
95 cairo_line_to (cr, x[i], y[i]);
97 cairo_close_path (cr);
99 cairo_perf_timer_start ();
100 cairo_perf_set_thread_aware (cr, FALSE);
103 cairo_perf_set_thread_aware (cr, TRUE);
104 cairo_fill_preserve (cr);
107 cairo_perf_timer_stop ();
111 return cairo_perf_timer_elapsed ();
115 draw_spiral_box (cairo_t *cr,
116 cairo_fill_rule_t fill_rule,
118 int width, int height, int loops)
121 int side = (width < height ? width : height) - 2;
124 cairo_set_source_rgb (cr, 0, 0, 0);
127 cairo_set_source_rgb (cr, 1, 0, 0);
128 cairo_set_fill_rule (cr, fill_rule);
129 cairo_translate (cr, 1, 1);
130 if (align == NONALIGN)
131 cairo_translate (cr, 0.1415926, 0.7182818);
134 while (side >= step) {
135 cairo_rectangle (cr, 0, 0, side, side);
136 cairo_translate (cr, step, step);
140 cairo_perf_timer_start ();
141 cairo_perf_set_thread_aware (cr, FALSE);
144 cairo_perf_set_thread_aware (cr, TRUE);
145 cairo_fill_preserve (cr);
148 cairo_perf_timer_stop ();
152 return cairo_perf_timer_elapsed ();
156 draw_spiral_stroke (cairo_t *cr,
158 int width, int height, int loops)
161 int side = width < height ? width : height;
164 cairo_set_source_rgb (cr, 0, 0, 0);
167 cairo_translate (cr, 1, 1);
168 cairo_set_source_rgb (cr, 1, 0, 0);
169 cairo_set_line_width (cr, 4.);
170 cairo_set_line_join (cr, CAIRO_LINE_JOIN_MITER);
171 cairo_set_line_cap (cr, CAIRO_LINE_CAP_BUTT);
175 case PIXALIGN: cairo_move_to (cr, 0,0); break;
176 case NONALIGN: cairo_move_to (cr, 0.1415926, 0.7182818); break;
178 while (side >= step) {
179 cairo_rel_line_to (cr, 0, side);
184 cairo_rel_line_to (cr, side, 0);
189 cairo_rel_line_to (cr, 0, -side);
194 cairo_rel_line_to (cr, -side, 0);
200 cairo_perf_timer_start ();
201 cairo_perf_set_thread_aware (cr, FALSE);
204 cairo_perf_set_thread_aware (cr, TRUE);
205 cairo_stroke_preserve (cr);
208 cairo_perf_timer_stop ();
212 return cairo_perf_timer_elapsed ();
216 draw_spiral_eo_pa_re (cairo_t *cr, int width, int height, int loops)
218 return draw_spiral (cr,
219 CAIRO_FILL_RULE_EVEN_ODD,
222 width, height, loops);
226 draw_spiral_nz_pa_re (cairo_t *cr, int width, int height, int loops)
228 return draw_spiral (cr,
229 CAIRO_FILL_RULE_WINDING,
232 width, height, loops);
236 draw_spiral_eo_na_re (cairo_t *cr, int width, int height, int loops)
238 return draw_spiral (cr,
239 CAIRO_FILL_RULE_EVEN_ODD,
242 width, height, loops);
246 draw_spiral_nz_na_re (cairo_t *cr, int width, int height, int loops)
248 return draw_spiral (cr,
249 CAIRO_FILL_RULE_WINDING,
252 width, height, loops);
256 draw_spiral_eo_pa_di (cairo_t *cr, int width, int height, int loops)
258 return draw_spiral (cr,
259 CAIRO_FILL_RULE_EVEN_ODD,
262 width, height, loops);
266 draw_spiral_nz_pa_di (cairo_t *cr, int width, int height, int loops)
268 return draw_spiral (cr,
269 CAIRO_FILL_RULE_WINDING,
272 width, height, loops);
276 draw_spiral_eo_na_di (cairo_t *cr, int width, int height, int loops)
278 return draw_spiral (cr,
279 CAIRO_FILL_RULE_EVEN_ODD,
282 width, height, loops);
286 draw_spiral_nz_na_di (cairo_t *cr, int width, int height, int loops)
288 return draw_spiral (cr,
289 CAIRO_FILL_RULE_WINDING,
292 width, height, loops);
296 draw_spiral_nz_pa_box (cairo_t *cr, int width, int height, int loops)
298 return draw_spiral_box (cr,
299 CAIRO_FILL_RULE_WINDING, PIXALIGN,
300 width, height, loops);
304 draw_spiral_nz_na_box (cairo_t *cr, int width, int height, int loops)
306 return draw_spiral_box (cr,
307 CAIRO_FILL_RULE_WINDING, NONALIGN,
308 width, height, loops);
313 draw_spiral_eo_pa_box (cairo_t *cr, int width, int height, int loops)
315 return draw_spiral_box (cr,
316 CAIRO_FILL_RULE_EVEN_ODD, PIXALIGN,
317 width, height, loops);
321 draw_spiral_eo_na_box (cairo_t *cr, int width, int height, int loops)
323 return draw_spiral_box (cr,
324 CAIRO_FILL_RULE_EVEN_ODD, NONALIGN,
325 width, height, loops);
329 draw_spiral_stroke_pa (cairo_t *cr, int width, int height, int loops)
331 return draw_spiral_stroke (cr,
333 width, height, loops);
337 draw_spiral_stroke_na (cairo_t *cr, int width, int height, int loops)
339 return draw_spiral_stroke (cr,
341 width, height, loops);
345 spiral_enabled (cairo_perf_t *perf)
347 return cairo_perf_can_run (perf, "spiral", NULL);
351 spiral (cairo_perf_t *perf, cairo_t *cr, int width, int height)
353 cairo_perf_run (perf, "spiral-box-nonalign-evenodd-fill", draw_spiral_eo_na_box, NULL);
354 cairo_perf_run (perf, "spiral-box-nonalign-nonzero-fill", draw_spiral_nz_na_box, NULL);
355 cairo_perf_run (perf, "spiral-box-pixalign-evenodd-fill", draw_spiral_eo_pa_box, NULL);
356 cairo_perf_run (perf, "spiral-box-pixalign-nonzero-fill", draw_spiral_nz_pa_box, NULL);
357 cairo_perf_run (perf, "spiral-diag-nonalign-evenodd-fill", draw_spiral_eo_na_di, NULL);
358 cairo_perf_run (perf, "spiral-diag-nonalign-nonzero-fill", draw_spiral_nz_na_di, NULL);
359 cairo_perf_run (perf, "spiral-diag-pixalign-evenodd-fill", draw_spiral_eo_pa_di, NULL);
360 cairo_perf_run (perf, "spiral-diag-pixalign-nonzero-fill", draw_spiral_nz_pa_di, NULL);
361 cairo_perf_run (perf, "spiral-rect-nonalign-evenodd-fill", draw_spiral_eo_na_re, NULL);
362 cairo_perf_run (perf, "spiral-rect-nonalign-nonzero-fill", draw_spiral_nz_na_re, NULL);
363 cairo_perf_run (perf, "spiral-rect-pixalign-evenodd-fill", draw_spiral_eo_pa_re, NULL);
364 cairo_perf_run (perf, "spiral-rect-pixalign-nonzero-fill", draw_spiral_nz_pa_re, NULL);
365 cairo_perf_run (perf, "spiral-nonalign-stroke", draw_spiral_stroke_na, NULL);
366 cairo_perf_run (perf, "spiral-pixalign-stroke", draw_spiral_stroke_pa, NULL);