{"id":13688,"url":"https://patchwork.libcamera.org/api/1.1/patches/13688/?format=json","web_url":"https://patchwork.libcamera.org/patch/13688/","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":"<20210906225636.14683-21-laurent.pinchart@ideasonboard.com>","date":"2021-09-06T22:56:27","name":"[libcamera-devel,v3,21/30] cam: Add Image class","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"9c0caa5b7b34f0af24f066f76e92111f047d4e66","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/13688/mbox/","series":[{"id":2482,"url":"https://patchwork.libcamera.org/api/1.1/series/2482/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2482","date":"2021-09-06T22:54:20","name":"libcamera: Handle fallout of FrameBuffer offset support","version":3,"mbox":"https://patchwork.libcamera.org/series/2482/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/13688/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/13688/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 A29C3BE175\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon,  6 Sep 2021 22:57:47 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 4718369168;\n\tTue,  7 Sep 2021 00:57:47 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 61C1B69187\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  7 Sep 2021 00:57:07 +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 EB221993;\n\tTue,  7 Sep 2021 00:57:06 +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=\"uVr1WLsK\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1630969027;\n\tbh=71I3HIVpN19PnV02rVKuqjH2ieN+1GD/UXroLqS/oyE=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=uVr1WLsKXUytSqJZllrEj6RuUoQFVAURHOoA9J94d8zcRihf9DfzgkAxoKXpSJSnU\n\t3fjqR8oheuq5wfHX0pcqsee4Srsj9CbCxANOU4P3Xnp6mBMUwSgML50BZCe7J9Q472\n\t50ebDUemGLLMiuz0P63AHsg79Jz3Y39LekKditds=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue,  7 Sep 2021 01:56:27 +0300","Message-Id":"<20210906225636.14683-21-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.32.0","In-Reply-To":"<20210906225420.13275-1-laurent.pinchart@ideasonboard.com>","References":"<20210906225420.13275-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v3 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>\n---\n src/cam/image.cpp   | 107 ++++++++++++++++++++++++++++++++++++++++++++\n src/cam/image.h     |  52 +++++++++++++++++++++\n src/cam/meson.build |   1 +\n 3 files changed, 160 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..7ae5f52dccb4\n--- /dev/null\n+++ b/src/cam/image.cpp\n@@ -0,0 +1,107 @@\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+\treturn planes_[plane];\n+}\n+\n+Span<const uint8_t> Image::data(unsigned int plane) const\n+{\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","21/30"]}