From 7c5376bb1f6b8b9191ca1453ea93a0a87cde972f Mon Sep 17 00:00:00 2001 From: Stephen Birarda Date: Fri, 14 Apr 2017 13:06:12 -0700 Subject: [PATCH] put fbx bakers on their own thread from ModelBakeWidget --- tools/oven/src/ui/ModelBakeWidget.cpp | 37 +++++++++++++++++++-------- tools/oven/src/ui/ModelBakeWidget.h | 4 +++ 2 files changed, 31 insertions(+), 10 deletions(-) diff --git a/tools/oven/src/ui/ModelBakeWidget.cpp b/tools/oven/src/ui/ModelBakeWidget.cpp index 5f31ed2673..3b28bb77fa 100644 --- a/tools/oven/src/ui/ModelBakeWidget.cpp +++ b/tools/oven/src/ui/ModelBakeWidget.cpp @@ -18,6 +18,7 @@ #include #include +#include #include "ModelBakeWidget.h" @@ -27,11 +28,18 @@ static const QString MODEL_START_DIR_SETTING_KEY = "model_search_directory"; ModelBakeWidget::ModelBakeWidget(QWidget* parent, Qt::WindowFlags flags) : QWidget(parent, flags), _exportDirectory(EXPORT_DIR_SETTING_KEY), - _modelStartDirectory(MODEL_START_DIR_SETTING_KEY) + _modelStartDirectory(MODEL_START_DIR_SETTING_KEY), + _bakerThread(new QThread(this)) { setupUI(); } +ModelBakeWidget::~ModelBakeWidget() { + // before we go down, stop the baker thread and make sure it's done + _bakerThread->quit(); + _bakerThread->wait(); +} + void ModelBakeWidget::setupUI() { // setup a grid layout to hold everything QGridLayout* gridLayout = new QGridLayout; @@ -115,13 +123,12 @@ void ModelBakeWidget::chooseFileButtonClicked() { // set the contents of the model file text box to be the path to the selected file _modelLineEdit->setText(selectedFiles.join(',')); - auto directoryOfModel = QFileInfo(selectedFiles[0]).absolutePath(); + if (_outputDirLineEdit->text().isEmpty()) { + auto directoryOfModel = QFileInfo(selectedFiles[0]).absolutePath(); - // save the directory containing this model so we can default to it next time we show the file dialog - _modelStartDirectory.set(directoryOfModel); - - // if our output directory is not yet set, set it to the directory of this model - _outputDirLineEdit->setText(directoryOfModel); + // if our output directory is not yet set, set it to the directory of this model + _outputDirLineEdit->setText(directoryOfModel); + } } } @@ -153,13 +160,11 @@ void ModelBakeWidget::bakeButtonClicked() { QDir outputDirectory(_outputDirLineEdit->text()); if (!outputDirectory.exists()) { - return; } // make sure we have a non empty URL to a model to bake if (_modelLineEdit->text().isEmpty()) { - return; } @@ -176,7 +181,19 @@ void ModelBakeWidget::bakeButtonClicked() { // everything seems to be in place, kick off a bake for this model now auto baker = new FBXBaker(modelToBakeURL, outputDirectory.absolutePath(), false); - baker->bake(); + + // move the baker to the baker thread + baker->moveToThread(_bakerThread); + + // make sure we start the baker thread if it isn't already running + if (!_bakerThread->isRunning()) { + _bakerThread->start(); + } + + // invoke the bake method on the baker thread + QMetaObject::invokeMethod(baker, "bake"); + + // keep a unique_ptr to this baker _bakers.emplace_back(baker); } } diff --git a/tools/oven/src/ui/ModelBakeWidget.h b/tools/oven/src/ui/ModelBakeWidget.h index 354ad9f311..f277d91938 100644 --- a/tools/oven/src/ui/ModelBakeWidget.h +++ b/tools/oven/src/ui/ModelBakeWidget.h @@ -19,12 +19,14 @@ #include class QLineEdit; +class QThread; class ModelBakeWidget : public QWidget { Q_OBJECT public: ModelBakeWidget(QWidget* parent = nullptr, Qt::WindowFlags flags = Qt::WindowFlags()); + ~ModelBakeWidget(); private slots: void chooseFileButtonClicked(); @@ -44,6 +46,8 @@ private: Setting::Handle _exportDirectory; Setting::Handle _modelStartDirectory; + + QThread* _bakerThread; }; #endif // hifi_ModelBakeWidget_h