blob: af25d0eaa13b37dce9396d6bb7e2307ddb714d70 [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
354static void
355create_dmabuf_buffer(struct display *display, struct buffer *buffer)
356{
357 struct zwp_linux_buffer_params_v1 *params;
358 uint64_t modifier;
Daniel Stoned5fa0902017-02-08 12:02:03 +0000359 uint32_t flags;
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000360 unsigned i;
361
362 modifier = 0;
Daniel Stoned5fa0902017-02-08 12:02:03 +0000363 flags = 0;
Pekka Paalanen319397e2016-07-04 16:25:16 +0300364
Daniel Stoned5fa0902017-02-08 12:02:03 +0000365 /* XXX: apparently some webcams may actually provide y-inverted images,
366 * in which case we should set
367 * flags = ZWP_LINUX_BUFFER_PARAMS_V1_FLAGS_Y_INVERT
368 */
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000369
370 params = zwp_linux_dmabuf_v1_create_params(display->dmabuf);
371 for (i = 0; i < display->format.num_planes; ++i)
372 zwp_linux_buffer_params_v1_add(params,
373 buffer->dmabuf_fds[i],
374 i, /* plane_idx */
375 buffer->data_offsets[i], /* offset */
376 display->format.strides[i],
377 modifier >> 32,
378 modifier & 0xffffffff);
379 zwp_linux_buffer_params_v1_add_listener(params, &params_listener,
380 buffer);
381 zwp_linux_buffer_params_v1_create(params,
382 display->format.width,
383 display->format.height,
384 display->drm_format,
Daniel Stoned5fa0902017-02-08 12:02:03 +0000385 flags);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000386}
387
388static int
389buffer_export(struct display *display, int index, int dmafd[])
390{
391 struct v4l2_exportbuffer expbuf;
392 unsigned i;
393
394 CLEAR(expbuf);
395
396 for (i = 0; i < display->format.num_planes; ++i) {
397 expbuf.type = display->format.type;
398 expbuf.index = index;
399 expbuf.plane = i;
400 if (xioctl(display->v4l_fd, VIDIOC_EXPBUF, &expbuf) == -1) {
401 perror("VIDIOC_EXPBUF");
402 while (i)
403 close(dmafd[--i]);
404 return 0;
405 }
406 dmafd[i] = expbuf.fd;
407 }
408
409 return 1;
410}
411
412static int
413queue_initial_buffers(struct display *display,
414 struct buffer buffers[NUM_BUFFERS])
415{
416 struct buffer *buffer;
417 int index;
418
419 for (index = 0; index < NUM_BUFFERS; ++index) {
420 buffer = &buffers[index];
421 buffer->display = display;
422 buffer->index = index;
423
424 if (!queue(display, buffer)) {
425 fprintf(stderr, "Failed to queue buffer\n");
426 return 0;
427 }
428
429 assert(!buffer->buffer);
430 if (!buffer_export(display, index, buffer->dmabuf_fds))
431 return 0;
432
433 create_dmabuf_buffer(display, buffer);
434 }
435
436 return 1;
437}
438
439static int
440dequeue(struct display *display)
441{
442 struct v4l2_buffer buf;
443 struct v4l2_plane planes[VIDEO_MAX_PLANES];
444
445 CLEAR(buf);
446 buf.type = display->format.type;
447 buf.memory = V4L2_MEMORY_MMAP;
448 buf.length = VIDEO_MAX_PLANES;
449 buf.m.planes = planes;
450
451 /* This ioctl is blocking until a buffer is ready to be displayed */
452 if (xioctl(display->v4l_fd, VIDIOC_DQBUF, &buf) == -1) {
453 perror("VIDIOC_DQBUF");
454 return -1;
455 }
456
457 assert(buf.flags & V4L2_BUF_FLAG_DONE);
458
459 return buf.index;
460}
461
462static int
463fill_buffer_format(struct display *display)
464{
465 struct v4l2_format fmt;
466 struct v4l2_pix_format *pix;
467 struct v4l2_pix_format_mplane *pix_mp;
468 int i;
469 char buf[4];
470
471 CLEAR(fmt);
472 fmt.type = display->format.type;
473
474 /* Preserve original settings as set by v4l2-ctl for example */
475 if (xioctl(display->v4l_fd, VIDIOC_G_FMT, &fmt) == -1) {
476 perror("VIDIOC_G_FMT");
477 return 0;
478 }
479
480 if (display->format.type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
481 pix = &fmt.fmt.pix;
482
483 printf("%d×%d, %.4s\n", pix->width, pix->height,
484 dump_format(pix->pixelformat, buf));
485
486 display->format.num_planes = 1;
487 display->format.width = pix->width;
488 display->format.height = pix->height;
489 display->format.strides[0] = pix->bytesperline;
490 } else {
491 pix_mp = &fmt.fmt.pix_mp;
492
493 display->format.num_planes = pix_mp->num_planes;
494 display->format.width = pix_mp->width;
495 display->format.height = pix_mp->height;
496
497 for (i = 0; i < pix_mp->num_planes; ++i)
498 display->format.strides[i] = pix_mp->plane_fmt[i].bytesperline;
499
500 printf("%d×%d, %.4s, %d planes\n",
501 pix_mp->width, pix_mp->height,
502 dump_format(pix_mp->pixelformat, buf),
503 pix_mp->num_planes);
504 }
505
506 return 1;
507}
508
509static int
510v4l_init(struct display *display, struct buffer buffers[NUM_BUFFERS]) {
511 if (!fill_buffer_format(display)) {
512 fprintf(stderr, "Failed to fill buffer format\n");
513 return 0;
514 }
515
516 if (!queue_initial_buffers(display, buffers)) {
517 fprintf(stderr, "Failed to queue initial buffers\n");
518 return 0;
519 }
520
521 return 1;
522}
523
524static int
525start_capture(struct display *display)
526{
527 int type = display->format.type;
528
529 if (xioctl(display->v4l_fd, VIDIOC_STREAMON, &type) == -1) {
530 perror("VIDIOC_STREAMON");
531 return 0;
532 }
533
534 return 1;
535}
536
537static void
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800538xdg_surface_handle_configure(void *data, struct zxdg_surface_v6 *surface,
539 uint32_t serial)
540{
541 struct window *window = data;
542
543 zxdg_surface_v6_ack_configure(surface, serial);
544
545 if (window->initialized && window->wait_for_configure)
546 redraw(window, NULL, 0);
547 window->wait_for_configure = false;
548}
549
550static const struct zxdg_surface_v6_listener xdg_surface_listener = {
551 xdg_surface_handle_configure,
552};
553
554static void
555xdg_toplevel_handle_configure(void *data, struct zxdg_toplevel_v6 *toplevel,
556 int32_t width, int32_t height,
557 struct wl_array *states)
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000558{
559}
560
561static void
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800562xdg_toplevel_handle_close(void *data, struct zxdg_toplevel_v6 *xdg_toplevel)
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000563{
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800564 running = 0;
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000565}
566
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800567static const struct zxdg_toplevel_v6_listener xdg_toplevel_listener = {
568 xdg_toplevel_handle_configure,
569 xdg_toplevel_handle_close,
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000570};
571
572static struct window *
573create_window(struct display *display)
574{
575 struct window *window;
576
Bryce Harrington0d1a6222016-02-11 16:42:49 -0800577 window = zalloc(sizeof *window);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000578 if (!window)
579 return NULL;
580
581 window->callback = NULL;
582 window->display = display;
583 window->surface = wl_compositor_create_surface(display->compositor);
584
585 if (display->shell) {
586 window->xdg_surface =
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800587 zxdg_shell_v6_get_xdg_surface(display->shell,
588 window->surface);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000589
590 assert(window->xdg_surface);
591
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800592 zxdg_surface_v6_add_listener(window->xdg_surface,
593 &xdg_surface_listener, window);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000594
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800595 window->xdg_toplevel =
596 zxdg_surface_v6_get_toplevel(window->xdg_surface);
597
598 assert(window->xdg_toplevel);
599
600 zxdg_toplevel_v6_add_listener(window->xdg_toplevel,
601 &xdg_toplevel_listener, window);
602
603 zxdg_toplevel_v6_set_title(window->xdg_toplevel, "simple-dmabuf-v4l");
604
605 window->wait_for_configure = true;
606 wl_surface_commit(window->surface);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000607 } else if (display->fshell) {
608 zwp_fullscreen_shell_v1_present_surface(display->fshell,
609 window->surface,
610 ZWP_FULLSCREEN_SHELL_V1_PRESENT_METHOD_DEFAULT,
611 NULL);
612 } else {
613 assert(0);
614 }
615
616 return window;
617}
618
619static void
620destroy_window(struct window *window)
621{
622 int i;
623 unsigned j;
624
625 if (window->callback)
626 wl_callback_destroy(window->callback);
627
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800628 if (window->xdg_toplevel)
629 zxdg_toplevel_v6_destroy(window->xdg_toplevel);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000630 if (window->xdg_surface)
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800631 zxdg_surface_v6_destroy(window->xdg_surface);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000632 wl_surface_destroy(window->surface);
633
634 for (i = 0; i < NUM_BUFFERS; i++) {
635 if (!window->buffers[i].buffer)
636 continue;
637
638 wl_buffer_destroy(window->buffers[i].buffer);
639 for (j = 0; j < window->display->format.num_planes; ++j)
640 close(window->buffers[i].dmabuf_fds[j]);
641 }
642
643 v4l_shutdown(window->display);
644
645 free(window);
646}
647
648static const struct wl_callback_listener frame_listener;
649
650static void
651redraw(void *data, struct wl_callback *callback, uint32_t time)
652{
653 struct window *window = data;
654 struct buffer *buffer;
655 int index, num_busy = 0;
656
657 /* Check for a deadlock situation where we would block forever trying
658 * to dequeue a buffer while all of them are locked by the compositor.
659 */
660 for (index = 0; index < NUM_BUFFERS; ++index)
661 if (window->buffers[index].busy)
662 ++num_busy;
663
664 /* A robust application would just postpone redraw until it has queued
665 * a buffer.
666 */
667 assert(num_busy < NUM_BUFFERS);
668
669 index = dequeue(window->display);
670 if (index < 0) {
671 /* We couldn’t get any buffer out of the camera, exiting. */
672 running = false;
673 return;
674 }
675
676 buffer = &window->buffers[index];
677 assert(!buffer->busy);
678
679 wl_surface_attach(window->surface, buffer->buffer, 0, 0);
680 wl_surface_damage(window->surface, 0, 0,
681 window->display->format.width,
682 window->display->format.height);
683
684 if (callback)
685 wl_callback_destroy(callback);
686
687 window->callback = wl_surface_frame(window->surface);
688 wl_callback_add_listener(window->callback, &frame_listener, window);
689 wl_surface_commit(window->surface);
690 buffer->busy = 1;
691}
692
693static const struct wl_callback_listener frame_listener = {
694 redraw
695};
696
697static void
698dmabuf_format(void *data, struct zwp_linux_dmabuf_v1 *zwp_linux_dmabuf,
699 uint32_t format)
700{
701 struct display *d = data;
702
703 if (format == d->drm_format)
704 d->requested_format_found = true;
705}
706
707static const struct zwp_linux_dmabuf_v1_listener dmabuf_listener = {
708 dmabuf_format
709};
710
711static void
712keyboard_handle_keymap(void *data, struct wl_keyboard *keyboard,
713 uint32_t format, int fd, uint32_t size)
714{
715 /* Just so we don’t leak the keymap fd */
716 close(fd);
717}
718
719static void
720keyboard_handle_enter(void *data, struct wl_keyboard *keyboard,
721 uint32_t serial, struct wl_surface *surface,
722 struct wl_array *keys)
723{
724}
725
726static void
727keyboard_handle_leave(void *data, struct wl_keyboard *keyboard,
728 uint32_t serial, struct wl_surface *surface)
729{
730}
731
732static void
733keyboard_handle_key(void *data, struct wl_keyboard *keyboard,
734 uint32_t serial, uint32_t time, uint32_t key,
735 uint32_t state)
736{
737 struct display *d = data;
738
739 if (!d->shell)
740 return;
741
742 if (key == KEY_ESC && state)
743 running = false;
744}
745
746static void
747keyboard_handle_modifiers(void *data, struct wl_keyboard *keyboard,
748 uint32_t serial, uint32_t mods_depressed,
749 uint32_t mods_latched, uint32_t mods_locked,
750 uint32_t group)
751{
752}
753
754static const struct wl_keyboard_listener keyboard_listener = {
755 keyboard_handle_keymap,
756 keyboard_handle_enter,
757 keyboard_handle_leave,
758 keyboard_handle_key,
759 keyboard_handle_modifiers,
760};
761
762static void
763seat_handle_capabilities(void *data, struct wl_seat *seat,
764 enum wl_seat_capability caps)
765{
766 struct display *d = data;
767
768 if ((caps & WL_SEAT_CAPABILITY_KEYBOARD) && !d->keyboard) {
769 d->keyboard = wl_seat_get_keyboard(seat);
770 wl_keyboard_add_listener(d->keyboard, &keyboard_listener, d);
771 } else if (!(caps & WL_SEAT_CAPABILITY_KEYBOARD) && d->keyboard) {
772 wl_keyboard_destroy(d->keyboard);
773 d->keyboard = NULL;
774 }
775}
776
777static const struct wl_seat_listener seat_listener = {
778 seat_handle_capabilities,
779};
780
781static void
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800782xdg_shell_ping(void *data, struct zxdg_shell_v6 *shell, uint32_t serial)
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000783{
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800784 zxdg_shell_v6_pong(shell, serial);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000785}
786
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800787static const struct zxdg_shell_v6_listener xdg_shell_listener = {
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000788 xdg_shell_ping,
789};
790
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000791static void
792registry_handle_global(void *data, struct wl_registry *registry,
793 uint32_t id, const char *interface, uint32_t version)
794{
795 struct display *d = data;
796
797 if (strcmp(interface, "wl_compositor") == 0) {
798 d->compositor =
799 wl_registry_bind(registry,
800 id, &wl_compositor_interface, 1);
801 } else if (strcmp(interface, "wl_seat") == 0) {
802 d->seat = wl_registry_bind(registry,
803 id, &wl_seat_interface, 1);
804 wl_seat_add_listener(d->seat, &seat_listener, d);
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800805 } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000806 d->shell = wl_registry_bind(registry,
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800807 id, &zxdg_shell_v6_interface, 1);
808 zxdg_shell_v6_add_listener(d->shell, &xdg_shell_listener, d);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000809 } else if (strcmp(interface, "zwp_fullscreen_shell_v1") == 0) {
810 d->fshell = wl_registry_bind(registry,
811 id, &zwp_fullscreen_shell_v1_interface,
812 1);
813 } else if (strcmp(interface, "zwp_linux_dmabuf_v1") == 0) {
814 d->dmabuf = wl_registry_bind(registry,
815 id, &zwp_linux_dmabuf_v1_interface,
816 1);
817 zwp_linux_dmabuf_v1_add_listener(d->dmabuf, &dmabuf_listener,
818 d);
819 }
820}
821
822static void
823registry_handle_global_remove(void *data, struct wl_registry *registry,
824 uint32_t name)
825{
826}
827
828static const struct wl_registry_listener registry_listener = {
829 registry_handle_global,
830 registry_handle_global_remove
831};
832
833static struct display *
834create_display(uint32_t requested_format)
835{
836 struct display *display;
837
838 display = malloc(sizeof *display);
839 if (display == NULL) {
840 fprintf(stderr, "out of memory\n");
841 exit(1);
842 }
843 display->display = wl_display_connect(NULL);
844 assert(display->display);
845
846 display->drm_format = requested_format;
847
848 display->registry = wl_display_get_registry(display->display);
849 wl_registry_add_listener(display->registry,
850 &registry_listener, display);
851 wl_display_roundtrip(display->display);
852 if (display->dmabuf == NULL) {
853 fprintf(stderr, "No zwp_linux_dmabuf global\n");
854 exit(1);
855 }
856
857 wl_display_roundtrip(display->display);
858
859 /* XXX: fake, because the compositor does not yet advertise anything */
860 display->requested_format_found = true;
861
862 if (!display->requested_format_found) {
863 fprintf(stderr, "DRM_FORMAT_YUYV not available\n");
864 exit(1);
865 }
866
867 return display;
868}
869
870static void
871destroy_display(struct display *display)
872{
873 if (display->dmabuf)
874 zwp_linux_dmabuf_v1_destroy(display->dmabuf);
875
876 if (display->shell)
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800877 zxdg_shell_v6_destroy(display->shell);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000878
879 if (display->fshell)
880 zwp_fullscreen_shell_v1_release(display->fshell);
881
882 if (display->compositor)
883 wl_compositor_destroy(display->compositor);
884
885 wl_registry_destroy(display->registry);
886 wl_display_flush(display->display);
887 wl_display_disconnect(display->display);
888 free(display);
889}
890
891static void
892usage(const char *argv0)
893{
894 printf("Usage: %s [V4L2 device] [V4L2 format] [DRM format]\n"
895 "\n"
896 "The default V4L2 device is /dev/video0\n"
897 "\n"
898 "Both formats are FOURCC values (see http://fourcc.org/)\n"
899 "V4L2 formats are defined in <linux/videodev2.h>\n"
900 "DRM formats are defined in <libdrm/drm_fourcc.h>\n"
901 "The default for both formats is YUYV.\n"
902 "If the V4L2 and DRM formats differ, the data is simply "
903 "reinterpreted rather than converted.\n", argv0);
Pekka Paalanendbb85d72016-07-04 16:25:15 +0300904
905 printf("\n"
906 "How to set up Vivid the virtual video driver for testing:\n"
907 "- build your kernel with CONFIG_VIDEO_VIVID=m\n"
908 "- add this to a /etc/modprobe.d/ file:\n"
909 " options vivid node_types=0x1 num_inputs=1 input_types=0x00\n"
910 "- modprobe vivid and check which device was created,\n"
911 " here we assume /dev/video0\n"
912 "- set the pixel format:\n"
913 " $ v4l2-ctl -d /dev/video0 --set-fmt-video=width=640,pixelformat=XR24\n"
914 "- launch the demo:\n"
915 " $ %s /dev/video0 XR24 XR24\n"
916 "You should see a test pattern with color bars, and some text.\n"
917 "\n"
918 "More about vivid: https://www.kernel.org/doc/Documentation/video4linux/vivid.txt\n"
919 "\n", argv0);
920
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000921 exit(0);
922}
923
924static void
925signal_int(int signum)
926{
927 running = false;
928}
929
930int
931main(int argc, char **argv)
932{
933 struct sigaction sigint;
934 struct display *display;
935 struct window *window;
936 const char *v4l_device;
937 uint32_t v4l_format, drm_format;
938 int ret = 0;
939
940 if (argc < 2) {
941 v4l_device = "/dev/video0";
942 } else if (!strcmp(argv[1], "--help")) {
943 usage(argv[0]);
944 } else {
945 v4l_device = argv[1];
946 }
947
948 if (argc < 3)
949 v4l_format = parse_format("YUYV");
950 else
951 v4l_format = parse_format(argv[2]);
952
953 if (argc < 4)
954 drm_format = v4l_format;
955 else
956 drm_format = parse_format(argv[3]);
957
958 display = create_display(drm_format);
959 display->format.format = v4l_format;
960
961 window = create_window(display);
962 if (!window)
963 return 1;
964
965 if (!v4l_connect(display, v4l_device))
966 return 1;
967
968 if (!v4l_init(display, window->buffers))
969 return 1;
970
971 sigint.sa_handler = signal_int;
972 sigemptyset(&sigint.sa_mask);
973 sigint.sa_flags = SA_RESETHAND;
974 sigaction(SIGINT, &sigint, NULL);
975
976 /* Here we retrieve the linux-dmabuf objects, or error */
977 wl_display_roundtrip(display->display);
978
979 /* In case of error, running will be 0 */
980 if (!running)
981 return 1;
982
983 /* We got all of our buffers, we can start the capture! */
984 if (!start_capture(display))
985 return 1;
986
Jonas Ådahl6ccd2862016-08-12 10:01:29 +0800987 window->initialized = true;
988
989 if (!window->wait_for_configure)
990 redraw(window, NULL, 0);
Emmanuel Gil Peyrot5d43af32016-01-11 19:04:38 +0000991
992 while (running && ret != -1)
993 ret = wl_display_dispatch(display->display);
994
995 fprintf(stderr, "simple-dmabuf-v4l exiting\n");
996 destroy_window(window);
997 destroy_display(display);
998
999 return 0;
1000}