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