2 * Copyright © 2010 Red Hat, Inc.
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:
12 * The above copyright notice and this permission notice shall be
13 * included in all copies or substantial portions of the Software.
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
24 * Author: Benjamin Otte <otte@gnome.org>
27 #include "cairo-test.h"
33 paint_with_alpha (cairo_t *cr)
35 cairo_paint_with_alpha (cr, 0.0);
39 mask_with_solid (cairo_t *cr)
41 cairo_pattern_t *pattern = cairo_pattern_create_rgba (1, 0, 0, 0);
43 cairo_mask (cr, pattern);
45 cairo_pattern_destroy (pattern);
49 mask_with_empty_gradient (cairo_t *cr)
51 cairo_pattern_t *pattern = cairo_pattern_create_linear (1, 2, 3, 4);
53 cairo_mask (cr, pattern);
55 cairo_pattern_destroy (pattern);
59 mask_with_gradient (cairo_t *cr)
61 cairo_pattern_t *pattern = cairo_pattern_create_radial (1, 2, 3, 4, 5, 6);
63 cairo_pattern_add_color_stop_rgba (pattern, 0, 1, 0, 0, 0);
64 cairo_pattern_add_color_stop_rgba (pattern, 0, 0, 0, 1, 0);
66 cairo_mask (cr, pattern);
68 cairo_pattern_destroy (pattern);
72 mask_with_surface (cairo_t *cr)
74 cairo_surface_t *surface = cairo_surface_create_similar (cairo_get_target (cr),
75 CAIRO_CONTENT_COLOR_ALPHA,
79 cairo_mask_surface (cr, surface, 0, 0);
81 cairo_surface_destroy (surface);
85 mask_with_alpha_surface (cairo_t *cr)
87 cairo_surface_t *surface = cairo_surface_create_similar (cairo_get_target (cr),
91 cairo_pattern_t *pattern = cairo_pattern_create_for_surface (surface);
92 cairo_pattern_set_extend (pattern, CAIRO_EXTEND_REFLECT);
94 cairo_mask (cr, pattern);
96 cairo_pattern_destroy (pattern);
97 cairo_surface_destroy (surface);
101 mask_with_nonclear_surface (cairo_t *cr)
103 static unsigned char data[8 * 4] = { 0, };
104 cairo_surface_t *surface = cairo_image_surface_create_for_data (data,
108 cairo_mask_surface (cr, surface, 0, 0);
110 cairo_surface_destroy (surface);
114 mask_with_0x0_surface (cairo_t *cr)
116 cairo_surface_t *surface = cairo_surface_create_similar (cairo_get_target (cr),
117 CAIRO_CONTENT_COLOR_ALPHA,
120 cairo_mask_surface (cr, surface, 0, 0);
122 cairo_surface_destroy (surface);
126 mask_with_extend_none (cairo_t *cr)
128 cairo_surface_t *surface = cairo_surface_create_similar (cairo_get_target (cr),
129 CAIRO_CONTENT_COLOR_ALPHA,
133 cairo_mask_surface (cr, surface, 2 * RECT, 2 * RECT);
135 cairo_surface_destroy (surface);
138 typedef void (* mask_func_t) (cairo_t *);
140 static mask_func_t mask_funcs[] = {
143 mask_with_empty_gradient,
146 mask_with_alpha_surface,
147 mask_with_nonclear_surface,
148 mask_with_0x0_surface,
149 mask_with_extend_none
152 static cairo_operator_t operators[] = {
153 CAIRO_OPERATOR_CLEAR,
154 CAIRO_OPERATOR_SOURCE,
157 CAIRO_OPERATOR_DEST_ATOP,
158 CAIRO_OPERATOR_SATURATE,
159 CAIRO_OPERATOR_MULTIPLY
162 static cairo_test_status_t
163 draw (cairo_t *cr, int width, int height)
167 /* 565-compatible gray background */
168 cairo_set_source_rgb (cr, 0.51613, 0.55555, 0.51613);
171 cairo_set_source_rgb (cr, 0.0, 1.0, 0.0); /* green */
172 /* mask with zero-alpha in several ways */
174 cairo_translate (cr, SPACE, SPACE);
176 for (op = 0; op < ARRAY_LENGTH (operators); op++) {
177 cairo_set_operator (cr, operators[op]);
179 for (i = 0; i < ARRAY_LENGTH (mask_funcs); i++) {
181 cairo_translate (cr, i * (RECT + SPACE), op * (RECT + SPACE));
182 cairo_rectangle (cr, 0, 0, RECT, RECT);
189 return CAIRO_TEST_SUCCESS;
192 CAIRO_TEST (zero_mask,
193 "Testing that masking with zero alpha works",
194 "alpha, mask", /* keywords */
195 NULL, /* requirements */
196 SPACE + (RECT + SPACE) * ARRAY_LENGTH (mask_funcs),
197 SPACE + (RECT + SPACE) * ARRAY_LENGTH (operators),