blob: 6f41b2a7240c7218bc7d2aaca7452fd4aaed0931 [file] [log] [blame]
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +00001/*
2 * Copyright © 2015 Collabora Ltd.
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
Bryce Harringtonb4dae9b2016-06-15 18:13:07 -070023#include "config.h"
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +000024
Jussi Kukkonen649bbce2016-07-19 14:16:27 +030025#include <stdint.h>
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +000026#include <stdio.h>
27#include <stdlib.h>
28#include <string.h>
29#include <stdbool.h>
30#include <assert.h>
31#include <unistd.h>
32#include <sys/mman.h>
33#include <signal.h>
34#include <fcntl.h>
35
36#include <drm_fourcc.h>
37
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +000038#include <sys/mman.h>
39#include <sys/stat.h>
40#include <fcntl.h>
41#include <errno.h>
42#include <linux/videodev2.h>
43#include <linux/input.h>
44
45#include <wayland-client.h>
Bryce Harrington0d1a6222016-02-11 16:42:49 -080046#include "shared/zalloc.h"
Jonas Ådahl6ccd2862016-08-12 10:01:29 +080047#include "xdg-shell-unstable-v6-client-protocol.h"
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +000048#include "fullscreen-shell-unstable-v1-client-protocol.h"
49#include "linux-dmabuf-unstable-v1-client-protocol.h"
50
51#define CLEAR(x) memset(&(x), 0, sizeof(x))
52
Jonas Ådahl6ccd2862016-08-12 10:01:29 +080053static void
54redraw(void *data, struct wl_callback *callback, uint32_t time);
55
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +000056static int
57xioctl(int fh, int request, void *arg)
58{
59 int r;
60
61 do {
62 r = ioctl(fh, request, arg);
63 } while (r == -1 && errno == EINTR);
64
65 return r;
66}
67
68static uint32_t
69parse_format(const char fmt[4])
70{
71 return fourcc_code(fmt[0], fmt[1], fmt[2], fmt[3]);
72}
73
74static inline const char *
75dump_format(uint32_t format, char out[4])
76{
77#if BYTE_ORDER == BIG_ENDIAN
78 format = __builtin_bswap32(format);
79#endif
80 memcpy(out, &format, 4);
81 return out;
82}
83
84struct buffer_format {
85 int width;
86 int height;
87 enum v4l2_buf_type type;
88 uint32_t format;
89
90 unsigned num_planes;
91 unsigned strides[VIDEO_MAX_PLANES];
92};
93
94struct display {
95 struct wl_display *display;
96 struct wl_registry *registry;
97 struct wl_compositor *compositor;
98 struct wl_seat *seat;
99 struct wl_keyboard *keyboard;
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800100 struct zxdg_shell_v6 *shell;
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000101 struct zwp_fullscreen_shell_v1 *fshell;
102 struct zwp_linux_dmabuf_v1 *dmabuf;
103 bool requested_format_found;
104
105 int v4l_fd;
106 struct buffer_format format;
107 uint32_t drm_format;
108};
109
110struct buffer {
111 struct wl_buffer *buffer;
112 struct display *display;
113 int busy;
114 int index;
115
116 int dmabuf_fds[VIDEO_MAX_PLANES];
117 int data_offsets[VIDEO_MAX_PLANES];
118};
119
120#define NUM_BUFFERS 3
121
122struct window {
123 struct display *display;
124 struct wl_surface *surface;
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800125 struct zxdg_surface_v6 *xdg_surface;
126 struct zxdg_toplevel_v6 *xdg_toplevel;
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000127 struct buffer buffers[NUM_BUFFERS];
128 struct wl_callback *callback;
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800129 bool wait_for_configure;
130 bool initialized;
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000131};
132
133static bool running = true;
134
135static int
136queue(struct display *display, struct buffer *buffer)
137{
138 struct v4l2_buffer buf;
139 struct v4l2_plane planes[VIDEO_MAX_PLANES];
140 unsigned i;
141
142 CLEAR(buf);
143 buf.type = display->format.type;
144 buf.memory = V4L2_MEMORY_MMAP;
145 buf.index = buffer->index;
146
147 if (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
148 CLEAR(planes);
149 buf.length = VIDEO_MAX_PLANES;
150 buf.m.planes = planes;
151 }
152
153 if (xioctl(display->v4l_fd, VIDIOC_QUERYBUF, &buf) == -1) {
154 perror("VIDIOC_QUERYBUF");
155 return 0;
156 }
157
158 if (xioctl(display->v4l_fd, VIDIOC_QBUF, &buf) == -1) {
159 perror("VIDIOC_QBUF");
160 return 0;
161 }
162
163 if (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
164 if (display->format.num_planes != buf.length) {
165 fprintf(stderr, "Wrong number of planes returned by "
166 "QUERYBUF\n");
167 return 0;
168 }
169
170 for (i = 0; i < buf.length; ++i)
171 buffer->data_offsets[i] = buf.m.planes[i].data_offset;
172 }
173
174 return 1;
175}
176
177static void
178buffer_release(void *data, struct wl_buffer *buffer)
179{
180 struct buffer *mybuf = data;
181
182 mybuf->busy = 0;
183
184 if (!queue(mybuf->display, mybuf))
185 running = false;
186}
187
188static const struct wl_buffer_listener buffer_listener = {
189 buffer_release
190};
191
192static unsigned int
193set_format(struct display *display, uint32_t format)
194{
195 struct v4l2_format fmt;
196 char buf[4];
197
198 CLEAR(fmt);
199
200 fmt.type = display->format.type;
201
202 if (xioctl(display->v4l_fd, VIDIOC_G_FMT, &fmt) == -1) {
203 perror("VIDIOC_G_FMT");
204 return 0;
205 }
206
207 /* No need to set the format if it already is the one we want */
208 if (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
209 fmt.fmt.pix.pixelformat == format)
210 return 1;
211 if (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
212 fmt.fmt.pix_mp.pixelformat == format)
213 return fmt.fmt.pix_mp.num_planes;
214
215 if (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
216 fmt.fmt.pix.pixelformat = format;
217 else
218 fmt.fmt.pix_mp.pixelformat = format;
219
220 if (xioctl(display->v4l_fd, VIDIOC_S_FMT, &fmt) == -1) {
221 perror("VIDIOC_S_FMT");
222 return 0;
223 }
224
225 if ((display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE &&
226 fmt.fmt.pix.pixelformat != format) ||
227 (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
228 fmt.fmt.pix_mp.pixelformat != format)) {
229 fprintf(stderr, "Failed to set format to %.4s\n",
230 dump_format(format, buf));
231 return 0;
232 }
233
234 if (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
235 return fmt.fmt.pix_mp.num_planes;
236
237 return 1;
238}
239
240static int
241v4l_connect(struct display *display, const char *dev_name)
242{
243 struct v4l2_capability cap;
244 struct v4l2_requestbuffers req;
245 unsigned int num_planes;
246
247 display->v4l_fd = open(dev_name, O_RDWR);
248 if (display->v4l_fd < 0) {
249 perror(dev_name);
250 return 0;
251 }
252
253 if (xioctl(display->v4l_fd, VIDIOC_QUERYCAP, &cap) == -1) {
254 if (errno == EINVAL) {
255 fprintf(stderr, "%s is no V4L2 device\n", dev_name);
256 } else {
257 perror("VIDIOC_QUERYCAP");
258 }
259 return 0;
260 }
261
262 if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE)
263 display->format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
264 else if (cap.capabilities & V4L2_CAP_VIDEO_CAPTURE_MPLANE)
265 display->format.type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
266 else {
267 fprintf(stderr, "%s is no video capture device\n", dev_name);
268 return 0;
269 }
270
271 if (!(cap.capabilities & V4L2_CAP_STREAMING)) {
272 fprintf(stderr, "%s does not support dmabuf i/o\n", dev_name);
273 return 0;
274 }
275
276 /* Select video input, video standard and tune here */
277
278 num_planes = set_format(display, display->format.format);
279 if (num_planes < 1)
280 return 0;
281
282 CLEAR(req);
283
284 req.type = display->format.type;
285 req.memory = V4L2_MEMORY_MMAP;
286 req.count = NUM_BUFFERS * num_planes;
287
288 if (xioctl(display->v4l_fd, VIDIOC_REQBUFS, &req) == -1) {
289 if (errno == EINVAL) {
290 fprintf(stderr, "%s does not support dmabuf\n",
291 dev_name);
292 } else {
293 perror("VIDIOC_REQBUFS");
294 }
295 return 0;
296 }
297
298 if (req.count < NUM_BUFFERS * num_planes) {
299 fprintf(stderr, "Insufficient buffer memory on %s\n", dev_name);
300 return 0;
301 }
302
303 printf("Created %d buffers\n", req.count);
304
305 return 1;
306}
307
308static void
309v4l_shutdown(struct display *display)
310{
311 close(display->v4l_fd);
312}
313
314static void
315create_succeeded(void *data,
316 struct zwp_linux_buffer_params_v1 *params,
317 struct wl_buffer *new_buffer)
318{
319 struct buffer *buffer = data;
320 unsigned i;
321
322 buffer->buffer = new_buffer;
323 wl_buffer_add_listener(buffer->buffer, &buffer_listener, buffer);
324
325 zwp_linux_buffer_params_v1_destroy(params);
326
327 for (i = 0; i < buffer->display->format.num_planes; ++i)
328 close(buffer->dmabuf_fds[i]);
329}
330
331static void
332create_failed(void *data, struct zwp_linux_buffer_params_v1 *params)
333{
334 struct buffer *buffer = data;
335 unsigned i;
336
337 buffer->buffer = NULL;
338
339 zwp_linux_buffer_params_v1_destroy(params);
340
341 for (i = 0; i < buffer->display->format.num_planes; ++i)
342 close(buffer->dmabuf_fds[i]);
343
344 running = false;
345
346 fprintf(stderr, "Error: zwp_linux_buffer_params.create failed.\n");
347}
348
349static const struct zwp_linux_buffer_params_v1_listener params_listener = {
350 create_succeeded,
351 create_failed
352};
353
Micah Fedke0fee9772017-02-06 12:57:41 -0500354static bool
355check_v4l2_control_bool(const int fd,
356 const struct v4l2_query_ext_ctrl *qectrl,
357 const char *control_name,
358 const int expected_value)
359{
360 struct v4l2_control ctrl;
361
362 memset(&ctrl, 0, sizeof(ctrl));
363 ctrl.id = qectrl->id;
364
365 if (qectrl->flags & V4L2_CTRL_FLAG_DISABLED)
366 return false;
367
368 if (!(qectrl->type == V4L2_CTRL_TYPE_BOOLEAN))
369 return false;
370
371 if (strcmp(qectrl->name, control_name))
372 return false;
373
374 /* with the early-outs out of the way, do the actual check */
375 if (xioctl(fd, VIDIOC_G_CTRL, &ctrl))
376 return false;
377
378 if (ctrl.value != expected_value)
379 return false;
380
381 return true;
382}
383
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000384static void
385create_dmabuf_buffer(struct display *display, struct buffer *buffer)
386{
387 struct zwp_linux_buffer_params_v1 *params;
388 uint64_t modifier;
Micah Fedke0fee9772017-02-06 12:57:41 -0500389 uint32_t lbp_flags;
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000390 unsigned i;
Micah Fedke0fee9772017-02-06 12:57:41 -0500391 struct v4l2_query_ext_ctrl qectrl;
392 const unsigned int v4l2_qec_flags =
393 V4L2_CTRL_FLAG_NEXT_CTRL | V4L2_CTRL_FLAG_NEXT_COMPOUND;
394 const char *vflip_ctrl = "Vertical Flip";
395 const char *hflip_ctrl = "Horizontal Flip";
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000396
397 modifier = 0;
Micah Fedke0fee9772017-02-06 12:57:41 -0500398 lbp_flags = 0;
Pekka Paalanen319397e2016-07-04 16:25:16 +0300399
Micah Fedke0fee9772017-02-06 12:57:41 -0500400 /* handle relevant v4l2 controls */
401 memset(&qectrl, 0, sizeof(qectrl));
402 qectrl.id |= v4l2_qec_flags;
403 while (!xioctl(display->v4l_fd, VIDIOC_QUERY_EXT_CTRL, &qectrl)) {
404 if (check_v4l2_control_bool(display->v4l_fd, &qectrl,
405 vflip_ctrl, 0x1)) {
406 lbp_flags = ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT;
407 printf ("\"%s\" control is set, inverting Y\n", vflip_ctrl);
408 } else if (check_v4l2_control_bool(display->v4l_fd, &qectrl,
409 hflip_ctrl, 0x1)) {
410 printf ("\"%s\" control is set, but dmabuf output cannot"
411 "be flipped horizontally\n", hflip_ctrl);
412 }
413 qectrl.id |= v4l2_qec_flags;
414 }
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000415
416 params = zwp_linux_dmabuf_v1_create_params(display->dmabuf);
417 for (i = 0; i < display->format.num_planes; ++i)
418 zwp_linux_buffer_params_v1_add(params,
419 buffer->dmabuf_fds[i],
420 i, /* plane_idx */
421 buffer->data_offsets[i], /* offset */
422 display->format.strides[i],
423 modifier >> 32,
424 modifier & 0xffffffff);
425 zwp_linux_buffer_params_v1_add_listener(params, &params_listener,
426 buffer);
427 zwp_linux_buffer_params_v1_create(params,
428 display->format.width,
429 display->format.height,
430 display->drm_format,
Micah Fedke0fee9772017-02-06 12:57:41 -0500431 lbp_flags);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000432}
433
434static int
435buffer_export(struct display *display, int index, int dmafd[])
436{
437 struct v4l2_exportbuffer expbuf;
438 unsigned i;
439
440 CLEAR(expbuf);
441
442 for (i = 0; i < display->format.num_planes; ++i) {
443 expbuf.type = display->format.type;
444 expbuf.index = index;
445 expbuf.plane = i;
446 if (xioctl(display->v4l_fd, VIDIOC_EXPBUF, &expbuf) == -1) {
447 perror("VIDIOC_EXPBUF");
448 while (i)
449 close(dmafd[--i]);
450 return 0;
451 }
452 dmafd[i] = expbuf.fd;
453 }
454
455 return 1;
456}
457
458static int
459queue_initial_buffers(struct display *display,
460 struct buffer buffers[NUM_BUFFERS])
461{
462 struct buffer *buffer;
463 int index;
464
465 for (index = 0; index < NUM_BUFFERS; ++index) {
466 buffer = &buffers[index];
467 buffer->display = display;
468 buffer->index = index;
469
470 if (!queue(display, buffer)) {
471 fprintf(stderr, "Failed to queue buffer\n");
472 return 0;
473 }
474
475 assert(!buffer->buffer);
476 if (!buffer_export(display, index, buffer->dmabuf_fds))
477 return 0;
478
479 create_dmabuf_buffer(display, buffer);
480 }
481
482 return 1;
483}
484
485static int
486dequeue(struct display *display)
487{
488 struct v4l2_buffer buf;
489 struct v4l2_plane planes[VIDEO_MAX_PLANES];
490
491 CLEAR(buf);
492 buf.type = display->format.type;
493 buf.memory = V4L2_MEMORY_MMAP;
494 buf.length = VIDEO_MAX_PLANES;
495 buf.m.planes = planes;
496
497 /* This ioctl is blocking until a buffer is ready to be displayed */
498 if (xioctl(display->v4l_fd, VIDIOC_DQBUF, &buf) == -1) {
499 perror("VIDIOC_DQBUF");
500 return -1;
501 }
502
503 assert(buf.flags & V4L2_BUF_FLAG_DONE);
504
505 return buf.index;
506}
507
508static int
509fill_buffer_format(struct display *display)
510{
511 struct v4l2_format fmt;
512 struct v4l2_pix_format *pix;
513 struct v4l2_pix_format_mplane *pix_mp;
514 int i;
515 char buf[4];
516
517 CLEAR(fmt);
518 fmt.type = display->format.type;
519
520 /* Preserve original settings as set by v4l2-ctl for example */
521 if (xioctl(display->v4l_fd, VIDIOC_G_FMT, &fmt) == -1) {
522 perror("VIDIOC_G_FMT");
523 return 0;
524 }
525
526 if (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
527 pix = &fmt.fmt.pix;
528
529 printf("%d×%d, %.4s\n", pix->width, pix->height,
530 dump_format(pix->pixelformat, buf));
531
532 display->format.num_planes = 1;
533 display->format.width = pix->width;
534 display->format.height = pix->height;
535 display->format.strides[0] = pix->bytesperline;
536 } else {
537 pix_mp = &fmt.fmt.pix_mp;
538
539 display->format.num_planes = pix_mp->num_planes;
540 display->format.width = pix_mp->width;
541 display->format.height = pix_mp->height;
542
543 for (i = 0; i < pix_mp->num_planes; ++i)
544 display->format.strides[i] = pix_mp->plane_fmt[i].bytesperline;
545
546 printf("%d×%d, %.4s, %d planes\n",
547 pix_mp->width, pix_mp->height,
548 dump_format(pix_mp->pixelformat, buf),
549 pix_mp->num_planes);
550 }
551
552 return 1;
553}
554
555static int
556v4l_init(struct display *display, struct buffer buffers[NUM_BUFFERS]) {
557 if (!fill_buffer_format(display)) {
558 fprintf(stderr, "Failed to fill buffer format\n");
559 return 0;
560 }
561
562 if (!queue_initial_buffers(display, buffers)) {
563 fprintf(stderr, "Failed to queue initial buffers\n");
564 return 0;
565 }
566
567 return 1;
568}
569
570static int
571start_capture(struct display *display)
572{
573 int type = display->format.type;
574
575 if (xioctl(display->v4l_fd, VIDIOC_STREAMON, &type) == -1) {
576 perror("VIDIOC_STREAMON");
577 return 0;
578 }
579
580 return 1;
581}
582
583static void
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800584xdg_surface_handle_configure(void *data, struct zxdg_surface_v6 *surface,
585 uint32_t serial)
586{
587 struct window *window = data;
588
589 zxdg_surface_v6_ack_configure(surface, serial);
590
591 if (window->initialized && window->wait_for_configure)
592 redraw(window, NULL, 0);
593 window->wait_for_configure = false;
594}
595
596static const struct zxdg_surface_v6_listener xdg_surface_listener = {
597 xdg_surface_handle_configure,
598};
599
600static void
601xdg_toplevel_handle_configure(void *data, struct zxdg_toplevel_v6 *toplevel,
602 int32_t width, int32_t height,
603 struct wl_array *states)
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000604{
605}
606
607static void
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800608xdg_toplevel_handle_close(void *data, struct zxdg_toplevel_v6 *xdg_toplevel)
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000609{
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800610 running = 0;
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000611}
612
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800613static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
614 xdg_toplevel_handle_configure,
615 xdg_toplevel_handle_close,
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000616};
617
618static struct window *
619create_window(struct display *display)
620{
621 struct window *window;
622
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800623 window = zalloc(sizeof *window);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000624 if (!window)
625 return NULL;
626
627 window->callback = NULL;
628 window->display = display;
629 window->surface = wl_compositor_create_surface(display->compositor);
630
631 if (display->shell) {
632 window->xdg_surface =
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800633 zxdg_shell_v6_get_xdg_surface(display->shell,
634 window->surface);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000635
636 assert(window->xdg_surface);
637
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800638 zxdg_surface_v6_add_listener(window->xdg_surface,
639 &xdg_surface_listener, window);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000640
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800641 window->xdg_toplevel =
642 zxdg_surface_v6_get_toplevel(window->xdg_surface);
643
644 assert(window->xdg_toplevel);
645
646 zxdg_toplevel_v6_add_listener(window->xdg_toplevel,
647 &xdg_toplevel_listener, window);
648
649 zxdg_toplevel_v6_set_title(window->xdg_toplevel, "simple-dmabuf-v4l");
650
651 window->wait_for_configure = true;
652 wl_surface_commit(window->surface);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000653 } else if (display->fshell) {
654 zwp_fullscreen_shell_v1_present_surface(display->fshell,
655 window->surface,
656 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT,
657 NULL);
658 } else {
659 assert(0);
660 }
661
662 return window;
663}
664
665static void
666destroy_window(struct window *window)
667{
668 int i;
669 unsigned j;
670
671 if (window->callback)
672 wl_callback_destroy(window->callback);
673
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800674 if (window->xdg_toplevel)
675 zxdg_toplevel_v6_destroy(window->xdg_toplevel);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000676 if (window->xdg_surface)
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800677 zxdg_surface_v6_destroy(window->xdg_surface);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000678 wl_surface_destroy(window->surface);
679
680 for (i = 0; i < NUM_BUFFERS; i++) {
681 if (!window->buffers[i].buffer)
682 continue;
683
684 wl_buffer_destroy(window->buffers[i].buffer);
685 for (j = 0; j < window->display->format.num_planes; ++j)
686 close(window->buffers[i].dmabuf_fds[j]);
687 }
688
689 v4l_shutdown(window->display);
690
691 free(window);
692}
693
694static const struct wl_callback_listener frame_listener;
695
696static void
697redraw(void *data, struct wl_callback *callback, uint32_t time)
698{
699 struct window *window = data;
700 struct buffer *buffer;
701 int index, num_busy = 0;
702
703 /* Check for a deadlock situation where we would block forever trying
704 * to dequeue a buffer while all of them are locked by the compositor.
705 */
706 for (index = 0; index < NUM_BUFFERS; ++index)
707 if (window->buffers[index].busy)
708 ++num_busy;
709
710 /* A robust application would just postpone redraw until it has queued
711 * a buffer.
712 */
713 assert(num_busy < NUM_BUFFERS);
714
715 index = dequeue(window->display);
716 if (index < 0) {
717 /* We couldn’t get any buffer out of the camera, exiting. */
718 running = false;
719 return;
720 }
721
722 buffer = &window->buffers[index];
723 assert(!buffer->busy);
724
725 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
726 wl_surface_damage(window->surface, 0, 0,
727 window->display->format.width,
728 window->display->format.height);
729
730 if (callback)
731 wl_callback_destroy(callback);
732
733 window->callback = wl_surface_frame(window->surface);
734 wl_callback_add_listener(window->callback, &frame_listener, window);
735 wl_surface_commit(window->surface);
736 buffer->busy = 1;
737}
738
739static const struct wl_callback_listener frame_listener = {
740 redraw
741};
742
743static void
744dmabuf_format(void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf,
745 uint32_t format)
746{
747 struct display *d = data;
748
749 if (format == d->drm_format)
750 d->requested_format_found = true;
751}
752
753static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
754 dmabuf_format
755};
756
757static void
758keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
759 uint32_t format, int fd, uint32_t size)
760{
761 /* Just so we don’t leak the keymap fd */
762 close(fd);
763}
764
765static void
766keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
767 uint32_t serial, struct wl_surface *surface,
768 struct wl_array *keys)
769{
770}
771
772static void
773keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
774 uint32_t serial, struct wl_surface *surface)
775{
776}
777
778static void
779keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
780 uint32_t serial, uint32_t time, uint32_t key,
781 uint32_t state)
782{
783 struct display *d = data;
784
785 if (!d->shell)
786 return;
787
788 if (key == KEY_ESC && state)
789 running = false;
790}
791
792static void
793keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
794 uint32_t serial, uint32_t mods_depressed,
795 uint32_t mods_latched, uint32_t mods_locked,
796 uint32_t group)
797{
798}
799
800static const struct wl_keyboard_listener keyboard_listener = {
801 keyboard_handle_keymap,
802 keyboard_handle_enter,
803 keyboard_handle_leave,
804 keyboard_handle_key,
805 keyboard_handle_modifiers,
806};
807
808static void
809seat_handle_capabilities(void *data, struct wl_seat *seat,
810 enum wl_seat_capability caps)
811{
812 struct display *d = data;
813
814 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
815 d->keyboard = wl_seat_get_keyboard(seat);
816 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
817 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
818 wl_keyboard_destroy(d->keyboard);
819 d->keyboard = NULL;
820 }
821}
822
823static const struct wl_seat_listener seat_listener = {
824 seat_handle_capabilities,
825};
826
827static void
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800828xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000829{
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800830 zxdg_shell_v6_pong(shell, serial);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000831}
832
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800833static const struct zxdg_shell_v6_listener xdg_shell_listener = {
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000834 xdg_shell_ping,
835};
836
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000837static void
838registry_handle_global(void *data, struct wl_registry *registry,
839 uint32_t id, const char *interface, uint32_t version)
840{
841 struct display *d = data;
842
843 if (strcmp(interface, "wl_compositor") == 0) {
844 d->compositor =
845 wl_registry_bind(registry,
846 id, &wl_compositor_interface, 1);
847 } else if (strcmp(interface, "wl_seat") == 0) {
848 d->seat = wl_registry_bind(registry,
849 id, &wl_seat_interface, 1);
850 wl_seat_add_listener(d->seat, &seat_listener, d);
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800851 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000852 d->shell = wl_registry_bind(registry,
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800853 id, &zxdg_shell_v6_interface, 1);
854 zxdg_shell_v6_add_listener(d->shell, &xdg_shell_listener, d);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000855 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
856 d->fshell = wl_registry_bind(registry,
857 id, &zwp_fullscreen_shell_v1_interface,
858 1);
859 } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0) {
860 d->dmabuf = wl_registry_bind(registry,
861 id, &zwp_linux_dmabuf_v1_interface,
862 1);
863 zwp_linux_dmabuf_v1_add_listener(d->dmabuf, &dmabuf_listener,
864 d);
865 }
866}
867
868static void
869registry_handle_global_remove(void *data, struct wl_registry *registry,
870 uint32_t name)
871{
872}
873
874static const struct wl_registry_listener registry_listener = {
875 registry_handle_global,
876 registry_handle_global_remove
877};
878
879static struct display *
880create_display(uint32_t requested_format)
881{
882 struct display *display;
883
884 display = malloc(sizeof *display);
885 if (display == NULL) {
886 fprintf(stderr, "out of memory\n");
887 exit(1);
888 }
889 display->display = wl_display_connect(NULL);
890 assert(display->display);
891
892 display->drm_format = requested_format;
893
894 display->registry = wl_display_get_registry(display->display);
895 wl_registry_add_listener(display->registry,
896 &registry_listener, display);
897 wl_display_roundtrip(display->display);
898 if (display->dmabuf == NULL) {
899 fprintf(stderr, "No zwp_linux_dmabuf global\n");
900 exit(1);
901 }
902
903 wl_display_roundtrip(display->display);
904
905 /* XXX: fake, because the compositor does not yet advertise anything */
906 display->requested_format_found = true;
907
908 if (!display->requested_format_found) {
909 fprintf(stderr, "DRM_FORMAT_YUYV not available\n");
910 exit(1);
911 }
912
913 return display;
914}
915
916static void
917destroy_display(struct display *display)
918{
919 if (display->dmabuf)
920 zwp_linux_dmabuf_v1_destroy(display->dmabuf);
921
922 if (display->shell)
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800923 zxdg_shell_v6_destroy(display->shell);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000924
925 if (display->fshell)
926 zwp_fullscreen_shell_v1_release(display->fshell);
927
928 if (display->compositor)
929 wl_compositor_destroy(display->compositor);
930
931 wl_registry_destroy(display->registry);
932 wl_display_flush(display->display);
933 wl_display_disconnect(display->display);
934 free(display);
935}
936
937static void
938usage(const char *argv0)
939{
940 printf("Usage: %s [V4L2 device] [V4L2 format] [DRM format]\n"
941 "\n"
942 "The default V4L2 device is /dev/video0\n"
943 "\n"
944 "Both formats are FOURCC values (see http://fourcc.org/)\n"
945 "V4L2 formats are defined in <linux/videodev2.h>\n"
946 "DRM formats are defined in <libdrm/drm_fourcc.h>\n"
947 "The default for both formats is YUYV.\n"
948 "If the V4L2 and DRM formats differ, the data is simply "
949 "reinterpreted rather than converted.\n", argv0);
Pekka Paalanendbb85d72016-07-04 16:25:15 +0300950
951 printf("\n"
952 "How to set up Vivid the virtual video driver for testing:\n"
953 "- build your kernel with CONFIG_VIDEO_VIVID=m\n"
954 "- add this to a /etc/modprobe.d/ file:\n"
955 " options vivid node_types=0x1 num_inputs=1 input_types=0x00\n"
956 "- modprobe vivid and check which device was created,\n"
957 " here we assume /dev/video0\n"
958 "- set the pixel format:\n"
959 " $ v4l2-ctl -d /dev/video0 --set-fmt-video=width=640,pixelformat=XR24\n"
960 "- launch the demo:\n"
961 " $ %s /dev/video0 XR24 XR24\n"
962 "You should see a test pattern with color bars, and some text.\n"
963 "\n"
964 "More about vivid: https://www.kernel.org/doc/Documentation/video4linux/vivid.txt\n"
965 "\n", argv0);
966
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000967 exit(0);
968}
969
970static void
971signal_int(int signum)
972{
973 running = false;
974}
975
976int
977main(int argc, char **argv)
978{
979 struct sigaction sigint;
980 struct display *display;
981 struct window *window;
982 const char *v4l_device;
983 uint32_t v4l_format, drm_format;
984 int ret = 0;
985
986 if (argc < 2) {
987 v4l_device = "/dev/video0";
988 } else if (!strcmp(argv[1], "--help")) {
989 usage(argv[0]);
990 } else {
991 v4l_device = argv[1];
992 }
993
994 if (argc < 3)
995 v4l_format = parse_format("YUYV");
996 else
997 v4l_format = parse_format(argv[2]);
998
999 if (argc < 4)
1000 drm_format = v4l_format;
1001 else
1002 drm_format = parse_format(argv[3]);
1003
1004 display = create_display(drm_format);
1005 display->format.format = v4l_format;
1006
1007 window = create_window(display);
1008 if (!window)
1009 return 1;
1010
1011 if (!v4l_connect(display, v4l_device))
1012 return 1;
1013
1014 if (!v4l_init(display, window->buffers))
1015 return 1;
1016
1017 sigint.sa_handler = signal_int;
1018 sigemptyset(&sigint.sa_mask);
1019 sigint.sa_flags = SA_RESETHAND;
1020 sigaction(SIGINT, &sigint, NULL);
1021
1022 /* Here we retrieve the linux-dmabuf objects, or error */
1023 wl_display_roundtrip(display->display);
1024
1025 /* In case of error, running will be 0 */
1026 if (!running)
1027 return 1;
1028
1029 /* We got all of our buffers, we can start the capture! */
1030 if (!start_capture(display))
1031 return 1;
1032
Jonas Ådahl6ccd2862016-08-12 10:01:29 +08001033 window->initialized = true;
1034
1035 if (!window->wait_for_configure)
1036 redraw(window, NULL, 0);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +00001037
1038 while (running && ret != -1)
1039 ret = wl_display_dispatch(display->display);
1040
1041 fprintf(stderr, "simple-dmabuf-v4l exiting\n");
1042 destroy_window(window);
1043 destroy_display(display);
1044
1045 return 0;
1046}