10670887b976becc024a1edb597cc2a1c2782a23
[framework/graphics/cairo.git] / perf / micro / many-strokes.c
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
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
42 do_many_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)) + .5;
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, 1.);
54
55     cairo_perf_timer_start ();
56     cairo_perf_set_thread_aware (cr, FALSE);
57
58     while (loops--) {
59         if (loops == 0)
60                 cairo_perf_set_thread_aware (cr, TRUE);
61         cairo_stroke_preserve (cr);
62     }
63
64     cairo_perf_timer_stop ();
65
66     cairo_new_path (cr);
67
68     return cairo_perf_timer_elapsed ();
69 }
70
71 static cairo_time_t
72 do_many_strokes_h (cairo_t *cr, int width, int height, int loops)
73 {
74     int count;
75
76     state = 0xc0ffee;
77     for (count = 0; count < 1000; count++) {
78         double h = uniform_random (0, height);
79         cairo_move_to (cr, uniform_random (0, width), h);
80         cairo_line_to (cr, uniform_random (0, width), h);
81     }
82
83     cairo_set_line_width (cr, 1.);
84
85     cairo_perf_timer_start ();
86     cairo_perf_set_thread_aware (cr, FALSE);
87
88     while (loops--) {
89         if (loops == 0)
90                 cairo_perf_set_thread_aware (cr, TRUE);
91         cairo_stroke_preserve (cr);
92     }
93
94     cairo_perf_timer_stop ();
95
96     cairo_new_path (cr);
97
98     return cairo_perf_timer_elapsed ();
99 }
100
101 static cairo_time_t
102 do_many_strokes_va (cairo_t *cr, int width, int height, int loops)
103 {
104     int count;
105
106     state = 0xc0ffee;
107     for (count = 0; count < 1000; count++) {
108         double v = floor (uniform_random (0, width)) + .5;
109         cairo_move_to (cr, v, floor (uniform_random (0, height)));
110         cairo_line_to (cr, v, ceil (uniform_random (0, height)));
111     }
112
113     cairo_set_line_width (cr, 1.);
114
115     cairo_perf_timer_start ();
116     cairo_perf_set_thread_aware (cr, FALSE);
117
118     while (loops--) {
119         if (loops == 0)
120                 cairo_perf_set_thread_aware (cr, TRUE);
121         cairo_stroke_preserve (cr);
122     }
123
124     cairo_perf_timer_stop ();
125
126     cairo_new_path (cr);
127
128     return cairo_perf_timer_elapsed ();
129 }
130
131 static cairo_time_t
132 do_many_strokes_v (cairo_t *cr, int width, int height, int loops)
133 {
134     int count;
135
136     state = 0xc0ffee;
137     for (count = 0; count < 1000; count++) {
138         double v = uniform_random (0, width);
139         cairo_move_to (cr, v, uniform_random (0, height));
140         cairo_line_to (cr, v, uniform_random (0, height));
141     }
142
143     cairo_set_line_width (cr, 1.);
144
145     cairo_perf_timer_start ();
146     cairo_perf_set_thread_aware (cr, FALSE);
147
148     while (loops--) {
149         if (loops == 0)
150                 cairo_perf_set_thread_aware (cr, TRUE);
151         cairo_stroke_preserve (cr);
152     }
153
154     cairo_perf_timer_stop ();
155
156     cairo_new_path (cr);
157
158     return cairo_perf_timer_elapsed ();
159 }
160
161 static cairo_time_t
162 do_many_strokes (cairo_t *cr, int width, int height, int loops)
163 {
164     int count;
165
166     /* lots and lots of overlapping strokes */
167     state = 0xc0ffee;
168     for (count = 0; count < 1000; count++) {
169         cairo_line_to (cr,
170                        uniform_random (0, width),
171                        uniform_random (0, height));
172     }
173
174     cairo_set_line_width (cr, 1.);
175
176     cairo_perf_timer_start ();
177     cairo_perf_set_thread_aware (cr, FALSE);
178
179     while (loops--) {
180         if (loops == 0)
181                 cairo_perf_set_thread_aware (cr, TRUE);
182         cairo_stroke_preserve (cr);
183     }
184
185     cairo_perf_timer_stop ();
186
187     cairo_new_path (cr);
188
189     return cairo_perf_timer_elapsed ();
190 }
191
192 cairo_bool_t
193 many_strokes_enabled (cairo_perf_t *perf)
194 {
195     return cairo_perf_can_run (perf, "many-strokes", NULL);
196 }
197
198 void
199 many_strokes (cairo_perf_t *perf, cairo_t *cr, int width, int height)
200 {
201     cairo_perf_run (perf, "many-strokes-halign", do_many_strokes_ha, NULL);
202     cairo_perf_run (perf, "many-strokes-valign", do_many_strokes_va, NULL);
203     cairo_perf_run (perf, "many-strokes-horizontal", do_many_strokes_h, NULL);
204     cairo_perf_run (perf, "many-strokes-vertical", do_many_strokes_v, NULL);
205     cairo_perf_run (perf, "many-strokes-random", do_many_strokes, NULL);
206 }