blob: 991761f560401659d744dd07b6fb2379286fa898 [file] [log] [blame]
Bryce Harringtonfb9089d2014-11-04 16:39:38 -08001/*
2 * Copyright © 2015 Samsung Electronics Co., Ltd
3 *
4 * Permission to use, copy, modify, distribute, and sell this software and
5 * its documentation for any purpose is hereby granted without fee, provided
6 * that the above copyright notice appear in all copies and that both that
7 * copyright notice and this permission notice appear in supporting
8 * documentation, and that the name of the copyright holders not be used in
9 * advertising or publicity pertaining to distribution of the software
10 * without specific, written prior permission. The copyright holders make
11 * no representations about the suitability of this software for any
12 * purpose. It is provided "as is" without express or implied warranty.
13 *
14 * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS
15 * SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
16 * FITNESS, IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
17 * SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER
18 * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF
19 * CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
20 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 */
22
23#include "config.h"
24
25#include <unistd.h>
26#include <stdio.h>
27#include <string.h> /* memcpy */
28#include <cairo.h>
29
30#include "weston-test-client-helper.h"
31
32char *server_parameters="--use-pixman --width=320 --height=240";
33
Bryce Harrington85e65f52015-05-14 12:21:13 -070034/** write_surface_as_png()
35 *
36 * Writes out a given weston test surface to disk as a PNG image
37 * using the provided filename (with path).
38 *
39 * @returns true if successfully saved file; false otherwise.
40 */
41static bool
42write_surface_as_png(const struct surface* weston_surface, const char *fname) {
43 cairo_surface_t *cairo_surface;
44 cairo_status_t status;
45 int bpp = 4; /* Assume ARGB */
46 int stride = bpp * weston_surface->width;
47
48 cairo_surface = cairo_image_surface_create_for_data(weston_surface->data,
49 CAIRO_FORMAT_ARGB32,
50 weston_surface->width,
51 weston_surface->height,
52 stride);
53 printf("Writing PNG to disk\n");
54 status = cairo_surface_write_to_png(cairo_surface, fname);
55 if (status != CAIRO_STATUS_SUCCESS) {
56 printf("Failed to save screenshot: %s\n",
57 cairo_status_to_string(status));
58 return false;
59 }
60 cairo_surface_destroy(cairo_surface);
61 return true;
62}
63
Bryce Harrington198f9412015-05-14 14:18:56 -070064/** load_surface_from_png()
65 *
66 * Reads a PNG image from disk using the given filename (and path)
67 * and returns as a freshly allocated weston test surface.
68 *
69 * @returns weston test surface with image, which should be free'd
70 * when no longer used; or, NULL in case of error.
71 */
72static struct surface*
73load_surface_from_png(const char *fname) {
74 struct surface *reference;
75 cairo_surface_t *reference_cairo_surface;
76 cairo_status_t status;
77 size_t source_data_size;
Bryce Harringtonc9198832015-05-21 11:53:54 -070078 int bpp;
Bryce Harrington198f9412015-05-14 14:18:56 -070079 int stride;
80
81 printf("Loading reference image %s\n", fname);
82 reference_cairo_surface = cairo_image_surface_create_from_png(fname);
83 status = cairo_surface_status(reference_cairo_surface);
84 if (status != CAIRO_STATUS_SUCCESS) {
85 printf("Could not open %s: %s\n", fname, cairo_status_to_string(status));
86 cairo_surface_destroy(reference_cairo_surface);
87 return NULL;
88 }
89
90 /* Disguise the cairo surface in a weston test surface */
91 reference = xzalloc(sizeof *reference);
92 if (reference == NULL) {
93 perror("xzalloc reference");
94 cairo_surface_destroy(reference_cairo_surface);
95 return NULL;
96 }
97 reference->width = cairo_image_surface_get_width(reference_cairo_surface);
98 reference->height = cairo_image_surface_get_height(reference_cairo_surface);
99 stride = cairo_image_surface_get_stride(reference_cairo_surface);
100 source_data_size = stride * reference->height;
101
Bryce Harringtonc9198832015-05-21 11:53:54 -0700102 /* Check that the file's stride matches our assumption */
103 bpp = 4;
104 if (stride != bpp * reference->width) {
105 printf("Mismatched stride for screenshot reference image %s\n", fname);
106 cairo_surface_destroy(reference_cairo_surface);
107 free(reference);
108 return NULL;
109 }
110
Bryce Harrington198f9412015-05-14 14:18:56 -0700111 /* Allocate new buffer for our weston reference, and copy the data from
112 the cairo surface so we can destroy it */
113 reference->data = xzalloc(source_data_size);
114 if (reference->data == NULL) {
115 perror("xzalloc reference data");
116 cairo_surface_destroy(reference_cairo_surface);
117 free(reference);
118 return NULL;
119 }
120 memcpy(reference->data,
121 cairo_image_surface_get_data(reference_cairo_surface),
122 source_data_size);
123
124 cairo_surface_destroy(reference_cairo_surface);
125 return reference;
126}
127
Bryce Harrington2eaf7d72015-05-14 12:50:00 -0700128/** create_screenshot_surface()
129 *
130 * Allocates and initializes a weston test surface for use in
131 * storing a screenshot of the client's output. Establishes a
132 * shm backed wl_buffer for retrieving screenshot image data
133 * from the server, sized to match the client's output display.
134 *
135 * @returns stack allocated surface image, which should be
136 * free'd when done using it.
137 */
138static struct surface*
139create_screenshot_surface(struct client *client) {
140 struct surface* screenshot;
141 screenshot = xzalloc(sizeof *screenshot);
142 if (screenshot == NULL)
143 return NULL;
Bryce Harrington0ccf8e22015-05-21 15:03:50 -0700144 screenshot->wl_buffer = create_shm_buffer(client,
145 client->output->width,
146 client->output->height,
147 &screenshot->data);
148 screenshot->height = client->output->height;
149 screenshot->width = client->output->width;
Bryce Harrington2eaf7d72015-05-14 12:50:00 -0700150
151 return screenshot;
152}
153
Bryce Harrington111e0222015-05-14 15:07:55 -0700154/** capture_screenshot_of_output()
155 *
156 * Requests a screenshot from the server of the output that the
157 * client appears on. The image data returned from the server
158 * can be accessed from the screenshot surface's data member.
159 *
160 * @returns a new surface object, which should be free'd when no
161 * longer needed.
162 */
163static struct surface *
164capture_screenshot_of_output(struct client *client) {
165 struct surface *screenshot;
166
167 /* Create a surface to hold the screenshot */
168 screenshot = create_screenshot_surface(client);
169
170 client->test->buffer_copy_done = 0;
171 weston_test_capture_screenshot(client->test->weston_test,
172 client->output->wl_output,
173 screenshot->wl_buffer);
174 while (client->test->buffer_copy_done == 0)
175 if (wl_display_dispatch(client->wl_display) < 0)
176 break;
177
178 /* FIXME: Document somewhere the orientation the screenshot is taken
179 * and how the clip coords are interpreted, in case of scaling/transform.
180 * If we're using read_pixels() just make sure it is documented somewhere.
181 * Protocol docs in the XML, comparison function docs in Doxygen style.
182 */
183
184 return screenshot;
185}
186
Derek Foreman35b7f252015-05-25 15:19:38 -0500187static void
188draw_stuff(char *pixels, int w, int h)
189{
190 int x, y;
191
192 for (x = 0; x < w; x++)
193 for (y = 0; y < h; y++) {
194 pixels[y * w * 4 + x * 4] = x;
195 pixels[y * w * 4 + x * 4 + 1] = x + y;
196 pixels[y * w * 4 + x * 4 + 2] = y;
197 pixels[y * w * 4 + x * 4 + 3] = 255;
198 }
199}
200
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800201TEST(internal_screenshot)
202{
Derek Foreman35b7f252015-05-25 15:19:38 -0500203 struct wl_buffer *buf;
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800204 struct client *client;
Derek Foreman35b7f252015-05-25 15:19:38 -0500205 struct wl_surface *surface;
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800206 struct surface *screenshot = NULL;
Derek Foreman35b7f252015-05-25 15:19:38 -0500207 struct surface *reference_good = NULL;
208 struct surface *reference_bad = NULL;
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800209 struct rectangle clip;
210 const char *fname;
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800211 bool match = false;
212 bool dump_all_images = true;
Derek Foreman35b7f252015-05-25 15:19:38 -0500213 void *pixels;
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800214
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800215 /* Create the client */
Bryce Harrington111e0222015-05-14 15:07:55 -0700216 printf("Creating client for test\n");
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800217 client = create_client_and_test_surface(100, 100, 100, 100);
218 assert(client);
Derek Foreman35b7f252015-05-25 15:19:38 -0500219 surface = client->surface->wl_surface;
220
221 buf = create_shm_buffer(client, 100, 100, &pixels);
222 draw_stuff(pixels, 100, 100);
223 wl_surface_attach(surface, buf, 0, 0);
224 wl_surface_damage(surface, 0, 0, 100, 100);
225 wl_surface_commit(surface);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800226
227 /* Take a snapshot. Result will be in screenshot->wl_buffer. */
Bryce Harrington111e0222015-05-14 15:07:55 -0700228 printf("Taking a screenshot\n");
229 screenshot = capture_screenshot_of_output(client);
230 assert(screenshot);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800231
Derek Foreman35b7f252015-05-25 15:19:38 -0500232 /* Load good reference image */
233 fname = screenshot_reference_filename("internal-screenshot-good", 0);
234 printf("Loading good reference image %s\n", fname);
235 reference_good = load_surface_from_png(fname);
236 assert(reference_good);
237
238 /* Load bad reference image */
239 fname = screenshot_reference_filename("internal-screenshot-bad", 0);
240 printf("Loading bad reference image %s\n", fname);
241 reference_bad = load_surface_from_png(fname);
242 assert(reference_bad);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800243
244 /* Test check_surfaces_equal()
Derek Foreman35b7f252015-05-25 15:19:38 -0500245 * We expect this to fail since we use a bad reference image
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800246 */
Derek Foreman35b7f252015-05-25 15:19:38 -0500247 match = check_surfaces_equal(screenshot, reference_bad);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800248 printf("Screenshot %s reference image\n", match? "equal to" : "different from");
249 assert(!match);
Derek Foreman35b7f252015-05-25 15:19:38 -0500250 free(reference_bad->data);
251 free(reference_bad);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800252
253 /* Test check_surfaces_match_in_clip()
254 * Alpha-blending and other effects can cause irrelevant discrepancies, so look only
255 * at a small portion of the solid-colored background
256 */
Derek Foreman35b7f252015-05-25 15:19:38 -0500257 clip.x = 100;
258 clip.y = 100;
259 clip.width = 100;
260 clip.height = 100;
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800261 printf("Clip: %d,%d %d x %d\n", clip.x, clip.y, clip.width, clip.height);
Derek Foreman35b7f252015-05-25 15:19:38 -0500262 match = check_surfaces_match_in_clip(screenshot, reference_good,
263 &clip);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800264 printf("Screenshot %s reference image in clipped area\n", match? "matches" : "doesn't match");
Derek Foreman35b7f252015-05-25 15:19:38 -0500265 free(reference_good->data);
266 free(reference_good);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800267
268 /* Test dumping of non-matching images */
269 if (!match || dump_all_images) {
Bryce Harrington85e65f52015-05-14 12:21:13 -0700270 fname = screenshot_output_filename("internal-screenshot", 0);
271 write_surface_as_png(screenshot, fname);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800272 }
273
274 free(screenshot);
275
276 printf("Test complete\n");
277 assert(match);
278}