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 
28 /* This is a variant on many strokes where we precompute
29  * a simplified stroke-to-path.
30  * When we have a real stroke-to-path, it would useful to compare the cost
31  * of stroking vs filling the "identical" paths.
32  */
33 
34 #include "cairo-perf.h"
35 
36 static uint32_t state;
37 
38 static double
uniform_random(double minval,double maxval)39 uniform_random (double minval, double maxval)
40 {
41     static uint32_t const poly = 0x9a795537U;
42     uint32_t n = 32;
43     while (n-->0)
44 	state = 2*state < state ? (2*state ^ poly) : 2*state;
45     return minval + state * (maxval - minval) / 4294967296.0;
46 }
47 
48 static cairo_time_t
do_many_fills_ha(cairo_t * cr,int width,int height,int loops)49 do_many_fills_ha (cairo_t *cr, int width, int height, int loops)
50 {
51     int count;
52 
53     state = 0xc0ffee;
54     for (count = 0; count < 1000; count++) {
55 	double y = floor (uniform_random (0, height));
56 	double x = floor (uniform_random (0, width));
57 	cairo_rectangle (cr, x, y, ceil (uniform_random (0, width)) - x, 1);
58     }
59 
60     cairo_perf_timer_start ();
61 
62     while (loops--)
63 	cairo_fill_preserve (cr);
64 
65     cairo_perf_timer_stop ();
66 
67     cairo_new_path (cr);
68 
69     return cairo_perf_timer_elapsed ();
70 }
71 
72 static cairo_time_t
do_many_fills_h(cairo_t * cr,int width,int height,int loops)73 do_many_fills_h (cairo_t *cr, int width, int height, int loops)
74 {
75     int count;
76 
77     state = 0xc0ffee;
78     for (count = 0; count < 1000; count++) {
79 	double y = uniform_random (0, height);
80 	double x = uniform_random (0, width);
81 	cairo_rectangle (cr, x, y, uniform_random (0, width) - x, 1);
82     }
83 
84     cairo_perf_timer_start ();
85 
86     while (loops--)
87 	cairo_fill_preserve (cr);
88 
89     cairo_perf_timer_stop ();
90 
91     cairo_new_path (cr);
92 
93     return cairo_perf_timer_elapsed ();
94 }
95 
96 static cairo_time_t
do_many_fills_va(cairo_t * cr,int width,int height,int loops)97 do_many_fills_va (cairo_t *cr, int width, int height, int loops)
98 {
99     int count;
100 
101     state = 0xc0ffee;
102     for (count = 0; count < 1000; count++) {
103 	double x = floor (uniform_random (0, width));
104 	double y = floor (uniform_random (0, height));
105 	cairo_rectangle (cr, x, y, 1, ceil (uniform_random (0, height) - y));
106     }
107 
108     cairo_perf_timer_start ();
109 
110     while (loops--)
111 	cairo_fill_preserve (cr);
112 
113     cairo_perf_timer_stop ();
114 
115     cairo_new_path (cr);
116 
117     return cairo_perf_timer_elapsed ();
118 }
119 
120 static cairo_time_t
do_many_fills_v(cairo_t * cr,int width,int height,int loops)121 do_many_fills_v (cairo_t *cr, int width, int height, int loops)
122 {
123     int count;
124 
125     state = 0xc0ffee;
126     for (count = 0; count < 1000; count++) {
127 	double x = uniform_random (0, width);
128 	double y = uniform_random (0, height);
129 	cairo_rectangle (cr, x, y, 1, uniform_random (0, height) - y);
130     }
131 
132     cairo_perf_timer_start ();
133 
134     while (loops--)
135 	cairo_fill_preserve (cr);
136 
137     cairo_perf_timer_stop ();
138 
139     cairo_new_path (cr);
140 
141     return cairo_perf_timer_elapsed ();
142 }
143 
144 static cairo_time_t
do_many_fills(cairo_t * cr,int width,int height,int loops)145 do_many_fills (cairo_t *cr, int width, int height, int loops)
146 {
147     int count;
148 
149     /* lots and lots of overlapping stroke-like fills */
150     state = 0xc0ffee;
151     for (count = 0; count < 1000; count++) {
152 	cairo_save (cr);
153 	cairo_translate (cr,
154 			 uniform_random (0, width),
155 			 uniform_random (0, height));
156 	cairo_rotate (cr, uniform_random (-M_PI,M_PI));
157 	cairo_rectangle (cr, 0, 0, uniform_random (0, width), 1);
158 	cairo_restore (cr);
159     }
160 
161     cairo_perf_timer_start ();
162 
163     while (loops--)
164 	cairo_fill_preserve (cr);
165 
166     cairo_perf_timer_stop ();
167 
168     cairo_new_path (cr);
169 
170     return cairo_perf_timer_elapsed ();
171 }
172 
173 cairo_bool_t
many_fills_enabled(cairo_perf_t * perf)174 many_fills_enabled (cairo_perf_t *perf)
175 {
176     return cairo_perf_can_run (perf, "many-fills", NULL);
177 }
178 
179 void
many_fills(cairo_perf_t * perf,cairo_t * cr,int width,int height)180 many_fills (cairo_perf_t *perf, cairo_t *cr, int width, int height)
181 {
182     cairo_perf_run (perf, "many-fills-halign", do_many_fills_ha, NULL);
183     cairo_perf_run (perf, "many-fills-valign", do_many_fills_va, NULL);
184     cairo_perf_run (perf, "many-fills-horizontal", do_many_fills_h, NULL);
185     cairo_perf_run (perf, "many-fills-vertical", do_many_fills_v, NULL);
186     cairo_perf_run (perf, "many-fills-random", do_many_fills, NULL);
187 }
188