render: cleaned up threading

This commit is contained in:
itsmattkc
2022-06-25 09:59:43 -07:00
parent ec215f7de6
commit 63f74691a5
19 changed files with 150 additions and 688 deletions
+2 -2
View File
@@ -46,8 +46,6 @@ set(OLIVE_SOURCES
render/renderer.cpp
render/renderer.h
render/rendercache.h
render/rendererthreadwrapper.cpp
render/rendererthreadwrapper.h
render/renderjobtracker.cpp
render/renderjobtracker.h
render/rendermanager.cpp
@@ -55,6 +53,8 @@ set(OLIVE_SOURCES
render/rendermodes.h
render/renderprocessor.cpp
render/renderprocessor.h
render/renderticket.cpp
render/renderticket.h
render/shadercode.h
render/subtitleparams.cpp
render/subtitleparams.h
+8 -11
View File
@@ -61,7 +61,7 @@ PreviewAutoCacher::~PreviewAutoCacher()
SetViewerNode(nullptr);
}
RenderTicketPtr PreviewAutoCacher::GetSingleFrame(const rational &t, RenderTicketPriority priority)
RenderTicketPtr PreviewAutoCacher::GetSingleFrame(const rational &t)
{
// If we have a single frame render queued (but not yet sent to the RenderManager), cancel it now
CancelQueuedSingleFrameRender();
@@ -70,7 +70,6 @@ RenderTicketPtr PreviewAutoCacher::GetSingleFrame(const rational &t, RenderTicke
auto sfr = std::make_shared<RenderTicket>();
sfr->Start();
sfr->setProperty("time", QVariant::fromValue(t));
sfr->setProperty("priority", int(priority));
// Queue it and try to render
single_frame_render_ = sfr;
@@ -79,9 +78,9 @@ RenderTicketPtr PreviewAutoCacher::GetSingleFrame(const rational &t, RenderTicke
return sfr;
}
RenderTicketPtr PreviewAutoCacher::GetRangeOfAudio(TimeRange range, RenderTicketPriority priority)
RenderTicketPtr PreviewAutoCacher::GetRangeOfAudio(TimeRange range)
{
return RenderAudio(range, false, priority);
return RenderAudio(range, false);
}
void PreviewAutoCacher::VideoInvalidated(const TimeRange &range)
@@ -567,7 +566,6 @@ void PreviewAutoCacher::TryRender()
if (single_frame_render_) {
// Check if already caching this
RenderTicketWatcher *watcher = RenderFrame(single_frame_render_->property("time").value<rational>(),
RenderTicketPriority(single_frame_render_->property("priority").toInt()),
nullptr);
video_immediate_passthroughs_[watcher].append(single_frame_render_);
@@ -585,7 +583,7 @@ void PreviewAutoCacher::TryRender()
// We want this hash, if we're not already rendering, start render now
if (!render_task) {
// Don't render any hash more than once
RenderFrame(t, RenderTicketPriority::kNormal, viewer_node_->video_frame_cache());
RenderFrame(t, viewer_node_->video_frame_cache());
}
emit SignalCacheProxyTaskProgress(double(queued_frame_iterator_.frame_index()) / double(queued_frame_iterator_.size()));
@@ -605,13 +603,13 @@ void PreviewAutoCacher::TryRender()
r.set_out(qMin(r.out(), r.in() + AudioVisualWaveform::kMinimumSampleRate.flipped()));
// Start job
RenderAudio(r, true, RenderTicketPriority::kNormal);
RenderAudio(r, true);
audio_iterator_.remove(r);
}
}
RenderTicketWatcher* PreviewAutoCacher::RenderFrame(Node *node, const rational& time, RenderTicketPriority priority, FrameHashCache *cache)
RenderTicketWatcher* PreviewAutoCacher::RenderFrame(Node *node, const rational& time, FrameHashCache *cache)
{
RenderTicketWatcher* watcher = new RenderTicketWatcher();
watcher->setProperty("job", QVariant::fromValue(last_update_time_));
@@ -625,19 +623,18 @@ RenderTicketWatcher* PreviewAutoCacher::RenderFrame(Node *node, const rational&
time,
RenderMode::kOffline,
cache,
priority,
RenderManager::kTexture));
return watcher;
}
RenderTicketPtr PreviewAutoCacher::RenderAudio(Node *node, const TimeRange &r, bool generate_waveforms, RenderTicketPriority priority)
RenderTicketPtr PreviewAutoCacher::RenderAudio(Node *node, const TimeRange &r, bool generate_waveforms)
{
RenderTicketWatcher* watcher = new RenderTicketWatcher();
watcher->setProperty("job", QVariant::fromValue(last_update_time_));
connect(watcher, &RenderTicketWatcher::Finished, this, &PreviewAutoCacher::AudioRendered);
audio_tasks_.insert(watcher, r);
RenderTicketPtr ticket = RenderManager::instance()->RenderAudio(node, r, copied_viewer_node_->GetAudioParams(), RenderMode::kOffline, generate_waveforms, priority);
RenderTicketPtr ticket = RenderManager::instance()->RenderAudio(node, r, copied_viewer_node_->GetAudioParams(), RenderMode::kOffline, generate_waveforms);
watcher->SetTicket(ticket);
return ticket;
}
+8 -10
View File
@@ -33,8 +33,6 @@
#include "render/audioparams.h"
#include "render/renderjobtracker.h"
#include "render/rendermanager.h"
#include "threading/threadpool.h"
#include "threading/threadticketwatcher.h"
namespace olive {
@@ -51,9 +49,9 @@ public:
virtual ~PreviewAutoCacher() override;
RenderTicketPtr GetSingleFrame(const rational& t, RenderTicketPriority prioritize);
RenderTicketPtr GetSingleFrame(const rational& t);
RenderTicketPtr GetRangeOfAudio(TimeRange range, RenderTicketPriority prioritize);
RenderTicketPtr GetRangeOfAudio(TimeRange range);
/**
* @brief Set the viewer node to auto-cache
@@ -100,16 +98,16 @@ signals:
private:
void TryRender();
RenderTicketWatcher *RenderFrame(Node *node, const rational &time, RenderTicketPriority priority, FrameHashCache *cache);
RenderTicketWatcher *RenderFrame(const rational &time, RenderTicketPriority priority, FrameHashCache *cache)
RenderTicketWatcher *RenderFrame(Node *node, const rational &time, FrameHashCache *cache);
RenderTicketWatcher *RenderFrame(const rational &time, FrameHashCache *cache)
{
return RenderFrame(copied_viewer_node_->GetConnectedTextureOutput(), time, priority, cache);
return RenderFrame(copied_viewer_node_->GetConnectedTextureOutput(), time, cache);
}
RenderTicketPtr RenderAudio(Node *node, const TimeRange &range, bool generate_waveforms, RenderTicketPriority priority);
RenderTicketPtr RenderAudio(const TimeRange &range, bool generate_waveforms, RenderTicketPriority priority)
RenderTicketPtr RenderAudio(Node *node, const TimeRange &range, bool generate_waveforms);
RenderTicketPtr RenderAudio(const TimeRange &range, bool generate_waveforms)
{
return RenderAudio(copied_viewer_node_->GetConnectedSampleOutput(), range, generate_waveforms, priority);
return RenderAudio(copied_viewer_node_->GetConnectedSampleOutput(), range, generate_waveforms);
}
/**
-181
View File
@@ -1,181 +0,0 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2022 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "rendererthreadwrapper.h"
namespace olive {
RendererThreadWrapper::RendererThreadWrapper(Renderer *inner, QObject *parent) :
Renderer(parent),
inner_(inner),
thread_(nullptr)
{
}
bool RendererThreadWrapper::Init()
{
// Init context in main thread
if (!inner_->Init()) {
return false;
}
// Create thread
thread_ = new QThread(this);
thread_->start(QThread::IdlePriority);
// Move context to thread
inner_->moveToThread(thread_);
// Queue post-init in new thread
QMetaObject::invokeMethod(inner_, "PostInit", Qt::BlockingQueuedConnection);
return true;
}
void RendererThreadWrapper::PostInit()
{
// Do nothing
}
void RendererThreadWrapper::DestroyInternal()
{
if (thread_) {
QMetaObject::invokeMethod(inner_, "DestroyInternal", Qt::BlockingQueuedConnection);
thread_->quit();
thread_->wait();
delete thread_;
thread_ = nullptr;
// Destroy in main thread
inner_->PostDestroy();
}
}
void RendererThreadWrapper::ClearDestination(Texture *texture, double r, double g, double b, double a)
{
QMetaObject::invokeMethod(inner_, "ClearDestination", Qt::BlockingQueuedConnection,
OLIVE_NS_ARG(Texture*, texture),
Q_ARG(double, r),
Q_ARG(double, g),
Q_ARG(double, b),
Q_ARG(double, a));
}
QVariant RendererThreadWrapper::CreateNativeTexture2D(int width, int height, VideoParams::Format format, int channel_count, const void *data, int linesize)
{
QVariant v;
QMetaObject::invokeMethod(inner_, "CreateNativeTexture2D", Qt::BlockingQueuedConnection,
Q_RETURN_ARG(QVariant, v),
Q_ARG(int, width),
Q_ARG(int, height),
OLIVE_NS_ARG(VideoParams::Format, format),
Q_ARG(int, channel_count),
Q_ARG(const void*, data),
Q_ARG(int, linesize));
return v;
}
QVariant RendererThreadWrapper::CreateNativeTexture3D(int width, int height, int depth, VideoParams::Format format, int channel_count, const void *data, int linesize)
{
QVariant v;
QMetaObject::invokeMethod(inner_, "CreateNativeTexture3D", Qt::BlockingQueuedConnection,
Q_RETURN_ARG(QVariant, v),
Q_ARG(int, width),
Q_ARG(int, height),
Q_ARG(int, depth),
OLIVE_NS_ARG(VideoParams::Format, format),
Q_ARG(int, channel_count),
Q_ARG(const void*, data),
Q_ARG(int, linesize));
return v;
}
void RendererThreadWrapper::DestroyNativeTexture(QVariant texture)
{
QMetaObject::invokeMethod(inner_, "DestroyNativeTexture", Qt::BlockingQueuedConnection,
Q_ARG(QVariant, texture));
}
QVariant RendererThreadWrapper::CreateNativeShader(ShaderCode code)
{
QVariant v;
QMetaObject::invokeMethod(inner_, "CreateNativeShader", Qt::BlockingQueuedConnection,
Q_RETURN_ARG(QVariant, v),
OLIVE_NS_ARG(ShaderCode, code));
return v;
}
void RendererThreadWrapper::DestroyNativeShader(QVariant shader)
{
QMetaObject::invokeMethod(inner_, "DestroyNativeShader", Qt::BlockingQueuedConnection,
Q_ARG(QVariant, shader));
}
void RendererThreadWrapper::UploadToTexture(Texture *texture, const void *data, int linesize)
{
QMetaObject::invokeMethod(inner_, "UploadToTexture", Qt::BlockingQueuedConnection,
OLIVE_NS_ARG(Texture*, texture),
Q_ARG(const void*, data),
Q_ARG(int, linesize));
}
void RendererThreadWrapper::DownloadFromTexture(Texture *texture, void *data, int linesize)
{
QMetaObject::invokeMethod(inner_, "DownloadFromTexture", Qt::BlockingQueuedConnection,
OLIVE_NS_ARG(Texture*, texture),
Q_ARG(void*, data),
Q_ARG(int, linesize));
}
void RendererThreadWrapper::Flush()
{
QMetaObject::invokeMethod(inner_, "Flush", Qt::BlockingQueuedConnection);
}
Color RendererThreadWrapper::GetPixelFromTexture(Texture *texture, const QPointF &pt)
{
Color c;
QMetaObject::invokeMethod(inner_, "GetPixelFromTexture", Qt::BlockingQueuedConnection,
OLIVE_NS_RETURN_ARG(Color, c),
OLIVE_NS_ARG(Texture*, texture),
Q_ARG(QPointF, pt));
return c;
}
void RendererThreadWrapper::Blit(QVariant shader, ShaderJob job, Texture *destination, VideoParams destination_params, bool clear_destination)
{
QMetaObject::invokeMethod(inner_, "Blit", Qt::BlockingQueuedConnection,
Q_ARG(QVariant, shader),
OLIVE_NS_ARG(ShaderJob, job),
OLIVE_NS_ARG(Texture*, destination),
OLIVE_NS_ARG(VideoParams, destination_params),
Q_ARG(bool, clear_destination));
}
}
-86
View File
@@ -1,86 +0,0 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2022 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef RENDERCONTEXTTHREADWRAPPER_H
#define RENDERCONTEXTTHREADWRAPPER_H
#include <QThread>
#include "renderer.h"
namespace olive {
class RendererThreadWrapper : public Renderer
{
public:
RendererThreadWrapper(Renderer* inner, QObject* parent = nullptr);
virtual ~RendererThreadWrapper() override
{
Destroy();
PostDestroy();
delete inner_;
}
virtual bool Init() override;
virtual void PostDestroy() override {}
public slots:
virtual void PostInit() override;
virtual void DestroyInternal() override;
virtual void ClearDestination(olive::Texture *texture = nullptr, double r = 0.0, double g = 0.0, double b = 0.0, double a = 0.0) override;
virtual QVariant CreateNativeTexture2D(int width, int height, olive::VideoParams::Format format, int channel_count, const void* data = nullptr, int linesize = 0) override;
virtual QVariant CreateNativeTexture3D(int width, int height, int depth, olive::VideoParams::Format format, int channel_count, const void* data = nullptr, int linesize = 0) override;
virtual void DestroyNativeTexture(QVariant texture) override;
virtual QVariant CreateNativeShader(olive::ShaderCode code) override;
virtual void DestroyNativeShader(QVariant shader) override;
virtual void UploadToTexture(olive::Texture* texture, const void* data, int linesize) override;
virtual void DownloadFromTexture(olive::Texture* texture, void* data, int linesize) override;
virtual void Flush() override;
virtual Color GetPixelFromTexture(olive::Texture *texture, const QPointF &pt) override;
protected slots:
virtual void Blit(QVariant shader,
olive::ShaderJob job,
olive::Texture* destination,
olive::VideoParams destination_params,
bool clear_destination) override;
private:
Renderer* inner_;
QThread* thread_;
};
}
#endif // RENDERCONTEXTTHREADWRAPPER_H
+3 -5
View File
@@ -27,7 +27,6 @@
#include "config/config.h"
#include "core.h"
#include "render/opengl/openglrenderer.h"
#include "render/rendererthreadwrapper.h"
#include "renderprocessor.h"
#include "task/conform/conform.h"
#include "task/taskmanager.h"
@@ -84,7 +83,7 @@ RenderManager::~RenderManager()
RenderTicketPtr RenderManager::RenderFrame(Node *node, const VideoParams &vparam, const AudioParams &param,
ColorManager* color_manager, const rational& time, RenderMode::Mode mode,
FrameHashCache* cache, RenderTicketPriority priority, ReturnType return_type)
FrameHashCache* cache, ReturnType return_type)
{
return RenderFrame(node,
color_manager,
@@ -97,7 +96,6 @@ RenderTicketPtr RenderManager::RenderFrame(Node *node, const VideoParams &vparam
VideoParams::kFormatInvalid,
nullptr,
cache,
priority,
return_type);
}
@@ -107,7 +105,7 @@ RenderTicketPtr RenderManager::RenderFrame(Node *node, ColorManager* color_manag
const QSize& force_size,
const QMatrix4x4& force_matrix, VideoParams::Format force_format,
ColorProcessorPtr force_color_output,
FrameHashCache* cache, RenderTicketPriority priority, ReturnType return_type)
FrameHashCache* cache, ReturnType return_type)
{
// Create ticket
RenderTicketPtr ticket = std::make_shared<RenderTicket>();
@@ -136,7 +134,7 @@ RenderTicketPtr RenderManager::RenderFrame(Node *node, ColorManager* color_manag
return ticket;
}
RenderTicketPtr RenderManager::RenderAudio(Node *node, const TimeRange &r, const AudioParams &params, RenderMode::Mode mode, bool generate_waveforms, RenderTicketPriority priority)
RenderTicketPtr RenderManager::RenderAudio(Node *node, const TimeRange &r, const AudioParams &params, RenderMode::Mode mode, bool generate_waveforms)
{
// Create ticket
RenderTicketPtr ticket = std::make_shared<RenderTicket>();
+4 -4
View File
@@ -30,8 +30,8 @@
#include "node/output/viewer/viewer.h"
#include "node/traverser.h"
#include "render/renderer.h"
#include "render/renderticket.h"
#include "rendercache.h"
#include "threading/threadpool.h"
namespace olive {
@@ -110,14 +110,14 @@ public:
*/
RenderTicketPtr RenderFrame(Node *node, const VideoParams &vparam, const AudioParams &param, ColorManager* color_manager,
const rational& time, RenderMode::Mode mode,
FrameHashCache* cache = nullptr, RenderTicketPriority priority = RenderTicketPriority::kNormal, ReturnType return_type = kFrame);
FrameHashCache* cache = nullptr, ReturnType return_type = kFrame);
RenderTicketPtr RenderFrame(Node *node, ColorManager* color_manager,
const rational& time, RenderMode::Mode mode,
const VideoParams& video_params, const AudioParams& audio_params,
const QSize& force_size,
const QMatrix4x4& force_matrix, VideoParams::Format force_format,
ColorProcessorPtr force_color_output,
FrameHashCache* cache = nullptr, RenderTicketPriority priority = RenderTicketPriority::kNormal, ReturnType return_type = kFrame);
FrameHashCache* cache = nullptr, ReturnType return_type = kFrame);
/**
* @brief Asynchronously generate a chunk of audio
@@ -126,7 +126,7 @@ public:
*
* This function is thread-safe.
*/
RenderTicketPtr RenderAudio(Node *viewer, const TimeRange& r, const AudioParams& params, RenderMode::Mode mode, bool generate_waveforms, RenderTicketPriority priority = RenderTicketPriority::kNormal);
RenderTicketPtr RenderAudio(Node *viewer, const TimeRange& r, const AudioParams& params, RenderMode::Mode mode, bool generate_waveforms);
bool RemoveTicket(RenderTicketPtr ticket);
+1 -1
View File
@@ -25,7 +25,7 @@
#include "node/traverser.h"
#include "render/renderer.h"
#include "rendercache.h"
#include "threading/threadticket.h"
#include "renderticket.h"
namespace olive {
+209
View File
@@ -0,0 +1,209 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2022 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#include "renderticket.h"
namespace olive {
RenderTicket::RenderTicket() :
is_running_(false),
has_result_(false),
finish_count_(0)
{
}
void RenderTicket::WaitForFinished(QMutex *mutex)
{
if (is_running_) {
wait_.wait(mutex);
}
}
void RenderTicket::Start()
{
QMutexLocker locker(&lock_);
is_running_ = true;
has_result_ = false;
result_.clear();
}
void RenderTicket::Finish()
{
FinishInternal(false, QVariant());
}
void RenderTicket::Finish(QVariant result)
{
FinishInternal(true, result);
}
QVariant RenderTicket::Get()
{
WaitForFinished();
// We don't have to mutex around this because there is no way to write to `result_` after
// the ticket has finished and the above function blocks the calling thread until it is finished
return result_;
}
void RenderTicket::WaitForFinished()
{
QMutexLocker locker(&lock_);
WaitForFinished(&lock_);
}
bool RenderTicket::IsRunning(bool lock)
{
if (lock) {
lock_.lock();
}
bool running = is_running_;
if (lock) {
lock_.unlock();
}
return running;
}
int RenderTicket::GetFinishCount(bool lock)
{
if (lock) {
lock_.lock();
}
int count = finish_count_;
if (lock) {
lock_.unlock();
}
return count;
}
bool RenderTicket::HasResult()
{
QMutexLocker locker(&lock_);
return has_result_;
}
void RenderTicket::FinishInternal(bool has_result, QVariant result)
{
QMutexLocker locker(&lock_);
if (!is_running_) {
qWarning() << "Tried to finish ticket that wasn't running";
} else {
is_running_ = false;
has_result_ = has_result;
result_ = result;
finish_count_++;
wait_.wakeAll();
locker.unlock();
emit Finished();
}
}
RenderTicketWatcher::RenderTicketWatcher(QObject *parent) :
QObject(parent),
ticket_(nullptr)
{
}
void RenderTicketWatcher::SetTicket(RenderTicketPtr ticket)
{
if (ticket_) {
qCritical() << "Tried to set a ticket on a RenderTicketWatcher twice";
return;
}
if (!ticket) {
qCritical() << "Tried to set a null ticket on a RenderTicketWatcher";
return;
}
ticket_ = ticket;
// Lock ticket so we can query if it's already finished by the time this code runs
QMutexLocker locker(ticket->lock());
connect(ticket_.get(), &RenderTicket::Finished, this, &RenderTicketWatcher::TicketFinished);
if (!ticket_->IsRunning(false) && ticket_->GetFinishCount(false) > 0) {
// Ticket has already finished before, so we emit a signal
locker.unlock();
TicketFinished();
}
}
bool RenderTicketWatcher::IsRunning()
{
if (ticket_) {
return ticket_->IsRunning();
} else {
return false;
}
}
void RenderTicketWatcher::WaitForFinished()
{
if (ticket_) {
ticket_->WaitForFinished();
}
}
QVariant RenderTicketWatcher::Get()
{
if (ticket_) {
return ticket_->Get();
} else {
return QVariant();
}
}
bool RenderTicketWatcher::HasResult()
{
if (ticket_) {
return ticket_->HasResult();
} else {
return false;
}
}
void RenderTicketWatcher::Cancel()
{
if (ticket_) {
ticket_->Cancel();
}
}
void RenderTicketWatcher::TicketFinished()
{
emit Finished(this);
}
}
+173
View File
@@ -0,0 +1,173 @@
/***
Olive - Non-Linear Video Editor
Copyright (C) 2022 Olive Team
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***/
#ifndef RENDERTICKET_H
#define RENDERTICKET_H
#include <QDateTime>
#include <QMutex>
#include <QWaitCondition>
#include "codec/frame.h"
#include "codec/samplebuffer.h"
#include "common/cancelableobject.h"
#include "common/timerange.h"
#include "node/output/viewer/viewer.h"
namespace olive {
class RenderTicket : public QObject, public CancelableObject
{
Q_OBJECT
public:
RenderTicket();
/**
* @brief Get the ticket's current state
*
* This function is thread safe, unless `lock` is set to false. Then the caller has responsibility
* of locking the mutex before and unlocking after this function is called.
*/
bool IsRunning(bool lock = true);
/**
* @brief Determine how many times ticket has been finished
*
* This function is thread safe, unless `lock` is set to false. Then the caller has responsibility
* of locking the mutex before and unlocking after this function is called.
*/
int GetFinishCount(bool lock = true);
/**
* @brief Check if this ticket has a result
*
* If this ticket is running, this will always return false.
*/
bool HasResult();
/**
* @brief Get value, if any
*/
QVariant Get();
/**
* @brief Wait for ticket to be finished
*
* If this ticket is not running, this function returns immediately.
*/
void WaitForFinished();
void WaitForFinished(QMutex* mutex);
/**
* @brief Access this ticket's mutex
*
* Use if you're doing several operations on a ticket and need to ensure thread safety while
* doing so. Most of the time this isn't necessary since all functions are thread safe by default.
*/
QMutex* lock()
{
return &lock_;
}
/**
* @brief Signal to the ticket that it is running
*
* If any value is set, it is cleared.
*/
void Start();
/**
* @brief Finish ticket with no value
*
* Sets ticket to no longer running and assume it has received no result.
*/
void Finish();
/**
* @brief Finish ticket with value
*
* Sets ticket to no longer running and provide a value generated by the operation requested.
*/
void Finish(QVariant result);
signals:
/**
* @brief Emitted when finish has been called by any means (either cancelled or with a result)
*/
void Finished();
private:
void FinishInternal(bool has_result, QVariant result);
bool is_running_;
QVariant result_;
bool has_result_;
int finish_count_;
QMutex lock_;
QWaitCondition wait_;
};
using RenderTicketPtr = std::shared_ptr<RenderTicket>;
class RenderTicketWatcher : public QObject
{
Q_OBJECT
public:
RenderTicketWatcher(QObject* parent = nullptr);
RenderTicketPtr GetTicket() const
{
return ticket_;
}
void SetTicket(RenderTicketPtr ticket);
bool IsRunning();
void WaitForFinished();
QVariant Get();
bool HasResult();
void Cancel();
signals:
void Finished(RenderTicketWatcher* watcher);
private:
RenderTicketPtr ticket_;
private slots:
void TicketFinished();
};
}
Q_DECLARE_METATYPE(olive::RenderTicketPtr)
#endif // RENDERTICKET_H