{"id":1693,"url":"https://patchwork.libcamera.org/api/patches/1693/?format=json","web_url":"https://patchwork.libcamera.org/patch/1693/","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":"<20190713172351.25452-13-laurent.pinchart@ideasonboard.com>","date":"2019-07-13T17:23:47","name":"[libcamera-devel,v2,12/16] libcamera: buffer: Add dmabuf file descriptors","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"510b40580e7499a2fa6f66cb66f0bbe693b72516","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/1693/mbox/","series":[{"id":430,"url":"https://patchwork.libcamera.org/api/series/430/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=430","date":"2019-07-13T17:23:35","name":"Add support for external buffers","version":2,"mbox":"https://patchwork.libcamera.org/series/430/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/1693/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/1693/checks/","tags":{},"headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 3C9E960C23\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 13 Jul 2019 19:24:45 +0200 (CEST)","from pendragon.ideasonboard.com (softbank126209254147.bbtec.net\n\t[126.209.254.147])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 2C4852B2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSat, 13 Jul 2019 19:24:43 +0200 (CEST)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1563038685;\n\tbh=TJRuO3cldATEFSTkNSOhV2RNzkwoVuSxkA+kPa9Z7A8=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=MQ15meMCVSFvPvijCUnDuVmw76POj1Q3ziPCXrUWYYp5E3Ywd7i146rNZBogUWzUh\n\tWKjFHkZgAs886w7ONmWA0V+9jxmokyJ9Z2OzEVF2HhCOdN6N/7eD1KtBPz2jdJASAC\n\t/YnemjJ1mkx1zfkk/UqWnXLdv5VYvc7XrC2l4R1I=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Sat, 13 Jul 2019 20:23:47 +0300","Message-Id":"<20190713172351.25452-13-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.21.0","In-Reply-To":"<20190713172351.25452-1-laurent.pinchart@ideasonboard.com>","References":"<20190713172351.25452-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 12/16] libcamera: buffer: Add dmabuf\n\tfile descriptors","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":"Sat, 13 Jul 2019 17:24:45 -0000"},"content":"From: Jacopo Mondi <jacopo@jmondi.org>\n\nIn addition to referencing buffer memory by index, add support to\nreferencing it using dmabuf file descriptors. This will be used to\nreference buffer memory allocated outside of libcamera and import it.\n\nThe dmabuf file descriptors are stored in an array in the Buffer class,\nand a new Stream::createBuffer() overload is added to construct a buffer\nfrom dmabuf file descriptor.\n\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/buffer.h |  3 +++\n include/libcamera/stream.h |  1 +\n src/libcamera/buffer.cpp   | 13 ++++++++++-\n src/libcamera/request.cpp  |  5 ++++\n src/libcamera/stream.cpp   | 47 +++++++++++++++++++++++++++++++++++++-\n 5 files changed, 67 insertions(+), 2 deletions(-)","diff":"diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h\nindex f5ba6207bcef..f8569a6b67f3 100644\n--- a/include/libcamera/buffer.h\n+++ b/include/libcamera/buffer.h\n@@ -7,6 +7,7 @@\n #ifndef __LIBCAMERA_BUFFER_H__\n #define __LIBCAMERA_BUFFER_H__\n \n+#include <array>\n #include <stdint.h>\n #include <vector>\n \n@@ -75,6 +76,7 @@ public:\n \tBuffer &operator=(const Buffer &) = delete;\n \n \tunsigned int index() const { return index_; }\n+\tconst std::array<int, 3> &dmabufs() const { return dmabuf_; }\n \n \tunsigned int bytesused() const { return bytesused_; }\n \tuint64_t timestamp() const { return timestamp_; }\n@@ -95,6 +97,7 @@ private:\n \tvoid setRequest(Request *request) { request_ = request; }\n \n \tunsigned int index_;\n+\tstd::array<int, 3> dmabuf_;\n \n \tunsigned int bytesused_;\n \tuint64_t timestamp_;\ndiff --git a/include/libcamera/stream.h b/include/libcamera/stream.h\nindex 08eb8cc7d5c7..1883d9e9b9c5 100644\n--- a/include/libcamera/stream.h\n+++ b/include/libcamera/stream.h\n@@ -75,6 +75,7 @@ public:\n \tStream();\n \n \tstd::unique_ptr<Buffer> createBuffer(unsigned int index);\n+\tstd::unique_ptr<Buffer> createBuffer(const std::array<int, 3> &fds);\n \n \tBufferPool &bufferPool() { return bufferPool_; }\n \tstd::vector<BufferMemory> &buffers() { return bufferPool_.buffers(); }\ndiff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp\nindex ecbf25246a55..99358633a088 100644\n--- a/src/libcamera/buffer.cpp\n+++ b/src/libcamera/buffer.cpp\n@@ -269,7 +269,8 @@ void BufferPool::destroyBuffers()\n  * for a stream with Stream::createBuffer().\n  */\n Buffer::Buffer(unsigned int index, const Buffer *metadata)\n-\t: index_(index), status_(Buffer::BufferSuccess), request_(nullptr),\n+\t: index_(index), dmabuf_({ -1, -1, -1 }),\n+\t  status_(Buffer::BufferSuccess), request_(nullptr),\n \t  stream_(nullptr)\n {\n \tif (metadata) {\n@@ -289,6 +290,16 @@ Buffer::Buffer(unsigned int index, const Buffer *metadata)\n  * \\return The buffer index\n  */\n \n+/**\n+ * \\fn Buffer::dmabufs()\n+ * \\brief Retrieve the dmabuf file descriptors for all buffer planes\n+ *\n+ * The dmabufs array contains one dmabuf file descriptor per plane. Unused\n+ * entries are set to -1.\n+ *\n+ * \\return The dmabuf file descriptors\n+ */\n+\n /**\n  * \\fn Buffer::bytesused()\n  * \\brief Retrieve the number of bytes occupied by the data in the buffer\ndiff --git a/src/libcamera/request.cpp b/src/libcamera/request.cpp\nindex 19131472710b..ee2158fc7a9c 100644\n--- a/src/libcamera/request.cpp\n+++ b/src/libcamera/request.cpp\n@@ -106,10 +106,15 @@ Request::~Request()\n  *\n  * \\return 0 on success or a negative error code otherwise\n  * \\retval -EEXIST The request already contains a buffer for the stream\n+ * \\retval -EINVAL The buffer does not reference a valid Stream\n  */\n int Request::addBuffer(std::unique_ptr<Buffer> buffer)\n {\n \tStream *stream = buffer->stream();\n+\tif (!stream) {\n+\t\tLOG(Request, Error) << \"Invalid stream reference\";\n+\t\treturn -EINVAL;\n+\t}\n \n \tauto it = bufferMap_.find(stream);\n \tif (it != bufferMap_.end()) {\ndiff --git a/src/libcamera/stream.cpp b/src/libcamera/stream.cpp\nindex 94aa4810f6b9..e6aa1b643a37 100644\n--- a/src/libcamera/stream.cpp\n+++ b/src/libcamera/stream.cpp\n@@ -424,17 +424,26 @@ Stream::Stream()\n }\n \n /**\n- * \\brief Create a Buffer instance\n+ * \\brief Create a Buffer instance referencing the memory buffer \\a index\n  * \\param[in] index The desired buffer index\n  *\n  * This method creates a Buffer instance that references a BufferMemory from\n  * the stream's buffers pool by its \\a index. The index shall be lower than the\n  * number of buffers in the pool.\n  *\n+ * This method is only valid for streams that use the InternalMemory type. It\n+ * will return a null pointer when called on streams using the ExternalMemory\n+ * type.\n+ *\n  * \\return A newly created Buffer on success or nullptr otherwise\n  */\n std::unique_ptr<Buffer> Stream::createBuffer(unsigned int index)\n {\n+\tif (memoryType_ != InternalMemory) {\n+\t\tLOG(Stream, Error) << \"Invalid stream memory type\";\n+\t\treturn nullptr;\n+\t}\n+\n \tif (index >= bufferPool_.count()) {\n \t\tLOG(Stream, Error) << \"Invalid buffer index \" << index;\n \t\treturn nullptr;\n@@ -447,6 +456,42 @@ std::unique_ptr<Buffer> Stream::createBuffer(unsigned int index)\n \treturn std::unique_ptr<Buffer>(buffer);\n }\n \n+/**\n+ * \\brief Create a Buffer instance that represents a memory area identified by\n+ * dmabuf file descriptors\n+ * \\param[in] fds The dmabuf file descriptors for each plane\n+ *\n+ * This method creates a Buffer instance that references buffer memory\n+ * allocated outside of libcamera through dmabuf file descriptors. The \\a\n+ * dmabuf array shall contain a file descriptor for each plane in the buffer,\n+ * and unused entries shall be set to -1.\n+ *\n+ * The buffer is created without a valid index, as it does not yet map to any of\n+ * the stream's BufferMemory instances. An index will be assigned at the time\n+ * the buffer is queued to the camera in a request. Applications may thus\n+ * create any number of Buffer instances, providing that no more than the\n+ * number of buffers allocated for the stream are queued at any given time.\n+ *\n+ * This method is only valid for streams that use the InternalMemory type. It\n+ * will return a null pointer when called on streams using the ExternalMemory\n+ * type.\n+ *\n+ * \\return A newly created Buffer on success or nullptr otherwise\n+ */\n+std::unique_ptr<Buffer> Stream::createBuffer(const std::array<int, 3> &fds)\n+{\n+\tif (memoryType_ != ExternalMemory) {\n+\t\tLOG(Stream, Error) << \"Invalid stream memory type\";\n+\t\treturn nullptr;\n+\t}\n+\n+\tBuffer *buffer = new Buffer();\n+\tbuffer->dmabuf_ = fds;\n+\tbuffer->stream_ = this;\n+\n+\treturn std::unique_ptr<Buffer>(buffer);\n+}\n+\n /**\n  * \\fn Stream::bufferPool()\n  * \\brief Retrieve the buffer pool for the stream\n","prefixes":["libcamera-devel","v2","12/16"]}