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 |
Pekka Paalanen | 95e2872 | 2016-05-20 18:01:05 +0300 | [diff] [blame] | 35 | draw_stuff(pixman_image_t *image) |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 36 | { |
Pekka Paalanen | 95e2872 | 2016-05-20 18:01:05 +0300 | [diff] [blame] | 37 | int w, h; |
| 38 | int stride; /* bytes */ |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 39 | int x, y; |
Derek Foreman | 97b9b17 | 2015-05-26 12:00:49 -0500 | [diff] [blame] | 40 | uint8_t r, g, b; |
Pekka Paalanen | 95e2872 | 2016-05-20 18:01:05 +0300 | [diff] [blame] | 41 | uint32_t *pixels; |
Derek Foreman | 97b9b17 | 2015-05-26 12:00:49 -0500 | [diff] [blame] | 42 | uint32_t *pixel; |
Pekka Paalanen | 95e2872 | 2016-05-20 18:01:05 +0300 | [diff] [blame] | 43 | pixman_format_code_t fmt; |
| 44 | |
| 45 | fmt = pixman_image_get_format(image); |
| 46 | w = pixman_image_get_width(image); |
| 47 | h = pixman_image_get_height(image); |
| 48 | stride = pixman_image_get_stride(image); |
| 49 | pixels = pixman_image_get_data(image); |
| 50 | |
| 51 | assert(PIXMAN_FORMAT_BPP(fmt) == 32); |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 52 | |
| 53 | for (x = 0; x < w; x++) |
| 54 | for (y = 0; y < h; y++) { |
Derek Foreman | 97b9b17 | 2015-05-26 12:00:49 -0500 | [diff] [blame] | 55 | b = x; |
| 56 | g = x + y; |
| 57 | r = y; |
Pekka Paalanen | 95e2872 | 2016-05-20 18:01:05 +0300 | [diff] [blame] | 58 | pixel = pixels + (y * stride / 4) + x; |
Derek Foreman | 97b9b17 | 2015-05-26 12:00:49 -0500 | [diff] [blame] | 59 | *pixel = (255 << 24) | (r << 16) | (g << 8) | b; |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 60 | } |
| 61 | } |
| 62 | |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 63 | TEST(internal_screenshot) |
| 64 | { |
Pekka Paalanen | 95e2872 | 2016-05-20 18:01:05 +0300 | [diff] [blame] | 65 | struct buffer *buf; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 66 | struct client *client; |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 67 | struct wl_surface *surface; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 68 | struct surface *screenshot = NULL; |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 69 | struct surface *reference_good = NULL; |
| 70 | struct surface *reference_bad = NULL; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 71 | struct rectangle clip; |
| 72 | const char *fname; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 73 | bool match = false; |
| 74 | bool dump_all_images = true; |
| 75 | |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 76 | /* Create the client */ |
Bryce Harrington | 111e022 | 2015-05-14 15:07:55 -0700 | [diff] [blame] | 77 | printf("Creating client for test\n"); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 78 | client = create_client_and_test_surface(100, 100, 100, 100); |
| 79 | assert(client); |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 80 | surface = client->surface->wl_surface; |
| 81 | |
Pekka Paalanen | e651bb0 | 2016-06-10 10:50:04 +0300 | [diff] [blame] | 82 | /* |
| 83 | * We are racing our screenshooting against weston-desktop-shell |
| 84 | * setting the cursor. If w-d-s wins, our screenshot will have a cursor |
| 85 | * shown, which makes the image comparison fail. Our window and the |
| 86 | * default pointer position are accidentally causing an overlap that |
| 87 | * intersects our test clip rectangle. |
| 88 | * |
| 89 | * w-d-s wins very rarely though, so the race is easy to miss. You can |
| 90 | * make it happen by putting a delay before the call to |
| 91 | * create_client_and_test_surface(). |
| 92 | * |
| 93 | * The weston_test_move_pointer() below makes the race irrelevant, as |
| 94 | * the cursor won't overlap with anything we care about. |
| 95 | */ |
| 96 | |
| 97 | /* Move the pointer away from the screenshot area. */ |
| 98 | weston_test_move_pointer(client->test->weston_test, 0, 0); |
| 99 | |
Pekka Paalanen | 95e2872 | 2016-05-20 18:01:05 +0300 | [diff] [blame] | 100 | buf = create_shm_buffer_a8r8g8b8(client, 100, 100); |
| 101 | draw_stuff(buf->image); |
| 102 | wl_surface_attach(surface, buf->proxy, 0, 0); |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 103 | wl_surface_damage(surface, 0, 0, 100, 100); |
| 104 | wl_surface_commit(surface); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 105 | |
| 106 | /* Take a snapshot. Result will be in screenshot->wl_buffer. */ |
Bryce Harrington | 111e022 | 2015-05-14 15:07:55 -0700 | [diff] [blame] | 107 | printf("Taking a screenshot\n"); |
| 108 | screenshot = capture_screenshot_of_output(client); |
| 109 | assert(screenshot); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 110 | |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 111 | /* Load good reference image */ |
| 112 | fname = screenshot_reference_filename("internal-screenshot-good", 0); |
| 113 | printf("Loading good reference image %s\n", fname); |
| 114 | reference_good = load_surface_from_png(fname); |
| 115 | assert(reference_good); |
| 116 | |
| 117 | /* Load bad reference image */ |
| 118 | fname = screenshot_reference_filename("internal-screenshot-bad", 0); |
| 119 | printf("Loading bad reference image %s\n", fname); |
| 120 | reference_bad = load_surface_from_png(fname); |
| 121 | assert(reference_bad); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 122 | |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame^] | 123 | /* Test check_images_match() without a clip. |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 124 | * We expect this to fail since we use a bad reference image |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 125 | */ |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame^] | 126 | match = check_images_match(screenshot->buffer->image, |
| 127 | reference_bad->buffer->image, NULL); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 128 | printf("Screenshot %s reference image\n", match? "equal to" : "different from"); |
| 129 | assert(!match); |
Pekka Paalanen | 924cd94 | 2016-05-20 17:25:38 +0300 | [diff] [blame] | 130 | buffer_destroy(reference_bad->buffer); |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 131 | free(reference_bad); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 132 | |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame^] | 133 | /* Test check_images_match() with clip. |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 134 | * Alpha-blending and other effects can cause irrelevant discrepancies, so look only |
| 135 | * at a small portion of the solid-colored background |
| 136 | */ |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 137 | clip.x = 100; |
| 138 | clip.y = 100; |
| 139 | clip.width = 100; |
| 140 | clip.height = 100; |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 141 | printf("Clip: %d,%d %d x %d\n", clip.x, clip.y, clip.width, clip.height); |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame^] | 142 | match = check_images_match(screenshot->buffer->image, |
| 143 | reference_good->buffer->image, |
| 144 | &clip); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 145 | 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] | 146 | buffer_destroy(reference_good->buffer); |
Derek Foreman | 35b7f25 | 2015-05-25 15:19:38 -0500 | [diff] [blame] | 147 | free(reference_good); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 148 | |
| 149 | /* Test dumping of non-matching images */ |
| 150 | if (!match || dump_all_images) { |
Bryce Harrington | 85e65f5 | 2015-05-14 12:21:13 -0700 | [diff] [blame] | 151 | fname = screenshot_output_filename("internal-screenshot", 0); |
| 152 | write_surface_as_png(screenshot, fname); |
Bryce Harrington | fb9089d | 2014-11-04 16:39:38 -0800 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | free(screenshot); |
| 156 | |
| 157 | printf("Test complete\n"); |
| 158 | assert(match); |
| 159 | } |