{"id":444,"url":"https://patchwork.libcamera.org/api/1.1/patches/444/?format=json","web_url":"https://patchwork.libcamera.org/patch/444/","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":"<20190129135357.32339-5-kieran.bingham@ideasonboard.com>","date":"2019-01-29T13:53:57","name":"[libcamera-devel,4/4] libcamera: buffer: Provide Buffer Planes","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"b3d18c0045688af67646933c659894ce7e887f56","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/444/mbox/","series":[{"id":154,"url":"https://patchwork.libcamera.org/api/1.1/series/154/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=154","date":"2019-01-29T13:53:53","name":"libcamera: Buffer Objects","version":1,"mbox":"https://patchwork.libcamera.org/series/154/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/444/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/444/checks/","tags":{},"headers":{"Return-Path":"<kieran.bingham@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 EACD260DB9\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 29 Jan 2019 14:54:02 +0100 (CET)","from localhost.localdomain\n\t(cpc89242-aztw30-2-0-cust488.18-1.cable.virginm.net [86.31.129.233])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 8835885;\n\tTue, 29 Jan 2019 14:54:02 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1548770042;\n\tbh=sIZk/gpukKuccbysgUiSbY0jibrHL3/r1lTLI6PakHM=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=SinKuUWP1r3SwleqHDdeukevpuNPm+VtTjR74gcjTiIE9s3fstTfrNjGWapKwOe1/\n\tejZPRRtfXxrI+lJKfitPPIRutq137rySnUAYWq9jl+fF4ZEJh+afQjdWwDl+46aR7i\n\tliKw5oPZrAP4YpgYSCvRXd2RvbpuaeDOYjJzlnzg=","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"LibCamera Devel <libcamera-devel@lists.libcamera.org>","Date":"Tue, 29 Jan 2019 13:53:57 +0000","Message-Id":"<20190129135357.32339-5-kieran.bingham@ideasonboard.com>","X-Mailer":"git-send-email 2.19.1","In-Reply-To":"<20190129135357.32339-1-kieran.bingham@ideasonboard.com>","References":"<20190129135357.32339-1-kieran.bingham@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH 4/4] libcamera: buffer: Provide Buffer\n\tPlanes","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":"Tue, 29 Jan 2019 13:54:03 -0000"},"content":"Extend the Buffer management to support multi-planar formats.\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: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\n include/libcamera/buffer.h |  38 +++++++-\n src/libcamera/buffer.cpp   | 189 +++++++++++++++++++++++++++++++++++--\n 2 files changed, 215 insertions(+), 12 deletions(-)","diff":"diff --git a/include/libcamera/buffer.h b/include/libcamera/buffer.h\nindex dda5075f2879..08a74fb70b88 100644\n--- a/include/libcamera/buffer.h\n+++ b/include/libcamera/buffer.h\n@@ -11,20 +11,50 @@\n \n namespace libcamera {\n \n-class Buffer\n+class Plane\n {\n public:\n-\tBuffer();\n-\t~Buffer();\n+\tPlane();\n+\tPlane(unsigned int length, unsigned int offset);\n+\t~Plane();\n \n \tint dmabuf() const { return fd_; };\n \tint setDmabuf(int fd);\n \n+\tint mmap(int fd);\n \tint mmap();\n-\tvoid munmap();\n+\tint munmap();\n+\n+\tunsigned int length() const { return length_; };\n+\tvoid *mem() const { return mem_; };\n \n private:\n \tint fd_;\n+\tunsigned int length_;\n+\tunsigned int offset_;\n+\tvoid *mem_;\n+};\n+\n+class Buffer\n+{\n+public:\n+\tBuffer();\n+\tvirtual ~Buffer();\n+\n+\tint mmap(int fd);\n+\tint munmap();\n+\n+\tunsigned int index() const { return index_; };\n+\tconst std::vector<Plane *> &planes() { return planes_; };\n+\n+private:\n+\tunsigned int index_;\n+\n+\tunsigned int format_;\n+\tunsigned int width_;\n+\tunsigned int height_;\n+\n+\tstd::vector<Plane *> planes_;\n };\n \n class BufferPool\ndiff --git a/src/libcamera/buffer.cpp b/src/libcamera/buffer.cpp\nindex 4a870df77e92..7b84a97dcd2b 100644\n--- a/src/libcamera/buffer.cpp\n+++ b/src/libcamera/buffer.cpp\n@@ -6,10 +6,14 @@\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@@ -17,25 +21,46 @@\n \n namespace libcamera {\n \n+LOG_DEFINE_CATEGORY(Buffer)\n+\n /**\n- * \\class Buffer\n- * \\brief A memory buffer to store a frame\n+ * \\class Plane\n+ * \\brief A memory buffer to store a single plane of a frame\n  *\n- * The Buffer class represents a memory buffer used to store a frame.\n+ * The Plane class represents a memory region used to store a single plane. It\n+ * may be backed by it's own dmaBuf handle, and represents a single plane from a\n+ * Buffer. In the case of multi-planar image formats, multiple Plane objects are\n+ * attached to a Buffer to manage the whole picture.\n  */\n-Buffer::Buffer()\n-\t: fd_(-1)\n+\n+Plane::Plane()\n+\t: fd_(-1), length_(0), offset_(0), mem_(0)\n {\n }\n \n-Buffer::~Buffer()\n+/**\n+ * \\brief Construct a Plane memory region for CPU mappings\n+ * \\param[in] length The size of the memory region which should be mapped\n+ * \\param[in] offset The offset into the file descriptor base at which the\n+ * buffer commences\n+ *\n+ * \\sa mmap(int fd)\n+ */\n+Plane::Plane(unsigned int length, unsigned int offset)\n+\t: fd_(-1), length_(length), offset_(offset), 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 Buffer::dmabuf()\n+ * \\fn Plane::dmabuf()\n  * \\brief Get the dmabuf file handle backing the buffer\n  */\n \n@@ -44,7 +69,7 @@ Buffer::~Buffer()\n  *\n  * The \\a fd dmabuf file handle is duplicated and stored.\n  */\n-int Buffer::setDmabuf(int fd)\n+int Plane::setDmabuf(int fd)\n {\n \tif (fd_ != -1) {\n \t\tclose(fd_);\n@@ -61,6 +86,154 @@ int Buffer::setDmabuf(int fd)\n \treturn 0;\n }\n \n+/**\n+ * \\brief Map the plane memory data to a CPU accessible address\n+ *\n+ * The \\a fd specifies the file descriptor to map the memory from.\n+ *\n+ * \\return 0 on success or a negative error value otherwise.\n+ */\n+int Plane::mmap(int fd)\n+{\n+\tvoid *map;\n+\n+\tmap = ::mmap(NULL, length_, PROT_READ | PROT_WRITE, MAP_SHARED, fd,\n+\t\t     offset_);\n+\tif (map == reinterpret_cast<void *>(-1)) {\n+\t\tint ret = -errno;\n+\t\tLOG(Buffer, Error)\n+\t\t\t<< \"Failed to mmap buffer: \" << strerror(ret);\n+\t\treturn ret;\n+\t}\n+\n+\tmem_ = map;\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+\treturn mmap(fd_);\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 buffer: \" << strerror(ret);\n+\t} else {\n+\t\tmem_ = 0;\n+\t}\n+\n+\treturn ret;\n+}\n+\n+/**\n+ * \\fn Plane::length()\n+ * \\brief Get the length of the memory buffer\n+ */\n+\n+/**\n+ * \\fn Plane::mem()\n+ * \\brief Get the CPU accessible memory address if mapped\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), format_(0), width_(0), height_(0)\n+{\n+}\n+\n+Buffer::~Buffer()\n+{\n+\tfor (Plane *plane : planes_)\n+\t\tdelete plane;\n+\n+\tplanes_.clear();\n+}\n+\n+/**\n+ * \\brief Map each buffer plane to a CPU accessible address\n+ *\n+ * Utilise the \\a fd file descriptor to map each of the Buffer's planes to a CPU\n+ * accessible address.\n+ *\n+ * \\return 0 on success or a negative error value otherwise.\n+ */\n+int Buffer::mmap(int fd)\n+{\n+\tint ret;\n+\n+\tfor (Plane *plane : planes_) {\n+\t\tret = plane->mmap(fd);\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\brief Unmap any existing CPU accessible mapping for each plane\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 Buffer::munmap()\n+{\n+\tint ret;\n+\n+\tfor (Plane *plane : planes_) {\n+\t\tret = plane->munmap();\n+\t\tif (ret)\n+\t\t\treturn ret;\n+\t}\n+\n+\treturn 0;\n+}\n+\n+/**\n+ * \\fn Buffer::index()\n+ * \\brief Get the Buffer index value\n+ */\n+\n+/**\n+ * \\fn Buffer::planes()\n+ * \\brief Return a reference to the vector holding all Planes within the buffer\n+ */\n+\n /**\n  * \\class BufferPool\n  * \\brief A pool of buffers\n","prefixes":["libcamera-devel","4/4"]}