From patchwork Sun Jan 12 01:02:10 2020 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: 2612 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 179AE606FC for ; Sun, 12 Jan 2020 02:03:30 +0100 (CET) X-Halon-ID: 56806cbd-34d7-11ea-b6d8-005056917f90 Authorized-sender: niklas@soderlund.pp.se Received: from bismarck.berto.se (p54ac5d7b.dip0.t-ipconnect.de [84.172.93.123]) by bin-vsp-out-02.atm.binero.net (Halon) with ESMTPA id 56806cbd-34d7-11ea-b6d8-005056917f90; Sun, 12 Jan 2020 02:03:26 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Sun, 12 Jan 2020 02:02:10 +0100 Message-Id: <20200112010212.2609025-31-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200112010212.2609025-1-niklas.soderlund@ragnatech.se> References: <20200112010212.2609025-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v4 30/32] 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: Sun, 12 Jan 2020 01:03:30 -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 701a2b9a73d53d96..047bf15ea7c188f7 100644 --- a/src/qcam/main_window.cpp +++ b/src/qcam/main_window.cpp @@ -201,6 +201,13 @@ int MainWindow::startCapture() } requests.push_back(request); + + /* Map memory buffers and cache the mappings. */ + 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 &iter : mappedBuffers_) { + void *memory = iter.second.first; + unsigned int length = iter.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 &iter : mappedBuffers_) { + void *memory = iter.second.first; + unsigned int length = iter.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__ */