1 /*
2  * Copyright © 2006 Red Hat, Inc.
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  * Red Hat, Inc. not be used in advertising or publicity pertaining to
10  * distribution of the software without specific, written prior
11  * permission. Red Hat, Inc. makes 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  * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL RED HAT, INC. 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  * Author: Carl D. Worth <cworth@cworth.org>
24  */
25 
26 #include "cairo-perf.h"
27 
28 static void
init_and_set_source_surface(cairo_t * cr,cairo_surface_t * source,int width,int height)29 init_and_set_source_surface (cairo_t		*cr,
30 			     cairo_surface_t	*source,
31 			     int		 width,
32 			     int		 height)
33 {
34     cairo_t *cr2;
35 
36     /* Fill it with something known */
37     cr2 = cairo_create (source);
38     cairo_set_operator (cr2, CAIRO_OPERATOR_SOURCE);
39     cairo_set_source_rgb (cr2, 0, 0, 1); /* blue */
40     cairo_paint (cr2);
41 
42     cairo_set_source_rgba (cr2, 1, 0, 0, 0.5); /* 50% red */
43     cairo_new_path (cr2);
44     cairo_rectangle (cr2, 0, 0, width/2.0, height/2.0);
45     cairo_rectangle (cr2, width/2.0, height/2.0, width/2.0, height/2.0);
46     cairo_fill (cr2);
47 
48     cairo_set_source_surface (cr, cairo_get_target (cr2), 0, 0);
49     cairo_destroy (cr2);
50 }
51 
52 static void
set_source_solid_rgb(cairo_t * cr,int width,int height)53 set_source_solid_rgb (cairo_t	*cr,
54 		      int	 width,
55 		      int	 height)
56 {
57     cairo_set_source_rgb (cr, 0.2, 0.6, 0.9);
58 }
59 
60 static void
set_source_solid_rgba(cairo_t * cr,int width,int height)61 set_source_solid_rgba (cairo_t	*cr,
62 		       int	 width,
63 		       int	 height)
64 {
65     cairo_set_source_rgba (cr, 0.2, 0.6, 0.9, 0.7);
66 }
67 
68 static void
set_source_image_surface_rgb(cairo_t * cr,int width,int height)69 set_source_image_surface_rgb (cairo_t	*cr,
70 			      int	 width,
71 			      int	 height)
72 {
73     cairo_surface_t *source;
74 
75     source = cairo_image_surface_create (CAIRO_FORMAT_RGB24,
76 					 width, height);
77     init_and_set_source_surface (cr, source, width, height);
78 
79     cairo_surface_destroy (source);
80 }
81 
82 static void
set_source_image_surface_rgba(cairo_t * cr,int width,int height)83 set_source_image_surface_rgba (cairo_t	*cr,
84 			       int	 width,
85 			       int	 height)
86 {
87     cairo_surface_t *source;
88 
89     source = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
90 					 width, height);
91     init_and_set_source_surface (cr, source, width, height);
92 
93     cairo_surface_destroy (source);
94 }
95 
96 static void
set_source_image_surface_rgba_mag(cairo_t * cr,int width,int height)97 set_source_image_surface_rgba_mag (cairo_t	*cr,
98 				   int		width,
99 				   int		height)
100 {
101     cairo_surface_t *source;
102 
103     source = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
104 					 width/2, height/2);
105     cairo_scale(cr, 2.1, 2.1);
106     init_and_set_source_surface (cr, source, width/2, height/2);
107     cairo_scale(cr, 1/2.1, 1/2.1);
108 
109     cairo_surface_destroy (source);
110 }
111 
112 static void
set_source_image_surface_rgba_min(cairo_t * cr,int width,int height)113 set_source_image_surface_rgba_min (cairo_t	*cr,
114 				   int		width,
115 				   int		height)
116 {
117     cairo_surface_t *source;
118 
119     source = cairo_image_surface_create (CAIRO_FORMAT_ARGB32,
120 					 width*2, height*2);
121     cairo_scale(cr, 1/1.9, 1/1.9);
122     init_and_set_source_surface (cr, source, width*2, height*2);
123     cairo_scale(cr, 1.9, 1.9);
124 
125     cairo_surface_destroy (source);
126 }
127 
128 static void
set_source_similar_surface_rgb(cairo_t * cr,int width,int height)129 set_source_similar_surface_rgb (cairo_t	*cr,
130 				int	 width,
131 				int	 height)
132 {
133     cairo_surface_t *source;
134 
135     source = cairo_surface_create_similar (cairo_get_group_target (cr),
136 					   CAIRO_CONTENT_COLOR,
137 					   width, height);
138     init_and_set_source_surface (cr, source, width, height);
139 
140     cairo_surface_destroy (source);
141 }
142 
143 static void
set_source_similar_surface_rgba(cairo_t * cr,int width,int height)144 set_source_similar_surface_rgba (cairo_t	*cr,
145 				 int		 width,
146 				 int		 height)
147 {
148     cairo_surface_t *source;
149 
150     source = cairo_surface_create_similar (cairo_get_group_target (cr),
151 					   CAIRO_CONTENT_COLOR_ALPHA,
152 					   width, height);
153     init_and_set_source_surface (cr, source, width, height);
154 
155     cairo_surface_destroy (source);
156 }
157 
158 static void
set_source_similar_surface_rgba_mag(cairo_t * cr,int width,int height)159 set_source_similar_surface_rgba_mag (cairo_t	*cr,
160 				     int	width,
161 				     int	height)
162 {
163     cairo_surface_t *source;
164 
165     source = cairo_surface_create_similar (cairo_get_group_target (cr),
166 					   CAIRO_CONTENT_COLOR_ALPHA,
167 					   width/2, height/2);
168     cairo_scale(cr, 2.1, 2.1);
169     init_and_set_source_surface (cr, source, width/2, height/2);
170     cairo_scale(cr, 1/2.1, 1/2.1);
171 
172     cairo_surface_destroy (source);
173 }
174 
175 static void
set_source_similar_surface_rgba_min(cairo_t * cr,int width,int height)176 set_source_similar_surface_rgba_min (cairo_t	*cr,
177 				     int	width,
178 				     int	height)
179 {
180     cairo_surface_t *source;
181 
182     source = cairo_surface_create_similar (cairo_get_group_target (cr),
183 					   CAIRO_CONTENT_COLOR_ALPHA,
184 					   width*2, height*2);
185     cairo_scale(cr, 1/1.9, 1/1.9);
186     init_and_set_source_surface (cr, source, width*2, height*2);
187     cairo_scale(cr, 1.9, 1.9);
188 
189     cairo_surface_destroy (source);
190 }
191 
192 static void
set_source_linear_rgb(cairo_t * cr,int width,int height)193 set_source_linear_rgb (cairo_t *cr,
194 		       int	width,
195 		       int	height)
196 {
197     cairo_pattern_t *linear;
198 
199     linear = cairo_pattern_create_linear (0.0, 0.0, width, height);
200     cairo_pattern_add_color_stop_rgb (linear, 0.0, 1, 0, 0); /* red */
201     cairo_pattern_add_color_stop_rgb (linear, 1.0, 0, 0, 1); /* blue */
202 
203     cairo_set_source (cr, linear);
204 
205     cairo_pattern_destroy (linear);
206 }
207 
208 static void
set_source_linear_rgba(cairo_t * cr,int width,int height)209 set_source_linear_rgba (cairo_t *cr,
210 			int	width,
211 			int	height)
212 {
213     cairo_pattern_t *linear;
214 
215     linear = cairo_pattern_create_linear (0.0, 0.0, width, height);
216     cairo_pattern_add_color_stop_rgba (linear, 0.0, 1, 0, 0, 0.5); /* 50% red */
217     cairo_pattern_add_color_stop_rgba (linear, 1.0, 0, 0, 1, 0.0); /*  0% blue */
218 
219     cairo_set_source (cr, linear);
220 
221     cairo_pattern_destroy (linear);
222 }
223 
224 static void
set_source_linear3_rgb(cairo_t * cr,int width,int height)225 set_source_linear3_rgb (cairo_t *cr,
226 		       int	width,
227 		       int	height)
228 {
229     cairo_pattern_t *linear;
230 
231     linear = cairo_pattern_create_linear (0.0, 0.0, width, height);
232     cairo_pattern_add_color_stop_rgb (linear, 0.0, 1, 0, 0); /* red */
233     cairo_pattern_add_color_stop_rgb (linear, 0.5, 0, 1, 0); /* green */
234     cairo_pattern_add_color_stop_rgb (linear, 1.0, 0, 0, 1); /* blue */
235 
236     cairo_set_source (cr, linear);
237 
238     cairo_pattern_destroy (linear);
239 }
240 
241 static void
set_source_linear3_rgba(cairo_t * cr,int width,int height)242 set_source_linear3_rgba (cairo_t *cr,
243 			int	width,
244 			int	height)
245 {
246     cairo_pattern_t *linear;
247 
248     linear = cairo_pattern_create_linear (0.0, 0.0, width, height);
249     cairo_pattern_add_color_stop_rgba (linear, 0.0, 1, 0, 0, 0.5); /* 50% red */
250     cairo_pattern_add_color_stop_rgba (linear, 0.5, 0, 1, 0, 0.0); /*  0% green */
251     cairo_pattern_add_color_stop_rgba (linear, 1.0, 0, 0, 1, 0.5); /*  50% blue */
252 
253     cairo_set_source (cr, linear);
254 
255     cairo_pattern_destroy (linear);
256 }
257 
258 static void
set_source_radial_rgb(cairo_t * cr,int width,int height)259 set_source_radial_rgb (cairo_t *cr,
260 		       int	width,
261 		       int	height)
262 {
263     cairo_pattern_t *radial;
264 
265     radial = cairo_pattern_create_radial (width/2.0, height/2.0, 0.0,
266 					  width/2.0, height/2.0, width/2.0);
267     cairo_pattern_add_color_stop_rgb (radial, 0.0, 1, 0, 0); /* red */
268     cairo_pattern_add_color_stop_rgb (radial, 1.0, 0, 0, 1); /* blue */
269 
270     cairo_set_source (cr, radial);
271 
272     cairo_pattern_destroy (radial);
273 }
274 
275 static void
set_source_radial_rgba(cairo_t * cr,int width,int height)276 set_source_radial_rgba (cairo_t *cr,
277 			int	width,
278 			int	height)
279 {
280     cairo_pattern_t *radial;
281 
282     radial = cairo_pattern_create_radial (width/2.0, height/2.0, 0.0,
283 					  width/2.0, height/2.0, width/2.0);
284     cairo_pattern_add_color_stop_rgba (radial, 0.0, 1, 0, 0, 0.5); /* 50% red */
285     cairo_pattern_add_color_stop_rgba (radial, 1.0, 0, 0, 1, 0.0); /*  0% blue */
286 
287     cairo_set_source (cr, radial);
288 
289     cairo_pattern_destroy (radial);
290 }
291 
292 typedef void (*set_source_func_t) (cairo_t *cr, int width, int height);
293 
294 void
cairo_perf_cover_sources_and_operators(cairo_perf_t * perf,const char * name,cairo_perf_func_t perf_func,cairo_count_func_t count_func)295 cairo_perf_cover_sources_and_operators (cairo_perf_t		*perf,
296 					const char		*name,
297 					cairo_perf_func_t	 perf_func,
298 					cairo_count_func_t	 count_func)
299 {
300     unsigned int i, j;
301     char *expanded_name;
302 
303     struct { set_source_func_t set_source; const char *name; } sources[] = {
304 	{ set_source_solid_rgb, "solid-rgb" },
305 	{ set_source_solid_rgba, "solid-rgba" },
306 	{ set_source_image_surface_rgb, "image-rgb" },
307 	{ set_source_image_surface_rgba, "image-rgba" },
308 	{ set_source_image_surface_rgba_mag, "image-rgba-mag" },
309 	{ set_source_image_surface_rgba_min, "image-rgba-min" },
310 	{ set_source_similar_surface_rgb, "similar-rgb" },
311 	{ set_source_similar_surface_rgba, "similar-rgba" },
312 	{ set_source_similar_surface_rgba_mag, "similar-rgba-mag" },
313 	{ set_source_similar_surface_rgba_min, "similar-rgba-min" },
314 	{ set_source_linear_rgb, "linear-rgb" },
315 	{ set_source_linear_rgba, "linear-rgba" },
316 	{ set_source_linear3_rgb, "linear3-rgb" },
317 	{ set_source_linear3_rgba, "linear3-rgba" },
318 	{ set_source_radial_rgb, "radial-rgb" },
319 	{ set_source_radial_rgba, "radial-rgba" }
320     };
321 
322     struct { cairo_operator_t op; const char *name; } operators[] = {
323 	{ CAIRO_OPERATOR_OVER, "over" },
324 	{ CAIRO_OPERATOR_SOURCE, "source" }
325     };
326 
327     for (i = 0; i < ARRAY_LENGTH (sources); i++) {
328 	(sources[i].set_source) (perf->cr, perf->size, perf->size);
329 
330 	for (j = 0; j < ARRAY_LENGTH (operators); j++) {
331 	    cairo_set_operator (perf->cr, operators[j].op);
332 
333 	    xasprintf (&expanded_name, "%s_%s_%s",
334 		       name, sources[i].name, operators[j].name);
335 	    cairo_perf_run (perf, expanded_name, perf_func, count_func);
336 	    free (expanded_name);
337 	}
338     }
339 }
340