Tizen 2.0 Release
[framework/graphics/cairo.git] / perf / micro / curve.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_curve_stroke (cairo_t *cr, int width, int height, int loops)
43 {
44     state = 0xc0ffee;
45     cairo_set_line_width (cr, 2.);
46     cairo_perf_timer_start ();
47     cairo_perf_set_thread_aware (cr, FALSE);
48
49     while (loops--) {
50         if (loops == 0)
51                 cairo_perf_set_thread_aware (cr, TRUE);
52         double x1 = uniform_random (0, width);
53         double x2 = uniform_random (0, width);
54         double x3 = uniform_random (0, width);
55         double y1 = uniform_random (0, height);
56         double y2 = uniform_random (0, height);
57         double y3 = uniform_random (0, height);
58         cairo_move_to (cr, uniform_random (0, width), uniform_random (0, height));
59         cairo_curve_to (cr, x1, y1, x2, y2, x3, y3);
60         cairo_stroke(cr);
61     }
62
63     cairo_perf_timer_stop ();
64
65     return cairo_perf_timer_elapsed ();
66 }
67
68 static cairo_time_t
69 do_curve_fill (cairo_t *cr, int width, int height, int loops)
70 {
71     state = 0xc0ffee;
72     cairo_perf_timer_start ();
73     cairo_perf_set_thread_aware (cr, FALSE);
74
75     while (loops--) {
76         if (loops == 0)
77                 cairo_perf_set_thread_aware (cr, TRUE);
78         double x0 = uniform_random (0, width);
79         double x1 = uniform_random (0, width);
80         double x2 = uniform_random (0, width);
81         double x3 = uniform_random (0, width);
82         double xm = uniform_random (0, width);
83         double xn = uniform_random (0, width);
84         double y0 = uniform_random (0, height);
85         double y1 = uniform_random (0, height);
86         double y2 = uniform_random (0, height);
87         double y3 = uniform_random (0, height);
88         double ym = uniform_random (0, height);
89         double yn = uniform_random (0, height);
90
91         cairo_move_to (cr, xm, ym);
92         cairo_curve_to (cr, x1, y1, x2, y2, xn, yn);
93         cairo_curve_to (cr, x3, y3, x0, y0, xm, ym);
94         cairo_close_path (cr);
95
96         cairo_fill(cr);
97     }
98
99     cairo_perf_timer_stop ();
100
101     return cairo_perf_timer_elapsed ();
102 }
103
104 cairo_bool_t
105 curve_enabled (cairo_perf_t *perf)
106 {
107     return cairo_perf_can_run (perf, "curve", NULL);
108 }
109
110 void
111 curve (cairo_perf_t *perf, cairo_t *cr, int width, int height)
112 {
113     cairo_set_source_rgb (cr, 1., 1., 1.);
114
115     cairo_perf_run (perf, "curve-stroked", do_curve_stroke, NULL);
116     cairo_perf_run (perf, "curve-filled", do_curve_fill, NULL);
117 }