[{"id":18600,"web_url":"https://patchwork.libcamera.org/comment/18600/","msgid":"<YQ04AZTx+nW1FNoN@pendragon.ideasonboard.com>","date":"2021-08-06T13:24:17","subject":"Re: [libcamera-devel] [PATCH] libcamera: MappedFrameBuffer: Use\n\ttyped Flags<MapModes>","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Kieran,\n\nThank you for the patch.\n\nOn Fri, Aug 06, 2021 at 02:15:11PM +0100, Kieran Bingham wrote:\n> Remove the need for callers to reference PROT_READ/PROT_WRITE directly\n> from <sys/mman.h> by instead exposing the Read/Write mapping options as\n> flags from the MappedFrameBuffer class itself.\n> \n> While here, introduce the <stdint.h> header which is required for the\n> int8_t as part of the Plane.\n\ns/int8_t/uint8_t/\n\n> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> ---\n>  .../libcamera/internal/mapped_framebuffer.h   | 14 ++++++++--\n>  src/android/jpeg/encoder_libjpeg.cpp          |  2 +-\n>  src/android/jpeg/thumbnailer.cpp              |  2 +-\n>  src/android/yuv/post_processor_yuv.cpp        |  2 +-\n>  src/ipa/ipu3/ipu3.cpp                         |  2 +-\n>  src/ipa/raspberrypi/raspberrypi.cpp           |  2 +-\n>  src/libcamera/mapped_framebuffer.cpp          | 28 +++++++++++++++++--\n>  test/mapped-buffer.cpp                        |  6 ++--\n>  8 files changed, 46 insertions(+), 12 deletions(-)\n> \n> diff --git a/include/libcamera/internal/mapped_framebuffer.h b/include/libcamera/internal/mapped_framebuffer.h\n> index 41e587364260..24bd2c50cd27 100644\n> --- a/include/libcamera/internal/mapped_framebuffer.h\n> +++ b/include/libcamera/internal/mapped_framebuffer.h\n> @@ -7,10 +7,11 @@\n>  #ifndef __LIBCAMERA_INTERNAL_MAPPED_FRAMEBUFFER_H__\n>  #define __LIBCAMERA_INTERNAL_MAPPED_FRAMEBUFFER_H__\n>  \n> -#include <sys/mman.h>\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> @@ -44,7 +45,16 @@ private:\n>  class MappedFrameBuffer : public MappedBuffer\n>  {\n>  public:\n> -\tMappedFrameBuffer(const FrameBuffer *buffer, int flags);\n> +\tenum MapModes {\n\nTo get the full benefits from Flags<>, this should be a scoped enum\n(enum class). Don't forget LIBCAMERA_FLAGS_ENABLE_OPERATORS(). Yes, it\nwill be a bit more verbose in the caller, but I think it's worth it to\nensure type safety.\n\n> +\t\tRead = 0 << 1,\n> +\t\tWrite = 1 << 1,\n> +\n> +\t\tReadWrite = Read | Write,\n> +\t};\n> +\n> +\tusing MapMode = Flags<MapModes>;\n\nThe File class uses MapFlags = Flags<MapFlag> and OpenMode =\nFlags<OpenModeFlag>. Now we have yet another naming scheme :-) I think\nit would be nice to try and be consistent.\n\n> +\n> +\tMappedFrameBuffer(const FrameBuffer *buffer, MapMode flags);\n>  };\n>  \n>  } /* namespace libcamera */\n> diff --git a/src/android/jpeg/encoder_libjpeg.cpp b/src/android/jpeg/encoder_libjpeg.cpp\n> index 372018d2207f..c37611f6b6cd 100644\n> --- a/src/android/jpeg/encoder_libjpeg.cpp\n> +++ b/src/android/jpeg/encoder_libjpeg.cpp\n> @@ -182,7 +182,7 @@ void EncoderLibJpeg::compressNV(Span<const uint8_t> frame)\n>  int EncoderLibJpeg::encode(const FrameBuffer &source, Span<uint8_t> dest,\n>  \t\t\t   Span<const uint8_t> exifData, unsigned int quality)\n>  {\n> -\tMappedFrameBuffer frame(&source, PROT_READ);\n> +\tMappedFrameBuffer frame(&source, MappedFrameBuffer::Read);\n>  \tif (!frame.isValid()) {\n>  \t\tLOG(JPEG, Error) << \"Failed to map FrameBuffer : \"\n>  \t\t\t\t << strerror(frame.error());\n> diff --git a/src/android/jpeg/thumbnailer.cpp b/src/android/jpeg/thumbnailer.cpp\n> index 535e2cece914..17196b2dd257 100644\n> --- a/src/android/jpeg/thumbnailer.cpp\n> +++ b/src/android/jpeg/thumbnailer.cpp\n> @@ -41,7 +41,7 @@ void Thumbnailer::createThumbnail(const FrameBuffer &source,\n>  \t\t\t\t  const Size &targetSize,\n>  \t\t\t\t  std::vector<unsigned char> *destination)\n>  {\n> -\tMappedFrameBuffer frame(&source, PROT_READ);\n> +\tMappedFrameBuffer frame(&source, MappedFrameBuffer::Read);\n>  \tif (!frame.isValid()) {\n>  \t\tLOG(Thumbnailer, Error)\n>  \t\t\t<< \"Failed to map FrameBuffer : \"\n> diff --git a/src/android/yuv/post_processor_yuv.cpp b/src/android/yuv/post_processor_yuv.cpp\n> index 509d4244d614..6f599a82a3a0 100644\n> --- a/src/android/yuv/post_processor_yuv.cpp\n> +++ b/src/android/yuv/post_processor_yuv.cpp\n> @@ -57,7 +57,7 @@ int PostProcessorYuv::process(const FrameBuffer &source,\n>  \tif (!isValidBuffers(source, *destination))\n>  \t\treturn -EINVAL;\n>  \n> -\tconst MappedFrameBuffer sourceMapped(&source, PROT_READ);\n> +\tconst MappedFrameBuffer sourceMapped(&source, MappedFrameBuffer::Read);\n>  \tif (!sourceMapped.isValid()) {\n>  \t\tLOG(YUV, Error) << \"Failed to mmap camera frame buffer\";\n>  \t\treturn -EINVAL;\n> diff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp\n> index 2647bf2f3b96..6028638c314c 100644\n> --- a/src/ipa/ipu3/ipu3.cpp\n> +++ b/src/ipa/ipu3/ipu3.cpp\n> @@ -211,7 +211,7 @@ void IPAIPU3::mapBuffers(const std::vector<IPABuffer> &buffers)\n>  \tfor (const IPABuffer &buffer : buffers) {\n>  \t\tconst FrameBuffer fb(buffer.planes);\n>  \t\tbuffers_.emplace(buffer.id,\n> -\t\t\t\t MappedFrameBuffer(&fb, PROT_READ | PROT_WRITE));\n> +\t\t\t\t MappedFrameBuffer(&fb, MappedFrameBuffer::ReadWrite));\n>  \t}\n>  }\n>  \n> diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp\n> index 76f67dd4567a..b905a9ca1dbf 100644\n> --- a/src/ipa/raspberrypi/raspberrypi.cpp\n> +++ b/src/ipa/raspberrypi/raspberrypi.cpp\n> @@ -411,7 +411,7 @@ void IPARPi::mapBuffers(const std::vector<IPABuffer> &buffers)\n>  {\n>  \tfor (const IPABuffer &buffer : buffers) {\n>  \t\tconst FrameBuffer fb(buffer.planes);\n> -\t\tbuffers_.emplace(buffer.id, MappedFrameBuffer(&fb, PROT_READ | PROT_WRITE));\n> +\t\tbuffers_.emplace(buffer.id, MappedFrameBuffer(&fb, MappedFrameBuffer::ReadWrite));\n>  \t}\n>  }\n>  \n> diff --git a/src/libcamera/mapped_framebuffer.cpp b/src/libcamera/mapped_framebuffer.cpp\n> index 0e30fc542154..65bdf9249133 100644\n> --- a/src/libcamera/mapped_framebuffer.cpp\n> +++ b/src/libcamera/mapped_framebuffer.cpp\n> @@ -142,6 +142,22 @@ MappedBuffer::~MappedBuffer()\n>   * \\brief Map a FrameBuffer using the MappedBuffer interface\n>   */\n>  \n> +/**\n> + * \\enum MappedFrameBuffer::MapModes\n> + * \\brief Specify the mapping mode for the FrameBuffer\n> + * \\var MappedFrameBuffer::Read\n> + * \\brief Create a Read-only mapping\n> + * \\var MappedFrameBuffer::Write\n> + * \\brief Create a Write-only mapping\n> + * \\var MappedFrameBuffer::ReadWrite\n> + * \\brief Create a mapping with both read and write capabilities\n> + */\n> +\n> +/**\n> + * \\typedef MappedFrameBuffer::MapMode\n> + * \\brief A bitwise combination of MappedFrameBuffer::MapModes values\n> + */\n> +\n>  /**\n>   * \\brief Map all planes of a FrameBuffer\n>   * \\param[in] buffer FrameBuffer to be mapped\n> @@ -151,12 +167,20 @@ MappedBuffer::~MappedBuffer()\n>   * The flags are passed directly to mmap and should be either PROT_READ,\n>   * PROT_WRITE, or a bitwise-or combination of both.\n\nThe documentation should be updated.\n\n>   */\n> -MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, int flags)\n> +MappedFrameBuffer::MappedFrameBuffer(const FrameBuffer *buffer, MapMode flags)\n>  {\n>  \tmaps_.reserve(buffer->planes().size());\n>  \n> +\tint mmap_flags = 0;\n\ns/mmap_flags/mmapFlags/\n\n> +\n> +\tif (flags & Read)\n> +\t\tmmap_flags |= PROT_READ;\n> +\n> +\tif (flags & Write)\n> +\t\tmmap_flags |= PROT_WRITE;\n> +\n>  \tfor (const FrameBuffer::Plane &plane : buffer->planes()) {\n> -\t\tvoid *address = mmap(nullptr, plane.length, flags,\n> +\t\tvoid *address = mmap(nullptr, plane.length, mmap_flags,\n>  \t\t\t\t     MAP_SHARED, plane.fd.fd(), 0);\n>  \t\tif (address == MAP_FAILED) {\n>  \t\t\terror_ = -errno;\n> diff --git a/test/mapped-buffer.cpp b/test/mapped-buffer.cpp\n> index a3d1511b74ce..ce75d9a9dbbd 100644\n> --- a/test/mapped-buffer.cpp\n> +++ b/test/mapped-buffer.cpp\n> @@ -71,7 +71,7 @@ protected:\n>  \t\tconst std::unique_ptr<FrameBuffer> &buffer = allocator_->buffers(stream_).front();\n>  \t\tstd::vector<MappedBuffer> maps;\n>  \n> -\t\tMappedFrameBuffer map(buffer.get(), PROT_READ);\n> +\t\tMappedFrameBuffer map(buffer.get(), MappedFrameBuffer::Read);\n>  \t\tif (!map.isValid()) {\n>  \t\t\tcout << \"Failed to successfully map buffer\" << endl;\n>  \t\t\treturn TestFail;\n> @@ -90,13 +90,13 @@ protected:\n>  \t\t}\n>  \n>  \t\t/* Test for multiple successful maps on the same buffer. */\n> -\t\tMappedFrameBuffer write_map(buffer.get(), PROT_WRITE);\n> +\t\tMappedFrameBuffer write_map(buffer.get(), MappedFrameBuffer::Write);\n>  \t\tif (!write_map.isValid()) {\n>  \t\t\tcout << \"Failed to map write buffer\" << endl;\n>  \t\t\treturn TestFail;\n>  \t\t}\n>  \n> -\t\tMappedFrameBuffer rw_map(buffer.get(), PROT_READ | PROT_WRITE);\n> +\t\tMappedFrameBuffer rw_map(buffer.get(), MappedFrameBuffer::ReadWrite);\n>  \t\tif (!rw_map.isValid()) {\n>  \t\t\tcout << \"Failed to map RW buffer\" << endl;\n>  \t\t\treturn TestFail;","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 4B7B5C3235\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri,  6 Aug 2021 13:24:33 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 06C4F68815;\n\tFri,  6 Aug 2021 15:24:33 +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 BAEF560266\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  6 Aug 2021 15:24:31 +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 582BD4FB;\n\tFri,  6 Aug 2021 15:24:31 +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=\"RwiAlMGE\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1628256271;\n\tbh=2LV0e1BXz802FjmSPs+7UyfOSAXABeGo042XQMefTds=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=RwiAlMGEHh+/SqC63/CG/ffUjpmh3RlZGCLhsSQ0sZOqdzJSYSy2myz+lmF9w6WhP\n\tND90lpArkbeHSJJw0FJsp+51r6h/8ZF4qVqW/MoSEdqHfnMqXzDbUwFR5/5M2dtok3\n\twL3HKflIYr2mitqyxp4RGuCeA7J17eIkoaU219os=","Date":"Fri, 6 Aug 2021 16:24:17 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Message-ID":"<YQ04AZTx+nW1FNoN@pendragon.ideasonboard.com>","References":"<20210806092529.572680-1-kieran.bingham@ideasonboard.com>\n\t<20210806131511.712042-1-kieran.bingham@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20210806131511.712042-1-kieran.bingham@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH] libcamera: MappedFrameBuffer: Use\n\ttyped Flags<MapModes>","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 <libcamera-devel@lists.libcamera.org>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]