1From 57fb6af4ea3ed3b21511dca0c443721ee7a67ba3 Mon Sep 17 00:00:00 2001
2From: "vicent.chi" <vicent.chi@rock-chips.com>
3Date: Thu, 31 Oct 2019 11:46:12 +0800
4Subject: [PATCH 3/6] libv4l: Support V4L2_MEMORY_DMABUF
5
6Signed-off-by: vicent.chi <vicent.chi@rock-chips.com>
7Signed-off-by: Jeffy Chen <jeffy.chen@rock-chips.com>
8---
9 lib/libv4l2/libv4l2-priv.h |  1 +
10 lib/libv4l2/libv4l2.c      | 23 +++++++++++++++++++++--
11 2 files changed, 22 insertions(+), 2 deletions(-)
12
13diff --git a/lib/libv4l2/libv4l2-priv.h b/lib/libv4l2/libv4l2-priv.h
14index 1924c91..30dd1bc 100644
15--- a/lib/libv4l2/libv4l2-priv.h
16+++ b/lib/libv4l2/libv4l2-priv.h
17@@ -104,6 +104,7 @@ struct v4l2_dev_info {
18 	void *plugin_library;
19 	void *dev_ops_priv;
20 	const struct libv4l_dev_ops *dev_ops;
21+	int has_dmabuf_memory;
22 };
23
24 /* From v4l2-plugin.c */
25diff --git a/lib/libv4l2/libv4l2.c b/lib/libv4l2/libv4l2.c
26index 0c02bec..efc9465 100644
27--- a/lib/libv4l2/libv4l2.c
28+++ b/lib/libv4l2/libv4l2.c
29@@ -543,7 +543,7 @@ static int v4l2_deactivate_read_stream(int index)
30
31 static int v4l2_needs_conversion(int index)
32 {
33-	if (devices[index].convert == NULL)
34+	if (devices[index].convert == NULL || devices[index].has_dmabuf_memory)
35 		return 0;
36
37 	return v4lconvert_needs_conversion(devices[index].convert,
38@@ -1305,12 +1305,18 @@ no_capture_request:
39 		struct v4l2_requestbuffers *req = arg;
40
41 		/* IMPROVEME (maybe?) add support for userptr's? */
42-		if (req->memory != V4L2_MEMORY_MMAP) {
43+		if (req->memory != V4L2_MEMORY_MMAP && req->memory != V4L2_MEMORY_DMABUF) {
44 			errno = EINVAL;
45 			result = -1;
46 			break;
47 		}
48
49+		if (req->memory == V4L2_MEMORY_DMABUF) {
50+			devices[index].has_dmabuf_memory = 1;
51+			V4L2_LOG("memory type is V4L2_MEMORY_DMABUF, "
52+			         "buf conversion and mmap emulation are disabled\n");
53+		}
54+
55 		result = v4l2_check_buffer_change_ok(index);
56 		if (result)
57 			break;
58@@ -1563,6 +1569,14 @@ ssize_t v4l2_read(int fd, void *dest, size_t n)
59 		goto leave;
60 	}
61
62+	if (!(devices[index].flags & V4L2_USE_READ_FOR_READ) &&
63+	    devices[index].has_dmabuf_memory) {
64+		V4L2_PERROR("memory type is V4L2_MEMORY_DMABUF, "
65+		            "no support v4l2 read\n");
66+		errno = EINVAL;
67+		return -1;
68+	}
69+
70 	/* Since we need to do conversion try to use mmap (streaming) mode under
71 	   the hood as that safes a memcpy for each frame read.
72
73@@ -1627,6 +1641,7 @@ void *v4l2_mmap(void *start, size_t length, int prot, int flags, int fd,
74 	void *result;
75
76 	index = v4l2_get_index(fd);
77+
78 	if (index != -1 && devices[index].dev_ops->mmap) {
79 		pthread_mutex_lock(&devices[index].stream_lock);
80 		result = devices[index].dev_ops->mmap(
81@@ -1637,6 +1652,10 @@ void *v4l2_mmap(void *start, size_t length, int prot, int flags, int fd,
82 		return result;
83 	}
84
85+	if (index != -1 && devices[index].has_dmabuf_memory) {
86+		return (void *)SYS_MMAP(start, length, prot, flags, fd, offset);
87+	}
88+
89 	if (index == -1 ||
90 			/* Check if the mmap data matches our answer to QUERY_BUF. If it doesn't,
91 			   let the kernel handle it (to allow for mmap-based non capture use) */
92--
932.20.1
94
95