[{"id":19093,"web_url":"https://patchwork.libcamera.org/comment/19093/","msgid":"<YSeLyAq7w/5y1ioP@pendragon.ideasonboard.com>","date":"2021-08-26T12:40:40","subject":"Re: [libcamera-devel] [PATCH v3 2/9] libcamera: mapped_framebuffer:\n\tReturn plane begin address by MappedBuffer::maps()","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Hiro,\n\nThank you for the patch.\n\nOn Thu, Aug 26, 2021 at 08:25:32PM +0900, Hirokazu Honda wrote:\n> MappedBuffer::maps() returns std::vector<MappedBuffer::Plane>.\n> Plane has the address, but the address points the beginning of the\n> buffer containing the plane.\n> This makes the Plane point the beginning of the plane. So\n> MappedBuffer::maps()[i].data() returns the address of i-th plane.\n> \n> Signed-off-by: Hirokazu Honda <hiroh@chromium.org>\n> Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> ---\n>  .../libcamera/internal/mapped_framebuffer.h   |  4 +-\n>  src/android/mm/generic_camera_buffer.cpp      |  2 -\n>  src/libcamera/mapped_framebuffer.cpp          | 71 ++++++++++++++++---\n>  src/libcamera/v4l2_videodevice.cpp            |  6 ++\n>  4 files changed, 70 insertions(+), 13 deletions(-)\n> \n> diff --git a/include/libcamera/internal/mapped_framebuffer.h b/include/libcamera/internal/mapped_framebuffer.h\n> index 3401a9fc..42479541 100644\n> --- a/include/libcamera/internal/mapped_framebuffer.h\n> +++ b/include/libcamera/internal/mapped_framebuffer.h\n> @@ -30,12 +30,14 @@ public:\n>  \n>  \tbool isValid() const { return error_ == 0; }\n>  \tint error() const { return error_; }\n> -\tconst std::vector<Plane> &maps() const { return maps_; }\n> +\t/* \\todo rename to planes(). */\n> +\tconst std::vector<Plane> &maps() const { return planes_; }\n>  \n>  protected:\n>  \tMappedBuffer();\n>  \n>  \tint error_;\n> +\tstd::vector<Plane> planes_;\n>  \tstd::vector<Plane> maps_;\n>  \n>  private:\n> diff --git a/src/android/mm/generic_camera_buffer.cpp b/src/android/mm/generic_camera_buffer.cpp\n> index d4f3f22b..299a5496 100644\n> --- a/src/android/mm/generic_camera_buffer.cpp\n> +++ b/src/android/mm/generic_camera_buffer.cpp\n> @@ -53,8 +53,6 @@ private:\n>  \tint flags_;\n>  \toff_t bufferLength_;\n>  \tstd::vector<PlaneInfo> planeInfo_;\n> -\t/* \\todo Remove planes_ when it will be added to MappedBuffer */\n> -\tstd::vector<Span<uint8_t>> planes_;\n>  };\n>  \n>  CameraBuffer::Private::Private([[maybe_unused]] CameraBuffer *cameraBuffer,\n> diff --git a/src/libcamera/mapped_framebuffer.cpp b/src/libcamera/mapped_framebuffer.cpp\n> index 2ebe9fdb..1dc88647 100644\n> --- a/src/libcamera/mapped_framebuffer.cpp\n> +++ b/src/libcamera/mapped_framebuffer.cpp\n> @@ -7,8 +7,11 @@\n>  \n>  #include \"libcamera/internal/mapped_framebuffer.h\"\n>  \n> +#include <algorithm>\n>  #include <errno.h>\n> +#include <map>\n>  #include <sys/mman.h>\n> +#include <unistd.h>\n>  \n>  #include <libcamera/base/log.h>\n>  \n> @@ -79,6 +82,7 @@ MappedBuffer::MappedBuffer(MappedBuffer &&other)\n>  MappedBuffer &MappedBuffer::operator=(MappedBuffer &&other)\n>  {\n>  \terror_ = other.error_;\n> +\tplanes_ = std::move(other.planes_);\n>  \tmaps_ = std::move(other.maps_);\n>  \tother.error_ = -ENOENT;\n>  \n> @@ -127,10 +131,18 @@ MappedBuffer::~MappedBuffer()\n>   */\n>  \n>  /**\n> - * \\var MappedBuffer::maps_\n> + * \\var MappedBuffer::planes_\n>   * \\brief Stores the internal mapped planes\n>   *\n>   * MappedBuffer derived classes shall store the mappings they create in this\n> + * vector which points the beginning of mapped plane addresses.\n> + */\n> +\n> +/**\n> + * \\var MappedBuffer::maps_\n> + * \\brief Stores the mapped buffer\n> + *\n> + * MappedBuffer derived classes shall store the mappings they create in this\n>   * vector which is parsed during destruct to unmap any memory mappings which\n>   * completed successfully.\n>   */\n> @@ -167,7 +179,8 @@ MappedBuffer::~MappedBuffer()\n>   */\n>  MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, MapFlags flags)\n>  {\n> -\tmaps_.reserve(buffer->planes().size());\n> +\tASSERT(!buffer->planes().empty());\n> +\tplanes_.reserve(buffer->planes().size());\n>  \n>  \tint mmapFlags = 0;\n>  \n> @@ -177,17 +190,55 @@ MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, MapFlags flags)\n>  \tif (flags & MapFlag::Write)\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\tLOG(Buffer, Fatal) << \"plane is out of buffer: \"\n> +\t\t\t\t\t   << \"buffer length=\" << length\n> +\t\t\t\t\t   << \", plane offset=\" << plane.offset\n> +\t\t\t\t\t   << \", plane length=\" << plane.length;\n> +\t\t\treturn;\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\tvoid *address = mmap(nullptr, plane.length, mmapFlags,\n> -\t\t\t\t     MAP_SHARED, plane.fd.fd(), 0);\n> -\t\tif (address == MAP_FAILED) {\n> -\t\t\terror_ = -errno;\n> -\t\t\tLOG(Buffer, Error) << \"Failed to mmap plane: \"\n> -\t\t\t\t\t   << strerror(-error_);\n> -\t\t\tbreak;\n> +\t\tconst int fd = plane.fd.fd();\n> +\t\tauto &info = mappedBuffers[fd];\n> +\t\tif (!info.address) {\n> +\t\t\tvoid *address =\n> +\t\t\t\tstatic_cast<uint8_t *>(\n\nYou can drop the static cast here, the variable is a void * and mmap()\nreturns a void *. I can fix this when pushing the patches.\n\n> +\t\t\t\t\tmmap(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\terror_ = -errno;\n> +\t\t\t\tLOG(Buffer, Error) << \"Failed to mmap plane: \"\n> +\t\t\t\t\t\t   << strerror(-error_);\n> +\t\t\t\treturn;\n> +\t\t\t}\n> +\n> +\t\t\tinfo.address = static_cast<uint8_t *>(address);\n> +\t\t\tmaps_.emplace_back(info.address, info.mapLength);\n>  \t\t}\n>  \n> -\t\tmaps_.emplace_back(static_cast<uint8_t *>(address), plane.length);\n> +\t\tplanes_.emplace_back(info.address + plane.offset, plane.length);\n>  \t}\n>  }\n>  \n> diff --git a/src/libcamera/v4l2_videodevice.cpp b/src/libcamera/v4l2_videodevice.cpp\n> index ce60dff6..2ff25af2 100644\n> --- a/src/libcamera/v4l2_videodevice.cpp\n> +++ b/src/libcamera/v4l2_videodevice.cpp\n> @@ -1283,6 +1283,12 @@ std::unique_ptr<FrameBuffer> V4L2VideoDevice::createBuffer(unsigned int index)\n>  \n>  \t\tFrameBuffer::Plane plane;\n>  \t\tplane.fd = std::move(fd);\n> +\t\t/*\n> +\t\t * V4L2 API doesn't provide dmabuf offset information of plane.\n> +\t\t * Set 0 as a placeholder offset.\n> +\t\t * \\todo Set the right offset once V4L2 API provides a way.\n> +\t\t */\n> +\t\tplane.offset = 0;\n>  \t\tplane.length = multiPlanar ?\n>  \t\t\tbuf.m.planes[nplane].length : buf.length;\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 6BF39BD87C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 26 Aug 2021 12:40:55 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id B7E886891F;\n\tThu, 26 Aug 2021 14:40:54 +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 4966C6888F\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 26 Aug 2021 14:40:53 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id B0C4C1924;\n\tThu, 26 Aug 2021 14:40:52 +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=\"WLfmLyJA\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1629981652;\n\tbh=E4Bv8OM0ine4taVKfbh69CzsLXCNKHvZByzHYZDmZC4=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=WLfmLyJA96X+gCGYeR24HLgAW1k7OHBOEdf2/Hs8clmcYSPe8KNn8MenogCJ9XJmQ\n\tTNXeXpM1BFwa8mluz6XEe8geXjFK1YNcuIbdTMqmrv0uM898JCWmohBAjbQoLcJa74\n\t8+AySScnCa6QyLQbxKKjF7ZC+kMXsJTkB8jOU8lk=","Date":"Thu, 26 Aug 2021 15:40:40 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Hirokazu Honda <hiroh@chromium.org>","Message-ID":"<YSeLyAq7w/5y1ioP@pendragon.ideasonboard.com>","References":"<20210826112539.170694-1-hiroh@chromium.org>\n\t<20210826112539.170694-3-hiroh@chromium.org>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20210826112539.170694-3-hiroh@chromium.org>","Subject":"Re: [libcamera-devel] [PATCH v3 2/9] libcamera: mapped_framebuffer:\n\tReturn plane begin address by MappedBuffer::maps()","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>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]