1
/* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2
/* cairo - a vector graphics library with display and print output
3
 *
4
 * Copyright 2002 University of Southern California
5
 * Copyright 2005 Red Hat, Inc.
6
 * Copyright 2007 Emmanuel Pacaud
7
 * Copyright 2008 Benjamin Otte
8
 * Copyright 2008 Chris Wilson
9
 *
10
 * This library is free software; you can redistribute it and/or
11
 * modify it either under the terms of the GNU Lesser General Public
12
 * License version 2.1 as published by the Free Software Foundation
13
 * (the "LGPL") or, at your option, under the terms of the Mozilla
14
 * Public License Version 1.1 (the "MPL"). If you do not alter this
15
 * notice, a recipient may use your version of this file under either
16
 * the MPL or the LGPL.
17
 *
18
 * You should have received a copy of the LGPL along with this library
19
 * in the file COPYING-LGPL-2.1; if not, write to the Free Software
20
 * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
21
 * You should have received a copy of the MPL along with this library
22
 * in the file COPYING-MPL-1.1
23
 *
24
 * The contents of this file are subject to the Mozilla Public License
25
 * Version 1.1 (the "License"); you may not use this file except in
26
 * compliance with the License. You may obtain a copy of the License at
27
 * http://www.mozilla.org/MPL/
28
 *
29
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
30
 * OF ANY KIND, either express or implied. See the LGPL or the MPL for
31
 * the specific language governing rights and limitations.
32
 *
33
 * The Original Code is the cairo graphics library.
34
 *
35
 * The Initial Developer of the Original Code is University of Southern
36
 * California.
37
 *
38
 * Contributor(s):
39
 *      Owen Taylor <otaylor@redhat.com>
40
 *      Kristian Høgsberg <krh@redhat.com>
41
 *      Emmanuel Pacaud <emmanuel.pacaud@lapp.in2p3.fr>
42
 *      Chris Wilson <chris@chris-wilson.co.uk>
43
 *      Andrea Canciani <ranma42@gmail.com>
44
 */
45

            
46
#include "cairo-test.h"
47

            
48
#define STEPS 16
49
#define START_OPERATOR	CAIRO_OPERATOR_CLEAR
50
#define STOP_OPERATOR	CAIRO_OPERATOR_HSL_LUMINOSITY
51

            
52
#define SIZE 3
53
#define COUNT 6
54
#define FULL_WIDTH  ((STEPS + 1) * COUNT - 1)
55
#define FULL_HEIGHT ((COUNT + STOP_OPERATOR - START_OPERATOR) / COUNT) * (STEPS + 1)
56

            
57
static void
58
3
create_patterns (cairo_t *bg, cairo_t *fg)
59
{
60
    int x;
61

            
62
51
    for (x = 0; x < STEPS; x++) {
63
48
	double i = (double) x / (STEPS - 1);
64
48
	cairo_set_source_rgba (bg, 0, 0, 0, i);
65
48
	cairo_rectangle (bg, x, 0, 1, STEPS);
66
48
	cairo_fill (bg);
67

            
68
48
	cairo_set_source_rgba (fg, 0, 0, 0, i);
69
48
	cairo_rectangle (fg, 0, x, STEPS, 1);
70
48
	cairo_fill (fg);
71
    }
72
3
}
73

            
74
/* expects a STEP*STEP pixel rectangle */
75
static void
76
87
do_composite (cairo_t *cr, cairo_operator_t op, cairo_surface_t *bg, cairo_surface_t *fg)
77
{
78
87
    cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
79
87
    cairo_set_source_surface (cr, bg, 0, 0);
80
87
    cairo_paint (cr);
81

            
82
87
    cairo_set_operator (cr, op);
83
87
    cairo_set_source_surface (cr, fg, 0, 0);
84
87
    cairo_paint (cr);
85
87
}
86

            
87
static void
88
3
subdraw (cairo_t *cr, int width, int height)
89
{
90
3
    size_t i = 0;
91
    cairo_operator_t op;
92
    cairo_t *bgcr, *fgcr;
93
    cairo_surface_t *bg, *fg;
94

            
95
3
    bg = cairo_surface_create_similar (cairo_get_target (cr),
96
	    CAIRO_CONTENT_ALPHA, SIZE * STEPS, SIZE * STEPS);
97
3
    fg = cairo_surface_create_similar (cairo_get_target (cr),
98
	    CAIRO_CONTENT_ALPHA, SIZE * STEPS, SIZE * STEPS);
99
3
    bgcr = cairo_create (bg);
100
3
    fgcr = cairo_create (fg);
101
3
    cairo_scale (bgcr, SIZE, SIZE);
102
3
    cairo_scale (fgcr, SIZE, SIZE);
103
3
    create_patterns (bgcr, fgcr);
104
3
    cairo_destroy (bgcr);
105
3
    cairo_destroy (fgcr);
106

            
107
90
    for (op = START_OPERATOR; op <= STOP_OPERATOR; op++, i++) {
108
87
	cairo_save (cr);
109
87
	cairo_translate (cr,
110
87
		SIZE * (STEPS + 1) * (i % COUNT),
111
87
		SIZE * (STEPS + 1) * (i / COUNT));
112
87
	cairo_rectangle (cr, 0, 0, SIZE * (STEPS + 1), SIZE * (STEPS+1));
113
87
	cairo_clip (cr);
114
87
	do_composite (cr, op, bg, fg);
115
87
	cairo_restore (cr);
116
    }
117

            
118
3
    cairo_surface_destroy (fg);
119
3
    cairo_surface_destroy (bg);
120
3
}
121

            
122

            
123
static cairo_surface_t *
124
3
create_source (cairo_surface_t *target, int width, int height)
125
{
126
    cairo_surface_t *similar;
127
    cairo_t *cr;
128

            
129
3
    similar = cairo_surface_create_similar (target,
130
					    CAIRO_CONTENT_ALPHA,
131
					    width, height);
132
3
    cr = cairo_create (similar);
133
3
    cairo_surface_destroy (similar);
134

            
135
3
    subdraw (cr, width, height);
136

            
137
3
    similar = cairo_surface_reference (cairo_get_target (cr));
138
3
    cairo_destroy (cr);
139

            
140
3
    return similar;
141
}
142

            
143
static cairo_test_status_t
144
3
draw (cairo_t *cr, int width, int height)
145
{
146
    cairo_surface_t *source;
147

            
148
3
    cairo_set_source_rgb (cr, 1, 1, 1);
149
3
    cairo_paint (cr);
150

            
151
3
    source = create_source (cairo_get_target (cr), width, height);
152
3
    cairo_set_source_surface (cr, source, 0, 0);
153
3
    cairo_surface_destroy (source);
154

            
155
3
    cairo_paint (cr);
156

            
157
3
    return CAIRO_TEST_SUCCESS;
158
}
159

            
160
1
CAIRO_TEST (operator_alpha_alpha,
161
	    "Tests result of compositing pure-alpha surfaces"
162
	    "\nCompositing of pure-alpha sources is inconsistent across backends.",
163
	    "alpha, similar, operator", /* keywords */
164
	    NULL, /* requirements */
165
	    FULL_WIDTH * SIZE, FULL_HEIGHT * SIZE,
166
	    NULL, draw)