{"id":3271,"url":"https://patchwork.libcamera.org/api/1.1/patches/3271/?format=json","web_url":"https://patchwork.libcamera.org/patch/3271/","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":"<20200323173559.21109-6-laurent.pinchart@ideasonboard.com>","date":"2020-03-23T17:35:43","name":"[libcamera-devel,v2,05/21] qcam: main_window: Move capture event processing to main thread","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"6fdd4402ff63b26486bfcc83264a890b1878d8b3","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/3271/mbox/","series":[{"id":762,"url":"https://patchwork.libcamera.org/api/1.1/series/762/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=762","date":"2020-03-23T17:35:38","name":"qcam: Bypass format conversion when not required","version":2,"mbox":"https://patchwork.libcamera.org/series/762/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/3271/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/3271/checks/","tags":{},"headers":{"Return-Path":"<laurent.pinchart@ideasonboard.com>","Received":["from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 286AF62C75\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 23 Mar 2020 18:36:18 +0100 (CET)","from pendragon.bb.dnainternet.fi (81-175-216-236.bb.dnainternet.fi\n\t[81.175.216.236])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id BCB5AA31\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 23 Mar 2020 18:36:17 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1584984977;\n\tbh=VZ+Fdi4R/qcjgacHAMFeo4rZahK6A5/1bLp+7gEqKb0=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=XTPmQ7CwPZBPO69+M4Jw/9S21an3F6o8lXLmFRfxCfvnqi5xTFJtmNyM4PS1OcjCv\n\t7WFTg5N9aLJLn3WCPB9/gsKWYBCzxIK6vQUWxxPaBgFIxDaPkxhF8GIA4TteinHcrp\n\tvwIkWDsgDMur7wjqOeq7FjN2J4Fj5lvIWpSLaH6k=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","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","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 05/21] qcam: main_window: Move capture\n\tevent processing to main thread","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":"Mon, 23 Mar 2020 17:36:20 -0000"},"content":"To avoid blocking the camera manager for a long amount of time, move\ncapture event processing to the main thread. Captured buffers are added\nto a queue and an event is posted to the main window to signal\navailability of a buffer.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\nChanges since v1:\n\n- Add blank line\n---\n src/qcam/main_window.cpp | 55 +++++++++++++++++++++++++++++++++++++++-\n src/qcam/main_window.h   |  8 ++++++\n 2 files changed, 62 insertions(+), 1 deletion(-)","diff":"diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp\nindex 354a53367d0f..805690d5006a 100644\n--- a/src/qcam/main_window.cpp\n+++ b/src/qcam/main_window.cpp\n@@ -19,6 +19,7 @@\n #include <QImage>\n #include <QImageWriter>\n #include <QInputDialog>\n+#include <QMutexLocker>\n #include <QStandardPaths>\n #include <QTimer>\n #include <QToolBar>\n@@ -31,6 +32,21 @@\n \n using namespace libcamera;\n \n+class CaptureEvent : public QEvent\n+{\n+public:\n+\tCaptureEvent()\n+\t\t: QEvent(type())\n+\t{\n+\t}\n+\n+\tstatic Type type()\n+\t{\n+\t\tstatic int type = QEvent::registerEventType();\n+\t\treturn static_cast<Type>(type);\n+\t}\n+};\n+\n MainWindow::MainWindow(CameraManager *cm, const OptionsParser::Options &options)\n \t: options_(options), cm_(cm), allocator_(nullptr), isCapturing_(false)\n {\n@@ -63,6 +79,16 @@ MainWindow::~MainWindow()\n \t}\n }\n \n+bool MainWindow::event(QEvent *e)\n+{\n+\tif (e->type() == CaptureEvent::type()) {\n+\t\tprocessCapture();\n+\t\treturn true;\n+\t}\n+\n+\treturn QMainWindow::event(e);\n+}\n+\n int MainWindow::createToolbars()\n {\n \tQAction *action;\n@@ -343,6 +369,13 @@ void MainWindow::stopCapture()\n \n \tconfig_.reset();\n \n+\t/*\n+\t * A CaptureEvent may have been posted before we stopped the camera,\n+\t * but not processed yet. Clear the queue of done buffers to avoid\n+\t * racing with the event handler.\n+\t */\n+\tdoneQueue_.clear();\n+\n \ttitleTimer_.stop();\n \tsetWindowTitle(title_);\n }\n@@ -371,10 +404,30 @@ void MainWindow::requestComplete(Request *request)\n \t\treturn;\n \n \tconst std::map<Stream *, FrameBuffer *> &buffers = request->buffers();\n+\tFrameBuffer *buffer = buffers.begin()->second;\n+\n+\t{\n+\t\tQMutexLocker locker(&mutex_);\n+\t\tdoneQueue_.enqueue(buffer);\n+\t}\n+\n+\tQCoreApplication::postEvent(this, new CaptureEvent);\n+}\n+\n+void MainWindow::processCapture()\n+{\n+\tFrameBuffer *buffer;\n+\n+\t{\n+\t\tQMutexLocker locker(&mutex_);\n+\t\tif (doneQueue_.isEmpty())\n+\t\t\treturn;\n+\n+\t\tbuffer = doneQueue_.dequeue();\n+\t}\n \n \tframesCaptured_++;\n \n-\tFrameBuffer *buffer = buffers.begin()->second;\n \tconst FrameMetadata &metadata = buffer->metadata();\n \n \tdouble fps = metadata.timestamp - lastBufferTime_;\ndiff --git a/src/qcam/main_window.h b/src/qcam/main_window.h\nindex 720a3393e3dc..c623120d5894 100644\n--- a/src/qcam/main_window.h\n+++ b/src/qcam/main_window.h\n@@ -11,7 +11,9 @@\n \n #include <QElapsedTimer>\n #include <QMainWindow>\n+#include <QMutex>\n #include <QObject>\n+#include <QQueue>\n #include <QTimer>\n \n #include <libcamera/buffer.h>\n@@ -40,6 +42,8 @@ public:\n \tMainWindow(CameraManager *cm, const OptionsParser::Options &options);\n \t~MainWindow();\n \n+\tbool event(QEvent *e) override;\n+\n private Q_SLOTS:\n \tvoid quit();\n \tvoid updateTitle();\n@@ -57,6 +61,7 @@ private:\n \tint openCamera();\n \n \tvoid requestComplete(Request *request);\n+\tvoid processCapture();\n \tint display(FrameBuffer *buffer);\n \tvoid queueRequest(FrameBuffer *buffer);\n \n@@ -78,6 +83,9 @@ private:\n \tuint32_t previousFrames_;\n \tuint32_t framesCaptured_;\n \n+\tQMutex mutex_;\n+\tQQueue<FrameBuffer *> doneQueue_;\n+\n \tQToolBar *toolbar_;\n \tViewFinder *viewfinder_;\n \tstd::map<int, std::pair<void *, unsigned int>> mappedBuffers_;\n","prefixes":["libcamera-devel","v2","05/21"]}