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