From patchwork Mon Mar 23 17:35:43 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 3271 Return-Path: Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [213.167.242.64]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 286AF62C75 for ; Mon, 23 Mar 2020 18:36:18 +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 BCB5AA31 for ; Mon, 23 Mar 2020 18:36:17 +0100 (CET) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1584984977; bh=VZ+Fdi4R/qcjgacHAMFeo4rZahK6A5/1bLp+7gEqKb0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=XTPmQ7CwPZBPO69+M4Jw/9S21an3F6o8lXLmFRfxCfvnqi5xTFJtmNyM4PS1OcjCv 7WFTg5N9aLJLn3WCPB9/gsKWYBCzxIK6vQUWxxPaBgFIxDaPkxhF8GIA4TteinHcrp vwIkWDsgDMur7wjqOeq7FjN2J4Fj5lvIWpSLaH6k= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Mon, 23 Mar 2020 19:35:43 +0200 Message-Id: <20200323173559.21109-6-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200323173559.21109-1-laurent.pinchart@ideasonboard.com> References: <20200323173559.21109-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH v2 05/21] qcam: main_window: Move capture event processing to main 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: Mon, 23 Mar 2020 17:36:20 -0000 To avoid blocking the camera manager for a long amount of time, move capture event processing to the main thread. Captured buffers are added to a queue and an event is posted to the main window to signal availability of a buffer. Signed-off-by: Laurent Pinchart Reviewed-by: Kieran Bingham --- Changes since v1: - Add blank line --- src/qcam/main_window.cpp | 55 +++++++++++++++++++++++++++++++++++++++- src/qcam/main_window.h | 8 ++++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp index 354a53367d0f..805690d5006a 100644 --- a/src/qcam/main_window.cpp +++ b/src/qcam/main_window.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -31,6 +32,21 @@ using namespace libcamera; +class CaptureEvent : public QEvent +{ +public: + CaptureEvent() + : QEvent(type()) + { + } + + static Type type() + { + static int type = QEvent::registerEventType(); + return static_cast(type); + } +}; + MainWindow::MainWindow(CameraManager *cm, const OptionsParser::Options &options) : options_(options), cm_(cm), allocator_(nullptr), isCapturing_(false) { @@ -63,6 +79,16 @@ MainWindow::~MainWindow() } } +bool MainWindow::event(QEvent *e) +{ + if (e->type() == CaptureEvent::type()) { + processCapture(); + return true; + } + + return QMainWindow::event(e); +} + int MainWindow::createToolbars() { QAction *action; @@ -343,6 +369,13 @@ void MainWindow::stopCapture() config_.reset(); + /* + * A CaptureEvent may have been posted before we stopped the camera, + * but not processed yet. Clear the queue of done buffers to avoid + * racing with the event handler. + */ + doneQueue_.clear(); + titleTimer_.stop(); setWindowTitle(title_); } @@ -371,10 +404,30 @@ void MainWindow::requestComplete(Request *request) return; const std::map &buffers = request->buffers(); + FrameBuffer *buffer = buffers.begin()->second; + + { + QMutexLocker locker(&mutex_); + doneQueue_.enqueue(buffer); + } + + QCoreApplication::postEvent(this, new CaptureEvent); +} + +void MainWindow::processCapture() +{ + FrameBuffer *buffer; + + { + QMutexLocker locker(&mutex_); + if (doneQueue_.isEmpty()) + return; + + buffer = doneQueue_.dequeue(); + } framesCaptured_++; - FrameBuffer *buffer = buffers.begin()->second; const FrameMetadata &metadata = buffer->metadata(); double fps = metadata.timestamp - lastBufferTime_; diff --git a/src/qcam/main_window.h b/src/qcam/main_window.h index 720a3393e3dc..c623120d5894 100644 --- a/src/qcam/main_window.h +++ b/src/qcam/main_window.h @@ -11,7 +11,9 @@ #include #include +#include #include +#include #include #include @@ -40,6 +42,8 @@ public: MainWindow(CameraManager *cm, const OptionsParser::Options &options); ~MainWindow(); + bool event(QEvent *e) override; + private Q_SLOTS: void quit(); void updateTitle(); @@ -57,6 +61,7 @@ private: int openCamera(); void requestComplete(Request *request); + void processCapture(); int display(FrameBuffer *buffer); void queueRequest(FrameBuffer *buffer); @@ -78,6 +83,9 @@ private: uint32_t previousFrames_; uint32_t framesCaptured_; + QMutex mutex_; + QQueue doneQueue_; + QToolBar *toolbar_; ViewFinder *viewfinder_; std::map> mappedBuffers_;