f3215f34b47dc0b9ff35c904e703b8c3a8d2c80a
[framework/graphics/cairo.git] / perf / micro / mask.c
1 /*
2  * Copyright © 2006 Red Hat, Inc.
3  * Copyright © 2009 Chris Wilson
4  *
5  * Permission to use, copy, modify, distribute, and sell this software
6  * and its documentation for any purpose is hereby granted without
7  * fee, provided that the above copyright notice appear in all copies
8  * and that both that copyright notice and this permission notice
9  * appear in supporting documentation, and that the name of
10  * Red Hat, Inc. not be used in advertising or publicity pertaining to
11  * distribution of the software without specific, written prior
12  * permission. Red Hat, Inc. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as
14  * is" without express or implied warranty.
15  *
16  * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18  * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
19  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
22  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Author: Carl D. Worth <cworth@cworth.org>
25  *         Chris Wilson <chris@chris-wilson.co.uk>
26  */
27
28 #include "cairo-perf.h"
29
30 static cairo_time_t
31 do_mask_solid (cairo_t *cr, int width, int height, int loops)
32 {
33     cairo_pattern_t *mask;
34
35     mask = cairo_pattern_create_rgba (0, 0, 0, .5);
36
37     cairo_perf_timer_start ();
38     cairo_perf_set_thread_aware (cr, FALSE);
39
40     while (loops--) {
41         if (loops == 0)
42                 cairo_perf_set_thread_aware (cr, TRUE);
43         cairo_mask (cr, mask);
44     }
45
46     cairo_perf_timer_stop ();
47
48     cairo_pattern_destroy (mask);
49
50     return cairo_perf_timer_elapsed ();
51 }
52
53 static cairo_surface_t *
54 init_surface (cairo_surface_t *surface, int width, int height)
55 {
56     cairo_t *cr;
57
58     cr = cairo_create (surface);
59     cairo_surface_destroy (surface);
60
61     cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
62     cairo_set_source_rgb (cr, 0, 0, 0); /* back */
63     cairo_paint (cr);
64
65     cairo_set_source_rgba (cr, 1, 1, 1, 0.5); /* 50% */
66     cairo_new_path (cr);
67     cairo_rectangle (cr, 0, 0, width/2.0, height/2.0);
68     cairo_rectangle (cr, width/2.0, height/2.0, width/2.0, height/2.0);
69     cairo_fill (cr);
70
71     surface = cairo_surface_reference (cairo_get_target (cr));
72     cairo_destroy (cr);
73
74     return surface;
75 }
76
77 static cairo_time_t
78 do_mask_image (cairo_t *cr, int width, int height, int loops)
79 {
80     cairo_surface_t *surface;
81     cairo_pattern_t *mask;
82
83     surface = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height);
84     mask = cairo_pattern_create_for_surface (init_surface (surface,
85                                                            width,
86                                                            height));
87     cairo_surface_destroy (surface);
88
89     cairo_perf_timer_start ();
90     cairo_perf_set_thread_aware (cr, FALSE);
91
92     while (loops--) {
93         if (loops == 0)
94                 cairo_perf_set_thread_aware (cr, TRUE);
95         cairo_mask (cr, mask);
96     }
97
98     cairo_perf_timer_stop ();
99
100     cairo_pattern_destroy (mask);
101
102     return cairo_perf_timer_elapsed ();
103 }
104
105 static cairo_time_t
106 do_mask_image_half (cairo_t *cr, int width, int height, int loops)
107 {
108     cairo_surface_t *surface;
109     cairo_pattern_t *mask;
110     cairo_matrix_t matrix;
111
112     surface = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height);
113     mask = cairo_pattern_create_for_surface (init_surface (surface,
114                                                            width,
115                                                            height));
116     cairo_surface_destroy (surface);
117     cairo_matrix_init_scale (&matrix, .5, .5);
118     cairo_pattern_set_matrix (mask, &matrix);
119
120     cairo_perf_timer_start ();
121     cairo_perf_set_thread_aware (cr, FALSE);
122
123     while (loops--) {
124         if (loops == 0)
125                 cairo_perf_set_thread_aware (cr, TRUE);
126         cairo_mask (cr, mask);
127     }
128
129     cairo_perf_timer_stop ();
130
131     cairo_pattern_destroy (mask);
132
133     return cairo_perf_timer_elapsed ();
134 }
135
136 static cairo_time_t
137 do_mask_image_double (cairo_t *cr, int width, int height, int loops)
138 {
139     cairo_surface_t *surface;
140     cairo_pattern_t *mask;
141     cairo_matrix_t matrix;
142
143     surface = cairo_image_surface_create (CAIRO_FORMAT_A8, width, height);
144     mask = cairo_pattern_create_for_surface (init_surface (surface,
145                                                            width,
146                                                            height));
147     cairo_surface_destroy (surface);
148     cairo_matrix_init_scale (&matrix, 2., 2.);
149     cairo_pattern_set_matrix (mask, &matrix);
150
151     cairo_perf_timer_start ();
152     cairo_perf_set_thread_aware (cr, FALSE);
153
154     while (loops--) {
155         if (loops == 0)
156                 cairo_perf_set_thread_aware (cr, TRUE);
157         cairo_mask (cr, mask);
158     }
159
160     cairo_perf_timer_stop ();
161
162     cairo_pattern_destroy (mask);
163
164     return cairo_perf_timer_elapsed ();
165 }
166
167 static cairo_time_t
168 do_mask_similar (cairo_t *cr, int width, int height, int loops)
169 {
170     cairo_surface_t *surface;
171     cairo_pattern_t *mask;
172
173     surface = cairo_surface_create_similar (cairo_get_group_target (cr),
174                                             CAIRO_CONTENT_ALPHA, width, height);
175     mask = cairo_pattern_create_for_surface (init_surface (surface,
176                                                            width,
177                                                            height));
178     cairo_surface_destroy (surface);
179
180     cairo_perf_timer_start ();
181     cairo_perf_set_thread_aware (cr, FALSE);
182
183     while (loops--) {
184         if (loops == 0)
185                 cairo_perf_set_thread_aware (cr, TRUE);
186         cairo_mask (cr, mask);
187     }
188
189     cairo_perf_timer_stop ();
190
191     cairo_pattern_destroy (mask);
192
193     return cairo_perf_timer_elapsed ();
194 }
195
196 static cairo_time_t
197 do_mask_similar_half (cairo_t *cr, int width, int height, int loops)
198 {
199     cairo_surface_t *surface;
200     cairo_pattern_t *mask;
201     cairo_matrix_t matrix;
202
203     surface = cairo_surface_create_similar (cairo_get_group_target (cr),
204                                             CAIRO_CONTENT_ALPHA, width, height);
205     mask = cairo_pattern_create_for_surface (init_surface (surface,
206                                                            width,
207                                                            height));
208     cairo_surface_destroy (surface);
209     cairo_matrix_init_scale (&matrix, .5, .5);
210     cairo_pattern_set_matrix (mask, &matrix);
211
212     cairo_perf_timer_start ();
213     cairo_perf_set_thread_aware (cr, FALSE);
214
215     while (loops--) {
216         if (loops == 0)
217                 cairo_perf_set_thread_aware (cr, TRUE);
218         cairo_mask (cr, mask);
219     }
220
221     cairo_perf_timer_stop ();
222
223     cairo_pattern_destroy (mask);
224
225     return cairo_perf_timer_elapsed ();
226 }
227
228 static cairo_time_t
229 do_mask_similar_double (cairo_t *cr, int width, int height, int loops)
230 {
231     cairo_surface_t *surface;
232     cairo_pattern_t *mask;
233     cairo_matrix_t matrix;
234
235     surface = cairo_surface_create_similar (cairo_get_group_target (cr),
236                                             CAIRO_CONTENT_ALPHA, width, height);
237     mask = cairo_pattern_create_for_surface (init_surface (surface,
238                                                            width,
239                                                            height));
240     cairo_surface_destroy (surface);
241     cairo_matrix_init_scale (&matrix, 2., 2.);
242     cairo_pattern_set_matrix (mask, &matrix);
243
244     cairo_perf_timer_start ();
245     cairo_perf_set_thread_aware (cr, FALSE);
246
247     while (loops--) {
248         if (loops == 0)
249                 cairo_perf_set_thread_aware (cr, TRUE);
250         cairo_mask (cr, mask);
251     }
252
253     cairo_perf_timer_stop ();
254
255     cairo_pattern_destroy (mask);
256
257     return cairo_perf_timer_elapsed ();
258 }
259
260 static cairo_time_t
261 do_mask_linear (cairo_t *cr, int width, int height, int loops)
262 {
263     cairo_pattern_t *mask;
264
265     mask = cairo_pattern_create_linear (0.0, 0.0, width, height);
266     cairo_pattern_add_color_stop_rgba (mask, 0.0, 0, 0, 0, 0.5); /*  50% */
267     cairo_pattern_add_color_stop_rgba (mask, 0.0, 0, 0, 0, 1.0); /* 100% */
268
269     cairo_perf_timer_start ();
270     cairo_perf_set_thread_aware (cr, FALSE);
271
272     while (loops--) {
273         if (loops == 0)
274                 cairo_perf_set_thread_aware (cr, TRUE);
275         cairo_mask (cr, mask);
276     }
277
278     cairo_perf_timer_stop ();
279
280     cairo_pattern_destroy (mask);
281
282     return cairo_perf_timer_elapsed ();
283 }
284
285 static cairo_time_t
286 do_mask_radial (cairo_t *cr, int width, int height, int loops)
287 {
288     cairo_pattern_t *mask;
289
290     mask = cairo_pattern_create_radial (width/2.0, height/2.0, 0.0,
291                                         width/2.0, height/2.0, width/2.0);
292     cairo_pattern_add_color_stop_rgba (mask, 0.0, 0, 0, 0, 0.5); /*  50% */
293     cairo_pattern_add_color_stop_rgba (mask, 0.0, 0, 0, 0, 1.0); /* 100% */
294
295     cairo_perf_timer_start ();
296     cairo_perf_set_thread_aware (cr, FALSE);
297
298     while (loops--) {
299         if (loops == 0)
300                 cairo_perf_set_thread_aware (cr, TRUE);
301         cairo_mask (cr, mask);
302     }
303
304     cairo_perf_timer_stop ();
305
306     cairo_pattern_destroy (mask);
307
308     return cairo_perf_timer_elapsed ();
309 }
310
311 cairo_bool_t
312 mask_enabled (cairo_perf_t *perf)
313 {
314     return cairo_perf_can_run (perf, "mask", NULL);
315 }
316
317 void
318 mask (cairo_perf_t *perf, cairo_t *cr, int width, int height)
319 {
320     if (! cairo_perf_can_run (perf, "mask", NULL))
321         return;
322
323     cairo_perf_cover_sources_and_operators (perf, "mask-solid",
324                                             do_mask_solid, NULL);
325     cairo_perf_cover_sources_and_operators (perf, "mask-image",
326                                             do_mask_image, NULL);
327     cairo_perf_cover_sources_and_operators (perf, "mask-image-half",
328                                             do_mask_image_half, NULL);
329     cairo_perf_cover_sources_and_operators (perf, "mask-image-double",
330                                             do_mask_image_double, NULL);
331     cairo_perf_cover_sources_and_operators (perf, "mask-similar",
332                                             do_mask_similar, NULL);
333     cairo_perf_cover_sources_and_operators (perf, "mask-similar-half",
334                                             do_mask_similar_half, NULL);
335     cairo_perf_cover_sources_and_operators (perf, "mask-similar-double",
336                                             do_mask_similar_double, NULL);
337     cairo_perf_cover_sources_and_operators (perf, "mask-linear",
338                                             do_mask_linear, NULL);
339     cairo_perf_cover_sources_and_operators (perf, "mask-radial",
340                                             do_mask_radial, NULL);
341 }