blob: b855c6a012a2694d5a86a246c8648f57b431e30d [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;
78 int stride;
79
80 printf("Loading reference image %s\n", fname);
81 reference_cairo_surface = cairo_image_surface_create_from_png(fname);
82 status = cairo_surface_status(reference_cairo_surface);
83 if (status != CAIRO_STATUS_SUCCESS) {
84 printf("Could not open %s: %s\n", fname, cairo_status_to_string(status));
85 cairo_surface_destroy(reference_cairo_surface);
86 return NULL;
87 }
88
89 /* Disguise the cairo surface in a weston test surface */
90 reference = xzalloc(sizeof *reference);
91 if (reference == NULL) {
92 perror("xzalloc reference");
93 cairo_surface_destroy(reference_cairo_surface);
94 return NULL;
95 }
96 reference->width = cairo_image_surface_get_width(reference_cairo_surface);
97 reference->height = cairo_image_surface_get_height(reference_cairo_surface);
98 stride = cairo_image_surface_get_stride(reference_cairo_surface);
99 source_data_size = stride * reference->height;
100
101 /* Allocate new buffer for our weston reference, and copy the data from
102 the cairo surface so we can destroy it */
103 reference->data = xzalloc(source_data_size);
104 if (reference->data == NULL) {
105 perror("xzalloc reference data");
106 cairo_surface_destroy(reference_cairo_surface);
107 free(reference);
108 return NULL;
109 }
110 memcpy(reference->data,
111 cairo_image_surface_get_data(reference_cairo_surface),
112 source_data_size);
113
114 cairo_surface_destroy(reference_cairo_surface);
115 return reference;
116}
117
Bryce Harrington2eaf7d72015-05-14 12:50:00 -0700118/** create_screenshot_surface()
119 *
120 * Allocates and initializes a weston test surface for use in
121 * storing a screenshot of the client's output. Establishes a
122 * shm backed wl_buffer for retrieving screenshot image data
123 * from the server, sized to match the client's output display.
124 *
125 * @returns stack allocated surface image, which should be
126 * free'd when done using it.
127 */
128static struct surface*
129create_screenshot_surface(struct client *client) {
130 struct surface* screenshot;
131 screenshot = xzalloc(sizeof *screenshot);
132 if (screenshot == NULL)
133 return NULL;
134 screenshot->wl_buffer = create_shm_buffer(client,
135 client->output->width,
136 client->output->height,
137 &screenshot->data);
138 screenshot->height = client->output->height;
139 screenshot->width = client->output->width;
140
141 return screenshot;
142}
143
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800144TEST(internal_screenshot)
145{
146 struct client *client;
147 struct surface *screenshot = NULL;
148 struct surface *reference = NULL;
149 struct rectangle clip;
150 const char *fname;
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800151 bool match = false;
152 bool dump_all_images = true;
153
154 printf("Starting test\n");
155
156 /* Create the client */
157 client = create_client_and_test_surface(100, 100, 100, 100);
158 assert(client);
159 printf("Client created\n");
160
161 /* Create a surface to hold the screenshot */
Bryce Harrington2eaf7d72015-05-14 12:50:00 -0700162 screenshot = create_screenshot_surface(client);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800163 assert(screenshot);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800164 printf("Screenshot buffer created and attached to surface\n");
165
166 /* Take a snapshot. Result will be in screenshot->wl_buffer. */
167 client->test->buffer_copy_done = 0;
168 weston_test_capture_screenshot(client->test->weston_test,
169 client->output->wl_output,
170 screenshot->wl_buffer);
171 printf("Capture request sent\n");
172 while (client->test->buffer_copy_done == 0)
173 if (wl_display_dispatch(client->wl_display) < 0)
174 break;
175 printf("Roundtrip done\n");
176
177 /* FIXME: Document somewhere the orientation the screenshot is taken
178 * and how the clip coords are interpreted, in case of scaling/transform.
179 * If we're using read_pixels() just make sure it is documented somewhere.
180 * Protocol docs in the XML, comparison function docs in Doxygen style.
181 */
182
183 /* Load reference image */
184 fname = screenshot_reference_filename("internal-screenshot", 0);
185 printf("Loading reference image %s\n", fname);
Bryce Harrington198f9412015-05-14 14:18:56 -0700186 reference = load_surface_from_png(fname);
187 assert(reference);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800188
189 /* Test check_surfaces_equal()
190 * We expect this to fail since the clock will differ from when we made the reference image
191 */
192 match = check_surfaces_equal(screenshot, reference);
193 printf("Screenshot %s reference image\n", match? "equal to" : "different from");
194 assert(!match);
195
196 /* Test check_surfaces_match_in_clip()
197 * Alpha-blending and other effects can cause irrelevant discrepancies, so look only
198 * at a small portion of the solid-colored background
199 */
200 clip.x = 50;
201 clip.y = 50;
202 clip.width = 101;
203 clip.height = 101;
204 printf("Clip: %d,%d %d x %d\n", clip.x, clip.y, clip.width, clip.height);
205 match = check_surfaces_match_in_clip(screenshot, reference, &clip);
206 printf("Screenshot %s reference image in clipped area\n", match? "matches" : "doesn't match");
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800207 free(reference);
208
209 /* Test dumping of non-matching images */
210 if (!match || dump_all_images) {
Bryce Harrington85e65f52015-05-14 12:21:13 -0700211 fname = screenshot_output_filename("internal-screenshot", 0);
212 write_surface_as_png(screenshot, fname);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800213 }
214
215 free(screenshot);
216
217 printf("Test complete\n");
218 assert(match);
219}