From patchwork Mon Dec 30 12:05:08 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: =?utf-8?q?Niklas_S=C3=B6derlund?= X-Patchwork-Id: 2473 Return-Path: Received: from bin-mail-out-06.binero.net (bin-mail-out-06.binero.net [195.74.38.229]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id F00896046A for ; Mon, 30 Dec 2019 13:06:12 +0100 (CET) X-Halon-ID: c56016c4-2afc-11ea-a00b-005056917a89 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p4fca2fd0.dip0.t-ipconnect.de [79.202.47.208]) by bin-vsp-out-01.atm.binero.net (Halon) with ESMTPA id c56016c4-2afc-11ea-a00b-005056917a89; Mon, 30 Dec 2019 13:06:12 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Mon, 30 Dec 2019 13:05:08 +0100 Message-Id: <20191230120510.938333-24-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191230120510.938333-1-niklas.soderlund@ragnatech.se> References: <20191230120510.938333-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 23/25] qcam: Cache buffer memory mapping X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 30 Dec 2019 12:06:13 -0000 With the buffer allocator in use it's possible to cache the dmabuf memory mappings when starting the camera instead of mapping and unmapping them each time. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- src/qcam/main_window.cpp | 28 ++++++++++++++++++++++------ src/qcam/main_window.h | 1 + 2 files changed, 23 insertions(+), 6 deletions(-) diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp index e53ed2801291f2c5..2008931075b96260 100644 --- a/src/qcam/main_window.cpp +++ b/src/qcam/main_window.cpp @@ -201,6 +201,13 @@ int MainWindow::startCapture() } requests.push_back(request); + + /* Cache buffer memory mapping. */ + const FrameBuffer::Plane &plane = buffer->planes().front(); + void *memory = mmap(NULL, plane.length, PROT_READ, MAP_SHARED, + plane.fd.fd(), 0); + mappedBuffers_[plane.fd.fd()] = + std::make_pair(memory, plane.length); } titleTimer_.start(2000); @@ -230,6 +237,13 @@ error: for (Request *request : requests) delete request; + for (auto &itr : mappedBuffers_) { + void *memory = itr.second.first; + unsigned int length = itr.second.second; + munmap(memory, length); + } + mappedBuffers_.clear(); + camera_->freeBuffers(); return ret; } @@ -243,6 +257,13 @@ void MainWindow::stopCapture() if (ret) std::cout << "Failed to stop capture" << std::endl; + for (auto &itr : mappedBuffers_) { + void *memory = itr.second.first; + unsigned int length = itr.second.second; + munmap(memory, length); + } + mappedBuffers_.clear(); + camera_->freeBuffers(); isCapturing_ = false; @@ -297,15 +318,10 @@ int MainWindow::display(FrameBuffer *buffer) if (buffer->planes().size() != 1) return -EINVAL; - /* \todo: Once the FrameBuffer is done cache mapped memory. */ const FrameBuffer::Plane &plane = buffer->planes().front(); - void *memory = mmap(NULL, plane.length, PROT_READ, MAP_SHARED, - plane.fd.fd(), 0); - + void *memory = mappedBuffers_[plane.fd.fd()].first; unsigned char *raw = static_cast(memory); viewfinder_->display(raw, buffer->metadata().planes[0].bytesused); - munmap(memory, plane.length); - return 0; } diff --git a/src/qcam/main_window.h b/src/qcam/main_window.h index 05cde4ceab5f7ea1..04fb9e3ea869c3fb 100644 --- a/src/qcam/main_window.h +++ b/src/qcam/main_window.h @@ -71,6 +71,7 @@ private: uint32_t framesCaptured_; ViewFinder *viewfinder_; + std::map> mappedBuffers_; }; #endif /* __QCAM_MAIN_WINDOW__ */