0262245a88e189b902c1908bbcfd704c864e122d
[framework/graphics/cairo.git] / test / map-to-image.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 #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 set_pixel_black(uint8_t *data, int stride,
38           cairo_format_t format, int x, int y)
39 {
40     switch (format) {
41     case CAIRO_FORMAT_ARGB32:
42     case CAIRO_FORMAT_RGB24:
43         *(uint32_t *)(data + y * stride + 4*x) = black_pixel_argb;
44         break;
45     case CAIRO_FORMAT_RGB16_565:
46         *(uint16_t *)(data + y * stride + 2*x) = black_pixel;
47         break;
48     case CAIRO_FORMAT_RGB30:
49     case CAIRO_FORMAT_A8:
50     case CAIRO_FORMAT_A1:
51     case CAIRO_FORMAT_INVALID:
52     default:
53         return FALSE;
54     }
55     return TRUE;
56 }
57
58 static cairo_test_status_t
59 all (cairo_t *cr, int width, int height)
60 {
61     cairo_surface_t *surface;
62     uint8_t *data;
63     int stride;
64     cairo_format_t format;
65     int i, j;
66
67     /* Fill background white */
68     cairo_set_source_rgb (cr, 1, 1, 1);
69     cairo_paint (cr);
70
71     surface = cairo_surface_map_to_image (cairo_get_target (cr), NULL);
72     cairo_surface_flush (surface);
73     format = cairo_image_surface_get_format (surface);
74     stride = cairo_image_surface_get_stride (surface);
75     data = cairo_image_surface_get_data (surface);
76     if (data) {
77         for (j = 0; j < HEIGHT; j++)
78             for (i = 0; i < WIDTH; i++)
79                 if (! set_pixel_black (data, stride, format, i, j))
80                     return CAIRO_TEST_FAILURE;
81     }
82     cairo_surface_mark_dirty (surface);
83     cairo_surface_unmap_image (cairo_get_target (cr), surface);
84
85     return CAIRO_TEST_SUCCESS;
86 }
87
88 static cairo_test_status_t
89 bit (cairo_t *cr, int width, int height)
90 {
91     cairo_surface_t *surface;
92     cairo_rectangle_int_t extents;
93     cairo_format_t format;
94     uint8_t *data;
95
96     extents.x = extents.y = extents.width = extents.height = 1;
97
98     /* Fill background white */
99     cairo_set_source_rgb (cr, 1, 1, 1);
100     cairo_paint (cr);
101
102     surface = cairo_surface_map_to_image (cairo_get_target (cr), &extents);
103     cairo_surface_flush (surface);
104     data = cairo_image_surface_get_data (surface);
105     format = cairo_image_surface_get_format (surface);
106     if (data) {
107         if (! set_pixel_black (data, 0, format, 0, 0))
108             return CAIRO_TEST_FAILURE;
109     }
110     cairo_surface_mark_dirty (surface);
111     cairo_surface_unmap_image (cairo_get_target (cr), surface);
112
113     return CAIRO_TEST_SUCCESS;
114 }
115
116 static cairo_test_status_t
117 fill (cairo_t *cr, int width, int height)
118 {
119     cairo_surface_t *surface;
120     cairo_rectangle_int_t extents;
121     cairo_t *cr2;
122
123     extents.x = extents.y = extents.width = extents.height = 1;
124
125     /* Fill background white */
126     cairo_set_source_rgb (cr, 1, 1, 1);
127     cairo_paint (cr);
128
129     surface = cairo_surface_map_to_image (cairo_get_target (cr), &extents);
130     cr2 = cairo_create (surface);
131     cairo_set_source_rgb (cr2, 1, 0, 0);
132     cairo_paint (cr2);
133     cairo_destroy (cr2);
134     cairo_surface_unmap_image (cairo_get_target (cr), surface);
135
136     return CAIRO_TEST_SUCCESS;
137 }
138
139 CAIRO_TEST (map_all_to_image,
140             "Test maping a surface to an image and modifying it externally",
141             "image", /* keywords */
142             "target=raster", /* requirements */
143             WIDTH, HEIGHT,
144             NULL, all)
145 CAIRO_TEST (map_bit_to_image,
146             "Test maping a surface to an image and modifying it externally",
147             "image", /* keywords */
148             "target=raster", /* requirements */
149             WIDTH, HEIGHT,
150             NULL, bit)
151 CAIRO_TEST (map_to_image_fill,
152             "Test maping a surface to an image and modifying it externally",
153             "image", /* keywords */
154             "target=raster", /* requirements */
155             WIDTH, HEIGHT,
156             NULL, fill)