585bf914861fb5c7408e6cf5079f4d95378a49b2
[src/agl-compositor.git] / tests / weston-test-fixture-compositor.c
1 /*
2  * Copyright 2019 Collabora, Ltd.
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 <string.h>
29 #include <assert.h>
30 #include <libudev.h>
31 #include <unistd.h>
32 #include <sys/file.h>
33 #include <errno.h>
34
35 #include "shared/helpers.h"
36 #include "weston-test-fixture-compositor.h"
37 #include "weston.h"
38 #include "test-config.h"
39
40 struct prog_args {
41         int argc;
42         char **argv;
43         char **saved;
44         int alloc;
45 };
46
47 static void
48 prog_args_init(struct prog_args *p)
49 {
50         memset(p, 0, sizeof(*p));
51 }
52
53 static void
54 prog_args_take(struct prog_args *p, char *arg)
55 {
56         assert(arg);
57
58         if (p->argc == p->alloc) {
59                 p->alloc += 10;
60                 p->argv = realloc(p->argv, sizeof(char *) * p->alloc);
61                 assert(p->argv);
62         }
63
64         p->argv[p->argc++] = arg;
65 }
66
67 /*
68  * The program to be executed will trample on argv, hence we need a copy to
69  * be able to free all our args.
70  */
71 static void
72 prog_args_save(struct prog_args *p)
73 {
74         assert(p->saved == NULL);
75
76         p->saved = calloc(p->argc, sizeof(char *));
77         assert(p->saved);
78
79         memcpy(p->saved, p->argv, sizeof(char *) * p->argc);
80 }
81
82 static void
83 prog_args_fini(struct prog_args *p)
84 {
85         int i;
86
87         assert(p->saved);
88
89         for (i = 0; i < p->argc; i++)
90                 free(p->saved[i]);
91         free(p->saved);
92         free(p->argv);
93         prog_args_init(p);
94 }
95
96 static char *
97 get_lock_path(void)
98 {
99         const char *env_path, *suffix;
100         char *lock_path;
101
102         suffix = "weston-test-suite-drm-lock";
103         env_path = getenv("XDG_RUNTIME_DIR");
104         if (!env_path) {
105                 fprintf(stderr, "Failed to compute lock file path. " \
106                         "XDG_RUNTIME_DIR is not set.\n");
107                 return NULL;
108         }
109
110         if (asprintf(&lock_path, "%s/%s", env_path, suffix) == -1)
111                 return NULL;
112
113         return lock_path;
114 }
115
116 /*
117  * DRM-backend tests need to be run sequentially, since there can only be
118  * one user at a time with master status in a DRM KMS device. Since the
119  * test suite runs the tests in parallel, there's a mechanism to assure
120  * only one DRM-backend test is running at a time: tests of this type keep
121  * waiting until they acquire a lock (which is hold until they end).
122  *
123  * Returns -1 in failure and fd in success.
124  */
125 static int
126 wait_for_lock(void)
127 {
128         char *lock_path;
129         int fd;
130
131         lock_path = get_lock_path();
132         if (!lock_path)
133                 goto err_path;
134
135         fd = open(lock_path, O_RDWR | O_CLOEXEC | O_CREAT, 00700);
136         if (fd == -1) {
137                 fprintf(stderr, "Could not open lock file %s: %s\n", lock_path, strerror(errno));
138                 goto err_open;
139         }
140         fprintf(stderr, "Waiting for lock on %s...\n", lock_path);
141
142         /* The call is blocking, so we don't need a 'while'. Also, as we
143          * have a timeout for each test, this won't get stuck waiting. */
144         if (flock(fd, LOCK_EX) == -1) {
145                 fprintf(stderr, "Could not lock %s: %s\n", lock_path, strerror(errno));
146                 goto err_lock;
147         }
148         fprintf(stderr, "Lock %s acquired.\n", lock_path);
149         free(lock_path);
150
151         return fd;
152
153 err_lock:
154         close(fd);
155 err_open:
156         free(lock_path);
157 err_path:
158         return -1;
159 }
160
161 /** Initialize part of compositor setup
162  *
163  * \param setup The variable to initialize.
164  * \param testset_name Value for testset_name member.
165  *
166  * \ingroup testharness_private
167  */
168 void
169 compositor_setup_defaults_(struct compositor_setup *setup,
170                            const char *testset_name)
171 {
172         *setup = (struct compositor_setup) {
173                 .test_quirks = (struct weston_testsuite_quirks){ },
174                 .backend = WESTON_BACKEND_HEADLESS,
175                 .renderer = RENDERER_NOOP,
176                 .shell = SHELL_DESKTOP,
177                 .xwayland = false,
178                 .width = 320,
179                 .height = 240,
180                 .scale = 1,
181                 .transform = WL_OUTPUT_TRANSFORM_NORMAL,
182                 .config_file = NULL,
183                 .extra_module = NULL,
184                 .logging_scopes = NULL,
185                 .testset_name = testset_name,
186         };
187 }
188
189 static const char *
190 backend_to_str(enum weston_compositor_backend b)
191 {
192         static const char * const names[] = {
193                 [WESTON_BACKEND_DRM] = "drm-backend.so",
194                 [WESTON_BACKEND_FBDEV] = "fbdev-backend.so",
195                 [WESTON_BACKEND_HEADLESS] = "headless-backend.so",
196                 [WESTON_BACKEND_RDP] = "rdp-backend.so",
197                 [WESTON_BACKEND_WAYLAND] = "wayland-backend.so",
198                 [WESTON_BACKEND_X11] = "X11-backend.so",
199         };
200         assert(b >= 0 && b < ARRAY_LENGTH(names));
201         return names[b];
202 }
203
204 static const char *
205 renderer_to_arg(enum weston_compositor_backend b, enum renderer_type r)
206 {
207         static const char * const headless_names[] = {
208                 [RENDERER_NOOP] = NULL,
209                 [RENDERER_PIXMAN] = "--use-pixman",
210                 [RENDERER_GL] = "--use-gl",
211         };
212         static const char * const drm_names[] = {
213                 [RENDERER_PIXMAN] = "--use-pixman",
214                 [RENDERER_GL] = NULL,
215         };
216
217         switch (b) {
218         case WESTON_BACKEND_HEADLESS:
219                 assert(r >= RENDERER_NOOP && r <= RENDERER_GL);
220                 return headless_names[r];
221         case WESTON_BACKEND_DRM:
222                 assert(r >= RENDERER_PIXMAN && r <= RENDERER_GL);
223                 return drm_names[r];
224         default:
225                 assert(0 && "renderer_to_str() does not know the backend");
226         }
227
228         return NULL;
229 }
230
231 static const char *
232 shell_to_str(enum shell_type t)
233 {
234         static const char * const names[] = {
235                 [SHELL_TEST_DESKTOP] = "weston-test-desktop-shell.so",
236                 [SHELL_DESKTOP] = "desktop-shell.so",
237                 [SHELL_FULLSCREEN] = "fullscreen-shell.so",
238                 [SHELL_IVI] = "ivi-shell.so",
239         };
240         assert(t >= 0 && t < ARRAY_LENGTH(names));
241         return names[t];
242 }
243
244 static const char *
245 transform_to_str(enum wl_output_transform t)
246 {
247         static const char * const names[] = {
248                 [WL_OUTPUT_TRANSFORM_NORMAL] = "normal",
249                 [WL_OUTPUT_TRANSFORM_90] = "rotate-90",
250                 [WL_OUTPUT_TRANSFORM_180] = "rotate-180",
251                 [WL_OUTPUT_TRANSFORM_270] = "rotate-270",
252                 [WL_OUTPUT_TRANSFORM_FLIPPED] = "flipped",
253                 [WL_OUTPUT_TRANSFORM_FLIPPED_90] = "flipped-rotate-90",
254                 [WL_OUTPUT_TRANSFORM_FLIPPED_180] = "flipped-rotate-180",
255                 [WL_OUTPUT_TRANSFORM_FLIPPED_270] = "flipped-rotate-270",
256         };
257
258         assert(t < ARRAY_LENGTH(names) && names[t]);
259         return names[t];
260 }
261
262 /** Execute compositor
263  *
264  * Manufactures the compositor command line and calls wet_main().
265  *
266  * Returns RESULT_SKIP if the given setup contains features that were disabled
267  * in the build, e.g. GL-renderer or DRM-backend.
268  *
269  * \ingroup testharness_private
270  */
271 int
272 execute_compositor(const struct compositor_setup *setup,
273                    struct wet_testsuite_data *data)
274 {
275         struct weston_testsuite_data test_data;
276         struct prog_args args;
277         char *tmp;
278         const char *ctmp, *drm_device;
279         int ret, lock_fd = -1;
280
281         if (setenv("WESTON_MODULE_MAP", WESTON_MODULE_MAP, 0) < 0 ||
282             setenv("WESTON_DATA_DIR", WESTON_DATA_DIR, 0) < 0) {
283                 fprintf(stderr, "Error: environment setup failed.\n");
284                 return RESULT_HARD_ERROR;
285         }
286
287 #ifndef BUILD_DRM_COMPOSITOR
288         if (setup->backend == WESTON_BACKEND_DRM) {
289                 fprintf(stderr, "DRM-backend required but not built, skipping.\n");
290                 return RESULT_SKIP;
291         }
292 #endif
293
294 #ifndef BUILD_FBDEV_COMPOSITOR
295         if (setup->backend == WESTON_BACKEND_FBDEV) {
296                 fprintf(stderr, "fbdev-backend required but not built, skipping.\n");
297                 return RESULT_SKIP;
298         }
299 #endif
300
301 #ifndef BUILD_RDP_COMPOSITOR
302         if (setup->backend == WESTON_BACKEND_RDP) {
303                 fprintf(stderr, "RDP-backend required but not built, skipping.\n");
304                 return RESULT_SKIP;
305         }
306 #endif
307
308 #ifndef BUILD_WAYLAND_COMPOSITOR
309         if (setup->backend == WESTON_BACKEND_WAYLAND) {
310                 fprintf(stderr, "wayland-backend required but not built, skipping.\n");
311                 return RESULT_SKIP;
312         }
313 #endif
314
315 #ifndef BUILD_X11_COMPOSITOR
316         if (setup->backend == WESTON_BACKEND_X11) {
317                 fprintf(stderr, "X11-backend required but not built, skipping.\n");
318                 return RESULT_SKIP;
319         }
320 #endif
321
322 #ifndef ENABLE_EGL
323         if (setup->renderer == RENDERER_GL) {
324                 fprintf(stderr, "GL-renderer required but not built, skipping.\n");
325                 return RESULT_SKIP;
326         }
327 #endif
328
329 #if !TEST_GL_RENDERER
330         if (setup->renderer == RENDERER_GL) {
331                 fprintf(stderr, "GL-renderer disabled for tests, skipping.\n");
332                 return RESULT_SKIP;
333         }
334 #endif
335
336         prog_args_init(&args);
337
338         /* argv[0] */
339         asprintf(&tmp, "weston-%s", setup->testset_name);
340         prog_args_take(&args, tmp);
341
342         asprintf(&tmp, "--backend=%s", backend_to_str(setup->backend));
343         prog_args_take(&args, tmp);
344
345         if (setup->backend == WESTON_BACKEND_DRM) {
346
347                 drm_device = getenv("WESTON_TEST_SUITE_DRM_DEVICE");
348                 if (!drm_device) {
349                         fprintf(stderr, "Skipping DRM-backend tests because " \
350                                 "WESTON_TEST_SUITE_DRM_DEVICE is not set. " \
351                                 "See test suite documentation to learn how " \
352                                 "to run them.\n");
353                         return RESULT_SKIP;
354                 }
355                 asprintf(&tmp, "--drm-device=%s", drm_device);
356                 prog_args_take(&args, tmp);
357
358                 prog_args_take(&args, strdup("--seat=weston-test-seat"));
359
360                 prog_args_take(&args, strdup("--continue-without-input"));
361
362                 lock_fd = wait_for_lock();
363                 if (lock_fd == -1)
364                         return RESULT_FAIL;
365         }
366
367         asprintf(&tmp, "--socket=%s", setup->testset_name);
368         prog_args_take(&args, tmp);
369
370         asprintf(&tmp, "--modules=%s%s%s", TESTSUITE_PLUGIN_PATH,
371                  setup->extra_module ? "," : "",
372                  setup->extra_module ? setup->extra_module : "");
373         prog_args_take(&args, tmp);
374
375         if (setup->backend != WESTON_BACKEND_DRM &&
376             setup->backend != WESTON_BACKEND_FBDEV) {
377                 asprintf(&tmp, "--width=%d", setup->width);
378                 prog_args_take(&args, tmp);
379
380                 asprintf(&tmp, "--height=%d", setup->height);
381                 prog_args_take(&args, tmp);
382         }
383
384         if (setup->scale != 1) {
385                 asprintf(&tmp, "--scale=%d", setup->scale);
386                 prog_args_take(&args, tmp);
387         }
388
389         if (setup->transform != WL_OUTPUT_TRANSFORM_NORMAL) {
390                 asprintf(&tmp, "--transform=%s",
391                          transform_to_str(setup->transform));
392                 prog_args_take(&args, tmp);
393         }
394
395         if (setup->config_file) {
396                 asprintf(&tmp, "--config=%s", setup->config_file);
397                 prog_args_take(&args, tmp);
398         } else {
399                 prog_args_take(&args, strdup("--no-config"));
400         }
401
402         ctmp = renderer_to_arg(setup->backend, setup->renderer);
403         if (ctmp)
404                 prog_args_take(&args, strdup(ctmp));
405
406         asprintf(&tmp, "--shell=%s", shell_to_str(setup->shell));
407         prog_args_take(&args, tmp);
408
409         if (setup->logging_scopes) {
410                 asprintf(&tmp, "--logger-scopes=%s", setup->logging_scopes);
411                 prog_args_take(&args, tmp);
412         }
413
414         if (setup->xwayland)
415                 prog_args_take(&args, strdup("--xwayland"));
416
417         test_data.test_quirks = setup->test_quirks;
418         test_data.test_private_data = data;
419         prog_args_save(&args);
420         ret = wet_main(args.argc, args.argv, &test_data);
421
422         prog_args_fini(&args);
423
424         /* We acquired a lock (if this is a DRM-backend test) and now we can
425          * close its fd and release it, as it has already been run. */
426         if (lock_fd != -1)
427                 close(lock_fd);
428
429         return ret;
430 }