Tizen 2.0 Release
[framework/graphics/cairo.git] / test / operator-alpha-alpha.c
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 create_patterns (cairo_t *bg, cairo_t *fg)
59 {
60     int x;
61
62     for (x = 0; x < STEPS; x++) {
63         double i = (double) x / (STEPS - 1);
64         cairo_set_source_rgba (bg, 0, 0, 0, i);
65         cairo_rectangle (bg, x, 0, 1, STEPS);
66         cairo_fill (bg);
67
68         cairo_set_source_rgba (fg, 0, 0, 0, i);
69         cairo_rectangle (fg, 0, x, STEPS, 1);
70         cairo_fill (fg);
71     }
72 }
73
74 /* expects a STEP*STEP pixel rectangle */
75 static void
76 do_composite (cairo_t *cr, cairo_operator_t op, cairo_surface_t *bg, cairo_surface_t *fg)
77 {
78     cairo_set_operator (cr, CAIRO_OPERATOR_SOURCE);
79     cairo_set_source_surface (cr, bg, 0, 0);
80     cairo_paint (cr);
81
82     cairo_set_operator (cr, op);
83     cairo_set_source_surface (cr, fg, 0, 0);
84     cairo_paint (cr);
85 }
86
87 static void
88 subdraw (cairo_t *cr, int width, int height)
89 {
90     size_t i = 0;
91     cairo_operator_t op;
92     cairo_t *bgcr, *fgcr;
93     cairo_surface_t *bg, *fg;
94
95     bg = cairo_surface_create_similar (cairo_get_target (cr),
96             CAIRO_CONTENT_ALPHA, SIZE * STEPS, SIZE * STEPS);
97     fg = cairo_surface_create_similar (cairo_get_target (cr),
98             CAIRO_CONTENT_ALPHA, SIZE * STEPS, SIZE * STEPS);
99     bgcr = cairo_create (bg);
100     fgcr = cairo_create (fg);
101     cairo_scale (bgcr, SIZE, SIZE);
102     cairo_scale (fgcr, SIZE, SIZE);
103     create_patterns (bgcr, fgcr);
104     cairo_destroy (bgcr);
105     cairo_destroy (fgcr);
106
107     for (op = START_OPERATOR; op <= STOP_OPERATOR; op++, i++) {
108         cairo_save (cr);
109         cairo_translate (cr,
110                 SIZE * (STEPS + 1) * (i % COUNT),
111                 SIZE * (STEPS + 1) * (i / COUNT));
112         cairo_rectangle (cr, 0, 0, SIZE * (STEPS + 1), SIZE * (STEPS+1));
113         cairo_clip (cr);
114         do_composite (cr, op, bg, fg);
115         cairo_restore (cr);
116     }
117
118     cairo_surface_destroy (fg);
119     cairo_surface_destroy (bg);
120 }
121
122
123 static cairo_surface_t *
124 create_source (cairo_surface_t *target, int width, int height)
125 {
126     cairo_surface_t *similar;
127     cairo_t *cr;
128
129     similar = cairo_surface_create_similar (target,
130                                             CAIRO_CONTENT_ALPHA,
131                                             width, height);
132     cr = cairo_create (similar);
133     cairo_surface_destroy (similar);
134
135     subdraw (cr, width, height);
136
137     similar = cairo_surface_reference (cairo_get_target (cr));
138     cairo_destroy (cr);
139
140     return similar;
141 }
142
143 static cairo_test_status_t
144 draw (cairo_t *cr, int width, int height)
145 {
146     cairo_surface_t *source;
147
148     cairo_set_source_rgb (cr, 1, 1, 1);
149     cairo_paint (cr);
150
151     source = create_source (cairo_get_target (cr), width, height);
152     cairo_set_source_surface (cr, source, 0, 0);
153     cairo_surface_destroy (source);
154
155     cairo_paint (cr);
156
157     return CAIRO_TEST_SUCCESS;
158 }
159
160 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)