Initialize Tizen 2.3
[framework/graphics/cairo.git] / perf / micro / wave.c
1 /*
2  * Copyright 2011 Red Hat Inc.
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: Benjamin Otte <otte@redhat.com>
25  */
26
27 #include "cairo-perf.h"
28
29 static cairo_surface_t *
30 generate_random_waveform(cairo_t *target, int width, int height)
31 {
32     cairo_surface_t *surface;
33     cairo_t *cr;
34     int i, r;
35
36     srand (0xdeadbeef);
37
38     surface = cairo_surface_create_similar (cairo_get_target (target),
39                                             CAIRO_CONTENT_ALPHA,
40                                             width, height);
41     cr = cairo_create (surface);
42
43     r = height / 2;
44
45     for (i = 0; i < width; i++)
46     {
47         r += rand () % (height / 4) - (height / 8);
48         if (r < 0)
49             r = 0;
50         else if (r > height)
51             r = height;
52         cairo_rectangle (cr, i, (height - r) / 2, 1, r);
53     }
54     cairo_fill (cr);
55     cairo_destroy (cr);
56
57     return surface;
58 }
59
60 static cairo_time_t
61 do_wave (cairo_t *cr, int width, int height, int loops)
62 {
63     cairo_surface_t *wave;
64     cairo_pattern_t *mask;
65
66     wave = generate_random_waveform (cr, width, height);
67
68     cairo_perf_timer_start ();
69     cairo_perf_set_thread_aware (cr, FALSE);
70
71     while (loops--) {
72         if (loops == 0)
73                 cairo_perf_set_thread_aware (cr, TRUE);
74         /* paint outline (and contents) */
75         cairo_set_source_rgb (cr, 1, 0, 0);
76         cairo_mask_surface (cr, wave, 0, 0);
77
78         /* overdraw inline */
79         /* first, create a mask */
80         cairo_push_group_with_content (cr, CAIRO_CONTENT_ALPHA);
81         cairo_set_source_surface (cr, wave, 0, 0);
82         cairo_paint (cr);
83         cairo_set_operator (cr, CAIRO_OPERATOR_IN);
84         cairo_set_source_surface (cr, wave, 1, 0);
85         cairo_paint (cr);
86         cairo_set_source_surface (cr, wave, -1, 0);
87         cairo_paint (cr);
88         cairo_set_source_surface (cr, wave, 0, 1);
89         cairo_paint (cr);
90         cairo_set_source_surface (cr, wave, 0, -1);
91         cairo_paint (cr);
92         mask = cairo_pop_group (cr);
93
94         /* second, paint the mask */
95         cairo_set_source_rgb (cr, 0, 1, 0);
96         cairo_mask (cr, mask);
97
98         cairo_pattern_destroy (mask);
99     }
100
101     cairo_perf_timer_stop ();
102
103     cairo_surface_destroy (wave);
104
105     return cairo_perf_timer_elapsed ();
106 }
107
108 cairo_bool_t
109 wave_enabled (cairo_perf_t *perf)
110 {
111     return cairo_perf_can_run (perf, "wave", NULL);
112 }
113
114 void
115 wave (cairo_perf_t *perf, cairo_t *cr, int width, int height)
116 {
117     cairo_perf_run (perf, "wave", do_wave, NULL);
118 }