Patch Detail
Show a patch.
GET /api/1.1/patches/13729/?format=api
{ "id": 13729, "url": "https://patchwork.libcamera.org/api/1.1/patches/13729/?format=api", "web_url": "https://patchwork.libcamera.org/patch/13729/", "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": "<20210907154628.11440-1-laurent.pinchart@ideasonboard.com>", "date": "2021-09-07T15:46:28", "name": "[libcamera-devel,v3.1,21/30] cam: Add Image class", "commit_ref": null, "pull_url": null, "state": "accepted", "archived": false, "hash": "73d09fb16f7117a81afa78e826c3ebd3853b2702", "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/13729/mbox/", "series": [ { "id": 2491, "url": "https://patchwork.libcamera.org/api/1.1/series/2491/?format=api", "web_url": "https://patchwork.libcamera.org/project/libcamera/list/?series=2491", "date": "2021-09-07T15:44:41", "name": null, "version": 1, "mbox": "https://patchwork.libcamera.org/series/2491/mbox/" } ], "comments": "https://patchwork.libcamera.org/api/patches/13729/comments/", "check": "pending", "checks": "https://patchwork.libcamera.org/api/patches/13729/checks/", "tags": {}, "headers": { "Return-Path": "<libcamera-devel-bounces@lists.libcamera.org>", "X-Original-To": "parsemail@patchwork.libcamera.org", "Delivered-To": "parsemail@patchwork.libcamera.org", "Received": [ "from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id 5C11BBE175\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 7 Sep 2021 15:46:52 +0000 (UTC)", "from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 19D6F60252;\n\tTue, 7 Sep 2021 17:46:52 +0200 (CEST)", "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 522BE60251\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 7 Sep 2021 17:46:50 +0200 (CEST)", "from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E818F24F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 7 Sep 2021 17:46:49 +0200 (CEST)" ], "Authentication-Results": "lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"qBuP+BYZ\"; dkim-atps=neutral", "DKIM-Signature": "v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1631029610;\n\tbh=dlhy2oG9v90m6JQVS4Nj1HOrSLVeESltVD1zNa13JDg=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=qBuP+BYZC2KODDVQE4X1TQ0GH40H02ps336pNTfQApj1tfE/XD3fyMkbVB9N9dZu7\n\tu4wML/jupkR4wTCitFrbEwQRgiCPflKeLi81ay/yNxl/7PBfziyJg42xo9JRmkmzLw\n\t3ZNR8s6ex23ZugYlWNULKpR/yXPqDVO8XuABcwRA=", "From": "Laurent Pinchart <laurent.pinchart@ideasonboard.com>", "To": "libcamera-devel@lists.libcamera.org", "Date": "Tue, 7 Sep 2021 18:46:28 +0300", "Message-Id": "<20210907154628.11440-1-laurent.pinchart@ideasonboard.com>", "X-Mailer": "git-send-email 2.32.0", "In-Reply-To": "<20210906225636.14683-21-laurent.pinchart@ideasonboard.com>", "References": "<20210906225636.14683-21-laurent.pinchart@ideasonboard.com>", "MIME-Version": "1.0", "Content-Transfer-Encoding": "8bit", "Subject": "[libcamera-devel] [PATCH v3.1 21/30] cam: Add Image class", "X-BeenThere": "libcamera-devel@lists.libcamera.org", "X-Mailman-Version": "2.1.29", "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>", "Errors-To": "libcamera-devel-bounces@lists.libcamera.org", "Sender": "\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>" }, "content": "The new Image class represents a multi-planar image with direct access\nto pixel data. It currently duplicates the function of the\nMappedFrameBuffer class which is internal to libcamera, and will serve\nas a design playground to improve the API until it is considered ready\nto be made part of the libcamera public API.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Hirokazu Honda <hiroh@chromium.org>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\nChanges since v3:\n\n- Added assertions\n---\n src/cam/image.cpp | 109 ++++++++++++++++++++++++++++++++++++++++++++\n src/cam/image.h | 52 +++++++++++++++++++++\n src/cam/meson.build | 1 +\n 3 files changed, 162 insertions(+)\n create mode 100644 src/cam/image.cpp\n create mode 100644 src/cam/image.h", "diff": "diff --git a/src/cam/image.cpp b/src/cam/image.cpp\nnew file mode 100644\nindex 000000000000..6085fa7ba9d7\n--- /dev/null\n+++ b/src/cam/image.cpp\n@@ -0,0 +1,109 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Google Inc.\n+ *\n+ * image.cpp - Multi-planar image with access to pixel data\n+ */\n+\n+#include \"image.h\"\n+\n+#include <assert.h>\n+#include <errno.h>\n+#include <iostream>\n+#include <map>\n+#include <string.h>\n+#include <sys/mman.h>\n+#include <unistd.h>\n+\n+using namespace libcamera;\n+\n+std::unique_ptr<Image> Image::fromFrameBuffer(const FrameBuffer *buffer, MapMode mode)\n+{\n+\tstd::unique_ptr<Image> image{ new Image() };\n+\n+\tassert(!buffer->planes().empty());\n+\n+\tint mmapFlags = 0;\n+\n+\tif (mode & MapMode::ReadOnly)\n+\t\tmmapFlags |= PROT_READ;\n+\n+\tif (mode & MapMode::WriteOnly)\n+\t\tmmapFlags |= PROT_WRITE;\n+\n+\tstruct MappedBufferInfo {\n+\t\tuint8_t *address = nullptr;\n+\t\tsize_t mapLength = 0;\n+\t\tsize_t dmabufLength = 0;\n+\t};\n+\tstd::map<int, MappedBufferInfo> mappedBuffers;\n+\n+\tfor (const FrameBuffer::Plane &plane : buffer->planes()) {\n+\t\tconst int fd = plane.fd.fd();\n+\t\tif (mappedBuffers.find(fd) == mappedBuffers.end()) {\n+\t\t\tconst size_t length = lseek(fd, 0, SEEK_END);\n+\t\t\tmappedBuffers[fd] = MappedBufferInfo{ nullptr, 0, length };\n+\t\t}\n+\n+\t\tconst size_t length = mappedBuffers[fd].dmabufLength;\n+\n+\t\tif (plane.offset > length ||\n+\t\t plane.offset + plane.length > length) {\n+\t\t\tstd::cerr << \"plane is out of buffer: buffer length=\"\n+\t\t\t\t << length << \", plane offset=\" << plane.offset\n+\t\t\t\t << \", plane length=\" << plane.length\n+\t\t\t\t << std::endl;\n+\t\t\treturn nullptr;\n+\t\t}\n+\t\tsize_t &mapLength = mappedBuffers[fd].mapLength;\n+\t\tmapLength = std::max(mapLength,\n+\t\t\t\t static_cast<size_t>(plane.offset + plane.length));\n+\t}\n+\n+\tfor (const FrameBuffer::Plane &plane : buffer->planes()) {\n+\t\tconst int fd = plane.fd.fd();\n+\t\tauto &info = mappedBuffers[fd];\n+\t\tif (!info.address) {\n+\t\t\tvoid *address = mmap(nullptr, info.mapLength, mmapFlags,\n+\t\t\t\t\t MAP_SHARED, fd, 0);\n+\t\t\tif (address == MAP_FAILED) {\n+\t\t\t\tint error = -errno;\n+\t\t\t\tstd::cerr << \"Failed to mmap plane: \"\n+\t\t\t\t\t << strerror(-error) << std::endl;\n+\t\t\t\treturn nullptr;\n+\t\t\t}\n+\n+\t\t\tinfo.address = static_cast<uint8_t *>(address);\n+\t\t\timage->maps_.emplace_back(info.address, info.mapLength);\n+\t\t}\n+\n+\t\timage->planes_.emplace_back(info.address + plane.offset, plane.length);\n+\t}\n+\n+\treturn image;\n+}\n+\n+Image::Image() = default;\n+\n+Image::~Image()\n+{\n+\tfor (Span<uint8_t> &map : maps_)\n+\t\tmunmap(map.data(), map.size());\n+}\n+\n+unsigned int Image::numPlanes() const\n+{\n+\treturn planes_.size();\n+}\n+\n+Span<uint8_t> Image::data(unsigned int plane)\n+{\n+\tassert(plane <= planes_.size());\n+\treturn planes_[plane];\n+}\n+\n+Span<const uint8_t> Image::data(unsigned int plane) const\n+{\n+\tassert(plane <= planes_.size());\n+\treturn planes_[plane];\n+}\ndiff --git a/src/cam/image.h b/src/cam/image.h\nnew file mode 100644\nindex 000000000000..1ce5f84e5f9e\n--- /dev/null\n+++ b/src/cam/image.h\n@@ -0,0 +1,52 @@\n+/* SPDX-License-Identifier: LGPL-2.1-or-later */\n+/*\n+ * Copyright (C) 2021, Google Inc.\n+ *\n+ * image.h - Multi-planar image with access to pixel data\n+ */\n+#ifndef __CAM_IMAGE_H__\n+#define __CAM_IMAGE_H__\n+\n+#include <memory>\n+#include <stdint.h>\n+#include <vector>\n+\n+#include <libcamera/base/class.h>\n+#include <libcamera/base/flags.h>\n+#include <libcamera/base/span.h>\n+\n+#include <libcamera/framebuffer.h>\n+\n+class Image\n+{\n+public:\n+\tenum class MapMode {\n+\t\tReadOnly = 1 << 0,\n+\t\tWriteOnly = 1 << 1,\n+\t\tReadWrite = ReadOnly | WriteOnly,\n+\t};\n+\n+\tstatic std::unique_ptr<Image> fromFrameBuffer(const libcamera::FrameBuffer *buffer,\n+\t\t\t\t\t\t MapMode mode);\n+\n+\t~Image();\n+\n+\tunsigned int numPlanes() const;\n+\n+\tlibcamera::Span<uint8_t> data(unsigned int plane);\n+\tlibcamera::Span<const uint8_t> data(unsigned int plane) const;\n+\n+private:\n+\tLIBCAMERA_DISABLE_COPY(Image)\n+\n+\tImage();\n+\n+\tstd::vector<libcamera::Span<uint8_t>> maps_;\n+\tstd::vector<libcamera::Span<uint8_t>> planes_;\n+};\n+\n+namespace libcamera {\n+LIBCAMERA_FLAGS_ENABLE_OPERATORS(Image::MapMode)\n+}\n+\n+#endif /* __CAM_IMAGE_H__ */\ndiff --git a/src/cam/meson.build b/src/cam/meson.build\nindex ea36aaa5c514..e8e2ae57d3f4 100644\n--- a/src/cam/meson.build\n+++ b/src/cam/meson.build\n@@ -14,6 +14,7 @@ cam_sources = files([\n 'event_loop.cpp',\n 'file_sink.cpp',\n 'frame_sink.cpp',\n+ 'image.cpp',\n 'main.cpp',\n 'options.cpp',\n 'stream_options.cpp',\n", "prefixes": [ "libcamera-devel", "v3.1", "21/30" ] }