summaryrefslogtreecommitdiff
path: root/test/record-replay-extend.c
blob: 4a412335920b3b1f1c3d8d6fe09e710c08cad86b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * Copyright © 2016 Adrian Johnson
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *	Adrian Johnson <ajohnson@redneon.com>
 */

#include "cairo-test.h"
#include <stdio.h>
#include <math.h>

#define PAT_SIZE  32
#define REPLAY_SIZE (PAT_SIZE*4)
#define PAD 10
#define WIDTH (REPLAY_SIZE*4 + PAD*5)
#define HEIGHT (REPLAY_SIZE + PAD*2)

/* Test replaying a recording surface pattern for each type of extend. */

static void
transform_extents(cairo_rectangle_t *extents, cairo_matrix_t *mat)
{
    double x1, y1, x2, y2, x, y;

#define UPDATE_BBOX \
    x1 = x < x1 ? x : x1; \
    y1 = y < y1 ? y : y1; \
    x2 = x > x2 ? x : x2; \
    y2 = y > y2 ? y : y2;

    x = extents->x;
    y = extents->y;
    cairo_matrix_transform_point (mat, &x, &y);
    x1 = x2 = x;
    y1 = y2 = y;

    x = extents->x + extents->width;
    y = extents->y;
    cairo_matrix_transform_point (mat, &x, &y);
    UPDATE_BBOX;

    x = extents->x;
    y = extents->y + extents->height;
    cairo_matrix_transform_point (mat, &x, &y);
    UPDATE_BBOX;

    x = extents->x + extents->width;
    y = extents->y + extents->height;
    cairo_matrix_transform_point (mat, &x, &y);
    UPDATE_BBOX;

    extents->x = x1;
    extents->y = y1;
    extents->width = x2 - extents->x;
    extents->height = y2 - extents->y;

#undef UPDATE_BBOX
}

static cairo_pattern_t *
create_pattern (cairo_matrix_t *mat, cairo_extend_t extend)
{
    cairo_surface_t *surf;
    cairo_pattern_t *pat;
    cairo_t *cr;
    cairo_rectangle_t extents = { 0, 0, PAT_SIZE, PAT_SIZE };

    transform_extents (&extents, mat);
    surf = cairo_recording_surface_create (CAIRO_CONTENT_COLOR_ALPHA, &extents);

    cr = cairo_create (surf);
    cairo_transform (cr, mat);

    cairo_rectangle (cr, 0, 0, PAT_SIZE/2, PAT_SIZE/2);
    cairo_set_source_rgb (cr, 1, 0, 0);
    cairo_fill (cr);

    cairo_translate (cr, PAT_SIZE/2, 0);
    cairo_rectangle (cr, 0, 0, PAT_SIZE/2, PAT_SIZE/2);
    cairo_set_source_rgb (cr, 0, 1, 0);
    cairo_fill (cr);

    cairo_translate (cr, 0, PAT_SIZE/2);
    cairo_rectangle (cr, 0, 0, PAT_SIZE/2, PAT_SIZE/2);
    cairo_set_source_rgb (cr, 0, 0, 1);
    cairo_fill (cr);

    cairo_translate (cr, -PAT_SIZE/2, 0);
    cairo_rectangle (cr, 0, 0, PAT_SIZE/2, PAT_SIZE/2);
    cairo_set_source_rgb (cr, 1, 1, 0);
    cairo_fill (cr);

    cairo_destroy (cr);

    pat = cairo_pattern_create_for_surface (surf);
    cairo_surface_destroy (surf);
    cairo_pattern_set_matrix (pat, mat);
    cairo_pattern_set_extend (pat, extend);
    cairo_pattern_set_filter (pat, CAIRO_FILTER_NEAREST);

    return pat;
}

