From patchwork Sun Jan 12 01:01:45 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: 2587 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 73D1C606D0 for ; Sun, 12 Jan 2020 02:03:02 +0100 (CET) X-Halon-ID: 45f62063-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 45f62063-34d7-11ea-b6d8-005056917f90; Sun, 12 Jan 2020 02:02:58 +0100 (CET) From: =?utf-8?q?Niklas_S=C3=B6derlund?= 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 Subject: [libcamera-devel] [PATCH v4 05/32] v4l2: camera: Handle memory mapping of buffers directly 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:02 -0000 In the upcoming FrameBuffer API the memory mapping of buffers will be left to the user of the FrameBuffer objects. Prepare the V4L2 compatibility layer to this upcoming change to ease conversion to the new API. Signed-off-by: Niklas Söderlund Reviewed-by: Laurent Pinchart --- * Changes since v3 - Return FileDescriptor from getBufferFd() - Call fops().munmap() in V4L2CameraProxy::munmap() --- src/v4l2/v4l2_camera.cpp | 12 ++++++------ src/v4l2/v4l2_camera.h | 5 +++-- src/v4l2/v4l2_camera_proxy.cpp | 24 +++++++++++++++++++----- src/v4l2/v4l2_camera_proxy.h | 2 +- src/v4l2/v4l2_compat_manager.cpp | 2 +- 5 files changed, 30 insertions(+), 15 deletions(-) diff --git a/src/v4l2/v4l2_camera.cpp b/src/v4l2/v4l2_camera.cpp index 2a507b9bb8318025..07f05a31261c1b25 100644 --- a/src/v4l2/v4l2_camera.cpp +++ b/src/v4l2/v4l2_camera.cpp @@ -121,12 +121,6 @@ int V4L2Camera::configure(StreamConfiguration *streamConfigOut, return 0; } -void *V4L2Camera::mmap(unsigned int index) -{ - Stream *stream = *camera_->streams().begin(); - return stream->buffers()[index].planes()[0].mem(); -} - int V4L2Camera::allocBuffers(unsigned int count) { int ret = camera_->allocateBuffers(); @@ -138,6 +132,12 @@ void V4L2Camera::freeBuffers() camera_->freeBuffers(); } +FileDescriptor V4L2Camera::getBufferFd(unsigned int index) +{ + Stream *stream = *camera_->streams().begin(); + return FileDescriptor(stream->buffers()[index].planes()[0].dmabuf()); +} + int V4L2Camera::streamOn() { if (isRunning_) diff --git a/src/v4l2/v4l2_camera.h b/src/v4l2/v4l2_camera.h index 81f7908e5e8a6beb..f760316c854fba2f 100644 --- a/src/v4l2/v4l2_camera.h +++ b/src/v4l2/v4l2_camera.h @@ -14,6 +14,7 @@ #include #include +#include #include "semaphore.h" @@ -53,14 +54,14 @@ public: void getStreamConfig(StreamConfiguration *streamConfig); std::vector completedBuffers(); - void *mmap(unsigned int index); - int configure(StreamConfiguration *streamConfigOut, const Size &size, PixelFormat pixelformat, unsigned int bufferCount); int allocBuffers(unsigned int count); void freeBuffers(); + FileDescriptor getBufferFd(unsigned int index); + int streamOn(); int streamOff(); diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp index 52f8468cdaa06a7a..b4a9e2107c0f9f28 100644 --- a/src/v4l2/v4l2_camera_proxy.cpp +++ b/src/v4l2/v4l2_camera_proxy.cpp @@ -75,7 +75,8 @@ void V4L2CameraProxy::close() vcam_->invokeMethod(&V4L2Camera::close, ConnectionTypeBlocking); } -void *V4L2CameraProxy::mmap(size_t length, int prot, int flags, off_t offset) +void *V4L2CameraProxy::mmap(void *addr, size_t length, int prot, int flags, + off_t offset) { LOG(V4L2Compat, Debug) << "Servicing mmap"; @@ -91,13 +92,22 @@ void *V4L2CameraProxy::mmap(size_t length, int prot, int flags, off_t offset) return MAP_FAILED; } - void *val = vcam_->invokeMethod(&V4L2Camera::mmap, - ConnectionTypeBlocking, index); + FileDescriptor fd = vcam_->invokeMethod(&V4L2Camera::getBufferFd, + ConnectionTypeBlocking, index); + if (!fd.isValid()) { + errno = EINVAL; + return MAP_FAILED; + } + + void *map = V4L2CompatManager::instance()->fops().mmap(addr, length, prot, + flags, fd.fd(), 0); + if (map == MAP_FAILED) + return map; buffers_[index].flags |= V4L2_BUF_FLAG_MAPPED; - mmaps_[val] = index; + mmaps_[map] = index; - return val; + return map; } int V4L2CameraProxy::munmap(void *addr, size_t length) @@ -110,6 +120,10 @@ int V4L2CameraProxy::munmap(void *addr, size_t length) return -1; } + if (V4L2CompatManager::instance()->fops().munmap(addr, length)) + LOG(V4L2Compat, Error) << "Unmapping " << addr + << " with length " << length; + buffers_[iter->second].flags &= ~V4L2_BUF_FLAG_MAPPED; mmaps_.erase(iter); diff --git a/src/v4l2/v4l2_camera_proxy.h b/src/v4l2/v4l2_camera_proxy.h index b59d19d707590d88..c8e61adf80f1b93b 100644 --- a/src/v4l2/v4l2_camera_proxy.h +++ b/src/v4l2/v4l2_camera_proxy.h @@ -28,7 +28,7 @@ public: int open(bool nonBlocking); void dup(); void close(); - void *mmap(size_t length, int prot, int flags, off_t offset); + void *mmap(void *addr, size_t length, int prot, int flags, off_t offset); int munmap(void *addr, size_t length); int ioctl(unsigned long request, void *arg); diff --git a/src/v4l2/v4l2_compat_manager.cpp b/src/v4l2/v4l2_compat_manager.cpp index fe453445a9fb2cdd..f5a7b2ac4229d5d5 100644 --- a/src/v4l2/v4l2_compat_manager.cpp +++ b/src/v4l2/v4l2_compat_manager.cpp @@ -223,7 +223,7 @@ void *V4L2CompatManager::mmap(void *addr, size_t length, int prot, int flags, if (!proxy) return fops_.mmap(addr, length, prot, flags, fd, offset); - void *map = proxy->mmap(length, prot, flags, offset); + void *map = proxy->mmap(addr, length, prot, flags, offset); if (map == MAP_FAILED) return map;