Tizen 2.0 Release
[framework/graphics/cairo.git] / test / recording-surface-extend.c
1 /*
2  * Copyright © 2007 Adrian Johnson
3  * Copyright © 2011 Intel Corporation
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without
8  * restriction, including without limitation the rights to use, copy,
9  * modify, merge, publish, distribute, sublicense, and/or sell copies
10  * of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  *
25  * Authors:
26  *      Adrian Johnson <ajohnson@redneon.com>
27  *      Chris Wilson <chris@chris-wilson.co.uk>
28  */
29
30 #include "cairo-test.h"
31
32 #define PAT_WIDTH  120
33 #define PAT_HEIGHT 120
34 #define SIZE (PAT_WIDTH*2)
35 #define PAD 2
36 #define WIDTH (PAD + SIZE + PAD)
37 #define HEIGHT WIDTH
38
39
40 /* This test is designed to test painting a recording surface pattern with
41  * CAIRO_EXTEND_NONE and a non identity pattern matrix.
42  */
43 static cairo_pattern_t *create_pattern (cairo_t *target)
44 {
45     cairo_surface_t *surface;
46     cairo_pattern_t *pattern;
47     cairo_t *cr;
48
49     surface = cairo_surface_create_similar (cairo_get_group_target (target),
50                                             CAIRO_CONTENT_COLOR_ALPHA,
51                                             PAT_WIDTH, PAT_HEIGHT);
52     cr = cairo_create (surface);
53     cairo_surface_destroy (surface);
54
55     cairo_set_source_rgba (cr, 1, 0, 1, 0.5);
56     cairo_rectangle (cr, PAT_WIDTH/6.0, PAT_HEIGHT/6.0, PAT_WIDTH/4.0, PAT_HEIGHT/4.0);
57     cairo_fill (cr);
58
59     cairo_set_source_rgba (cr, 0, 1, 1, 0.5);
60     cairo_rectangle (cr, PAT_WIDTH/2.0, PAT_HEIGHT/2.0, PAT_WIDTH/4.0, PAT_HEIGHT/4.0);
61     cairo_fill (cr);
62
63     cairo_set_line_width (cr, 1);
64     cairo_move_to (cr, PAT_WIDTH/6.0, 0);
65     cairo_line_to (cr, 0, 0);
66     cairo_line_to (cr, 0, PAT_HEIGHT/6.0);
67     cairo_set_source_rgb (cr, 1, 0, 0);
68     cairo_stroke (cr);
69     cairo_move_to (cr, PAT_WIDTH/6.0, PAT_HEIGHT);
70     cairo_line_to (cr, 0, PAT_HEIGHT);
71     cairo_line_to (cr, 0, 5*PAT_HEIGHT/6.0);
72     cairo_set_source_rgb (cr, 0, 1, 0);
73     cairo_stroke (cr);
74     cairo_move_to (cr, 5*PAT_WIDTH/6.0, 0);
75     cairo_line_to (cr, PAT_WIDTH, 0);
76     cairo_line_to (cr, PAT_WIDTH, PAT_HEIGHT/6.0);
77     cairo_set_source_rgb (cr, 0, 0, 1);
78     cairo_stroke (cr);
79     cairo_move_to (cr, 5*PAT_WIDTH/6.0, PAT_HEIGHT);
80     cairo_line_to (cr, PAT_WIDTH, PAT_HEIGHT);
81     cairo_line_to (cr, PAT_WIDTH, 5*PAT_HEIGHT/6.0);
82     cairo_set_source_rgb (cr, 1, 1, 0);
83     cairo_stroke (cr);
84
85     cairo_set_source_rgb (cr, 0.5, 0.5, 0.5);
86     cairo_set_line_width (cr, PAT_WIDTH/10.0);
87
88     cairo_move_to (cr, 0,         PAT_HEIGHT/4.0);
89     cairo_line_to (cr, PAT_WIDTH, PAT_HEIGHT/4.0);
90     cairo_stroke (cr);
91
92     cairo_move_to (cr, PAT_WIDTH/4.0,         0);
93     cairo_line_to (cr, PAT_WIDTH/4.0, PAT_WIDTH);
94     cairo_stroke (cr);
95
96     pattern = cairo_pattern_create_for_surface (cairo_get_target (cr));
97     cairo_destroy (cr);
98
99     return pattern;
100 }
101
102 static cairo_test_status_t
103 draw (cairo_t *cr, cairo_extend_t extend)
104 {
105     cairo_pattern_t *pattern;
106     cairo_matrix_t   mat;
107
108     cairo_translate (cr, PAD, PAD);
109
110     pattern = create_pattern (cr);
111
112     cairo_matrix_init_identity (&mat);
113     cairo_matrix_scale (&mat, 2, 1.5);
114     cairo_matrix_rotate (&mat, 1);
115     cairo_matrix_translate (&mat, -PAT_WIDTH/4.0, -PAT_WIDTH/2.0);
116     cairo_pattern_set_matrix (pattern, &mat);
117     cairo_pattern_set_extend (pattern, extend);
118
119     cairo_set_source (cr, pattern);
120     cairo_paint (cr);
121
122     cairo_pattern_destroy (pattern);
123
124     return CAIRO_TEST_SUCCESS;
125 }
126
127 static cairo_test_status_t
128 none (cairo_t *cr, int width, int height)
129 {
130     return draw (cr, CAIRO_EXTEND_NONE);
131 }
132
133 static cairo_test_status_t
134 repeat (cairo_t *cr, int width, int height)
135 {
136     return draw (cr, CAIRO_EXTEND_REPEAT);
137 }
138
139 static cairo_test_status_t
140 reflect (cairo_t *cr, int width, int height)
141 {
142     return draw (cr, CAIRO_EXTEND_REFLECT);
143 }
144
145 static cairo_test_status_t
146 pad (cairo_t *cr, int width, int height)
147 {
148     return draw (cr, CAIRO_EXTEND_PAD);
149 }
150
151 CAIRO_TEST (recording_surface_extend_none,
152             "Paint recording surface pattern with extend modes",
153             "recording, extend", /* keywords */
154             NULL, /* requirements */
155             WIDTH, HEIGHT,
156             NULL, none)
157 CAIRO_TEST (recording_surface_extend_repeat,
158             "Paint recording surface pattern with extend modes",
159             "recording, extend", /* keywords */
160             NULL, /* requirements */
161             WIDTH, HEIGHT,
162             NULL, repeat)
163 CAIRO_TEST (recording_surface_extend_reflect,
164             "Paint recording surface pattern with extend modes",
165             "recording, extend", /* keywords */
166             NULL, /* requirements */
167             WIDTH, HEIGHT,
168             NULL, reflect)
169 CAIRO_TEST (recording_surface_extend_pad,
170             "Paint recording surface pattern with extend modes",
171             "recording, extend", /* keywords */
172             NULL, /* requirements */
173             WIDTH, HEIGHT,
174             NULL, pad)