static cairo_test_status_t
record_replay_extend (cairo_t *cr, int width, int height, cairo_extend_t extend)
{
    cairo_pattern_t *pat;
    cairo_matrix_t mat;

    /* record surface extents (-PAT_SIZE/2, -PAT_SIZE/2) to (PAT_SIZE/2, PAT_SIZE/2) */
    cairo_translate (cr, PAD, PAD);
    cairo_matrix_init_translate (&mat, -PAT_SIZE/2, -PAT_SIZE/2);
    pat = create_pattern (&mat, extend);

    /* test repeating patterns when the source is outside of the target clip */
    if (extend == CAIRO_EXTEND_REPEAT || extend == CAIRO_EXTEND_REFLECT) {
	cairo_matrix_init_translate (&mat, 3*PAT_SIZE/2, 3*PAT_SIZE/2);
	cairo_pattern_set_matrix (pat, &mat);
    }

    cairo_set_source (cr, pat);
    cairo_pattern_destroy (pat);
    cairo_rectangle (cr, 0, 0, REPLAY_SIZE, REPLAY_SIZE);
    cairo_fill (cr);

    /* record surface extents (-2*PAT_SIZE/2, -2*PAT_SIZE/2) to (2*PAT_SIZE/2, 2*PAT_SIZE/2) */
    cairo_translate (cr, REPLAY_SIZE + PAD, 0);
    cairo_matrix_init_translate (&mat, -2.0*PAT_SIZE/2, -2.0*PAT_SIZE/2);
    cairo_matrix_scale (&mat, 2, 2);
    pat = create_pattern (&mat, extend);
    cairo_set_source (cr, pat);
    cairo_pattern_destroy (pat);
    cairo_rectangle (cr, 0, 0, REPLAY_SIZE, REPLAY_SIZE);
    cairo_fill (cr);

    /* record surface extents (-0.5*PAT_SIZE/2, -0.5*PAT_SIZE/2) to (0.5*PAT_SIZE/2, 0.5*PAT_SIZE/2) */
    cairo_translate (cr, REPLAY_SIZE + PAD, 0);
    cairo_matrix_init_translate (&mat, -0.5*PAT_SIZE/2, -0.5*PAT_SIZE/2);
    cairo_matrix_scale (&mat, 0.5, 0.5);
    pat = create_pattern (&mat, extend);
    cairo_set_source (cr, pat);
    cairo_pattern_destroy (pat);
    cairo_rectangle (cr, 0, 0, REPLAY_SIZE, REPLAY_SIZE);
    cairo_fill (cr);

    /* record surface centered on (0,0) and rotated 45 deg */
    cairo_translate (cr, REPLAY_SIZE + PAD, 0);
    cairo_matrix_init_translate (&mat, -PAT_SIZE/sqrt(2), -PAT_SIZE/sqrt(2));
    cairo_matrix_rotate (&mat, M_PI/4.0);
    cairo_matrix_translate (&mat, PAT_SIZE/2, -PAT_SIZE/2);
    pat = create_pattern (&mat, extend);
    cairo_set_source (cr, pat);
    cairo_pattern_destroy (pat);
    cairo_rectangle (cr, 0, 0, REPLAY_SIZE, REPLAY_SIZE);
    cairo_fill (cr);

    return CAIRO_TEST_SUCCESS;
}

static cairo_test_status_t
record_replay_extend_none (cairo_t *cr, int width, int height)
{
    return record_replay_extend (cr, width, height, CAIRO_EXTEND_NONE);
}

static cairo_test_status_t
record_replay_extend_repeat (cairo_t *cr, int width, int height)
{
    return record_replay_extend (cr, width, height, CAIRO_EXTEND_REPEAT);
}

static cairo_test_status_t
record_replay_extend_reflect (cairo_t *cr, int width, int height)
{
    return record_replay_extend (cr, width, height, CAIRO_EXTEND_REFLECT);
}

static cairo_test_status_t
record_replay_extend_pad (cairo_t *cr, int width, int height)
{
    return record_replay_extend (cr, width, height, CAIRO_EXTEND_PAD);
}

CAIRO_TEST (record_replay_extend_none,
	    "Paint recording pattern with CAIRO_EXTEND_NONE",
	    "record,pattern,extend", /* keywords */
	    NULL, /* requirements */
	    WIDTH, HEIGHT,
	    NULL, record_replay_extend_none)
CAIRO_TEST (record_replay_extend_repeat,
	    "Paint recording pattern with CAIRO_EXTEND_REPEAT",
	    "record,pattern,extend", /* keywords */
	    NULL, /* requirements */
	    WIDTH, HEIGHT,
	    NULL, record_replay_extend_repeat)
CAIRO_TEST (record_replay_extend_reflect,
	    "Paint recording pattern with CAIRO_EXTEND_REFLECT",
	    "record,pattern,extend", /* keywords */
	    NULL, /* requirements */
	    WIDTH, HEIGHT,
	    NULL, record_replay_extend_reflect)
CAIRO_TEST (record_replay_extend_pad,
	    "Paint recording pattern with CAIRO_EXTEND_PAD",
	    "record,pattern,extend", /* keywords */
	    NULL, /* requirements */
	    WIDTH, HEIGHT,
	    NULL, record_replay_extend_pad)