U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -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 | 65e7e7a | 2012-12-07 13:50:29 -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 | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 24 | */ |
| 25 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 26 | #include "config.h" |
| 27 | |
Jussi Kukkonen | 649bbce | 2016-07-19 14:16:27 +0300 | [diff] [blame^] | 28 | #include <stdint.h> |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 29 | #include <stdlib.h> |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 30 | #include <assert.h> |
| 31 | #include <signal.h> |
| 32 | #include <unistd.h> |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 33 | #include <string.h> |
Bryce Harrington | a768026 | 2014-11-19 17:18:34 -0800 | [diff] [blame] | 34 | |
Pekka Paalanen | b5e3ea2 | 2016-06-03 17:12:10 +0300 | [diff] [blame] | 35 | #include "compositor.h" |
Pekka Paalanen | 58f98c9 | 2016-06-03 16:45:21 +0300 | [diff] [blame] | 36 | #include "compositor/weston.h" |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 37 | #include "weston-test-server-protocol.h" |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 38 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 39 | #ifdef ENABLE_EGL |
| 40 | #include <EGL/egl.h> |
| 41 | #include <EGL/eglext.h> |
Pekka Paalanen | b5e3ea2 | 2016-06-03 17:12:10 +0300 | [diff] [blame] | 42 | #include "weston-egl-ext.h" |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 43 | #endif /* ENABLE_EGL */ |
| 44 | |
Jon Cruz | 867d50e | 2015-06-15 15:37:10 -0700 | [diff] [blame] | 45 | #include "shared/helpers.h" |
| 46 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 47 | struct weston_test { |
| 48 | struct weston_compositor *compositor; |
| 49 | struct weston_layer layer; |
| 50 | struct weston_process process; |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 51 | struct weston_seat seat; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 52 | }; |
| 53 | |
| 54 | struct weston_test_surface { |
| 55 | struct weston_surface *surface; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 56 | struct weston_view *view; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 57 | int32_t x, y; |
| 58 | struct weston_test *test; |
| 59 | }; |
| 60 | |
| 61 | static void |
| 62 | test_client_sigchld(struct weston_process *process, int status) |
| 63 | { |
| 64 | struct weston_test *test = |
| 65 | container_of(process, struct weston_test, process); |
| 66 | |
Emilio Pozuelo Monfort | dae8a4b | 2014-02-07 09:34:48 +0100 | [diff] [blame] | 67 | /* Chain up from weston-test-runner's exit code so that automake |
| 68 | * knows the exit status and can report e.g. skipped tests. */ |
| 69 | if (WIFEXITED(status) && WEXITSTATUS(status) != 0) |
| 70 | exit(WEXITSTATUS(status)); |
| 71 | |
| 72 | /* In case the child aborted or segfaulted... */ |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 73 | assert(status == 0); |
| 74 | |
| 75 | wl_display_terminate(test->compositor->wl_display); |
| 76 | } |
| 77 | |
| 78 | static struct weston_seat * |
| 79 | get_seat(struct weston_test *test) |
| 80 | { |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 81 | return &test->seat; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | static void |
| 85 | notify_pointer_position(struct weston_test *test, struct wl_resource *resource) |
| 86 | { |
| 87 | struct weston_seat *seat = get_seat(test); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 88 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 89 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 90 | weston_test_send_pointer_position(resource, pointer->x, pointer->y); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | static void |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 94 | test_surface_configure(struct weston_surface *surface, int32_t sx, int32_t sy) |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 95 | { |
Giulio Camuffo | 7fe01b1 | 2013-03-28 18:02:42 +0100 | [diff] [blame] | 96 | struct weston_test_surface *test_surface = surface->configure_private; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 97 | struct weston_test *test = test_surface->test; |
| 98 | |
Giulio Camuffo | 412e6a5 | 2014-07-09 22:12:56 +0300 | [diff] [blame] | 99 | if (wl_list_empty(&test_surface->view->layer_link.link)) |
| 100 | weston_layer_entry_insert(&test->layer.view_list, |
| 101 | &test_surface->view->layer_link); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 102 | |
Jason Ekstrand | 918f2dd | 2013-12-02 21:01:53 -0600 | [diff] [blame] | 103 | weston_view_set_position(test_surface->view, |
| 104 | test_surface->x, test_surface->y); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 105 | |
Kristian Høgsberg | ace0a39 | 2013-11-13 21:55:57 -0800 | [diff] [blame] | 106 | weston_view_update_transform(test_surface->view); |
Armin Krezović | d0cf441 | 2016-06-30 06:04:32 +0200 | [diff] [blame] | 107 | |
| 108 | test_surface->surface->is_mapped = true; |
| 109 | test_surface->view->is_mapped = true; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | static void |
| 113 | move_surface(struct wl_client *client, struct wl_resource *resource, |
| 114 | struct wl_resource *surface_resource, |
| 115 | int32_t x, int32_t y) |
| 116 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 117 | struct weston_surface *surface = |
| 118 | wl_resource_get_user_data(surface_resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 119 | struct weston_test_surface *test_surface; |
| 120 | |
Giulio Camuffo | 7fe01b1 | 2013-03-28 18:02:42 +0100 | [diff] [blame] | 121 | test_surface = surface->configure_private; |
Jason Ekstrand | a7af704 | 2013-10-12 22:38:11 -0500 | [diff] [blame] | 122 | if (!test_surface) { |
| 123 | test_surface = malloc(sizeof *test_surface); |
| 124 | if (!test_surface) { |
| 125 | wl_resource_post_no_memory(resource); |
| 126 | return; |
| 127 | } |
| 128 | |
| 129 | test_surface->view = weston_view_create(surface); |
| 130 | if (!test_surface->view) { |
| 131 | wl_resource_post_no_memory(resource); |
| 132 | free(test_surface); |
| 133 | return; |
| 134 | } |
| 135 | |
| 136 | surface->configure_private = test_surface; |
| 137 | surface->configure = test_surface_configure; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | test_surface->surface = surface; |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 141 | test_surface->test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 142 | test_surface->x = x; |
| 143 | test_surface->y = y; |
| 144 | } |
| 145 | |
| 146 | static void |
| 147 | move_pointer(struct wl_client *client, struct wl_resource *resource, |
| 148 | int32_t x, int32_t y) |
| 149 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 150 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 151 | struct weston_seat *seat = get_seat(test); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 152 | struct weston_pointer *pointer = weston_seat_get_pointer(seat); |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 153 | struct weston_pointer_motion_event event = { 0 }; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 154 | |
Jonas Ådahl | d251010 | 2014-10-05 21:39:14 +0200 | [diff] [blame] | 155 | event = (struct weston_pointer_motion_event) { |
| 156 | .mask = WESTON_POINTER_MOTION_REL, |
| 157 | .dx = wl_fixed_to_double(wl_fixed_from_int(x) - pointer->x), |
| 158 | .dy = wl_fixed_to_double(wl_fixed_from_int(y) - pointer->y), |
| 159 | }; |
| 160 | |
| 161 | notify_motion(seat, 100, &event); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 162 | |
| 163 | notify_pointer_position(test, resource); |
| 164 | } |
| 165 | |
| 166 | static void |
| 167 | send_button(struct wl_client *client, struct wl_resource *resource, |
| 168 | int32_t button, uint32_t state) |
| 169 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 170 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 171 | struct weston_seat *seat = get_seat(test); |
| 172 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 173 | notify_button(seat, 100, button, state); |
| 174 | } |
| 175 | |
| 176 | static void |
| 177 | activate_surface(struct wl_client *client, struct wl_resource *resource, |
| 178 | struct wl_resource *surface_resource) |
| 179 | { |
| 180 | struct weston_surface *surface = surface_resource ? |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 181 | wl_resource_get_user_data(surface_resource) : NULL; |
| 182 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 183 | struct weston_seat *seat; |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 184 | struct weston_keyboard *keyboard; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 185 | |
| 186 | seat = get_seat(test); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 187 | keyboard = weston_seat_get_keyboard(seat); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 188 | if (surface) { |
Bryce Harrington | 260c2ff | 2016-06-29 19:04:06 -0700 | [diff] [blame] | 189 | weston_seat_set_keyboard_focus(seat, surface); |
Derek Foreman | 1281a36 | 2015-07-31 16:55:32 -0500 | [diff] [blame] | 190 | notify_keyboard_focus_in(seat, &keyboard->keys, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 191 | STATE_UPDATE_AUTOMATIC); |
| 192 | } |
| 193 | else { |
| 194 | notify_keyboard_focus_out(seat); |
Bryce Harrington | 260c2ff | 2016-06-29 19:04:06 -0700 | [diff] [blame] | 195 | weston_seat_set_keyboard_focus(seat, surface); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 196 | } |
| 197 | } |
| 198 | |
| 199 | static void |
| 200 | send_key(struct wl_client *client, struct wl_resource *resource, |
| 201 | uint32_t key, enum wl_keyboard_key_state state) |
| 202 | { |
Kristian Høgsberg | 8f7f483 | 2013-06-25 16:18:35 -0400 | [diff] [blame] | 203 | struct weston_test *test = wl_resource_get_user_data(resource); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 204 | struct weston_seat *seat = get_seat(test); |
| 205 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 206 | notify_key(seat, 100, key, state, STATE_UPDATE_AUTOMATIC); |
| 207 | } |
| 208 | |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 209 | static void |
| 210 | device_release(struct wl_client *client, |
| 211 | struct wl_resource *resource, const char *device) |
| 212 | { |
| 213 | struct weston_test *test = wl_resource_get_user_data(resource); |
| 214 | struct weston_seat *seat = get_seat(test); |
| 215 | |
| 216 | if (strcmp(device, "pointer") == 0) { |
| 217 | weston_seat_release_pointer(seat); |
| 218 | } else if (strcmp(device, "keyboard") == 0) { |
| 219 | weston_seat_release_keyboard(seat); |
| 220 | } else if (strcmp(device, "touch") == 0) { |
| 221 | weston_seat_release_touch(seat); |
| 222 | } else if (strcmp(device, "seat") == 0) { |
| 223 | weston_seat_release(seat); |
| 224 | } else { |
| 225 | assert(0 && "Unsupported device"); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | static void |
| 230 | device_add(struct wl_client *client, |
| 231 | struct wl_resource *resource, const char *device) |
| 232 | { |
| 233 | struct weston_test *test = wl_resource_get_user_data(resource); |
| 234 | struct weston_seat *seat = get_seat(test); |
| 235 | |
| 236 | if (strcmp(device, "pointer") == 0) { |
| 237 | weston_seat_init_pointer(seat); |
| 238 | } else if (strcmp(device, "keyboard") == 0) { |
| 239 | weston_seat_init_keyboard(seat, NULL); |
| 240 | } else if (strcmp(device, "touch") == 0) { |
| 241 | weston_seat_init_touch(seat); |
| 242 | } else { |
| 243 | assert(0 && "Unsupported device"); |
| 244 | } |
| 245 | } |
| 246 | |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 247 | #ifdef ENABLE_EGL |
| 248 | static int |
| 249 | is_egl_buffer(struct wl_resource *resource) |
| 250 | { |
| 251 | PFNEGLQUERYWAYLANDBUFFERWL query_buffer = |
| 252 | (void *) eglGetProcAddress("eglQueryWaylandBufferWL"); |
| 253 | EGLint format; |
| 254 | |
| 255 | if (query_buffer(eglGetCurrentDisplay(), |
| 256 | resource, |
| 257 | EGL_TEXTURE_FORMAT, |
| 258 | &format)) |
| 259 | return 1; |
| 260 | |
| 261 | return 0; |
| 262 | } |
| 263 | #endif /* ENABLE_EGL */ |
| 264 | |
| 265 | static void |
| 266 | get_n_buffers(struct wl_client *client, struct wl_resource *resource) |
| 267 | { |
| 268 | int n_buffers = 0; |
| 269 | |
| 270 | #ifdef ENABLE_EGL |
| 271 | struct wl_resource *buffer_resource; |
| 272 | int i; |
| 273 | |
| 274 | for (i = 0; i < 1000; i++) { |
| 275 | buffer_resource = wl_client_get_object(client, i); |
| 276 | |
| 277 | if (buffer_resource == NULL) |
| 278 | continue; |
| 279 | |
| 280 | if (is_egl_buffer(buffer_resource)) |
| 281 | n_buffers++; |
| 282 | } |
| 283 | #endif /* ENABLE_EGL */ |
| 284 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 285 | weston_test_send_n_egl_buffers(resource, n_buffers); |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Bryce Harrington | f280d11 | 2015-05-05 15:13:20 -0700 | [diff] [blame] | 288 | enum weston_test_screenshot_outcome { |
| 289 | WESTON_TEST_SCREENSHOT_SUCCESS, |
| 290 | WESTON_TEST_SCREENSHOT_NO_MEMORY, |
| 291 | WESTON_TEST_SCREENSHOT_BAD_BUFFER |
| 292 | }; |
| 293 | |
| 294 | typedef void (*weston_test_screenshot_done_func_t)(void *data, |
| 295 | enum weston_test_screenshot_outcome outcome); |
| 296 | |
| 297 | struct test_screenshot { |
| 298 | struct weston_compositor *compositor; |
| 299 | struct wl_global *global; |
| 300 | struct wl_client *client; |
| 301 | struct weston_process process; |
| 302 | struct wl_listener destroy_listener; |
| 303 | }; |
| 304 | |
| 305 | struct test_screenshot_frame_listener { |
| 306 | struct wl_listener listener; |
| 307 | struct weston_buffer *buffer; |
| 308 | weston_test_screenshot_done_func_t done; |
| 309 | void *data; |
| 310 | }; |
| 311 | |
| 312 | static void |
| 313 | copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height, int stride) |
| 314 | { |
| 315 | uint8_t *end; |
| 316 | |
| 317 | end = dst + height * stride; |
| 318 | while (dst < end) { |
| 319 | memcpy(dst, src, stride); |
| 320 | dst += stride; |
| 321 | src -= stride; |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | |
| 326 | static void |
| 327 | copy_bgra(uint8_t *dst, uint8_t *src, int height, int stride) |
| 328 | { |
| 329 | /* TODO: optimize this out */ |
| 330 | memcpy(dst, src, height * stride); |
| 331 | } |
| 332 | |
| 333 | static void |
| 334 | copy_row_swap_RB(void *vdst, void *vsrc, int bytes) |
| 335 | { |
| 336 | uint32_t *dst = vdst; |
| 337 | uint32_t *src = vsrc; |
| 338 | uint32_t *end = dst + bytes / 4; |
| 339 | |
| 340 | while (dst < end) { |
| 341 | uint32_t v = *src++; |
| 342 | /* A R G B */ |
| 343 | uint32_t tmp = v & 0xff00ff00; |
| 344 | tmp |= (v >> 16) & 0x000000ff; |
| 345 | tmp |= (v << 16) & 0x00ff0000; |
| 346 | *dst++ = tmp; |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | static void |
| 351 | copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height, int stride) |
| 352 | { |
| 353 | uint8_t *end; |
| 354 | |
| 355 | end = dst + height * stride; |
| 356 | while (dst < end) { |
| 357 | copy_row_swap_RB(dst, src, stride); |
| 358 | dst += stride; |
| 359 | src -= stride; |
| 360 | } |
| 361 | } |
| 362 | |
| 363 | static void |
| 364 | copy_rgba(uint8_t *dst, uint8_t *src, int height, int stride) |
| 365 | { |
| 366 | uint8_t *end; |
| 367 | |
| 368 | end = dst + height * stride; |
| 369 | while (dst < end) { |
| 370 | copy_row_swap_RB(dst, src, stride); |
| 371 | dst += stride; |
| 372 | src += stride; |
| 373 | } |
| 374 | } |
| 375 | |
| 376 | static void |
| 377 | test_screenshot_frame_notify(struct wl_listener *listener, void *data) |
| 378 | { |
| 379 | struct test_screenshot_frame_listener *l = |
| 380 | container_of(listener, |
| 381 | struct test_screenshot_frame_listener, listener); |
| 382 | struct weston_output *output = data; |
| 383 | struct weston_compositor *compositor = output->compositor; |
| 384 | int32_t stride; |
| 385 | uint8_t *pixels, *d, *s; |
| 386 | |
| 387 | output->disable_planes--; |
| 388 | wl_list_remove(&listener->link); |
| 389 | stride = l->buffer->width * (PIXMAN_FORMAT_BPP(compositor->read_format) / 8); |
| 390 | pixels = malloc(stride * l->buffer->height); |
| 391 | |
| 392 | if (pixels == NULL) { |
| 393 | l->done(l->data, WESTON_TEST_SCREENSHOT_NO_MEMORY); |
| 394 | free(l); |
| 395 | return; |
| 396 | } |
| 397 | |
Chris Michael | 2ec5f2a | 2015-12-03 12:23:12 -0500 | [diff] [blame] | 398 | /* FIXME: Needs to handle output transformations */ |
Bryce Harrington | f280d11 | 2015-05-05 15:13:20 -0700 | [diff] [blame] | 399 | |
| 400 | compositor->renderer->read_pixels(output, |
| 401 | compositor->read_format, |
| 402 | pixels, |
| 403 | 0, 0, |
| 404 | output->current_mode->width, |
| 405 | output->current_mode->height); |
| 406 | |
| 407 | stride = wl_shm_buffer_get_stride(l->buffer->shm_buffer); |
| 408 | |
| 409 | d = wl_shm_buffer_get_data(l->buffer->shm_buffer); |
| 410 | s = pixels + stride * (l->buffer->height - 1); |
| 411 | |
| 412 | wl_shm_buffer_begin_access(l->buffer->shm_buffer); |
| 413 | |
| 414 | /* XXX: It would be nice if we used Pixman to do all this rather |
| 415 | * than our own implementation |
| 416 | */ |
| 417 | switch (compositor->read_format) { |
| 418 | case PIXMAN_a8r8g8b8: |
| 419 | case PIXMAN_x8r8g8b8: |
| 420 | if (compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP) |
| 421 | copy_bgra_yflip(d, s, output->current_mode->height, stride); |
| 422 | else |
| 423 | copy_bgra(d, pixels, output->current_mode->height, stride); |
| 424 | break; |
| 425 | case PIXMAN_x8b8g8r8: |
| 426 | case PIXMAN_a8b8g8r8: |
| 427 | if (compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP) |
| 428 | copy_rgba_yflip(d, s, output->current_mode->height, stride); |
| 429 | else |
| 430 | copy_rgba(d, pixels, output->current_mode->height, stride); |
| 431 | break; |
| 432 | default: |
| 433 | break; |
| 434 | } |
| 435 | |
| 436 | wl_shm_buffer_end_access(l->buffer->shm_buffer); |
| 437 | |
| 438 | l->done(l->data, WESTON_TEST_SCREENSHOT_SUCCESS); |
| 439 | free(pixels); |
| 440 | free(l); |
| 441 | } |
| 442 | |
| 443 | static bool |
| 444 | weston_test_screenshot_shoot(struct weston_output *output, |
| 445 | struct weston_buffer *buffer, |
| 446 | weston_test_screenshot_done_func_t done, |
| 447 | void *data) |
| 448 | { |
| 449 | struct test_screenshot_frame_listener *l; |
| 450 | |
| 451 | /* Get the shm buffer resource the client created */ |
| 452 | if (!wl_shm_buffer_get(buffer->resource)) { |
| 453 | done(data, WESTON_TEST_SCREENSHOT_BAD_BUFFER); |
| 454 | return false; |
| 455 | } |
| 456 | |
| 457 | buffer->shm_buffer = wl_shm_buffer_get(buffer->resource); |
| 458 | buffer->width = wl_shm_buffer_get_width(buffer->shm_buffer); |
| 459 | buffer->height = wl_shm_buffer_get_height(buffer->shm_buffer); |
| 460 | |
| 461 | /* Verify buffer is big enough */ |
| 462 | if (buffer->width < output->current_mode->width || |
| 463 | buffer->height < output->current_mode->height) { |
| 464 | done(data, WESTON_TEST_SCREENSHOT_BAD_BUFFER); |
| 465 | return false; |
| 466 | } |
| 467 | |
| 468 | /* allocate the frame listener */ |
| 469 | l = malloc(sizeof *l); |
| 470 | if (l == NULL) { |
| 471 | done(data, WESTON_TEST_SCREENSHOT_NO_MEMORY); |
| 472 | return false; |
| 473 | } |
| 474 | |
| 475 | /* Set up the listener */ |
| 476 | l->buffer = buffer; |
| 477 | l->done = done; |
| 478 | l->data = data; |
| 479 | l->listener.notify = test_screenshot_frame_notify; |
| 480 | wl_signal_add(&output->frame_signal, &l->listener); |
| 481 | |
| 482 | /* Fire off a repaint */ |
| 483 | output->disable_planes++; |
| 484 | weston_output_schedule_repaint(output); |
| 485 | |
| 486 | return true; |
| 487 | } |
| 488 | |
| 489 | static void |
| 490 | capture_screenshot_done(void *data, enum weston_test_screenshot_outcome outcome) |
| 491 | { |
| 492 | struct wl_resource *resource = data; |
| 493 | |
| 494 | switch (outcome) { |
| 495 | case WESTON_TEST_SCREENSHOT_SUCCESS: |
| 496 | weston_test_send_capture_screenshot_done(resource); |
| 497 | break; |
| 498 | case WESTON_TEST_SCREENSHOT_NO_MEMORY: |
| 499 | wl_resource_post_no_memory(resource); |
| 500 | break; |
| 501 | default: |
| 502 | break; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | |
| 507 | /** |
| 508 | * Grabs a snapshot of the screen. |
| 509 | */ |
| 510 | static void |
| 511 | capture_screenshot(struct wl_client *client, |
| 512 | struct wl_resource *resource, |
| 513 | struct wl_resource *output_resource, |
| 514 | struct wl_resource *buffer_resource) |
| 515 | { |
| 516 | struct weston_output *output = |
| 517 | wl_resource_get_user_data(output_resource); |
| 518 | struct weston_buffer *buffer = |
| 519 | weston_buffer_from_resource(buffer_resource); |
| 520 | |
| 521 | if (buffer == NULL) { |
| 522 | wl_resource_post_no_memory(resource); |
| 523 | return; |
| 524 | } |
| 525 | |
| 526 | weston_test_screenshot_shoot(output, buffer, |
| 527 | capture_screenshot_done, resource); |
| 528 | } |
| 529 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 530 | static const struct weston_test_interface test_implementation = { |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 531 | move_surface, |
| 532 | move_pointer, |
| 533 | send_button, |
| 534 | activate_surface, |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 535 | send_key, |
Marek Chalupa | c8daf77 | 2015-03-30 06:37:55 -0400 | [diff] [blame] | 536 | device_release, |
| 537 | device_add, |
Neil Roberts | 40c0c3f | 2013-10-29 20:13:45 +0000 | [diff] [blame] | 538 | get_n_buffers, |
Bryce Harrington | f280d11 | 2015-05-05 15:13:20 -0700 | [diff] [blame] | 539 | capture_screenshot, |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 540 | }; |
| 541 | |
| 542 | static void |
| 543 | bind_test(struct wl_client *client, void *data, uint32_t version, uint32_t id) |
| 544 | { |
| 545 | struct weston_test *test = data; |
| 546 | struct wl_resource *resource; |
| 547 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 548 | resource = wl_resource_create(client, &weston_test_interface, 1, id); |
Marek Chalupa | 42ebdda | 2014-07-11 12:33:02 +0200 | [diff] [blame] | 549 | if (!resource) { |
| 550 | wl_client_post_no_memory(client); |
| 551 | return; |
| 552 | } |
| 553 | |
Kristian Høgsberg | 442a5fa | 2013-07-03 18:13:33 -0400 | [diff] [blame] | 554 | wl_resource_set_implementation(resource, |
| 555 | &test_implementation, test, NULL); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 556 | |
| 557 | notify_pointer_position(test, resource); |
| 558 | } |
| 559 | |
| 560 | static void |
| 561 | idle_launch_client(void *data) |
| 562 | { |
| 563 | struct weston_test *test = data; |
| 564 | pid_t pid; |
| 565 | sigset_t allsigs; |
| 566 | char *path; |
| 567 | |
| 568 | path = getenv("WESTON_TEST_CLIENT_PATH"); |
| 569 | if (path == NULL) |
Pekka Paalanen | f72e479 | 2013-11-21 16:23:57 +0200 | [diff] [blame] | 570 | return; |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 571 | pid = fork(); |
| 572 | if (pid == -1) |
| 573 | exit(EXIT_FAILURE); |
| 574 | if (pid == 0) { |
| 575 | sigfillset(&allsigs); |
| 576 | sigprocmask(SIG_UNBLOCK, &allsigs, NULL); |
| 577 | execl(path, path, NULL); |
| 578 | weston_log("compositor: executing '%s' failed: %m\n", path); |
| 579 | exit(EXIT_FAILURE); |
| 580 | } |
| 581 | |
| 582 | test->process.pid = pid; |
| 583 | test->process.cleanup = test_client_sigchld; |
| 584 | weston_watch_process(&test->process); |
| 585 | } |
| 586 | |
| 587 | WL_EXPORT int |
Kristian Høgsberg | cb4685b | 2013-02-20 15:37:49 -0500 | [diff] [blame] | 588 | module_init(struct weston_compositor *ec, |
Ossama Othman | a50e6e4 | 2013-05-14 09:48:26 -0700 | [diff] [blame] | 589 | int *argc, char *argv[]) |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 590 | { |
| 591 | struct weston_test *test; |
| 592 | struct wl_event_loop *loop; |
| 593 | |
Peter Hutterer | f3d6227 | 2013-08-08 11:57:05 +1000 | [diff] [blame] | 594 | test = zalloc(sizeof *test); |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 595 | if (test == NULL) |
| 596 | return -1; |
| 597 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 598 | test->compositor = ec; |
| 599 | weston_layer_init(&test->layer, &ec->cursor_layer.link); |
| 600 | |
Derek Foreman | f6a6592 | 2015-02-24 09:32:14 -0600 | [diff] [blame] | 601 | if (wl_global_create(ec->wl_display, &weston_test_interface, 1, |
Kristian Høgsberg | 919cddb | 2013-07-08 19:03:57 -0400 | [diff] [blame] | 602 | test, bind_test) == NULL) |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 603 | return -1; |
| 604 | |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 605 | /* create our own seat */ |
| 606 | weston_seat_init(&test->seat, ec, "test-seat"); |
| 607 | |
| 608 | /* add devices */ |
| 609 | weston_seat_init_pointer(&test->seat); |
Pekka Paalanen | 315bf8c | 2016-06-16 12:04:53 +0300 | [diff] [blame] | 610 | if (weston_seat_init_keyboard(&test->seat, NULL) < 0) |
| 611 | return -1; |
Marek Chalupa | c3c3fc4 | 2015-03-30 09:17:40 -0400 | [diff] [blame] | 612 | weston_seat_init_touch(&test->seat); |
| 613 | |
U. Artie Eoff | 65e7e7a | 2012-12-07 13:50:29 -0800 | [diff] [blame] | 614 | loop = wl_display_get_event_loop(ec->wl_display); |
| 615 | wl_event_loop_add_idle(loop, idle_launch_client, test); |
| 616 | |
| 617 | return 0; |
| 618 | } |