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