ae08b02f5419b6dcff9824067167ba78a584168d
[platform/upstream/weston.git] / tests / weston-test.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
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:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25
26 #include "config.h"
27
28 #include <stdint.h>
29 #include <stdlib.h>
30 #include <assert.h>
31 #include <signal.h>
32 #include <unistd.h>
33 #include <string.h>
34
35 #include "compositor.h"
36 #include "compositor/weston.h"
37 #include "weston-test-server-protocol.h"
38
39 #ifdef ENABLE_EGL
40 #include <EGL/egl.h>
41 #include <EGL/eglext.h>
42 #include "weston-egl-ext.h"
43 #endif /* ENABLE_EGL */
44
45 #include "shared/helpers.h"
46 #include "shared/timespec-util.h"
47
48 struct weston_test {
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;
54 };
55
56 struct weston_test_surface {
57         struct weston_surface *surface;
58         struct weston_view *view;
59         int32_t x, y;
60         struct weston_test *test;
61 };
62
63 static void
64 test_client_sigchld(struct weston_process *process, int status)
65 {
66         struct weston_test *test =
67                 container_of(process, struct weston_test, process);
68
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));
73
74         /* In case the child aborted or segfaulted... */
75         assert(status == 0);
76
77         wl_display_terminate(test->compositor->wl_display);
78 }
79
80 static int
81 test_seat_init(struct weston_test *test)
82 {
83         assert(!test->is_seat_initialized &&
84                "Trying to add already added test seat");
85
86         /* create our own seat */
87         weston_seat_init(&test->seat, test->compositor, "test-seat");
88         test->is_seat_initialized = true;
89
90         /* add devices */
91         weston_seat_init_pointer(&test->seat);
92         if (weston_seat_init_keyboard(&test->seat, NULL) < 0)
93                 return -1;
94         weston_seat_init_touch(&test->seat);
95
96         return 0;
97 }
98
99 static void
100 test_seat_release(struct weston_test *test)
101 {
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);
107 }
108
109 static struct weston_seat *
110 get_seat(struct weston_test *test)
111 {
112         return &test->seat;
113 }
114
115 static void
116 notify_pointer_position(struct weston_test *test, struct wl_resource *resource)
117 {
118         struct weston_seat *seat = get_seat(test);
119         struct weston_pointer *pointer = weston_seat_get_pointer(seat);
120
121         weston_test_send_pointer_position(resource, pointer->x, pointer->y);
122 }
123
124 static void
125 test_surface_committed(struct weston_surface *surface, int32_t sx, int32_t sy)
126 {
127         struct weston_test_surface *test_surface = surface->committed_private;
128         struct weston_test *test = test_surface->test;
129
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);
133
134         weston_view_set_position(test_surface->view,
135                                  test_surface->x, test_surface->y);
136
137         weston_view_update_transform(test_surface->view);
138
139         test_surface->surface->is_mapped = true;
140         test_surface->view->is_mapped = true;
141 }
142
143 static void
144 move_surface(struct wl_client *client, struct wl_resource *resource,
145              struct wl_resource *surface_resource,
146              int32_t x, int32_t y)
147 {
148         struct weston_surface *surface =
149                 wl_resource_get_user_data(surface_resource);
150         struct weston_test_surface *test_surface;
151
152         test_surface = surface->committed_private;
153         if (!test_surface) {
154                 test_surface = malloc(sizeof *test_surface);
155                 if (!test_surface) {
156                         wl_resource_post_no_memory(resource);
157                         return;
158                 }
159
160                 test_surface->view = weston_view_create(surface);
161                 if (!test_surface->view) {
162                         wl_resource_post_no_memory(resource);
163                         free(test_surface);
164                         return;
165                 }
166
167                 surface->committed_private = test_surface;
168                 surface->committed = test_surface_committed;
169         }
170
171         test_surface->surface = surface;
172         test_surface->test = wl_resource_get_user_data(resource);
173         test_surface->x = x;
174         test_surface->y = y;
175 }
176
177 static void
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)
181 {
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;
187
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),
192         };
193
194         timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
195
196         notify_motion(seat, &time, &event);
197
198         notify_pointer_position(test, resource);
199 }
200
201 static void
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)
205 {
206         struct timespec time;
207
208         struct weston_test *test = wl_resource_get_user_data(resource);
209         struct weston_seat *seat = get_seat(test);
210
211         timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
212
213         notify_button(seat, &time, button, state);
214 }
215
216 static void
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)
220 {
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;
225
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;
231
232         notify_axis(seat, &time, &axis_event);
233 }
234
235 static void
236 activate_surface(struct wl_client *client, struct wl_resource *resource,
237                  struct wl_resource *surface_resource)
238 {
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;
244
245         seat = get_seat(test);
246         keyboard = weston_seat_get_keyboard(seat);
247         if (surface) {
248                 weston_seat_set_keyboard_focus(seat, surface);
249                 notify_keyboard_focus_in(seat, &keyboard->keys,
250                                          STATE_UPDATE_AUTOMATIC);
251         }
252         else {
253                 notify_keyboard_focus_out(seat);
254                 weston_seat_set_keyboard_focus(seat, surface);
255         }
256 }
257
258 static void
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)
262 {
263         struct weston_test *test = wl_resource_get_user_data(resource);
264         struct weston_seat *seat = get_seat(test);
265         struct timespec time;
266
267         timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
268
269         notify_key(seat, &time, key, state, STATE_UPDATE_AUTOMATIC);
270 }
271
272 static void
273 device_release(struct wl_client *client,
274                struct wl_resource *resource, const char *device)
275 {
276         struct weston_test *test = wl_resource_get_user_data(resource);
277         struct weston_seat *seat = get_seat(test);
278
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);
287         } else {
288                 assert(0 && "Unsupported device");
289         }
290 }
291
292 static void
293 device_add(struct wl_client *client,
294            struct wl_resource *resource, const char *device)
295 {
296         struct weston_test *test = wl_resource_get_user_data(resource);
297         struct weston_seat *seat = get_seat(test);
298
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);
307         } else {
308                 assert(0 && "Unsupported device");
309         }
310 }
311
312 enum weston_test_screenshot_outcome {
313         WESTON_TEST_SCREENSHOT_SUCCESS,
314         WESTON_TEST_SCREENSHOT_NO_MEMORY,
315         WESTON_TEST_SCREENSHOT_BAD_BUFFER
316         };
317
318 typedef void (*weston_test_screenshot_done_func_t)(void *data,
319                                                    enum weston_test_screenshot_outcome outcome);
320
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;
327 };
328
329 struct test_screenshot_frame_listener {
330         struct wl_listener listener;
331         struct weston_buffer *buffer;
332         weston_test_screenshot_done_func_t done;
333         void *data;
334 };
335
336 static void
337 copy_bgra_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
338 {
339         uint8_t *end;
340
341         end = dst + height * stride;
342         while (dst < end) {
343                 memcpy(dst, src, stride);
344                 dst += stride;
345                 src -= stride;
346         }
347 }
348
349
350 static void
351 copy_bgra(uint8_t *dst, uint8_t *src, int height, int stride)
352 {
353         /* TODO: optimize this out */
354         memcpy(dst, src, height * stride);
355 }
356
357 static void
358 copy_row_swap_RB(void *vdst, void *vsrc, int bytes)
359 {
360         uint32_t *dst = vdst;
361         uint32_t *src = vsrc;
362         uint32_t *end = dst + bytes / 4;
363
364         while (dst < end) {
365                 uint32_t v = *src++;
366                 /*                    A R G B */
367                 uint32_t tmp = v & 0xff00ff00;
368                 tmp |= (v >> 16) & 0x000000ff;
369                 tmp |= (v << 16) & 0x00ff0000;
370                 *dst++ = tmp;
371         }
372 }
373
374 static void
375 copy_rgba_yflip(uint8_t *dst, uint8_t *src, int height, int stride)
376 {
377         uint8_t *end;
378
379         end = dst + height * stride;
380         while (dst < end) {
381                 copy_row_swap_RB(dst, src, stride);
382                 dst += stride;
383                 src -= stride;
384         }
385 }
386
387 static void
388 copy_rgba(uint8_t *dst, uint8_t *src, int height, int stride)
389 {
390         uint8_t *end;
391
392         end = dst + height * stride;
393         while (dst < end) {
394                 copy_row_swap_RB(dst, src, stride);
395                 dst += stride;
396                 src += stride;
397         }
398 }
399
400 static void
401 test_screenshot_frame_notify(struct wl_listener *listener, void *data)
402 {
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;
408         int32_t stride;
409         uint8_t *pixels, *d, *s;
410
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);
415
416         if (pixels == NULL) {
417                 l->done(l->data, WESTON_TEST_SCREENSHOT_NO_MEMORY);
418                 free(l);
419                 return;
420         }
421
422         /* FIXME: Needs to handle output transformations */
423
424         compositor->renderer->read_pixels(output,
425                                           compositor->read_format,
426                                           pixels,
427                                           0, 0,
428                                           output->current_mode->width,
429                                           output->current_mode->height);
430
431         stride = wl_shm_buffer_get_stride(l->buffer->shm_buffer);
432
433         d = wl_shm_buffer_get_data(l->buffer->shm_buffer);
434         s = pixels + stride * (l->buffer->height - 1);
435
436         wl_shm_buffer_begin_access(l->buffer->shm_buffer);
437
438         /* XXX: It would be nice if we used Pixman to do all this rather
439          *  than our own implementation
440          */
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);
446                 else
447                         copy_bgra(d, pixels, output->current_mode->height, stride);
448                 break;
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);
453                 else
454                         copy_rgba(d, pixels, output->current_mode->height, stride);
455                 break;
456         default:
457                 break;
458         }
459
460         wl_shm_buffer_end_access(l->buffer->shm_buffer);
461
462         l->done(l->data, WESTON_TEST_SCREENSHOT_SUCCESS);
463         free(pixels);
464         free(l);
465 }
466
467 static bool
468 weston_test_screenshot_shoot(struct weston_output *output,
469                              struct weston_buffer *buffer,
470                              weston_test_screenshot_done_func_t done,
471                              void *data)
472 {
473         struct test_screenshot_frame_listener *l;
474
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);
478                 return false;
479         }
480
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);
484
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);
489                 return false;
490         }
491
492         /* allocate the frame listener */
493         l = malloc(sizeof *l);
494         if (l == NULL) {
495                 done(data, WESTON_TEST_SCREENSHOT_NO_MEMORY);
496                 return false;
497         }
498
499         /* Set up the listener */
500         l->buffer = buffer;
501         l->done = done;
502         l->data = data;
503         l->listener.notify = test_screenshot_frame_notify;
504         wl_signal_add(&output->frame_signal, &l->listener);
505
506         /* Fire off a repaint */
507         output->disable_planes++;
508         weston_output_schedule_repaint(output);
509
510         return true;
511 }
512
513 static void
514 capture_screenshot_done(void *data, enum weston_test_screenshot_outcome outcome)
515 {
516         struct wl_resource *resource = data;
517
518         switch (outcome) {
519         case WESTON_TEST_SCREENSHOT_SUCCESS:
520                 weston_test_send_capture_screenshot_done(resource);
521                 break;
522         case WESTON_TEST_SCREENSHOT_NO_MEMORY:
523                 wl_resource_post_no_memory(resource);
524                 break;
525         default:
526                 break;
527         }
528 }
529
530
531 /**
532  * Grabs a snapshot of the screen.
533  */
534 static void
535 capture_screenshot(struct wl_client *client,
536                    struct wl_resource *resource,
537                    struct wl_resource *output_resource,
538                    struct wl_resource *buffer_resource)
539 {
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);
544
545         if (buffer == NULL) {
546                 wl_resource_post_no_memory(resource);
547                 return;
548         }
549
550         weston_test_screenshot_shoot(output, buffer,
551                                      capture_screenshot_done, resource);
552 }
553
554 static void
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)
558 {
559         struct weston_test *test = wl_resource_get_user_data(resource);
560         struct weston_seat *seat = get_seat(test);
561         struct timespec time;
562
563         timespec_from_proto(&time, tv_sec_hi, tv_sec_lo, tv_nsec);
564
565         notify_touch(seat, &time, touch_id, wl_fixed_to_double(x),
566                      wl_fixed_to_double(y), touch_type);
567 }
568
569 static const struct weston_test_interface test_implementation = {
570         move_surface,
571         move_pointer,
572         send_button,
573         send_axis,
574         activate_surface,
575         send_key,
576         device_release,
577         device_add,
578         capture_screenshot,
579         send_touch,
580 };
581
582 static void
583 bind_test(struct wl_client *client, void *data, uint32_t version, uint32_t id)
584 {
585         struct weston_test *test = data;
586         struct wl_resource *resource;
587
588         resource = wl_resource_create(client, &weston_test_interface, 1, id);
589         if (!resource) {
590                 wl_client_post_no_memory(client);
591                 return;
592         }
593
594         wl_resource_set_implementation(resource,
595                                        &test_implementation, test, NULL);
596
597         notify_pointer_position(test, resource);
598 }
599
600 static void
601 idle_launch_client(void *data)
602 {
603         struct weston_test *test = data;
604         pid_t pid;
605         sigset_t allsigs;
606         char *path;
607
608         path = getenv("WESTON_TEST_CLIENT_PATH");
609         if (path == NULL)
610                 return;
611         pid = fork();
612         if (pid == -1)
613                 exit(EXIT_FAILURE);
614         if (pid == 0) {
615                 sigfillset(&allsigs);
616                 sigprocmask(SIG_UNBLOCK, &allsigs, NULL);
617                 execl(path, path, NULL);
618                 weston_log("compositor: executing '%s' failed: %m\n", path);
619                 exit(EXIT_FAILURE);
620         }
621
622         test->process.pid = pid;
623         test->process.cleanup = test_client_sigchld;
624         weston_watch_process(&test->process);
625 }
626
627 WL_EXPORT int
628 wet_module_init(struct weston_compositor *ec,
629                 int *argc, char *argv[])
630 {
631         struct weston_test *test;
632         struct wl_event_loop *loop;
633
634         test = zalloc(sizeof *test);
635         if (test == NULL)
636                 return -1;
637
638         test->compositor = ec;
639         weston_layer_init(&test->layer, ec);
640         weston_layer_set_position(&test->layer, WESTON_LAYER_POSITION_CURSOR - 1);
641
642         if (wl_global_create(ec->wl_display, &weston_test_interface, 1,
643                              test, bind_test) == NULL)
644                 return -1;
645
646         if (test_seat_init(test) == -1)
647                 return -1;
648
649         loop = wl_display_get_event_loop(ec->wl_display);
650         wl_event_loop_add_idle(loop, idle_launch_client, test);
651
652         return 0;
653 }