{"id":2577,"url":"https://patchwork.libcamera.org/api/patches/2577/?format=json","web_url":"https://patchwork.libcamera.org/patch/2577/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20200110193808.2266294-31-niklas.soderlund@ragnatech.se>","date":"2020-01-10T19:38:05","name":"[libcamera-devel,v3,30/33] cam: Cache buffer memory mapping","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"54d6021a5297035d346cf75f2ddb014e71d76d75","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/people/5/?format=json","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2577/mbox/","series":[{"id":615,"url":"https://patchwork.libcamera.org/api/series/615/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=615","date":"2020-01-10T19:37:35","name":"libcamera: Rework buffer API","version":3,"mbox":"https://patchwork.libcamera.org/series/615/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2577/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2577/checks/","tags":{},"headers":{"Return-Path":"<niklas.soderlund@ragnatech.se>","Received":["from bin-mail-out-05.binero.net (bin-mail-out-05.binero.net\n\t[195.74.38.228])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 9A7EF60699\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tFri, 10 Jan 2020 20:39:09 +0100 (CET)","from bismarck.berto.se (p54ac5d7b.dip0.t-ipconnect.de\n\t[84.172.93.123]) by bin-vsp-out-02.atm.binero.net (Halon) with ESMTPA\n\tid dcd8ebaf-33e0-11ea-b6d8-005056917f90;\n\tFri, 10 Jan 2020 20:39:06 +0100 (CET)"],"X-Halon-ID":"dcd8ebaf-33e0-11ea-b6d8-005056917f90","Authorized-sender":"niklas@soderlund.pp.se","From":"=?utf-8?q?Niklas_S=C3=B6derlund?= <niklas.soderlund@ragnatech.se>","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","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","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":"<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":"Fri, 10 Jan 2020 19:39:10 -0000"},"content":"With the buffer allocator in use it's possible to cache the dmabuf\nmemory mappings when starting the camera instead of mapping and\nunmapping them each time.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\n---\n src/cam/buffer_writer.cpp | 27 ++++++++++++++++++++++-----\n src/cam/buffer_writer.h   |  5 +++++\n src/cam/capture.cpp       |  3 +++\n 3 files changed, 30 insertions(+), 5 deletions(-)","diff":"diff --git a/src/cam/buffer_writer.cpp b/src/cam/buffer_writer.cpp\nindex 1d7366c87714cd91..c5a5eb46224ac3eb 100644\n--- a/src/cam/buffer_writer.cpp\n+++ b/src/cam/buffer_writer.cpp\n@@ -22,6 +22,27 @@ BufferWriter::BufferWriter(const std::string &pattern)\n {\n }\n \n+BufferWriter::~BufferWriter()\n+{\n+\tfor (auto &iter : mappedBuffers_) {\n+\t\tvoid *memory = iter.second.first;\n+\t\tunsigned int length = iter.second.second;\n+\t\tmunmap(memory, length);\n+\t}\n+\tmappedBuffers_.clear();\n+}\n+\n+void BufferWriter::mapBuffer(FrameBuffer *buffer)\n+{\n+\tfor (const FrameBuffer::Plane &plane : buffer->planes()) {\n+\t\tvoid *memory = mmap(NULL, plane.length, PROT_READ, MAP_SHARED,\n+\t\t\t\t    plane.fd.fd(), 0);\n+\n+\t\tmappedBuffers_[plane.fd.fd()] =\n+\t\t\tstd::make_pair(memory, plane.length);\n+\t}\n+}\n+\n int BufferWriter::write(FrameBuffer *buffer, const std::string &streamName)\n {\n \tstd::string filename;\n@@ -44,9 +65,7 @@ int BufferWriter::write(FrameBuffer *buffer, const std::string &streamName)\n \t\treturn -errno;\n \n \tfor (const FrameBuffer::Plane &plane : buffer->planes()) {\n-\t\t/* \\todo Once the FrameBuffer is done cache mapped memory. */\n-\t\tvoid *data = mmap(NULL, plane.length, PROT_READ, MAP_SHARED,\n-\t\t\t\t  plane.fd.fd(), 0);\n+\t\tvoid *data = mappedBuffers_[plane.fd.fd()].first;\n \t\tunsigned int length = plane.length;\n \n \t\tret = ::write(fd, data, length);\n@@ -61,8 +80,6 @@ int BufferWriter::write(FrameBuffer *buffer, const std::string &streamName)\n \t\t\t\t  << length << std::endl;\n \t\t\tbreak;\n \t\t}\n-\n-\t\tmunmap(data, length);\n \t}\n \n \tclose(fd);\ndiff --git a/src/cam/buffer_writer.h b/src/cam/buffer_writer.h\nindex 5917a7dfb5e28106..8c9b2436fdae4fc3 100644\n--- a/src/cam/buffer_writer.h\n+++ b/src/cam/buffer_writer.h\n@@ -7,6 +7,7 @@\n #ifndef __LIBCAMERA_BUFFER_WRITER_H__\n #define __LIBCAMERA_BUFFER_WRITER_H__\n \n+#include <map>\n #include <string>\n \n #include <libcamera/buffer.h>\n@@ -15,12 +16,16 @@ class BufferWriter\n {\n public:\n \tBufferWriter(const std::string &pattern = \"frame-#.bin\");\n+\t~BufferWriter();\n+\n+\tvoid mapBuffer(libcamera::FrameBuffer *buffer);\n \n \tint write(libcamera::FrameBuffer *buffer,\n \t\t  const std::string &streamName);\n \n private:\n \tstd::string pattern_;\n+\tstd::map<int, std::pair<void *, unsigned int>> mappedBuffers_;\n };\n \n #endif /* __LIBCAMERA_BUFFER_WRITER_H__ */\ndiff --git a/src/cam/capture.cpp b/src/cam/capture.cpp\nindex dd078eb0ae4a2c62..738fa1c267eb6e36 100644\n--- a/src/cam/capture.cpp\n+++ b/src/cam/capture.cpp\n@@ -116,6 +116,9 @@ int Capture::capture(EventLoop *loop, FrameBufferAllocator *allocator)\n \t\t\t\t\t  << std::endl;\n \t\t\t\treturn ret;\n \t\t\t}\n+\n+\t\t\tif (writer_)\n+\t\t\t\twriter_->mapBuffer(buffer.get());\n \t\t}\n \n \t\trequests.push_back(request);\n","prefixes":["libcamera-devel","v3","30/33"]}