Upload Tizen2.0 source
[framework/graphics/cairo.git] / perf / micro / many-fills.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
28 /* This is a variant on many strokes where we precompute
29  * a simplified stroke-to-path.
30  * When we have a real stroke-to-path, it would useful to compare the cost
31  * of stroking vs filling the "identical" paths.
32  */
33
34 #include "cairo-perf.h"
35
36 static uint32_t state;
37
38 static double
39 uniform_random (double minval, double maxval)
40 {
41     static uint32_t const poly = 0x9a795537U;
42     uint32_t n = 32;
43     while (n-->0)
44         state = 2*state < state ? (2*state ^ poly) : 2*state;
45     return minval + state * (maxval - minval) / 4294967296.0;
46 }
47
48 static cairo_time_t
49 do_many_fills_ha (cairo_t *cr, int width, int height, int loops)
50 {
51     int count;
52
53     state = 0xc0ffee;
54     for (count = 0; count < 1000; count++) {
55         double y = floor (uniform_random (0, height));
56         double x = floor (uniform_random (0, width));
57         cairo_rectangle (cr, x, y, ceil (uniform_random (0, width)) - x, 1);
58     }
59
60     cairo_perf_timer_start ();
61     cairo_perf_set_thread_aware (cr, FALSE);
62
63     while (loops--) {
64         if (loops == 0)
65                 cairo_perf_set_thread_aware (cr, TRUE);
66         cairo_fill_preserve (cr);
67     }
68
69     cairo_perf_timer_stop ();
70
71     cairo_new_path (cr);
72
73     return cairo_perf_timer_elapsed ();
74 }
75
76 static cairo_time_t
77 do_many_fills_h (cairo_t *cr, int width, int height, int loops)
78 {
79     int count;
80
81     state = 0xc0ffee;
82     for (count = 0; count < 1000; count++) {
83         double y = uniform_random (0, height);
84         double x = uniform_random (0, width);
85         cairo_rectangle (cr, x, y, uniform_random (0, width) - x, 1);
86     }
87
88     cairo_perf_timer_start ();
89     cairo_perf_set_thread_aware (cr, FALSE);
90
91     while (loops--) {
92         if (loops == 0)
93                 cairo_perf_set_thread_aware (cr, TRUE);
94         cairo_fill_preserve (cr);
95     }
96
97     cairo_perf_timer_stop ();
98
99     cairo_new_path (cr);
100
101     return cairo_perf_timer_elapsed ();
102 }
103
104 static cairo_time_t
105 do_many_fills_va (cairo_t *cr, int width, int height, int loops)
106 {
107     int count;
108
109     state = 0xc0ffee;
110     for (count = 0; count < 1000; count++) {
111         double x = floor (uniform_random (0, width));
112         double y = floor (uniform_random (0, height));
113         cairo_rectangle (cr, x, y, 1, ceil (uniform_random (0, height) - y));
114     }
115
116     cairo_perf_timer_start ();
117     cairo_perf_set_thread_aware (cr, FALSE);
118
119     while (loops--) {
120         if (loops == 0)
121                 cairo_perf_set_thread_aware (cr, TRUE);
122         cairo_fill_preserve (cr);
123     }
124
125     cairo_perf_timer_stop ();
126
127     cairo_new_path (cr);
128
129     return cairo_perf_timer_elapsed ();
130 }
131
132 static cairo_time_t
133 do_many_fills_v (cairo_t *cr, int width, int height, int loops)
134 {
135     int count;
136
137     state = 0xc0ffee;
138     for (count = 0; count < 1000; count++) {
139         double x = uniform_random (0, width);
140         double y = uniform_random (0, height);
141         cairo_rectangle (cr, x, y, 1, uniform_random (0, height) - y);
142     }
143
144     cairo_perf_timer_start ();
145     cairo_perf_set_thread_aware (cr, FALSE);
146
147     while (loops--) {
148         if (loops == 0)
149                 cairo_perf_set_thread_aware (cr, TRUE);
150         cairo_fill_preserve (cr);
151     }
152
153     cairo_perf_timer_stop ();
154
155     cairo_new_path (cr);
156
157     return cairo_perf_timer_elapsed ();
158 }
159
160 static cairo_time_t
161 do_many_fills (cairo_t *cr, int width, int height, int loops)
162 {
163     int count;
164
165     /* lots and lots of overlapping stroke-like fills */
166     state = 0xc0ffee;
167     for (count = 0; count < 1000; count++) {
168         cairo_save (cr);
169         cairo_translate (cr,
170                          uniform_random (0, width),
171                          uniform_random (0, height));
172         cairo_rotate (cr, uniform_random (-M_PI,M_PI));
173         cairo_rectangle (cr, 0, 0, uniform_random (0, width), 1);
174         cairo_restore (cr);
175     }
176
177     cairo_perf_timer_start ();
178     cairo_perf_set_thread_aware (cr, FALSE);
179
180     while (loops--) {
181         if (loops == 0)
182                 cairo_perf_set_thread_aware (cr, TRUE);
183         cairo_fill_preserve (cr);
184     }
185
186     cairo_perf_timer_stop ();
187
188     cairo_new_path (cr);
189
190     return cairo_perf_timer_elapsed ();
191 }
192
193 cairo_bool_t
194 many_fills_enabled (cairo_perf_t *perf)
195 {
196     return cairo_perf_can_run (perf, "many-fills", NULL);
197 }
198
199 void
200 many_fills (cairo_perf_t *perf, cairo_t *cr, int width, int height)
201 {
202     cairo_perf_run (perf, "many-fills-halign", do_many_fills_ha, NULL);
203     cairo_perf_run (perf, "many-fills-valign", do_many_fills_va, NULL);
204     cairo_perf_run (perf, "many-fills-horizontal", do_many_fills_h, NULL);
205     cairo_perf_run (perf, "many-fills-vertical", do_many_fills_v, NULL);
206     cairo_perf_run (perf, "many-fills-random", do_many_fills, NULL);
207 }