Patch Detail
Show a patch.
GET /api/1.1/patches/516/?format=api
{ "id": 516, "url": "https://patchwork.libcamera.org/api/1.1/patches/516/?format=api", "web_url": "https://patchwork.libcamera.org/patch/516/", "project": { "id": 1, "url": "https://patchwork.libcamera.org/api/1.1/projects/1/?format=api", "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-2-laurent.pinchart@ideasonboard.com>", "date": "2019-02-06T06:07:52", "name": "[libcamera-devel,01/27] libcamera: Add Buffer Management", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": false, "hash": "b83f90bdaeb7de26aaca518f7bd5cb01b2035686", "submitter": { "id": 2, "url": "https://patchwork.libcamera.org/api/1.1/people/2/?format=api", "name": "Laurent Pinchart", "email": "laurent.pinchart@ideasonboard.com" }, "delegate": null, "mbox": "https://patchwork.libcamera.org/patch/516/mbox/", "series": [ { "id": 170, "url": "https://patchwork.libcamera.org/api/1.1/series/170/?format=api", "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/516/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/516/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 99B4361021\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed, 6 Feb 2019 07:08:23 +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 2EFAE2D7\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=FUtewdVKAYxafzetZ6UuBVTPQLD4D1IKuJRWky1K3io=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=CZePm75PxaNUUpuXwDudmqQ0yRDbJHNH+xYqA/5fNxp3framPJ+cTXtr+D0UaP1ny\n\tK0Ao4ZeQYDZD51XCIPSUqv3MSzDfoVaBvaR6xV1m1Fg6zCd+N/ERii1zwVsq8Vejrc\n\tmKSbPxG9atQQORV6wpdGBH/aoASHs0XUhxmhgfXA=", "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Wed, 6 Feb 2019 08:07:52 +0200", "Message-Id": "<20190206060818.13907-2-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 01/27] libcamera: Add Buffer Management", "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:23 -0000" }, "content": "From: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\nProvide classes that represent frame buffers and pools of frame buffers.\n\nAn image within the system may use one or more Plane objects to track each\nplane in the case of multi-planar image formats. The Buffer class manages all\nof the data required to render or interpret the raw image data.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nSigned-off-by: Jacopo Mondi <jacopo@jmondi.org>\nSigned-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\n include/libcamera/buffer.h | 74 ++++++++++\n include/libcamera/libcamera.h | 1 +\n include/libcamera/meson.build | 1 +\n src/libcamera/buffer.cpp | 253 ++++++++++++++++++++++++++++++++++\n src/libcamera/meson.build | 1 +\n 5 files changed, 330 insertions(+)\n create mode 100644 include/libcamera/buffer.h\n create mode 100644 src/libcamera/buffer.cpp", "diff": "diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h\nnew file mode 100644\nindex 000000000000..21a1ec4c574e\n--- /dev/null\n+++ b/include/libcamera/buffer.h\n@@ -0,0 +1,74 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * buffer.h - Buffer handling\n+ */\n+#ifndef __LIBCAMERA_BUFFER_H__\n+#define __LIBCAMERA_BUFFER_H__\n+\n+#include <vector>\n+\n+#include <libcamera/signal.h>\n+\n+namespace libcamera {\n+\n+class BufferPool;\n+\n+class Plane final\n+{\n+public:\n+\tPlane();\n+\t~Plane();\n+\n+\tint dmabuf() const { return fd_; }\n+\tint setDmabuf(int fd, unsigned int length);\n+\n+\tvoid *mem();\n+\tunsigned int length() const { return length_; }\n+\n+private:\n+\tint mmap();\n+\tint munmap();\n+\n+\tint fd_;\n+\tunsigned int length_;\n+\tvoid *mem_;\n+};\n+\n+class Buffer final\n+{\n+public:\n+\tBuffer();\n+\n+\tunsigned int index() const { return index_; }\n+\tstd::vector<Plane> &planes() { return planes_; }\n+\n+\tSignal<Buffer *> completed;\n+\n+private:\n+\tfriend class BufferPool;\n+\n+\tunsigned int index_;\n+\n+\tstd::vector<Plane> planes_;\n+};\n+\n+class BufferPool final\n+{\n+public:\n+\t~BufferPool();\n+\n+\tvoid createBuffers(unsigned int count);\n+\tvoid destroyBuffers();\n+\n+\tunsigned int count() const { return buffers_.size(); }\n+\tstd::vector<Buffer> &buffers() { return buffers_; }\n+\n+private:\n+\tstd::vector<Buffer> buffers_;\n+};\n+\n+} /* namespace libcamera */\n+\n+#endif /* __LIBCAMERA_BUFFER_H__ */\ndiff --git a/include/libcamera/libcamera.h b/include/libcamera/libcamera.h\nindex 272dfd5e4a67..8167e8099ac0 100644\n--- a/include/libcamera/libcamera.h\n+++ b/include/libcamera/libcamera.h\n@@ -7,6 +7,7 @@\n #ifndef __LIBCAMERA_LIBCAMERA_H__\n #define __LIBCAMERA_LIBCAMERA_H__\n \n+#include <libcamera/buffer.h>\n #include <libcamera/camera.h>\n #include <libcamera/camera_manager.h>\n #include <libcamera/event_dispatcher.h>\ndiff --git a/include/libcamera/meson.build b/include/libcamera/meson.build\nindex 54a680787e5c..8c14423bc444 100644\n--- a/include/libcamera/meson.build\n+++ b/include/libcamera/meson.build\n@@ -1,4 +1,5 @@\n libcamera_api = files([\n+ 'buffer.h',\n 'camera.h',\n 'camera_manager.h',\n 'event_dispatcher.h',\ndiff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp\nnew file mode 100644\nindex 000000000000..5f6114cf3bc5\n--- /dev/null\n+++ b/src/libcamera/buffer.cpp\n@@ -0,0 +1,253 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2019, Google Inc.\n+ *\n+ * buffer.cpp - Buffer handling\n+ */\n+\n+#include <errno.h>\n+#include <string.h>\n+#include <sys/mman.h>\n+#include <unistd.h>\n+\n+#include <libcamera/buffer.h>\n+\n+#include \"log.h\"\n+\n+/**\n+ * \\file buffer.h\n+ * \\brief Buffer handling\n+ */\n+\n+namespace libcamera {\n+\n+LOG_DEFINE_CATEGORY(Buffer)\n+\n+/**\n+ * \\class Plane\n+ * \\brief A memory region to store a single plane of a frame\n+ *\n+ * Planar pixel formats use multiple memory regions to store planes\n+ * corresponding to the different colour components of a frame. The Plane class\n+ * tracks the specific details of a memory region used to store a single plane\n+ * for a given frame and provides the means to access the memory, both for the\n+ * application and for DMA. A Buffer then contains one or multiple planes\n+ * depending on its pixel format.\n+ *\n+ * To support DMA access, planes are associated with dmabuf objects represented\n+ * by file handles. Each plane carries a dmabuf file handle and an offset within\n+ * the buffer. Those file handles may refer to the same dmabuf object, depending\n+ * on whether the devices accessing the memory regions composing the image\n+ * support non-contiguous DMA to planes ore require DMA-contiguous memory.\n+ *\n+ * To support CPU access, planes carry the CPU address of their backing memory.\n+ * Similarly to the dmabuf file handles, the CPU addresses for planes composing\n+ * an image may or may not be contiguous.\n+ */\n+\n+Plane::Plane()\n+\t: fd_(-1), length_(0), mem_(0)\n+{\n+}\n+\n+Plane::~Plane()\n+{\n+\tmunmap();\n+\n+\tif (fd_ != -1)\n+\t\tclose(fd_);\n+}\n+\n+/**\n+ * \\fn Plane::dmabuf()\n+ * \\brief Get the dmabuf file handle backing the buffer\n+ */\n+\n+/**\n+ * \\brief Set the dmabuf file handle backing the buffer\n+ * \\param[in] fd The dmabuf file handle\n+ * \\param[in] length The size of the memory region\n+ *\n+ * The \\a fd dmabuf file handle is duplicated and stored. The caller may close\n+ * the original file handle.\n+ *\n+ * \\return 0 on success or a negative error value otherwise.\n+ */\n+int Plane::setDmabuf(int fd, unsigned int length)\n+{\n+\tif (fd < 0) {\n+\t\tLOG(Buffer, Error) << \"Invalid dmabuf fd provided\";\n+\t\treturn -EINVAL;\n+\t}\n+\n+\tif (fd_ != -1) {\n+\t\tclose(fd_);\n+\t\tfd_ = -1;\n+\t}\n+\n+\tfd_ = dup(fd);\n+\tif (fd_ == -1) {\n+\t\tint ret = -errno;\n+\t\tLOG(Buffer, Error)\n+\t\t\t<< \"Failed to duplicate dmabuf: \" << strerror(-ret);\n+\t\treturn ret;\n+\t}\n+\n+\tlength_ = length;\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief Map the plane memory data to a CPU accessible address\n+ *\n+ * The file descriptor to map the memory from must be set by a call to\n+ * setDmaBuf() before calling this function.\n+ *\n+ * \\sa setDmaBuf()\n+ *\n+ * \\return 0 on success or a negative error value otherwise.\n+ */\n+int Plane::mmap()\n+{\n+\tvoid *map;\n+\n+\tif (mem_)\n+\t\treturn 0;\n+\n+\tmap = ::mmap(NULL, length_, PROT_READ | PROT_WRITE, MAP_SHARED, fd_, 0);\n+\tif (map == MAP_FAILED) {\n+\t\tint ret = -errno;\n+\t\tLOG(Buffer, Error)\n+\t\t\t<< \"Failed to mmap plane: \" << strerror(-ret);\n+\t\treturn ret;\n+\t}\n+\n+\tmem_ = map;\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief Unmap any existing CPU accessible mapping\n+ *\n+ * Unmap the memory mapped by an earlier call to mmap().\n+ *\n+ * \\return 0 on success or a negative error value otherwise.\n+ */\n+int Plane::munmap()\n+{\n+\tint ret = 0;\n+\n+\tif (mem_)\n+\t\tret = ::munmap(mem_, length_);\n+\n+\tif (ret) {\n+\t\tret = -errno;\n+\t\tLOG(Buffer, Warning)\n+\t\t\t<< \"Failed to unmap plane: \" << strerror(-ret);\n+\t} else {\n+\t\tmem_ = 0;\n+\t}\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * \\fn Plane::mem()\n+ * \\brief Retrieve the CPU accessible memory address of the Plane\n+ * \\return The CPU accessible memory address on success or nullptr otherwise.\n+ */\n+void *Plane::mem()\n+{\n+\tif (!mem_)\n+\t\tmmap();\n+\n+\treturn mem_;\n+}\n+\n+/**\n+ * \\fn Plane::length()\n+ * \\brief Retrieve the length of the memory region\n+ * \\return The length of the memory region\n+ */\n+\n+/**\n+ * \\class Buffer\n+ * \\brief A memory buffer to store an image\n+ *\n+ * The Buffer class represents the memory buffers used to store a\n+ * full frame image, which may contain multiple separate memory Plane\n+ * objects if the image format is multi-planar.\n+ */\n+\n+Buffer::Buffer()\n+\t: index_(-1)\n+{\n+}\n+\n+/**\n+ * \\fn Buffer::index()\n+ * \\brief Retrieve the Buffer index\n+ * \\return The buffer index\n+ */\n+\n+/**\n+ * \\fn Buffer::planes()\n+ * \\brief Retrieve the planes within the buffer\n+ * \\return A reference to a vector holding all Planes within the buffer\n+ */\n+\n+/**\n+ * \\var Buffer::completed\n+ * \\brief A Signal to provide notifications that the specific Buffer is ready\n+ */\n+\n+/**\n+ * \\class BufferPool\n+ * \\brief A pool of buffers\n+ *\n+ * The BufferPool class groups together a collection of Buffers to store frames.\n+ * The buffers must be exported by a device before they can be imported into\n+ * another device for further use.\n+ */\n+\n+BufferPool::~BufferPool()\n+{\n+\tdestroyBuffers();\n+}\n+\n+/**\n+ * \\brief Create buffers in the Pool\n+ * \\param[in] count The number of buffers to create\n+ */\n+void BufferPool::createBuffers(unsigned int count)\n+{\n+\tunsigned int index = 0;\n+\n+\tbuffers_.resize(count);\n+\tfor (Buffer &buffer : buffers_)\n+\t\tbuffer.index_ = index++;\n+}\n+\n+/**\n+ * \\brief Release all buffers from pool\n+ */\n+void BufferPool::destroyBuffers()\n+{\n+\tbuffers_.resize(0);\n+}\n+\n+/**\n+ * \\fn BufferPool::count()\n+ * \\brief Retrieve the number of buffers contained within the pool\n+ * \\return The number of buffers contained in the pool\n+ */\n+\n+/**\n+ * \\fn BufferPool::buffers()\n+ * \\brief Retrieve all the buffers in the pool\n+ * \\return A vector containing all the buffers in the pool.\n+ */\n+\n+} /* namespace libcamera */\ndiff --git a/src/libcamera/meson.build b/src/libcamera/meson.build\nindex 9f6ff99eebe2..a4e9cc8f936c 100644\n--- a/src/libcamera/meson.build\n+++ b/src/libcamera/meson.build\n@@ -1,4 +1,5 @@\n libcamera_sources = files([\n+ 'buffer.cpp',\n 'camera.cpp',\n 'camera_manager.cpp',\n 'device_enumerator.cpp',\n", "prefixes": [ "libcamera-devel", "01/27" ] }