{"id":3277,"url":"https://patchwork.libcamera.org/api/patches/3277/?format=json","web_url":"https://patchwork.libcamera.org/patch/3277/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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-11-laurent.pinchart@ideasonboard.com>","date":"2020-03-23T17:35:48","name":"[libcamera-devel,v2,10/21] qcam: main_window: Document functions and reorganize member data","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"f4389cbba520900f1536f67b656acfbf68601c4c","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/3277/mbox/","series":[{"id":762,"url":"https://patchwork.libcamera.org/api/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/3277/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/3277/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 CB31562CA6\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 23 Mar 2020 18:36:19 +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 6A95B308\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 23 Mar 2020 18:36:19 +0100 (CET)"],"DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1584984979;\n\tbh=BujrZLoKPy/+bE7zekAjMLPsYYYJ00vMwXL5VFlLGSI=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=K9nK1mTHyLZC5Hev3cta6vpqxgPNCW2LIptLIFu8v4eF2ijIyc0NPsQfVwDw+SBni\n\tgrP4LVrrHC3tg1u3p5aSAQXGbOQfeEsV7jXuFFbBv9fG/okI/w4OD4Vzfdoz3R0QPE\n\tYPjSgFieE0r4zO63cDGJdnGtqcdopZYfR0UGQZng=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Mon, 23 Mar 2020 19:35:48 +0200","Message-Id":"<20200323173559.21109-11-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 10/21] qcam: main_window: Document\n\tfunctions and reorganize member data","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:23 -0000"},"content":"The qcam application is our reference implementation of a libcamera GUI\napplication. Document the code of the MainWindow class to make it easier\nto follow, and reorganize the member data in logical groups for clarity.\nNo code change.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n---\n src/qcam/main_window.cpp | 69 +++++++++++++++++++++++++++++++++++++++-\n src/qcam/main_window.h   | 24 ++++++++------\n 2 files changed, 82 insertions(+), 11 deletions(-)","diff":"diff --git a/src/qcam/main_window.cpp b/src/qcam/main_window.cpp\nindex 7959c17cd610..20e84171e4a5 100644\n--- a/src/qcam/main_window.cpp\n+++ b/src/qcam/main_window.cpp\n@@ -31,6 +31,9 @@\n \n using namespace libcamera;\n \n+/**\n+ * \\brief Custom QEvent to signal capture completion\n+ */\n class CaptureEvent : public QEvent\n {\n public:\n@@ -51,6 +54,10 @@ MainWindow::MainWindow(CameraManager *cm, const OptionsParser::Options &options)\n {\n \tint ret;\n \n+\t/*\n+\t * Initialize the UI: Create the toolbar, set the window title and\n+\t * create the viewfinder widget.\n+\t */\n \tcreateToolbars();\n \n \ttitle_ = \"QCam \" + QString::fromStdString(CameraManager::version());\n@@ -61,6 +68,7 @@ MainWindow::MainWindow(CameraManager *cm, const OptionsParser::Options &options)\n \tsetCentralWidget(viewfinder_);\n \tadjustSize();\n \n+\t/* Open the camera and start capture. */\n \tret = openCamera();\n \tif (ret < 0)\n \t\tquit();\n@@ -96,13 +104,14 @@ int MainWindow::createToolbars()\n \t/* Disable right click context menu. */\n \ttoolbar_->setContextMenuPolicy(Qt::PreventContextMenu);\n \n+\t/* Quit action. */\n \taction = toolbar_->addAction(QIcon::fromTheme(\"application-exit\",\n \t\t\t\t\t\t      QIcon(\":x-circle.svg\")),\n \t\t\t\t     \"Quit\");\n \taction->setShortcut(Qt::CTRL | Qt::Key_Q);\n \tconnect(action, &QAction::triggered, this, &MainWindow::quit);\n \n-\t/* Camera selection. */\n+\t/* Camera selector. */\n \tQComboBox *cameraCombo = new QComboBox();\n \tconnect(cameraCombo, QOverload<int>::of(&QComboBox::activated),\n \t\tthis, &MainWindow::switchCamera);\n@@ -114,6 +123,7 @@ int MainWindow::createToolbars()\n \n \ttoolbar_->addSeparator();\n \n+\t/* Start/Stop action. */\n \ticonPlay_ = QIcon::fromTheme(\"media-playback-start\",\n \t\t\t\t     QIcon(\":play-circle.svg\"));\n \ticonStop_ = QIcon::fromTheme(\"media-playback-stop\",\n@@ -125,6 +135,7 @@ int MainWindow::createToolbars()\n \tconnect(action, &QAction::toggled, this, &MainWindow::toggleCapture);\n \tstartStopAction_ = action;\n \n+\t/* Save As... action. */\n \taction = toolbar_->addAction(QIcon::fromTheme(\"document-save-as\",\n \t\t\t\t\t\t      QIcon(\":save.svg\")),\n \t\t\t\t     \"Save As...\");\n@@ -142,6 +153,7 @@ void MainWindow::quit()\n \n void MainWindow::updateTitle()\n {\n+\t/* Calculate the average frame rate over the last period. */\n \tunsigned int duration = frameRateInterval_.elapsed();\n \tunsigned int frames = framesCaptured_ - previousFrames_;\n \tdouble fps = frames * 1000.0 / duration;\n@@ -153,8 +165,13 @@ void MainWindow::updateTitle()\n \tsetWindowTitle(title_ + \" : \" + QString::number(fps, 'f', 2) + \" fps\");\n }\n \n+/* -----------------------------------------------------------------------------\n+ * Camera Selection\n+ */\n+\n void MainWindow::switchCamera(int index)\n {\n+\t/* Get and acquire the new camera. */\n \tconst auto &cameras = cm_->cameras();\n \tif (static_cast<unsigned int>(index) >= cameras.size())\n \t\treturn;\n@@ -168,6 +185,10 @@ void MainWindow::switchCamera(int index)\n \n \tstd::cout << \"Switching to camera \" << cam->name() << std::endl;\n \n+\t/*\n+\t * Stop the capture session, release the current camera, replace it with\n+\t * the new camera and start a new capture session.\n+\t */\n \tstartStopAction_->setChecked(false);\n \n \tcamera_->release();\n@@ -181,9 +202,11 @@ std::string MainWindow::chooseCamera()\n \tQStringList cameras;\n \tbool result;\n \n+\t/* If only one camera is available, use it automatically. */\n \tif (cm_->cameras().size() == 1)\n \t\treturn cm_->cameras()[0]->name();\n \n+\t/* Present a dialog box to pick a camera. */\n \tfor (const std::shared_ptr<Camera> &cam : cm_->cameras())\n \t\tcameras.append(QString::fromStdString(cam->name()));\n \n@@ -200,6 +223,10 @@ int MainWindow::openCamera()\n {\n \tstd::string cameraName;\n \n+\t/*\n+\t * Use the camera specified on the command line, if any, or display the\n+\t * camera selection dialog box otherwise.\n+\t */\n \tif (options_.isSet(OptCamera))\n \t\tcameraName = static_cast<std::string>(options_[OptCamera]);\n \telse\n@@ -208,6 +235,7 @@ int MainWindow::openCamera()\n \tif (cameraName == \"\")\n \t\treturn -EINVAL;\n \n+\t/* Get and acquire the camera. */\n \tcamera_ = cm_->get(cameraName);\n \tif (!camera_) {\n \t\tstd::cout << \"Camera \" << cameraName << \" not found\"\n@@ -226,6 +254,10 @@ int MainWindow::openCamera()\n \treturn 0;\n }\n \n+/* -----------------------------------------------------------------------------\n+ * Capture Start & Stop\n+ */\n+\n void MainWindow::toggleCapture(bool start)\n {\n \tif (start) {\n@@ -239,10 +271,16 @@ void MainWindow::toggleCapture(bool start)\n \t}\n }\n \n+/**\n+ * \\brief Start capture with the current camera\n+ *\n+ * This function shall not be called directly, use toggleCapture() instead.\n+ */\n int MainWindow::startCapture()\n {\n \tint ret;\n \n+\t/* Configure the camera. */\n \tconfig_ = camera_->generateConfiguration({ StreamRole::Viewfinder });\n \n \tStreamConfiguration &cfg = config_->at(0);\n@@ -279,6 +317,7 @@ int MainWindow::startCapture()\n \t\treturn ret;\n \t}\n \n+\t/* Configure the viewfinder. */\n \tStream *stream = cfg.stream();\n \tret = viewfinder_->setFormat(cfg.pixelFormat,\n \t\t\t\t     QSize(cfg.size.width, cfg.size.height));\n@@ -289,6 +328,7 @@ int MainWindow::startCapture()\n \n \tadjustSize();\n \n+\t/* Allocate buffers and requests. */\n \tallocator_ = new FrameBufferAllocator(camera_);\n \tret = allocator_->allocate(stream);\n \tif (ret < 0) {\n@@ -321,6 +361,7 @@ int MainWindow::startCapture()\n \t\t\tstd::make_pair(memory, plane.length);\n \t}\n \n+\t/* Start the title timer and the camera. */\n \ttitleTimer_.start(2000);\n \tframeRateInterval_.start();\n \tpreviousFrames_ = 0;\n@@ -335,6 +376,7 @@ int MainWindow::startCapture()\n \n \tcamera_->requestCompleted.connect(this, &MainWindow::requestComplete);\n \n+\t/* Queue all requests. */\n \tfor (Request *request : requests) {\n \t\tret = camera_->queueRequest(request);\n \t\tif (ret < 0) {\n@@ -368,6 +410,12 @@ error:\n \treturn ret;\n }\n \n+/**\n+ * \\brief Stop ongoing capture\n+ *\n+ * This function may be called directly when tearing down the MainWindow. Use\n+ * toggleCapture() instead in all other cases.\n+ */\n void MainWindow::stopCapture()\n {\n \tif (!isCapturing_)\n@@ -403,6 +451,10 @@ void MainWindow::stopCapture()\n \tsetWindowTitle(title_);\n }\n \n+/* -----------------------------------------------------------------------------\n+ * Image Save\n+ */\n+\n void MainWindow::saveImageAs()\n {\n \tQImage image = viewfinder_->getCurrentImage();\n@@ -418,11 +470,20 @@ void MainWindow::saveImageAs()\n \twriter.write(image);\n }\n \n+/* -----------------------------------------------------------------------------\n+ * Request Completion Handling\n+ */\n+\n void MainWindow::requestComplete(Request *request)\n {\n \tif (request->status() == Request::RequestCancelled)\n \t\treturn;\n \n+\t/*\n+\t * We're running in the libcamera thread context, expensive operations\n+\t * are not allowed. Add the buffer to the done queue and post a\n+\t * CaptureEvent for the application thread to handle.\n+\t */\n \tconst std::map<Stream *, FrameBuffer *> &buffers = request->buffers();\n \tFrameBuffer *buffer = buffers.begin()->second;\n \n@@ -436,6 +497,11 @@ void MainWindow::requestComplete(Request *request)\n \n void MainWindow::processCapture()\n {\n+\t/*\n+\t * Retrieve the next buffer from the done queue. The queue may be empty\n+\t * if stopCapture() has been called while a CaptureEvent was posted but\n+\t * not processed yet. Return immediately in that case.\n+\t */\n \tFrameBuffer *buffer;\n \n \t{\n@@ -460,6 +526,7 @@ void MainWindow::processCapture()\n \t\t  << \" fps: \" << std::fixed << std::setprecision(2) << fps\n \t\t  << std::endl;\n \n+\t/* Display the buffer and requeue it to the camera. */\n \tdisplay(buffer);\n \n \tqueueRequest(buffer);\ndiff --git a/src/qcam/main_window.h b/src/qcam/main_window.h\nindex 34a090cc463e..c662a2ef3bcf 100644\n--- a/src/qcam/main_window.h\n+++ b/src/qcam/main_window.h\n@@ -57,6 +57,7 @@ private Q_SLOTS:\n \n private:\n \tint createToolbars();\n+\n \tstd::string chooseCamera();\n \tint openCamera();\n \n@@ -68,34 +69,37 @@ private:\n \tint display(FrameBuffer *buffer);\n \tvoid queueRequest(FrameBuffer *buffer);\n \n+\t/* UI elements */\n+\tQToolBar *toolbar_;\n+\tQAction *startStopAction_;\n+\tViewFinder *viewfinder_;\n+\n \tQIcon iconPlay_;\n \tQIcon iconStop_;\n \n \tQString title_;\n \tQTimer titleTimer_;\n \n+\t/* Options */\n \tconst OptionsParser::Options &options_;\n \n+\t/* Camera manager, camera, configuration and buffers */\n \tCameraManager *cm_;\n \tstd::shared_ptr<Camera> camera_;\n \tFrameBufferAllocator *allocator_;\n \n-\tbool isCapturing_;\n \tstd::unique_ptr<CameraConfiguration> config_;\n+\tstd::map<int, std::pair<void *, unsigned int>> mappedBuffers_;\n \n-\tuint64_t lastBufferTime_;\n+\t/* Capture state, buffers queue and statistics */\n+\tbool isCapturing_;\n+\tQQueue<FrameBuffer *> doneQueue_;\n+\tQMutex mutex_;\t/* Protects doneQueue_ */\n \n+\tuint64_t lastBufferTime_;\n \tQElapsedTimer frameRateInterval_;\n \tuint32_t previousFrames_;\n \tuint32_t framesCaptured_;\n-\n-\tQMutex mutex_;\n-\tQQueue<FrameBuffer *> doneQueue_;\n-\n-\tQToolBar *toolbar_;\n-\tQAction *startStopAction_;\n-\tViewFinder *viewfinder_;\n-\tstd::map<int, std::pair<void *, unsigned int>> mappedBuffers_;\n };\n \n #endif /* __QCAM_MAIN_WINDOW__ */\n","prefixes":["libcamera-devel","v2","10/21"]}