Tizen 2.0 Release
[framework/graphics/cairo.git] / src / test-compositor-surface.c
1 /* cairo - a vector graphics library with display and print output
2  *
3  * Copyright © 2011 Intel Corporation
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it either under the terms of the GNU Lesser General Public
7  * License version 2.1 as published by the Free Software Foundation
8  * (the "LGPL") or, at your option, under the terms of the Mozilla
9  * Public License Version 1.1 (the "MPL"). If you do not alter this
10  * notice, a recipient may use your version of this file under either
11  * the MPL or the LGPL.
12  *
13  * You should have received a copy of the LGPL along with this library
14  * in the file COPYING-LGPL-2.1; if not, write to the Free Software
15  * Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA
16  * You should have received a copy of the MPL along with this library
17  * in the file COPYING-MPL-1.1
18  *
19  * The contents of this file are subject to the Mozilla Public License
20  * Version 1.1 (the "License"); you may not use this file except in
21  * compliance with the License. You may obtain a copy of the License at
22  * http://www.mozilla.org/MPL/
23  *
24  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY
25  * OF ANY KIND, either express or implied. See the LGPL or the MPL for
26  * the specific language governing rights and limitations.
27  *
28  * The Original Code is the cairo graphics library.
29  *
30  * The Initial Developer of the Original Code is Intel Corporation
31  *
32  * Contributor(s):
33  *      Chris Wilson <chris@chris-wilson.co.uk>
34  */
35
36 #include "cairoint.h"
37
38 #include "test-compositor-surface-private.h"
39
40 #include "cairo-compositor-private.h"
41 #include "cairo-default-context-private.h"
42 #include "cairo-error-private.h"
43 #include "cairo-image-surface-private.h"
44 #include "cairo-surface-backend-private.h"
45
46 typedef struct _test_compositor_surface {
47     cairo_image_surface_t base;
48 } test_compositor_surface_t;
49
50 static const cairo_surface_backend_t test_compositor_surface_backend;
51
52 cairo_surface_t *
53 test_compositor_surface_create (const cairo_compositor_t *compositor,
54                                 cairo_content_t content,
55                                 int             width,
56                                 int             height)
57 {
58     test_compositor_surface_t *surface;
59     pixman_image_t *pixman_image;
60     pixman_format_code_t pixman_format;
61
62     switch (content) {
63     case CAIRO_CONTENT_ALPHA:
64         pixman_format = PIXMAN_a8;
65         break;
66     case CAIRO_CONTENT_COLOR:
67         pixman_format = PIXMAN_x8r8g8b8;
68         break;
69     case CAIRO_CONTENT_COLOR_ALPHA:
70         pixman_format = PIXMAN_a8r8g8b8;
71         break;
72     default:
73         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_INVALID_CONTENT));
74     }
75
76     pixman_image = pixman_image_create_bits (pixman_format, width, height,
77                                              NULL, 0);
78     if (unlikely (pixman_image == NULL))
79         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
80
81     surface = malloc (sizeof (test_compositor_surface_t));
82     if (unlikely (surface == NULL)) {
83         pixman_image_unref (pixman_image);
84         return _cairo_surface_create_in_error (_cairo_error (CAIRO_STATUS_NO_MEMORY));
85     }
86
87     _cairo_surface_init (&surface->base.base,
88                          &test_compositor_surface_backend,
89                          NULL, /* device */
90                          content);
91     _cairo_image_surface_init (&surface->base, pixman_image, pixman_format);
92
93     surface->base.compositor = compositor;
94
95     return &surface->base.base;
96 }
97
98 static cairo_surface_t *
99 test_compositor_surface_create_similar (void            *abstract_surface,
100                                         cairo_content_t  content,
101                                         int              width,
102                                         int              height)
103 {
104     test_compositor_surface_t *surface = abstract_surface;
105
106     return test_compositor_surface_create (surface->base.compositor,
107                                            content, width, height);
108 }
109
110 static cairo_int_status_t
111 test_compositor_surface_paint (void                     *_surface,
112                                cairo_operator_t          op,
113                                const cairo_pattern_t    *source,
114                                const cairo_clip_t       *clip)
115 {
116     test_compositor_surface_t *surface = _surface;
117     return _cairo_compositor_paint (surface->base.compositor,
118                                     _surface, op, source,
119                                     clip);
120 }
121
122 static cairo_int_status_t
123 test_compositor_surface_mask (void                      *_surface,
124                               cairo_operator_t           op,
125                               const cairo_pattern_t     *source,
126                               const cairo_pattern_t     *mask,
127                               const cairo_clip_t        *clip)
128 {
129     test_compositor_surface_t *surface = _surface;
130     return _cairo_compositor_mask (surface->base.compositor,
131                                    _surface, op, source, mask,
132                                     clip);
133 }
134
135 static cairo_int_status_t
136 test_compositor_surface_stroke (void                            *_surface,
137                                 cairo_operator_t                 op,
138                                 const cairo_pattern_t           *source,
139                                 const cairo_path_fixed_t        *path,
140                                 const cairo_stroke_style_t      *style,
141                                 const cairo_matrix_t            *ctm,
142                                 const cairo_matrix_t            *ctm_inverse,
143                                 double                           tolerance,
144                                 cairo_antialias_t                antialias,
145                                 const cairo_clip_t              *clip)
146 {
147     test_compositor_surface_t *surface = _surface;
148     return _cairo_compositor_stroke (surface->base.compositor,
149                                      _surface, op, source,
150                                      path, style, ctm, ctm_inverse,
151                                      tolerance, antialias,
152                                      clip);
153 }
154
155 static cairo_int_status_t
156 test_compositor_surface_fill (void                      *_surface,
157                               cairo_operator_t           op,
158                               const cairo_pattern_t     *source,
159                               const cairo_path_fixed_t  *path,
160                               cairo_fill_rule_t          fill_rule,
161                               double                     tolerance,
162                               cairo_antialias_t          antialias,
163                               const cairo_clip_t        *clip)
164 {
165     test_compositor_surface_t *surface = _surface;
166     return _cairo_compositor_fill (surface->base.compositor,
167                                    _surface, op, source,
168                                    path, fill_rule, tolerance, antialias,
169                                    clip);
170 }
171
172 static cairo_int_status_t
173 test_compositor_surface_glyphs (void                    *_surface,
174                                 cairo_operator_t         op,
175                                 const cairo_pattern_t   *source,
176                                 cairo_glyph_t           *glyphs,
177                                 int                      num_glyphs,
178                                 cairo_scaled_font_t     *scaled_font,
179                                 const cairo_clip_t      *clip)
180 {
181     test_compositor_surface_t *surface = _surface;
182     return _cairo_compositor_glyphs (surface->base.compositor,
183                                      _surface, op, source,
184                                      glyphs, num_glyphs, scaled_font,
185                                      clip);
186 }
187
188 static const cairo_surface_backend_t test_compositor_surface_backend = {
189     CAIRO_SURFACE_TYPE_IMAGE,
190     _cairo_image_surface_finish,
191     _cairo_default_context_create,
192
193     test_compositor_surface_create_similar,
194     NULL, /* create similar image */
195     _cairo_image_surface_map_to_image,
196     _cairo_image_surface_unmap_image,
197
198     _cairo_image_surface_source,
199     _cairo_image_surface_acquire_source_image,
200     _cairo_image_surface_release_source_image,
201     _cairo_image_surface_snapshot,
202
203     NULL, /* copy_page */
204     NULL, /* show_page */
205
206     _cairo_image_surface_get_extents,
207     _cairo_image_surface_get_font_options,
208
209     NULL, /* flush */
210     NULL, /* mark_dirty_rectangle */
211
212     test_compositor_surface_paint,
213     test_compositor_surface_mask,
214     test_compositor_surface_stroke,
215     test_compositor_surface_fill,
216     NULL, /* fill/stroke */
217     test_compositor_surface_glyphs,
218 };
219
220 static const cairo_compositor_t *
221 get_fallback_compositor (void)
222 {
223     return &_cairo_fallback_compositor;
224 }
225
226 cairo_surface_t *
227 _cairo_test_fallback_compositor_surface_create (cairo_content_t content,
228                                                 int             width,
229                                                 int             height)
230 {
231     return test_compositor_surface_create (get_fallback_compositor(),
232                                            content, width, height);
233 }
234
235 cairo_surface_t *
236 _cairo_test_mask_compositor_surface_create (cairo_content_t     content,
237                                                 int             width,
238                                                 int             height)
239 {
240     return test_compositor_surface_create (_cairo_image_mask_compositor_get(),
241                                            content, width, height);
242 }
243
244 cairo_surface_t *
245 _cairo_test_traps_compositor_surface_create (cairo_content_t    content,
246                                              int                width,
247                                              int                height)
248 {
249     return test_compositor_surface_create (_cairo_image_traps_compositor_get(),
250                                            content, width, height);
251 }
252
253 cairo_surface_t *
254 _cairo_test_spans_compositor_surface_create (cairo_content_t    content,
255                                              int                width,
256                                              int                height)
257 {
258     return test_compositor_surface_create (_cairo_image_spans_compositor_get(),
259                                            content, width, height);
260 }