1 /*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person
5 * obtaining a copy of this software and associated documentation
6 * files (the "Software"), to deal in the Software without
7 * restriction, including without limitation the rights to use, copy,
8 * modify, merge, publish, distribute, sublicense, and/or sell copies
9 * of the Software, and to permit persons to whom the Software is
10 * furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 * Author: Chris Wilson <chris@chris-wilson.co.uk>
25 */
26
27 #include "cairo-perf.h"
28
29 static uint32_t state;
30
31 static double
uniform_random(double minval,double maxval)32 uniform_random (double minval, double maxval)
33 {
34 static uint32_t const poly = 0x9a795537U;
35 uint32_t n = 32;
36 while (n-->0)
37 state = 2*state < state ? (2*state ^ poly) : 2*state;
38 return minval + state * (maxval - minval) / 4294967296.0;
39 }
40
41 static cairo_time_t
do_wide_strokes_ha(cairo_t * cr,int width,int height,int loops)42 do_wide_strokes_ha (cairo_t *cr, int width, int height, int loops)
43 {
44 int count;
45
46 state = 0xc0ffee;
47 for (count = 0; count < 1000; count++) {
48 double h = floor (uniform_random (0, height));
49 cairo_move_to (cr, floor (uniform_random (0, width)), h);
50 cairo_line_to (cr, ceil (uniform_random (0, width)), h);
51 }
52
53 cairo_set_line_width (cr, 5.);
54
55 cairo_perf_timer_start ();
56
57 while (loops--)
58 cairo_stroke_preserve (cr);
59
60 cairo_perf_timer_stop ();
61
62 cairo_new_path (cr);
63
64 return cairo_perf_timer_elapsed ();
65 }
66
67 static cairo_time_t
do_wide_strokes_h(cairo_t * cr,int width,int height,int loops)68 do_wide_strokes_h (cairo_t *cr, int width, int height, int loops)
69 {
70 int count;
71
72 state = 0xc0ffee;
73 for (count = 0; count < 1000; count++) {
74 double h = uniform_random (0, height);
75 cairo_move_to (cr, uniform_random (0, width), h);
76 cairo_line_to (cr, uniform_random (0, width), h);
77 }
78
79 cairo_set_line_width (cr, 5.);
80
81 cairo_perf_timer_start ();
82
83 while (loops--)
84 cairo_stroke_preserve (cr);
85
86 cairo_perf_timer_stop ();
87
88 cairo_new_path (cr);
89
90 return cairo_perf_timer_elapsed ();
91 }
92
93 static cairo_time_t
do_wide_strokes_va(cairo_t * cr,int width,int height,int loops)94 do_wide_strokes_va (cairo_t *cr, int width, int height, int loops)
95 {
96 int count;
97
98 state = 0xc0ffee;
99 for (count = 0; count < 1000; count++) {
100 double v = floor (uniform_random (0, width));
101 cairo_move_to (cr, v, floor (uniform_random (0, height)));
102 cairo_line_to (cr, v, ceil (uniform_random (0, height)));
103 }
104
105 cairo_set_line_width (cr, 5.);
106
107 cairo_perf_timer_start ();
108
109 while (loops--)
110 cairo_stroke_preserve (cr);
111
112 cairo_perf_timer_stop ();
113
114 cairo_new_path (cr);
115
116 return cairo_perf_timer_elapsed ();
117 }
118
119 static cairo_time_t
do_wide_strokes_v(cairo_t * cr,int width,int height,int loops)120 do_wide_strokes_v (cairo_t *cr, int width, int height, int loops)
121 {
122 int count;
123
124 state = 0xc0ffee;
125 for (count = 0; count < 1000; count++) {
126 double v = uniform_random (0, width);
127 cairo_move_to (cr, v, uniform_random (0, height));
128 cairo_line_to (cr, v, uniform_random (0, height));
129 }
130
131 cairo_set_line_width (cr, 5.);
132
133 cairo_perf_timer_start ();
134
135 while (loops--)
136 cairo_stroke_preserve (cr);
137
138 cairo_perf_timer_stop ();
139
140 cairo_new_path (cr);
141
142 return cairo_perf_timer_elapsed ();
143 }
144
145 static cairo_time_t
do_wide_strokes(cairo_t * cr,int width,int height,int loops)146 do_wide_strokes (cairo_t *cr, int width, int height, int loops)
147 {
148 int count;
149
150 /* lots and lots of overlapping strokes */
151 state = 0xc0ffee;
152 for (count = 0; count < 1000; count++) {
153 cairo_line_to (cr,
154 uniform_random (0, width),
155 uniform_random (0, height));
156 }
157
158 cairo_set_line_width (cr, 5.);
159
160 cairo_perf_timer_start ();
161
162 while (loops--)
163 cairo_stroke_preserve (cr);
164
165 cairo_perf_timer_stop ();
166
167 cairo_new_path (cr);
168
169 return cairo_perf_timer_elapsed ();
170 }
171
172 cairo_bool_t
wide_strokes_enabled(cairo_perf_t * perf)173 wide_strokes_enabled (cairo_perf_t *perf)
174 {
175 return cairo_perf_can_run (perf, "wide-strokes", NULL);
176 }
177
178 void
wide_strokes(cairo_perf_t * perf,cairo_t * cr,int width,int height)179 wide_strokes (cairo_perf_t *perf, cairo_t *cr, int width, int height)
180 {
181 cairo_set_source_rgb (cr, 1., 1., 1.);
182
183 cairo_perf_run (perf, "wide-strokes-halign", do_wide_strokes_ha, NULL);
184 cairo_perf_run (perf, "wide-strokes-valign", do_wide_strokes_va, NULL);
185 cairo_perf_run (perf, "wide-strokes-horizontal", do_wide_strokes_h, NULL);
186 cairo_perf_run (perf, "wide-strokes-vertical", do_wide_strokes_v, NULL);
187 cairo_perf_run (perf, "wide-strokes-random", do_wide_strokes, NULL);
188 }
189