bbb766ff690a8482b1c16fe6b72193ae2f68339c
[framework/graphics/cairo.git] / perf / micro / pattern_create_radial.c
1 /*
2  * Copyright © 2006 Dan Amelang
3  *
4  * Permission to use, copy, modify, distribute, and sell this software
5  * and its documentation for any purpose is hereby granted without
6  * fee, provided that the above copyright notice appear in all copies
7  * and that both that copyright notice and this permission notice
8  * appear in supporting documentation, and that the name of
9  * the authors not be used in advertising or publicity pertaining to
10  * distribution of the software without specific, written prior
11  * permission. The authors make no representations about the
12  * suitability of this software for any purpose.  It is provided "as
13  * is" without express or implied warranty.
14  *
15  * THE AUTHORS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY SPECIAL,
18  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
19  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
20  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
21  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22  *
23  * Authors: Dan Amelang <dan@amelang.net>
24  *
25  * This test was originally created to test _cairo_fixed_from_double.
26  * cairo_pattern_create_radial was selected as the entry point into
27  * cairo as it makes several calls to _cairo_fixed_from_double and
28  * presents a somewhat realistic use-case (although the RADIALS_COUNT
29  * isn't very realistic).
30  */
31 #include <time.h>
32 #include "cairo-perf.h"
33
34 #define RADIALS_COUNT (10000)
35
36 static struct
37 {
38     double cx0;
39     double cy0;
40     double radius0;
41     double cx1;
42     double cy1;
43     double radius1;
44 } radials[RADIALS_COUNT];
45
46 static double
47 generate_double_in_range (double min, double max)
48 {
49     double d;
50
51     d = rand () / (double) RAND_MAX;
52     d *= max - min;
53     d += min;
54
55     return d;
56 }
57
58 static cairo_time_t
59 do_pattern_create_radial (cairo_t *cr, int width, int height, int loops)
60 {
61     cairo_perf_timer_start ();
62     cairo_perf_set_thread_aware (cr, FALSE);
63
64     while (loops--) {
65         cairo_pattern_t *pattern;
66         int i;
67
68         if (loops == 0)
69                 cairo_perf_set_thread_aware (cr, TRUE);
70
71         for (i = 0; i < RADIALS_COUNT; i++) {
72             pattern =
73                 cairo_pattern_create_radial (radials[i].cx0, radials[i].cy0,
74                                              radials[i].radius0,
75                                              radials[i].cx1, radials[i].cy1,
76                                              radials[i].radius1);
77             cairo_pattern_destroy (pattern);
78         }
79     }
80
81     cairo_perf_timer_stop ();
82
83     return cairo_perf_timer_elapsed ();
84 }
85
86 cairo_bool_t
87 pattern_create_radial_enabled (cairo_perf_t *perf)
88 {
89     return cairo_perf_can_run (perf, "pattern-create-radial", NULL);
90 }
91
92 void
93 pattern_create_radial (cairo_perf_t *perf, cairo_t *cr, int width, int height)
94 {
95     int i;
96
97     srand (time (0));
98     for (i = 0; i < RADIALS_COUNT; i++)
99     {
100         radials[i].cx0 = generate_double_in_range (-50000.0, 50000.0);
101         radials[i].cy0 = generate_double_in_range (-50000.0, 50000.0);
102         radials[i].radius0 = generate_double_in_range (0.0, 1000.0);
103         radials[i].cx1 = generate_double_in_range (-50000.0, 50000.0);
104         radials[i].cy1 = generate_double_in_range (-50000.0, 50000.0);
105         radials[i].radius1 = generate_double_in_range (0.0, 1000.0);
106     }
107
108     cairo_perf_run (perf, "pattern-create-radial",
109                           do_pattern_create_radial, NULL);
110 }