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
#include "cairo-test.h"
27

            
28
static cairo_surface_t *create_source_surface (int size);
29

            
30
/* We use a relatively large source to exercise bug:
31
 *   Bug 7360 painting huge surfaces fails
32
 *   [https://bugs.freedesktop.org/show_bug.cgi?id=7360]
33
 * but still keep the resultant image small for reasonably quick checking.
34
 */
35
#define SOURCE_SIZE 2000
36
#define INTER_SIZE 512
37
#define SIZE 96
38

            
39
static void
40
18
draw_pattern (cairo_surface_t **surface_inout, int surface_size)
41
{
42
    cairo_t *cr;
43
18
    int mid = surface_size/2;
44

            
45
18
    cr = cairo_create (*surface_inout);
46
18
    cairo_surface_destroy (*surface_inout);
47

            
48
18
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
49
18
    cairo_set_source_rgba (cr, 0, 0, 0, 0);
50
18
    cairo_paint (cr);
51

            
52
18
    cairo_rectangle (cr, 0, 0, surface_size, surface_size);
53
18
    cairo_rectangle (cr, mid - SIZE/4, mid + SIZE/4, SIZE/2, -SIZE/2);
54
18
    cairo_clip (cr);
55

            
56
    /* outside squares -> opaque */
57
18
    cairo_set_source_rgb (cr, 1, 1, 1);
58
18
    cairo_rectangle (cr,
59
		     0, 0,
60
18
		     surface_size / 2, surface_size / 2);
61
18
    cairo_fill (cr);
62
18
    cairo_set_source_rgb (cr, 1, 0, 0);
63
18
    cairo_rectangle (cr,
64
18
		     surface_size / 2, 0,
65
18
		     surface_size / 2, surface_size / 2);
66
18
    cairo_fill (cr);
67
18
    cairo_set_source_rgb (cr, 0, 1, 0);
68
18
    cairo_rectangle (cr,
69
18
		     0, surface_size / 2,
70
18
		     surface_size / 2, surface_size / 2);
71
18
    cairo_fill (cr);
72
18
    cairo_set_source_rgb (cr, 0, 0, 1);
73
18
    cairo_rectangle (cr,
74
18
		     surface_size / 2, surface_size / 2,
75
18
		     surface_size / 2, surface_size / 2);
76
18
    cairo_fill (cr);
77

            
78
18
    cairo_reset_clip (cr);
79
18
    cairo_rectangle (cr, mid - SIZE/4, mid - SIZE/4, SIZE/2, SIZE/2);
80
18
    cairo_clip (cr);
81

            
82
    /* inside squares -> translucent */
83
18
    cairo_set_source_rgba (cr, 0, 0, 1, .5);
84
18
    cairo_rectangle (cr,
85
		     0, 0,
86
18
		     surface_size / 2, surface_size / 2);
87
18
    cairo_fill (cr);
88
18
    cairo_set_source_rgba (cr, 0, 1, 0, .5);
89
18
    cairo_rectangle (cr,
90
18
		     surface_size / 2, 0,
91
18
		     surface_size / 2, surface_size / 2);
92
18
    cairo_fill (cr);
93
18
    cairo_set_source_rgba (cr, 1, 0, 0, .5);
94
18
    cairo_rectangle (cr,
95
18
		     0, surface_size / 2,
96
18
		     surface_size / 2, surface_size / 2);
97
18
    cairo_fill (cr);
98
18
    cairo_set_source_rgba (cr, 1, 1, 1, .5);
99
18
    cairo_rectangle (cr,
100
18
		     surface_size / 2, surface_size / 2,
101
18
		     surface_size / 2, surface_size / 2);
102
18
    cairo_fill (cr);
103

            
104

            
105
18
    *surface_inout = cairo_surface_reference (cairo_get_target (cr));
106
18
    cairo_destroy (cr);
107
18
}
108

            
109
static cairo_test_status_t
110
18
draw (cairo_t *cr, int width, int height)
111
{
112
    cairo_surface_t *surface;
113
    cairo_surface_t *similar;
114
    cairo_status_t status;
115
    cairo_t *cr2;
116

            
117
18
    cairo_set_source_rgb (cr, 0, 0, 0);
118
18
    cairo_paint (cr);
119

            
120
18
    surface = create_source_surface (SOURCE_SIZE);
121
18
    if (surface == NULL) /* can't create the source so skip the test */
122
	return CAIRO_TEST_UNTESTED;
123

            
124
18
    draw_pattern (&surface, SOURCE_SIZE);
125

            
126
    /* copy a subregion to a smaller intermediate surface */
127
18
    similar = cairo_surface_create_similar (surface,
128
					    CAIRO_CONTENT_COLOR_ALPHA,
129
					    INTER_SIZE, INTER_SIZE);
130
18
    cr2 = cairo_create (similar);
131
18
    cairo_surface_destroy (similar);
132
18
    cairo_set_source_surface (cr2, surface,
133
			      (INTER_SIZE - SOURCE_SIZE)/2,
134
			      (INTER_SIZE - SOURCE_SIZE)/2);
135
18
    cairo_paint (cr2);
136

            
137
    /* and then paint onto a small surface for checking */
138
18
    cairo_set_source_surface (cr, cairo_get_target (cr2),
139
18
			      (width - INTER_SIZE)/2,
140
18
			      (height - INTER_SIZE)/2);
141
18
    cairo_destroy (cr2);
142
18
    cairo_rectangle (cr, 16, 16, 64, 64);
143
18
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
144
18
    cairo_fill (cr);
145

            
146
    /* destroy the surface last, as this triggers XCloseDisplay */
147
18
    cairo_surface_finish (surface);
148
18
    status = cairo_surface_status (surface);
149
18
    cairo_surface_destroy (surface);
150

            
151
18
    return cairo_test_status_from_status (cairo_test_get_context (cr),
152
					  status);
153
}
154

            
155
static cairo_test_status_t
156
6
preamble (cairo_test_context_t *ctx)
157
{
158
    cairo_surface_t *surface;
159
    cairo_status_t status;
160

            
161
6
    surface = create_source_surface (SOURCE_SIZE);
162
6
    if (surface == NULL) /* can't create the source so skip the test */
163
	return CAIRO_TEST_UNTESTED;
164

            
165
6
    cairo_surface_finish (surface);
166
6
    status = cairo_surface_status (surface);
167
6
    cairo_surface_destroy (surface);
168

            
169
6
    return cairo_test_status_from_status (ctx, status);
170
}