[{"id":11574,"web_url":"https://patchwork.libcamera.org/comment/11574/","msgid":"<20200724170143.GP5921@pendragon.ideasonboard.com>","date":"2020-07-24T17:01:43","subject":"Re: [libcamera-devel] [RFC PATCH 3/8] qcam: Convert to use\n\tMappedFrameBuffer","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 Mon, Jul 20, 2020 at 11:42:27PM +0100, Kieran Bingham wrote:\n> Remove the local mapping code, and utilise the libcamera buffer map\n> helper class.\n> \n> Signed-off-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n> ---\n> \n> Working towards a more generic MappedBuffer base interface of the newly\n> introduced MappedFrameBuffer hits a snag that the viewfinder in qcam\n> already defines a type which conflicts.\n> \n> Remove it by converting qcam to use the new MappedFrameBuffer types.\n\nI'd drop this as I think we should start with MappedFrameBuffer being an\ninternal class.\n\n>  src/qcam/main_window.cpp | 27 +++++++++++----------------\n>  src/qcam/main_window.h   |  2 +-\n>  src/qcam/viewfinder.cpp  |  4 ++--\n>  src/qcam/viewfinder.h    |  7 +------\n>  4 files changed, 15 insertions(+), 25 deletions(-)\n> \n> diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp\n> index 7bc13603774e..b25260ba7b28 100644\n> --- a/src/qcam/main_window.cpp\n> +++ b/src/qcam/main_window.cpp\n> @@ -446,10 +446,14 @@ int MainWindow::startCapture()\n>  \n>  \t\tfor (const std::unique_ptr<FrameBuffer> &buffer : allocator_->buffers(stream)) {\n>  \t\t\t/* Map memory buffers and cache the mappings. */\n> -\t\t\tconst FrameBuffer::Plane &plane = buffer->planes().front();\n> -\t\t\tvoid *memory = mmap(NULL, plane.length, PROT_READ, MAP_SHARED,\n> -\t\t\t\t\t    plane.fd.fd(), 0);\n> -\t\t\tmappedBuffers_[buffer.get()] = { memory, plane.length };\n> +\t\t\tMappedFrameBuffer mapped(buffer.get(), PROT_READ);\n> +\t\t\tif (!mapped.isValid()) {\n> +\t\t\t\tqWarning() << \"Failed to map buffer\";\n> +\t\t\t\tret = mapped.error();\n> +\t\t\t\tgoto error;\n> +\t\t\t}\n> +\n> +\t\t\tmappedBuffers_.insert({buffer.get(), std::move(mapped)});\n>  \n>  \t\t\t/* Store buffers on the free list. */\n>  \t\t\tfreeBuffers_[stream].enqueue(buffer.get());\n> @@ -512,12 +516,7 @@ error:\n>  \tfor (Request *request : requests)\n>  \t\tdelete request;\n>  \n> -\tfor (auto &iter : mappedBuffers_) {\n> -\t\tconst MappedBuffer &buffer = iter.second;\n> -\t\tmunmap(buffer.memory, buffer.size);\n> -\t}\n>  \tmappedBuffers_.clear();\n> -\n>  \tfreeBuffers_.clear();\n>  \n>  \tdelete allocator_;\n> @@ -548,10 +547,6 @@ void MainWindow::stopCapture()\n>  \n>  \tcamera_->requestCompleted.disconnect(this, &MainWindow::requestComplete);\n>  \n> -\tfor (auto &iter : mappedBuffers_) {\n> -\t\tconst MappedBuffer &buffer = iter.second;\n> -\t\tmunmap(buffer.memory, buffer.size);\n> -\t}\n>  \tmappedBuffers_.clear();\n>  \n>  \tdelete allocator_;\n> @@ -643,10 +638,10 @@ void MainWindow::processRaw(FrameBuffer *buffer, const ControlList &metadata)\n>  \t\t\t\t\t\t\t\"DNG Files (*.dng)\");\n>  \n>  \tif (!filename.isEmpty()) {\n> -\t\tconst MappedBuffer &mapped = mappedBuffers_[buffer];\n> +\t\tconst MappedFrameBuffer &mapped = mappedBuffers_.at(buffer);\n>  \t\tDNGWriter::write(filename.toStdString().c_str(), camera_.get(),\n>  \t\t\t\t rawStream_->configuration(), metadata, buffer,\n> -\t\t\t\t mapped.memory);\n> +\t\t\t\t mapped.maps()[0].address);\n>  \t}\n>  #endif\n>  \n> @@ -720,7 +715,7 @@ void MainWindow::processViewfinder(FrameBuffer *buffer)\n>  \t\t<< \"fps:\" << Qt::fixed << qSetRealNumberPrecision(2) << fps;\n>  \n>  \t/* Render the frame on the viewfinder. */\n> -\tviewfinder_->render(buffer, &mappedBuffers_[buffer]);\n> +\tviewfinder_->render(buffer, &mappedBuffers_.at(buffer));\n>  }\n>  \n>  void MainWindow::queueRequest(FrameBuffer *buffer)\n> diff --git a/src/qcam/main_window.h b/src/qcam/main_window.h\n> index 4606fe487ad4..ddf51ca01111 100644\n> --- a/src/qcam/main_window.h\n> +++ b/src/qcam/main_window.h\n> @@ -119,7 +119,7 @@ private:\n>  \tFrameBufferAllocator *allocator_;\n>  \n>  \tstd::unique_ptr<CameraConfiguration> config_;\n> -\tstd::map<FrameBuffer *, MappedBuffer> mappedBuffers_;\n> +\tstd::map<FrameBuffer *, MappedFrameBuffer> mappedBuffers_;\n>  \n>  \t/* Capture state, buffers queue and statistics */\n>  \tbool isCapturing_;\n> diff --git a/src/qcam/viewfinder.cpp b/src/qcam/viewfinder.cpp\n> index dcf8a15d2df6..591d26eae87c 100644\n> --- a/src/qcam/viewfinder.cpp\n> +++ b/src/qcam/viewfinder.cpp\n> @@ -78,14 +78,14 @@ int ViewFinder::setFormat(const libcamera::PixelFormat &format,\n>  \treturn 0;\n>  }\n>  \n> -void ViewFinder::render(libcamera::FrameBuffer *buffer, MappedBuffer *map)\n> +void ViewFinder::render(libcamera::FrameBuffer *buffer, libcamera::MappedFrameBuffer *map)\n>  {\n>  \tif (buffer->planes().size() != 1) {\n>  \t\tqWarning() << \"Multi-planar buffers are not supported\";\n>  \t\treturn;\n>  \t}\n>  \n> -\tunsigned char *memory = static_cast<unsigned char *>(map->memory);\n> +\tunsigned char *memory = static_cast<unsigned char *>(map->maps()[0].address);\n>  \tsize_t size = buffer->metadata().planes[0].bytesused;\n>  \n>  \t{\n> diff --git a/src/qcam/viewfinder.h b/src/qcam/viewfinder.h\n> index 26a1320537d2..c4cc51f14dda 100644\n> --- a/src/qcam/viewfinder.h\n> +++ b/src/qcam/viewfinder.h\n> @@ -23,11 +23,6 @@\n>  \n>  class QImage;\n>  \n> -struct MappedBuffer {\n> -\tvoid *memory;\n> -\tsize_t size;\n> -};\n> -\n>  class ViewFinder : public QWidget\n>  {\n>  \tQ_OBJECT\n> @@ -39,7 +34,7 @@ public:\n>  \tconst QList<libcamera::PixelFormat> &nativeFormats() const;\n>  \n>  \tint setFormat(const libcamera::PixelFormat &format, const QSize &size);\n> -\tvoid render(libcamera::FrameBuffer *buffer, MappedBuffer *map);\n> +\tvoid render(libcamera::FrameBuffer *buffer, libcamera::MappedFrameBuffer *map);\n>  \tvoid stop();\n>  \n>  \tQImage getCurrentImage();","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 A6F6CBD878\n\tfor <parsemail@patchwork.libcamera.org>;\n\tFri, 24 Jul 2020 17:01:52 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 763DD61218;\n\tFri, 24 Jul 2020 19:01:52 +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 EBCCD605B2\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 24 Jul 2020 19:01:50 +0200 (CEST)","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 8B38C538;\n\tFri, 24 Jul 2020 19:01:50 +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=\"UyGn+KZf\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1595610110;\n\tbh=MgRiapC+FR1hIIUYXTMuiwMwIxjDb21mKhSaH3apGLw=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=UyGn+KZfncZtbyjCaQM4I/9vEjpgqzWa7eoXGBzl5LTIdJAO5mnMFq8LzwVYB2eQK\n\tFBlr2dky90fzfIWvDAWLzUVILYVTp6faOLbYJyh9n1gDMmauWKExAJvUJYPkeqP4um\n\tK+xkLjxbGB7u+9los0cwqhJOkwk81VnqlgIRkwMg=","Date":"Fri, 24 Jul 2020 20:01:43 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Message-ID":"<20200724170143.GP5921@pendragon.ideasonboard.com>","References":"<20200720224232.153717-1-kieran.bingham@ideasonboard.com>\n\t<20200720224232.153717-4-kieran.bingham@ideasonboard.com>","MIME-Version":"1.0","Content-Disposition":"inline","In-Reply-To":"<20200720224232.153717-4-kieran.bingham@ideasonboard.com>","Subject":"Re: [libcamera-devel] [RFC PATCH 3/8] qcam: Convert to use\n\tMappedFrameBuffer","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>","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>"}}]