1 /*
2  * Copyright © 2007 Chris Wilson.
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  * Chris Wilson. Not be used in advertising or publicity pertaining to
10  * distribution of the software without specific, written prior
11  * permission. Chris Wilson 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  * CHRIS WILSON DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
16  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS, IN NO EVENT SHALL CHRIS WILSON 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: Chris Wilson <chris at chris-wilson.co.uk>
24  */
25 
26 #if HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29 
30 #include "cairo-test.h"
31 #include <stdlib.h> /* drand48() */
32 
33 #define LOOPS 10
34 #define NRAND 100
35 
36 #ifndef HAVE_DRAND48
37 #define drand48() (rand () / (double) RAND_MAX)
38 #endif
39 
40 static cairo_scaled_font_t *scaled_font;
41 
42 static cairo_t *
_cairo_create_similar(cairo_t * cr,int width,int height)43 _cairo_create_similar (cairo_t *cr, int width, int height)
44 {
45     cairo_surface_t *similar;
46 
47     similar = cairo_surface_create_similar (cairo_get_target (cr),
48 	                                    cairo_surface_get_content (cairo_get_target (cr)),
49 				            width, height);
50     cr = cairo_create (similar);
51     cairo_surface_destroy (similar);
52 
53     return cr;
54 }
55 
56 static cairo_t *
_cairo_create_image(cairo_t * cr,cairo_format_t format,int width,int height)57 _cairo_create_image (cairo_t *cr, cairo_format_t format, int width, int height)
58 {
59     cairo_surface_t *image;
60 
61     image = cairo_image_surface_create (format, width, height);
62     cr = cairo_create (image);
63     cairo_surface_destroy (image);
64 
65     return cr;
66 }
67 
68 static void
_propagate_status(cairo_t * dst,cairo_t * src)69 _propagate_status (cairo_t *dst, cairo_t *src)
70 {
71     cairo_path_t path;
72 
73     path.status = cairo_status (src);
74     if (path.status) {
75 	path.num_data = 0;
76 	path.data = NULL;
77 	cairo_append_path (dst, &path);
78     }
79 }
80 
81 static void
_draw(cairo_t * cr,double red,double green,double blue)82 _draw (cairo_t *cr,
83        double red,
84        double green,
85        double blue)
86 {
87     cairo_text_extents_t extents;
88 
89     cairo_set_source_rgb (cr, red, green, blue);
90     cairo_paint (cr);
91 
92     cairo_move_to (cr, 0, 0);
93     cairo_line_to (cr, 1, 1);
94     cairo_stroke (cr);
95 
96     cairo_mask (cr, cairo_get_source (cr));
97 
98     cairo_set_scaled_font (cr, scaled_font);
99     cairo_text_extents (cr, "cairo", &extents);
100     cairo_move_to (cr,
101 	           -extents.x_bearing - .5 * extents.width,
102 		   -extents.y_bearing - .5 * extents.height);
103     cairo_show_text (cr, "cairo");
104 }
105 
106 static void
use_similar(cairo_t * cr,double red,double green,double blue)107 use_similar (cairo_t *cr,
108 	    double red,
109 	    double green,
110 	    double blue)
111 {
112     cairo_t *cr2;
113 
114     if (cairo_status (cr))
115 	return;
116 
117     cr2 = _cairo_create_similar (cr, 1, 1);
118 
119     _draw (cr2, red, green, blue);
120 
121     _propagate_status (cr, cr2);
122     cairo_destroy (cr2);
123 }
124 
125 static void
use_image(cairo_t * cr,cairo_format_t format,double red,double green,double blue)126 use_image (cairo_t *cr,
127 	   cairo_format_t format,
128 	   double red,
129 	   double green,
130 	   double blue)
131 {
132     cairo_t *cr2;
133 
134     if (cairo_status (cr))
135 	return;
136 
137     cr2 = _cairo_create_image (cr, format, 1, 1);
138 
139     _draw (cr2, red, green, blue);
140 
141     _propagate_status (cr, cr2);
142     cairo_destroy (cr2);
143 }
144 
145 static void
use_solid(cairo_t * cr,double red,double green,double blue)146 use_solid (cairo_t *cr,
147 	   double red,
148 	   double green,
149 	   double blue)
150 {
151     /* mix in dissimilar solids */
152     use_image (cr, CAIRO_FORMAT_A1, red, green, blue);
153     use_image (cr, CAIRO_FORMAT_A8, red, green, blue);
154     use_image (cr, CAIRO_FORMAT_RGB24, red, green, blue);
155     use_image (cr, CAIRO_FORMAT_ARGB32, red, green, blue);
156 
157     use_similar (cr, red, green, blue);
158 
159     _draw (cr, red, green, blue);
160 }
161 
162 static cairo_test_status_t
draw(cairo_t * cr,int width,int height)163 draw (cairo_t *cr, int width, int height)
164 {
165     const cairo_test_context_t *ctx = cairo_test_get_context (cr);
166     cairo_status_t status;
167     const double colors[8][3] = {
168 	{ 1.0, 0.0, 0.0 }, /* red */
169 	{ 0.0, 1.0, 0.0 }, /* green */
170 	{ 1.0, 1.0, 0.0 }, /* yellow */
171 	{ 0.0, 0.0, 1.0 }, /* blue */
172 	{ 1.0, 0.0, 1.0 }, /* magenta */
173 	{ 0.0, 1.0, 1.0 }, /* cyan */
174 	{ 1.0, 1.0, 1.0 }, /* white */
175 	{ 0.0, 0.0, 0.0 }, /* black */
176     };
177     int i, j, loop;
178 
179     /* cache a resolved scaled-font */
180     scaled_font = cairo_get_scaled_font (cr);
181 
182     for (loop = 0; loop < LOOPS; loop++) {
183 	for (i = 0; i < LOOPS; i++) {
184 	    for (j = 0; j < 8; j++) {
185 		use_solid (cr, colors[j][0], colors[j][1], colors[j][2]);
186 		status = cairo_status (cr);
187 		if (status)
188 		    return cairo_test_status_from_status (ctx, status);
189 	    }
190 	}
191 
192 	for (i = 0; i < NRAND; i++) {
193 	    use_solid (cr, drand48 (), drand48 (), drand48 ());
194 	    status = cairo_status (cr);
195 	    if (status)
196 		return cairo_test_status_from_status (ctx, status);
197 	}
198     }
199 
200     /* stress test only, so clear the surface before comparing */
201     cairo_set_source_rgb (cr, 0, 0, 1);
202     cairo_paint (cr);
203 
204     return CAIRO_TEST_SUCCESS;
205 }
206 
207 CAIRO_TEST (solid_pattern_cache_stress,
208 	    "Stress the solid pattern cache and ensure it behaves",
209 	    "stress", /* keywords */
210 	    NULL, /* requirements */
211 	    1, 1,
212 	    NULL, draw)
213