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.
27 #include "cairo-perf.h"
29 #define NUM_SEGMENTS 256
31 static unsigned state;
33 uniform_random (double minval, double maxval)
35 static unsigned const poly = 0x9a795537U;
38 state = 2*state < state ? (2*state ^ poly) : 2*state;
39 return minval + state * (maxval - minval) / 4294967296.0;
43 draw_random (cairo_t *cr, cairo_fill_rule_t fill_rule,
44 int width, int height, int loops)
46 double x[NUM_SEGMENTS];
47 double y[NUM_SEGMENTS];
51 cairo_set_source_rgb (cr, 0, 0, 0);
54 for (i = 0; i < NUM_SEGMENTS; i++) {
55 x[i] = uniform_random (0, width);
56 y[i] = uniform_random (0, height);
60 cairo_translate (cr, 1, 1);
61 cairo_set_fill_rule (cr, fill_rule);
62 cairo_set_source_rgb (cr, 1, 0, 0);
65 cairo_move_to (cr, 0, 0);
66 for (i = 0; i < NUM_SEGMENTS; i++)
67 cairo_line_to (cr, x[i], y[i]);
68 cairo_close_path (cr);
70 cairo_perf_timer_start ();
71 cairo_perf_set_thread_aware (cr, FALSE);
74 cairo_perf_set_thread_aware (cr, TRUE);
75 cairo_fill_preserve (cr);
77 cairo_perf_timer_stop ();
81 return cairo_perf_timer_elapsed ();
85 draw_random_curve (cairo_t *cr, cairo_fill_rule_t fill_rule,
86 int width, int height, int loops)
88 double x[3*NUM_SEGMENTS];
89 double y[3*NUM_SEGMENTS];
93 cairo_set_source_rgb (cr, 0, 0, 0);
96 for (i = 0; i < 3*NUM_SEGMENTS; i++) {
97 x[i] = uniform_random (0, width);
98 y[i] = uniform_random (0, height);
102 cairo_translate (cr, 1, 1);
103 cairo_set_fill_rule (cr, fill_rule);
104 cairo_set_source_rgb (cr, 1, 0, 0);
107 cairo_move_to (cr, 0, 0);
108 for (i = 0; i < NUM_SEGMENTS; i++) {
114 cairo_close_path (cr);
116 cairo_perf_timer_start ();
117 cairo_perf_set_thread_aware (cr, FALSE);
120 cairo_perf_set_thread_aware (cr, TRUE);
121 cairo_fill_preserve (cr);
124 cairo_perf_timer_stop ();
128 return cairo_perf_timer_elapsed ();
132 random_eo (cairo_t *cr, int width, int height, int loops)
134 return draw_random (cr, CAIRO_FILL_RULE_EVEN_ODD, width, height, loops);
138 random_nz (cairo_t *cr, int width, int height, int loops)
140 return draw_random (cr, CAIRO_FILL_RULE_WINDING, width, height, loops);
144 random_curve_eo (cairo_t *cr, int width, int height, int loops)
146 return draw_random_curve (cr, CAIRO_FILL_RULE_EVEN_ODD, width, height, loops);
150 random_curve_nz (cairo_t *cr, int width, int height, int loops)
152 return draw_random_curve (cr, CAIRO_FILL_RULE_WINDING, width, height, loops);
156 intersections_enabled (cairo_perf_t *perf)
158 return cairo_perf_can_run (perf, "intersections", NULL);
162 intersections (cairo_perf_t *perf, cairo_t *cr, int width, int height)
164 cairo_perf_run (perf, "intersections-nz-fill", random_nz, NULL);
165 cairo_perf_run (perf, "intersections-eo-fill", random_eo, NULL);
167 cairo_perf_run (perf, "intersections-nz-curve-fill", random_curve_nz, NULL);
168 cairo_perf_run (perf, "intersections-eo-curve-fill", random_curve_eo, NULL);