Tizen 2.0 Release
[framework/graphics/cairo.git] / perf / micro / many-curves.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 #include "cairo-perf.h"
28
29 static uint32_t state;
30
31 static double
32 uniform_random (double minval, double maxval)
33 {
34     static uint32_t const poly = 0x9a795537U;
35     uint32_t n = 32;
36     while (n-->0)
37         state = 2*state < state ? (2*state ^ poly) : 2*state;
38     return minval + state * (maxval - minval) / 4294967296.0;
39 }
40
41 static cairo_time_t
42 do_many_curves_stroked (cairo_t *cr, int width, int height, int loops)
43 {
44     int count;
45
46     state = 0xc0ffee;
47     cairo_move_to (cr, uniform_random (0, width), uniform_random (0, height));
48     for (count = 0; count < 1000; count++) {
49         double x1 = uniform_random (0, width);
50         double x2 = uniform_random (0, width);
51         double x3 = uniform_random (0, width);
52         double y1 = uniform_random (0, height);
53         double y2 = uniform_random (0, height);
54         double y3 = uniform_random (0, height);
55         cairo_curve_to (cr, x1, y1, x2, y2, x3, y3);
56     }
57
58     cairo_perf_timer_start ();
59     cairo_perf_set_thread_aware (cr, FALSE);
60
61     while (loops--) {
62         if (loops == 0)
63                 cairo_perf_set_thread_aware (cr, TRUE);
64         cairo_stroke_preserve (cr);
65     }
66
67     cairo_perf_timer_stop ();
68
69     cairo_new_path (cr);
70
71     return cairo_perf_timer_elapsed ();
72 }
73
74 static cairo_time_t
75 do_many_curves_hair_stroked (cairo_t *cr, int width, int height, int loops)
76 {
77     cairo_set_line_width (cr, 1.);
78     return do_many_curves_stroked (cr, width, height, loops);
79 }
80
81 static cairo_time_t
82 do_many_curves_wide_stroked (cairo_t *cr, int width, int height, int loops)
83 {
84     cairo_set_line_width (cr, 5.);
85     return do_many_curves_stroked (cr, width, height, loops);
86 }
87
88 static cairo_time_t
89 do_many_curves_filled (cairo_t *cr, int width, int height, int loops)
90 {
91     int count;
92
93     state = 0xc0ffee;
94     for (count = 0; count < 1000; count++) {
95         double x0 = uniform_random (0, width);
96         double x1 = uniform_random (0, width);
97         double x2 = uniform_random (0, width);
98         double x3 = uniform_random (0, width);
99         double xm = uniform_random (0, width);
100         double xn = uniform_random (0, width);
101         double y0 = uniform_random (0, height);
102         double y1 = uniform_random (0, height);
103         double y2 = uniform_random (0, height);
104         double y3 = uniform_random (0, height);
105         double ym = uniform_random (0, height);
106         double yn = uniform_random (0, height);
107         cairo_move_to (cr, xm, ym);
108         cairo_curve_to (cr, x1, y1, x2, y2, xn, yn);
109         cairo_curve_to (cr, x3, y3, x0, y0, xm, ym);
110         cairo_close_path (cr);
111     }
112
113     cairo_perf_timer_start ();
114     cairo_perf_set_thread_aware (cr, FALSE);
115
116     while (loops--) {
117         if (loops == 0)
118                 cairo_perf_set_thread_aware (cr, TRUE);
119         cairo_fill_preserve (cr);
120     }
121
122     cairo_perf_timer_stop ();
123
124     cairo_new_path (cr);
125
126     return cairo_perf_timer_elapsed ();
127 }
128
129 cairo_bool_t
130 many_curves_enabled (cairo_perf_t *perf)
131 {
132     return cairo_perf_can_run (perf, "many-curves", NULL);
133 }
134
135 void
136 many_curves (cairo_perf_t *perf, cairo_t *cr, int width, int height)
137 {
138     cairo_set_source_rgb (cr, 1., 1., 1.);
139
140     cairo_perf_run (perf, "many-curves-hair-stroked", do_many_curves_hair_stroked, NULL);
141     cairo_perf_run (perf, "many-curves-wide-stroked", do_many_curves_wide_stroked, NULL);
142     cairo_perf_run (perf, "many-curves-filled", do_many_curves_filled, NULL);
143 }