Upload Tizen2.0 source
[framework/graphics/cairo.git] / perf / micro / intersections.c
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     cairo_perf_set_thread_aware (cr, FALSE);
72     while (loops--) {
73         if (loops == 0)
74             cairo_perf_set_thread_aware (cr, TRUE);
75         cairo_fill_preserve (cr);
76     }
77     cairo_perf_timer_stop ();
78
79     cairo_restore (cr);
80
81     return cairo_perf_timer_elapsed ();
82 }
83
84 static cairo_time_t
85 draw_random_curve (cairo_t *cr, cairo_fill_rule_t fill_rule,
86                    int width, int height, int loops)
87 {
88     double x[3*NUM_SEGMENTS];
89     double y[3*NUM_SEGMENTS];
90     int i;
91
92     cairo_save (cr);
93     cairo_set_source_rgb (cr, 0, 0, 0);
94     cairo_paint (cr);
95
96     for (i = 0; i < 3*NUM_SEGMENTS; i++) {
97          x[i] = uniform_random (0, width);
98          y[i] = uniform_random (0, height);
99     }
100
101     state = 0x12345678;
102     cairo_translate (cr, 1, 1);
103     cairo_set_fill_rule (cr, fill_rule);
104     cairo_set_source_rgb (cr, 1, 0, 0);
105
106     cairo_new_path (cr);
107     cairo_move_to (cr, 0, 0);
108     for (i = 0; i < NUM_SEGMENTS; i++) {
109         cairo_curve_to (cr,
110                         x[3*i+0], y[3*i+0],
111                         x[3*i+1], y[3*i+1],
112                         x[3*i+2], y[3*i+2]);
113     }
114     cairo_close_path (cr);
115
116     cairo_perf_timer_start ();
117     cairo_perf_set_thread_aware (cr, FALSE);
118     while (loops--) {
119         if (loops == 0)
120             cairo_perf_set_thread_aware (cr, TRUE);
121         cairo_fill_preserve (cr);
122     }
123
124     cairo_perf_timer_stop ();
125
126     cairo_restore (cr);
127
128     return cairo_perf_timer_elapsed ();
129 }
130
131 static cairo_time_t
132 random_eo (cairo_t *cr, int width, int height, int loops)
133 {
134     return draw_random (cr, CAIRO_FILL_RULE_EVEN_ODD, width, height, loops);
135 }
136
137 static cairo_time_t
138 random_nz (cairo_t *cr, int width, int height, int loops)
139 {
140     return draw_random (cr, CAIRO_FILL_RULE_WINDING, width, height, loops);
141 }
142
143 static cairo_time_t
144 random_curve_eo (cairo_t *cr, int width, int height, int loops)
145 {
146     return draw_random_curve (cr, CAIRO_FILL_RULE_EVEN_ODD, width, height, loops);
147 }
148
149 static cairo_time_t
150 random_curve_nz (cairo_t *cr, int width, int height, int loops)
151 {
152     return draw_random_curve (cr, CAIRO_FILL_RULE_WINDING, width, height, loops);
153 }
154
155 cairo_bool_t
156 intersections_enabled (cairo_perf_t *perf)
157 {
158     return cairo_perf_can_run (perf, "intersections", NULL);
159 }
160
161 void
162 intersections (cairo_perf_t *perf, cairo_t *cr, int width, int height)
163 {
164     cairo_perf_run (perf, "intersections-nz-fill", random_nz, NULL);
165     cairo_perf_run (perf, "intersections-eo-fill", random_eo, NULL);
166
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);
169 }