[{"id":19503,"web_url":"https://patchwork.libcamera.org/comment/19503/","msgid":"<6f057829-d187-edce-cabd-5680941ab88d@ideasonboard.com>","date":"2021-09-07T11:27:11","subject":"Re: [libcamera-devel] [PATCH v3 21/30] cam: Add Image class","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"On 06/09/2021 23:56, Laurent Pinchart wrote:\n> The new Image class represents a multi-planar image with direct access\n> to pixel data. It currently duplicates the function of the\n> MappedFrameBuffer class which is internal to libcamera, and will serve\n> as a design playground to improve the API until it is considered ready\n> to be made part of the libcamera public API.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> Reviewed-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\n> \n> diff --git a/src/cam/image.cpp b/src/cam/image.cpp\n> new file mode 100644\n> index 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\n\tassert(plane < planes_.size()); ?\n\n> +\treturn planes_[plane];\n> +}\n> +\n> +Span<const uint8_t> Image::data(unsigned int plane) const\n> +{\n\n\tassert(plane < planes_.size()); ?\n\nBut other than that, It's good to get something started to handle this\nproperly.\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> +\treturn planes_[plane];\n> +}\n> diff --git a/src/cam/image.h b/src/cam/image.h\n> new file mode 100644\n> index 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__ */\n> diff --git a/src/cam/meson.build b/src/cam/meson.build\n> index 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>","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 2A0E3BDC71\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  7 Sep 2021 11:27:16 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 8D4636916C;\n\tTue,  7 Sep 2021 13:27:15 +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 74B2160251\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  7 Sep 2021 13:27:14 +0200 (CEST)","from [192.168.0.20]\n\t(cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 01D4B499;\n\tTue,  7 Sep 2021 13:27:13 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"q/5Z044V\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1631014034;\n\tbh=ze1Fv0FpTDnfCUThQIG0tLKvJf07fU25xvzw+ctAovs=;\n\th=To:References:From:Subject:Date:In-Reply-To:From;\n\tb=q/5Z044Vko87Jow1SzpzlceWEmsl5crJfGsHPl7U7MMAEwRlRdZYOWhFQLv95Unsi\n\tl4+jDz8R2f3I+uge1qn2eFfC2Be4IkEANHAdryfQfowBsxH2/ZII995tovwrlZTwrE\n\t3lAlak77yG4uKEUnLcV+n+IgeJbKQWO7viKDQG54=","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","References":"<20210906225420.13275-1-laurent.pinchart@ideasonboard.com>\n\t<20210906225636.14683-21-laurent.pinchart@ideasonboard.com>","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Message-ID":"<6f057829-d187-edce-cabd-5680941ab88d@ideasonboard.com>","Date":"Tue, 7 Sep 2021 12:27:11 +0100","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101\n\tThunderbird/78.11.0","MIME-Version":"1.0","In-Reply-To":"<20210906225636.14683-21-laurent.pinchart@ideasonboard.com>","Content-Type":"text/plain; charset=utf-8","Content-Language":"en-GB","Content-Transfer-Encoding":"8bit","Subject":"Re: [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>"}}]