[{"id":3387,"web_url":"https://patchwork.libcamera.org/comment/3387/","msgid":"<20200108030300.GA4871@pendragon.ideasonboard.com>","date":"2020-01-08T03:03:00","subject":"Re: [libcamera-devel] [PATCH v2 23/25] qcam: Cache buffer memory\n\tmapping","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Niklas,\n\nThank you for the patch.\n\nOn Mon, Dec 30, 2019 at 01:05:08PM +0100, Niklas Söderlund wrote:\n> With the buffer allocator in use it's possible to cache the dmabuf\n> memory mappings when starting the camera instead of mapping and\n> unmapping them each time.\n> \n> Signed-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n> ---\n>  src/qcam/main_window.cpp | 28 ++++++++++++++++++++++------\n>  src/qcam/main_window.h   |  1 +\n>  2 files changed, 23 insertions(+), 6 deletions(-)\n> \n> diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp\n> index e53ed2801291f2c5..2008931075b96260 100644\n> --- a/src/qcam/main_window.cpp\n> +++ b/src/qcam/main_window.cpp\n> @@ -201,6 +201,13 @@ int MainWindow::startCapture()\n>  \t\t}\n>  \n>  \t\trequests.push_back(request);\n> +\n> +\t\t/* Cache buffer memory mapping. */\n\n\"Map memory buffers and cache the mappings.\"\n\n> +\t\tconst FrameBuffer::Plane &plane = buffer->planes().front();\n> +\t\tvoid *memory = mmap(NULL, plane.length, PROT_READ, MAP_SHARED,\n> +\t\t\t\t    plane.fd.fd(), 0);\n> +\t\tmappedBuffers_[plane.fd.fd()] =\n> +\t\t\tstd::make_pair(memory, plane.length);\n\nI think we should create a helper for this, but it can be done on top.\n\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\n>  \t}\n>  \n>  \ttitleTimer_.start(2000);\n> @@ -230,6 +237,13 @@ error:\n>  \tfor (Request *request : requests)\n>  \t\tdelete request;\n>  \n> +\tfor (auto &itr : mappedBuffers_) {\n\ns/itr/iter/ or s/itr/it/ (that's what we use elsewhere in the code)\n\n> +\t\tvoid *memory = itr.second.first;\n> +\t\tunsigned int length = itr.second.second;\n> +\t\tmunmap(memory, length);\n> +\t}\n> +\tmappedBuffers_.clear();\n> +\n>  \tcamera_->freeBuffers();\n>  \treturn ret;\n>  }\n> @@ -243,6 +257,13 @@ void MainWindow::stopCapture()\n>  \tif (ret)\n>  \t\tstd::cout << \"Failed to stop capture\" << std::endl;\n>  \n> +\tfor (auto &itr : mappedBuffers_) {\n> +\t\tvoid *memory = itr.second.first;\n> +\t\tunsigned int length = itr.second.second;\n> +\t\tmunmap(memory, length);\n> +\t}\n> +\tmappedBuffers_.clear();\n> +\n>  \tcamera_->freeBuffers();\n>  \tisCapturing_ = false;\n>  \n> @@ -297,15 +318,10 @@ int MainWindow::display(FrameBuffer *buffer)\n>  \tif (buffer->planes().size() != 1)\n>  \t\treturn -EINVAL;\n>  \n> -\t/* \\todo: Once the FrameBuffer is done cache mapped memory. */\n>  \tconst FrameBuffer::Plane &plane = buffer->planes().front();\n> -\tvoid *memory = mmap(NULL, plane.length, PROT_READ, MAP_SHARED,\n> -\t\t\t    plane.fd.fd(), 0);\n> -\n> +\tvoid *memory = mappedBuffers_[plane.fd.fd()].first;\n>  \tunsigned char *raw = static_cast<unsigned char *>(memory);\n>  \tviewfinder_->display(raw, buffer->metadata().planes[0].bytesused);\n>  \n> -\tmunmap(memory, plane.length);\n> -\n>  \treturn 0;\n>  }\n> diff --git a/src/qcam/main_window.h b/src/qcam/main_window.h\n> index 05cde4ceab5f7ea1..04fb9e3ea869c3fb 100644\n> --- a/src/qcam/main_window.h\n> +++ b/src/qcam/main_window.h\n> @@ -71,6 +71,7 @@ private:\n>  \tuint32_t framesCaptured_;\n>  \n>  \tViewFinder *viewfinder_;\n> +\tstd::map<int, std::pair<void *, unsigned int>> mappedBuffers_;\n>  };\n>  \n>  #endif /* __QCAM_MAIN_WINDOW__ */","headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["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 5C2A16045D\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tWed,  8 Jan 2020 04:03:12 +0100 (CET)","from pendragon.ideasonboard.com (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id D64B652F;\n\tWed,  8 Jan 2020 04:03:11 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1578452592;\n\tbh=Xjk2Uxs3s/S0ZR25jWQzfHSeFYUahH8XQYWLxEm1cBY=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=D+4j0oaG56ucp/nyI2dgJTgLsZrtCx8Umv1BNAZeRrPLNU6XSymSQGLW5DfGrNHdl\n\tExMF/IgsCho2E7o3mPtiy/odQJLrhzoqTrhO7XHXo19MywAHJ9echUKyQHpV/MUkHh\n\tQ+XpoZurPT+HaI3qlFjGU4v41vSVV9Bwhomeohig=","Date":"Wed, 8 Jan 2020 05:03:00 +0200","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Niklas =?utf-8?q?S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","Cc":"libcamera-devel@lists.libcamera.org","Message-ID":"<20200108030300.GA4871@pendragon.ideasonboard.com>","References":"<20191230120510.938333-1-niklas.soderlund@ragnatech.se>\n\t<20191230120510.938333-24-niklas.soderlund@ragnatech.se>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","Content-Transfer-Encoding":"8bit","In-Reply-To":"<20191230120510.938333-24-niklas.soderlund@ragnatech.se>","User-Agent":"Mutt/1.10.1 (2018-07-13)","Subject":"Re: [libcamera-devel] [PATCH v2 23/25] qcam: Cache buffer memory\n\tmapping","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>","X-List-Received-Date":"Wed, 08 Jan 2020 03:03:12 -0000"}}]