Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2015 Samsung Electronics Co., Ltd |
| 3 | * |
Bryce Harrington | 2cc9297 | 2015-06-11 15:39:40 -0700 | [diff] [blame] | 4 | * Permission is hereby granted, free of charge, to any person obtaining |
| 5 | * a copy of this software and associated documentation files (the |
| 6 | * "Software"), to deal in the Software without restriction, including |
| 7 | * without limitation the rights to use, copy, modify, merge, publish, |
| 8 | * distribute, sublicense, and/or sell copies of the Software, and to |
| 9 | * permit persons to whom the Software is furnished to do so, subject to |
| 10 | * the following conditions: |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 11 | * |
Bryce Harrington | 2cc9297 | 2015-06-11 15:39:40 -0700 | [diff] [blame] | 12 | * The above copyright notice and this permission notice (including the |
| 13 | * next paragraph) shall be included in all copies or substantial |
| 14 | * portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 18 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS |
| 20 | * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 21 | * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN |
| 22 | * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 23 | * SOFTWARE. |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 28 | #include <stdio.h> |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 29 | |
| 30 | #include "weston-test-client-helper.h" |
| 31 | |
| 32 | char *server_parameters="--use-pixman --width=320 --height=240"; |
| 33 | |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 34 | static void |
Derek Foreman | 97b9b17 | 2015-05-26 12:00:49 -0500 | [diff] [blame] | 35 | draw_stuff(void *pixels, int w, int h) |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 36 | { |
| 37 | int x, y; |
Derek Foreman | 97b9b17 | 2015-05-26 12:00:49 -0500 | [diff] [blame] | 38 | uint8_t r, g, b; |
| 39 | uint32_t *pixel; |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 40 | |
| 41 | for (x = 0; x < w; x++) |
| 42 | for (y = 0; y < h; y++) { |
Derek Foreman | 97b9b17 | 2015-05-26 12:00:49 -0500 | [diff] [blame] | 43 | b = x; |
| 44 | g = x + y; |
| 45 | r = y; |
| 46 | pixel = (uint32_t *)pixels + y * w + x; |
| 47 | *pixel = (255 << 24) | (r << 16) | (g << 8) | b; |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 48 | } |
| 49 | } |
| 50 | |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 51 | TEST(internal_screenshot) |
| 52 | { |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 53 | struct wl_buffer *buf; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 54 | struct client *client; |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 55 | struct wl_surface *surface; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 56 | struct surface *screenshot = NULL; |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 57 | struct surface *reference_good = NULL; |
| 58 | struct surface *reference_bad = NULL; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 59 | struct rectangle clip; |
| 60 | const char *fname; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 61 | bool match = false; |
| 62 | bool dump_all_images = true; |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 63 | void *pixels; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 64 | |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 65 | /* Create the client */ |
Bryce Harrington | 111e022 | 2015-05-14 15:07:55 -0700 | [diff] [blame] | 66 | printf("Creating client for test\n"); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 67 | client = create_client_and_test_surface(100, 100, 100, 100); |
| 68 | assert(client); |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 69 | surface = client->surface->wl_surface; |
| 70 | |
Pekka Paalanen | e651bb0 | 2016-06-10 10:50:04 +0300 | [diff] [blame] | 71 | /* |
| 72 | * We are racing our screenshooting against weston-desktop-shell |
| 73 | * setting the cursor. If w-d-s wins, our screenshot will have a cursor |
| 74 | * shown, which makes the image comparison fail. Our window and the |
| 75 | * default pointer position are accidentally causing an overlap that |
| 76 | * intersects our test clip rectangle. |
| 77 | * |
| 78 | * w-d-s wins very rarely though, so the race is easy to miss. You can |
| 79 | * make it happen by putting a delay before the call to |
| 80 | * create_client_and_test_surface(). |
| 81 | * |
| 82 | * The weston_test_move_pointer() below makes the race irrelevant, as |
| 83 | * the cursor won't overlap with anything we care about. |
| 84 | */ |
| 85 | |
| 86 | /* Move the pointer away from the screenshot area. */ |
| 87 | weston_test_move_pointer(client->test->weston_test, 0, 0); |
| 88 | |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 89 | buf = create_shm_buffer(client, 100, 100, &pixels); |
| 90 | draw_stuff(pixels, 100, 100); |
| 91 | wl_surface_attach(surface, buf, 0, 0); |
| 92 | wl_surface_damage(surface, 0, 0, 100, 100); |
| 93 | wl_surface_commit(surface); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 94 | |
| 95 | /* Take a snapshot. Result will be in screenshot->wl_buffer. */ |
Bryce Harrington | 111e022 | 2015-05-14 15:07:55 -0700 | [diff] [blame] | 96 | printf("Taking a screenshot\n"); |
| 97 | screenshot = capture_screenshot_of_output(client); |
| 98 | assert(screenshot); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 99 | |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 100 | /* Load good reference image */ |
| 101 | fname = screenshot_reference_filename("internal-screenshot-good", 0); |
| 102 | printf("Loading good reference image %s\n", fname); |
| 103 | reference_good = load_surface_from_png(fname); |
| 104 | assert(reference_good); |
| 105 | |
| 106 | /* Load bad reference image */ |
| 107 | fname = screenshot_reference_filename("internal-screenshot-bad", 0); |
| 108 | printf("Loading bad reference image %s\n", fname); |
| 109 | reference_bad = load_surface_from_png(fname); |
| 110 | assert(reference_bad); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 111 | |
| 112 | /* Test check_surfaces_equal() |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 113 | * We expect this to fail since we use a bad reference image |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 114 | */ |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 115 | match = check_surfaces_equal(screenshot, reference_bad); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 116 | printf("Screenshot %s reference image\n", match? "equal to" : "different from"); |
| 117 | assert(!match); |
Pekka Paalanen | 924cd94 | 2016-05-20 17:25:38 +0300 | [diff] [blame] | 118 | buffer_destroy(reference_bad->buffer); |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 119 | free(reference_bad); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 120 | |
| 121 | /* Test check_surfaces_match_in_clip() |
| 122 | * Alpha-blending and other effects can cause irrelevant discrepancies, so look only |
| 123 | * at a small portion of the solid-colored background |
| 124 | */ |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 125 | clip.x = 100; |
| 126 | clip.y = 100; |
| 127 | clip.width = 100; |
| 128 | clip.height = 100; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 129 | 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] | 130 | match = check_surfaces_match_in_clip(screenshot, reference_good, |
| 131 | &clip); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 132 | printf("Screenshot %s reference image in clipped area\n", match? "matches" : "doesn't match"); |
Pekka Paalanen | 924cd94 | 2016-05-20 17:25:38 +0300 | [diff] [blame] | 133 | buffer_destroy(reference_good->buffer); |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 134 | free(reference_good); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 135 | |
| 136 | /* Test dumping of non-matching images */ |
| 137 | if (!match || dump_all_images) { |
Bryce Harrington | 85e65f5 | 2015-05-14 12:21:13 -0700 | [diff] [blame] | 138 | fname = screenshot_output_filename("internal-screenshot", 0); |
| 139 | write_surface_as_png(screenshot, fname); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | free(screenshot); |
| 143 | |
| 144 | printf("Test complete\n"); |
| 145 | assert(match); |
| 146 | } |