Upload Tizen2.0 source
[framework/graphics/cairo.git] / test / raster-source.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_pattern_t *pattern, void *closure,
53              cairo_surface_t *target,
54              const cairo_rectangle_int_t *extents)
55 {
56     return cairo_image_surface_create_from_png (closure);
57 }
58
59 static cairo_surface_t *
60 red_acquire (cairo_pattern_t *pattern, void *closure,
61              cairo_surface_t *target,
62              const cairo_rectangle_int_t *extents)
63 {
64     cairo_surface_t *image;
65     cairo_t *cr;
66
67     image = cairo_surface_create_similar_image (target,
68                                                 CAIRO_FORMAT_RGB24,
69                                                 extents->width,
70                                                 extents->height);
71     cairo_surface_set_device_offset (image, extents->x, extents->y);
72
73     cr = cairo_create (image);
74     cairo_set_source_rgb (cr, 1, 0, 0);
75     cairo_paint (cr);
76     cairo_destroy (cr);
77
78     return image;
79 }
80
81 static void
82 release (cairo_pattern_t *pattern, void *closure, cairo_surface_t *image)
83 {
84     cairo_surface_destroy (image);
85 }
86
87 static cairo_test_status_t
88 draw (cairo_t *cr, int width, int height)
89 {
90     const char *png_filename = "png.png";
91     cairo_pattern_t *png, *red;
92     cairo_content_t content;
93     int png_width, png_height;
94     int i, j;
95
96     png_dimensions (png_filename, &content, &png_width, &png_height);
97
98     png = cairo_pattern_create_raster_source ((void*)png_filename,
99                                               content, png_width, png_height);
100     cairo_raster_source_pattern_set_acquire (png, png_acquire, release);
101
102     red = cairo_pattern_create_raster_source (NULL,
103                                               CAIRO_CONTENT_COLOR, WIDTH, HEIGHT);
104     cairo_raster_source_pattern_set_acquire (red, red_acquire, release);
105
106     cairo_set_source_rgb (cr, 0, 0, 1);
107     cairo_paint (cr);
108
109     cairo_translate (cr, 0, (HEIGHT-png_height)/2);
110     for (i = 0; i < 4; i++) {
111         for (j = 0; j < 4; j++) {
112             cairo_pattern_t *source;
113             if ((i ^ j) & 1)
114                 source = red;
115             else
116                 source = png;
117             cairo_set_source (cr, source);
118             cairo_rectangle (cr, i * WIDTH/4, j * png_height/4, WIDTH/4, png_height/4);
119             cairo_fill (cr);
120         }
121     }
122
123     cairo_pattern_destroy (red);
124     cairo_pattern_destroy (png);
125
126     return CAIRO_TEST_SUCCESS;
127 }
128
129 CAIRO_TEST (raster_source,
130             "Check that the mime-surface embedding works",
131             "api", /* keywords */
132             NULL, /* requirements */
133             WIDTH, HEIGHT,
134             NULL, draw)