From patchwork Fri Jan 10 19:38:05 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: 2577 Return-Path: Received: from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net [195.74.38.228]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 9A7EF60699 for ; Fri, 10 Jan 2020 20:39:09 +0100 (CET) X-Halon-ID: dcd8ebaf-33e0-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 dcd8ebaf-33e0-11ea-b6d8-005056917f90; Fri, 10 Jan 2020 20:39:06 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= To: libcamera-devel@lists.libcamera.org Date: Fri, 10 Jan 2020 20:38:05 +0100 Message-Id: <20200110193808.2266294-31-niklas.soderlund@ragnatech.se> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200110193808.2266294-1-niklas.soderlund@ragnatech.se> References: <20200110193808.2266294-1-niklas.soderlund@ragnatech.se> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v3 30/33] cam: 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: Fri, 10 Jan 2020 19:39:10 -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/cam/buffer_writer.cpp | 27 ++++++++++++++++++++++----- src/cam/buffer_writer.h | 5 +++++ src/cam/capture.cpp | 3 +++ 3 files changed, 30 insertions(+), 5 deletions(-) diff --git a/src/cam/buffer_writer.cpp b/src/cam/buffer_writer.cpp index 1d7366c87714cd91..c5a5eb46224ac3eb 100644 --- a/src/cam/buffer_writer.cpp +++ b/src/cam/buffer_writer.cpp @@ -22,6 +22,27 @@ BufferWriter::BufferWriter(const std::string &pattern) { } +BufferWriter::~BufferWriter() +{ + for (auto &iter : mappedBuffers_) { + void *memory = iter.second.first; + unsigned int length = iter.second.second; + munmap(memory, length); + } + mappedBuffers_.clear(); +} + +void BufferWriter::mapBuffer(FrameBuffer *buffer) +{ + for (const FrameBuffer::Plane &plane : buffer->planes()) { + void *memory = mmap(NULL, plane.length, PROT_READ, MAP_SHARED, + plane.fd.fd(), 0); + + mappedBuffers_[plane.fd.fd()] = + std::make_pair(memory, plane.length); + } +} + int BufferWriter::write(FrameBuffer *buffer, const std::string &streamName) { std::string filename; @@ -44,9 +65,7 @@ int BufferWriter::write(FrameBuffer *buffer, const std::string &streamName) return -errno; for (const FrameBuffer::Plane &plane : buffer->planes()) { - /* \todo Once the FrameBuffer is done cache mapped memory. */ - void *data = mmap(NULL, plane.length, PROT_READ, MAP_SHARED, - plane.fd.fd(), 0); + void *data = mappedBuffers_[plane.fd.fd()].first; unsigned int length = plane.length; ret = ::write(fd, data, length); @@ -61,8 +80,6 @@ int BufferWriter::write(FrameBuffer *buffer, const std::string &streamName) << length << std::endl; break; } - - munmap(data, length); } close(fd); diff --git a/src/cam/buffer_writer.h b/src/cam/buffer_writer.h index 5917a7dfb5e28106..8c9b2436fdae4fc3 100644 --- a/src/cam/buffer_writer.h +++ b/src/cam/buffer_writer.h @@ -7,6 +7,7 @@ #ifndef __LIBCAMERA_BUFFER_WRITER_H__ #define __LIBCAMERA_BUFFER_WRITER_H__ +#include #include #include @@ -15,12 +16,16 @@ class BufferWriter { public: BufferWriter(const std::string &pattern = "frame-#.bin"); + ~BufferWriter(); + + void mapBuffer(libcamera::FrameBuffer *buffer); int write(libcamera::FrameBuffer *buffer, const std::string &streamName); private: std::string pattern_; + std::map> mappedBuffers_; }; #endif /* __LIBCAMERA_BUFFER_WRITER_H__ */ diff --git a/src/cam/capture.cpp b/src/cam/capture.cpp index dd078eb0ae4a2c62..738fa1c267eb6e36 100644 --- a/src/cam/capture.cpp +++ b/src/cam/capture.cpp @@ -116,6 +116,9 @@ int Capture::capture(EventLoop *loop, FrameBufferAllocator *allocator) << std::endl; return ret; } + + if (writer_) + writer_->mapBuffer(buffer.get()); } requests.push_back(request);