2 * Copyright © 2012 Intel Corporation
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:
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.
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
35 #include "compositor.h"
36 #include "compositor/weston.h"
37 #include "weston-test-server-protocol.h"
41 #include <EGL/eglext.h>
42 #include "weston-egl-ext.h"
43 #endif /* ENABLE_EGL */
45 #include "shared/helpers.h"
46 #include "shared/timespec-util.h"
49 struct weston_compositor *compositor;
50 struct weston_layer layer;
51 struct weston_process process;
52 struct weston_seat seat;
53 bool is_seat_initialized;
56 struct weston_test_surface {
57 struct weston_surface *surface;
58 struct weston_view *view;
60 struct weston_test *test;
64 test_client_sigchld(struct weston_process *process, int status)
66 struct weston_test *test =
67 container_of(process, struct weston_test, process);
69 /* Chain up from weston-test-runner's exit code so that automake
70 * knows the exit status and can report e.g. skipped tests. */
71 if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
72 exit(WEXITSTATUS(status));
74 /* In case the child aborted or segfaulted... */
77 wl_display_terminate(test->compositor->wl_display);
81 test_seat_init(struct weston_test *test)
83 assert(!test->is_seat_initialized &&
84 "Trying to add already added test seat");
86 /* create our own seat */
87 weston_seat_init(&test->seat, test->compositor, "test-seat");
88 test->is_seat_initialized = true;
91 weston_seat_init_pointer(&test->seat);
92 if (weston_seat_init_keyboard(&test->seat, NULL) < 0)
94 weston_seat_init_touch(&test->seat);
100 test_seat_release(struct weston_test *test)
102 assert(test->is_seat_initialized &&
103 "Trying to release already released test seat");
104 test->is_seat_initialized = false;
105 weston_seat_release(&test->seat);
106 memset(&test->seat, 0, sizeof test->seat);
109 static struct weston_seat *
110 get_seat(struct weston_test *test)
116 notify_pointer_position(struct weston_test *test, struct wl_resource *resource)
118 struct weston_seat *seat = get_seat(test);
119 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
121 weston_test_send_pointer_position(resource, pointer->x, pointer->y);
125 test_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
127 struct weston_test_surface *test_surface = surface->committed_private;
128 struct weston_test *test = test_surface->test;
130 if (wl_list_empty(&test_surface->view->layer_link.link))
131 weston_layer_entry_insert(&test->layer.view_list,
132 &test_surface->view->layer_link);
134 weston_view_set_position(test_surface->view,
135 test_surface->x, test_surface->y);
137 weston_view_update_transform(test_surface->view);
139 test_surface->surface->is_mapped = true;
140 test_surface->view->is_mapped = true;
144 move_surface(struct wl_client *client, struct wl_resource *resource,
145 struct wl_resource *surface_resource,
146 int32_t x, int32_t y)
148 struct weston_surface *surface =
149 wl_resource_get_user_data(surface_resource);
150 struct weston_test_surface *test_surface;
152 test_surface = surface->committed_private;
154 test_surface = malloc(sizeof *test_surface);
156 wl_resource_post_no_memory(resource);
160 test_surface->view = weston_view_create(surface);
161 if (!test_surface->view) {
162 wl_resource_post_no_memory(resource);
167 surface->committed_private = test_surface;
168 surface->committed = test_surface_committed;
171 test_surface->surface = surface;
172 test_surface->test = wl_resource_get_user_data(resource);
178 move_pointer(struct wl_client *client, struct wl_resource *resource,
179 uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec,
180 int32_t x, int32_t y)
182 struct weston_test *test = wl_resource_get_user_data(resource);
183 struct weston_seat *seat = get_seat(test);
184 struct weston_pointer *pointer = weston_seat_get_pointer(seat);
185 struct weston_pointer_motion_event event = { 0 };
186 struct timespec time;
188 event = (struct weston_pointer_motion_event) {
189 .mask = WESTON_POINTER_MOTION_REL,
190 .dx = wl_fixed_to_double(wl_fixed_from_int(x) - pointer->x),
191 .dy = wl_fixed_to_double(wl_fixed_from_int(y) - pointer->y),
194 timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
196 notify_motion(seat, &time, &event);
198 notify_pointer_position(test, resource);
202 send_button(struct wl_client *client, struct wl_resource *resource,
203 uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec,
204 int32_t button, uint32_t state)
206 struct timespec time;
208 struct weston_test *test = wl_resource_get_user_data(resource);
209 struct weston_seat *seat = get_seat(test);
211 timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
213 notify_button(seat, &time, button, state);
217 send_axis(struct wl_client *client, struct wl_resource *resource,
218 uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec,
219 uint32_t axis, wl_fixed_t value)
221 struct weston_test *test = wl_resource_get_user_data(resource);
222 struct weston_seat *seat = get_seat(test);
223 struct timespec time;
224 struct weston_pointer_axis_event axis_event;
226 timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
227 axis_event.axis = axis;
228 axis_event.value = wl_fixed_to_double(value);
229 axis_event.has_discrete = false;
230 axis_event.discrete = 0;
232 notify_axis(seat, &time, &axis_event);
236 activate_surface(struct wl_client *client, struct wl_resource *resource,
237 struct wl_resource *surface_resource)
239 struct weston_surface *surface = surface_resource ?
240 wl_resource_get_user_data(surface_resource) : NULL;
241 struct weston_test *test = wl_resource_get_user_data(resource);
242 struct weston_seat *seat;
243 struct weston_keyboard *keyboard;
245 seat = get_seat(test);
246 keyboard = weston_seat_get_keyboard(seat);
248 weston_seat_set_keyboard_focus(seat, surface);
249 notify_keyboard_focus_in(seat, &keyboard->keys,
250 STATE_UPDATE_AUTOMATIC);
253 notify_keyboard_focus_out(seat);
254 weston_seat_set_keyboard_focus(seat, surface);
259 send_key(struct wl_client *client, struct wl_resource *resource,
260 uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec,
261 uint32_t key, enum wl_keyboard_key_state state)
263 struct weston_test *test = wl_resource_get_user_data(resource);
264 struct weston_seat *seat = get_seat(test);
265 struct timespec time;
267 timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
269 notify_key(seat, &time, key, state, STATE_UPDATE_AUTOMATIC);
273 device_release(struct wl_client *client,
274 struct wl_resource *resource, const char *device)
276 struct weston_test *test = wl_resource_get_user_data(resource);
277 struct weston_seat *seat = get_seat(test);
279 if (strcmp(device, "pointer") == 0) {
280 weston_seat_release_pointer(seat);
281 } else if (strcmp(device, "keyboard") == 0) {
282 weston_seat_release_keyboard(seat);
283 } else if (strcmp(device, "touch") == 0) {
284 weston_seat_release_touch(seat);
285 } else if (strcmp(device, "seat") == 0) {
286 test_seat_release(test);
288 assert(0 && "Unsupported device");
293 device_add(struct wl_client *client,
294 struct wl_resource *resource, const char *device)
296 struct weston_test *test = wl_resource_get_user_data(resource);
297 struct weston_seat *seat = get_seat(test);
299 if (strcmp(device, "pointer") == 0) {
300 weston_seat_init_pointer(seat);
301 } else if (strcmp(device, "keyboard") == 0) {
302 weston_seat_init_keyboard(seat, NULL);
303 } else if (strcmp(device, "touch") == 0) {
304 weston_seat_init_touch(seat);
305 } else if (strcmp(device, "seat") == 0) {
306 test_seat_init(test);
308 assert(0 && "Unsupported device");
312 enum weston_test_screenshot_outcome {
313 WESTON_TEST_SCREENSHOT_SUCCESS,
314 WESTON_TEST_SCREENSHOT_NO_MEMORY,
315 WESTON_TEST_SCREENSHOT_BAD_BUFFER
318 typedef void (*weston_test_screenshot_done_func_t)(void *data,
319 enum weston_test_screenshot_outcome outcome);
321 struct test_screenshot {
322 struct weston_compositor *compositor;
323 struct wl_global *global;
324 struct wl_client *client;
325 struct weston_process process;
326 struct wl_listener destroy_listener;
329 struct test_screenshot_frame_listener {
330 struct wl_listener listener;
331 struct weston_buffer *buffer;
332 weston_test_screenshot_done_func_t done;
337 copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
341 end = dst + height * stride;
343 memcpy(dst, src, stride);
351 copy_bgra(uint8_t *dst, uint8_t *src, int height, int stride)
353 /* TODO: optimize this out */
354 memcpy(dst, src, height * stride);
358 copy_row_swap_RB(void *vdst, void *vsrc, int bytes)
360 uint32_t *dst = vdst;
361 uint32_t *src = vsrc;
362 uint32_t *end = dst + bytes / 4;
367 uint32_t tmp = v & 0xff00ff00;
368 tmp |= (v >> 16) & 0x000000ff;
369 tmp |= (v << 16) & 0x00ff0000;
375 copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
379 end = dst + height * stride;
381 copy_row_swap_RB(dst, src, stride);
388 copy_rgba(uint8_t *dst, uint8_t *src, int height, int stride)
392 end = dst + height * stride;
394 copy_row_swap_RB(dst, src, stride);
401 test_screenshot_frame_notify(struct wl_listener *listener, void *data)
403 struct test_screenshot_frame_listener *l =
404 container_of(listener,
405 struct test_screenshot_frame_listener, listener);
406 struct weston_output *output = data;
407 struct weston_compositor *compositor = output->compositor;
409 uint8_t *pixels, *d, *s;
411 output->disable_planes--;
412 wl_list_remove(&listener->link);
413 stride = l->buffer->width * (PIXMAN_FORMAT_BPP(compositor->read_format) / 8);
414 pixels = malloc(stride * l->buffer->height);
416 if (pixels == NULL) {
417 l->done(l->data, WESTON_TEST_SCREENSHOT_NO_MEMORY);
422 /* FIXME: Needs to handle output transformations */
424 compositor->renderer->read_pixels(output,
425 compositor->read_format,
428 output->current_mode->width,
429 output->current_mode->height);
431 stride = wl_shm_buffer_get_stride(l->buffer->shm_buffer);
433 d = wl_shm_buffer_get_data(l->buffer->shm_buffer);
434 s = pixels + stride * (l->buffer->height - 1);
436 wl_shm_buffer_begin_access(l->buffer->shm_buffer);
438 /* XXX: It would be nice if we used Pixman to do all this rather
439 * than our own implementation
441 switch (compositor->read_format) {
442 case PIXMAN_a8r8g8b8:
443 case PIXMAN_x8r8g8b8:
444 if (compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP)
445 copy_bgra_yflip(d, s, output->current_mode->height, stride);
447 copy_bgra(d, pixels, output->current_mode->height, stride);
449 case PIXMAN_x8b8g8r8:
450 case PIXMAN_a8b8g8r8:
451 if (compositor->capabilities & WESTON_CAP_CAPTURE_YFLIP)
452 copy_rgba_yflip(d, s, output->current_mode->height, stride);
454 copy_rgba(d, pixels, output->current_mode->height, stride);
460 wl_shm_buffer_end_access(l->buffer->shm_buffer);
462 l->done(l->data, WESTON_TEST_SCREENSHOT_SUCCESS);
468 weston_test_screenshot_shoot(struct weston_output *output,
469 struct weston_buffer *buffer,
470 weston_test_screenshot_done_func_t done,
473 struct test_screenshot_frame_listener *l;
475 /* Get the shm buffer resource the client created */
476 if (!wl_shm_buffer_get(buffer->resource)) {
477 done(data, WESTON_TEST_SCREENSHOT_BAD_BUFFER);
481 buffer->shm_buffer = wl_shm_buffer_get(buffer->resource);
482 buffer->width = wl_shm_buffer_get_width(buffer->shm_buffer);
483 buffer->height = wl_shm_buffer_get_height(buffer->shm_buffer);
485 /* Verify buffer is big enough */
486 if (buffer->width < output->current_mode->width ||
487 buffer->height < output->current_mode->height) {
488 done(data, WESTON_TEST_SCREENSHOT_BAD_BUFFER);
492 /* allocate the frame listener */
493 l = malloc(sizeof *l);
495 done(data, WESTON_TEST_SCREENSHOT_NO_MEMORY);
499 /* Set up the listener */
503 l->listener.notify = test_screenshot_frame_notify;
504 wl_signal_add(&output->frame_signal, &l->listener);
506 /* Fire off a repaint */
507 output->disable_planes++;
508 weston_output_schedule_repaint(output);
514 capture_screenshot_done(void *data, enum weston_test_screenshot_outcome outcome)
516 struct wl_resource *resource = data;
519 case WESTON_TEST_SCREENSHOT_SUCCESS:
520 weston_test_send_capture_screenshot_done(resource);
522 case WESTON_TEST_SCREENSHOT_NO_MEMORY:
523 wl_resource_post_no_memory(resource);
532 * Grabs a snapshot of the screen.
535 capture_screenshot(struct wl_client *client,
536 struct wl_resource *resource,
537 struct wl_resource *output_resource,
538 struct wl_resource *buffer_resource)
540 struct weston_output *output =
541 weston_head_from_resource(output_resource)->output;
542 struct weston_buffer *buffer =
543 weston_buffer_from_resource(buffer_resource);
545 if (buffer == NULL) {
546 wl_resource_post_no_memory(resource);
550 weston_test_screenshot_shoot(output, buffer,
551 capture_screenshot_done, resource);
555 send_touch(struct wl_client *client, struct wl_resource *resource,
556 uint32_t tv_sec_hi, uint32_t tv_sec_lo, uint32_t tv_nsec,
557 int32_t touch_id, wl_fixed_t x, wl_fixed_t y, uint32_t touch_type)
559 struct weston_test *test = wl_resource_get_user_data(resource);
560 struct weston_seat *seat = get_seat(test);
561 struct timespec time;
563 timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
565 notify_touch(seat, &time, touch_id, wl_fixed_to_double(x),
566 wl_fixed_to_double(y), touch_type);
569 static const struct weston_test_interface test_implementation = {
583 bind_test(struct wl_client *client, void *data, uint32_t version, uint32_t id)
585 struct weston_test *test = data;
586 struct wl_resource *resource;
588 resource = wl_resource_create(client, &weston_test_interface, 1, id);
590 wl_client_post_no_memory(client);
594 wl_resource_set_implementation(resource,
595 &test_implementation, test, NULL);
597 notify_pointer_position(test, resource);
601 idle_launch_client(void *data)
603 struct weston_test *test = data;
608 path = getenv("WESTON_TEST_CLIENT_PATH");
615 sigfillset(&allsigs);
616 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
617 execl(path, path, NULL);
618 weston_log("compositor: executing '%s' failed: %m\n", path);
622 test->process.pid = pid;
623 test->process.cleanup = test_client_sigchld;
624 weston_watch_process(&test->process);
628 wet_module_init(struct weston_compositor *ec,
629 int *argc, char *argv[])
631 struct weston_test *test;
632 struct wl_event_loop *loop;
634 test = zalloc(sizeof *test);
638 test->compositor = ec;
639 weston_layer_init(&test->layer, ec);
640 weston_layer_set_position(&test->layer, WESTON_LAYER_POSITION_CURSOR - 1);
642 if (wl_global_create(ec->wl_display, &weston_test_interface, 1,
643 test, bind_test) == NULL)
646 if (test_seat_init(test) == -1)
649 loop = wl_display_get_event_loop(ec->wl_display);
650 wl_event_loop_add_idle(loop, idle_launch_client, test);