Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 32 | char *server_parameters="--use-pixman --width=320 --height=240"; |
| 33 | |
Bryce Harrington | 85e65f5 | 2015-05-14 12:21:13 -0700 | [diff] [blame] | 34 | /** 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 | */ |
| 41 | static bool |
| 42 | write_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 Harrington | 198f941 | 2015-05-14 14:18:56 -0700 | [diff] [blame] | 64 | /** 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 | */ |
| 72 | static struct surface* |
| 73 | load_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 Harrington | c919883 | 2015-05-21 11:53:54 -0700 | [diff] [blame] | 78 | int bpp; |
Bryce Harrington | 198f941 | 2015-05-14 14:18:56 -0700 | [diff] [blame] | 79 | int stride; |
| 80 | |
Bryce Harrington | 198f941 | 2015-05-14 14:18:56 -0700 | [diff] [blame] | 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 | |
Bryce Harrington | c919883 | 2015-05-21 11:53:54 -0700 | [diff] [blame] | 101 | /* Check that the file's stride matches our assumption */ |
| 102 | bpp = 4; |
| 103 | if (stride != bpp * reference->width) { |
| 104 | printf("Mismatched stride for screenshot reference image %s\n", fname); |
| 105 | cairo_surface_destroy(reference_cairo_surface); |
| 106 | free(reference); |
| 107 | return NULL; |
| 108 | } |
| 109 | |
Bryce Harrington | 198f941 | 2015-05-14 14:18:56 -0700 | [diff] [blame] | 110 | /* Allocate new buffer for our weston reference, and copy the data from |
| 111 | the cairo surface so we can destroy it */ |
| 112 | reference->data = xzalloc(source_data_size); |
| 113 | if (reference->data == NULL) { |
| 114 | perror("xzalloc reference data"); |
| 115 | cairo_surface_destroy(reference_cairo_surface); |
| 116 | free(reference); |
| 117 | return NULL; |
| 118 | } |
| 119 | memcpy(reference->data, |
| 120 | cairo_image_surface_get_data(reference_cairo_surface), |
| 121 | source_data_size); |
| 122 | |
| 123 | cairo_surface_destroy(reference_cairo_surface); |
| 124 | return reference; |
| 125 | } |
| 126 | |
Bryce Harrington | 2eaf7d7 | 2015-05-14 12:50:00 -0700 | [diff] [blame] | 127 | /** create_screenshot_surface() |
| 128 | * |
| 129 | * Allocates and initializes a weston test surface for use in |
| 130 | * storing a screenshot of the client's output. Establishes a |
| 131 | * shm backed wl_buffer for retrieving screenshot image data |
| 132 | * from the server, sized to match the client's output display. |
| 133 | * |
| 134 | * @returns stack allocated surface image, which should be |
| 135 | * free'd when done using it. |
| 136 | */ |
| 137 | static struct surface* |
| 138 | create_screenshot_surface(struct client *client) { |
| 139 | struct surface* screenshot; |
| 140 | screenshot = xzalloc(sizeof *screenshot); |
| 141 | if (screenshot == NULL) |
| 142 | return NULL; |
Bryce Harrington | 0ccf8e2 | 2015-05-21 15:03:50 -0700 | [diff] [blame] | 143 | screenshot->wl_buffer = create_shm_buffer(client, |
| 144 | client->output->width, |
| 145 | client->output->height, |
| 146 | &screenshot->data); |
| 147 | screenshot->height = client->output->height; |
| 148 | screenshot->width = client->output->width; |
Bryce Harrington | 2eaf7d7 | 2015-05-14 12:50:00 -0700 | [diff] [blame] | 149 | |
| 150 | return screenshot; |
| 151 | } |
| 152 | |
Bryce Harrington | 111e022 | 2015-05-14 15:07:55 -0700 | [diff] [blame] | 153 | /** capture_screenshot_of_output() |
| 154 | * |
| 155 | * Requests a screenshot from the server of the output that the |
| 156 | * client appears on. The image data returned from the server |
| 157 | * can be accessed from the screenshot surface's data member. |
| 158 | * |
| 159 | * @returns a new surface object, which should be free'd when no |
| 160 | * longer needed. |
| 161 | */ |
| 162 | static struct surface * |
| 163 | capture_screenshot_of_output(struct client *client) { |
| 164 | struct surface *screenshot; |
| 165 | |
| 166 | /* Create a surface to hold the screenshot */ |
| 167 | screenshot = create_screenshot_surface(client); |
| 168 | |
| 169 | client->test->buffer_copy_done = 0; |
| 170 | weston_test_capture_screenshot(client->test->weston_test, |
| 171 | client->output->wl_output, |
| 172 | screenshot->wl_buffer); |
| 173 | while (client->test->buffer_copy_done == 0) |
| 174 | if (wl_display_dispatch(client->wl_display) < 0) |
| 175 | break; |
| 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 | return screenshot; |
| 184 | } |
| 185 | |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 186 | static void |
Derek Foreman | 97b9b17 | 2015-05-26 12:00:49 -0500 | [diff] [blame^] | 187 | draw_stuff(void *pixels, int w, int h) |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 188 | { |
| 189 | int x, y; |
Derek Foreman | 97b9b17 | 2015-05-26 12:00:49 -0500 | [diff] [blame^] | 190 | uint8_t r, g, b; |
| 191 | uint32_t *pixel; |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 192 | |
| 193 | for (x = 0; x < w; x++) |
| 194 | for (y = 0; y < h; y++) { |
Derek Foreman | 97b9b17 | 2015-05-26 12:00:49 -0500 | [diff] [blame^] | 195 | b = x; |
| 196 | g = x + y; |
| 197 | r = y; |
| 198 | pixel = (uint32_t *)pixels + y * w + x; |
| 199 | *pixel = (255 << 24) | (r << 16) | (g << 8) | b; |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 200 | } |
| 201 | } |
| 202 | |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 203 | TEST(internal_screenshot) |
| 204 | { |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 205 | struct wl_buffer *buf; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 206 | struct client *client; |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 207 | struct wl_surface *surface; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 208 | struct surface *screenshot = NULL; |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 209 | struct surface *reference_good = NULL; |
| 210 | struct surface *reference_bad = NULL; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 211 | struct rectangle clip; |
| 212 | const char *fname; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 213 | bool match = false; |
| 214 | bool dump_all_images = true; |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 215 | void *pixels; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 216 | |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 217 | /* Create the client */ |
Bryce Harrington | 111e022 | 2015-05-14 15:07:55 -0700 | [diff] [blame] | 218 | printf("Creating client for test\n"); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 219 | client = create_client_and_test_surface(100, 100, 100, 100); |
| 220 | assert(client); |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 221 | surface = client->surface->wl_surface; |
| 222 | |
| 223 | buf = create_shm_buffer(client, 100, 100, &pixels); |
| 224 | draw_stuff(pixels, 100, 100); |
| 225 | wl_surface_attach(surface, buf, 0, 0); |
| 226 | wl_surface_damage(surface, 0, 0, 100, 100); |
| 227 | wl_surface_commit(surface); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 228 | |
| 229 | /* Take a snapshot. Result will be in screenshot->wl_buffer. */ |
Bryce Harrington | 111e022 | 2015-05-14 15:07:55 -0700 | [diff] [blame] | 230 | printf("Taking a screenshot\n"); |
| 231 | screenshot = capture_screenshot_of_output(client); |
| 232 | assert(screenshot); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 233 | |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 234 | /* Load good reference image */ |
| 235 | fname = screenshot_reference_filename("internal-screenshot-good", 0); |
| 236 | printf("Loading good reference image %s\n", fname); |
| 237 | reference_good = load_surface_from_png(fname); |
| 238 | assert(reference_good); |
| 239 | |
| 240 | /* Load bad reference image */ |
| 241 | fname = screenshot_reference_filename("internal-screenshot-bad", 0); |
| 242 | printf("Loading bad reference image %s\n", fname); |
| 243 | reference_bad = load_surface_from_png(fname); |
| 244 | assert(reference_bad); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 245 | |
| 246 | /* Test check_surfaces_equal() |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 247 | * We expect this to fail since we use a bad reference image |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 248 | */ |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 249 | match = check_surfaces_equal(screenshot, reference_bad); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 250 | printf("Screenshot %s reference image\n", match? "equal to" : "different from"); |
| 251 | assert(!match); |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 252 | free(reference_bad->data); |
| 253 | free(reference_bad); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 254 | |
| 255 | /* Test check_surfaces_match_in_clip() |
| 256 | * Alpha-blending and other effects can cause irrelevant discrepancies, so look only |
| 257 | * at a small portion of the solid-colored background |
| 258 | */ |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 259 | clip.x = 100; |
| 260 | clip.y = 100; |
| 261 | clip.width = 100; |
| 262 | clip.height = 100; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 263 | printf("Clip: %d,%d %d x %d\n", clip.x, clip.y, clip.width, clip.height); |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 264 | match = check_surfaces_match_in_clip(screenshot, reference_good, |
| 265 | &clip); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 266 | printf("Screenshot %s reference image in clipped area\n", match? "matches" : "doesn't match"); |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 267 | free(reference_good->data); |
| 268 | free(reference_good); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 269 | |
| 270 | /* Test dumping of non-matching images */ |
| 271 | if (!match || dump_all_images) { |
Bryce Harrington | 85e65f5 | 2015-05-14 12:21:13 -0700 | [diff] [blame] | 272 | fname = screenshot_output_filename("internal-screenshot", 0); |
| 273 | write_surface_as_png(screenshot, fname); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | free(screenshot); |
| 277 | |
| 278 | printf("Test complete\n"); |
| 279 | assert(match); |
| 280 | } |