{"id":518,"url":"https://patchwork.libcamera.org/api/patches/518/?format=json","web_url":"https://patchwork.libcamera.org/patch/518/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20190206060818.13907-4-laurent.pinchart@ideasonboard.com>","date":"2019-02-06T06:07:54","name":"[libcamera-devel,03/27] libcamera: v4l2_device: Request buffers from the device","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"5590b28e705e0867cdec379f7f8035858ac94d68","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/518/mbox/","series":[{"id":170,"url":"https://patchwork.libcamera.org/api/series/170/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=170","date":"2019-02-06T06:07:51","name":"Capture frames throught requests","version":1,"mbox":"https://patchwork.libcamera.org/series/170/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/518/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/518/checks/","tags":{},"headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 12EE361022\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  6 Feb 2019 07:08:24 +0100 (CET)","from pendragon.ideasonboard.com (d51A4137F.access.telenet.be\n\t[81.164.19.127])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id AE58141\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  6 Feb 2019 07:08:23 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1549433303;\n\tbh=wm9SKSfXHld43zjn4G73Pb8itDkjVwXqFj0dqKP0qL0=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=kq+hJ6WK2BmEBQBdreUYmgVrFCCCtsXj0LFwwSP5qQNCiqN0JCQ4mUYFjV6PMninb\n\tfuhyvx9p1diBhcVS13xdBiJyx1YID+LPLuAJVPOWiCkJwM776p/HtpsERqDsgpCEPG\n\t5SpeMbuLurHxZroVSOUYyxh2/GyndrRC7eQkvLXw=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Wed,  6 Feb 2019 08:07:54 +0200","Message-Id":"<20190206060818.13907-4-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.19.2","In-Reply-To":"<20190206060818.13907-1-laurent.pinchart@ideasonboard.com>","References":"<20190206060818.13907-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 03/27] libcamera: v4l2_device: Request\n\tbuffers from the device","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.23","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","X-List-Received-Date":"Wed, 06 Feb 2019 06:08:24 -0000"},"content":"From: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\nProvide an exportBuffers() function which allocates buffers with the MMAP\nmethod, exports them using the dmabuf API and populates the given BufferPool.\n\nSigned-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\n src/libcamera/include/v4l2_device.h |  17 ++-\n src/libcamera/v4l2_device.cpp       | 158 +++++++++++++++++++++++++++-\n 2 files changed, 173 insertions(+), 2 deletions(-)","diff":"diff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h\nindex 87cde10d2417..510b74f12d05 100644\n--- a/src/libcamera/include/v4l2_device.h\n+++ b/src/libcamera/include/v4l2_device.h\n@@ -8,11 +8,16 @@\n #define __LIBCAMERA_V4L2_DEVICE_H__\n \n #include <string>\n+#include <vector>\n \n #include <linux/videodev2.h>\n \n namespace libcamera {\n \n+class Buffer;\n+class BufferPool;\n+class MediaEntity;\n+\n struct V4L2Capability final : v4l2_capability {\n \tconst char *driver() const\n \t{\n@@ -67,7 +72,6 @@ public:\n \tunsigned int planesCount;\n };\n \n-class MediaEntity;\n class V4L2Device\n {\n public:\n@@ -89,6 +93,9 @@ public:\n \tint getFormat(V4L2DeviceFormat *format);\n \tint setFormat(V4L2DeviceFormat *format);\n \n+\tint exportBuffers(unsigned int count, BufferPool *pool);\n+\tint releaseBuffers();\n+\n private:\n \tint getFormatSingleplane(V4L2DeviceFormat *format);\n \tint setFormatSingleplane(V4L2DeviceFormat *format);\n@@ -96,10 +103,18 @@ private:\n \tint getFormatMultiplane(V4L2DeviceFormat *format);\n \tint setFormatMultiplane(V4L2DeviceFormat *format);\n \n+\tint requestBuffers(unsigned int count);\n+\tint createPlane(Buffer *buffer, unsigned int plane,\n+\t\t\tunsigned int length);\n+\n \tstd::string deviceNode_;\n \tint fd_;\n \tV4L2Capability caps_;\n+\n \tenum v4l2_buf_type bufferType_;\n+\tenum v4l2_memory memoryType_;\n+\n+\tBufferPool *bufferPool_;\n };\n \n } /* namespace libcamera */\ndiff --git a/src/libcamera/v4l2_device.cpp b/src/libcamera/v4l2_device.cpp\nindex 1823457529f5..2d0a1cb6abbe 100644\n--- a/src/libcamera/v4l2_device.cpp\n+++ b/src/libcamera/v4l2_device.cpp\n@@ -10,6 +10,9 @@\n #include <sys/ioctl.h>\n #include <sys/mman.h>\n #include <unistd.h>\n+#include <vector>\n+\n+#include <libcamera/buffer.h>\n \n #include \"log.h\"\n #include \"media_object.h\"\n@@ -209,8 +212,14 @@ LOG_DEFINE_CATEGORY(V4L2)\n  * \\param deviceNode The file-system path to the video device node\n  */\n V4L2Device::V4L2Device(const std::string &deviceNode)\n-\t: deviceNode_(deviceNode), fd_(-1)\n+\t: deviceNode_(deviceNode), fd_(-1), bufferPool_(nullptr)\n {\n+\t/*\n+\t * We default to an MMAP based CAPTURE device, however this will be\n+\t * updated based upon the device capabilities.\n+\t */\n+\tbufferType_ = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;\n+\tmemoryType_ = V4L2_MEMORY_MMAP;\n }\n \n /**\n@@ -305,6 +314,8 @@ void V4L2Device::close()\n \tif (fd_ < 0)\n \t\treturn;\n \n+\treleaseBuffers();\n+\n \t::close(fd_);\n \tfd_ = -1;\n }\n@@ -475,4 +486,149 @@ int V4L2Device::setFormatMultiplane(V4L2DeviceFormat *format)\n \treturn 0;\n }\n \n+int V4L2Device::requestBuffers(unsigned int count)\n+{\n+\tstruct v4l2_requestbuffers rb = {};\n+\tint ret;\n+\n+\trb.count = count;\n+\trb.type = bufferType_;\n+\trb.memory = memoryType_;\n+\n+\tret = ioctl(fd_, VIDIOC_REQBUFS, &rb);\n+\tif (ret < 0) {\n+\t\tret = -errno;\n+\t\tLOG(V4L2, Error)\n+\t\t\t<< \"Unable to request \" << count << \" buffers: \"\n+\t\t\t<< strerror(-ret);\n+\t\treturn ret;\n+\t}\n+\n+\tLOG(V4L2, Debug)\n+\t\t<< deviceNode_ << \":\" << rb.count << \" buffers requested.\";\n+\n+\treturn rb.count;\n+}\n+\n+/**\n+ * \\brief Request \\a count buffers to be allocated from the device and stored in\n+ * the buffer pool provided.\n+ * \\param[in] count Number of buffers to allocate\n+ * \\param[out] pool BufferPool to populate with buffers\n+ * \\return 0 on success or a negative error code otherwise\n+ */\n+int V4L2Device::exportBuffers(unsigned int count, BufferPool *pool)\n+{\n+\tunsigned int allocatedBuffers;\n+\tunsigned int i;\n+\tint ret;\n+\n+\tmemoryType_ = V4L2_MEMORY_MMAP;\n+\n+\tret = requestBuffers(count);\n+\tif (ret < 0)\n+\t\treturn ret;\n+\n+\tallocatedBuffers = ret;\n+\tif (allocatedBuffers < count) {\n+\t\tLOG(V4L2, Error) << \"Not enough buffers provided by V4L2Device\";\n+\t\trequestBuffers(0);\n+\t\treturn -ENOMEM;\n+\t}\n+\n+\tcount = ret;\n+\n+\t/* Map the buffers. */\n+\tfor (i = 0; i < count; ++i) {\n+\t\tstruct v4l2_plane planes[VIDEO_MAX_PLANES] = {};\n+\t\tstruct v4l2_buffer buf = {};\n+\t\tstruct Buffer &buffer = pool->buffers()[i];\n+\n+\t\tbuf.index = i;\n+\t\tbuf.type = bufferType_;\n+\t\tbuf.memory = memoryType_;\n+\t\tbuf.length = VIDEO_MAX_PLANES;\n+\t\tbuf.m.planes = planes;\n+\n+\t\tret = ioctl(fd_, VIDIOC_QUERYBUF, &buf);\n+\t\tif (ret < 0) {\n+\t\t\tret = -errno;\n+\t\t\tLOG(V4L2, Error)\n+\t\t\t\t<< \"Unable to query buffer \" << i << \": \"\n+\t\t\t\t<< strerror(-ret);\n+\t\t\tbreak;\n+\t\t}\n+\n+\t\tif (V4L2_TYPE_IS_MULTIPLANAR(buf.type)) {\n+\t\t\tfor (unsigned int p = 0; p < buf.length; ++p) {\n+\t\t\t\tret = createPlane(&buffer, p,\n+\t\t\t\t\t\t  buf.m.planes[p].length);\n+\t\t\t\tif (ret)\n+\t\t\t\t\tbreak;\n+\t\t\t}\n+\t\t} else {\n+\t\t\tret = createPlane(&buffer, 0, buf.length);\n+\t\t}\n+\n+\t\tif (ret) {\n+\t\t\tLOG(V4L2, Error) << \"Failed to create plane\";\n+\t\t\tbreak;\n+\t\t}\n+\t}\n+\n+\tif (ret) {\n+\t\trequestBuffers(0);\n+\t\tpool->destroyBuffers();\n+\t\treturn ret;\n+\t}\n+\n+\tbufferPool_ = pool;\n+\n+\treturn 0;\n+}\n+\n+int V4L2Device::createPlane(Buffer *buffer, unsigned int planeIndex,\n+\t\t\t    unsigned int length)\n+{\n+\tstruct v4l2_exportbuffer expbuf = {};\n+\tint ret;\n+\n+\tLOG(V4L2, Debug)\n+\t\t<< \"Buffer \" << buffer->index()\n+\t\t<< \" plane \" << planeIndex\n+\t\t<< \": length=\" << length;\n+\n+\texpbuf.type = bufferType_;\n+\texpbuf.index = buffer->index();\n+\texpbuf.plane = planeIndex;\n+\texpbuf.flags = O_RDWR;\n+\n+\tret = ioctl(fd_, VIDIOC_EXPBUF, &expbuf);\n+\tif (ret < 0) {\n+\t\tret = -errno;\n+\t\tLOG(V4L2, Error)\n+\t\t\t<< \"Failed to export buffer: \" << strerror(-ret);\n+\t\treturn ret;\n+\t}\n+\n+\tbuffer->planes().emplace_back();\n+\tPlane &plane = buffer->planes().back();\n+\tplane.setDmabuf(expbuf.fd, length);\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief Release all internally allocated buffers\n+ */\n+int V4L2Device::releaseBuffers()\n+{\n+\tLOG(V4L2, Debug) << \"Releasing bufferPool\";\n+\n+\trequestBuffers(0);\n+\tbufferPool_ = nullptr;\n+\n+\treturn 0;\n+}\n+\n } /* namespace libcamera */\n","prefixes":["libcamera-devel","03/27"]}