2 * Copyright © 2012 Intel Corporation
3 * Copyright © 2015 Samsung Electronics Co., Ltd
4 * Copyright 2016, 2017 Collabora, Ltd.
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial
16 * portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
39 #include "test-config.h"
40 #include "shared/os-compatibility.h"
41 #include "shared/xalloc.h"
42 #include <libweston/zalloc.h>
43 #include "weston-test-client-helper.h"
45 #define max(a, b) (((a) > (b)) ? (a) : (b))
46 #define min(a, b) (((a) > (b)) ? (b) : (a))
47 #define clip(x, a, b) min(max(x, a), b)
50 surface_contains(struct surface *surface, int x, int y)
52 /* test whether a global x,y point is contained in the surface */
55 int sw = surface->width;
56 int sh = surface->height;
57 return x >= sx && y >= sy && x < sx + sw && y < sy + sh;
61 frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time)
67 wl_callback_destroy(callback);
70 static const struct wl_callback_listener frame_listener = {
71 frame_callback_handler
75 frame_callback_set(struct wl_surface *surface, int *done)
77 struct wl_callback *callback;
80 callback = wl_surface_frame(surface);
81 wl_callback_add_listener(callback, &frame_listener, done);
87 frame_callback_wait_nofail(struct client *client, int *done)
90 if (wl_display_dispatch(client->wl_display) < 0)
98 move_client(struct client *client, int x, int y)
100 struct surface *surface = client->surface;
103 client->surface->x = x;
104 client->surface->y = y;
105 weston_test_move_surface(client->test->weston_test, surface->wl_surface,
106 surface->x, surface->y);
107 /* The attach here is necessary because commit() will call configure
108 * only on surfaces newly attached, and the one that sets the surface
109 * position is the configure. */
110 wl_surface_attach(surface->wl_surface, surface->buffer->proxy, 0, 0);
111 wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
114 frame_callback_set(surface->wl_surface, &done);
116 wl_surface_commit(surface->wl_surface);
118 frame_callback_wait(client, &done);
122 pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
123 uint32_t serial, struct wl_surface *wl_surface,
124 wl_fixed_t x, wl_fixed_t y)
126 struct pointer *pointer = data;
129 pointer->focus = wl_surface_get_user_data(wl_surface);
131 pointer->focus = NULL;
133 pointer->x = wl_fixed_to_int(x);
134 pointer->y = wl_fixed_to_int(y);
136 testlog("test-client: got pointer enter %d %d, surface %p\n",
137 pointer->x, pointer->y, pointer->focus);
141 pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
142 uint32_t serial, struct wl_surface *wl_surface)
144 struct pointer *pointer = data;
146 pointer->focus = NULL;
148 testlog("test-client: got pointer leave, surface %p\n",
149 wl_surface ? wl_surface_get_user_data(wl_surface) : NULL);
153 pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
154 uint32_t time_msec, wl_fixed_t x, wl_fixed_t y)
156 struct pointer *pointer = data;
158 pointer->x = wl_fixed_to_int(x);
159 pointer->y = wl_fixed_to_int(y);
160 pointer->motion_time_msec = time_msec;
161 pointer->motion_time_timespec = pointer->input_timestamp;
162 pointer->input_timestamp = (struct timespec) { 0 };
164 testlog("test-client: got pointer motion %d %d\n",
165 pointer->x, pointer->y);
169 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
170 uint32_t serial, uint32_t time_msec, uint32_t button,
173 struct pointer *pointer = data;
175 pointer->button = button;
176 pointer->state = state;
177 pointer->button_time_msec = time_msec;
178 pointer->button_time_timespec = pointer->input_timestamp;
179 pointer->input_timestamp = (struct timespec) { 0 };
181 testlog("test-client: got pointer button %u %u\n", button, state);
185 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
186 uint32_t time_msec, uint32_t axis, wl_fixed_t value)
188 struct pointer *pointer = data;
190 pointer->axis = axis;
191 pointer->axis_value = wl_fixed_to_double(value);
192 pointer->axis_time_msec = time_msec;
193 pointer->axis_time_timespec = pointer->input_timestamp;
194 pointer->input_timestamp = (struct timespec) { 0 };
196 testlog("test-client: got pointer axis %u %f\n",
197 axis, wl_fixed_to_double(value));
201 pointer_handle_frame(void *data, struct wl_pointer *wl_pointer)
203 testlog("test-client: got pointer frame\n");
207 pointer_handle_axis_source(void *data, struct wl_pointer *wl_pointer,
210 testlog("test-client: got pointer axis source %u\n", source);
214 pointer_handle_axis_stop(void *data, struct wl_pointer *wl_pointer,
215 uint32_t time_msec, uint32_t axis)
217 struct pointer *pointer = data;
219 pointer->axis = axis;
220 pointer->axis_stop_time_msec = time_msec;
221 pointer->axis_stop_time_timespec = pointer->input_timestamp;
222 pointer->input_timestamp = (struct timespec) { 0 };
224 testlog("test-client: got pointer axis stop %u\n", axis);
228 pointer_handle_axis_discrete(void *data, struct wl_pointer *wl_pointer,
229 uint32_t axis, int32_t value)
231 testlog("test-client: got pointer axis discrete %u %d\n", axis, value);
234 static const struct wl_pointer_listener pointer_listener = {
235 pointer_handle_enter,
236 pointer_handle_leave,
237 pointer_handle_motion,
238 pointer_handle_button,
240 pointer_handle_frame,
241 pointer_handle_axis_source,
242 pointer_handle_axis_stop,
243 pointer_handle_axis_discrete,
247 keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
248 uint32_t format, int fd, uint32_t size)
252 testlog("test-client: got keyboard keymap\n");
256 keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
257 uint32_t serial, struct wl_surface *wl_surface,
258 struct wl_array *keys)
260 struct keyboard *keyboard = data;
263 keyboard->focus = wl_surface_get_user_data(wl_surface);
265 keyboard->focus = NULL;
267 testlog("test-client: got keyboard enter, surface %p\n",
272 keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
273 uint32_t serial, struct wl_surface *wl_surface)
275 struct keyboard *keyboard = data;
277 keyboard->focus = NULL;
279 testlog("test-client: got keyboard leave, surface %p\n",
280 wl_surface ? wl_surface_get_user_data(wl_surface) : NULL);
284 keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
285 uint32_t serial, uint32_t time_msec, uint32_t key,
288 struct keyboard *keyboard = data;
291 keyboard->state = state;
292 keyboard->key_time_msec = time_msec;
293 keyboard->key_time_timespec = keyboard->input_timestamp;
294 keyboard->input_timestamp = (struct timespec) { 0 };
296 testlog("test-client: got keyboard key %u %u\n", key, state);
300 keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
301 uint32_t serial, uint32_t mods_depressed,
302 uint32_t mods_latched, uint32_t mods_locked,
305 struct keyboard *keyboard = data;
307 keyboard->mods_depressed = mods_depressed;
308 keyboard->mods_latched = mods_latched;
309 keyboard->mods_locked = mods_locked;
310 keyboard->group = group;
312 testlog("test-client: got keyboard modifiers %u %u %u %u\n",
313 mods_depressed, mods_latched, mods_locked, group);
317 keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
318 int32_t rate, int32_t delay)
320 struct keyboard *keyboard = data;
322 keyboard->repeat_info.rate = rate;
323 keyboard->repeat_info.delay = delay;
325 testlog("test-client: got keyboard repeat_info %d %d\n", rate, delay);
328 static const struct wl_keyboard_listener keyboard_listener = {
329 keyboard_handle_keymap,
330 keyboard_handle_enter,
331 keyboard_handle_leave,
333 keyboard_handle_modifiers,
334 keyboard_handle_repeat_info,
338 touch_handle_down(void *data, struct wl_touch *wl_touch,
339 uint32_t serial, uint32_t time_msec,
340 struct wl_surface *surface, int32_t id,
341 wl_fixed_t x_w, wl_fixed_t y_w)
343 struct touch *touch = data;
345 touch->down_x = wl_fixed_to_int(x_w);
346 touch->down_y = wl_fixed_to_int(y_w);
348 touch->down_time_msec = time_msec;
349 touch->down_time_timespec = touch->input_timestamp;
350 touch->input_timestamp = (struct timespec) { 0 };
352 testlog("test-client: got touch down %d %d, surf: %p, id: %d\n",
353 touch->down_x, touch->down_y, surface, id);
357 touch_handle_up(void *data, struct wl_touch *wl_touch,
358 uint32_t serial, uint32_t time_msec, int32_t id)
360 struct touch *touch = data;
362 touch->up_time_msec = time_msec;
363 touch->up_time_timespec = touch->input_timestamp;
364 touch->input_timestamp = (struct timespec) { 0 };
366 testlog("test-client: got touch up, id: %d\n", id);
370 touch_handle_motion(void *data, struct wl_touch *wl_touch,
371 uint32_t time_msec, int32_t id,
372 wl_fixed_t x_w, wl_fixed_t y_w)
374 struct touch *touch = data;
375 touch->x = wl_fixed_to_int(x_w);
376 touch->y = wl_fixed_to_int(y_w);
377 touch->motion_time_msec = time_msec;
378 touch->motion_time_timespec = touch->input_timestamp;
379 touch->input_timestamp = (struct timespec) { 0 };
381 testlog("test-client: got touch motion, %d %d, id: %d\n",
382 touch->x, touch->y, id);
386 touch_handle_frame(void *data, struct wl_touch *wl_touch)
388 struct touch *touch = data;
392 testlog("test-client: got touch frame (%d)\n", touch->frame_no);
396 touch_handle_cancel(void *data, struct wl_touch *wl_touch)
398 struct touch *touch = data;
402 testlog("test-client: got touch cancel (%d)\n", touch->cancel_no);
405 static const struct wl_touch_listener touch_listener = {
414 surface_enter(void *data,
415 struct wl_surface *wl_surface, struct wl_output *output)
417 struct surface *surface = data;
419 surface->output = wl_output_get_user_data(output);
421 testlog("test-client: got surface enter output %p\n", surface->output);
425 surface_leave(void *data,
426 struct wl_surface *wl_surface, struct wl_output *output)
428 struct surface *surface = data;
430 surface->output = NULL;
432 testlog("test-client: got surface leave output %p\n",
433 wl_output_get_user_data(output));
436 static const struct wl_surface_listener surface_listener = {
441 static struct buffer *
442 create_shm_buffer(struct client *client, int width, int height,
443 pixman_format_code_t format, uint32_t wlfmt)
445 struct wl_shm *shm = client->wl_shm;
448 struct wl_shm_pool *pool;
456 buf = xzalloc(sizeof *buf);
458 bytes_pp = PIXMAN_FORMAT_BPP(format) / 8;
459 stride_bytes = width * bytes_pp;
460 /* round up to multiple of 4 bytes for Pixman */
461 stride_bytes = (stride_bytes + 3) & ~3u;
462 assert(stride_bytes / bytes_pp >= (unsigned)width);
464 buf->len = stride_bytes * height;
465 assert(buf->len / stride_bytes == (unsigned)height);
467 fd = os_create_anonymous_file(buf->len);
470 data = mmap(NULL, buf->len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
471 if (data == MAP_FAILED) {
473 assert(data != MAP_FAILED);
476 pool = wl_shm_create_pool(shm, fd, buf->len);
477 buf->proxy = wl_shm_pool_create_buffer(pool, 0, width, height,
478 stride_bytes, wlfmt);
479 wl_shm_pool_destroy(pool);
482 buf->image = pixman_image_create_bits(format, width, height,
492 create_shm_buffer_a8r8g8b8(struct client *client, int width, int height)
494 assert(client->has_argb);
496 return create_shm_buffer(client, width, height,
497 PIXMAN_a8r8g8b8, WL_SHM_FORMAT_ARGB8888);
501 buffer_destroy(struct buffer *buf)
505 pixels = pixman_image_get_data(buf->image);
508 wl_buffer_destroy(buf->proxy);
509 assert(munmap(pixels, buf->len) == 0);
512 assert(pixman_image_unref(buf->image));
518 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
520 struct client *client = data;
522 if (format == WL_SHM_FORMAT_ARGB8888)
523 client->has_argb = 1;
526 struct wl_shm_listener shm_listener = {
531 test_handle_pointer_position(void *data, struct weston_test *weston_test,
532 wl_fixed_t x, wl_fixed_t y)
534 struct test *test = data;
535 test->pointer_x = wl_fixed_to_int(x);
536 test->pointer_y = wl_fixed_to_int(y);
538 testlog("test-client: got global pointer %d %d\n",
539 test->pointer_x, test->pointer_y);
543 test_handle_capture_screenshot_done(void *data, struct weston_test *weston_test)
545 struct test *test = data;
547 testlog("Screenshot has been captured\n");
548 test->buffer_copy_done = 1;
551 static const struct weston_test_listener test_listener = {
552 test_handle_pointer_position,
553 test_handle_capture_screenshot_done,
557 input_destroy(struct input *inp)
560 wl_pointer_release(inp->pointer->wl_pointer);
564 wl_keyboard_release(inp->keyboard->wl_keyboard);
568 wl_touch_release(inp->touch->wl_touch);
571 wl_list_remove(&inp->link);
572 wl_seat_release(inp->wl_seat);
573 free(inp->seat_name);
578 input_update_devices(struct input *input)
580 struct pointer *pointer;
581 struct keyboard *keyboard;
584 struct wl_seat *seat = input->wl_seat;
585 enum wl_seat_capability caps = input->caps;
587 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
588 pointer = xzalloc(sizeof *pointer);
589 pointer->wl_pointer = wl_seat_get_pointer(seat);
590 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
591 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
593 input->pointer = pointer;
594 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
595 wl_pointer_destroy(input->pointer->wl_pointer);
596 free(input->pointer);
597 input->pointer = NULL;
600 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
601 keyboard = xzalloc(sizeof *keyboard);
602 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
603 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
604 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
606 input->keyboard = keyboard;
607 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
608 wl_keyboard_destroy(input->keyboard->wl_keyboard);
609 free(input->keyboard);
610 input->keyboard = NULL;
613 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
614 touch = xzalloc(sizeof *touch);
615 touch->wl_touch = wl_seat_get_touch(seat);
616 wl_touch_set_user_data(touch->wl_touch, touch);
617 wl_touch_add_listener(touch->wl_touch, &touch_listener,
619 input->touch = touch;
620 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
621 wl_touch_destroy(input->touch->wl_touch);
628 seat_handle_capabilities(void *data, struct wl_seat *seat,
629 enum wl_seat_capability caps)
631 struct input *input = data;
635 /* we will create/update the devices only with the right (test) seat.
636 * If we haven't discovered which seat is the test seat, just
637 * store capabilities and bail out */
638 if (input->seat_name && strcmp(input->seat_name, "test-seat") == 0)
639 input_update_devices(input);
641 testlog("test-client: got seat %p capabilities: %x\n", input, caps);
645 seat_handle_name(void *data, struct wl_seat *seat, const char *name)
647 struct input *input = data;
649 input->seat_name = strdup(name);
650 assert(input->seat_name && "No memory");
652 /* We only update the devices and set client input for the test seat */
653 if (strcmp(name, "test-seat") == 0) {
654 assert(!input->client->input &&
655 "Multiple test seats detected!");
657 input_update_devices(input);
658 input->client->input = input;
661 testlog("test-client: got seat %p name: \'%s\'\n", input, name);
664 static const struct wl_seat_listener seat_listener = {
665 seat_handle_capabilities,
670 output_handle_geometry(void *data,
671 struct wl_output *wl_output,
680 struct output *output = data;
687 output_handle_mode(void *data,
688 struct wl_output *wl_output,
694 struct output *output = data;
696 if (flags & WL_OUTPUT_MODE_CURRENT) {
697 output->width = width;
698 output->height = height;
703 output_handle_scale(void *data,
704 struct wl_output *wl_output,
707 struct output *output = data;
709 output->scale = scale;
713 output_handle_done(void *data,
714 struct wl_output *wl_output)
716 struct output *output = data;
718 output->initialized = 1;
721 static const struct wl_output_listener output_listener = {
722 output_handle_geometry,
729 output_destroy(struct output *output)
731 assert(wl_proxy_get_version((struct wl_proxy *)output->wl_output) >= 3);
732 wl_output_release(output->wl_output);
733 wl_list_remove(&output->link);
738 handle_global(void *data, struct wl_registry *registry,
739 uint32_t id, const char *interface, uint32_t version)
741 struct client *client = data;
742 struct output *output;
744 struct global *global;
747 global = xzalloc(sizeof *global);
749 global->interface = strdup(interface);
751 global->version = version;
752 wl_list_insert(client->global_list.prev, &global->link);
754 /* We deliberately bind all globals with the maximum (advertised)
755 * version, because this test suite must be kept up-to-date with
756 * Weston. We must always implement at least the version advertised
757 * by Weston. This is not ok for normal clients, but it is ok in
761 if (strcmp(interface, "wl_compositor") == 0) {
762 client->wl_compositor =
763 wl_registry_bind(registry, id,
764 &wl_compositor_interface, version);
765 } else if (strcmp(interface, "wl_seat") == 0) {
766 input = xzalloc(sizeof *input);
767 input->client = client;
768 input->global_name = global->name;
770 wl_registry_bind(registry, id,
771 &wl_seat_interface, version);
772 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
773 wl_list_insert(&client->inputs, &input->link);
774 } else if (strcmp(interface, "wl_shm") == 0) {
776 wl_registry_bind(registry, id,
777 &wl_shm_interface, version);
778 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
779 } else if (strcmp(interface, "wl_output") == 0) {
780 output = xzalloc(sizeof *output);
782 wl_registry_bind(registry, id,
783 &wl_output_interface, version);
784 wl_output_add_listener(output->wl_output,
785 &output_listener, output);
786 wl_list_insert(&client->output_list, &output->link);
787 client->output = output;
788 } else if (strcmp(interface, "weston_test") == 0) {
789 test = xzalloc(sizeof *test);
791 wl_registry_bind(registry, id,
792 &weston_test_interface, version);
793 weston_test_add_listener(test->weston_test, &test_listener, test);
795 } else if (strcmp(interface, "wl_drm") == 0) {
796 client->has_wl_drm = true;
800 static struct global *
801 client_find_global_with_name(struct client *client, uint32_t name)
803 struct global *global;
805 wl_list_for_each(global, &client->global_list, link) {
806 if (global->name == name)
813 static struct input *
814 client_find_input_with_name(struct client *client, uint32_t name)
818 wl_list_for_each(input, &client->inputs, link) {
819 if (input->global_name == name)
827 global_destroy(struct global *global)
829 wl_list_remove(&global->link);
830 free(global->interface);
835 handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
837 struct client *client = data;
838 struct global *global;
841 global = client_find_global_with_name(client, name);
842 assert(global && "Request to remove unknown global");
844 if (strcmp(global->interface, "wl_seat") == 0) {
845 input = client_find_input_with_name(client, name);
847 if (client->input == input)
848 client->input = NULL;
849 input_destroy(input);
853 /* XXX: handle wl_output */
855 global_destroy(global);
858 static const struct wl_registry_listener registry_listener = {
860 handle_global_remove,
864 skip(const char *fmt, ...)
869 vfprintf(stderr, fmt, argp);
872 /* automake tests uses exit code 77. weston-test-runner will see
873 * this and use it, and then weston-test's sigchld handler (in the
874 * weston process) will use that as an exit status, which is what
875 * ninja will see in the end. */
880 expect_protocol_error(struct client *client,
881 const struct wl_interface *intf,
885 uint32_t errcode, failed = 0;
886 const struct wl_interface *interface;
889 /* if the error has not come yet, make it happen */
890 wl_display_roundtrip(client->wl_display);
892 err = wl_display_get_error(client->wl_display);
894 assert(err && "Expected protocol error but nothing came");
895 assert(err == EPROTO && "Expected protocol error but got local error");
897 errcode = wl_display_get_protocol_error(client->wl_display,
901 if (errcode != code) {
902 testlog("Should get error code %d but got %d\n", code, errcode);
906 /* this should be definitely set */
909 if (strcmp(intf->name, interface->name) != 0) {
910 testlog("Should get interface '%s' but got '%s'\n",
911 intf->name, interface->name);
916 testlog("Expected other protocol error\n");
921 testlog("Got expected protocol error on '%s' (object id: %d) "
922 "with code %d\n", interface->name, id, errcode);
926 log_handler(const char *fmt, va_list args)
928 fprintf(stderr, "libwayland: ");
929 vfprintf(stderr, fmt, args);
935 struct client *client;
937 wl_log_set_handler_client(log_handler);
939 /* connect to display */
940 client = xzalloc(sizeof *client);
941 client->wl_display = wl_display_connect(NULL);
942 assert(client->wl_display);
943 wl_list_init(&client->global_list);
944 wl_list_init(&client->inputs);
945 wl_list_init(&client->output_list);
947 /* setup registry so we can bind to interfaces */
948 client->wl_registry = wl_display_get_registry(client->wl_display);
949 wl_registry_add_listener(client->wl_registry, ®istry_listener,
952 /* this roundtrip makes sure we have all globals and we bound to them */
953 client_roundtrip(client);
954 /* this roundtrip makes sure we got all wl_shm.format and wl_seat.*
956 client_roundtrip(client);
958 /* must have WL_SHM_FORMAT_ARGB32 */
959 assert(client->has_argb);
961 /* must have weston_test interface */
962 assert(client->test);
964 /* must have an output */
965 assert(client->output);
967 /* the output must be initialized */
968 assert(client->output->initialized == 1);
970 /* must have seat set */
971 assert(client->input);
977 create_test_surface(struct client *client)
979 struct surface *surface;
981 surface = xzalloc(sizeof *surface);
983 surface->wl_surface =
984 wl_compositor_create_surface(client->wl_compositor);
985 assert(surface->wl_surface);
987 wl_surface_add_listener(surface->wl_surface, &surface_listener,
990 wl_surface_set_user_data(surface->wl_surface, surface);
996 surface_destroy(struct surface *surface)
998 if (surface->wl_surface)
999 wl_surface_destroy(surface->wl_surface);
1000 if (surface->buffer)
1001 buffer_destroy(surface->buffer);
1006 create_client_and_test_surface(int x, int y, int width, int height)
1008 struct client *client;
1009 struct surface *surface;
1010 pixman_color_t color = { 16384, 16384, 16384, 16384 }; /* uint16_t */
1011 pixman_image_t *solid;
1013 client = create_client();
1015 /* initialize the client surface */
1016 surface = create_test_surface(client);
1017 client->surface = surface;
1019 surface->width = width;
1020 surface->height = height;
1021 surface->buffer = create_shm_buffer_a8r8g8b8(client, width, height);
1023 solid = pixman_image_create_solid_fill(&color);
1024 pixman_image_composite32(PIXMAN_OP_SRC,
1027 surface->buffer->image, /* dst */
1029 0, 0, /* mask x,y */
1032 pixman_image_unref(solid);
1034 move_client(client, x, y);
1040 client_destroy(struct client *client)
1042 if (client->surface)
1043 surface_destroy(client->surface);
1045 while (!wl_list_empty(&client->inputs)) {
1046 input_destroy(container_of(client->inputs.next,
1047 struct input, link));
1050 while (!wl_list_empty(&client->output_list)) {
1051 output_destroy(container_of(client->output_list.next,
1052 struct output, link));
1055 while (!wl_list_empty(&client->global_list)) {
1056 global_destroy(container_of(client->global_list.next,
1057 struct global, link));
1061 weston_test_destroy(client->test->weston_test);
1066 wl_shm_destroy(client->wl_shm);
1067 if (client->wl_compositor)
1068 wl_compositor_destroy(client->wl_compositor);
1069 if (client->wl_registry)
1070 wl_registry_destroy(client->wl_registry);
1072 client_roundtrip(client);
1074 if (client->wl_display)
1075 wl_display_disconnect(client->wl_display);
1082 char *path = getenv("WESTON_TEST_OUTPUT_PATH");
1091 screenshot_output_filename(const char *basename, uint32_t seq)
1095 if (asprintf(&filename, "%s/%s-%02d.png",
1096 output_path(), basename, seq) < 0)
1102 reference_path(void)
1104 char *path = getenv("WESTON_TEST_REFERENCE_PATH");
1107 return WESTON_TEST_REFERENCE_PATH;
1112 screenshot_reference_filename(const char *basename, uint32_t seq)
1116 if (asprintf(&filename, "%s/%s-%02d.png",
1117 reference_path(), basename, seq) < 0)
1123 image_filename(const char *basename)
1127 if (asprintf(&filename, "%s/%s.png", reference_path(), basename) < 0)
1132 struct format_map_entry {
1133 cairo_format_t cairo;
1134 pixman_format_code_t pixman;
1137 static const struct format_map_entry format_map[] = {
1138 { CAIRO_FORMAT_ARGB32, PIXMAN_a8r8g8b8 },
1139 { CAIRO_FORMAT_RGB24, PIXMAN_x8r8g8b8 },
1140 { CAIRO_FORMAT_A8, PIXMAN_a8 },
1141 { CAIRO_FORMAT_RGB16_565, PIXMAN_r5g6b5 },
1144 static pixman_format_code_t
1145 format_cairo2pixman(cairo_format_t fmt)
1149 for (i = 0; i < ARRAY_LENGTH(format_map); i++)
1150 if (format_map[i].cairo == fmt)
1151 return format_map[i].pixman;
1153 assert(0 && "unknown Cairo pixel format");
1156 static cairo_format_t
1157 format_pixman2cairo(pixman_format_code_t fmt)
1161 for (i = 0; i < ARRAY_LENGTH(format_map); i++)
1162 if (format_map[i].pixman == fmt)
1163 return format_map[i].cairo;
1165 assert(0 && "unknown Pixman pixel format");
1171 * \param r Range to validate or NULL.
1172 * \return The given range, or {0, 0} for NULL.
1174 * Will abort if range is invalid, that is a > b.
1177 range_get(const struct range *r)
1180 return (struct range){ 0, 0 };
1182 assert(r->a <= r->b);
1187 * Compute the ROI for image comparisons
1189 * \param img_a An image.
1190 * \param img_b Another image.
1191 * \param clip_rect Explicit ROI, or NULL for using the whole
1194 * \return The region of interest (ROI) that is guaranteed to be inside both
1197 * If clip_rect is given, it must fall inside of both images.
1198 * If clip_rect is NULL, the images must be of the same size.
1199 * If any precondition is violated, this function aborts with an error.
1201 * The ROI is given as pixman_box32_t, where x2,y2 are non-inclusive.
1203 static pixman_box32_t
1204 image_check_get_roi(pixman_image_t *img_a, pixman_image_t *img_b,
1205 const struct rectangle *clip_rect)
1213 width_a = pixman_image_get_width(img_a);
1214 height_a = pixman_image_get_height(img_a);
1216 width_b = pixman_image_get_width(img_b);
1217 height_b = pixman_image_get_height(img_b);
1220 box.x1 = clip_rect->x;
1221 box.y1 = clip_rect->y;
1222 box.x2 = clip_rect->x + clip_rect->width;
1223 box.y2 = clip_rect->y + clip_rect->height;
1227 box.x2 = max(width_a, width_b);
1228 box.y2 = max(height_a, height_b);
1231 assert(box.x1 >= 0);
1232 assert(box.y1 >= 0);
1233 assert(box.x2 > box.x1);
1234 assert(box.y2 > box.y1);
1235 assert(box.x2 <= width_a);
1236 assert(box.x2 <= width_b);
1237 assert(box.y2 <= height_a);
1238 assert(box.y2 <= height_b);
1243 struct image_iterator {
1245 int stride; /* bytes */
1249 image_iter_init(struct image_iterator *it, pixman_image_t *image)
1251 pixman_format_code_t fmt;
1253 it->stride = pixman_image_get_stride(image);
1254 it->data = (void *)pixman_image_get_data(image);
1256 fmt = pixman_image_get_format(image);
1257 assert(PIXMAN_FORMAT_BPP(fmt) == 32);
1261 image_iter_get_row(struct image_iterator *it, int y)
1263 return (uint32_t *)(it->data + y * it->stride);
1266 struct pixel_diff_stat {
1267 struct pixel_diff_stat_channel {
1274 testlog_pixel_diff_stat(const struct pixel_diff_stat *stat)
1278 testlog("Image difference statistics:\n");
1279 for (i = 0; i < 4; i++) {
1280 testlog("\tch %d: [%d, %d]\n",
1281 i, stat->ch[i].min_diff, stat->ch[i].max_diff);
1286 fuzzy_match_pixels(uint32_t pix_a, uint32_t pix_b,
1287 const struct range *fuzz,
1288 struct pixel_diff_stat *stat)
1294 for (shift = 0, i = 0; i < 4; shift += 8, i++) {
1295 int val_a = (pix_a >> shift) & 0xffu;
1296 int val_b = (pix_b >> shift) & 0xffu;
1297 int d = val_b - val_a;
1299 stat->ch[i].min_diff = min(stat->ch[i].min_diff, d);
1300 stat->ch[i].max_diff = max(stat->ch[i].max_diff, d);
1302 if (d < fuzz->a || d > fuzz->b)
1310 * Test if a given region within two images are pixel-identical
1312 * Returns true if the two images pixel-wise identical, and false otherwise.
1314 * \param img_a First image.
1315 * \param img_b Second image.
1316 * \param clip_rect The region of interest, or NULL for comparing the whole
1318 * \param prec Per-channel allowed difference, or NULL for identical match
1321 * This function hard-fails if clip_rect is not inside both images. If clip_rect
1322 * is given, the images do not have to match in size, otherwise size mismatch
1323 * will be a hard failure.
1325 * The per-pixel, per-channel difference is computed as img_b - img_a which is
1326 * required to be in the range [prec->a, prec->b] inclusive. The difference is
1327 * signed. All four channels are compared the same way, without any special
1328 * meaning on alpha channel.
1331 check_images_match(pixman_image_t *img_a, pixman_image_t *img_b,
1332 const struct rectangle *clip_rect, const struct range *prec)
1334 struct range fuzz = range_get(prec);
1335 struct pixel_diff_stat diffstat = {};
1336 struct image_iterator it_a;
1337 struct image_iterator it_b;
1343 box = image_check_get_roi(img_a, img_b, clip_rect);
1345 image_iter_init(&it_a, img_a);
1346 image_iter_init(&it_b, img_b);
1348 for (y = box.y1; y < box.y2; y++) {
1349 pix_a = image_iter_get_row(&it_a, y) + box.x1;
1350 pix_b = image_iter_get_row(&it_b, y) + box.x1;
1352 for (x = box.x1; x < box.x2; x++) {
1353 if (!fuzzy_match_pixels(*pix_a, *pix_b,
1368 * \param src Source pixel as x8r8g8b8.
1369 * \param add The tint as x8r8g8b8, x8 must be zero; r8, g8 and b8 must be
1370 * no greater than 0xc0 to avoid overflow to another channel.
1371 * \return The tinted pixel color as x8r8g8b8, x8 guaranteed to be 0xff.
1373 * The source pixel RGB values are divided by 4, and then the tint is added.
1374 * To achieve colors outside of the range of src, a tint color channel must be
1375 * at least 0x40. (0xff / 4 = 0x3f, 0xff - 0x3f = 0xc0)
1378 tint(uint32_t src, uint32_t add)
1382 v = ((src & 0xfcfcfcfc) >> 2) | 0xff000000;
1388 * Create a visualization of image differences.
1390 * \param img_a First image, which is used as the basis for the output.
1391 * \param img_b Second image.
1392 * \param clip_rect The region of interest, or NULL for comparing the whole
1394 * \param prec Per-channel allowed difference, or NULL for identical match
1396 * \return A new image with the differences highlighted.
1398 * Regions outside of the region of interest are shaded with black, matching
1399 * pixels are shaded with green, and differing pixels are shaded with
1402 * This function hard-fails if clip_rect is not inside both images. If clip_rect
1403 * is given, the images do not have to match in size, otherwise size mismatch
1404 * will be a hard failure.
1406 * The per-pixel, per-channel difference is computed as img_b - img_a which is
1407 * required to be in the range [prec->a, prec->b] inclusive. The difference is
1408 * signed. All four channels are compared the same way, without any special
1409 * meaning on alpha channel.
1412 visualize_image_difference(pixman_image_t *img_a, pixman_image_t *img_b,
1413 const struct rectangle *clip_rect,
1414 const struct range *prec)
1416 struct range fuzz = range_get(prec);
1417 struct pixel_diff_stat diffstat = {};
1418 pixman_image_t *diffimg;
1419 pixman_image_t *shade;
1420 struct image_iterator it_a;
1421 struct image_iterator it_b;
1422 struct image_iterator it_d;
1430 pixman_color_t shade_color = { 0, 0, 0, 32768 };
1432 width = pixman_image_get_width(img_a);
1433 height = pixman_image_get_height(img_a);
1434 box = image_check_get_roi(img_a, img_b, clip_rect);
1436 diffimg = pixman_image_create_bits_no_clear(PIXMAN_x8r8g8b8,
1437 width, height, NULL, 0);
1439 /* Fill diffimg with a black-shaded copy of img_a, and then fill
1440 * the clip_rect area with original img_a.
1442 shade = pixman_image_create_solid_fill(&shade_color);
1443 pixman_image_composite32(PIXMAN_OP_SRC, img_a, shade, diffimg,
1444 0, 0, 0, 0, 0, 0, width, height);
1445 pixman_image_unref(shade);
1446 pixman_image_composite32(PIXMAN_OP_SRC, img_a, NULL, diffimg,
1447 box.x1, box.y1, 0, 0, box.x1, box.y1,
1448 box.x2 - box.x1, box.y2 - box.y1);
1450 image_iter_init(&it_a, img_a);
1451 image_iter_init(&it_b, img_b);
1452 image_iter_init(&it_d, diffimg);
1454 for (y = box.y1; y < box.y2; y++) {
1455 pix_a = image_iter_get_row(&it_a, y) + box.x1;
1456 pix_b = image_iter_get_row(&it_b, y) + box.x1;
1457 pix_d = image_iter_get_row(&it_d, y) + box.x1;
1459 for (x = box.x1; x < box.x2; x++) {
1460 if (fuzzy_match_pixels(*pix_a, *pix_b,
1462 *pix_d = tint(*pix_d, 0x00008000); /* green */
1464 *pix_d = tint(*pix_d, 0x00c00000); /* red */
1472 testlog_pixel_diff_stat(&diffstat);
1478 * Write an image into a PNG file.
1480 * \param image The image.
1481 * \param fname The name and path for the file.
1483 * \returns true if successfully saved file; false otherwise.
1485 * \note Only image formats directly supported by Cairo are accepted, not all
1489 write_image_as_png(pixman_image_t *image, const char *fname)
1491 cairo_surface_t *cairo_surface;
1492 cairo_status_t status;
1495 fmt = format_pixman2cairo(pixman_image_get_format(image));
1497 cairo_surface = cairo_image_surface_create_for_data(
1498 (void *)pixman_image_get_data(image),
1500 pixman_image_get_width(image),
1501 pixman_image_get_height(image),
1502 pixman_image_get_stride(image));
1504 status = cairo_surface_write_to_png(cairo_surface, fname);
1505 if (status != CAIRO_STATUS_SUCCESS) {
1506 testlog("Failed to save image '%s': %s\n", fname,
1507 cairo_status_to_string(status));
1512 cairo_surface_destroy(cairo_surface);
1517 static pixman_image_t *
1518 image_convert_to_a8r8g8b8(pixman_image_t *image)
1520 pixman_image_t *ret;
1524 if (pixman_image_get_format(image) == PIXMAN_a8r8g8b8)
1525 return pixman_image_ref(image);
1527 width = pixman_image_get_width(image);
1528 height = pixman_image_get_height(image);
1530 ret = pixman_image_create_bits_no_clear(PIXMAN_a8r8g8b8, width, height,
1534 pixman_image_composite32(PIXMAN_OP_SRC, image, NULL, ret,
1535 0, 0, 0, 0, 0, 0, width, height);
1541 destroy_cairo_surface(pixman_image_t *image, void *data)
1543 cairo_surface_t *surface = data;
1545 cairo_surface_destroy(surface);
1549 * Load an image from a PNG file
1551 * Reads a PNG image from disk using the given filename (and path)
1552 * and returns as a Pixman image. Use pixman_image_unref() to free it.
1554 * The returned image is always in PIXMAN_a8r8g8b8 format.
1556 * @returns Pixman image, or NULL in case of error.
1559 load_image_from_png(const char *fname)
1561 pixman_image_t *image;
1562 pixman_image_t *converted;
1563 cairo_format_t cairo_fmt;
1564 pixman_format_code_t pixman_fmt;
1565 cairo_surface_t *reference_cairo_surface;
1566 cairo_status_t status;
1572 reference_cairo_surface = cairo_image_surface_create_from_png(fname);
1573 cairo_surface_flush(reference_cairo_surface);
1574 status = cairo_surface_status(reference_cairo_surface);
1575 if (status != CAIRO_STATUS_SUCCESS) {
1576 testlog("Could not open %s: %s\n", fname,
1577 cairo_status_to_string(status));
1578 cairo_surface_destroy(reference_cairo_surface);
1582 cairo_fmt = cairo_image_surface_get_format(reference_cairo_surface);
1583 pixman_fmt = format_cairo2pixman(cairo_fmt);
1585 width = cairo_image_surface_get_width(reference_cairo_surface);
1586 height = cairo_image_surface_get_height(reference_cairo_surface);
1587 stride = cairo_image_surface_get_stride(reference_cairo_surface);
1588 data = cairo_image_surface_get_data(reference_cairo_surface);
1590 /* The Cairo surface will own the data, so we keep it around. */
1591 image = pixman_image_create_bits_no_clear(pixman_fmt,
1592 width, height, data, stride);
1595 pixman_image_set_destroy_function(image, destroy_cairo_surface,
1596 reference_cairo_surface);
1598 converted = image_convert_to_a8r8g8b8(image);
1599 pixman_image_unref(image);
1605 * Take screenshot of a single output
1607 * Requests a screenshot from the server of the output that the
1608 * client appears on. This implies that the compositor goes through an output
1609 * repaint to provide the screenshot before this function returns. This
1610 * function is therefore both a server roundtrip and a wait for a repaint.
1612 * @returns A new buffer object, that should be freed with buffer_destroy().
1615 capture_screenshot_of_output(struct client *client)
1617 struct buffer *buffer;
1619 buffer = create_shm_buffer_a8r8g8b8(client,
1620 client->output->width,
1621 client->output->height);
1623 client->test->buffer_copy_done = 0;
1624 weston_test_capture_screenshot(client->test->weston_test,
1625 client->output->wl_output,
1627 while (client->test->buffer_copy_done == 0)
1628 if (wl_display_dispatch(client->wl_display) < 0)
1631 /* FIXME: Document somewhere the orientation the screenshot is taken
1632 * and how the clip coords are interpreted, in case of scaling/transform.
1633 * If we're using read_pixels() just make sure it is documented somewhere.
1634 * Protocol docs in the XML, comparison function docs in Doxygen style.
1641 write_visual_diff(pixman_image_t *ref_image,
1642 struct buffer *shot,
1643 const struct rectangle *clip,
1644 const char *test_name,
1646 const struct range *fuzz)
1649 char *ext_test_name;
1650 pixman_image_t *diff;
1653 ret = asprintf(&ext_test_name, "%s-diff", test_name);
1656 fname = screenshot_output_filename(ext_test_name, seq_no);
1657 diff = visualize_image_difference(ref_image, shot->image, clip, fuzz);
1658 write_image_as_png(diff, fname);
1660 pixman_image_unref(diff);
1662 free(ext_test_name);
1666 * Take a screenshot and verify its contents
1668 * Takes a screenshot and writes the image into a PNG file named with
1669 * get_test_name() and seq_no. Compares the contents to the given reference
1670 * image over the given clip rectangle, reports whether they match to the
1671 * test log, and if they do not match writes a visual diff into a PNG file.
1673 * The compositor output size and the reference image size must both contain
1674 * the clip rectangle.
1676 * This function uses the pixel value allowed fuzz approriate for GL-renderer
1677 * with 8 bits per channel data.
1679 * \param client The client, for connecting to the compositor.
1680 * \param ref_image The reference image file basename, without sequence number
1682 * \param ref_seq_no The reference image sequence number.
1683 * \param clip The region of interest, or NULL for comparing the whole
1685 * \param seq_no Test sequence number, for writing output files.
1686 * \return True if the screen contents matches the reference image,
1689 * For bootstrapping, ref_image can be NULL or the file can be missing.
1690 * In that case the screenshot file is written but no comparison is performed,
1691 * and false is returned.
1694 verify_screen_content(struct client *client,
1695 const char *ref_image,
1697 const struct rectangle *clip,
1700 const char *test_name = get_test_name();
1701 const struct range gl_fuzz = { -3, 4 };
1702 struct buffer *shot;
1703 pixman_image_t *ref = NULL;
1704 char *ref_fname = NULL;
1708 shot = capture_screenshot_of_output(client);
1710 shot_fname = screenshot_output_filename(test_name, seq_no);
1711 write_image_as_png(shot->image, shot_fname);
1714 ref_fname = screenshot_reference_filename(ref_image, ref_seq_no);
1715 ref = load_image_from_png(ref_fname);
1719 match = check_images_match(ref, shot->image, clip, &gl_fuzz);
1720 testlog("Verify reference image %s vs. shot %s: %s\n",
1721 ref_fname, shot_fname, match ? "PASS" : "FAIL");
1724 write_visual_diff(ref, shot, clip,
1725 test_name, seq_no, &gl_fuzz);
1728 pixman_image_unref(ref);
1730 testlog("No reference image, shot %s: FAIL\n", shot_fname);
1735 buffer_destroy(shot);
1742 * Create a wl_buffer from a PNG file
1744 * Loads the named PNG file from the directory of reference images,
1745 * creates a wl_buffer with scale times the image dimensions in pixels,
1746 * and copies the image content into the buffer using nearest-neighbor filter.
1748 * \param client The client, for the Wayland connection.
1749 * \param basename The PNG file name without .png suffix.
1750 * \param scale Upscaling factor >= 1.
1753 client_buffer_from_image_file(struct client *client,
1754 const char *basename,
1759 pixman_image_t *img;
1761 pixman_transform_t scaling;
1765 fname = image_filename(basename);
1766 img = load_image_from_png(fname);
1770 buf_w = scale * pixman_image_get_width(img);
1771 buf_h = scale * pixman_image_get_height(img);
1772 buf = create_shm_buffer_a8r8g8b8(client, buf_w, buf_h);
1774 pixman_transform_init_scale(&scaling,
1775 pixman_fixed_1 / scale,
1776 pixman_fixed_1 / scale);
1777 pixman_image_set_transform(img, &scaling);
1778 pixman_image_set_filter(img, PIXMAN_FILTER_NEAREST, NULL, 0);
1780 pixman_image_composite32(PIXMAN_OP_SRC,
1783 buf->image, /* dst */
1785 0, 0, /* mask x,y */
1788 pixman_image_unref(img);
1794 * Bind to a singleton global in wl_registry
1796 * \param client Client whose registry and globals to use.
1797 * \param iface The Wayland interface to look for.
1798 * \param version The version to bind the interface with.
1799 * \return A struct wl_proxy, which you need to cast to the proper type.
1801 * Asserts that the global being searched for is a singleton and is found.
1803 * Binds with the exact version given, does not take compositor interface
1804 * version into account.
1807 bind_to_singleton_global(struct client *client,
1808 const struct wl_interface *iface,
1812 struct global *g = NULL;
1813 struct wl_proxy *proxy;
1815 wl_list_for_each(tmp, &client->global_list, link) {
1816 if (strcmp(tmp->interface, iface->name))
1819 assert(!g && "multiple singleton objects");
1823 assert(g && "singleton not found");
1825 proxy = wl_registry_bind(client->wl_registry, g->name, iface, version);
1832 * Create a wp_viewport for the client surface
1834 * \param client The client->surface to use.
1835 * \return A fresh viewport object.
1837 struct wp_viewport *
1838 client_create_viewport(struct client *client)
1840 struct wp_viewporter *viewporter;
1841 struct wp_viewport *viewport;
1843 viewporter = bind_to_singleton_global(client,
1844 &wp_viewporter_interface, 1);
1845 viewport = wp_viewporter_get_viewport(viewporter,
1846 client->surface->wl_surface);
1848 wp_viewporter_destroy(viewporter);
1854 * Fill the image with the given color
1856 * \param image The image to write to.
1857 * \param color The color to use.
1860 fill_image_with_color(pixman_image_t *image, pixman_color_t *color)
1862 pixman_image_t *solid;
1866 width = pixman_image_get_width(image);
1867 height = pixman_image_get_height(image);
1869 solid = pixman_image_create_solid_fill(color);
1870 pixman_image_composite32(PIXMAN_OP_SRC,
1875 0, 0, /* mask x,y */
1878 pixman_image_unref(solid);
1882 * Convert 8-bit RGB to opaque Pixman color
1884 * \param tmp Pixman color struct to fill in.
1885 * \param r Red value, 0 - 255.
1886 * \param g Green value, 0 - 255.
1887 * \param b Blue value, 0 - 255.
1891 color_rgb888(pixman_color_t *tmp, uint8_t r, uint8_t g, uint8_t b)
1894 tmp->red = (r << 8) + r;
1895 tmp->green = (g << 8) + g;
1896 tmp->blue = (b << 8) + b;