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
#define WIDTH	3
30
#define HEIGHT	3
31

            
32
/* A single, black pixel */
33
static const uint32_t black_pixel_argb = 0xff000000;
34
static const uint32_t black_pixel      = 0x00000000;
35

            
36
static cairo_bool_t
37
30
set_pixel_black(uint8_t *data, int stride,
38
	  cairo_format_t format, int x, int y)
39
{
40
30
    switch (format) {
41
20
    case CAIRO_FORMAT_ARGB32:
42
    case CAIRO_FORMAT_RGB24:
43
20
	*(uint32_t *)(data + y * stride + 4*x) = black_pixel_argb;
44
20
	break;
45
10
    case CAIRO_FORMAT_RGB16_565:
46
10
	*(uint16_t *)(data + y * stride + 2*x) = black_pixel;
47
10
	break;
48
    case CAIRO_FORMAT_RGBA128F:
49
    case CAIRO_FORMAT_RGB96F:
50
    case CAIRO_FORMAT_RGB30:
51
    case CAIRO_FORMAT_A8:
52
    case CAIRO_FORMAT_A1:
53
    case CAIRO_FORMAT_INVALID:
54
    default:
55
	return FALSE;
56
    }
57
30
    return TRUE;
58
}
59

            
60
static cairo_test_status_t
61
3
all (cairo_t *cr, int width, int height)
62
{
63
    cairo_surface_t *surface;
64
    uint8_t *data;
65
    int stride;
66
    cairo_format_t format;
67
    int i, j;
68

            
69
    /* Fill background white */
70
3
    cairo_set_source_rgb (cr, 1, 1, 1);
71
3
    cairo_paint (cr);
72

            
73
3
    surface = cairo_surface_map_to_image (cairo_get_target (cr), NULL);
74
3
    cairo_surface_flush (surface);
75
3
    format = cairo_image_surface_get_format (surface);
76
3
    stride = cairo_image_surface_get_stride (surface);
77
3
    data = cairo_image_surface_get_data (surface);
78
3
    if (data) {
79
12
	for (j = 0; j < HEIGHT; j++)
80
36
	    for (i = 0; i < WIDTH; i++)
81
27
		if (! set_pixel_black (data, stride, format, i, j))
82
		    return CAIRO_TEST_FAILURE;
83
    }
84
3
    cairo_surface_mark_dirty (surface);
85
3
    cairo_surface_unmap_image (cairo_get_target (cr), surface);
86

            
87
3
    return CAIRO_TEST_SUCCESS;
88
}
89

            
90
static cairo_test_status_t
91
3
bit (cairo_t *cr, int width, int height)
92
{
93
    cairo_surface_t *surface;
94
    cairo_rectangle_int_t extents;
95
    cairo_format_t format;
96
    uint8_t *data;
97

            
98
3
    extents.x = extents.y = extents.width = extents.height = 1;
99

            
100
    /* Fill background white */
101
3
    cairo_set_source_rgb (cr, 1, 1, 1);
102
3
    cairo_paint (cr);
103

            
104
3
    surface = cairo_surface_map_to_image (cairo_get_target (cr), &extents);
105
3
    cairo_surface_flush (surface);
106
3
    data = cairo_image_surface_get_data (surface);
107
3
    format = cairo_image_surface_get_format (surface);
108
3
    if (data) {
109
3
	if (! set_pixel_black (data, 0, format, 0, 0))
110
	    return CAIRO_TEST_FAILURE;
111
    }
112
3
    cairo_surface_mark_dirty (surface);
113
3
    cairo_surface_unmap_image (cairo_get_target (cr), surface);
114

            
115
3
    return CAIRO_TEST_SUCCESS;
116
}
117

            
118
static cairo_test_status_t
119
3
fill (cairo_t *cr, int width, int height)
120
{
121
    cairo_surface_t *surface;
122
    cairo_rectangle_int_t extents;
123
    cairo_t *cr2;
124

            
125
3
    extents.x = extents.y = extents.width = extents.height = 1;
126

            
127
    /* Fill background white */
128
3
    cairo_set_source_rgb (cr, 1, 1, 1);
129
3
    cairo_paint (cr);
130

            
131
3
    surface = cairo_surface_map_to_image (cairo_get_target (cr), &extents);
132
3
    cr2 = cairo_create (surface);
133
3
    cairo_set_source_rgb (cr2, 1, 0, 0);
134
3
    cairo_paint (cr2);
135
3
    cairo_destroy (cr2);
136
3
    cairo_surface_unmap_image (cairo_get_target (cr), surface);
137

            
138
3
    return CAIRO_TEST_SUCCESS;
139
}
140

            
141
1
CAIRO_TEST (map_all_to_image,
142
	    "Test mapping a surface to an image and modifying it externally",
143
	    "image", /* keywords */
144
	    "target=raster", /* requirements */
145
	    WIDTH, HEIGHT,
146
	    NULL, all)
147
1
CAIRO_TEST (map_bit_to_image,
148
	    "Test mapping a surface to an image and modifying it externally",
149
	    "image", /* keywords */
150
	    "target=raster", /* requirements */
151
	    WIDTH, HEIGHT,
152
	    NULL, bit)
153
1
CAIRO_TEST (map_to_image_fill,
154
	    "Test mapping a surface to an image and modifying it externally",
155
	    "image", /* keywords */
156
	    "target=raster", /* requirements */
157
	    WIDTH, HEIGHT,
158
	    NULL, fill)