{"id":2587,"url":"https://patchwork.libcamera.org/api/1.1/patches/2587/?format=json","web_url":"https://patchwork.libcamera.org/patch/2587/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/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":"<20200112010212.2609025-6-niklas.soderlund@ragnatech.se>","date":"2020-01-12T01:01:45","name":"[libcamera-devel,v4,05/32] v4l2: camera: Handle memory mapping of buffers directly","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"b1b6aa9cd99612ba97754395904db324353d0c13","submitter":{"id":5,"url":"https://patchwork.libcamera.org/api/1.1/people/5/?format=json","name":"Niklas Söderlund","email":"niklas.soderlund@ragnatech.se"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/2587/mbox/","series":[{"id":617,"url":"https://patchwork.libcamera.org/api/1.1/series/617/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=617","date":"2020-01-12T01:01:40","name":"libcamera: Rework buffer API","version":4,"mbox":"https://patchwork.libcamera.org/series/617/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/2587/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/2587/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 73D1C606D0\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun, 12 Jan 2020 02:03:02 +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 45f62063-34d7-11ea-b6d8-005056917f90;\n\tSun, 12 Jan 2020 02:02:58 +0100 (CET)"],"X-Halon-ID":"45f62063-34d7-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":"Sun, 12 Jan 2020 02:01:45 +0100","Message-Id":"<20200112010212.2609025-6-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","Content-Type":"text/plain; charset=UTF-8","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v4 05/32] v4l2: camera: Handle memory\n\tmapping of buffers directly","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":"Sun, 12 Jan 2020 01:03:02 -0000"},"content":"In the upcoming FrameBuffer API the memory mapping of buffers will be\nleft to the user of the FrameBuffer objects. Prepare the V4L2\ncompatibility layer to this upcoming change to ease conversion to the\nnew API.\n\nSigned-off-by: Niklas Söderlund <niklas.soderlund@ragnatech.se>\nReviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n* Changes since v3\n- Return FileDescriptor from getBufferFd()\n- Call fops().munmap() in V4L2CameraProxy::munmap()\n---\n src/v4l2/v4l2_camera.cpp         | 12 ++++++------\n src/v4l2/v4l2_camera.h           |  5 +++--\n src/v4l2/v4l2_camera_proxy.cpp   | 24 +++++++++++++++++++-----\n src/v4l2/v4l2_camera_proxy.h     |  2 +-\n src/v4l2/v4l2_compat_manager.cpp |  2 +-\n 5 files changed, 30 insertions(+), 15 deletions(-)","diff":"diff --git a/src/v4l2/v4l2_camera.cpp b/src/v4l2/v4l2_camera.cpp\nindex 2a507b9bb8318025..07f05a31261c1b25 100644\n--- a/src/v4l2/v4l2_camera.cpp\n+++ b/src/v4l2/v4l2_camera.cpp\n@@ -121,12 +121,6 @@ int V4L2Camera::configure(StreamConfiguration *streamConfigOut,\n \treturn 0;\n }\n \n-void *V4L2Camera::mmap(unsigned int index)\n-{\n-\tStream *stream = *camera_->streams().begin();\n-\treturn stream->buffers()[index].planes()[0].mem();\n-}\n-\n int V4L2Camera::allocBuffers(unsigned int count)\n {\n \tint ret = camera_->allocateBuffers();\n@@ -138,6 +132,12 @@ void V4L2Camera::freeBuffers()\n \tcamera_->freeBuffers();\n }\n \n+FileDescriptor V4L2Camera::getBufferFd(unsigned int index)\n+{\n+\tStream *stream = *camera_->streams().begin();\n+\treturn FileDescriptor(stream->buffers()[index].planes()[0].dmabuf());\n+}\n+\n int V4L2Camera::streamOn()\n {\n \tif (isRunning_)\ndiff --git a/src/v4l2/v4l2_camera.h b/src/v4l2/v4l2_camera.h\nindex 81f7908e5e8a6beb..f760316c854fba2f 100644\n--- a/src/v4l2/v4l2_camera.h\n+++ b/src/v4l2/v4l2_camera.h\n@@ -14,6 +14,7 @@\n \n #include <libcamera/buffer.h>\n #include <libcamera/camera.h>\n+#include <libcamera/file_descriptor.h>\n \n #include \"semaphore.h\"\n \n@@ -53,14 +54,14 @@ public:\n \tvoid getStreamConfig(StreamConfiguration *streamConfig);\n \tstd::vector<V4L2FrameMetadata> completedBuffers();\n \n-\tvoid *mmap(unsigned int index);\n-\n \tint configure(StreamConfiguration *streamConfigOut,\n \t\t      const Size &size, PixelFormat pixelformat,\n \t\t      unsigned int bufferCount);\n \n \tint allocBuffers(unsigned int count);\n \tvoid freeBuffers();\n+\tFileDescriptor getBufferFd(unsigned int index);\n+\n \tint streamOn();\n \tint streamOff();\n \ndiff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp\nindex 52f8468cdaa06a7a..b4a9e2107c0f9f28 100644\n--- a/src/v4l2/v4l2_camera_proxy.cpp\n+++ b/src/v4l2/v4l2_camera_proxy.cpp\n@@ -75,7 +75,8 @@ void V4L2CameraProxy::close()\n \tvcam_->invokeMethod(&V4L2Camera::close, ConnectionTypeBlocking);\n }\n \n-void *V4L2CameraProxy::mmap(size_t length, int prot, int flags, off_t offset)\n+void *V4L2CameraProxy::mmap(void *addr, size_t length, int prot, int flags,\n+\t\t\t    off_t offset)\n {\n \tLOG(V4L2Compat, Debug) << \"Servicing mmap\";\n \n@@ -91,13 +92,22 @@ void *V4L2CameraProxy::mmap(size_t length, int prot, int flags, off_t offset)\n \t\treturn MAP_FAILED;\n \t}\n \n-\tvoid *val = vcam_->invokeMethod(&V4L2Camera::mmap,\n-\t\t\t\t\tConnectionTypeBlocking, index);\n+\tFileDescriptor fd = vcam_->invokeMethod(&V4L2Camera::getBufferFd,\n+\t\t\t\t\t\tConnectionTypeBlocking, index);\n+\tif (!fd.isValid()) {\n+\t\terrno = EINVAL;\n+\t\treturn MAP_FAILED;\n+\t}\n+\n+\tvoid *map = V4L2CompatManager::instance()->fops().mmap(addr, length, prot,\n+\t\t\t\t\t\t\t       flags, fd.fd(), 0);\n+\tif (map == MAP_FAILED)\n+\t\treturn map;\n \n \tbuffers_[index].flags |= V4L2_BUF_FLAG_MAPPED;\n-\tmmaps_[val] = index;\n+\tmmaps_[map] = index;\n \n-\treturn val;\n+\treturn map;\n }\n \n int V4L2CameraProxy::munmap(void *addr, size_t length)\n@@ -110,6 +120,10 @@ int V4L2CameraProxy::munmap(void *addr, size_t length)\n \t\treturn -1;\n \t}\n \n+\tif (V4L2CompatManager::instance()->fops().munmap(addr, length))\n+\t\tLOG(V4L2Compat, Error) << \"Unmapping \" << addr\n+\t\t\t\t       << \" with length \" << length;\n+\n \tbuffers_[iter->second].flags &= ~V4L2_BUF_FLAG_MAPPED;\n \tmmaps_.erase(iter);\n \ndiff --git a/src/v4l2/v4l2_camera_proxy.h b/src/v4l2/v4l2_camera_proxy.h\nindex b59d19d707590d88..c8e61adf80f1b93b 100644\n--- a/src/v4l2/v4l2_camera_proxy.h\n+++ b/src/v4l2/v4l2_camera_proxy.h\n@@ -28,7 +28,7 @@ public:\n \tint open(bool nonBlocking);\n \tvoid dup();\n \tvoid close();\n-\tvoid *mmap(size_t length, int prot, int flags, off_t offset);\n+\tvoid *mmap(void *addr, size_t length, int prot, int flags, off_t offset);\n \tint munmap(void *addr, size_t length);\n \n \tint ioctl(unsigned long request, void *arg);\ndiff --git a/src/v4l2/v4l2_compat_manager.cpp b/src/v4l2/v4l2_compat_manager.cpp\nindex fe453445a9fb2cdd..f5a7b2ac4229d5d5 100644\n--- a/src/v4l2/v4l2_compat_manager.cpp\n+++ b/src/v4l2/v4l2_compat_manager.cpp\n@@ -223,7 +223,7 @@ void *V4L2CompatManager::mmap(void *addr, size_t length, int prot, int flags,\n \tif (!proxy)\n \t\treturn fops_.mmap(addr, length, prot, flags, fd, offset);\n \n-\tvoid *map = proxy->mmap(length, prot, flags, offset);\n+\tvoid *map = proxy->mmap(addr, length, prot, flags, offset);\n \tif (map == MAP_FAILED)\n \t\treturn map;\n \n","prefixes":["libcamera-devel","v4","05/32"]}