blob: 451ccad7bb6e190586f093afb474951d294369fa [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 Harrington2eaf7d72015-05-14 12:50:00 -070064/** create_screenshot_surface()
65 *
66 * Allocates and initializes a weston test surface for use in
67 * storing a screenshot of the client's output. Establishes a
68 * shm backed wl_buffer for retrieving screenshot image data
69 * from the server, sized to match the client's output display.
70 *
71 * @returns stack allocated surface image, which should be
72 * free'd when done using it.
73 */
74static struct surface*
75create_screenshot_surface(struct client *client) {
76 struct surface* screenshot;
77 screenshot = xzalloc(sizeof *screenshot);
78 if (screenshot == NULL)
79 return NULL;
80 screenshot->wl_buffer = create_shm_buffer(client,
81 client->output->width,
82 client->output->height,
83 &screenshot->data);
84 screenshot->height = client->output->height;
85 screenshot->width = client->output->width;
86
87 return screenshot;
88}
89
Bryce Harringtonfb9089d2014-11-04 16:39:38 -080090TEST(internal_screenshot)
91{
92 struct client *client;
93 struct surface *screenshot = NULL;
94 struct surface *reference = NULL;
95 struct rectangle clip;
96 const char *fname;
97 cairo_surface_t *reference_cairo_surface;
98 cairo_status_t status;
99 bool match = false;
100 bool dump_all_images = true;
101
102 printf("Starting test\n");
103
104 /* Create the client */
105 client = create_client_and_test_surface(100, 100, 100, 100);
106 assert(client);
107 printf("Client created\n");
108
109 /* Create a surface to hold the screenshot */
Bryce Harrington2eaf7d72015-05-14 12:50:00 -0700110 screenshot = create_screenshot_surface(client);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800111 assert(screenshot);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800112 printf("Screenshot buffer created and attached to surface\n");
113
114 /* Take a snapshot. Result will be in screenshot->wl_buffer. */
115 client->test->buffer_copy_done = 0;
116 weston_test_capture_screenshot(client->test->weston_test,
117 client->output->wl_output,
118 screenshot->wl_buffer);
119 printf("Capture request sent\n");
120 while (client->test->buffer_copy_done == 0)
121 if (wl_display_dispatch(client->wl_display) < 0)
122 break;
123 printf("Roundtrip done\n");
124
125 /* FIXME: Document somewhere the orientation the screenshot is taken
126 * and how the clip coords are interpreted, in case of scaling/transform.
127 * If we're using read_pixels() just make sure it is documented somewhere.
128 * Protocol docs in the XML, comparison function docs in Doxygen style.
129 */
130
131 /* Load reference image */
132 fname = screenshot_reference_filename("internal-screenshot", 0);
133 printf("Loading reference image %s\n", fname);
134 reference_cairo_surface = cairo_image_surface_create_from_png(fname);
135 status = cairo_surface_status(reference_cairo_surface);
136 if (status != CAIRO_STATUS_SUCCESS) {
137 printf("Could not open %s: %s\n", fname, cairo_status_to_string(status));
138 cairo_surface_destroy(reference_cairo_surface);
139 assert(status != CAIRO_STATUS_SUCCESS);
140 }
141
142 /* Disguise the cairo surface in a weston test surface */
143 reference = xzalloc(sizeof *reference);
144 reference->width = cairo_image_surface_get_width(reference_cairo_surface);
145 reference->height = cairo_image_surface_get_height(reference_cairo_surface);
146 reference->data = cairo_image_surface_get_data(reference_cairo_surface);
147
148 /* Test check_surfaces_equal()
149 * We expect this to fail since the clock will differ from when we made the reference image
150 */
151 match = check_surfaces_equal(screenshot, reference);
152 printf("Screenshot %s reference image\n", match? "equal to" : "different from");
153 assert(!match);
154
155 /* Test check_surfaces_match_in_clip()
156 * Alpha-blending and other effects can cause irrelevant discrepancies, so look only
157 * at a small portion of the solid-colored background
158 */
159 clip.x = 50;
160 clip.y = 50;
161 clip.width = 101;
162 clip.height = 101;
163 printf("Clip: %d,%d %d x %d\n", clip.x, clip.y, clip.width, clip.height);
164 match = check_surfaces_match_in_clip(screenshot, reference, &clip);
165 printf("Screenshot %s reference image in clipped area\n", match? "matches" : "doesn't match");
166 cairo_surface_destroy(reference_cairo_surface);
167 free(reference);
168
169 /* Test dumping of non-matching images */
170 if (!match || dump_all_images) {
Bryce Harrington85e65f52015-05-14 12:21:13 -0700171 fname = screenshot_output_filename("internal-screenshot", 0);
172 write_surface_as_png(screenshot, fname);
Bryce Harringtonfb9089d2014-11-04 16:39:38 -0800173 }
174
175 free(screenshot);
176
177 printf("Test complete\n");
178 assert(match);
179}