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
37 #include "shared/os-compatibility.h"
38 #include "shared/xalloc.h"
39 #include "shared/zalloc.h"
40 #include "weston-test-client-helper.h"
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)
47 surface_contains(struct surface *surface, int x, int y)
49 /* test whether a global x,y point is contained in the surface */
52 int sw = surface->width;
53 int sh = surface->height;
54 return x >= sx && y >= sy && x < sx + sw && y < sy + sh;
58 frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time)
64 wl_callback_destroy(callback);
67 static const struct wl_callback_listener frame_listener = {
68 frame_callback_handler
72 frame_callback_set(struct wl_surface *surface, int *done)
74 struct wl_callback *callback;
77 callback = wl_surface_frame(surface);
78 wl_callback_add_listener(callback, &frame_listener, done);
84 frame_callback_wait_nofail(struct client *client, int *done)
87 if (wl_display_dispatch(client->wl_display) < 0)
95 move_client(struct client *client, int x, int y)
97 struct surface *surface = client->surface;
100 client->surface->x = x;
101 client->surface->y = y;
102 weston_test_move_surface(client->test->weston_test, surface->wl_surface,
103 surface->x, surface->y);
104 /* The attach here is necessary because commit() will call configure
105 * only on surfaces newly attached, and the one that sets the surface
106 * position is the configure. */
107 wl_surface_attach(surface->wl_surface, surface->buffer->proxy, 0, 0);
108 wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
111 frame_callback_set(surface->wl_surface, &done);
113 wl_surface_commit(surface->wl_surface);
115 frame_callback_wait(client, &done);
119 pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
120 uint32_t serial, struct wl_surface *wl_surface,
121 wl_fixed_t x, wl_fixed_t y)
123 struct pointer *pointer = data;
126 pointer->focus = wl_surface_get_user_data(wl_surface);
128 pointer->focus = NULL;
130 pointer->x = wl_fixed_to_int(x);
131 pointer->y = wl_fixed_to_int(y);
133 fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n",
134 pointer->x, pointer->y, pointer->focus);
138 pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
139 uint32_t serial, struct wl_surface *wl_surface)
141 struct pointer *pointer = data;
143 pointer->focus = NULL;
145 fprintf(stderr, "test-client: got pointer leave, surface %p\n",
146 wl_surface ? wl_surface_get_user_data(wl_surface) : NULL);
150 pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
151 uint32_t time_msec, wl_fixed_t x, wl_fixed_t y)
153 struct pointer *pointer = data;
155 pointer->x = wl_fixed_to_int(x);
156 pointer->y = wl_fixed_to_int(y);
157 pointer->motion_time_msec = time_msec;
158 pointer->motion_time_timespec = pointer->input_timestamp;
159 pointer->input_timestamp = (struct timespec) { 0 };
161 fprintf(stderr, "test-client: got pointer motion %d %d\n",
162 pointer->x, pointer->y);
166 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
167 uint32_t serial, uint32_t time_msec, uint32_t button,
170 struct pointer *pointer = data;
172 pointer->button = button;
173 pointer->state = state;
174 pointer->button_time_msec = time_msec;
175 pointer->button_time_timespec = pointer->input_timestamp;
176 pointer->input_timestamp = (struct timespec) { 0 };
178 fprintf(stderr, "test-client: got pointer button %u %u\n",
183 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
184 uint32_t time_msec, uint32_t axis, wl_fixed_t value)
186 struct pointer *pointer = data;
188 pointer->axis = axis;
189 pointer->axis_value = wl_fixed_to_double(value);
190 pointer->axis_time_msec = time_msec;
191 pointer->axis_time_timespec = pointer->input_timestamp;
192 pointer->input_timestamp = (struct timespec) { 0 };
194 fprintf(stderr, "test-client: got pointer axis %u %f\n",
195 axis, wl_fixed_to_double(value));
199 pointer_handle_frame(void *data, struct wl_pointer *wl_pointer)
201 fprintf(stderr, "test-client: got pointer frame\n");
205 pointer_handle_axis_source(void *data, struct wl_pointer *wl_pointer,
208 fprintf(stderr, "test-client: got pointer axis source %u\n", source);
212 pointer_handle_axis_stop(void *data, struct wl_pointer *wl_pointer,
213 uint32_t time_msec, uint32_t axis)
215 struct pointer *pointer = data;
217 pointer->axis = axis;
218 pointer->axis_stop_time_msec = time_msec;
219 pointer->axis_stop_time_timespec = pointer->input_timestamp;
220 pointer->input_timestamp = (struct timespec) { 0 };
222 fprintf(stderr, "test-client: got pointer axis stop %u\n", axis);
226 pointer_handle_axis_discrete(void *data, struct wl_pointer *wl_pointer,
227 uint32_t axis, int32_t value)
229 fprintf(stderr, "test-client: got pointer axis discrete %u %d\n",
233 static const struct wl_pointer_listener pointer_listener = {
234 pointer_handle_enter,
235 pointer_handle_leave,
236 pointer_handle_motion,
237 pointer_handle_button,
239 pointer_handle_frame,
240 pointer_handle_axis_source,
241 pointer_handle_axis_stop,
242 pointer_handle_axis_discrete,
246 keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
247 uint32_t format, int fd, uint32_t size)
251 fprintf(stderr, "test-client: got keyboard keymap\n");
255 keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
256 uint32_t serial, struct wl_surface *wl_surface,
257 struct wl_array *keys)
259 struct keyboard *keyboard = data;
262 keyboard->focus = wl_surface_get_user_data(wl_surface);
264 keyboard->focus = NULL;
266 fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
271 keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
272 uint32_t serial, struct wl_surface *wl_surface)
274 struct keyboard *keyboard = data;
276 keyboard->focus = NULL;
278 fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
279 wl_surface ? wl_surface_get_user_data(wl_surface) : NULL);
283 keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
284 uint32_t serial, uint32_t time_msec, uint32_t key,
287 struct keyboard *keyboard = data;
290 keyboard->state = state;
291 keyboard->key_time_msec = time_msec;
292 keyboard->key_time_timespec = keyboard->input_timestamp;
293 keyboard->input_timestamp = (struct timespec) { 0 };
295 fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
299 keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
300 uint32_t serial, uint32_t mods_depressed,
301 uint32_t mods_latched, uint32_t mods_locked,
304 struct keyboard *keyboard = data;
306 keyboard->mods_depressed = mods_depressed;
307 keyboard->mods_latched = mods_latched;
308 keyboard->mods_locked = mods_locked;
309 keyboard->group = group;
311 fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n",
312 mods_depressed, mods_latched, mods_locked, group);
316 keyboard_handle_repeat_info(void *data, struct wl_keyboard *wl_keyboard,
317 int32_t rate, int32_t delay)
319 struct keyboard *keyboard = data;
321 keyboard->repeat_info.rate = rate;
322 keyboard->repeat_info.delay = delay;
324 fprintf(stderr, "test-client: got keyboard repeat_info %d %d\n",
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 fprintf(stderr, "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 fprintf(stderr, "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 fprintf(stderr, "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 fprintf(stderr, "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 fprintf(stderr, "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 fprintf(stderr, "test-client: got surface enter output %p\n",
426 surface_leave(void *data,
427 struct wl_surface *wl_surface, struct wl_output *output)
429 struct surface *surface = data;
431 surface->output = NULL;
433 fprintf(stderr, "test-client: got surface leave output %p\n",
434 wl_output_get_user_data(output));
437 static const struct wl_surface_listener surface_listener = {
442 static struct buffer *
443 create_shm_buffer(struct client *client, int width, int height,
444 pixman_format_code_t format, uint32_t wlfmt)
446 struct wl_shm *shm = client->wl_shm;
449 struct wl_shm_pool *pool;
457 buf = xzalloc(sizeof *buf);
459 bytes_pp = PIXMAN_FORMAT_BPP(format) / 8;
460 stride_bytes = width * bytes_pp;
461 /* round up to multiple of 4 bytes for Pixman */
462 stride_bytes = (stride_bytes + 3) & ~3u;
463 assert(stride_bytes / bytes_pp >= (unsigned)width);
465 buf->len = stride_bytes * height;
466 assert(buf->len / stride_bytes == (unsigned)height);
468 fd = os_create_anonymous_file(buf->len);
471 data = mmap(NULL, buf->len, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
472 if (data == MAP_FAILED) {
474 assert(data != MAP_FAILED);
477 pool = wl_shm_create_pool(shm, fd, buf->len);
478 buf->proxy = wl_shm_pool_create_buffer(pool, 0, width, height,
479 stride_bytes, wlfmt);
480 wl_shm_pool_destroy(pool);
483 buf->image = pixman_image_create_bits(format, width, height,
493 create_shm_buffer_a8r8g8b8(struct client *client, int width, int height)
495 assert(client->has_argb);
497 return create_shm_buffer(client, width, height,
498 PIXMAN_a8r8g8b8, WL_SHM_FORMAT_ARGB8888);
502 buffer_destroy(struct buffer *buf)
506 pixels = pixman_image_get_data(buf->image);
509 wl_buffer_destroy(buf->proxy);
510 assert(munmap(pixels, buf->len) == 0);
513 assert(pixman_image_unref(buf->image));
519 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
521 struct client *client = data;
523 if (format == WL_SHM_FORMAT_ARGB8888)
524 client->has_argb = 1;
527 struct wl_shm_listener shm_listener = {
532 test_handle_pointer_position(void *data, struct weston_test *weston_test,
533 wl_fixed_t x, wl_fixed_t y)
535 struct test *test = data;
536 test->pointer_x = wl_fixed_to_int(x);
537 test->pointer_y = wl_fixed_to_int(y);
539 fprintf(stderr, "test-client: got global pointer %d %d\n",
540 test->pointer_x, test->pointer_y);
544 test_handle_capture_screenshot_done(void *data, struct weston_test *weston_test)
546 struct test *test = data;
548 printf("Screenshot has been captured\n");
549 test->buffer_copy_done = 1;
552 static const struct weston_test_listener test_listener = {
553 test_handle_pointer_position,
554 test_handle_capture_screenshot_done,
558 input_destroy(struct input *inp)
561 wl_pointer_release(inp->pointer->wl_pointer);
565 wl_keyboard_release(inp->keyboard->wl_keyboard);
569 wl_touch_release(inp->touch->wl_touch);
572 wl_list_remove(&inp->link);
573 wl_seat_release(inp->wl_seat);
574 free(inp->seat_name);
579 input_update_devices(struct input *input)
581 struct pointer *pointer;
582 struct keyboard *keyboard;
585 struct wl_seat *seat = input->wl_seat;
586 enum wl_seat_capability caps = input->caps;
588 if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
589 pointer = xzalloc(sizeof *pointer);
590 pointer->wl_pointer = wl_seat_get_pointer(seat);
591 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
592 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
594 input->pointer = pointer;
595 } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
596 wl_pointer_destroy(input->pointer->wl_pointer);
597 free(input->pointer);
598 input->pointer = NULL;
601 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
602 keyboard = xzalloc(sizeof *keyboard);
603 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
604 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
605 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
607 input->keyboard = keyboard;
608 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
609 wl_keyboard_destroy(input->keyboard->wl_keyboard);
610 free(input->keyboard);
611 input->keyboard = NULL;
614 if ((caps & WL_SEAT_CAPABILITY_TOUCH) && !input->touch) {
615 touch = xzalloc(sizeof *touch);
616 touch->wl_touch = wl_seat_get_touch(seat);
617 wl_touch_set_user_data(touch->wl_touch, touch);
618 wl_touch_add_listener(touch->wl_touch, &touch_listener,
620 input->touch = touch;
621 } else if (!(caps & WL_SEAT_CAPABILITY_TOUCH) && input->touch) {
622 wl_touch_destroy(input->touch->wl_touch);
629 seat_handle_capabilities(void *data, struct wl_seat *seat,
630 enum wl_seat_capability caps)
632 struct input *input = data;
636 /* we will create/update the devices only with the right (test) seat.
637 * If we haven't discovered which seat is the test seat, just
638 * store capabilities and bail out */
639 if (input->seat_name && strcmp(input->seat_name, "test-seat") == 0)
640 input_update_devices(input);
642 fprintf(stderr, "test-client: got seat %p capabilities: %x\n",
647 seat_handle_name(void *data, struct wl_seat *seat, const char *name)
649 struct input *input = data;
651 input->seat_name = strdup(name);
652 assert(input->seat_name && "No memory");
654 /* We only update the devices and set client input for the test seat */
655 if (strcmp(name, "test-seat") == 0) {
656 assert(!input->client->input &&
657 "Multiple test seats detected!");
659 input_update_devices(input);
660 input->client->input = input;
663 fprintf(stderr, "test-client: got seat %p name: \'%s\'\n",
667 static const struct wl_seat_listener seat_listener = {
668 seat_handle_capabilities,
673 output_handle_geometry(void *data,
674 struct wl_output *wl_output,
683 struct output *output = data;
690 output_handle_mode(void *data,
691 struct wl_output *wl_output,
697 struct output *output = data;
699 if (flags & WL_OUTPUT_MODE_CURRENT) {
700 output->width = width;
701 output->height = height;
706 output_handle_scale(void *data,
707 struct wl_output *wl_output,
710 struct output *output = data;
712 output->scale = scale;
716 output_handle_done(void *data,
717 struct wl_output *wl_output)
719 struct output *output = data;
721 output->initialized = 1;
724 static const struct wl_output_listener output_listener = {
725 output_handle_geometry,
732 handle_global(void *data, struct wl_registry *registry,
733 uint32_t id, const char *interface, uint32_t version)
735 struct client *client = data;
736 struct output *output;
738 struct global *global;
741 global = xzalloc(sizeof *global);
743 global->interface = strdup(interface);
745 global->version = version;
746 wl_list_insert(client->global_list.prev, &global->link);
748 if (strcmp(interface, "wl_compositor") == 0) {
749 client->wl_compositor =
750 wl_registry_bind(registry, id,
751 &wl_compositor_interface, version);
752 } else if (strcmp(interface, "wl_seat") == 0) {
753 input = xzalloc(sizeof *input);
754 input->client = client;
755 input->global_name = global->name;
757 wl_registry_bind(registry, id,
758 &wl_seat_interface, version);
759 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
760 wl_list_insert(&client->inputs, &input->link);
761 } else if (strcmp(interface, "wl_shm") == 0) {
763 wl_registry_bind(registry, id,
764 &wl_shm_interface, version);
765 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
766 } else if (strcmp(interface, "wl_output") == 0) {
767 output = xzalloc(sizeof *output);
769 wl_registry_bind(registry, id,
770 &wl_output_interface, version);
771 wl_output_add_listener(output->wl_output,
772 &output_listener, output);
773 client->output = output;
774 } else if (strcmp(interface, "weston_test") == 0) {
775 test = xzalloc(sizeof *test);
777 wl_registry_bind(registry, id,
778 &weston_test_interface, version);
779 weston_test_add_listener(test->weston_test, &test_listener, test);
781 } else if (strcmp(interface, "wl_drm") == 0) {
782 client->has_wl_drm = true;
786 static struct global *
787 client_find_global_with_name(struct client *client, uint32_t name)
789 struct global *global;
791 wl_list_for_each(global, &client->global_list, link) {
792 if (global->name == name)
799 static struct input *
800 client_find_input_with_name(struct client *client, uint32_t name)
804 wl_list_for_each(input, &client->inputs, link) {
805 if (input->global_name == name)
813 handle_global_remove(void *data, struct wl_registry *registry, uint32_t name)
815 struct client *client = data;
816 struct global *global;
819 global = client_find_global_with_name(client, name);
820 assert(global && "Request to remove unknown global");
822 if (strcmp(global->interface, "wl_seat") == 0) {
823 input = client_find_input_with_name(client, name);
825 if (client->input == input)
826 client->input = NULL;
827 input_destroy(input);
831 wl_list_remove(&global->link);
832 free(global->interface);
836 static const struct wl_registry_listener registry_listener = {
838 handle_global_remove,
842 skip(const char *fmt, ...)
847 vfprintf(stderr, fmt, argp);
850 /* automake tests uses exit code 77. weston-test-runner will see
851 * this and use it, and then weston-test's sigchld handler (in the
852 * weston process) will use that as an exit status, which is what
853 * automake will see in the end. */
858 expect_protocol_error(struct client *client,
859 const struct wl_interface *intf,
863 uint32_t errcode, failed = 0;
864 const struct wl_interface *interface;
867 /* if the error has not come yet, make it happen */
868 wl_display_roundtrip(client->wl_display);
870 err = wl_display_get_error(client->wl_display);
872 assert(err && "Expected protocol error but nothing came");
873 assert(err == EPROTO && "Expected protocol error but got local error");
875 errcode = wl_display_get_protocol_error(client->wl_display,
879 if (errcode != code) {
880 fprintf(stderr, "Should get error code %d but got %d\n",
885 /* this should be definitely set */
888 if (strcmp(intf->name, interface->name) != 0) {
889 fprintf(stderr, "Should get interface '%s' but got '%s'\n",
890 intf->name, interface->name);
895 fprintf(stderr, "Expected other protocol error\n");
900 fprintf(stderr, "Got expected protocol error on '%s' (object id: %d) "
901 "with code %d\n", interface->name, id, errcode);
905 log_handler(const char *fmt, va_list args)
907 fprintf(stderr, "libwayland: ");
908 vfprintf(stderr, fmt, args);
914 struct client *client;
916 wl_log_set_handler_client(log_handler);
918 /* connect to display */
919 client = xzalloc(sizeof *client);
920 client->wl_display = wl_display_connect(NULL);
921 assert(client->wl_display);
922 wl_list_init(&client->global_list);
923 wl_list_init(&client->inputs);
925 /* setup registry so we can bind to interfaces */
926 client->wl_registry = wl_display_get_registry(client->wl_display);
927 wl_registry_add_listener(client->wl_registry, ®istry_listener,
930 /* this roundtrip makes sure we have all globals and we bound to them */
931 client_roundtrip(client);
932 /* this roundtrip makes sure we got all wl_shm.format and wl_seat.*
934 client_roundtrip(client);
936 /* must have WL_SHM_FORMAT_ARGB32 */
937 assert(client->has_argb);
939 /* must have weston_test interface */
940 assert(client->test);
942 /* must have an output */
943 assert(client->output);
945 /* the output must be initialized */
946 assert(client->output->initialized == 1);
948 /* must have seat set */
949 assert(client->input);
955 create_test_surface(struct client *client)
957 struct surface *surface;
959 surface = xzalloc(sizeof *surface);
961 surface->wl_surface =
962 wl_compositor_create_surface(client->wl_compositor);
963 assert(surface->wl_surface);
965 wl_surface_add_listener(surface->wl_surface, &surface_listener,
968 wl_surface_set_user_data(surface->wl_surface, surface);
974 create_client_and_test_surface(int x, int y, int width, int height)
976 struct client *client;
977 struct surface *surface;
978 pixman_color_t color = { 16384, 16384, 16384, 16384 }; /* uint16_t */
979 pixman_image_t *solid;
981 client = create_client();
983 /* initialize the client surface */
984 surface = create_test_surface(client);
985 client->surface = surface;
987 surface->width = width;
988 surface->height = height;
989 surface->buffer = create_shm_buffer_a8r8g8b8(client, width, height);
991 solid = pixman_image_create_solid_fill(&color);
992 pixman_image_composite32(PIXMAN_OP_SRC,
995 surface->buffer->image, /* dst */
1000 pixman_image_unref(solid);
1002 move_client(client, x, y);
1010 char *path = getenv("WESTON_TEST_OUTPUT_PATH");
1019 screenshot_output_filename(const char *basename, uint32_t seq)
1023 if (asprintf(&filename, "%s/%s-%02d.png",
1024 output_path(), basename, seq) < 0)
1030 reference_path(void)
1032 char *path = getenv("WESTON_TEST_REFERENCE_PATH");
1035 return "./tests/reference";
1040 screenshot_reference_filename(const char *basename, uint32_t seq)
1044 if (asprintf(&filename, "%s/%s-%02d.png",
1045 reference_path(), basename, seq) < 0)
1050 struct format_map_entry {
1051 cairo_format_t cairo;
1052 pixman_format_code_t pixman;
1055 static const struct format_map_entry format_map[] = {
1056 { CAIRO_FORMAT_ARGB32, PIXMAN_a8r8g8b8 },
1057 { CAIRO_FORMAT_RGB24, PIXMAN_x8r8g8b8 },
1058 { CAIRO_FORMAT_A8, PIXMAN_a8 },
1059 { CAIRO_FORMAT_RGB16_565, PIXMAN_r5g6b5 },
1062 static pixman_format_code_t
1063 format_cairo2pixman(cairo_format_t fmt)
1067 for (i = 0; i < ARRAY_LENGTH(format_map); i++)
1068 if (format_map[i].cairo == fmt)
1069 return format_map[i].pixman;
1071 assert(0 && "unknown Cairo pixel format");
1074 static cairo_format_t
1075 format_pixman2cairo(pixman_format_code_t fmt)
1079 for (i = 0; i < ARRAY_LENGTH(format_map); i++)
1080 if (format_map[i].pixman == fmt)
1081 return format_map[i].cairo;
1083 assert(0 && "unknown Pixman pixel format");
1087 * Compute the ROI for image comparisons
1089 * \param img_a An image.
1090 * \param img_b Another image.
1091 * \param clip_rect Explicit ROI, or NULL for using the whole
1094 * \return The region of interest (ROI) that is guaranteed to be inside both
1097 * If clip_rect is given, it must fall inside of both images.
1098 * If clip_rect is NULL, the images must be of the same size.
1099 * If any precondition is violated, this function aborts with an error.
1101 * The ROI is given as pixman_box32_t, where x2,y2 are non-inclusive.
1103 static pixman_box32_t
1104 image_check_get_roi(pixman_image_t *img_a, pixman_image_t *img_b,
1105 const struct rectangle *clip_rect)
1113 width_a = pixman_image_get_width(img_a);
1114 height_a = pixman_image_get_height(img_a);
1116 width_b = pixman_image_get_width(img_b);
1117 height_b = pixman_image_get_height(img_b);
1120 box.x1 = clip_rect->x;
1121 box.y1 = clip_rect->y;
1122 box.x2 = clip_rect->x + clip_rect->width;
1123 box.y2 = clip_rect->y + clip_rect->height;
1127 box.x2 = max(width_a, width_b);
1128 box.y2 = max(height_a, height_b);
1131 assert(box.x1 >= 0);
1132 assert(box.y1 >= 0);
1133 assert(box.x2 > box.x1);
1134 assert(box.y2 > box.y1);
1135 assert(box.x2 <= width_a);
1136 assert(box.x2 <= width_b);
1137 assert(box.y2 <= height_a);
1138 assert(box.y2 <= height_b);
1143 struct image_iterator {
1145 int stride; /* bytes */
1149 image_iter_init(struct image_iterator *it, pixman_image_t *image)
1151 pixman_format_code_t fmt;
1153 it->stride = pixman_image_get_stride(image);
1154 it->data = (void *)pixman_image_get_data(image);
1156 fmt = pixman_image_get_format(image);
1157 assert(PIXMAN_FORMAT_BPP(fmt) == 32);
1161 image_iter_get_row(struct image_iterator *it, int y)
1163 return (uint32_t *)(it->data + y * it->stride);
1167 * Test if a given region within two images are pixel-identical.
1169 * Returns true if the two images pixel-wise identical, and false otherwise.
1171 * \param img_a First image.
1172 * \param img_b Second image.
1173 * \param clip_rect The region of interest, or NULL for comparing the whole
1176 * This function hard-fails if clip_rect is not inside both images. If clip_rect
1177 * is given, the images do not have to match in size, otherwise size mismatch
1178 * will be a hard failure.
1181 check_images_match(pixman_image_t *img_a, pixman_image_t *img_b,
1182 const struct rectangle *clip_rect)
1184 struct image_iterator it_a;
1185 struct image_iterator it_b;
1191 box = image_check_get_roi(img_a, img_b, clip_rect);
1193 image_iter_init(&it_a, img_a);
1194 image_iter_init(&it_b, img_b);
1196 for (y = box.y1; y < box.y2; y++) {
1197 pix_a = image_iter_get_row(&it_a, y) + box.x1;
1198 pix_b = image_iter_get_row(&it_b, y) + box.x1;
1200 for (x = box.x1; x < box.x2; x++) {
1201 if (*pix_a != *pix_b)
1215 * \param src Source pixel as x8r8g8b8.
1216 * \param add The tint as x8r8g8b8, x8 must be zero; r8, g8 and b8 must be
1217 * no greater than 0xc0 to avoid overflow to another channel.
1218 * \return The tinted pixel color as x8r8g8b8, x8 guaranteed to be 0xff.
1220 * The source pixel RGB values are divided by 4, and then the tint is added.
1221 * To achieve colors outside of the range of src, a tint color channel must be
1222 * at least 0x40. (0xff / 4 = 0x3f, 0xff - 0x3f = 0xc0)
1225 tint(uint32_t src, uint32_t add)
1229 v = ((src & 0xfcfcfcfc) >> 2) | 0xff000000;
1235 * Create a visualization of image differences.
1237 * \param img_a First image, which is used as the basis for the output.
1238 * \param img_b Second image.
1239 * \param clip_rect The region of interest, or NULL for comparing the whole
1241 * \return A new image with the differences highlighted.
1243 * Regions outside of the region of interest are shaded with black, matching
1244 * pixels are shaded with green, and differing pixels are shaded with
1247 * This function hard-fails if clip_rect is not inside both images. If clip_rect
1248 * is given, the images do not have to match in size, otherwise size mismatch
1249 * will be a hard failure.
1252 visualize_image_difference(pixman_image_t *img_a, pixman_image_t *img_b,
1253 const struct rectangle *clip_rect)
1255 pixman_image_t *diffimg;
1256 pixman_image_t *shade;
1257 struct image_iterator it_a;
1258 struct image_iterator it_b;
1259 struct image_iterator it_d;
1267 pixman_color_t shade_color = { 0, 0, 0, 32768 };
1269 width = pixman_image_get_width(img_a);
1270 height = pixman_image_get_height(img_a);
1271 box = image_check_get_roi(img_a, img_b, clip_rect);
1273 diffimg = pixman_image_create_bits_no_clear(PIXMAN_x8r8g8b8,
1274 width, height, NULL, 0);
1276 /* Fill diffimg with a black-shaded copy of img_a, and then fill
1277 * the clip_rect area with original img_a.
1279 shade = pixman_image_create_solid_fill(&shade_color);
1280 pixman_image_composite32(PIXMAN_OP_SRC, img_a, shade, diffimg,
1281 0, 0, 0, 0, 0, 0, width, height);
1282 pixman_image_unref(shade);
1283 pixman_image_composite32(PIXMAN_OP_SRC, img_a, NULL, diffimg,
1284 box.x1, box.y1, 0, 0, box.x1, box.y1,
1285 box.x2 - box.x1, box.y2 - box.y1);
1287 image_iter_init(&it_a, img_a);
1288 image_iter_init(&it_b, img_b);
1289 image_iter_init(&it_d, diffimg);
1291 for (y = box.y1; y < box.y2; y++) {
1292 pix_a = image_iter_get_row(&it_a, y) + box.x1;
1293 pix_b = image_iter_get_row(&it_b, y) + box.x1;
1294 pix_d = image_iter_get_row(&it_d, y) + box.x1;
1296 for (x = box.x1; x < box.x2; x++) {
1297 if (*pix_a == *pix_b)
1298 *pix_d = tint(*pix_d, 0x00008000); /* green */
1300 *pix_d = tint(*pix_d, 0x00c00000); /* red */
1312 * Write an image into a PNG file.
1314 * \param image The image.
1315 * \param fname The name and path for the file.
1317 * \returns true if successfully saved file; false otherwise.
1319 * \note Only image formats directly supported by Cairo are accepted, not all
1323 write_image_as_png(pixman_image_t *image, const char *fname)
1325 cairo_surface_t *cairo_surface;
1326 cairo_status_t status;
1329 fmt = format_pixman2cairo(pixman_image_get_format(image));
1331 cairo_surface = cairo_image_surface_create_for_data(
1332 (void *)pixman_image_get_data(image),
1334 pixman_image_get_width(image),
1335 pixman_image_get_height(image),
1336 pixman_image_get_stride(image));
1338 status = cairo_surface_write_to_png(cairo_surface, fname);
1339 if (status != CAIRO_STATUS_SUCCESS) {
1340 fprintf(stderr, "Failed to save image '%s': %s\n", fname,
1341 cairo_status_to_string(status));
1346 cairo_surface_destroy(cairo_surface);
1351 static pixman_image_t *
1352 image_convert_to_a8r8g8b8(pixman_image_t *image)
1354 pixman_image_t *ret;
1358 if (pixman_image_get_format(image) == PIXMAN_a8r8g8b8)
1359 return pixman_image_ref(image);
1361 width = pixman_image_get_width(image);
1362 height = pixman_image_get_height(image);
1364 ret = pixman_image_create_bits_no_clear(PIXMAN_a8r8g8b8, width, height,
1368 pixman_image_composite32(PIXMAN_OP_SRC, image, NULL, ret,
1369 0, 0, 0, 0, 0, 0, width, height);
1375 destroy_cairo_surface(pixman_image_t *image, void *data)
1377 cairo_surface_t *surface = data;
1379 cairo_surface_destroy(surface);
1383 * Load an image from a PNG file
1385 * Reads a PNG image from disk using the given filename (and path)
1386 * and returns as a Pixman image. Use pixman_image_unref() to free it.
1388 * The returned image is always in PIXMAN_a8r8g8b8 format.
1390 * @returns Pixman image, or NULL in case of error.
1393 load_image_from_png(const char *fname)
1395 pixman_image_t *image;
1396 pixman_image_t *converted;
1397 cairo_format_t cairo_fmt;
1398 pixman_format_code_t pixman_fmt;
1399 cairo_surface_t *reference_cairo_surface;
1400 cairo_status_t status;
1406 reference_cairo_surface = cairo_image_surface_create_from_png(fname);
1407 cairo_surface_flush(reference_cairo_surface);
1408 status = cairo_surface_status(reference_cairo_surface);
1409 if (status != CAIRO_STATUS_SUCCESS) {
1410 printf("Could not open %s: %s\n", fname, cairo_status_to_string(status));
1411 cairo_surface_destroy(reference_cairo_surface);
1415 cairo_fmt = cairo_image_surface_get_format(reference_cairo_surface);
1416 pixman_fmt = format_cairo2pixman(cairo_fmt);
1418 width = cairo_image_surface_get_width(reference_cairo_surface);
1419 height = cairo_image_surface_get_height(reference_cairo_surface);
1420 stride = cairo_image_surface_get_stride(reference_cairo_surface);
1421 data = cairo_image_surface_get_data(reference_cairo_surface);
1423 /* The Cairo surface will own the data, so we keep it around. */
1424 image = pixman_image_create_bits_no_clear(pixman_fmt,
1425 width, height, data, stride);
1428 pixman_image_set_destroy_function(image, destroy_cairo_surface,
1429 reference_cairo_surface);
1431 converted = image_convert_to_a8r8g8b8(image);
1432 pixman_image_unref(image);
1438 * Take screenshot of a single output
1440 * Requests a screenshot from the server of the output that the
1441 * client appears on. This implies that the compositor goes through an output
1442 * repaint to provide the screenshot before this function returns. This
1443 * function is therefore both a server roundtrip and a wait for a repaint.
1445 * @returns A new buffer object, that should be freed with buffer_destroy().
1448 capture_screenshot_of_output(struct client *client)
1450 struct buffer *buffer;
1452 buffer = create_shm_buffer_a8r8g8b8(client,
1453 client->output->width,
1454 client->output->height);
1456 client->test->buffer_copy_done = 0;
1457 weston_test_capture_screenshot(client->test->weston_test,
1458 client->output->wl_output,
1460 while (client->test->buffer_copy_done == 0)
1461 if (wl_display_dispatch(client->wl_display) < 0)
1464 /* FIXME: Document somewhere the orientation the screenshot is taken
1465 * and how the clip coords are interpreted, in case of scaling/transform.
1466 * If we're using read_pixels() just make sure it is documented somewhere.
1467 * Protocol docs in the XML, comparison function docs in Doxygen style.