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
/*
27
 * Test case derived from the bug report by Michel Iwaniec:
28
 * https://lists.cairographics.org/archives/cairo/2008-November/015660.html
29
 */
30

            
31
#include "cairo-test.h"
32

            
33
static cairo_surface_t *
34
3
create_source (cairo_surface_t *target, int width, int height)
35
{
36
    cairo_surface_t *similar;
37
    cairo_t *cr;
38

            
39
3
    similar = cairo_surface_create_similar (target,
40
					    CAIRO_CONTENT_COLOR,
41
					    width, height);
42
3
    cr = cairo_create (similar);
43
3
    cairo_surface_destroy (similar);
44

            
45
3
    cairo_set_source_rgb (cr, 1, 1, 1);
46
3
    cairo_rectangle (cr,
47
3
		     width - 4, height - 4,
48
		     2, 2);
49
3
    cairo_fill (cr);
50
3
    cairo_set_source_rgb (cr, 1, 0, 0);
51
3
    cairo_rectangle (cr,
52
3
		     width - 2, height - 4,
53
		     2, 2);
54
3
    cairo_fill (cr);
55
3
    cairo_set_source_rgb (cr, 0, 1, 0);
56
3
    cairo_rectangle (cr,
57
3
		     width - 4, height - 2,
58
		     2, 2);
59
3
    cairo_fill (cr);
60
3
    cairo_set_source_rgb (cr, 0, 0, 1);
61
3
    cairo_rectangle (cr,
62
3
		     width - 2, height - 2,
63
		     2, 2);
64
3
    cairo_fill (cr);
65

            
66
3
    similar = cairo_surface_reference (cairo_get_target (cr));
67
3
    cairo_destroy (cr);
68

            
69
3
    return similar;
70
}
71

            
72
static void
73
24
draw_grid (cairo_t *cr, cairo_pattern_t *pattern, int dst_x, int dst_y)
74
{
75
    cairo_matrix_t m;
76

            
77
24
    cairo_save (cr);
78
24
    cairo_translate (cr, dst_x, dst_y);
79
24
    cairo_scale (cr, 16, 16);
80
24
    cairo_rotate (cr, 1);
81

            
82
24
    cairo_matrix_init_translate (&m, 2560-4, 1280-4);
83
24
    cairo_pattern_set_matrix (pattern, &m);
84
24
    cairo_set_source (cr, pattern);
85
24
    cairo_rectangle (cr, 0, 0, 4, 4);
86
24
    cairo_fill (cr);
87

            
88
24
    cairo_set_source_rgb (cr, .7, .7, .7);
89
24
    cairo_set_line_width (cr, 1./16);
90
24
    cairo_move_to (cr, 0, 0);
91
24
    cairo_line_to (cr, 4, 0);
92
24
    cairo_move_to (cr, 0, 2);
93
24
    cairo_line_to (cr, 4, 2);
94
24
    cairo_move_to (cr, 0, 4);
95
24
    cairo_line_to (cr, 4, 4);
96
24
    cairo_move_to (cr, 0, 0);
97
24
    cairo_line_to (cr, 0, 4);
98
24
    cairo_move_to (cr, 2, 0);
99
24
    cairo_line_to (cr, 2, 4);
100
24
    cairo_move_to (cr, 4, 0);
101
24
    cairo_line_to (cr, 4, 4);
102
24
    cairo_stroke (cr);
103

            
104
24
    cairo_restore (cr);
105
24
}
106

            
107
static cairo_test_status_t
108
3
draw (cairo_t *cr, int width, int height)
109
{
110
    cairo_surface_t *source;
111
    cairo_pattern_t *pattern;
112

            
113
3
    cairo_paint (cr);
114

            
115
3
    source = create_source (cairo_get_target (cr), 2560, 1280);
116
3
    pattern = cairo_pattern_create_for_surface (source);
117
3
    cairo_surface_destroy (source);
118

            
119
3
    cairo_pattern_set_filter (pattern, CAIRO_FILTER_NEAREST);
120
3
    cairo_pattern_set_extend (pattern, CAIRO_EXTEND_NONE);
121

            
122
3
    draw_grid (cr, pattern, 50, 0);
123
3
    draw_grid (cr, pattern, 130, 0);
124
3
    draw_grid (cr, pattern, 210, 0);
125
3
    draw_grid (cr, pattern, 290, 0);
126

            
127
3
    draw_grid (cr, pattern, 50,  230);
128
3
    draw_grid (cr, pattern, 130, 230);
129
3
    draw_grid (cr, pattern, 210, 230);
130
3
    draw_grid (cr, pattern, 290, 230);
131

            
132
3
    cairo_pattern_destroy (pattern);
133

            
134
3
    return CAIRO_TEST_SUCCESS;
135
}
136

            
137
1
CAIRO_TEST (scale_offset_similar,
138
	    "Tests drawing surfaces under various scales and transforms",
139
	    "surface, scale-offset", /* keywords */
140
	    NULL, /* requirements */
141
	    320, 320,
142
	    NULL, draw)
143