[{"id":19504,"web_url":"https://patchwork.libcamera.org/comment/19504/","msgid":"<1286e9ed-fa5f-0ecc-5696-a4d51733daf4@ideasonboard.com>","date":"2021-09-07T11:37:54","subject":"Re: [libcamera-devel] [PATCH v3 22/30] cam: file_sink: Use Image\n\tclass to access pixel data","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> Replace the manual implementation of frame buffer mapping with the Image\n> class to improve code sharing.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> Reviewed-by: Jean-Michel Hautbois <jeanmichel.hautbois@ideasonboard.com>\n> Reviewed-by: Hirokazu Honda <hiroh@chromium.org>\n> ---\n>  src/cam/file_sink.cpp | 42 +++++++++++++-----------------------------\n>  src/cam/file_sink.h   |  6 ++++--\n>  2 files changed, 17 insertions(+), 31 deletions(-)\n> \n> diff --git a/src/cam/file_sink.cpp b/src/cam/file_sink.cpp\n> index 0fc7d621f50b..3c2e565b27a2 100644\n> --- a/src/cam/file_sink.cpp\n> +++ b/src/cam/file_sink.cpp\n> @@ -5,17 +5,18 @@\n>   * file_sink.cpp - File Sink\n>   */\n>  \n> +#include <assert.h>\n>  #include <fcntl.h>\n>  #include <iomanip>\n>  #include <iostream>\n>  #include <sstream>\n>  #include <string.h>\n> -#include <sys/mman.h>\n>  #include <unistd.h>\n>  \n>  #include <libcamera/camera.h>\n>  \n>  #include \"file_sink.h\"\n> +#include \"image.h\"\n>  \n>  using namespace libcamera;\n>  \n> @@ -26,12 +27,6 @@ FileSink::FileSink(const std::string &pattern)\n>  \n>  FileSink::~FileSink()\n>  {\n> -\tfor (auto &iter : mappedBuffers_) {\n> -\t\tvoid *memory = iter.second.first;\n> -\t\tunsigned int length = iter.second.second;\n> -\t\tmunmap(memory, length);\n> -\t}\n> -\tmappedBuffers_.clear();\n>  }\n>  \n>  int FileSink::configure(const libcamera::CameraConfiguration &config)\n> @@ -51,23 +46,11 @@ int FileSink::configure(const libcamera::CameraConfiguration &config)\n>  \n>  void FileSink::mapBuffer(FrameBuffer *buffer)\n>  {\n> -\t/* \\todo use MappedFrameBuffer. */\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\t/**\n> -\t\t\t * \\todo Should we try to only map the portions of the\n> -\t\t\t * dmabuf that are used by planes ?\n> -\t\t\t */\n> -\t\t\tsize_t length = lseek(fd, 0, SEEK_END);\n> -\t\t\tvoid *memory = mmap(NULL, plane.length, PROT_READ,\n> -\t\t\t\t\t    MAP_SHARED, fd, 0);\n> -\t\t\tmappedBuffers_[fd] = std::make_pair(memory, length);\n> -\t\t}\n> +\tstd::unique_ptr<Image> image =\n> +\t\tImage::fromFrameBuffer(buffer, Image::MapMode::ReadOnly);\n> +\tassert(image != nullptr);\n>  \n> -\t\tvoid *memory = mappedBuffers_[fd].first;\n> -\t\tplaneData_[&plane] = static_cast<uint8_t *>(memory) + plane.offset;\n> -\t}\n> +\tmappedBuffers_[buffer] = std::move(image);\n>  }\n>  \n>  bool FileSink::processRequest(Request *request)\n> @@ -108,19 +91,20 @@ void FileSink::writeBuffer(const Stream *stream, FrameBuffer *buffer)\n>  \t\treturn;\n>  \t}\n>  \n> +\tImage *image = mappedBuffers_[buffer].get();\n> +\n>  \tfor (unsigned int i = 0; i < buffer->planes().size(); ++i) {\n> -\t\tconst FrameBuffer::Plane &plane = buffer->planes()[i];\n>  \t\tconst FrameMetadata::Plane &meta = buffer->metadata().planes()[i];\n>  \n> -\t\tuint8_t *data = planeData_[&plane];\n> -\t\tunsigned int length = std::min(meta.bytesused, plane.length);\n> +\t\tSpan<uint8_t> data = image->data(i);\n> +\t\tunsigned int length = std::min<unsigned int>(meta.bytesused, data.size());\n>  \n> -\t\tif (meta.bytesused > plane.length)\n> +\t\tif (meta.bytesused > data.size())\n>  \t\t\tstd::cerr << \"payload size \" << meta.bytesused\n> -\t\t\t\t  << \" larger than plane size \" << plane.length\n> +\t\t\t\t  << \" larger than plane size \" << data.size()\n>  \t\t\t\t  << std::endl;\n>  \n> -\t\tret = ::write(fd, data, length);\n> +\t\tret = ::write(fd, data.data(), length);\n>  \t\tif (ret < 0) {\n>  \t\t\tret = -errno;\n>  \t\t\tstd::cerr << \"write error: \" << strerror(-ret)\n> diff --git a/src/cam/file_sink.h b/src/cam/file_sink.h\n> index c12325d955c5..335be93b8732 100644\n> --- a/src/cam/file_sink.h\n> +++ b/src/cam/file_sink.h\n> @@ -8,12 +8,15 @@\n>  #define __CAM_FILE_SINK_H__\n>  \n>  #include <map>\n> +#include <memory>\n>  #include <string>\n>  \n>  #include <libcamera/stream.h>\n>  \n>  #include \"frame_sink.h\"\n>  \n> +class Image;\n> +\n>  class FileSink : public FrameSink\n>  {\n>  public:\n> @@ -32,8 +35,7 @@ private:\n>  \n>  \tstd::map<const libcamera::Stream *, std::string> streamNames_;\n>  \tstd::string pattern_;\n> -\tstd::map<int, std::pair<void *, unsigned int>> mappedBuffers_;\n> -\tstd::map<const libcamera::FrameBuffer::Plane *, uint8_t *> planeData_;\n> +\tstd::map<libcamera::FrameBuffer *, std::unique_ptr<Image>> mappedBuffers_;\n\nThings are getting simpler ;-)\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n\n>  };\n>  \n>  #endif /* __CAM_FILE_SINK_H__ */\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 8FA08BE175\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue,  7 Sep 2021 11:37:59 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id E36056916C;\n\tTue,  7 Sep 2021 13:37:58 +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 9E69360251\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue,  7 Sep 2021 13:37:57 +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 D5BAA499;\n\tTue,  7 Sep 2021 13:37:56 +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=\"GXHFUIDz\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1631014677;\n\tbh=4X6kJcNjLnRpnEnzNpa99+S8Yz3e9Rx9Px+BmoRASX4=;\n\th=To:References:From:Subject:Date:In-Reply-To:From;\n\tb=GXHFUIDzG/Zoz8cTNoa/zQNtSE4/Qf5c5MUVkaOoGH89eSsrStW0PCOvjZcCUiAs6\n\tJEyRTJnVVpopnMvPehKZWyvY7AmPHsggBAD9B+4fjFVDJKnPSwCL96+Udfho4ENliZ\n\tJBfSXT8edd1LkDoqfUFpVnLBir4WDVgiORDnqRaE=","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-22-laurent.pinchart@ideasonboard.com>","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Message-ID":"<1286e9ed-fa5f-0ecc-5696-a4d51733daf4@ideasonboard.com>","Date":"Tue, 7 Sep 2021 12:37:54 +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-22-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 22/30] cam: file_sink: Use Image\n\tclass to access pixel data","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>"}}]