tests: Only run buffer-count test if we have at least mesa 10
[platform/upstream/weston.git] / tests / weston-test-client-helper.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <config.h>
24
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <sys/mman.h>
30
31 #include "../shared/os-compatibility.h"
32 #include "weston-test-client-helper.h"
33
34 static inline void *
35 xzalloc(size_t size)
36 {
37         void *p;
38
39         p = calloc(1, size);
40         assert(p);
41
42         return p;
43 }
44
45 int
46 surface_contains(struct surface *surface, int x, int y)
47 {
48         /* test whether a global x,y point is contained in the surface */
49         int sx = surface->x;
50         int sy = surface->y;
51         int sw = surface->width;
52         int sh = surface->height;
53         return x >= sx && y >= sy && x < sx + sw && y < sy + sh;
54 }
55
56 static void
57 frame_callback_handler(void *data, struct wl_callback *callback, uint32_t time)
58 {
59         int *done = data;
60
61         *done = 1;
62
63         wl_callback_destroy(callback);
64 }
65
66 static const struct wl_callback_listener frame_listener = {
67         frame_callback_handler
68 };
69
70 struct wl_callback *
71 frame_callback_set(struct wl_surface *surface, int *done)
72 {
73         struct wl_callback *callback;
74
75         *done = 0;
76         callback = wl_surface_frame(surface);
77         wl_callback_add_listener(callback, &frame_listener, done);
78
79         return callback;
80 }
81
82 void
83 frame_callback_wait(struct client *client, int *done)
84 {
85         while (!*done) {
86                 assert(wl_display_dispatch(client->wl_display) >= 0);
87         }
88 }
89
90 void
91 move_client(struct client *client, int x, int y)
92 {
93         struct surface *surface = client->surface;
94         int done;
95
96         client->surface->x = x;
97         client->surface->y = y;
98         wl_test_move_surface(client->test->wl_test, surface->wl_surface,
99                              surface->x, surface->y);
100         /* The attach here is necessary because commit() will call congfigure
101          * only on surfaces newly attached, and the one that sets the surface
102          * position is the configure. */
103         wl_surface_attach(surface->wl_surface, surface->wl_buffer, 0, 0);
104         wl_surface_damage(surface->wl_surface, 0, 0, surface->width,
105                           surface->height);
106
107         frame_callback_set(surface->wl_surface, &done);
108
109         wl_surface_commit(surface->wl_surface);
110
111         frame_callback_wait(client, &done);
112 }
113
114 int
115 get_n_egl_buffers(struct client *client)
116 {
117         client->test->n_egl_buffers = -1;
118
119         wl_test_get_n_egl_buffers(client->test->wl_test);
120         wl_display_roundtrip(client->wl_display);
121
122         return client->test->n_egl_buffers;
123 }
124
125 static void
126 pointer_handle_enter(void *data, struct wl_pointer *wl_pointer,
127                      uint32_t serial, struct wl_surface *wl_surface,
128                      wl_fixed_t x, wl_fixed_t y)
129 {
130         struct pointer *pointer = data;
131
132         pointer->focus = wl_surface_get_user_data(wl_surface);
133         pointer->x = wl_fixed_to_int(x);
134         pointer->y = wl_fixed_to_int(y);
135
136         fprintf(stderr, "test-client: got pointer enter %d %d, surface %p\n",
137                 pointer->x, pointer->y, pointer->focus);
138 }
139
140 static void
141 pointer_handle_leave(void *data, struct wl_pointer *wl_pointer,
142                      uint32_t serial, struct wl_surface *wl_surface)
143 {
144         struct pointer *pointer = data;
145
146         pointer->focus = NULL;
147
148         fprintf(stderr, "test-client: got pointer leave, surface %p\n",
149                 wl_surface_get_user_data(wl_surface));
150 }
151
152 static void
153 pointer_handle_motion(void *data, struct wl_pointer *wl_pointer,
154                       uint32_t time, wl_fixed_t x, wl_fixed_t y)
155 {
156         struct pointer *pointer = data;
157
158         pointer->x = wl_fixed_to_int(x);
159         pointer->y = wl_fixed_to_int(y);
160
161         fprintf(stderr, "test-client: got pointer motion %d %d\n",
162                 pointer->x, pointer->y);
163 }
164
165 static void
166 pointer_handle_button(void *data, struct wl_pointer *wl_pointer,
167                       uint32_t serial, uint32_t time, uint32_t button,
168                       uint32_t state)
169 {
170         struct pointer *pointer = data;
171
172         pointer->button = button;
173         pointer->state = state;
174
175         fprintf(stderr, "test-client: got pointer button %u %u\n",
176                 button, state);
177 }
178
179 static void
180 pointer_handle_axis(void *data, struct wl_pointer *wl_pointer,
181                     uint32_t time, uint32_t axis, wl_fixed_t value)
182 {
183         fprintf(stderr, "test-client: got pointer axis %u %f\n",
184                 axis, wl_fixed_to_double(value));
185 }
186
187 static const struct wl_pointer_listener pointer_listener = {
188         pointer_handle_enter,
189         pointer_handle_leave,
190         pointer_handle_motion,
191         pointer_handle_button,
192         pointer_handle_axis,
193 };
194
195 static void
196 keyboard_handle_keymap(void *data, struct wl_keyboard *wl_keyboard,
197                        uint32_t format, int fd, uint32_t size)
198 {
199         close(fd);
200
201         fprintf(stderr, "test-client: got keyboard keymap\n");
202 }
203
204 static void
205 keyboard_handle_enter(void *data, struct wl_keyboard *wl_keyboard,
206                       uint32_t serial, struct wl_surface *wl_surface,
207                       struct wl_array *keys)
208 {
209         struct keyboard *keyboard = data;
210
211         keyboard->focus = wl_surface_get_user_data(wl_surface);
212
213         fprintf(stderr, "test-client: got keyboard enter, surface %p\n",
214                 keyboard->focus);
215 }
216
217 static void
218 keyboard_handle_leave(void *data, struct wl_keyboard *wl_keyboard,
219                       uint32_t serial, struct wl_surface *wl_surface)
220 {
221         struct keyboard *keyboard = data;
222
223         keyboard->focus = NULL;
224
225         fprintf(stderr, "test-client: got keyboard leave, surface %p\n",
226                 wl_surface_get_user_data(wl_surface));
227 }
228
229 static void
230 keyboard_handle_key(void *data, struct wl_keyboard *wl_keyboard,
231                     uint32_t serial, uint32_t time, uint32_t key,
232                     uint32_t state)
233 {
234         struct keyboard *keyboard = data;
235
236         keyboard->key = key;
237         keyboard->state = state;
238
239         fprintf(stderr, "test-client: got keyboard key %u %u\n", key, state);
240 }
241
242 static void
243 keyboard_handle_modifiers(void *data, struct wl_keyboard *wl_keyboard,
244                           uint32_t serial, uint32_t mods_depressed,
245                           uint32_t mods_latched, uint32_t mods_locked,
246                           uint32_t group)
247 {
248         struct keyboard *keyboard = data;
249
250         keyboard->mods_depressed = mods_depressed;
251         keyboard->mods_latched = mods_latched;
252         keyboard->mods_locked = mods_locked;
253         keyboard->group = group;
254
255         fprintf(stderr, "test-client: got keyboard modifiers %u %u %u %u\n",
256                 mods_depressed, mods_latched, mods_locked, group);
257 }
258
259 static const struct wl_keyboard_listener keyboard_listener = {
260         keyboard_handle_keymap,
261         keyboard_handle_enter,
262         keyboard_handle_leave,
263         keyboard_handle_key,
264         keyboard_handle_modifiers,
265 };
266
267 static void
268 surface_enter(void *data,
269               struct wl_surface *wl_surface, struct wl_output *output)
270 {
271         struct surface *surface = data;
272
273         surface->output = wl_output_get_user_data(output);
274
275         fprintf(stderr, "test-client: got surface enter output %p\n",
276                 surface->output);
277 }
278
279 static void
280 surface_leave(void *data,
281               struct wl_surface *wl_surface, struct wl_output *output)
282 {
283         struct surface *surface = data;
284
285         surface->output = NULL;
286
287         fprintf(stderr, "test-client: got surface leave output %p\n",
288                 wl_output_get_user_data(output));
289 }
290
291 static const struct wl_surface_listener surface_listener = {
292         surface_enter,
293         surface_leave
294 };
295
296 struct wl_buffer *
297 create_shm_buffer(struct client *client, int width, int height, void **pixels)
298 {
299         struct wl_shm *shm = client->wl_shm;
300         int stride = width * 4;
301         int size = stride * height;
302         struct wl_shm_pool *pool;
303         struct wl_buffer *buffer;
304         int fd;
305         void *data;
306
307         fd = os_create_anonymous_file(size);
308         assert(fd >= 0);
309
310         data = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
311         if (data == MAP_FAILED) {
312                 close(fd);
313                 assert(data != MAP_FAILED);
314         }
315
316         pool = wl_shm_create_pool(shm, fd, size);
317         buffer = wl_shm_pool_create_buffer(pool, 0, width, height, stride,
318                                            WL_SHM_FORMAT_ARGB8888);
319         wl_shm_pool_destroy(pool);
320
321         close(fd);
322
323         if (pixels)
324                 *pixels = data;
325
326         return buffer;
327 }
328
329 static void
330 shm_format(void *data, struct wl_shm *wl_shm, uint32_t format)
331 {
332         struct client *client = data;
333
334         if (format == WL_SHM_FORMAT_ARGB8888)
335                 client->has_argb = 1;
336 }
337
338 struct wl_shm_listener shm_listener = {
339         shm_format
340 };
341
342 static void
343 test_handle_pointer_position(void *data, struct wl_test *wl_test,
344                              wl_fixed_t x, wl_fixed_t y)
345 {
346         struct test *test = data;
347         test->pointer_x = wl_fixed_to_int(x);
348         test->pointer_y = wl_fixed_to_int(y);
349
350         fprintf(stderr, "test-client: got global pointer %d %d\n",
351                 test->pointer_x, test->pointer_y);
352 }
353
354 static void
355 test_handle_n_egl_buffers(void *data, struct wl_test *wl_test, uint32_t n)
356 {
357         struct test *test = data;
358
359         test->n_egl_buffers = n;
360 }
361
362 static const struct wl_test_listener test_listener = {
363         test_handle_pointer_position,
364         test_handle_n_egl_buffers,
365 };
366
367 static void
368 seat_handle_capabilities(void *data, struct wl_seat *seat,
369                          enum wl_seat_capability caps)
370 {
371         struct input *input = data;
372         struct pointer *pointer;
373         struct keyboard *keyboard;
374
375         if ((caps & WL_SEAT_CAPABILITY_POINTER) && !input->pointer) {
376                 pointer = xzalloc(sizeof *pointer);
377                 pointer->wl_pointer = wl_seat_get_pointer(seat);
378                 wl_pointer_set_user_data(pointer->wl_pointer, pointer);
379                 wl_pointer_add_listener(pointer->wl_pointer, &pointer_listener,
380                                         pointer);
381                 input->pointer = pointer;
382         } else if (!(caps & WL_SEAT_CAPABILITY_POINTER) && input->pointer) {
383                 wl_pointer_destroy(input->pointer->wl_pointer);
384                 free(input->pointer);
385                 input->pointer = NULL;
386         }
387
388         if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !input->keyboard) {
389                 keyboard = xzalloc(sizeof *keyboard);
390                 keyboard->wl_keyboard = wl_seat_get_keyboard(seat);
391                 wl_keyboard_set_user_data(keyboard->wl_keyboard, keyboard);
392                 wl_keyboard_add_listener(keyboard->wl_keyboard, &keyboard_listener,
393                                          keyboard);
394                 input->keyboard = keyboard;
395         } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && input->keyboard) {
396                 wl_keyboard_destroy(input->keyboard->wl_keyboard);
397                 free(input->keyboard);
398                 input->keyboard = NULL;
399         }
400 }
401
402 static const struct wl_seat_listener seat_listener = {
403         seat_handle_capabilities,
404 };
405
406 static void
407 output_handle_geometry(void *data,
408                        struct wl_output *wl_output,
409                        int x, int y,
410                        int physical_width,
411                        int physical_height,
412                        int subpixel,
413                        const char *make,
414                        const char *model,
415                        int32_t transform)
416 {
417         struct output *output = data;
418
419         output->x = x;
420         output->y = y;
421 }
422
423 static void
424 output_handle_mode(void *data,
425                    struct wl_output *wl_output,
426                    uint32_t flags,
427                    int width,
428                    int height,
429                    int refresh)
430 {
431         struct output *output = data;
432
433         if (flags & WL_OUTPUT_MODE_CURRENT) {
434                 output->width = width;
435                 output->height = height;
436         }
437 }
438
439 static const struct wl_output_listener output_listener = {
440         output_handle_geometry,
441         output_handle_mode
442 };
443
444 static void
445 handle_global(void *data, struct wl_registry *registry,
446               uint32_t id, const char *interface, uint32_t version)
447 {
448         struct client *client = data;
449         struct input *input;
450         struct output *output;
451         struct test *test;
452         struct global *global;
453
454         global = xzalloc(sizeof *global);
455         global->name = id;
456         global->interface = strdup(interface);
457         assert(interface);
458         global->version = version;
459         wl_list_insert(client->global_list.prev, &global->link);
460
461         if (strcmp(interface, "wl_compositor") == 0) {
462                 client->wl_compositor =
463                         wl_registry_bind(registry, id,
464                                          &wl_compositor_interface, 1);
465         } else if (strcmp(interface, "wl_seat") == 0) {
466                 input = xzalloc(sizeof *input);
467                 input->wl_seat =
468                         wl_registry_bind(registry, id,
469                                          &wl_seat_interface, 1);
470                 wl_seat_add_listener(input->wl_seat, &seat_listener, input);
471                 client->input = input;
472         } else if (strcmp(interface, "wl_shm") == 0) {
473                 client->wl_shm =
474                         wl_registry_bind(registry, id,
475                                          &wl_shm_interface, 1);
476                 wl_shm_add_listener(client->wl_shm, &shm_listener, client);
477         } else if (strcmp(interface, "wl_output") == 0) {
478                 output = xzalloc(sizeof *output);
479                 output->wl_output =
480                         wl_registry_bind(registry, id,
481                                          &wl_output_interface, 1);
482                 wl_output_add_listener(output->wl_output,
483                                        &output_listener, output);
484                 client->output = output;
485         } else if (strcmp(interface, "wl_test") == 0) {
486                 test = xzalloc(sizeof *test);
487                 test->wl_test =
488                         wl_registry_bind(registry, id,
489                                          &wl_test_interface, 1);
490                 wl_test_add_listener(test->wl_test, &test_listener, test);
491                 client->test = test;
492         }
493 }
494
495 static const struct wl_registry_listener registry_listener = {
496         handle_global
497 };
498
499 void
500 skip(const char *fmt, ...)
501 {
502         va_list argp;
503
504         va_start(argp, fmt);
505         vfprintf(stderr, fmt, argp);
506         va_end(argp);
507
508         /* automake tests uses exit code 77, but we don't have a good
509          * way to make weston exit with that from here. */
510         exit(0);
511 }
512
513 static void
514 log_handler(const char *fmt, va_list args)
515 {
516         fprintf(stderr, "libwayland: ");
517         vfprintf(stderr, fmt, args);
518 }
519
520 struct client *
521 client_create(int x, int y, int width, int height)
522 {
523         struct client *client;
524         struct surface *surface;
525
526         wl_log_set_handler_client(log_handler);
527
528         /* connect to display */
529         client = xzalloc(sizeof *client);
530         client->wl_display = wl_display_connect(NULL);
531         assert(client->wl_display);
532         wl_list_init(&client->global_list);
533
534         /* setup registry so we can bind to interfaces */
535         client->wl_registry = wl_display_get_registry(client->wl_display);
536         wl_registry_add_listener(client->wl_registry, &registry_listener, client);
537
538         /* trigger global listener */
539         wl_display_dispatch(client->wl_display);
540         wl_display_roundtrip(client->wl_display);
541
542         /* must have WL_SHM_FORMAT_ARGB32 */
543         assert(client->has_argb);
544
545         /* must have wl_test interface */
546         assert(client->test);
547
548         /* must have an output */
549         assert(client->output);
550
551         /* initialize the client surface */
552         surface = xzalloc(sizeof *surface);
553         surface->wl_surface =
554                 wl_compositor_create_surface(client->wl_compositor);
555         assert(surface->wl_surface);
556
557         wl_surface_add_listener(surface->wl_surface, &surface_listener,
558                                 surface);
559
560         client->surface = surface;
561         wl_surface_set_user_data(surface->wl_surface, surface);
562
563         surface->width = width;
564         surface->height = height;
565         surface->wl_buffer = create_shm_buffer(client, width, height,
566                                                &surface->data);
567
568         memset(surface->data, 64, width * height * 4);
569
570         move_client(client, x, y);
571
572         return client;
573 }