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
3
 *
4
 * Copyright (c) 2008  M Joonas Pihlaja
5
 *
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
13
 * conditions:
14
 *
15
 * The above copyright notice and this permission notice shall be
16
 * included in all copies or substantial portions of the Software.
17
 *
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.
26
 */
27
#include "cairo-perf.h"
28

            
29
#define NUM_SEGMENTS 256
30

            
31
static unsigned state;
32
static double
33
uniform_random (double minval, double maxval)
34
{
35
    static unsigned const poly = 0x9a795537U;
36
    unsigned n = 32;
37
    while (n-->0)
38
	state = 2*state < state ? (2*state ^ poly) : 2*state;
39
    return minval + state * (maxval - minval) / 4294967296.0;
40
}
41

            
42
static cairo_time_t
43
draw_random (cairo_t *cr, cairo_fill_rule_t fill_rule,
44
	     int width, int height, int loops)
45
{
46
    double x[NUM_SEGMENTS];
47
    double y[NUM_SEGMENTS];
48
    int i;
49

            
50
    cairo_save (cr);
51
    cairo_set_source_rgb (cr, 0, 0, 0);
52
    cairo_paint (cr);
53

            
54
    for (i = 0; i < NUM_SEGMENTS; i++) {
55
         x[i] = uniform_random (0, width);
56
         y[i] = uniform_random (0, height);
57
    }
58

            
59
    state = 0x12345678;
60
    cairo_translate (cr, 1, 1);
61
    cairo_set_fill_rule (cr, fill_rule);
62
    cairo_set_source_rgb (cr, 1, 0, 0);
63

            
64
    cairo_new_path (cr);
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);
69

            
70
    cairo_perf_timer_start ();
71
    while (loops--)
72
        cairo_fill_preserve (cr);
73
    cairo_perf_timer_stop ();
74

            
75
    cairo_restore (cr);
76

            
77
    return cairo_perf_timer_elapsed ();
78
}
79

            
80
static cairo_time_t
81
draw_random_curve (cairo_t *cr, cairo_fill_rule_t fill_rule,
82
		   int width, int height, int loops)
83
{
84
    double x[3*NUM_SEGMENTS];
85
    double y[3*NUM_SEGMENTS];
86
    int i;
87

            
88
    cairo_save (cr);
89
    cairo_set_source_rgb (cr, 0, 0, 0);
90
    cairo_paint (cr);
91

            
92
    for (i = 0; i < 3*NUM_SEGMENTS; i++) {
93
         x[i] = uniform_random (0, width);
94
         y[i] = uniform_random (0, height);
95
    }
96

            
97
    state = 0x12345678;
98
    cairo_translate (cr, 1, 1);
99
    cairo_set_fill_rule (cr, fill_rule);
100
    cairo_set_source_rgb (cr, 1, 0, 0);
101

            
102
    cairo_new_path (cr);
103
    cairo_move_to (cr, 0, 0);
104
    for (i = 0; i < NUM_SEGMENTS; i++) {
105
	cairo_curve_to (cr,
106
			x[3*i+0], y[3*i+0],
107
			x[3*i+1], y[3*i+1],
108
			x[3*i+2], y[3*i+2]);
109
    }
110
    cairo_close_path (cr);
111

            
112
    cairo_perf_timer_start ();
113
    while (loops--)
114
        cairo_fill_preserve (cr);
115
    cairo_perf_timer_stop ();
116

            
117
    cairo_restore (cr);
118

            
119
    return cairo_perf_timer_elapsed ();
120
}
121

            
122
static cairo_time_t
123
random_eo (cairo_t *cr, int width, int height, int loops)
124
{
125
    return draw_random (cr, CAIRO_FILL_RULE_EVEN_ODD, width, height, loops);
126
}
127

            
128
static cairo_time_t
129
random_nz (cairo_t *cr, int width, int height, int loops)
130
{
131
    return draw_random (cr, CAIRO_FILL_RULE_WINDING, width, height, loops);
132
}
133

            
134
static cairo_time_t
135
random_curve_eo (cairo_t *cr, int width, int height, int loops)
136
{
137
    return draw_random_curve (cr, CAIRO_FILL_RULE_EVEN_ODD, width, height, loops);
138
}
139

            
140
static cairo_time_t
141
random_curve_nz (cairo_t *cr, int width, int height, int loops)
142
{
143
    return draw_random_curve (cr, CAIRO_FILL_RULE_WINDING, width, height, loops);
144
}
145

            
146
cairo_bool_t
147
intersections_enabled (cairo_perf_t *perf)
148
{
149
    return cairo_perf_can_run (perf, "intersections", NULL);
150
}
151

            
152
void
153
intersections (cairo_perf_t *perf, cairo_t *cr, int width, int height)
154
{
155
    cairo_perf_run (perf, "intersections-nz-fill", random_nz, NULL);
156
    cairo_perf_run (perf, "intersections-eo-fill", random_eo, NULL);
157

            
158
    cairo_perf_run (perf, "intersections-nz-curve-fill", random_curve_nz, NULL);
159
    cairo_perf_run (perf, "intersections-eo-curve-fill", random_curve_eo, NULL);
160
}