[{"id":13721,"web_url":"https://patchwork.libcamera.org/comment/13721/","msgid":"<20201116121952.GG6540@pendragon.ideasonboard.com>","date":"2020-11-16T12:19:52","subject":"Re: [libcamera-devel] [PATCH v2 2/2] ipa: raspberrypi: Use\n\tMappedFrameBuffer for the IPA buffers","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Naush,\n\nThank you for the patch.\n\nOn Mon, Nov 16, 2020 at 11:24:58AM +0000, Naushir Patuck wrote:\n> Instead of directly mmaping/unmapping buffers passed to the IPA, use\n> a MappedFrameBuffer. The latter is a cleaner interface, and avoid\n> code duplication.\n> \n> Signed-off-by: Naushir Patuck <naush@raspberrypi.com>\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> ---\n>  src/ipa/raspberrypi/raspberrypi.cpp | 41 +++++++++++------------------\n>  1 file changed, 15 insertions(+), 26 deletions(-)\n> \n> diff --git a/src/ipa/raspberrypi/raspberrypi.cpp b/src/ipa/raspberrypi/raspberrypi.cpp\n> index 1c255aa2..6bb45b75 100644\n> --- a/src/ipa/raspberrypi/raspberrypi.cpp\n> +++ b/src/ipa/raspberrypi/raspberrypi.cpp\n> @@ -24,6 +24,7 @@\n>  \n>  #include <libipa/ipa_interface_wrapper.h>\n>  \n> +#include \"libcamera/internal/buffer.h\"\n>  #include \"libcamera/internal/camera_sensor.h\"\n>  #include \"libcamera/internal/log.h\"\n>  #include \"libcamera/internal/utils.h\"\n> @@ -110,8 +111,7 @@ private:\n>  \tvoid applyLS(const struct AlscStatus *lsStatus, ControlList &ctrls);\n>  \tvoid resampleTable(uint16_t dest[], double const src[12][16], int destW, int destH);\n>  \n> -\tstd::map<unsigned int, FrameBuffer> buffers_;\n> -\tstd::map<unsigned int, void *> buffersMemory_;\n> +\tstd::map<unsigned int, MappedFrameBuffer> buffers_;\n>  \n>  \tControlInfoMap unicamCtrls_;\n>  \tControlInfoMap ispCtrls_;\n> @@ -319,31 +319,18 @@ void IPARPi::configure(const CameraSensorInfo &sensorInfo,\n>  void IPARPi::mapBuffers(const std::vector<IPABuffer> &buffers)\n>  {\n>  \tfor (const IPABuffer &buffer : buffers) {\n> -\t\tauto elem = buffers_.emplace(std::piecewise_construct,\n> -\t\t\t\t\t     std::forward_as_tuple(buffer.id),\n> -\t\t\t\t\t     std::forward_as_tuple(buffer.planes));\n> -\t\tconst FrameBuffer &fb = elem.first->second;\n> -\n> -\t\tbuffersMemory_[buffer.id] = mmap(nullptr, fb.planes()[0].length,\n> -\t\t\t\t\t\t PROT_READ | PROT_WRITE, MAP_SHARED,\n> -\t\t\t\t\t\t fb.planes()[0].fd.fd(), 0);\n> -\n> -\t\tif (buffersMemory_[buffer.id] == MAP_FAILED) {\n> -\t\t\tint ret = -errno;\n> -\t\t\tLOG(IPARPI, Fatal) << \"Failed to mmap buffer: \" << strerror(-ret);\n> -\t\t}\n> +\t\tconst FrameBuffer fb = FrameBuffer(buffer.planes);\n\nThis could be written\n\n\t\tconst FrameBuffer fb(buffer.planes);\n\n> +\t\tbuffers_.emplace(buffer.id, MappedFrameBuffer(&fb, PROT_READ | PROT_WRITE));\n>  \t}\n>  }\n>  \n>  void IPARPi::unmapBuffers(const std::vector<unsigned int> &ids)\n>  {\n>  \tfor (unsigned int id : ids) {\n> -\t\tconst auto fb = buffers_.find(id);\n> -\t\tif (fb == buffers_.end())\n> +\t\tauto it = buffers_.find(id);\n> +\t\tif (it == buffers_.end())\n>  \t\t\tcontinue;\n>  \n> -\t\tmunmap(buffersMemory_[id], fb->second.planes()[0].length);\n> -\t\tbuffersMemory_.erase(id);\n>  \t\tbuffers_.erase(id);\n>  \t}\n>  }\n> @@ -785,15 +772,16 @@ void IPARPi::prepareISP(unsigned int bufferId)\n>  \n>  bool IPARPi::parseEmbeddedData(unsigned int bufferId, struct DeviceStatus &deviceStatus)\n>  {\n> -\tauto it = buffersMemory_.find(bufferId);\n> -\tif (it == buffersMemory_.end()) {\n> +\tauto it = buffers_.find(bufferId);\n> +\tif (it == buffers_.end()) {\n>  \t\tLOG(IPARPI, Error) << \"Could not find embedded buffer!\";\n>  \t\treturn false;\n>  \t}\n>  \n> -\tint size = buffers_.find(bufferId)->second.planes()[0].length;\n> +\tint size = buffers_.find(bufferId)->second.maps()[0].size();\n> +\tvoid *ptr = buffers_.find(bufferId)->second.maps()[0].data();\n\nCould we use it instead of buffers_.find(bufferId) in the two lines\nabove ? I'd even write\n\n\tSpan<uint8_t> mem = it->second.maps()[0];\n\nand use mem.size() and mem.data() below.\n\n>  \thelper_->Parser().SetBufferSize(size);\n> -\tRPiController::MdParser::Status status = helper_->Parser().Parse(it->second);\n> +\tRPiController::MdParser::Status status = helper_->Parser().Parse(ptr);\n>  \tif (status != RPiController::MdParser::Status::OK) {\n>  \t\tLOG(IPARPI, Error) << \"Embedded Buffer parsing failed, error \" << status;\n>  \t} else {\n> @@ -820,13 +808,14 @@ bool IPARPi::parseEmbeddedData(unsigned int bufferId, struct DeviceStatus &devic\n>  \n>  void IPARPi::processStats(unsigned int bufferId)\n>  {\n> -\tauto it = buffersMemory_.find(bufferId);\n> -\tif (it == buffersMemory_.end()) {\n> +\tauto it = buffers_.find(bufferId);\n> +\tif (it == buffers_.end()) {\n>  \t\tLOG(IPARPI, Error) << \"Could not find stats buffer!\";\n>  \t\treturn;\n>  \t}\n>  \n> -\tbcm2835_isp_stats *stats = static_cast<bcm2835_isp_stats *>(it->second);\n> +\tvoid *ptr = buffers_.find(bufferId)->second.maps()[0].data();\n> +\tbcm2835_isp_stats *stats = static_cast<bcm2835_isp_stats *>(ptr);\n\nSimilarly,\n\n\tSpan<uint8_t> mem = it->second.maps()[0];\n\tbcm2835_isp_stats *stats = static_cast<bcm2835_isp_stats *>(mem.data());\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n>  \tRPiController::StatisticsPtr statistics = std::make_shared<bcm2835_isp_stats>(*stats);\n>  \tcontroller_.Process(statistics, &rpiMetadata_);\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 012D0BE082\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 16 Nov 2020 12:20:03 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 861B6632C1;\n\tMon, 16 Nov 2020 13:20:03 +0100 (CET)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 8D4A2631B5\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 16 Nov 2020 13:20:01 +0100 (CET)","from pendragon.ideasonboard.com (85-76-51-86-nat.elisa-mobile.fi\n\t[85.76.51.86])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id E58B8A1B;\n\tMon, 16 Nov 2020 13:20:00 +0100 (CET)"],"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=\"slXaw369\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1605529201;\n\tbh=IIAk5hqba/bBjDh0FvfCWP/WtCXFX1U7/yqZjqv0IVA=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=slXaw369oEjthlfQvhD3nxlRj81u46jSiYNjioZqtC0IOoF/AeToNPw+Pq9WKn6at\n\tiHp0sb3qplxV2dor4ycNOvcNHgBW5LwsKNuWbb38nYga4EYzmPSYjzMSGEPFOSpyjR\n\tltDUvnrU2pM5Z8Y2T0RQe27sQo1v5bzQLk3RWI7I=","Date":"Mon, 16 Nov 2020 14:19:52 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Naushir Patuck <naush@raspberrypi.com>","Message-ID":"<20201116121952.GG6540@pendragon.ideasonboard.com>","References":"<20201116112458.148477-1-naush@raspberrypi.com>\n\t<20201116112458.148477-2-naush@raspberrypi.com>","MIME-Version":"1.0","Content-Disposition":"inline","In-Reply-To":"<20201116112458.148477-2-naush@raspberrypi.com>","Subject":"Re: [libcamera-devel] [PATCH v2 2/2] ipa: raspberrypi: Use\n\tMappedFrameBuffer for the IPA buffers","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","Content-Type":"text/plain; charset=\"us-ascii\"","Content-Transfer-Encoding":"7bit","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]