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
static char *png_filename = NULL;
38

            
39
/* Lazy way of determining PNG dimensions... */
40
static void
41
3
png_dimensions (const char *filename,
42
		cairo_content_t *content, int *width, int *height)
43
{
44
    cairo_surface_t *surface;
45

            
46
3
    surface = cairo_image_surface_create_from_png (filename);
47
3
    *content = cairo_surface_get_content (surface);
48
3
    *width = cairo_image_surface_get_width (surface);
49
3
    *height = cairo_image_surface_get_height (surface);
50
3
    cairo_surface_destroy (surface);
51
3
}
52

            
53
static cairo_surface_t *
54
24
png_acquire (cairo_pattern_t *pattern, void *closure,
55
	     cairo_surface_t *target,
56
	     const cairo_rectangle_int_t *extents)
57
{
58
24
    return cairo_image_surface_create_from_png (closure);
59
}
60

            
61
static cairo_surface_t *
62
24
red_acquire (cairo_pattern_t *pattern, void *closure,
63
	     cairo_surface_t *target,
64
	     const cairo_rectangle_int_t *extents)
65
{
66
    cairo_surface_t *image;
67
    cairo_t *cr;
68

            
69
24
    image = cairo_surface_create_similar_image (target,
70
						CAIRO_FORMAT_RGB24,
71
24
						extents->width,
72
24
						extents->height);
73
24
    cairo_surface_set_device_offset (image, extents->x, extents->y);
74

            
75
24
    cr = cairo_create (image);
76
24
    cairo_set_source_rgb (cr, 1, 0, 0);
77
24
    cairo_paint (cr);
78
24
    cairo_destroy (cr);
79

            
80
24
    return image;
81
}
82

            
83
static void
84
48
release (cairo_pattern_t *pattern, void *closure, cairo_surface_t *image)
85
{
86
48
    cairo_surface_destroy (image);
87
48
}
88

            
89
static void
90
3
free_filename(void)
91
{
92
3
    free (png_filename);
93
3
}
94

            
95
static cairo_test_status_t
96
3
draw (cairo_t *cr, int width, int height)
97
{
98
    cairo_pattern_t *png, *red;
99
    cairo_content_t content;
100
    int png_width, png_height;
101
    int i, j;
102

            
103
3
    if (png_filename == NULL) {
104
3
      const cairo_test_context_t *ctx = cairo_test_get_context (cr);
105
3
      xasprintf (&png_filename, "%s/png.png", ctx->srcdir);
106
3
      atexit (free_filename);
107
    }
108

            
109
3
    png_dimensions (png_filename, &content, &png_width, &png_height);
110

            
111
3
    png = cairo_pattern_create_raster_source ((void*)png_filename,
112
					      content, png_width, png_height);
113
3
    cairo_raster_source_pattern_set_acquire (png, png_acquire, release);
114

            
115
3
    red = cairo_pattern_create_raster_source (NULL,
116
					      CAIRO_CONTENT_COLOR, WIDTH, HEIGHT);
117
3
    cairo_raster_source_pattern_set_acquire (red, red_acquire, release);
118

            
119
3
    cairo_set_source_rgb (cr, 0, 0, 1);
120
3
    cairo_paint (cr);
121

            
122
3
    cairo_translate (cr, 0, (HEIGHT-png_height)/2);
123
15
    for (i = 0; i < 4; i++) {
124
60
	for (j = 0; j < 4; j++) {
125
	    cairo_pattern_t *source;
126
48
	    if ((i ^ j) & 1)
127
24
		source = red;
128
	    else
129
24
		source = png;
130
48
	    cairo_set_source (cr, source);
131
48
	    cairo_rectangle (cr, i * WIDTH/4, j * png_height/4, WIDTH/4, png_height/4);
132
48
	    cairo_fill (cr);
133
	}
134
    }
135

            
136
3
    cairo_pattern_destroy (red);
137
3
    cairo_pattern_destroy (png);
138

            
139
3
    return CAIRO_TEST_SUCCESS;
140
}
141

            
142
1
CAIRO_TEST (raster_source,
143
	    "Check that the mime-surface embedding works",
144
	    "api", /* keywords */
145
	    NULL, /* requirements */
146
	    WIDTH, HEIGHT,
147
	    NULL, draw)