{"id":490,"url":"https://patchwork.libcamera.org/api/1.1/patches/490/?format=json","web_url":"https://patchwork.libcamera.org/patch/490/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20190203110102.5663-4-kieran.bingham@ideasonboard.com>","date":"2019-02-03T11:00:54","name":"[libcamera-devel,03/11] libcamera: v4l2_device: Request buffers from the device","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"eda18ce49fefe06d1a85dfc5f2fef94d527bf861","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/1.1/people/4/?format=json","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/490/mbox/","series":[{"id":165,"url":"https://patchwork.libcamera.org/api/1.1/series/165/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=165","date":"2019-02-03T11:00:51","name":"libcamera: V4L2 Streams","version":1,"mbox":"https://patchwork.libcamera.org/series/165/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/490/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/490/checks/","tags":{},"headers":{"Return-Path":"<kieran.bingham@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 776EF60DB8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  3 Feb 2019 12:01:07 +0100 (CET)","from localhost.localdomain\n\t(218.182-78-194.adsl-static.isp.belgacom.be [194.78.182.218])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 24DF741;\n\tSun,  3 Feb 2019 12:01:07 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1549191667;\n\tbh=vADjpcRolxRvmqMSCf9mbfAOlHvYZfvFHgaf20N6v7U=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=Ecayt8CmuFd1N784orcPDUEoiUrVO2P/mGtVwe6ywks4ErONXmbAkTkTZAb3JlzD8\n\thtgd3fusIxV5u91/l3lWBzX24PXnlKXAmMVhHUZndnIrgPhIizsmvBxIjgd5wGGFzM\n\tVER563935bZFZozVAAqdakRMIaID9y51jzky7E9Q=","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"LibCamera Devel <libcamera-devel@lists.libcamera.org>","Date":"Sun,  3 Feb 2019 12:00:54 +0100","Message-Id":"<20190203110102.5663-4-kieran.bingham@ideasonboard.com>","X-Mailer":"git-send-email 2.19.1","In-Reply-To":"<20190203110102.5663-1-kieran.bingham@ideasonboard.com>","References":"<20190203110102.5663-1-kieran.bingham@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 03/11] 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":"Sun, 03 Feb 2019 11:01:07 -0000"},"content":"Provide a requestBuffers() interface to allocate buffers from the V4L2Device\nand keep a vector of the buffers in the V4L2Device object for later queue and\ndequeue operations.\n\nSigned-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\n include/libcamera/buffer.h          |   4 +-\n src/libcamera/include/v4l2_device.h |  10 +++\n src/libcamera/v4l2_device.cpp       | 111 +++++++++++++++++++++++++++-\n 3 files changed, 122 insertions(+), 3 deletions(-)","diff":"diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h\nindex e730fc0f84fd..97fab5c65cce 100644\n--- a/include/libcamera/buffer.h\n+++ b/include/libcamera/buffer.h\n@@ -71,10 +71,10 @@ public:\n \tvoid free();\n \n \tunsigned int count() const { return buffers_.size(); };\n-\tconst std::vector<Buffer *> &buffers() { return buffers_; };\n+\tstd::vector<Buffer *> &buffers() { return buffers_; };\n \n private:\n-\tvirtual int allocateMemory() = 0;\n+\tvirtual int allocateMemory() { return 0; };\n \n \tBufferPool::Memory memory_;\n \tstd::vector<Buffer *> buffers_;\ndiff --git a/src/libcamera/include/v4l2_device.h b/src/libcamera/include/v4l2_device.h\nindex 9e9eada94130..6599ce2d761c 100644\n--- a/src/libcamera/include/v4l2_device.h\n+++ b/src/libcamera/include/v4l2_device.h\n@@ -8,6 +8,7 @@\n #define __LIBCAMERA_V4L2_DEVICE_H__\n \n #include <string>\n+#include <vector>\n \n #include <linux/videodev2.h>\n \n@@ -76,6 +77,7 @@ public:\n };\n \n class MediaEntity;\n+\n class V4L2Device\n {\n public:\n@@ -97,6 +99,8 @@ public:\n \tint format(V4L2DeviceFormat *fmt);\n \tint setFormat(V4L2DeviceFormat *fmt);\n \n+\tBufferPool *requestBuffers(unsigned int qty = 8);\n+\n private:\n \tint getFormatSingleplane(V4L2DeviceFormat *fmt);\n \tint setFormatSingleplane(V4L2DeviceFormat *fmt);\n@@ -104,10 +108,16 @@ private:\n \tint getFormatMultiplane(V4L2DeviceFormat *fmt);\n \tint setFormatMultiplane(V4L2DeviceFormat *fmt);\n \n+\tint requestBuffers(unsigned int qty, std::vector<Buffer *> &buffers);\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 d507ea9700b3..8bcd8bbc34f6 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@@ -187,8 +190,14 @@ V4L2Buffer::V4L2Buffer(struct v4l2_buffer &vb)\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@@ -205,6 +214,8 @@ V4L2Device::V4L2Device(const MediaEntity &entity)\n V4L2Device::~V4L2Device()\n {\n \tclose();\n+\n+\tdelete bufferPool_;\n }\n \n /**\n@@ -305,6 +316,104 @@ void V4L2Device::close()\n  * \\return The string containing the device location\n  */\n \n+/**\n+ * \\brief Request \\a qty buffers to be allocated from the device and returned\n+ * as a BufferPool\n+ * \\return A BufferPool containing the device buffers, or nullptr if the\n+ * allocation fails\n+ */\n+BufferPool *V4L2Device::requestBuffers(unsigned int qty)\n+{\n+\tint ret;\n+\n+\tif (bufferPool_) {\n+\t\tLOG(V4L2, Warning) << \"Pre-existing buffer pool will be lost\";\n+\t\tdelete bufferPool_;\n+\t}\n+\n+\tbufferPool_ = new BufferPool(BufferPool::Internal);\n+\n+\tret = requestBuffers(qty, bufferPool_->buffers());\n+\tif (ret) {\n+\t\tdelete bufferPool_;\n+\t\tbufferPool_ = nullptr;\n+\t\treturn nullptr;\n+\t}\n+\n+\treturn bufferPool_;\n+}\n+\n+/**\n+ * \\brief Request \\a qty buffers from the V4L2Device and track Buffer\n+ * representations within the specified \\a buffers target.\n+ */\n+int V4L2Device::requestBuffers(unsigned int qty, std::vector<Buffer *> &buffers)\n+{\n+\tstruct v4l2_requestbuffers rb = {};\n+\tunsigned int i;\n+\tint ret;\n+\n+\trb.count = qty;\n+\trb.type = bufferType_;\n+\trb.memory = memoryType_; /** \\todo Support dmaBuf memoryTypes */\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 \" << qty << \" 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+\tif (memoryType_ != V4L2_MEMORY_MMAP) {\n+\t\tLOG(V4L2, Error)\n+\t\t\t<< \"Non-mmap allocations are not yet supported.\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\t/* Map the buffers. */\n+\tfor (i = 0; i < rb.count; ++i) {\n+\t\tstruct v4l2_plane planes[VIDEO_MAX_PLANES] = {};\n+\t\tstruct v4l2_buffer buf = {};\n+\t\tstruct Buffer *buffer;\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\tLOG(V4L2, Error)\n+\t\t\t\t<< \"Unable to query buffer \" << i << \": \"\n+\t\t\t\t<< strerror(errno);\n+\t\t\treturn ret;\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\tLOG(V4L2, Debug)\n+\t\t\t\t\t<< \"plane: \" << p\n+\t\t\t\t\t<< \" length: \" << buf.m.planes[p].length\n+\t\t\t\t\t<< \" offset: \"\n+\t\t\t\t\t<< buf.m.planes[p].data_offset;\n+\t\t} else {\n+\t\t\tLOG(V4L2, Debug) << \"length: \" << buf.length\n+\t\t\t\t\t << \" offset: \" << buf.m.offset;\n+\t\t}\n+\n+\t\tbuffer = new V4L2Buffer(buf);\n+\t\tbuffers.push_back(buffer);\n+\t}\n+\n+\treturn 0;\n+}\n+\n /**\n  * \\brief Retrieve the image format set on the V4L2 device\n  * \\return 0 for success, a negative error code otherwise\n","prefixes":["libcamera-devel","03/11"]}