From patchwork Wed Jan 22 20:57:22 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 2728 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id B306660893 for ; Wed, 22 Jan 2020 21:57:46 +0100 (CET) Received: from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi [81.175.216.236]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 5DF112E5 for ; Wed, 22 Jan 2020 21:57:46 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1579726666; bh=WfJvYv+m/UaA5uvzwJC01dn3Xw6cGJUCVJKyRUB+0hc=; h=From:To:Subject:Date:In-Reply-To:References:From; b=crQ2EdAllS0rIteht4RVYaJ8uWvjdAOd+NDKpMEo557OoVA4q57z0lxFCyNMo0VZt BDPEp6hJJRBQzqv/PVDKbSHO+md5NdEv9pyhArtNEDPD+cA9f0aNj+Aj6WRv4b3xuc cbBOkqQ5npOPtgX+6P9fp5dPToZreSkE+PVPicWs= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Wed, 22 Jan 2020 22:57:22 +0200 Message-Id: <20200122205723.8865-13-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200122205723.8865-1-laurent.pinchart@ideasonboard.com> References: <20200122205723.8865-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 12/13] v4l2: Remove internal thread 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: Wed, 22 Jan 2020 20:57:49 -0000 Now that libcamera creates threads internally and doesn't rely on an application-provided event loop, remove the thread from the V4L2 compatibility layer. The split between the V4L2CameraProxy and V4L2Camera classes is still kept to separate the V4L2 adaptation from camera operation. This may be further refactored later. Signed-off-by: Laurent Pinchart Reviewed-by: Niklas Söderlund --- src/v4l2/v4l2_camera.h | 2 +- src/v4l2/v4l2_camera_proxy.cpp | 43 ++++++++++------------------ src/v4l2/v4l2_compat_manager.cpp | 48 +++++++++----------------------- src/v4l2/v4l2_compat_manager.h | 13 ++------- 4 files changed, 31 insertions(+), 75 deletions(-) diff --git a/src/v4l2/v4l2_camera.h b/src/v4l2/v4l2_camera.h index f1f04d9ef6ed..37bd358462db 100644 --- a/src/v4l2/v4l2_camera.h +++ b/src/v4l2/v4l2_camera.h @@ -21,7 +21,7 @@ using namespace libcamera; -class V4L2Camera : public Object +class V4L2Camera { public: struct Buffer { diff --git a/src/v4l2/v4l2_camera_proxy.cpp b/src/v4l2/v4l2_camera_proxy.cpp index e58fd6a0d8b5..622520479be0 100644 --- a/src/v4l2/v4l2_camera_proxy.cpp +++ b/src/v4l2/v4l2_camera_proxy.cpp @@ -41,8 +41,7 @@ int V4L2CameraProxy::open(bool nonBlocking) { LOG(V4L2Compat, Debug) << "Servicing open"; - int ret = vcam_->invokeMethod(&V4L2Camera::open, - ConnectionTypeBlocking); + int ret = vcam_->open(); if (ret < 0) { errno = -ret; return -1; @@ -50,8 +49,7 @@ int V4L2CameraProxy::open(bool nonBlocking) nonBlocking_ = nonBlocking; - vcam_->invokeMethod(&V4L2Camera::getStreamConfig, - ConnectionTypeBlocking, &streamConfig_); + vcam_->getStreamConfig(&streamConfig_); setFmtFromConfig(streamConfig_); sizeimage_ = calculateSizeImage(streamConfig_); @@ -72,7 +70,7 @@ void V4L2CameraProxy::close() if (--refcount_ > 0) return; - vcam_->invokeMethod(&V4L2Camera::close, ConnectionTypeBlocking); + vcam_->close(); } void *V4L2CameraProxy::mmap(void *addr, size_t length, int prot, int flags, @@ -284,11 +282,9 @@ int V4L2CameraProxy::vidioc_s_fmt(struct v4l2_format *arg) tryFormat(arg); Size size(arg->fmt.pix.width, arg->fmt.pix.height); - int ret = vcam_->invokeMethod(&V4L2Camera::configure, - ConnectionTypeBlocking, - &streamConfig_, size, - v4l2ToDrm(arg->fmt.pix.pixelformat), - bufferCount_); + int ret = vcam_->configure(&streamConfig_, size, + v4l2ToDrm(arg->fmt.pix.pixelformat), + bufferCount_); if (ret < 0) return -EINVAL; @@ -319,13 +315,12 @@ int V4L2CameraProxy::freeBuffers() { LOG(V4L2Compat, Debug) << "Freeing libcamera bufs"; - int ret = vcam_->invokeMethod(&V4L2Camera::streamOff, - ConnectionTypeBlocking); + int ret = vcam_->streamOff(); if (ret < 0) { LOG(V4L2Compat, Error) << "Failed to stop stream"; return ret; } - vcam_->invokeMethod(&V4L2Camera::freeBuffers, ConnectionTypeBlocking); + vcam_->freeBuffers(); bufferCount_ = 0; return 0; @@ -349,11 +344,9 @@ int V4L2CameraProxy::vidioc_reqbufs(struct v4l2_requestbuffers *arg) return freeBuffers(); Size size(curV4L2Format_.fmt.pix.width, curV4L2Format_.fmt.pix.height); - ret = vcam_->invokeMethod(&V4L2Camera::configure, - ConnectionTypeBlocking, - &streamConfig_, size, - v4l2ToDrm(curV4L2Format_.fmt.pix.pixelformat), - arg->count); + ret = vcam_->configure(&streamConfig_, size, + v4l2ToDrm(curV4L2Format_.fmt.pix.pixelformat), + arg->count); if (ret < 0) return -EINVAL; @@ -366,8 +359,7 @@ int V4L2CameraProxy::vidioc_reqbufs(struct v4l2_requestbuffers *arg) arg->count = streamConfig_.bufferCount; bufferCount_ = arg->count; - ret = vcam_->invokeMethod(&V4L2Camera::allocBuffers, - ConnectionTypeBlocking, arg->count); + ret = vcam_->allocBuffers(arg->count); if (ret < 0) { arg->count = 0; return ret; @@ -415,8 +407,7 @@ int V4L2CameraProxy::vidioc_qbuf(struct v4l2_buffer *arg) arg->index >= bufferCount_) return -EINVAL; - int ret = vcam_->invokeMethod(&V4L2Camera::qbuf, ConnectionTypeBlocking, - arg->index); + int ret = vcam_->qbuf(arg->index); if (ret < 0) return ret; @@ -459,10 +450,7 @@ int V4L2CameraProxy::vidioc_streamon(int *arg) if (!validateBufferType(*arg)) return -EINVAL; - int ret = vcam_->invokeMethod(&V4L2Camera::streamOn, - ConnectionTypeBlocking); - - return ret; + return vcam_->streamOn(); } int V4L2CameraProxy::vidioc_streamoff(int *arg) @@ -472,8 +460,7 @@ int V4L2CameraProxy::vidioc_streamoff(int *arg) if (!validateBufferType(*arg)) return -EINVAL; - int ret = vcam_->invokeMethod(&V4L2Camera::streamOff, - ConnectionTypeBlocking); + int ret = vcam_->streamOff(); for (struct v4l2_buffer &buf : buffers_) buf.flags &= ~(V4L2_BUF_FLAG_QUEUED | V4L2_BUF_FLAG_DONE); diff --git a/src/v4l2/v4l2_compat_manager.cpp b/src/v4l2/v4l2_compat_manager.cpp index f5a7b2ac4229..961d06b3e39a 100644 --- a/src/v4l2/v4l2_compat_manager.cpp +++ b/src/v4l2/v4l2_compat_manager.cpp @@ -37,7 +37,7 @@ void get_symbol(T &func, const char *name) } /* namespace */ V4L2CompatManager::V4L2CompatManager() - : cm_(nullptr), initialized_(false) + : cm_(nullptr) { get_symbol(fops_.openat, "openat"); get_symbol(fops_.dup, "dup"); @@ -52,24 +52,15 @@ V4L2CompatManager::~V4L2CompatManager() devices_.clear(); mmaps_.clear(); - if (isRunning()) { - exit(0); - /* \todo Wait with a timeout, just in case. */ - wait(); + if (cm_) { + proxies_.clear(); + cm_->stop(); + delete cm_; + cm_ = nullptr; } } -int V4L2CompatManager::init() -{ - start(); - - MutexLocker locker(mutex_); - cv_.wait(locker, [&] { return initialized_; }); - - return 0; -} - -void V4L2CompatManager::run() +int V4L2CompatManager::start() { cm_ = new CameraManager(); @@ -77,7 +68,9 @@ void V4L2CompatManager::run() if (ret) { LOG(V4L2Compat, Error) << "Failed to start camera manager: " << strerror(-ret); - return; + delete cm_; + cm_ = nullptr; + return ret; } LOG(V4L2Compat, Debug) << "Started camera manager"; @@ -93,22 +86,7 @@ void V4L2CompatManager::run() ++index; } - /* - * libcamera has been initialized. Unlock the init() caller as we're - * now ready to handle calls from the framework. - */ - mutex_.lock(); - initialized_ = true; - mutex_.unlock(); - cv_.notify_one(); - - /* Now start processing events and messages. */ - exec(); - - proxies_.clear(); - cm_->stop(); - delete cm_; - cm_ = nullptr; + return 0; } V4L2CompatManager *V4L2CompatManager::instance() @@ -159,8 +137,8 @@ int V4L2CompatManager::openat(int dirfd, const char *path, int oflag, mode_t mod major(statbuf.st_rdev) != 81) return fd; - if (!isRunning()) - init(); + if (!cm_) + start(); ret = getCameraIndex(fd); if (ret < 0) { diff --git a/src/v4l2/v4l2_compat_manager.h b/src/v4l2/v4l2_compat_manager.h index d51b5953d930..872c7c3b10e8 100644 --- a/src/v4l2/v4l2_compat_manager.h +++ b/src/v4l2/v4l2_compat_manager.h @@ -8,22 +8,19 @@ #ifndef __V4L2_COMPAT_MANAGER_H__ #define __V4L2_COMPAT_MANAGER_H__ -#include #include #include #include -#include #include #include #include -#include "thread.h" #include "v4l2_camera_proxy.h" using namespace libcamera; -class V4L2CompatManager : public Thread +class V4L2CompatManager { public: struct FileOperations { @@ -46,8 +43,6 @@ public: static V4L2CompatManager *instance(); - int init(); - V4L2CameraProxy *getProxy(int fd); const FileOperations &fops() const { return fops_; } @@ -64,17 +59,13 @@ private: V4L2CompatManager(); ~V4L2CompatManager(); - void run() override; + int start(); int getCameraIndex(int fd); FileOperations fops_; CameraManager *cm_; - std::mutex mutex_; - std::condition_variable cv_; - bool initialized_; - std::vector> proxies_; std::map devices_; std::map mmaps_;