Upload Tizen2.0 source
[framework/graphics/cairo.git] / test / clip-group-shapes.c
1 /*
2  * Copyright (c) 2010 M Joonas Pihlaja
3  *
4  * Permission is hereby granted, free of charge, to any person
5  * obtaining a copy of this software and associated documentation
6  * files (the "Software"), to deal in the Software without
7  * restriction, including without limitation the rights to use, copy,
8  * modify, merge, publish, distribute, sublicense, and/or sell copies
9  * of the Software, and to permit persons to whom the Software is
10  * furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  *
24  * Author: M Joonas Pihlaja <jpihlaja@cc.helsinki.fi>
25  */
26 #include "cairo-test.h"
27
28 /* Tests specific clipping fast paths and their interaction with
29  * groups:  It shouldn't matter if the clip is set before or after
30  * pushing a group.
31  *
32  * There's some overlap with the following tests, but they test for
33  * different things:
34  *
35  *  group-clip.c (tests preserving paths), clipped-group.c (tests
36  *  clipping the same thing different ways), clip-push-group (tests
37  *  for a specific bug).
38  */
39
40 #define GENERATE_REF 0
41
42 /* For determining whether we establish the clip path before or after
43  * pushing a group. */
44 enum {
45     CLIP_OUTSIDE_GROUP,
46     CLIP_INSIDE_GROUP
47 };
48
49 typedef void (*clipper_t)(cairo_t *cr, int w, int h);
50
51 static cairo_test_status_t
52 clip_and_paint (cairo_t *cr,
53                 int w, int h,
54                 clipper_t do_clip,
55                 int clip_where)
56 {
57     cairo_save (cr); {
58         if (GENERATE_REF) {
59             do_clip (cr, w, h);
60             cairo_paint (cr);
61         } else {
62             if (clip_where == CLIP_OUTSIDE_GROUP)
63                 do_clip (cr, w, h);
64             cairo_push_group (cr); {
65                 if (clip_where == CLIP_INSIDE_GROUP)
66                     do_clip (cr, w, h);
67                 cairo_paint (cr);
68             }
69             cairo_pop_group_to_source (cr);
70             if (clip_where == CLIP_OUTSIDE_GROUP)
71                 cairo_reset_clip (cr);
72             cairo_paint (cr);
73         }
74     }
75     cairo_restore (cr);
76     return CAIRO_TEST_SUCCESS;
77 }
78
79 static cairo_test_status_t
80 run_clip_test (cairo_t *cr, int w, int h, clipper_t do_clip)
81 {
82     cairo_set_source_rgb (cr, 1,1,1);
83     cairo_paint (cr);
84     cairo_set_source_rgb (cr, 1,0,0);
85
86     /* Left. */
87     clip_and_paint (cr, w/2, h, do_clip, CLIP_OUTSIDE_GROUP);
88
89     /* Right */
90     cairo_translate(cr, w/2, 0);
91     clip_and_paint (cr, w/2, h, do_clip, CLIP_INSIDE_GROUP);
92
93     return CAIRO_TEST_SUCCESS;
94 }
95
96 static void
97 clip_aligned_rectangles (cairo_t *cr, int w, int h)
98 {
99     int x1 = 0.2 * w;
100     int y1 = 0.2 * h;
101     int x2 = 0.8 * w;
102     int y2 = 0.8 * h;
103
104     cairo_rectangle (cr, x1, y1, w, h);
105     cairo_clip (cr);
106
107     cairo_rectangle (cr, x2, y2, -w, -h);
108     cairo_clip (cr);
109 }
110
111 static void
112 clip_unaligned_rectangles (cairo_t *cr, int w, int h)
113 {
114     /* This clip stresses the antialiased edges produced by an
115      * unaligned rectangular clip. The edges should be produced by
116      * compositing red on white with alpha = 0.5 on the sides, and with
117      * alpha = 0.25 in the corners. */
118     int x1 = 0.2 * w;
119     int y1 = 0.2 * h;
120     int x2 = 0.8 * w;
121     int y2 = 0.8 * h;
122
123     cairo_rectangle (cr, x1+0.5, y1+0.5, w, h);
124     cairo_clip (cr);
125
126     cairo_rectangle (cr, x2+0.5, y2+0.5, -w, -h);
127     w = x2 - x1;
128     h = y2 - y1;
129     cairo_rectangle (cr, x2, y1+1, -w+1, h-1);
130     cairo_clip (cr);
131 }
132
133 static void
134 clip_circles (cairo_t *cr, int w, int h)
135 {
136     int x1 = 0.5 * w;
137     int y1 = 0.5 * h;
138     int x2 = 0.75 * w;
139     int y2 = 0.75 * h;
140     int r = 0.4*MIN(w,h);
141
142     cairo_arc (cr, x1, y1, r, 0, 6.28);
143     cairo_close_path (cr);
144     cairo_clip (cr);
145
146     cairo_arc (cr, x2, y2, r, 0, 6.28);
147     cairo_close_path (cr);
148     cairo_clip (cr);
149 }
150
151 static cairo_test_status_t
152 draw_aligned_rectangles (cairo_t *cr, int width, int height)
153 {
154     return run_clip_test (cr, width, height, clip_aligned_rectangles);
155 }
156
157 static cairo_test_status_t
158 draw_unaligned_rectangles (cairo_t *cr, int width, int height)
159 {
160     return run_clip_test (cr, width, height, clip_unaligned_rectangles);
161 }
162
163 static cairo_test_status_t
164 draw_circles (cairo_t *cr, int width, int height)
165 {
166     return run_clip_test (cr, width, height, clip_circles);
167 }
168
169 CAIRO_TEST (clip_group_shapes_aligned_rectangles,
170             "Test clip and group interaction with aligned rectangle clips",
171             "clip", /* keywords */
172             NULL, /* requirements */
173             200, 100,
174             NULL, draw_aligned_rectangles)
175
176 CAIRO_TEST (clip_group_shapes_unaligned_rectangles,
177             "Test clip and group interaction with unaligned rectangle clips",
178             "clip", /* keywords */
179             "target=raster", /* requirements */
180             200, 100,
181             NULL, draw_unaligned_rectangles)
182
183 CAIRO_TEST (clip_group_shapes_circles,
184             "Test clip and group interaction with circular clips",
185             "clip", /* keywords */
186             NULL, /* requirements */
187             200, 100,
188             NULL, draw_circles)