{"id":13249,"url":"https://patchwork.libcamera.org/api/1.1/patches/13249/?format=json","web_url":"https://patchwork.libcamera.org/patch/13249/","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":"<20210806131511.712042-1-kieran.bingham@ideasonboard.com>","date":"2021-08-06T13:15:11","name":"[libcamera-devel] libcamera: MappedFrameBuffer: Use typed Flags<MapModes>","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"ceab834e55b7d4ca8bdbc6060a3dd9133742c4f4","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/1.1/people/4/?format=json","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/13249/mbox/","series":[{"id":2318,"url":"https://patchwork.libcamera.org/api/1.1/series/2318/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2318","date":"2021-08-06T13:15:11","name":"[libcamera-devel] libcamera: MappedFrameBuffer: Use typed Flags<MapModes>","version":1,"mbox":"https://patchwork.libcamera.org/series/2318/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/13249/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/13249/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 EAC1BBD87D\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri,  6 Aug 2021 13:15:16 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 40C2D68822;\n\tFri,  6 Aug 2021 15:15:16 +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 A175C60266\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri,  6 Aug 2021 15:15:14 +0200 (CEST)","from Monstersaurus.local\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 217784FB;\n\tFri,  6 Aug 2021 15:15:14 +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=\"mNZjVhji\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1628255714;\n\tbh=U5yFXwYn2I7YJbgCgf1Htbpy0Ltl1vqaFOfD10PjLOk=;\n\th=From:To:Cc:Subject:Date:In-Reply-To:References:From;\n\tb=mNZjVhjiNhqMe0MOEO7ObZi8nUP1PsKyHlOlXwf4RHh8DZJct/5ZtEOMVcXMtagzO\n\tXxM9iFyoWXZysIeooK9YiD/8GRoL80d5z5F8O5AJkFWoMIKcVMey2W0LvKMRYWgMff\n\t8A5T8Zl14tOLEJGHR3LNWqvAJ1kaejxxxmvSEGdk=","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","To":"libcamera devel <libcamera-devel@lists.libcamera.org>","Date":"Fri,  6 Aug 2021 14:15:11 +0100","Message-Id":"<20210806131511.712042-1-kieran.bingham@ideasonboard.com>","X-Mailer":"git-send-email 2.30.2","In-Reply-To":"<20210806092529.572680-1-kieran.bingham@ideasonboard.com>","References":"<20210806092529.572680-1-kieran.bingham@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH] libcamera: MappedFrameBuffer: Use typed\n\tFlags<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>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"Remove the need for callers to reference PROT_READ/PROT_WRITE directly\nfrom <sys/mman.h> by instead exposing the Read/Write mapping options as\nflags from the MappedFrameBuffer class itself.\n\nWhile here, introduce the <stdint.h> header which is required for the\nint8_t as part of the Plane.\n\nSigned-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(-)","diff":"diff --git a/include/libcamera/internal/mapped_framebuffer.h b/include/libcamera/internal/mapped_framebuffer.h\nindex 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+\t\tRead = 0 << 1,\n+\t\tWrite = 1 << 1,\n+\n+\t\tReadWrite = Read | Write,\n+\t};\n+\n+\tusing MapMode = Flags<MapModes>;\n+\n+\tMappedFrameBuffer(const FrameBuffer *buffer, MapMode flags);\n };\n \n } /* namespace libcamera */\ndiff --git a/src/android/jpeg/encoder_libjpeg.cpp b/src/android/jpeg/encoder_libjpeg.cpp\nindex 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());\ndiff --git a/src/android/jpeg/thumbnailer.cpp b/src/android/jpeg/thumbnailer.cpp\nindex 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 : \"\ndiff --git a/src/android/yuv/post_processor_yuv.cpp b/src/android/yuv/post_processor_yuv.cpp\nindex 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;\ndiff --git a/src/ipa/ipu3/ipu3.cpp b/src/ipa/ipu3/ipu3.cpp\nindex 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 \ndiff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp\nindex 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 \ndiff --git a/src/libcamera/mapped_framebuffer.cpp b/src/libcamera/mapped_framebuffer.cpp\nindex 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  */\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+\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;\ndiff --git a/test/mapped-buffer.cpp b/test/mapped-buffer.cpp\nindex 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;\n","prefixes":["libcamera-devel"]}