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