Upload Tizen2.0 source
[framework/graphics/cairo.git] / perf / micro / box-outline.c
1 /*
2  * Copyright © 2006 Red Hat, Inc.
3  *
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use, copy,
9  * modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  * Author: Carl D. Worth <cworth@cworth.org>
26  */
27
28 #include "cairo-perf.h"
29
30 /* This test case is designed to illustrate a performance bug that
31  * exists in cairo in which using cairo_stroke is much slower than
32  * cairo_fill to draw an identical figure, (and in particular a figure
33  * that is much more natural to draw with cairo_stroke). The figure is
34  * a 100x100 square outline 1-pixel wide, nicely pixel aligned.
35  *
36  * The performance bug should affect any path whose resulting contour
37  * consists only of pixel-aligned horizontal and vertical elements.
38  *
39  * Initial testing on on machine shows stroke as 5x slower than fill
40  * for the xlib backend and 16x slower for the image backend.
41  */
42
43 static cairo_time_t
44 box_outline_stroke (cairo_t *cr, int width, int height, int loops)
45 {
46     cairo_set_source_rgb (cr, 0, 0, 1); /* blue */
47     cairo_paint (cr);
48
49     cairo_rectangle (cr,
50                      1.5, 1.5,
51                      width - 3, height - 3);
52     cairo_set_line_width (cr, 1.0);
53     cairo_set_source_rgb (cr, 1, 0, 0); /* red */
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 box_outline_alpha_stroke (cairo_t *cr, int width, int height, int loops)
73 {
74     cairo_set_source_rgb (cr, 0, 0, 1); /* blue */
75     cairo_paint (cr);
76
77     cairo_rectangle (cr,
78                      1.5, 1.5,
79                      width - 3, height - 3);
80     cairo_set_line_width (cr, 1.0);
81     cairo_set_source_rgba (cr, 1, 0, 0, .5); /* red */
82
83     cairo_perf_timer_start ();
84     cairo_perf_set_thread_aware (cr, FALSE);
85
86     while (loops--) {
87         if (loops == 0)
88             cairo_perf_set_thread_aware (cr, TRUE);
89         cairo_stroke_preserve (cr);
90     }
91
92     cairo_perf_timer_stop ();
93
94     cairo_new_path (cr);
95
96     return cairo_perf_timer_elapsed ();
97 }
98
99 static cairo_time_t
100 box_outline_aa_stroke (cairo_t *cr, int width, int height, int loops)
101 {
102     cairo_set_source_rgb (cr, 0, 0, 1); /* blue */
103     cairo_paint (cr);
104
105     cairo_translate (cr, .5, .5);
106     cairo_rectangle (cr,
107                      1.5, 1.5,
108                      width - 3, height - 3);
109     cairo_set_line_width (cr, 1.0);
110     cairo_set_source_rgb (cr, 1, 0, 0); /* red */
111
112     cairo_perf_timer_start ();
113     cairo_perf_set_thread_aware (cr, FALSE);
114
115     while (loops--) {
116         if (loops == 0)
117             cairo_perf_set_thread_aware (cr, TRUE);
118         cairo_stroke_preserve (cr);
119     }
120
121     cairo_perf_timer_stop ();
122
123     cairo_new_path (cr);
124
125     return cairo_perf_timer_elapsed ();
126 }
127
128 static cairo_time_t
129 box_outline_fill (cairo_t *cr, int width, int height, int loops)
130 {
131     cairo_set_source_rgb (cr, 0, 0, 1); /* blue */
132     cairo_paint (cr);
133
134     cairo_rectangle (cr,
135                      1.0, 1.0,
136                      width - 2, height - 2);
137     cairo_rectangle (cr,
138                      2.0, 2.0,
139                      width - 4, height - 4);
140     cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
141     cairo_set_source_rgb (cr, 0, 1, 0); /* green */
142
143     cairo_perf_timer_start ();
144     cairo_perf_set_thread_aware (cr, FALSE);
145
146     while (loops--) {
147         if (loops == 0)
148             cairo_perf_set_thread_aware (cr, TRUE);
149         cairo_fill_preserve (cr);
150     }
151
152     cairo_perf_timer_stop ();
153
154     cairo_new_path (cr);
155
156     return cairo_perf_timer_elapsed ();
157 }
158
159 static cairo_time_t
160 box_outline_alpha_fill (cairo_t *cr, int width, int height, int loops)
161 {
162     cairo_set_source_rgb (cr, 0, 0, 1); /* blue */
163     cairo_paint (cr);
164
165     cairo_rectangle (cr,
166                      1.0, 1.0,
167                      width - 2, height - 2);
168     cairo_rectangle (cr,
169                      2.0, 2.0,
170                      width - 4, height - 4);
171     cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
172     cairo_set_source_rgba (cr, 0, 1, 0, .5); /* green */
173
174     cairo_perf_timer_start ();
175     cairo_perf_set_thread_aware (cr, FALSE);
176
177     while (loops--) {
178         if (loops == 0)
179             cairo_perf_set_thread_aware (cr, TRUE);
180         cairo_fill_preserve (cr);
181     }
182
183     cairo_perf_timer_stop ();
184
185     cairo_new_path (cr);
186
187     return cairo_perf_timer_elapsed ();
188 }
189
190 static cairo_time_t
191 box_outline_aa_fill (cairo_t *cr, int width, int height, int loops)
192 {
193     cairo_set_source_rgb (cr, 0, 0, 1); /* blue */
194     cairo_paint (cr);
195
196     cairo_translate (cr, .5, .5);
197     cairo_rectangle (cr,
198                      1.0, 1.0,
199                      width - 2, height - 2);
200     cairo_rectangle (cr,
201                      2.0, 2.0,
202                      width - 4, height - 4);
203     cairo_set_fill_rule (cr, CAIRO_FILL_RULE_EVEN_ODD);
204     cairo_set_source_rgb (cr, 0, 1, 0); /* green */
205
206     cairo_perf_timer_start ();
207     cairo_perf_set_thread_aware (cr, FALSE);
208
209     while (loops--) {
210         if (loops == 0)
211             cairo_perf_set_thread_aware (cr, TRUE);
212         cairo_fill_preserve (cr);
213     }
214
215     cairo_perf_timer_stop ();
216
217     cairo_new_path (cr);
218
219     return cairo_perf_timer_elapsed ();
220 }
221
222 cairo_bool_t
223 box_outline_enabled (cairo_perf_t *perf)
224 {
225     return cairo_perf_can_run (perf, "box-outline", NULL);
226 }
227
228 void
229 box_outline (cairo_perf_t *perf, cairo_t *cr, int width, int height)
230 {
231     cairo_perf_run (perf, "box-outline-stroke", box_outline_stroke, NULL);
232     cairo_perf_run (perf, "box-outline-fill", box_outline_fill, NULL);
233
234     cairo_perf_run (perf, "box-outline-alpha-stroke", box_outline_alpha_stroke, NULL);
235     cairo_perf_run (perf, "box-outline-alpha-fill", box_outline_alpha_fill, NULL);
236
237     cairo_perf_run (perf, "box-outline-aa-stroke", box_outline_aa_stroke, NULL);
238     cairo_perf_run (perf, "box-outline-aa-fill", box_outline_aa_fill, NULL);
239 }