Upload Tizen2.0 source
[framework/graphics/cairo.git] / test / record-mesh.c
1 /*
2  * Copyright © 2007 Red Hat, Inc.
3  * Copyright © 2009 Adrian Johnson
4  * Copyright © 2011 Intel Corporation
5  *
6  * Permission is hereby granted, free of charge, to any person
7  * obtaining a copy of this software and associated documentation
8  * files (the "Software"), to deal in the Software without
9  * restriction, including without limitation the rights to use, copy,
10  * modify, merge, publish, distribute, sublicense, and/or sell copies
11  * of the Software, and to permit persons to whom the Software is
12  * furnished to do so, subject to the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24  * SOFTWARE.
25  *
26  * Authors:
27  *      Behdad Esfahbod <behdad@behdad.org>
28  *      Adrian Johnson <ajohnson@redneon.com>
29  *      Chris Wilson <chris@chris-wilson.co.uk>
30  */
31
32 #include <math.h>
33 #include "cairo-test.h"
34 #include <stdio.h>
35
36 #define PAT_WIDTH  170
37 #define PAT_HEIGHT 170
38 #define SIZE PAT_WIDTH
39 #define PAD 2
40 #define WIDTH (PAD + SIZE + PAD)
41 #define HEIGHT WIDTH
42
43 /* This test is designed to paint a mesh pattern. The mesh contains
44  * two overlapping patches */
45
46 static cairo_pattern_t *
47 mesh (void)
48 {
49     cairo_pattern_t *pattern;
50
51     pattern = cairo_pattern_create_mesh ();
52
53     cairo_mesh_pattern_begin_patch (pattern);
54
55     cairo_mesh_pattern_move_to (pattern, 0, 0);
56     cairo_mesh_pattern_curve_to (pattern, 30, -30,  60,  30, 100, 0);
57     cairo_mesh_pattern_curve_to (pattern, 60,  30, 130,  60, 100, 100);
58     cairo_mesh_pattern_curve_to (pattern, 60,  70,  30, 130,   0, 100);
59     cairo_mesh_pattern_curve_to (pattern, 30,  70, -30,  30,   0, 0);
60
61     cairo_mesh_pattern_set_corner_color_rgb (pattern, 0, 1, 0, 0);
62     cairo_mesh_pattern_set_corner_color_rgb (pattern, 1, 0, 1, 0);
63     cairo_mesh_pattern_set_corner_color_rgb (pattern, 2, 0, 0, 1);
64     cairo_mesh_pattern_set_corner_color_rgb (pattern, 3, 1, 1, 0);
65
66     cairo_mesh_pattern_end_patch (pattern);
67
68     cairo_mesh_pattern_begin_patch (pattern);
69
70     cairo_mesh_pattern_move_to (pattern, 50, 50);
71     cairo_mesh_pattern_curve_to (pattern,  80,  20, 110,  80, 150, 50);
72     cairo_mesh_pattern_curve_to (pattern, 110,  80, 180, 110, 150, 150);
73     cairo_mesh_pattern_curve_to (pattern, 110, 120,  80, 180,  50, 150);
74     cairo_mesh_pattern_curve_to (pattern,  80, 120,  20,  80,  50, 50);
75
76     cairo_mesh_pattern_set_corner_color_rgba (pattern, 0, 1, 0, 0, 0.3);
77     cairo_mesh_pattern_set_corner_color_rgb  (pattern, 1, 0, 1, 0);
78     cairo_mesh_pattern_set_corner_color_rgba (pattern, 2, 0, 0, 1, 0.3);
79     cairo_mesh_pattern_set_corner_color_rgb  (pattern, 3, 1, 1, 0);
80
81     cairo_mesh_pattern_end_patch (pattern);
82
83     return pattern;
84 }
85
86 static cairo_t *
87 draw (cairo_t *cr)
88 {
89     cairo_pattern_t *source;
90
91     cairo_set_source_rgb (cr, 0, 1, 1);
92     cairo_paint (cr);
93
94     source = mesh ();
95     cairo_set_source (cr, source);
96     cairo_pattern_destroy (source);
97
98     cairo_rectangle (cr, 10, 10, SIZE-20, SIZE-20);
99     cairo_clip (cr);
100     cairo_paint (cr);
101
102     return cr;
103 }
104
105 static cairo_t *
106 record_create (cairo_t *target)
107 {
108     cairo_surface_t *surface;
109     cairo_t *cr;
110
111     surface = cairo_recording_surface_create (cairo_surface_get_content (cairo_get_target (target)), NULL);
112     cr = cairo_create (surface);
113     cairo_surface_destroy (surface);
114
115     return cr;
116 }
117
118 static cairo_surface_t *
119 record_get (cairo_t *target)
120 {
121     cairo_surface_t *surface;
122
123     surface = cairo_surface_reference (cairo_get_target (target));
124     cairo_destroy (target);
125
126     return surface;
127 }
128
129 static cairo_test_status_t
130 record_replay (cairo_t *cr, cairo_t *(*func)(cairo_t *), int width, int height)
131 {
132     cairo_surface_t *surface;
133     int x, y;
134
135     surface = record_get (func (record_create (cr)));
136
137     cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
138     cairo_set_source_surface (cr, surface, 0, 0);
139     cairo_surface_destroy (surface);
140     cairo_pattern_set_extend (cairo_get_source (cr), CAIRO_EXTEND_NONE);
141
142     for (y = 0; y < height; y += 2) {
143         for (x = 0; x < width; x += 2) {
144             cairo_rectangle (cr, x, y, 2, 2);
145             cairo_clip (cr);
146             cairo_paint (cr);
147             cairo_reset_clip (cr);
148         }
149     }
150
151     return CAIRO_TEST_SUCCESS;
152 }
153
154 static cairo_test_status_t
155 record_mesh (cairo_t *cr, int width, int height)
156 {
157     return record_replay (cr, draw, width, height);
158 }
159
160 CAIRO_TEST (record_mesh,
161             "Paint mesh pattern through a recording surface",
162             "record,mesh,pattern", /* keywords */
163             NULL, /* requirements */
164             WIDTH, HEIGHT,
165             NULL, record_mesh)
166