U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright © 2012 Intel Corporation |
| 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: |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -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. |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 24 | */ |
| 25 | |
Bryce Harrington | 12cc405 | 2014-11-19 17:18:33 -0800 | [diff] [blame] | 26 | #include "config.h" |
Kristian Høgsberg | c7d2c4c | 2013-08-26 14:43:17 -0700 | [diff] [blame] | 27 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 28 | #include <stdlib.h> |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame^] | 29 | #include <stdint.h> |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 30 | #include <stdio.h> |
| 31 | #include <string.h> |
| 32 | #include <unistd.h> |
Marek Chalupa | 4d06d46 | 2014-07-16 11:27:06 +0200 | [diff] [blame] | 33 | #include <errno.h> |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 34 | #include <sys/mman.h> |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 35 | #include <cairo.h> |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 36 | |
Jon Cruz | 4678bab | 2015-06-15 15:37:07 -0700 | [diff] [blame] | 37 | #include "shared/os-compatibility.h" |
Bryce Harrington | e99e4bf | 2016-03-16 14:15:18 -0700 | [diff] [blame] | 38 | #include "shared/xalloc.h" |
| 39 | #include "shared/zalloc.h" |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 40 | #include "weston-test-client-helper.h" |
| 41 | |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 42 | #define max(a, b) (((a) > (b)) ? (a) : (b)) |
| 43 | #define min(a, b) (((a) > (b)) ? (b) : (a)) |
| 44 | #define clip(x, a, b) min(max(x, a), b) |
| 45 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 46 | int |
| 47 | surface_contains(struct surface *surface, int x, int y) |
| 48 | { |
| 49 | /* test whether a global x,y point is contained in the surface */ |
| 50 | int sx = surface->x; |
| 51 | int sy = surface->y; |
| 52 | int sw = surface->width; |
| 53 | int sh = surface->height; |
| 54 | return x >= sx && y >= sy && x < sx + sw && y < sy + sh; |
| 55 | } |
| 56 | |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 57 | static void |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 58 | frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time) |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 59 | { |
| 60 | int *done = data; |
| 61 | |
| 62 | *done = 1; |
| 63 | |
| 64 | wl_callback_destroy(callback); |
| 65 | } |
| 66 | |
| 67 | static const struct wl_callback_listener frame_listener = { |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 68 | frame_callback_handler |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 69 | }; |
| 70 | |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 71 | struct wl_callback * |
| 72 | frame_callback_set(struct wl_surface *surface, int *done) |
| 73 | { |
| 74 | struct wl_callback *callback; |
| 75 | |
| 76 | *done = 0; |
| 77 | callback = wl_surface_frame(surface); |
| 78 | wl_callback_add_listener(callback, &frame_listener, done); |
| 79 | |
| 80 | return callback; |
| 81 | } |
| 82 | |
Marek Chalupa | 1740aa8 | 2014-07-16 11:32:50 +0200 | [diff] [blame] | 83 | int |
| 84 | frame_callback_wait_nofail(struct client *client, int *done) |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 85 | { |
| 86 | while (!*done) { |
Marek Chalupa | 1740aa8 | 2014-07-16 11:32:50 +0200 | [diff] [blame] | 87 | if (wl_display_dispatch(client->wl_display) < 0) |
| 88 | return 0; |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 89 | } |
Marek Chalupa | 1740aa8 | 2014-07-16 11:32:50 +0200 | [diff] [blame] | 90 | |
| 91 | return 1; |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 92 | } |
| 93 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 94 | void |
| 95 | move_client(struct client *client, int x, int y) |
| 96 | { |
| 97 | struct surface *surface = client->surface; |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 98 | int done; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 99 | |
| 100 | client->surface->x = x; |
| 101 | client->surface->y = y; |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 102 | weston_test_move_surface(client->test->weston_test, surface->wl_surface, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 103 | surface->x, surface->y); |
Bryce Harrington | cb50ed2 | 2014-12-10 18:33:47 -0800 | [diff] [blame] | 104 | /* The attach here is necessary because commit() will call configure |
Giulio Camuffo | 82cb505 | 2013-02-28 18:44:54 +0100 | [diff] [blame] | 105 | * only on surfaces newly attached, and the one that sets the surface |
| 106 | * position is the configure. */ |
Pekka Paalanen | 924cd94 | 2016-05-20 17:25:38 +0300 | [diff] [blame] | 107 | wl_surface_attach(surface->wl_surface, surface->buffer->proxy, 0, 0); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 108 | wl_surface_damage(surface->wl_surface, 0, 0, surface->width, |
| 109 | surface->height); |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 110 | |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 111 | frame_callback_set(surface->wl_surface, &done); |
Kristian Høgsberg | db6dc7d | 2012-12-11 21:49:13 -0500 | [diff] [blame] | 112 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 113 | wl_surface_commit(surface->wl_surface); |
| 114 | |
Pekka Paalanen | 8aaef7d | 2013-02-08 17:01:25 +0200 | [diff] [blame] | 115 | frame_callback_wait(client, &done); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 118 | int |
| 119 | get_n_egl_buffers(struct client *client) |
| 120 | { |
| 121 | client->test->n_egl_buffers = -1; |
| 122 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 123 | weston_test_get_n_egl_buffers(client->test->weston_test); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 124 | wl_display_roundtrip(client->wl_display); |
| 125 | |
| 126 | return client->test->n_egl_buffers; |
| 127 | } |
| 128 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 129 | static void |
| 130 | pointer_handle_enter(void *data, struct wl_pointer *wl_pointer, |
| 131 | uint32_t serial, struct wl_surface *wl_surface, |
| 132 | wl_fixed_t x, wl_fixed_t y) |
| 133 | { |
| 134 | struct pointer *pointer = data; |
| 135 | |
Pekka Paalanen | fd01ba0 | 2016-04-21 16:47:20 +0300 | [diff] [blame] | 136 | if (wl_surface) |
| 137 | pointer->focus = wl_surface_get_user_data(wl_surface); |
| 138 | else |
| 139 | pointer->focus = NULL; |
| 140 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 141 | pointer->x = wl_fixed_to_int(x); |
| 142 | pointer->y = wl_fixed_to_int(y); |
| 143 | |
| 144 | fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n", |
| 145 | pointer->x, pointer->y, pointer->focus); |
| 146 | } |
| 147 | |
| 148 | static void |
| 149 | pointer_handle_leave(void *data, struct wl_pointer *wl_pointer, |
| 150 | uint32_t serial, struct wl_surface *wl_surface) |
| 151 | { |
| 152 | struct pointer *pointer = data; |
| 153 | |
| 154 | pointer->focus = NULL; |
| 155 | |
| 156 | fprintf(stderr, "test-client: got pointer leave, surface %p\n", |
Pekka Paalanen | fd01ba0 | 2016-04-21 16:47:20 +0300 | [diff] [blame] | 157 | wl_surface ? wl_surface_get_user_data(wl_surface) : NULL); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 158 | } |
| 159 | |
| 160 | static void |
| 161 | pointer_handle_motion(void *data, struct wl_pointer *wl_pointer, |
| 162 | uint32_t time, wl_fixed_t x, wl_fixed_t y) |
| 163 | { |
| 164 | struct pointer *pointer = data; |
| 165 | |
| 166 | pointer->x = wl_fixed_to_int(x); |
| 167 | pointer->y = wl_fixed_to_int(y); |
| 168 | |
| 169 | fprintf(stderr, "test-client: got pointer motion %d %d\n", |
| 170 | pointer->x, pointer->y); |
| 171 | } |
| 172 | |
| 173 | static void |
| 174 | pointer_handle_button(void *data, struct wl_pointer *wl_pointer, |
| 175 | uint32_t serial, uint32_t time, uint32_t button, |
| 176 | uint32_t state) |
| 177 | { |
| 178 | struct pointer *pointer = data; |
| 179 | |
| 180 | pointer->button = button; |
| 181 | pointer->state = state; |
| 182 | |
| 183 | fprintf(stderr, "test-client: got pointer button %u %u\n", |
| 184 | button, state); |
| 185 | } |
| 186 | |
| 187 | static void |
| 188 | pointer_handle_axis(void *data, struct wl_pointer *wl_pointer, |
| 189 | uint32_t time, uint32_t axis, wl_fixed_t value) |
| 190 | { |
Kristian Høgsberg | 4c8ce20 | 2013-10-09 14:23:00 -0700 | [diff] [blame] | 191 | fprintf(stderr, "test-client: got pointer axis %u %f\n", |
| 192 | axis, wl_fixed_to_double(value)); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 193 | } |
| 194 | |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 195 | static void |
| 196 | pointer_handle_frame(void *data, struct wl_pointer *wl_pointer) |
| 197 | { |
| 198 | fprintf(stderr, "test-client: got pointer frame\n"); |
| 199 | } |
| 200 | |
| 201 | static void |
| 202 | pointer_handle_axis_source(void *data, struct wl_pointer *wl_pointer, |
| 203 | uint32_t source) |
| 204 | { |
| 205 | fprintf(stderr, "test-client: got pointer axis source %u\n", source); |
| 206 | } |
| 207 | |
| 208 | static void |
| 209 | pointer_handle_axis_stop(void *data, struct wl_pointer *wl_pointer, |
| 210 | uint32_t time, uint32_t axis) |
| 211 | { |
| 212 | fprintf(stderr, "test-client: got pointer axis stop\n"); |
| 213 | } |
| 214 | |
| 215 | static void |
| 216 | pointer_handle_axis_discrete(void *data, struct wl_pointer *wl_pointer, |
| 217 | uint32_t axis, int32_t value) |
| 218 | { |
| 219 | fprintf(stderr, "test-client: got pointer axis discrete %u %d\n", |
| 220 | axis, value); |
| 221 | } |
| 222 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 223 | static const struct wl_pointer_listener pointer_listener = { |
| 224 | pointer_handle_enter, |
| 225 | pointer_handle_leave, |
| 226 | pointer_handle_motion, |
| 227 | pointer_handle_button, |
| 228 | pointer_handle_axis, |
Peter Hutterer | 87743e9 | 2016-01-18 16:38:22 +1000 | [diff] [blame] | 229 | pointer_handle_frame, |
| 230 | pointer_handle_axis_source, |
| 231 | pointer_handle_axis_stop, |
| 232 | pointer_handle_axis_discrete, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 233 | }; |
| 234 | |
| 235 | static void |
| 236 | keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard, |
| 237 | uint32_t format, int fd, uint32_t size) |
| 238 | { |
| 239 | close(fd); |
| 240 | |
| 241 | fprintf(stderr, "test-client: got keyboard keymap\n"); |
| 242 | } |
| 243 | |
| 244 | static void |
| 245 | keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard, |
| 246 | uint32_t serial, struct wl_surface *wl_surface, |
| 247 | struct wl_array *keys) |
| 248 | { |
| 249 | struct keyboard *keyboard = data; |
| 250 | |
Pekka Paalanen | fd01ba0 | 2016-04-21 16:47:20 +0300 | [diff] [blame] | 251 | if (wl_surface) |
| 252 | keyboard->focus = wl_surface_get_user_data(wl_surface); |
| 253 | else |
| 254 | keyboard->focus = NULL; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 255 | |
| 256 | fprintf(stderr, "test-client: got keyboard enter, surface %p\n", |
| 257 | keyboard->focus); |
| 258 | } |
| 259 | |
| 260 | static void |
| 261 | keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard, |
| 262 | uint32_t serial, struct wl_surface *wl_surface) |
| 263 | { |
| 264 | struct keyboard *keyboard = data; |
| 265 | |
| 266 | keyboard->focus = NULL; |
| 267 | |
| 268 | fprintf(stderr, "test-client: got keyboard leave, surface %p\n", |
Pekka Paalanen | fd01ba0 | 2016-04-21 16:47:20 +0300 | [diff] [blame] | 269 | wl_surface ? wl_surface_get_user_data(wl_surface) : NULL); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static void |
| 273 | keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard, |
| 274 | uint32_t serial, uint32_t time, uint32_t key, |
| 275 | uint32_t state) |
| 276 | { |
| 277 | struct keyboard *keyboard = data; |
| 278 | |
| 279 | keyboard->key = key; |
| 280 | keyboard->state = state; |
| 281 | |
| 282 | fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state); |
| 283 | } |
| 284 | |
| 285 | static void |
| 286 | keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard, |
| 287 | uint32_t serial, uint32_t mods_depressed, |
| 288 | uint32_t mods_latched, uint32_t mods_locked, |
| 289 | uint32_t group) |
| 290 | { |
| 291 | struct keyboard *keyboard = data; |
| 292 | |
| 293 | keyboard->mods_depressed = mods_depressed; |
| 294 | keyboard->mods_latched = mods_latched; |
| 295 | keyboard->mods_locked = mods_locked; |
| 296 | keyboard->group = group; |
| 297 | |
| 298 | fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n", |
| 299 | mods_depressed, mods_latched, mods_locked, group); |
| 300 | } |
| 301 | |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 302 | static void |
| 303 | keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard, |
| 304 | int32_t rate, int32_t delay) |
| 305 | { |
| 306 | struct keyboard *keyboard = data; |
| 307 | |
| 308 | keyboard->repeat_info.rate = rate; |
| 309 | keyboard->repeat_info.delay = delay; |
| 310 | |
| 311 | fprintf(stderr, "test-client: got keyboard repeat_info %d %d\n", |
| 312 | rate, delay); |
| 313 | } |
| 314 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 315 | static const struct wl_keyboard_listener keyboard_listener = { |
| 316 | keyboard_handle_keymap, |
| 317 | keyboard_handle_enter, |
| 318 | keyboard_handle_leave, |
| 319 | keyboard_handle_key, |
| 320 | keyboard_handle_modifiers, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 321 | keyboard_handle_repeat_info, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 322 | }; |
| 323 | |
| 324 | static void |
Marek Chalupa | 8a5523c | 2015-03-30 09:20:07 -0400 | [diff] [blame] | 325 | touch_handle_down(void *data, struct wl_touch *wl_touch, |
| 326 | uint32_t serial, uint32_t time, struct wl_surface *surface, |
| 327 | int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) |
| 328 | { |
| 329 | struct touch *touch = data; |
| 330 | |
| 331 | touch->down_x = wl_fixed_to_int(x_w); |
| 332 | touch->down_y = wl_fixed_to_int(y_w); |
| 333 | touch->id = id; |
| 334 | |
| 335 | fprintf(stderr, "test-client: got touch down %d %d, surf: %p, id: %d\n", |
| 336 | touch->down_x, touch->down_y, surface, id); |
| 337 | } |
| 338 | |
| 339 | static void |
| 340 | touch_handle_up(void *data, struct wl_touch *wl_touch, |
| 341 | uint32_t serial, uint32_t time, int32_t id) |
| 342 | { |
| 343 | struct touch *touch = data; |
| 344 | touch->up_id = id; |
| 345 | |
| 346 | fprintf(stderr, "test-client: got touch up, id: %d\n", id); |
| 347 | } |
| 348 | |
| 349 | static void |
| 350 | touch_handle_motion(void *data, struct wl_touch *wl_touch, |
| 351 | uint32_t time, int32_t id, wl_fixed_t x_w, wl_fixed_t y_w) |
| 352 | { |
| 353 | struct touch *touch = data; |
| 354 | touch->x = wl_fixed_to_int(x_w); |
| 355 | touch->y = wl_fixed_to_int(y_w); |
| 356 | |
| 357 | fprintf(stderr, "test-client: got touch motion, %d %d, id: %d\n", |
| 358 | touch->x, touch->y, id); |
| 359 | } |
| 360 | |
| 361 | static void |
| 362 | touch_handle_frame(void *data, struct wl_touch *wl_touch) |
| 363 | { |
| 364 | struct touch *touch = data; |
| 365 | |
| 366 | ++touch->frame_no; |
| 367 | |
| 368 | fprintf(stderr, "test-client: got touch frame (%d)\n", touch->frame_no); |
| 369 | } |
| 370 | |
| 371 | static void |
| 372 | touch_handle_cancel(void *data, struct wl_touch *wl_touch) |
| 373 | { |
| 374 | struct touch *touch = data; |
| 375 | |
| 376 | ++touch->cancel_no; |
| 377 | |
| 378 | fprintf(stderr, "test-client: got touch cancel (%d)\n", touch->cancel_no); |
| 379 | } |
| 380 | |
| 381 | static const struct wl_touch_listener touch_listener = { |
| 382 | touch_handle_down, |
| 383 | touch_handle_up, |
| 384 | touch_handle_motion, |
| 385 | touch_handle_frame, |
| 386 | touch_handle_cancel, |
| 387 | }; |
| 388 | |
| 389 | static void |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 390 | surface_enter(void *data, |
| 391 | struct wl_surface *wl_surface, struct wl_output *output) |
| 392 | { |
| 393 | struct surface *surface = data; |
| 394 | |
| 395 | surface->output = wl_output_get_user_data(output); |
| 396 | |
| 397 | fprintf(stderr, "test-client: got surface enter output %p\n", |
| 398 | surface->output); |
| 399 | } |
| 400 | |
| 401 | static void |
| 402 | surface_leave(void *data, |
| 403 | struct wl_surface *wl_surface, struct wl_output *output) |
| 404 | { |
| 405 | struct surface *surface = data; |
| 406 | |
| 407 | surface->output = NULL; |
| 408 | |
| 409 | fprintf(stderr, "test-client: got surface leave output %p\n", |
| 410 | wl_output_get_user_data(output)); |
| 411 | } |
| 412 | |
| 413 | static const struct wl_surface_listener surface_listener = { |
| 414 | surface_enter, |
| 415 | surface_leave |
| 416 | }; |
| 417 | |
Pekka Paalanen | 2be0c98 | 2016-05-20 18:30:03 +0300 | [diff] [blame] | 418 | static struct buffer * |
| 419 | create_shm_buffer(struct client *client, int width, int height, |
| 420 | pixman_format_code_t format, uint32_t wlfmt) |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 421 | { |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 422 | struct wl_shm *shm = client->wl_shm; |
Pekka Paalanen | 2be0c98 | 2016-05-20 18:30:03 +0300 | [diff] [blame] | 423 | struct buffer *buf; |
| 424 | size_t stride_bytes; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 425 | struct wl_shm_pool *pool; |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 426 | int fd; |
| 427 | void *data; |
Pekka Paalanen | 2be0c98 | 2016-05-20 18:30:03 +0300 | [diff] [blame] | 428 | size_t bytes_pp; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 429 | |
Pekka Paalanen | 2be0c98 | 2016-05-20 18:30:03 +0300 | [diff] [blame] | 430 | assert(width > 0); |
| 431 | assert(height > 0); |
| 432 | |
| 433 | buf = xzalloc(sizeof *buf); |
| 434 | |
| 435 | bytes_pp = PIXMAN_FORMAT_BPP(format) / 8; |
| 436 | stride_bytes = width * bytes_pp; |
| 437 | /* round up to multiple of 4 bytes for Pixman */ |
| 438 | stride_bytes = (stride_bytes + 3) & ~3u; |
| 439 | assert(stride_bytes / bytes_pp >= (unsigned)width); |
| 440 | |
| 441 | buf->len = stride_bytes * height; |
| 442 | assert(buf->len / stride_bytes == (unsigned)height); |
| 443 | |
| 444 | fd = os_create_anonymous_file(buf->len); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 445 | assert(fd >= 0); |
| 446 | |
Pekka Paalanen | 2be0c98 | 2016-05-20 18:30:03 +0300 | [diff] [blame] | 447 | data = mmap(NULL, buf->len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0); |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 448 | if (data == MAP_FAILED) { |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 449 | close(fd); |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 450 | assert(data != MAP_FAILED); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Pekka Paalanen | 2be0c98 | 2016-05-20 18:30:03 +0300 | [diff] [blame] | 453 | pool = wl_shm_create_pool(shm, fd, buf->len); |
| 454 | buf->proxy = wl_shm_pool_create_buffer(pool, 0, width, height, |
| 455 | stride_bytes, wlfmt); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 456 | wl_shm_pool_destroy(pool); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 457 | close(fd); |
Pekka Paalanen | 32ac9b9 | 2013-02-08 17:01:26 +0200 | [diff] [blame] | 458 | |
Pekka Paalanen | 2be0c98 | 2016-05-20 18:30:03 +0300 | [diff] [blame] | 459 | buf->image = pixman_image_create_bits(format, width, height, |
| 460 | data, stride_bytes); |
Pekka Paalanen | 924cd94 | 2016-05-20 17:25:38 +0300 | [diff] [blame] | 461 | |
| 462 | assert(buf->proxy); |
| 463 | assert(buf->image); |
| 464 | |
| 465 | return buf; |
| 466 | } |
| 467 | |
Pekka Paalanen | 2be0c98 | 2016-05-20 18:30:03 +0300 | [diff] [blame] | 468 | struct buffer * |
| 469 | create_shm_buffer_a8r8g8b8(struct client *client, int width, int height) |
| 470 | { |
| 471 | assert(client->has_argb); |
| 472 | |
| 473 | return create_shm_buffer(client, width, height, |
| 474 | PIXMAN_a8r8g8b8, WL_SHM_FORMAT_ARGB8888); |
| 475 | } |
| 476 | |
Pekka Paalanen | 924cd94 | 2016-05-20 17:25:38 +0300 | [diff] [blame] | 477 | void |
| 478 | buffer_destroy(struct buffer *buf) |
| 479 | { |
| 480 | void *pixels; |
| 481 | |
| 482 | pixels = pixman_image_get_data(buf->image); |
| 483 | |
| 484 | if (buf->proxy) { |
| 485 | wl_buffer_destroy(buf->proxy); |
| 486 | assert(munmap(pixels, buf->len) == 0); |
| 487 | } |
| 488 | |
| 489 | assert(pixman_image_unref(buf->image)); |
| 490 | |
| 491 | free(buf); |
| 492 | } |
| 493 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 494 | static void |
| 495 | shm_format(void *data, struct wl_shm *wl_shm, uint32_t format) |
| 496 | { |
| 497 | struct client *client = data; |
| 498 | |
| 499 | if (format == WL_SHM_FORMAT_ARGB8888) |
| 500 | client->has_argb = 1; |
| 501 | } |
| 502 | |
| 503 | struct wl_shm_listener shm_listener = { |
| 504 | shm_format |
| 505 | }; |
| 506 | |
| 507 | static void |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 508 | test_handle_pointer_position(void *data, struct weston_test *weston_test, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 509 | wl_fixed_t x, wl_fixed_t y) |
| 510 | { |
| 511 | struct test *test = data; |
| 512 | test->pointer_x = wl_fixed_to_int(x); |
| 513 | test->pointer_y = wl_fixed_to_int(y); |
| 514 | |
| 515 | fprintf(stderr, "test-client: got global pointer %d %d\n", |
| 516 | test->pointer_x, test->pointer_y); |
| 517 | } |
| 518 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 519 | static void |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 520 | test_handle_n_egl_buffers(void *data, struct weston_test *weston_test, uint32_t n) |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 521 | { |
| 522 | struct test *test = data; |
| 523 | |
| 524 | test->n_egl_buffers = n; |
| 525 | } |
| 526 | |
Bryce Harrington | 692275f | 2015-04-23 16:33:49 -0700 | [diff] [blame] | 527 | static void |
| 528 | test_handle_capture_screenshot_done(void *data, struct weston_test *weston_test) |
| 529 | { |
| 530 | struct test *test = data; |
| 531 | |
| 532 | printf("Screenshot has been captured\n"); |
| 533 | test->buffer_copy_done = 1; |
| 534 | } |
| 535 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 536 | static const struct weston_test_listener test_listener = { |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 537 | test_handle_pointer_position, |
| 538 | test_handle_n_egl_buffers, |
Bryce Harrington | 692275f | 2015-04-23 16:33:49 -0700 | [diff] [blame] | 539 | test_handle_capture_screenshot_done, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 540 | }; |
| 541 | |
| 542 | static void |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 543 | input_update_devices(struct input *input) |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 544 | { |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 545 | struct pointer *pointer; |
| 546 | struct keyboard *keyboard; |
Marek Chalupa | 8a5523c | 2015-03-30 09:20:07 -0400 | [diff] [blame] | 547 | struct touch *touch; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 548 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 549 | struct wl_seat *seat = input->wl_seat; |
| 550 | enum wl_seat_capability caps = input->caps; |
| 551 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 552 | if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 553 | pointer = xzalloc(sizeof *pointer); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 554 | pointer->wl_pointer = wl_seat_get_pointer(seat); |
| 555 | wl_pointer_set_user_data(pointer->wl_pointer, pointer); |
| 556 | wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener, |
| 557 | pointer); |
| 558 | input->pointer = pointer; |
| 559 | } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) { |
| 560 | wl_pointer_destroy(input->pointer->wl_pointer); |
| 561 | free(input->pointer); |
| 562 | input->pointer = NULL; |
| 563 | } |
| 564 | |
| 565 | if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 566 | keyboard = xzalloc(sizeof *keyboard); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 567 | keyboard->wl_keyboard = wl_seat_get_keyboard(seat); |
| 568 | wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard); |
| 569 | wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener, |
| 570 | keyboard); |
| 571 | input->keyboard = keyboard; |
| 572 | } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) { |
| 573 | wl_keyboard_destroy(input->keyboard->wl_keyboard); |
| 574 | free(input->keyboard); |
| 575 | input->keyboard = NULL; |
| 576 | } |
Marek Chalupa | 8a5523c | 2015-03-30 09:20:07 -0400 | [diff] [blame] | 577 | |
| 578 | if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) { |
| 579 | touch = xzalloc(sizeof *touch); |
| 580 | touch->wl_touch = wl_seat_get_touch(seat); |
| 581 | wl_touch_set_user_data(touch->wl_touch, touch); |
| 582 | wl_touch_add_listener(touch->wl_touch, &touch_listener, |
| 583 | touch); |
| 584 | input->touch = touch; |
| 585 | } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) { |
| 586 | wl_touch_destroy(input->touch->wl_touch); |
| 587 | free(input->touch); |
| 588 | input->touch = NULL; |
| 589 | } |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 590 | } |
| 591 | |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 592 | static void |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 593 | seat_handle_capabilities(void *data, struct wl_seat *seat, |
| 594 | enum wl_seat_capability caps) |
| 595 | { |
| 596 | struct input *input = data; |
| 597 | |
| 598 | input->caps = caps; |
| 599 | |
| 600 | /* we will create/update the devices only with the right (test) seat. |
| 601 | * If we haven't discovered which seat is the test seat, just |
| 602 | * store capabilities and bail out */ |
Dawid Gajownik | 74a635b | 2015-08-06 17:12:19 -0300 | [diff] [blame] | 603 | if (input->seat_name && strcmp(input->seat_name, "test-seat") == 0) |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 604 | input_update_devices(input); |
| 605 | |
| 606 | fprintf(stderr, "test-client: got seat %p capabilities: %x\n", |
| 607 | input, caps); |
| 608 | } |
| 609 | |
| 610 | static void |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 611 | seat_handle_name(void *data, struct wl_seat *seat, const char *name) |
| 612 | { |
| 613 | struct input *input = data; |
| 614 | |
| 615 | input->seat_name = strdup(name); |
| 616 | assert(input->seat_name && "No memory"); |
| 617 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 618 | fprintf(stderr, "test-client: got seat %p name: \'%s\'\n", |
| 619 | input, name); |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 620 | } |
| 621 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 622 | static const struct wl_seat_listener seat_listener = { |
| 623 | seat_handle_capabilities, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 624 | seat_handle_name, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 625 | }; |
| 626 | |
| 627 | static void |
| 628 | output_handle_geometry(void *data, |
| 629 | struct wl_output *wl_output, |
| 630 | int x, int y, |
| 631 | int physical_width, |
| 632 | int physical_height, |
| 633 | int subpixel, |
| 634 | const char *make, |
| 635 | const char *model, |
| 636 | int32_t transform) |
| 637 | { |
| 638 | struct output *output = data; |
| 639 | |
| 640 | output->x = x; |
| 641 | output->y = y; |
| 642 | } |
| 643 | |
| 644 | static void |
| 645 | output_handle_mode(void *data, |
| 646 | struct wl_output *wl_output, |
| 647 | uint32_t flags, |
| 648 | int width, |
| 649 | int height, |
| 650 | int refresh) |
| 651 | { |
| 652 | struct output *output = data; |
| 653 | |
| 654 | if (flags & WL_OUTPUT_MODE_CURRENT) { |
| 655 | output->width = width; |
| 656 | output->height = height; |
| 657 | } |
| 658 | } |
| 659 | |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 660 | static void |
| 661 | output_handle_scale(void *data, |
| 662 | struct wl_output *wl_output, |
| 663 | int scale) |
| 664 | { |
| 665 | struct output *output = data; |
| 666 | |
| 667 | output->scale = scale; |
| 668 | } |
| 669 | |
| 670 | static void |
| 671 | output_handle_done(void *data, |
| 672 | struct wl_output *wl_output) |
| 673 | { |
| 674 | struct output *output = data; |
| 675 | |
| 676 | output->initialized = 1; |
| 677 | } |
| 678 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 679 | static const struct wl_output_listener output_listener = { |
| 680 | output_handle_geometry, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 681 | output_handle_mode, |
| 682 | output_handle_done, |
| 683 | output_handle_scale, |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 684 | }; |
| 685 | |
| 686 | static void |
| 687 | handle_global(void *data, struct wl_registry *registry, |
| 688 | uint32_t id, const char *interface, uint32_t version) |
| 689 | { |
| 690 | struct client *client = data; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 691 | struct output *output; |
| 692 | struct test *test; |
Kristian Høgsberg | 1cb3df4 | 2012-12-11 23:03:56 -0500 | [diff] [blame] | 693 | struct global *global; |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 694 | struct input *input; |
Kristian Høgsberg | 1cb3df4 | 2012-12-11 23:03:56 -0500 | [diff] [blame] | 695 | |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 696 | global = xzalloc(sizeof *global); |
Kristian Høgsberg | 1cb3df4 | 2012-12-11 23:03:56 -0500 | [diff] [blame] | 697 | global->name = id; |
| 698 | global->interface = strdup(interface); |
| 699 | assert(interface); |
| 700 | global->version = version; |
| 701 | wl_list_insert(client->global_list.prev, &global->link); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 702 | |
| 703 | if (strcmp(interface, "wl_compositor") == 0) { |
| 704 | client->wl_compositor = |
| 705 | wl_registry_bind(registry, id, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 706 | &wl_compositor_interface, version); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 707 | } else if (strcmp(interface, "wl_seat") == 0) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 708 | input = xzalloc(sizeof *input); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 709 | input->wl_seat = |
| 710 | wl_registry_bind(registry, id, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 711 | &wl_seat_interface, version); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 712 | wl_seat_add_listener(input->wl_seat, &seat_listener, input); |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 713 | wl_list_insert(&client->inputs, &input->link); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 714 | } else if (strcmp(interface, "wl_shm") == 0) { |
| 715 | client->wl_shm = |
| 716 | wl_registry_bind(registry, id, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 717 | &wl_shm_interface, version); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 718 | wl_shm_add_listener(client->wl_shm, &shm_listener, client); |
| 719 | } else if (strcmp(interface, "wl_output") == 0) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 720 | output = xzalloc(sizeof *output); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 721 | output->wl_output = |
| 722 | wl_registry_bind(registry, id, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 723 | &wl_output_interface, version); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 724 | wl_output_add_listener(output->wl_output, |
| 725 | &output_listener, output); |
| 726 | client->output = output; |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 727 | } else if (strcmp(interface, "weston_test") == 0) { |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 728 | test = xzalloc(sizeof *test); |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 729 | test->weston_test = |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 730 | wl_registry_bind(registry, id, |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 731 | &weston_test_interface, version); |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 732 | weston_test_add_listener(test->weston_test, &test_listener, test); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 733 | client->test = test; |
Derek Foreman | 9bb1339 | 2015-01-23 12:12:36 -0600 | [diff] [blame] | 734 | } else if (strcmp(interface, "wl_drm") == 0) { |
| 735 | client->has_wl_drm = true; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 736 | } |
| 737 | } |
| 738 | |
| 739 | static const struct wl_registry_listener registry_listener = { |
| 740 | handle_global |
| 741 | }; |
| 742 | |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 743 | void |
| 744 | skip(const char *fmt, ...) |
| 745 | { |
| 746 | va_list argp; |
| 747 | |
| 748 | va_start(argp, fmt); |
| 749 | vfprintf(stderr, fmt, argp); |
| 750 | va_end(argp); |
| 751 | |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 752 | /* automake tests uses exit code 77. weston-test-runner will see |
| 753 | * this and use it, and then weston-test's sigchld handler (in the |
| 754 | * weston process) will use that as an exit status, which is what |
| 755 | * automake will see in the end. */ |
| 756 | exit(77); |
Kristian Høgsberg | 42284f5 | 2014-01-01 17:38:04 -0800 | [diff] [blame] | 757 | } |
| 758 | |
Marek Chalupa | 4d06d46 | 2014-07-16 11:27:06 +0200 | [diff] [blame] | 759 | void |
| 760 | expect_protocol_error(struct client *client, |
| 761 | const struct wl_interface *intf, |
| 762 | uint32_t code) |
| 763 | { |
| 764 | int err; |
| 765 | uint32_t errcode, failed = 0; |
| 766 | const struct wl_interface *interface; |
| 767 | unsigned int id; |
| 768 | |
| 769 | /* if the error has not come yet, make it happen */ |
| 770 | wl_display_roundtrip(client->wl_display); |
| 771 | |
| 772 | err = wl_display_get_error(client->wl_display); |
| 773 | |
| 774 | assert(err && "Expected protocol error but nothing came"); |
| 775 | assert(err == EPROTO && "Expected protocol error but got local error"); |
| 776 | |
| 777 | errcode = wl_display_get_protocol_error(client->wl_display, |
| 778 | &interface, &id); |
| 779 | |
| 780 | /* check error */ |
| 781 | if (errcode != code) { |
| 782 | fprintf(stderr, "Should get error code %d but got %d\n", |
| 783 | code, errcode); |
| 784 | failed = 1; |
| 785 | } |
| 786 | |
| 787 | /* this should be definitely set */ |
| 788 | assert(interface); |
| 789 | |
| 790 | if (strcmp(intf->name, interface->name) != 0) { |
| 791 | fprintf(stderr, "Should get interface '%s' but got '%s'\n", |
| 792 | intf->name, interface->name); |
| 793 | failed = 1; |
| 794 | } |
| 795 | |
| 796 | if (failed) { |
| 797 | fprintf(stderr, "Expected other protocol error\n"); |
| 798 | abort(); |
| 799 | } |
| 800 | |
| 801 | /* all OK */ |
| 802 | fprintf(stderr, "Got expected protocol error on '%s' (object id: %d) " |
| 803 | "with code %d\n", interface->name, id, errcode); |
| 804 | } |
| 805 | |
Pekka Paalanen | 07921d7 | 2012-12-12 14:26:40 +0200 | [diff] [blame] | 806 | static void |
| 807 | log_handler(const char *fmt, va_list args) |
| 808 | { |
| 809 | fprintf(stderr, "libwayland: "); |
| 810 | vfprintf(stderr, fmt, args); |
| 811 | } |
| 812 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 813 | static void |
| 814 | input_destroy(struct input *inp) |
| 815 | { |
| 816 | wl_list_remove(&inp->link); |
| 817 | wl_seat_destroy(inp->wl_seat); |
| 818 | free(inp); |
| 819 | } |
| 820 | |
| 821 | /* find the test-seat and set it in client. |
| 822 | * Destroy other inputs */ |
| 823 | static void |
| 824 | client_set_input(struct client *cl) |
| 825 | { |
| 826 | struct input *inp, *inptmp; |
| 827 | wl_list_for_each_safe(inp, inptmp, &cl->inputs, link) { |
| 828 | assert(inp->seat_name && "BUG: input with no name"); |
| 829 | if (strcmp(inp->seat_name, "test-seat") == 0) { |
| 830 | cl->input = inp; |
| 831 | input_update_devices(inp); |
| 832 | } else { |
| 833 | input_destroy(inp); |
| 834 | } |
| 835 | } |
| 836 | |
| 837 | /* we keep only one input */ |
| 838 | assert(wl_list_length(&cl->inputs) == 1); |
| 839 | } |
| 840 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 841 | struct client * |
Pekka Paalanen | 1ffd461 | 2015-03-26 12:49:35 +0200 | [diff] [blame] | 842 | create_client(void) |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 843 | { |
| 844 | struct client *client; |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 845 | |
Pekka Paalanen | 07921d7 | 2012-12-12 14:26:40 +0200 | [diff] [blame] | 846 | wl_log_set_handler_client(log_handler); |
| 847 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 848 | /* connect to display */ |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 849 | client = xzalloc(sizeof *client); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 850 | client->wl_display = wl_display_connect(NULL); |
| 851 | assert(client->wl_display); |
Kristian Høgsberg | 1cb3df4 | 2012-12-11 23:03:56 -0500 | [diff] [blame] | 852 | wl_list_init(&client->global_list); |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 853 | wl_list_init(&client->inputs); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 854 | |
| 855 | /* setup registry so we can bind to interfaces */ |
| 856 | client->wl_registry = wl_display_get_registry(client->wl_display); |
Pekka Paalanen | 1ffd461 | 2015-03-26 12:49:35 +0200 | [diff] [blame] | 857 | wl_registry_add_listener(client->wl_registry, ®istry_listener, |
| 858 | client); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 859 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 860 | /* this roundtrip makes sure we have all globals and we bound to them */ |
| 861 | client_roundtrip(client); |
| 862 | /* this roundtrip makes sure we got all wl_shm.format and wl_seat.* |
| 863 | * events */ |
| 864 | client_roundtrip(client); |
| 865 | |
| 866 | /* find the right input for us */ |
| 867 | client_set_input(client); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 868 | |
| 869 | /* must have WL_SHM_FORMAT_ARGB32 */ |
| 870 | assert(client->has_argb); |
| 871 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 872 | /* must have weston_test interface */ |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 873 | assert(client->test); |
| 874 | |
| 875 | /* must have an output */ |
| 876 | assert(client->output); |
| 877 | |
Marek Chalupa | 643d85f | 2015-03-30 06:37:56 -0400 | [diff] [blame] | 878 | /* the output must be initialized */ |
| 879 | assert(client->output->initialized == 1); |
| 880 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 881 | /* must have seat set */ |
| 882 | assert(client->input); |
| 883 | |
Pekka Paalanen | 1ffd461 | 2015-03-26 12:49:35 +0200 | [diff] [blame] | 884 | return client; |
| 885 | } |
| 886 | |
| 887 | struct client * |
Pekka Paalanen | 4ac06ff | 2015-03-26 12:56:10 +0200 | [diff] [blame] | 888 | create_client_and_test_surface(int x, int y, int width, int height) |
Pekka Paalanen | 1ffd461 | 2015-03-26 12:49:35 +0200 | [diff] [blame] | 889 | { |
| 890 | struct client *client; |
| 891 | struct surface *surface; |
Pekka Paalanen | 924cd94 | 2016-05-20 17:25:38 +0300 | [diff] [blame] | 892 | pixman_color_t color = { 16384, 16384, 16384, 16384 }; /* uint16_t */ |
| 893 | pixman_image_t *solid; |
Pekka Paalanen | 1ffd461 | 2015-03-26 12:49:35 +0200 | [diff] [blame] | 894 | |
| 895 | client = create_client(); |
| 896 | |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 897 | /* initialize the client surface */ |
Kristian Høgsberg | c1b244f | 2013-10-09 14:01:23 -0700 | [diff] [blame] | 898 | surface = xzalloc(sizeof *surface); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 899 | surface->wl_surface = |
| 900 | wl_compositor_create_surface(client->wl_compositor); |
| 901 | assert(surface->wl_surface); |
| 902 | |
| 903 | wl_surface_add_listener(surface->wl_surface, &surface_listener, |
| 904 | surface); |
| 905 | |
| 906 | client->surface = surface; |
| 907 | wl_surface_set_user_data(surface->wl_surface, surface); |
| 908 | |
| 909 | surface->width = width; |
| 910 | surface->height = height; |
Pekka Paalanen | 924cd94 | 2016-05-20 17:25:38 +0300 | [diff] [blame] | 911 | surface->buffer = create_shm_buffer_a8r8g8b8(client, width, height); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 912 | |
Pekka Paalanen | 924cd94 | 2016-05-20 17:25:38 +0300 | [diff] [blame] | 913 | solid = pixman_image_create_solid_fill(&color); |
| 914 | pixman_image_composite32(PIXMAN_OP_SRC, |
| 915 | solid, /* src */ |
| 916 | NULL, /* mask */ |
| 917 | surface->buffer->image, /* dst */ |
| 918 | 0, 0, /* src x,y */ |
| 919 | 0, 0, /* mask x,y */ |
| 920 | 0, 0, /* dst x,y */ |
| 921 | width, height); |
| 922 | pixman_image_unref(solid); |
U. Artie Eoff | 1ba9b38 | 2012-12-07 13:50:32 -0800 | [diff] [blame] | 923 | |
| 924 | move_client(client, x, y); |
| 925 | |
| 926 | return client; |
| 927 | } |
Bryce Harrington | c1a1d6c | 2014-12-09 14:46:36 -0800 | [diff] [blame] | 928 | |
| 929 | static const char* |
Bryce Harrington | 3835d05 | 2015-05-18 17:47:13 -0700 | [diff] [blame] | 930 | output_path(void) |
| 931 | { |
Bryce Harrington | c1a1d6c | 2014-12-09 14:46:36 -0800 | [diff] [blame] | 932 | char *path = getenv("WESTON_TEST_OUTPUT_PATH"); |
| 933 | |
| 934 | if (!path) |
| 935 | return "."; |
| 936 | return path; |
| 937 | } |
| 938 | |
| 939 | char* |
Bryce Harrington | 3835d05 | 2015-05-18 17:47:13 -0700 | [diff] [blame] | 940 | screenshot_output_filename(const char *basename, uint32_t seq) |
| 941 | { |
Bryce Harrington | c1a1d6c | 2014-12-09 14:46:36 -0800 | [diff] [blame] | 942 | char *filename; |
| 943 | |
| 944 | if (asprintf(&filename, "%s/%s-%02d.png", |
| 945 | output_path(), basename, seq) < 0) |
| 946 | return NULL; |
| 947 | return filename; |
| 948 | } |
| 949 | |
| 950 | static const char* |
Bryce Harrington | 3835d05 | 2015-05-18 17:47:13 -0700 | [diff] [blame] | 951 | reference_path(void) |
| 952 | { |
Bryce Harrington | c1a1d6c | 2014-12-09 14:46:36 -0800 | [diff] [blame] | 953 | char *path = getenv("WESTON_TEST_REFERENCE_PATH"); |
| 954 | |
| 955 | if (!path) |
| 956 | return "./tests/reference"; |
| 957 | return path; |
| 958 | } |
| 959 | |
| 960 | char* |
Bryce Harrington | 3835d05 | 2015-05-18 17:47:13 -0700 | [diff] [blame] | 961 | screenshot_reference_filename(const char *basename, uint32_t seq) |
| 962 | { |
Bryce Harrington | c1a1d6c | 2014-12-09 14:46:36 -0800 | [diff] [blame] | 963 | char *filename; |
| 964 | |
| 965 | if (asprintf(&filename, "%s/%s-%02d.png", |
| 966 | reference_path(), basename, seq) < 0) |
| 967 | return NULL; |
| 968 | return filename; |
| 969 | } |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 970 | |
Pekka Paalanen | 289fdeb | 2016-05-25 11:47:41 +0300 | [diff] [blame] | 971 | struct format_map_entry { |
| 972 | cairo_format_t cairo; |
| 973 | pixman_format_code_t pixman; |
| 974 | }; |
| 975 | |
| 976 | static const struct format_map_entry format_map[] = { |
| 977 | { CAIRO_FORMAT_ARGB32, PIXMAN_a8r8g8b8 }, |
| 978 | { CAIRO_FORMAT_RGB24, PIXMAN_x8r8g8b8 }, |
| 979 | { CAIRO_FORMAT_A8, PIXMAN_a8 }, |
| 980 | { CAIRO_FORMAT_RGB16_565, PIXMAN_r5g6b5 }, |
| 981 | }; |
| 982 | |
| 983 | static pixman_format_code_t |
| 984 | format_cairo2pixman(cairo_format_t fmt) |
| 985 | { |
| 986 | unsigned i; |
| 987 | |
| 988 | for (i = 0; i < ARRAY_LENGTH(format_map); i++) |
| 989 | if (format_map[i].cairo == fmt) |
| 990 | return format_map[i].pixman; |
| 991 | |
| 992 | assert(0 && "unknown Cairo pixel format"); |
| 993 | } |
| 994 | |
Pekka Paalanen | fd10ef0 | 2016-05-27 13:54:35 +0300 | [diff] [blame] | 995 | static cairo_format_t |
| 996 | format_pixman2cairo(pixman_format_code_t fmt) |
| 997 | { |
| 998 | unsigned i; |
| 999 | |
| 1000 | for (i = 0; i < ARRAY_LENGTH(format_map); i++) |
| 1001 | if (format_map[i].pixman == fmt) |
| 1002 | return format_map[i].cairo; |
| 1003 | |
| 1004 | assert(0 && "unknown Pixman pixel format"); |
| 1005 | } |
| 1006 | |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 1007 | /** |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame] | 1008 | * Compute the ROI for image comparisons |
Bryce Harrington | 39a5be2 | 2015-05-14 14:36:18 -0700 | [diff] [blame] | 1009 | * |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame] | 1010 | * \param img_a An image. |
| 1011 | * \param img_b Another image. |
| 1012 | * \param clip_rect Explicit ROI, or NULL for using the whole |
| 1013 | * image area. |
| 1014 | * |
| 1015 | * \return The region of interest (ROI) that is guaranteed to be inside both |
| 1016 | * images. |
| 1017 | * |
| 1018 | * If clip_rect is given, it must fall inside of both images. |
| 1019 | * If clip_rect is NULL, the images must be of the same size. |
| 1020 | * If any precondition is violated, this function aborts with an error. |
| 1021 | * |
| 1022 | * The ROI is given as pixman_box32_t, where x2,y2 are non-inclusive. |
Bryce Harrington | 39a5be2 | 2015-05-14 14:36:18 -0700 | [diff] [blame] | 1023 | */ |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame] | 1024 | static pixman_box32_t |
| 1025 | image_check_get_roi(pixman_image_t *img_a, pixman_image_t *img_b, |
| 1026 | const struct rectangle *clip_rect) |
Bryce Harrington | 39a5be2 | 2015-05-14 14:36:18 -0700 | [diff] [blame] | 1027 | { |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame] | 1028 | int width_a; |
| 1029 | int width_b; |
| 1030 | int height_a; |
| 1031 | int height_b; |
| 1032 | pixman_box32_t box; |
| 1033 | |
| 1034 | width_a = pixman_image_get_width(img_a); |
| 1035 | height_a = pixman_image_get_height(img_a); |
| 1036 | |
| 1037 | width_b = pixman_image_get_width(img_b); |
| 1038 | height_b = pixman_image_get_height(img_b); |
| 1039 | |
| 1040 | if (clip_rect) { |
| 1041 | box.x1 = clip_rect->x; |
| 1042 | box.y1 = clip_rect->y; |
| 1043 | box.x2 = clip_rect->x + clip_rect->width; |
| 1044 | box.y2 = clip_rect->y + clip_rect->height; |
| 1045 | } else { |
| 1046 | box.x1 = 0; |
| 1047 | box.y1 = 0; |
| 1048 | box.x2 = max(width_a, width_b); |
| 1049 | box.y2 = max(height_a, height_b); |
Bryce Harrington | 39a5be2 | 2015-05-14 14:36:18 -0700 | [diff] [blame] | 1050 | } |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame] | 1051 | |
| 1052 | assert(box.x1 >= 0); |
| 1053 | assert(box.y1 >= 0); |
| 1054 | assert(box.x2 > box.x1); |
| 1055 | assert(box.y2 > box.y1); |
| 1056 | assert(box.x2 <= width_a); |
| 1057 | assert(box.x2 <= width_b); |
| 1058 | assert(box.y2 <= height_a); |
| 1059 | assert(box.y2 <= height_b); |
| 1060 | |
| 1061 | return box; |
| 1062 | } |
| 1063 | |
| 1064 | struct image_iterator { |
| 1065 | char *data; |
| 1066 | int stride; /* bytes */ |
| 1067 | }; |
| 1068 | |
| 1069 | static void |
| 1070 | image_iter_init(struct image_iterator *it, pixman_image_t *image) |
| 1071 | { |
| 1072 | pixman_format_code_t fmt; |
| 1073 | |
| 1074 | it->stride = pixman_image_get_stride(image); |
| 1075 | it->data = (void *)pixman_image_get_data(image); |
| 1076 | |
| 1077 | fmt = pixman_image_get_format(image); |
| 1078 | assert(PIXMAN_FORMAT_BPP(fmt) == 32); |
| 1079 | } |
| 1080 | |
| 1081 | static uint32_t * |
| 1082 | image_iter_get_row(struct image_iterator *it, int y) |
| 1083 | { |
| 1084 | return (uint32_t *)(it->data + y * it->stride); |
Bryce Harrington | 39a5be2 | 2015-05-14 14:36:18 -0700 | [diff] [blame] | 1085 | } |
| 1086 | |
| 1087 | /** |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame] | 1088 | * Test if a given region within two images are pixel-identical. |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 1089 | * |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame] | 1090 | * Returns true if the two images pixel-wise identical, and false otherwise. |
| 1091 | * |
| 1092 | * \param img_a First image. |
| 1093 | * \param img_b Second image. |
| 1094 | * \param clip_rect The region of interest, or NULL for comparing the whole |
| 1095 | * images. |
| 1096 | * |
| 1097 | * This function hard-fails if clip_rect is not inside both images. If clip_rect |
| 1098 | * is given, the images do not have to match in size, otherwise size mismatch |
| 1099 | * will be a hard failure. |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 1100 | */ |
| 1101 | bool |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame] | 1102 | check_images_match(pixman_image_t *img_a, pixman_image_t *img_b, |
| 1103 | const struct rectangle *clip_rect) |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 1104 | { |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame] | 1105 | struct image_iterator it_a; |
| 1106 | struct image_iterator it_b; |
| 1107 | pixman_box32_t box; |
| 1108 | int x, y; |
| 1109 | uint32_t *pix_a; |
| 1110 | uint32_t *pix_b; |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 1111 | |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame] | 1112 | box = image_check_get_roi(img_a, img_b, clip_rect); |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 1113 | |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame] | 1114 | image_iter_init(&it_a, img_a); |
| 1115 | image_iter_init(&it_b, img_b); |
Pekka Paalanen | 924cd94 | 2016-05-20 17:25:38 +0300 | [diff] [blame] | 1116 | |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame] | 1117 | for (y = box.y1; y < box.y2; y++) { |
| 1118 | pix_a = image_iter_get_row(&it_a, y) + box.x1; |
| 1119 | pix_b = image_iter_get_row(&it_b, y) + box.x1; |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 1120 | |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame] | 1121 | for (x = box.x1; x < box.x2; x++) { |
| 1122 | if (*pix_a != *pix_b) |
| 1123 | return false; |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 1124 | |
Pekka Paalanen | 47d68da | 2016-05-23 17:42:27 +0300 | [diff] [blame] | 1125 | pix_a++; |
| 1126 | pix_b++; |
Bryce Harrington | 273c285 | 2014-12-15 15:51:25 -0800 | [diff] [blame] | 1127 | } |
| 1128 | } |
| 1129 | |
| 1130 | return true; |
| 1131 | } |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1132 | |
Pekka Paalanen | fd10ef0 | 2016-05-27 13:54:35 +0300 | [diff] [blame] | 1133 | /** |
Pekka Paalanen | a5bb91d | 2016-06-02 18:02:46 +0300 | [diff] [blame] | 1134 | * Tint a color |
| 1135 | * |
| 1136 | * \param src Source pixel as x8r8g8b8. |
| 1137 | * \param add The tint as x8r8g8b8, x8 must be zero; r8, g8 and b8 must be |
| 1138 | * no greater than 0xc0 to avoid overflow to another channel. |
| 1139 | * \return The tinted pixel color as x8r8g8b8, x8 guaranteed to be 0xff. |
| 1140 | * |
| 1141 | * The source pixel RGB values are divided by 4, and then the tint is added. |
| 1142 | * To achieve colors outside of the range of src, a tint color channel must be |
| 1143 | * at least 0x40. (0xff / 4 = 0x3f, 0xff - 0x3f = 0xc0) |
| 1144 | */ |
| 1145 | static uint32_t |
| 1146 | tint(uint32_t src, uint32_t add) |
| 1147 | { |
| 1148 | uint32_t v; |
| 1149 | |
| 1150 | v = ((src & 0xfcfcfcfc) >> 2) | 0xff000000; |
| 1151 | |
| 1152 | return v + add; |
| 1153 | } |
| 1154 | |
| 1155 | /** |
| 1156 | * Create a visualization of image differences. |
| 1157 | * |
| 1158 | * \param img_a First image, which is used as the basis for the output. |
| 1159 | * \param img_b Second image. |
| 1160 | * \param clip_rect The region of interest, or NULL for comparing the whole |
| 1161 | * images. |
| 1162 | * \return A new image with the differences highlighted. |
| 1163 | * |
| 1164 | * Regions outside of the region of interest are shaded with black, matching |
| 1165 | * pixels are shaded with green, and differing pixels are shaded with |
| 1166 | * bright red. |
| 1167 | * |
| 1168 | * This function hard-fails if clip_rect is not inside both images. If clip_rect |
| 1169 | * is given, the images do not have to match in size, otherwise size mismatch |
| 1170 | * will be a hard failure. |
| 1171 | */ |
| 1172 | pixman_image_t * |
| 1173 | visualize_image_difference(pixman_image_t *img_a, pixman_image_t *img_b, |
| 1174 | const struct rectangle *clip_rect) |
| 1175 | { |
| 1176 | pixman_image_t *diffimg; |
| 1177 | pixman_image_t *shade; |
| 1178 | struct image_iterator it_a; |
| 1179 | struct image_iterator it_b; |
| 1180 | struct image_iterator it_d; |
| 1181 | int width; |
| 1182 | int height; |
| 1183 | pixman_box32_t box; |
| 1184 | int x, y; |
| 1185 | uint32_t *pix_a; |
| 1186 | uint32_t *pix_b; |
| 1187 | uint32_t *pix_d; |
| 1188 | pixman_color_t shade_color = { 0, 0, 0, 32768 }; |
| 1189 | |
| 1190 | width = pixman_image_get_width(img_a); |
| 1191 | height = pixman_image_get_height(img_a); |
| 1192 | box = image_check_get_roi(img_a, img_b, clip_rect); |
| 1193 | |
| 1194 | diffimg = pixman_image_create_bits_no_clear(PIXMAN_x8r8g8b8, |
| 1195 | width, height, NULL, 0); |
| 1196 | |
| 1197 | /* Fill diffimg with a black-shaded copy of img_a, and then fill |
| 1198 | * the clip_rect area with original img_a. |
| 1199 | */ |
| 1200 | shade = pixman_image_create_solid_fill(&shade_color); |
| 1201 | pixman_image_composite32(PIXMAN_OP_SRC, img_a, shade, diffimg, |
| 1202 | 0, 0, 0, 0, 0, 0, width, height); |
| 1203 | pixman_image_unref(shade); |
| 1204 | pixman_image_composite32(PIXMAN_OP_SRC, img_a, NULL, diffimg, |
| 1205 | box.x1, box.y1, 0, 0, box.x1, box.y1, |
| 1206 | box.x2 - box.x1, box.y2 - box.y1); |
| 1207 | |
| 1208 | image_iter_init(&it_a, img_a); |
| 1209 | image_iter_init(&it_b, img_b); |
| 1210 | image_iter_init(&it_d, diffimg); |
| 1211 | |
| 1212 | for (y = box.y1; y < box.y2; y++) { |
| 1213 | pix_a = image_iter_get_row(&it_a, y) + box.x1; |
| 1214 | pix_b = image_iter_get_row(&it_b, y) + box.x1; |
| 1215 | pix_d = image_iter_get_row(&it_d, y) + box.x1; |
| 1216 | |
| 1217 | for (x = box.x1; x < box.x2; x++) { |
| 1218 | if (*pix_a == *pix_b) |
| 1219 | *pix_d = tint(*pix_d, 0x00008000); /* green */ |
| 1220 | else |
| 1221 | *pix_d = tint(*pix_d, 0x00c00000); /* red */ |
| 1222 | |
| 1223 | pix_a++; |
| 1224 | pix_b++; |
| 1225 | pix_d++; |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | return diffimg; |
| 1230 | } |
| 1231 | |
| 1232 | /** |
Pekka Paalanen | fd10ef0 | 2016-05-27 13:54:35 +0300 | [diff] [blame] | 1233 | * Write an image into a PNG file. |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1234 | * |
Pekka Paalanen | fd10ef0 | 2016-05-27 13:54:35 +0300 | [diff] [blame] | 1235 | * \param image The image. |
| 1236 | * \param fname The name and path for the file. |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1237 | * |
Pekka Paalanen | fd10ef0 | 2016-05-27 13:54:35 +0300 | [diff] [blame] | 1238 | * \returns true if successfully saved file; false otherwise. |
| 1239 | * |
| 1240 | * \note Only image formats directly supported by Cairo are accepted, not all |
| 1241 | * Pixman formats. |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1242 | */ |
| 1243 | bool |
Pekka Paalanen | fd10ef0 | 2016-05-27 13:54:35 +0300 | [diff] [blame] | 1244 | write_image_as_png(pixman_image_t *image, const char *fname) |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1245 | { |
| 1246 | cairo_surface_t *cairo_surface; |
| 1247 | cairo_status_t status; |
Pekka Paalanen | fd10ef0 | 2016-05-27 13:54:35 +0300 | [diff] [blame] | 1248 | cairo_format_t fmt; |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1249 | |
Pekka Paalanen | fd10ef0 | 2016-05-27 13:54:35 +0300 | [diff] [blame] | 1250 | fmt = format_pixman2cairo(pixman_image_get_format(image)); |
| 1251 | |
| 1252 | cairo_surface = cairo_image_surface_create_for_data( |
| 1253 | (void *)pixman_image_get_data(image), |
| 1254 | fmt, |
| 1255 | pixman_image_get_width(image), |
| 1256 | pixman_image_get_height(image), |
| 1257 | pixman_image_get_stride(image)); |
| 1258 | |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1259 | status = cairo_surface_write_to_png(cairo_surface, fname); |
| 1260 | if (status != CAIRO_STATUS_SUCCESS) { |
Pekka Paalanen | fd10ef0 | 2016-05-27 13:54:35 +0300 | [diff] [blame] | 1261 | fprintf(stderr, "Failed to save image '%s': %s\n", fname, |
| 1262 | cairo_status_to_string(status)); |
| 1263 | |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1264 | return false; |
| 1265 | } |
Pekka Paalanen | fd10ef0 | 2016-05-27 13:54:35 +0300 | [diff] [blame] | 1266 | |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1267 | cairo_surface_destroy(cairo_surface); |
Pekka Paalanen | fd10ef0 | 2016-05-27 13:54:35 +0300 | [diff] [blame] | 1268 | |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1269 | return true; |
| 1270 | } |
| 1271 | |
Pekka Paalanen | 289fdeb | 2016-05-25 11:47:41 +0300 | [diff] [blame] | 1272 | static pixman_image_t * |
| 1273 | image_convert_to_a8r8g8b8(pixman_image_t *image) |
| 1274 | { |
| 1275 | pixman_image_t *ret; |
| 1276 | int width; |
| 1277 | int height; |
| 1278 | |
| 1279 | if (pixman_image_get_format(image) == PIXMAN_a8r8g8b8) |
| 1280 | return pixman_image_ref(image); |
| 1281 | |
| 1282 | width = pixman_image_get_width(image); |
| 1283 | height = pixman_image_get_height(image); |
| 1284 | |
| 1285 | ret = pixman_image_create_bits_no_clear(PIXMAN_a8r8g8b8, width, height, |
| 1286 | NULL, 0); |
| 1287 | assert(ret); |
| 1288 | |
| 1289 | pixman_image_composite32(PIXMAN_OP_SRC, image, NULL, ret, |
| 1290 | 0, 0, 0, 0, 0, 0, width, height); |
| 1291 | |
| 1292 | return ret; |
| 1293 | } |
| 1294 | |
| 1295 | static void |
| 1296 | destroy_cairo_surface(pixman_image_t *image, void *data) |
| 1297 | { |
| 1298 | cairo_surface_t *surface = data; |
| 1299 | |
| 1300 | cairo_surface_destroy(surface); |
| 1301 | } |
| 1302 | |
| 1303 | /** |
| 1304 | * Load an image from a PNG file |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1305 | * |
| 1306 | * Reads a PNG image from disk using the given filename (and path) |
Pekka Paalanen | 289fdeb | 2016-05-25 11:47:41 +0300 | [diff] [blame] | 1307 | * and returns as a Pixman image. Use pixman_image_unref() to free it. |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1308 | * |
Pekka Paalanen | 289fdeb | 2016-05-25 11:47:41 +0300 | [diff] [blame] | 1309 | * The returned image is always in PIXMAN_a8r8g8b8 format. |
| 1310 | * |
| 1311 | * @returns Pixman image, or NULL in case of error. |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1312 | */ |
Pekka Paalanen | 289fdeb | 2016-05-25 11:47:41 +0300 | [diff] [blame] | 1313 | pixman_image_t * |
| 1314 | load_image_from_png(const char *fname) |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1315 | { |
Pekka Paalanen | 289fdeb | 2016-05-25 11:47:41 +0300 | [diff] [blame] | 1316 | pixman_image_t *image; |
| 1317 | pixman_image_t *converted; |
| 1318 | cairo_format_t cairo_fmt; |
| 1319 | pixman_format_code_t pixman_fmt; |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1320 | cairo_surface_t *reference_cairo_surface; |
| 1321 | cairo_status_t status; |
Pekka Paalanen | 289fdeb | 2016-05-25 11:47:41 +0300 | [diff] [blame] | 1322 | int width; |
| 1323 | int height; |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1324 | int stride; |
Pekka Paalanen | 289fdeb | 2016-05-25 11:47:41 +0300 | [diff] [blame] | 1325 | void *data; |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1326 | |
| 1327 | reference_cairo_surface = cairo_image_surface_create_from_png(fname); |
Pekka Paalanen | 289fdeb | 2016-05-25 11:47:41 +0300 | [diff] [blame] | 1328 | cairo_surface_flush(reference_cairo_surface); |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1329 | status = cairo_surface_status(reference_cairo_surface); |
| 1330 | if (status != CAIRO_STATUS_SUCCESS) { |
| 1331 | printf("Could not open %s: %s\n", fname, cairo_status_to_string(status)); |
| 1332 | cairo_surface_destroy(reference_cairo_surface); |
| 1333 | return NULL; |
| 1334 | } |
| 1335 | |
Pekka Paalanen | 289fdeb | 2016-05-25 11:47:41 +0300 | [diff] [blame] | 1336 | cairo_fmt = cairo_image_surface_get_format(reference_cairo_surface); |
| 1337 | pixman_fmt = format_cairo2pixman(cairo_fmt); |
| 1338 | |
| 1339 | width = cairo_image_surface_get_width(reference_cairo_surface); |
| 1340 | height = cairo_image_surface_get_height(reference_cairo_surface); |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1341 | stride = cairo_image_surface_get_stride(reference_cairo_surface); |
Pekka Paalanen | 289fdeb | 2016-05-25 11:47:41 +0300 | [diff] [blame] | 1342 | data = cairo_image_surface_get_data(reference_cairo_surface); |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1343 | |
Pekka Paalanen | 289fdeb | 2016-05-25 11:47:41 +0300 | [diff] [blame] | 1344 | /* The Cairo surface will own the data, so we keep it around. */ |
| 1345 | image = pixman_image_create_bits_no_clear(pixman_fmt, |
| 1346 | width, height, data, stride); |
| 1347 | assert(image); |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1348 | |
Pekka Paalanen | 289fdeb | 2016-05-25 11:47:41 +0300 | [diff] [blame] | 1349 | pixman_image_set_destroy_function(image, destroy_cairo_surface, |
| 1350 | reference_cairo_surface); |
Pekka Paalanen | 924cd94 | 2016-05-20 17:25:38 +0300 | [diff] [blame] | 1351 | |
Pekka Paalanen | 289fdeb | 2016-05-25 11:47:41 +0300 | [diff] [blame] | 1352 | converted = image_convert_to_a8r8g8b8(image); |
| 1353 | pixman_image_unref(image); |
Pekka Paalanen | 924cd94 | 2016-05-20 17:25:38 +0300 | [diff] [blame] | 1354 | |
Pekka Paalanen | 289fdeb | 2016-05-25 11:47:41 +0300 | [diff] [blame] | 1355 | return converted; |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1356 | } |
| 1357 | |
Pekka Paalanen | 365c129 | 2016-05-27 14:11:01 +0300 | [diff] [blame] | 1358 | /** |
| 1359 | * Take screenshot of a single output |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1360 | * |
| 1361 | * Requests a screenshot from the server of the output that the |
Pekka Paalanen | 365c129 | 2016-05-27 14:11:01 +0300 | [diff] [blame] | 1362 | * client appears on. This implies that the compositor goes through an output |
| 1363 | * repaint to provide the screenshot before this function returns. This |
| 1364 | * function is therefore both a server roundtrip and a wait for a repaint. |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1365 | * |
Pekka Paalanen | 365c129 | 2016-05-27 14:11:01 +0300 | [diff] [blame] | 1366 | * @returns A new buffer object, that should be freed with buffer_destroy(). |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1367 | */ |
Pekka Paalanen | 365c129 | 2016-05-27 14:11:01 +0300 | [diff] [blame] | 1368 | struct buffer * |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1369 | capture_screenshot_of_output(struct client *client) |
| 1370 | { |
Pekka Paalanen | 365c129 | 2016-05-27 14:11:01 +0300 | [diff] [blame] | 1371 | struct buffer *buffer; |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1372 | |
Pekka Paalanen | 365c129 | 2016-05-27 14:11:01 +0300 | [diff] [blame] | 1373 | buffer = create_shm_buffer_a8r8g8b8(client, |
| 1374 | client->output->width, |
| 1375 | client->output->height); |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1376 | |
| 1377 | client->test->buffer_copy_done = 0; |
| 1378 | weston_test_capture_screenshot(client->test->weston_test, |
| 1379 | client->output->wl_output, |
Pekka Paalanen | 365c129 | 2016-05-27 14:11:01 +0300 | [diff] [blame] | 1380 | buffer->proxy); |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1381 | while (client->test->buffer_copy_done == 0) |
| 1382 | if (wl_display_dispatch(client->wl_display) < 0) |
| 1383 | break; |
| 1384 | |
| 1385 | /* FIXME: Document somewhere the orientation the screenshot is taken |
| 1386 | * and how the clip coords are interpreted, in case of scaling/transform. |
| 1387 | * If we're using read_pixels() just make sure it is documented somewhere. |
| 1388 | * Protocol docs in the XML, comparison function docs in Doxygen style. |
| 1389 | */ |
| 1390 | |
Pekka Paalanen | 365c129 | 2016-05-27 14:11:01 +0300 | [diff] [blame] | 1391 | return buffer; |
Bryce Harrington | 892122e | 2015-09-24 14:31:44 -0700 | [diff] [blame] | 1392 | } |