Update change log.
[platform/upstream/cairo.git] / boilerplate / cairo-boilerplate-test-surfaces.c
1 /* -*- Mode: c; c-basic-offset: 4; indent-tabs-mode: t; tab-width: 8; -*- */
2 /*
3  * Copyright © 2004,2006 Red Hat, Inc.
4  *
5  * Permission to use, copy, modify, distribute, and sell this software
6  * and its documentation for any purpose is hereby granted without
7  * fee, provided that the above copyright notice appear in all copies
8  * and that both that copyright notice and this permission notice
9  * appear in supporting documentation, and that the name of
10  * Red Hat, Inc. not be used in advertising or publicity pertaining to
11  * distribution of the software without specific, written prior
12  * permission. Red Hat, Inc. makes no representations about the
13  * suitability of this software for any purpose.  It is provided "as
14  * is" without express or implied warranty.
15  *
16  * RED HAT, INC. DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS
17  * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
18  * FITNESS, IN NO EVENT SHALL RED HAT, INC. BE LIABLE FOR ANY SPECIAL,
19  * INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
20  * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
21  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR
22  * IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
23  *
24  * Author: Carl D. Worth <cworth@cworth.org>
25  */
26
27 #include "../cairo-version.h"
28
29 #include "cairo-boilerplate-private.h"
30
31 #include <cairo-types-private.h>
32
33 #include <test-compositor-surface.h>
34 #include <test-null-compositor-surface.h>
35 #if CAIRO_HAS_TEST_PAGINATED_SURFACE
36 #include <test-paginated-surface.h>
37 #endif
38
39 static cairo_surface_t *
40 _cairo_boilerplate_test_base_compositor_create_surface (const char                 *name,
41                                                         cairo_content_t     content,
42                                                         double              width,
43                                                         double              height,
44                                                         double              max_width,
45                                                         double              max_height,
46                                                         cairo_boilerplate_mode_t mode,
47                                                         void              **closure)
48 {
49     *closure = NULL;
50     return _cairo_test_base_compositor_surface_create (content, ceil (width), ceil (height));
51 }
52
53
54 static cairo_surface_t *
55 _cairo_boilerplate_test_fallback_compositor_create_surface (const char             *name,
56                                                             cairo_content_t         content,
57                                                             double                  width,
58                                                             double                  height,
59                                                             double                  max_width,
60                                                             double                  max_height,
61                                                             cairo_boilerplate_mode_t mode,
62                                                             void                  **closure)
63 {
64     *closure = NULL;
65     return _cairo_test_fallback_compositor_surface_create (content, ceil (width), ceil (height));
66 }
67
68 static cairo_surface_t *
69 _cairo_boilerplate_test_mask_compositor_create_surface (const char                 *name,
70                                                          cairo_content_t            content,
71                                                          double                     width,
72                                                          double                     height,
73                                                          double                     max_width,
74                                                          double                     max_height,
75                                                          cairo_boilerplate_mode_t   mode,
76                                                          void                     **closure)
77 {
78     *closure = NULL;
79     return _cairo_test_mask_compositor_surface_create (content, ceil (width), ceil (height));
80 }
81
82
83 static cairo_surface_t *
84 _cairo_boilerplate_test_traps_compositor_create_surface (const char                *name,
85                                                          cairo_content_t            content,
86                                                          double                     width,
87                                                          double                     height,
88                                                          double                     max_width,
89                                                          double                     max_height,
90                                                          cairo_boilerplate_mode_t   mode,
91                                                          void                     **closure)
92 {
93     *closure = NULL;
94     return _cairo_test_traps_compositor_surface_create (content, ceil (width), ceil (height));
95 }
96
97 static cairo_surface_t *
98 _cairo_boilerplate_test_spans_compositor_create_surface (const char                *name,
99                                                          cairo_content_t            content,
100                                                          double                     width,
101                                                          double                     height,
102                                                          double                     max_width,
103                                                          double                     max_height,
104                                                          cairo_boilerplate_mode_t   mode,
105                                                          void                     **closure)
106 {
107     *closure = NULL;
108     return _cairo_test_spans_compositor_surface_create (content, ceil (width), ceil (height));
109 }
110
111 static cairo_surface_t *
112 _cairo_boilerplate_test_no_fallback_compositor_create_surface (const char                  *name,
113                                                                cairo_content_t      content,
114                                                                double                       width,
115                                                                double                       height,
116                                                                double                       max_width,
117                                                                double                       max_height,
118                                                                cairo_boilerplate_mode_t   mode,
119                                                                void                       **closure)
120 {
121     if (mode == CAIRO_BOILERPLATE_MODE_TEST)
122         return NULL;
123
124     *closure = NULL;
125     return _cairo_test_no_fallback_compositor_surface_create (content, ceil (width), ceil (height));
126 }
127
128 static cairo_surface_t *
129 _cairo_boilerplate_test_no_traps_compositor_create_surface (const char             *name,
130                                                          cairo_content_t            content,
131                                                          double                     width,
132                                                          double                     height,
133                                                          double                     max_width,
134                                                          double                     max_height,
135                                                          cairo_boilerplate_mode_t   mode,
136                                                          void                     **closure)
137 {
138     if (mode == CAIRO_BOILERPLATE_MODE_TEST)
139         return NULL;
140
141     *closure = NULL;
142     return _cairo_test_no_traps_compositor_surface_create (content, ceil (width), ceil (height));
143 }
144
145 static cairo_surface_t *
146 _cairo_boilerplate_test_no_spans_compositor_create_surface (const char             *name,
147                                                          cairo_content_t            content,
148                                                          double                     width,
149                                                          double                     height,
150                                                          double                     max_width,
151                                                          double                     max_height,
152                                                          cairo_boilerplate_mode_t   mode,
153                                                          void                     **closure)
154 {
155     if (mode == CAIRO_BOILERPLATE_MODE_TEST)
156         return NULL;
157
158     *closure = NULL;
159     return _cairo_test_no_spans_compositor_surface_create (content, ceil (width), ceil (height));
160 }
161
162 #if CAIRO_HAS_TEST_PAGINATED_SURFACE
163 static const cairo_user_data_key_t test_paginated_closure_key;
164
165 typedef struct {
166     cairo_surface_t *target;
167 } test_paginated_closure_t;
168
169 static cairo_surface_t *
170 _cairo_boilerplate_test_paginated_create_surface (const char                *name,
171                                                   cairo_content_t            content,
172                                                   double                     width,
173                                                   double                     height,
174                                                   double                     max_width,
175                                                   double                     max_height,
176                                                   cairo_boilerplate_mode_t   mode,
177                                                   void                     **closure)
178 {
179     test_paginated_closure_t *tpc;
180     cairo_format_t format;
181     cairo_surface_t *surface;
182     cairo_status_t status;
183
184     *closure = tpc = xmalloc (sizeof (test_paginated_closure_t));
185
186     format = cairo_boilerplate_format_from_content (content);
187     tpc->target = cairo_image_surface_create (format,
188                                               ceil (width), ceil (height));
189
190     surface = _cairo_test_paginated_surface_create (tpc->target);
191     if (cairo_surface_status (surface))
192         goto CLEANUP;
193
194     status = cairo_surface_set_user_data (surface,
195                                           &test_paginated_closure_key,
196                                           tpc, NULL);
197     if (status == CAIRO_STATUS_SUCCESS)
198         return surface;
199
200     cairo_surface_destroy (surface);
201     surface = cairo_boilerplate_surface_create_in_error (status);
202
203     cairo_surface_destroy (tpc->target);
204
205   CLEANUP:
206     free (tpc);
207     return surface;
208 }
209
210 /* The only reason we go through all these machinations to write a PNG
211  * image is to _really ensure_ that the data actually landed in our
212  * buffer through the paginated surface to the test_paginated_surface.
213  *
214  * If we didn't implement this function then the default
215  * cairo_surface_write_to_png would result in the paginated_surface's
216  * acquire_source_image function replaying the recording-surface to an
217  * intermediate image surface. And in that case the
218  * test_paginated_surface would not be involved and wouldn't be
219  * tested.
220  */
221 static cairo_status_t
222 _cairo_boilerplate_test_paginated_surface_write_to_png (cairo_surface_t *surface,
223                                                         const char      *filename)
224 {
225     test_paginated_closure_t *tpc;
226     cairo_status_t status;
227
228     /* show page first.  the automatic show_page is too late for us */
229     cairo_surface_show_page (surface);
230     status = cairo_surface_status (surface);
231     if (status)
232         return status;
233
234     tpc = cairo_surface_get_user_data (surface, &test_paginated_closure_key);
235     return cairo_surface_write_to_png (tpc->target, filename);
236 }
237
238 static cairo_surface_t *
239 _cairo_boilerplate_test_paginated_get_image_surface (cairo_surface_t *surface,
240                                                      int              page,
241                                                      int              width,
242                                                      int              height)
243 {
244     test_paginated_closure_t *tpc;
245     cairo_status_t status;
246
247     /* XXX separate finish as per PDF */
248     if (page != 0)
249         return cairo_boilerplate_surface_create_in_error (CAIRO_STATUS_SURFACE_TYPE_MISMATCH);
250
251     /* show page first.  the automatic show_page is too late for us */
252     cairo_surface_show_page (surface);
253     status = cairo_surface_status (surface);
254     if (status)
255         return cairo_boilerplate_surface_create_in_error (status);
256
257     tpc = cairo_surface_get_user_data (surface, &test_paginated_closure_key);
258     return _cairo_boilerplate_get_image_surface (tpc->target, 0, width, height);
259 }
260
261 static void
262 _cairo_boilerplate_test_paginated_cleanup (void *closure)
263 {
264     test_paginated_closure_t *tpc = closure;
265
266     cairo_surface_destroy (tpc->target);
267     free (tpc);
268 }
269 #endif
270
271 static const cairo_boilerplate_target_t targets[] = {
272     {
273         "test-base", "base", NULL, NULL,
274         CAIRO_SURFACE_TYPE_IMAGE,
275         CAIRO_CONTENT_COLOR_ALPHA, 0,
276         "_cairo_test_base_compositor_surface_create",
277         _cairo_boilerplate_test_base_compositor_create_surface,
278         cairo_surface_create_similar,
279         NULL, NULL,
280         _cairo_boilerplate_get_image_surface,
281         cairo_surface_write_to_png,
282         NULL, NULL, NULL, TRUE, FALSE, FALSE
283     },
284     {
285         "test-base", "base", NULL, NULL,
286         CAIRO_SURFACE_TYPE_IMAGE,
287         CAIRO_CONTENT_COLOR, 0,
288         "_cairo_test_base_compositor_surface_create",
289         _cairo_boilerplate_test_base_compositor_create_surface,
290         cairo_surface_create_similar,
291         NULL, NULL,
292         _cairo_boilerplate_get_image_surface,
293         cairo_surface_write_to_png,
294         NULL, NULL, NULL, FALSE, FALSE, FALSE
295     },
296
297     {
298         "test-fallback", "image", NULL, NULL,
299         CAIRO_SURFACE_TYPE_IMAGE,
300         CAIRO_CONTENT_COLOR_ALPHA, 0,
301         "_cairo_test_fallback_compositor_surface_create",
302         _cairo_boilerplate_test_fallback_compositor_create_surface,
303         cairo_surface_create_similar,
304         NULL, NULL,
305         _cairo_boilerplate_get_image_surface,
306         cairo_surface_write_to_png,
307         NULL, NULL, NULL, FALSE, FALSE, FALSE
308     },
309     {
310         "test-fallback", "image", NULL, NULL,
311         CAIRO_SURFACE_TYPE_IMAGE,
312         CAIRO_CONTENT_COLOR, 0,
313         "_cairo_test_fallback_compositor_surface_create",
314         _cairo_boilerplate_test_fallback_compositor_create_surface,
315         cairo_surface_create_similar,
316         NULL, NULL,
317         _cairo_boilerplate_get_image_surface,
318         cairo_surface_write_to_png,
319         NULL, NULL, NULL, FALSE, FALSE, FALSE
320     },
321
322     {
323         "test-mask", "mask", NULL, NULL,
324         CAIRO_SURFACE_TYPE_IMAGE,
325         CAIRO_CONTENT_COLOR_ALPHA, 0,
326         "_cairo_test_mask_compositor_surface_create",
327         _cairo_boilerplate_test_mask_compositor_create_surface,
328         cairo_surface_create_similar,
329         NULL, NULL,
330         _cairo_boilerplate_get_image_surface,
331         cairo_surface_write_to_png,
332         NULL, NULL, NULL, TRUE, FALSE, FALSE
333     },
334     {
335         "test-mask", "mask", NULL, NULL,
336         CAIRO_SURFACE_TYPE_IMAGE,
337         CAIRO_CONTENT_COLOR, 0,
338         "_cairo_test_mask_compositor_surface_create",
339         _cairo_boilerplate_test_mask_compositor_create_surface,
340         cairo_surface_create_similar,
341         NULL, NULL,
342         _cairo_boilerplate_get_image_surface,
343         cairo_surface_write_to_png,
344         NULL, NULL, NULL, FALSE, FALSE, FALSE
345     },
346
347     {
348         "test-traps", "traps", NULL, NULL,
349         CAIRO_SURFACE_TYPE_IMAGE,
350         CAIRO_CONTENT_COLOR_ALPHA, 0,
351         "_cairo_test_traps_compositor_surface_create",
352         _cairo_boilerplate_test_traps_compositor_create_surface,
353         cairo_surface_create_similar,
354         NULL, NULL,
355         _cairo_boilerplate_get_image_surface,
356         cairo_surface_write_to_png,
357         NULL, NULL, NULL, TRUE, FALSE, FALSE
358     },
359     {
360         "test-traps", "traps", NULL, NULL,
361         CAIRO_SURFACE_TYPE_IMAGE,
362         CAIRO_CONTENT_COLOR, 0,
363         "_cairo_test_traps_compositor_surface_create",
364         _cairo_boilerplate_test_traps_compositor_create_surface,
365         cairo_surface_create_similar,
366         NULL, NULL,
367         _cairo_boilerplate_get_image_surface,
368         cairo_surface_write_to_png,
369         NULL, NULL, NULL, FALSE, FALSE, FALSE
370     },
371
372     {
373         "test-spans", "spans", NULL, NULL,
374         CAIRO_SURFACE_TYPE_IMAGE,
375         CAIRO_CONTENT_COLOR_ALPHA, 0,
376         "_cairo_test_spans_compositor_surface_create",
377         _cairo_boilerplate_test_spans_compositor_create_surface,
378         cairo_surface_create_similar,
379         NULL, NULL,
380         _cairo_boilerplate_get_image_surface,
381         cairo_surface_write_to_png,
382         NULL, NULL, NULL, TRUE, FALSE, FALSE
383     },
384     {
385         "test-spans", "spans", NULL, NULL,
386         CAIRO_SURFACE_TYPE_IMAGE,
387         CAIRO_CONTENT_COLOR, 0,
388         "_cairo_test_spans_compositor_surface_create",
389         _cairo_boilerplate_test_spans_compositor_create_surface,
390         cairo_surface_create_similar,
391         NULL, NULL,
392         _cairo_boilerplate_get_image_surface,
393         cairo_surface_write_to_png,
394         NULL, NULL, NULL, FALSE, FALSE, FALSE
395     },
396
397     {
398         "no-fallback", "image", NULL, NULL,
399         CAIRO_SURFACE_TYPE_IMAGE,
400         CAIRO_CONTENT_COLOR_ALPHA, 0,
401         "_cairo_test_no_fallback_compositor_surface_create",
402         _cairo_boilerplate_test_no_fallback_compositor_create_surface,
403         cairo_surface_create_similar,
404         NULL, NULL,
405         _cairo_boilerplate_get_image_surface,
406         cairo_surface_write_to_png,
407         NULL, NULL, NULL, FALSE, FALSE, FALSE
408     },
409     {
410         "no-traps", "traps", NULL, NULL,
411         CAIRO_SURFACE_TYPE_IMAGE,
412         CAIRO_CONTENT_COLOR_ALPHA, 0,
413         "_cairo_test_no_traps_compositor_surface_create",
414         _cairo_boilerplate_test_no_traps_compositor_create_surface,
415         cairo_surface_create_similar,
416         NULL, NULL,
417         _cairo_boilerplate_get_image_surface,
418         cairo_surface_write_to_png,
419         NULL, NULL, NULL, TRUE, FALSE, FALSE
420     },
421     {
422         "no-spans", "spans", NULL, NULL,
423         CAIRO_SURFACE_TYPE_IMAGE,
424         CAIRO_CONTENT_COLOR_ALPHA, 0,
425         "_cairo_test_no_spans_compositor_surface_create",
426         _cairo_boilerplate_test_no_spans_compositor_create_surface,
427         cairo_surface_create_similar,
428         NULL, NULL,
429         _cairo_boilerplate_get_image_surface,
430         cairo_surface_write_to_png,
431         NULL, NULL, NULL, TRUE, FALSE, FALSE
432     },
433 #if CAIRO_HAS_TEST_PAGINATED_SURFACE
434     {
435         "test-paginated", "image", NULL, NULL,
436         CAIRO_INTERNAL_SURFACE_TYPE_TEST_PAGINATED,
437         CAIRO_CONTENT_COLOR_ALPHA, 0,
438         "_cairo_test_paginated_surface_create",
439         _cairo_boilerplate_test_paginated_create_surface,
440         cairo_surface_create_similar,
441         NULL, NULL,
442         _cairo_boilerplate_test_paginated_get_image_surface,
443         _cairo_boilerplate_test_paginated_surface_write_to_png,
444         _cairo_boilerplate_test_paginated_cleanup,
445         NULL, NULL, FALSE, TRUE, FALSE
446     },
447     {
448         "test-paginated", "image", NULL, NULL,
449         CAIRO_INTERNAL_SURFACE_TYPE_TEST_PAGINATED,
450         CAIRO_CONTENT_COLOR, 0,
451         "_cairo_test_paginated_surface_create",
452         _cairo_boilerplate_test_paginated_create_surface,
453         cairo_surface_create_similar,
454         NULL, NULL,
455         _cairo_boilerplate_test_paginated_get_image_surface,
456         _cairo_boilerplate_test_paginated_surface_write_to_png,
457         _cairo_boilerplate_test_paginated_cleanup,
458         NULL, NULL, FALSE, TRUE, FALSE
459     },
460 #endif
461 };
462 CAIRO_BOILERPLATE (test, targets)