renderer: implemented function to wait for workers in the main thread

The workers run in separate threads meaning if any significant change is made
(e.g. parameters changing, or even closing the program), these workers may still
be mid-render. This is particularly problematic when closing since the nodes a
worker is rendering may be deleted mid-render. Render backends now have a
function that pauses the main thread (but starts a second event loop so the UI
isn't frozen) until the worker threads are all finished. This way, massive
changes can be made safely without race conditions.
This commit is contained in:
itsmattkc
2020-01-22 17:11:17 +11:00
parent fdbaa75dff
commit 24f7eeb210
11 changed files with 197 additions and 20 deletions
+10 -5
View File
@@ -39,7 +39,10 @@ bool OpenGLBackend::InitInternal()
// Create master texture (the one sent to the viewer)
master_texture_ = std::make_shared<OpenGLTexture>();
master_texture_->Create(share_ctx, params().effective_width(), params().effective_height(), params().format());
master_texture_->Create(share_ctx,
params().effective_width(),
params().effective_height(),
params().format());
// Create copy buffer/pipeline
copy_buffer_.Create(share_ctx);
@@ -156,11 +159,13 @@ void OpenGLBackend::EmitCachedFrameReady(const rational &time, const QVariant &v
void OpenGLBackend::ParamsChangedEvent()
{
// Assume if the texture is allocated, it needs to be changed. Otherwise the texture should be created through
// InitInternal() with the correct parameters
if (master_texture_) {
// If we're initiated, we need to recreate the texture. Otherwise this backend isn't active so it doesn't matter.
if (IsInitiated()) {
master_texture_->Destroy();
master_texture_->Create(QOpenGLContext::currentContext(), params().effective_width(), params().effective_height(), params().format());
master_texture_->Create(QOpenGLContext::currentContext(),
params().effective_width(),
params().effective_height(),
params().format());
}
}
+29
View File
@@ -3,6 +3,9 @@
#include <QDateTime>
#include <QThread>
#include "core.h"
#include "window/mainwindow/mainwindow.h"
RenderBackend::RenderBackend(QObject *parent) :
QObject(parent),
compiled_(false),
@@ -12,6 +15,8 @@ RenderBackend::RenderBackend(QObject *parent) :
recompile_queued_(false),
input_update_queued_(false)
{
// FIXME: Don't create in CLI mode
cancel_dialog_ = new RenderCancelDialog(Core::instance()->main_window());
}
bool RenderBackend::Init()
@@ -30,6 +35,8 @@ bool RenderBackend::Init()
thread->start(QThread::LowPriority);
}
cancel_dialog_->SetWorkerCount(threads_.size());
started_ = InitInternal();
// Connects workers and moves them to their respective threads
@@ -50,6 +57,8 @@ void RenderBackend::Close()
started_ = false;
CancelQueue();
Decompile();
CloseInternal();
@@ -81,6 +90,8 @@ const QString &RenderBackend::GetError() const
void RenderBackend::SetViewerNode(ViewerOutput *viewer_node)
{
if (viewer_node_ != nullptr) {
CancelQueue();
DisconnectViewer(viewer_node_);
Decompile();
@@ -297,6 +308,7 @@ void RenderBackend::CacheNext()
render_job_info_.insert(cache_frame, job_time);
SetWorkerBusyState(worker, true);
cancel_dialog_->WorkerStarted();
QMetaObject::invokeMethod(worker,
"Render",
@@ -312,6 +324,20 @@ ViewerOutput *RenderBackend::viewer_node() const
return copied_viewer_node_;
}
void RenderBackend::CancelQueue()
{
cache_queue_.clear();
int busy = 0;
for (int i=0;i<processor_busy_state_.size();i++) {
if (processor_busy_state_.at(i))
busy++;
}
qDebug() << this << "has" << busy << "busy workers";
cancel_dialog_->RunIfWorkersAreBusy();
}
bool RenderBackend::ViewerIsConnected() const
{
return viewer_node_ != nullptr;
@@ -375,6 +401,9 @@ void RenderBackend::InitWorkers()
// Connect to it
ConnectWorkerToThis(processor);
// Connect cancel dialog to it
connect(processor, &RenderWorker::CompletedCache, cancel_dialog_, &RenderCancelDialog::WorkerDone, Qt::QueuedConnection);
// Finally, we can move it to its own thread
processor->moveToThread(thread);
+5 -2
View File
@@ -4,6 +4,7 @@
#include <QLinkedList>
#include "common/constructors.h"
#include "dialog/rendercancel/rendercancel.h"
#include "decodercache.h"
#include "node/graph.h"
#include "node/output/viewer/viewer.h"
@@ -15,8 +16,6 @@ class RenderBackend : public QObject
public:
RenderBackend(QObject* parent = nullptr);
DISABLE_COPY_MOVE(RenderBackend)
bool Init();
void Close();
@@ -29,6 +28,8 @@ public:
ViewerOutput* viewer_node() const;
void CancelQueue();
public slots:
void InvalidateCache(const rational &start_range, const rational &end_range);
@@ -142,6 +143,8 @@ private:
QVector<bool> processor_busy_state_;
RenderCancelDialog* cancel_dialog_;
};
#endif // RENDERBACKEND_H
+1 -4
View File
@@ -76,10 +76,7 @@ const VideoRenderingParams &VideoRenderBackend::params() const
void VideoRenderBackend::SetParameters(const VideoRenderingParams& params)
{
if (!AllProcessorsAreAvailable()) {
qCritical() << "Attempted to set parameters on a backend whose workers are still busy";
return;
}
CancelQueue();
// Set new parameters
params_ = params;