1
/*
2
 * Copyright © 2008 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@chris-wilson.co.uk>
24
 */
25

            
26
/* This test tries to emulate the behaviour of most toolkits; it tries
27
 * to simulate typical usage of a single surface with multiple exposures.
28
 *
29
 * The first goal of the test is to reproduce the XSetClipMask(NULL) bug
30
 * reintroduced in 1.6.2 (but was originally fixed in 40558cb15). As I've
31
 * made the same mistake again, it is worth adding a regression test...
32
 */
33

            
34
#include "cairo-test.h"
35

            
36
#include <stdio.h>
37
#include <stdlib.h>
38

            
39
#include "cairo.h"
40

            
41
#include "buffer-diff.h"
42

            
43
#define SIZE 160
44
#define NLOOPS 10
45

            
46
static const char *png_filename = "romedalen.png";
47

            
48
static void
49
303
draw_mask (cairo_t *cr)
50
{
51
    cairo_surface_t *surface;
52
    cairo_t *cr2;
53

            
54
303
    surface = cairo_surface_create_similar (cairo_get_group_target (cr),
55
	                                    CAIRO_CONTENT_ALPHA,
56
					    50, 50);
57
303
    cr2 = cairo_create (surface);
58
303
    cairo_surface_destroy (surface);
59

            
60
303
    cairo_rectangle (cr2,
61
	             0, 0,
62
	             40, 40);
63
303
    cairo_rectangle (cr2,
64
	             10, 10,
65
	             40, 40);
66
303
    cairo_clip (cr2);
67

            
68
303
    cairo_move_to (cr2, 0, 25);
69
303
    cairo_line_to (cr2, 50, 25);
70
303
    cairo_move_to (cr2, 25, 0);
71
303
    cairo_line_to (cr2, 25, 50);
72
303
    cairo_set_source_rgb (cr2, 1, 1, 1);
73
303
    cairo_stroke (cr2);
74

            
75
303
    cairo_set_source_rgb (cr, 1, 0, 0);
76
303
    cairo_mask_surface (cr, cairo_get_target (cr2), 50, 50);
77
303
    cairo_destroy (cr2);
78
303
}
79

            
80
static cairo_surface_t *
81
3
clone_similar_surface (cairo_surface_t * target, cairo_surface_t *surface)
82
{
83
    cairo_t *cr;
84
    cairo_surface_t *similar;
85

            
86
3
    similar = cairo_surface_create_similar (target,
87
	                              cairo_surface_get_content (surface),
88
				      cairo_image_surface_get_width (surface),
89
				      cairo_image_surface_get_height (surface));
90

            
91
3
    cr = cairo_create (similar);
92
3
    cairo_surface_destroy (similar);
93

            
94
3
    cairo_set_source_surface (cr, surface, 0, 0);
95
3
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
96
3
    cairo_paint (cr);
97

            
98
3
    similar = cairo_surface_reference (cairo_get_target (cr));
99
3
    cairo_destroy (cr);
100

            
101
3
    return similar;
102
}
103

            
104
static void
105
303
draw_image (const cairo_test_context_t *ctx,
106
	    cairo_t *cr,
107
	    cairo_surface_t *image)
108
{
109
303
    cairo_set_source_surface (cr, image, 0, 0);
110
303
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
111
303
    cairo_paint (cr);
112
303
}
113

            
114
static void
115
303
draw (const cairo_test_context_t *ctx,
116
      cairo_t *cr,
117
      cairo_surface_t *image,
118
      cairo_rectangle_t *region,
119
      int n_regions)
120
{
121
303
    cairo_save (cr);
122
303
    if (region != NULL) {
123
	int i;
124
1500
	for (i = 0; i < n_regions; i++) {
125
1200
	    cairo_rectangle (cr,
126
1200
			     region[i].x, region[i].y,
127
1200
			     region[i].width, region[i].height);
128
	}
129
300
	cairo_clip (cr);
130
    }
131
303
    cairo_push_group (cr);
132
303
    draw_image (ctx, cr, image);
133
303
    draw_mask (cr);
134
303
    cairo_pop_group_to_source (cr);
135
303
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
136
303
    cairo_paint (cr);
137
303
    cairo_restore (cr);
138
303
}
139

            
140
static cairo_test_status_t
141
3
draw_func (cairo_t *cr, int width, int height)
142
{
143
    cairo_rectangle_t region[4];
144
    const cairo_test_context_t *ctx;
145
    cairo_surface_t *source, *image;
146
    int i, j;
147

            
148
3
    ctx = cairo_test_get_context (cr);
149

            
150
3
    source = cairo_test_create_surface_from_png (ctx, png_filename);
151
3
    image = clone_similar_surface (cairo_get_group_target (cr), source);
152
3
    cairo_surface_destroy (source);
153

            
154
3
    cairo_push_group_with_content (cr, CAIRO_CONTENT_COLOR);
155
3
    draw (ctx, cr, image, NULL, 0);
156
33
    for (i = 0; i < NLOOPS; i++) {
157
330
	for (j = 0; j < NLOOPS; j++) {
158
300
	    region[0].x = i * SIZE / NLOOPS;
159
300
	    region[0].y = i * SIZE / NLOOPS;
160
300
	    region[0].width = SIZE / 4;
161
300
	    region[0].height = SIZE / 4;
162

            
163
300
	    region[1].x = j * SIZE / NLOOPS;
164
300
	    region[1].y = j * SIZE / NLOOPS;
165
300
	    region[1].width = SIZE / 4;
166
300
	    region[1].height = SIZE / 4;
167

            
168
300
	    region[2].x = i * SIZE / NLOOPS;
169
300
	    region[2].y = j * SIZE / NLOOPS;
170
300
	    region[2].width = SIZE / 4;
171
300
	    region[2].height = SIZE / 4;
172

            
173
300
	    region[3].x = j * SIZE / NLOOPS;
174
300
	    region[3].y = i * SIZE / NLOOPS;
175
300
	    region[3].width = SIZE / 4;
176
300
	    region[3].height = SIZE / 4;
177

            
178
300
	    draw (ctx, cr, image, region, 4);
179
	}
180
    }
181

            
182
3
    cairo_pop_group_to_source (cr);
183
3
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
184
3
    cairo_paint (cr);
185

            
186
3
    cairo_surface_destroy (image);
187

            
188
3
    return CAIRO_TEST_SUCCESS;
189
}
190

            
191
1
CAIRO_TEST (xlib_expose_event,
192
	    "Emulate a typical expose event",
193
	    "xlib", /* keywords */
194
	    NULL, /* requirements */
195
	    SIZE, SIZE,
196
	    NULL, draw_func)