Tizen 2.0 Release
[framework/graphics/cairo.git] / test / mime-surface.c
1 /*
2  * Copyright © 2011 Intel Corporation
3  *
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:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
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
22  * SOFTWARE.
23  *
24  * Author: Chris Wilson <chris@chris-wilson.co.uk>
25  */
26
27 #include "cairo-test.h"
28
29 #include <stdio.h>
30 #include <errno.h>
31
32 /* Basic test to exercise the new mime-surface callback. */
33
34 #define WIDTH 200
35 #define HEIGHT 80
36
37 /* Lazy way of determining PNG dimensions... */
38 static void
39 png_dimensions (const char *filename,
40                 cairo_content_t *content, int *width, int *height)
41 {
42     cairo_surface_t *surface;
43
44     surface = cairo_image_surface_create_from_png (filename);
45     *content = cairo_surface_get_content (surface);
46     *width = cairo_image_surface_get_width (surface);
47     *height = cairo_image_surface_get_height (surface);
48     cairo_surface_destroy (surface);
49 }
50
51 static cairo_surface_t *
52 png_acquire (cairo_surface_t *mime_surface, void *closure,
53              cairo_surface_t *target, const cairo_rectangle_int_t *roi,
54              cairo_rectangle_int_t *extents)
55 {
56     cairo_surface_t *image;
57
58     image = cairo_image_surface_create_from_png (closure);
59     extents->x = extents->y = 0;
60     extents->width = cairo_image_surface_get_width (image);
61     extents->height = cairo_image_surface_get_height (image);
62     return image;
63 }
64
65 static cairo_surface_t *
66 red_acquire (cairo_surface_t *mime_surface, void *closure,
67              cairo_surface_t *target, const cairo_rectangle_int_t *roi,
68              cairo_rectangle_int_t *extents)
69 {
70     cairo_surface_t *image;
71     cairo_t *cr;
72
73     image = cairo_surface_create_similar_image (target,
74                                                 CAIRO_FORMAT_RGB24,
75                                                 roi->width, roi->height);
76     cr = cairo_create (image);
77     cairo_set_source_rgb (cr, 1, 0, 0);
78     cairo_paint (cr);
79     cairo_destroy (cr);
80
81     *extents = *roi;
82     return image;
83 }
84
85 static void
86 release (cairo_surface_t *mime_surface, void *closure, cairo_surface_t *image)
87 {
88     cairo_surface_destroy (image);
89 }
90
91 static cairo_test_status_t
92 draw (cairo_t *cr, int width, int height)
93 {
94     const char *png_filename = "png.png";
95     cairo_surface_t *png, *red;
96     cairo_content_t content;
97     int png_width, png_height;
98     int i, j;
99
100     png_dimensions (png_filename, &content, &png_width, &png_height);
101
102     png = cairo_mime_surface_create ((void*)png_filename, content, png_width, png_height);
103     cairo_mime_surface_set_acquire (png, png_acquire, release);
104
105     red = cairo_mime_surface_create (NULL, CAIRO_CONTENT_COLOR, WIDTH, HEIGHT);
106     cairo_mime_surface_set_acquire (red, red_acquire, release);
107
108     cairo_set_source_rgb (cr, 0, 0, 1);
109     cairo_paint (cr);
110
111     cairo_translate (cr, 0, (HEIGHT-png_height)/2);
112     for (i = 0; i < 4; i++) {
113         for (j = 0; j < 4; j++) {
114             cairo_surface_t *source;
115             if ((i ^ j) & 1)
116                 source = red;
117             else
118                 source = png;
119             cairo_set_source_surface (cr, source, 0, 0);
120             cairo_rectangle (cr, i * WIDTH/4, j * png_height/4, WIDTH/4, png_height/4);
121             cairo_fill (cr);
122         }
123     }
124
125     cairo_surface_destroy (red);
126     cairo_surface_destroy (png);
127
128     return CAIRO_TEST_SUCCESS;
129 }
130
131 static cairo_test_status_t
132 check_status (const cairo_test_context_t *ctx,
133               cairo_status_t status,
134               cairo_status_t expected)
135 {
136     if (status == expected)
137         return CAIRO_TEST_SUCCESS;
138
139     cairo_test_log (ctx,
140                     "Error: Expected status value %d (%s), received %d (%s)\n",
141                     expected,
142                     cairo_status_to_string (expected),
143                     status,
144                     cairo_status_to_string (status));
145     return CAIRO_TEST_FAILURE;
146 }
147
148 static cairo_test_status_t
149 preamble (cairo_test_context_t *ctx)
150 {
151     cairo_surface_t *mime;
152     cairo_status_t status;
153     cairo_t *cr;
154
155     /* drawing to a mime-surface is verboten */
156
157     mime = cairo_mime_surface_create (NULL, CAIRO_CONTENT_COLOR, 0, 0);
158     cr = cairo_create (mime);
159     cairo_surface_destroy (mime);
160     status = cairo_status (cr);
161     cairo_destroy (cr);
162     status = check_status (ctx, status, CAIRO_STATUS_WRITE_ERROR);
163     if (status)
164         return status;
165
166     return CAIRO_TEST_SUCCESS;
167 }
168
169 CAIRO_TEST (mime_surface,
170             "Check that the mime-surface embedding works",
171             "api", /* keywords */
172             NULL, /* requirements */
173             WIDTH, HEIGHT,
174             preamble, draw)