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