moved things around and started rewriting render backend
This commit is contained in:
@@ -0,0 +1,26 @@
|
||||
# Olive - Non-Linear Video Editor
|
||||
# Copyright (C) 2019 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/>.
|
||||
|
||||
set(OLIVE_SOURCES
|
||||
${OLIVE_SOURCES}
|
||||
threading/threadticket.cpp
|
||||
threading/threadticket.h
|
||||
threading/threadticketwatcher.cpp
|
||||
threading/threadticketwatcher.h
|
||||
threading/threadpool.cpp
|
||||
threading/threadpool.h
|
||||
PARENT_SCOPE
|
||||
)
|
||||
@@ -0,0 +1,138 @@
|
||||
/***
|
||||
|
||||
Olive - Non-Linear Video Editor
|
||||
Copyright (C) 2019 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 "threadpool.h"
|
||||
|
||||
OLIVE_NAMESPACE_ENTER
|
||||
|
||||
ThreadPool::ThreadPool(QThread::Priority priority, int threads, QObject *parent) :
|
||||
QObject(parent)
|
||||
{
|
||||
all_threads_.resize(threads ? threads : QThread::idealThreadCount());
|
||||
|
||||
// Create threads
|
||||
for (int i=0; i<all_threads_.size(); i++) {
|
||||
ThreadPoolThread* t = new ThreadPoolThread(this);
|
||||
|
||||
// Add to vector of all threads
|
||||
all_threads_[i] = t;
|
||||
|
||||
// Append to list of available threads
|
||||
available_threads_.push_back(t);
|
||||
|
||||
// Connect done signal
|
||||
connect(t, &ThreadPoolThread::Done, this, &ThreadPool::ThreadDone);
|
||||
|
||||
// Start the thread at the given priority
|
||||
t->start(priority);
|
||||
}
|
||||
}
|
||||
|
||||
ThreadPool::~ThreadPool()
|
||||
{
|
||||
foreach (ThreadPoolThread* thread, all_threads_) {
|
||||
thread->Cancel();
|
||||
thread->wait();
|
||||
delete thread;
|
||||
}
|
||||
|
||||
RunNext();
|
||||
}
|
||||
|
||||
void ThreadPool::AddTicket(RenderTicketPtr ticket, bool prioritize)
|
||||
{
|
||||
if (prioritize) {
|
||||
ticket_queue_.push_front(ticket);
|
||||
} else {
|
||||
ticket_queue_.push_back(ticket);
|
||||
}
|
||||
|
||||
RunNext();
|
||||
}
|
||||
|
||||
void ThreadPool::RunNext()
|
||||
{
|
||||
while (!ticket_queue_.empty() && !available_threads_.empty()) {
|
||||
// Run function
|
||||
RenderTicketPtr ticket = ticket_queue_.front();
|
||||
ticket_queue_.pop_front();
|
||||
|
||||
if (!ticket->WasCancelled()) {
|
||||
ThreadPoolThread* thread = available_threads_.front();
|
||||
available_threads_.pop_front();
|
||||
|
||||
// Run the ticket in the thread, which actually just calls our virtual function RunTicket
|
||||
thread->RunTicket(ticket);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ThreadPool::ThreadDone()
|
||||
{
|
||||
ThreadPoolThread* thread = static_cast<ThreadPoolThread*>(sender());
|
||||
|
||||
available_threads_.push_back(thread);
|
||||
|
||||
RunNext();
|
||||
}
|
||||
|
||||
ThreadPoolThread::ThreadPoolThread(ThreadPool *parent)
|
||||
{
|
||||
pool_ = parent;
|
||||
cancelled_ = false;
|
||||
|
||||
// Ensures mutex is definitely locked by the time the thread is running
|
||||
mutex_.lock();
|
||||
}
|
||||
|
||||
ThreadPoolThread::~ThreadPoolThread()
|
||||
{
|
||||
mutex_.unlock();
|
||||
}
|
||||
|
||||
void ThreadPoolThread::RunTicket(RenderTicketPtr ticket)
|
||||
{
|
||||
mutex_.lock();
|
||||
ticket_ = ticket;
|
||||
wait_cond_.wakeAll();
|
||||
mutex_.unlock();
|
||||
}
|
||||
|
||||
void ThreadPoolThread::Cancel()
|
||||
{
|
||||
cancelled_ = true;
|
||||
wait_cond_.wakeAll();
|
||||
}
|
||||
|
||||
void ThreadPoolThread::run()
|
||||
{
|
||||
while (!cancelled_) {
|
||||
wait_cond_.wait(&mutex_);
|
||||
|
||||
if (ticket_) {
|
||||
pool_->RunTicket(ticket_);
|
||||
ticket_ = nullptr;
|
||||
}
|
||||
|
||||
emit Done();
|
||||
}
|
||||
}
|
||||
|
||||
OLIVE_NAMESPACE_EXIT
|
||||
@@ -0,0 +1,94 @@
|
||||
/***
|
||||
|
||||
Olive - Non-Linear Video Editor
|
||||
Copyright (C) 2019 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 THREADPOOL_H
|
||||
#define THREADPOOL_H
|
||||
|
||||
#include <QThread>
|
||||
|
||||
#include "threading/threadticket.h"
|
||||
|
||||
OLIVE_NAMESPACE_ENTER
|
||||
|
||||
class ThreadPoolThread;
|
||||
|
||||
class ThreadPool : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ThreadPool(QThread::Priority priority = QThread::InheritPriority, int threads = 0, QObject* parent = nullptr);
|
||||
|
||||
virtual ~ThreadPool() override;
|
||||
|
||||
RenderTicketPtr Queue();
|
||||
|
||||
virtual void RunTicket(RenderTicketPtr ticket) const = 0;
|
||||
|
||||
public slots:
|
||||
void AddTicket(OLIVE_NAMESPACE::RenderTicketPtr ticket, bool prioritize = false);
|
||||
|
||||
private:
|
||||
void RunNext();
|
||||
|
||||
QVector<ThreadPoolThread*> all_threads_;
|
||||
|
||||
std::list<ThreadPoolThread*> available_threads_;
|
||||
|
||||
std::list<RenderTicketPtr> ticket_queue_;
|
||||
|
||||
private slots:
|
||||
void ThreadDone();
|
||||
|
||||
};
|
||||
|
||||
class ThreadPoolThread : public QThread
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
ThreadPoolThread(ThreadPool* parent);
|
||||
|
||||
virtual ~ThreadPoolThread() override;
|
||||
|
||||
void RunTicket(RenderTicketPtr ticket);
|
||||
|
||||
void Cancel();
|
||||
|
||||
protected:
|
||||
virtual void run() override;
|
||||
|
||||
signals:
|
||||
void Done();
|
||||
|
||||
private:
|
||||
ThreadPool* pool_;
|
||||
|
||||
RenderTicketPtr ticket_;
|
||||
|
||||
QMutex mutex_;
|
||||
|
||||
QWaitCondition wait_cond_;
|
||||
|
||||
QAtomicInt cancelled_;
|
||||
|
||||
};
|
||||
|
||||
OLIVE_NAMESPACE_EXIT
|
||||
|
||||
#endif // THREADPOOL_H
|
||||
@@ -0,0 +1,119 @@
|
||||
/***
|
||||
|
||||
Olive - Non-Linear Video Editor
|
||||
Copyright (C) 2019 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 "threadticket.h"
|
||||
|
||||
OLIVE_NAMESPACE_ENTER
|
||||
|
||||
RenderTicket::RenderTicket() :
|
||||
started_(false),
|
||||
finished_(false),
|
||||
cancelled_(false)
|
||||
{
|
||||
SetJobTime();
|
||||
}
|
||||
|
||||
void RenderTicket::WaitForFinished()
|
||||
{
|
||||
QMutexLocker locker(&lock_);
|
||||
|
||||
if (!finished_) {
|
||||
wait_.wait(&lock_);
|
||||
}
|
||||
}
|
||||
|
||||
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_;
|
||||
}
|
||||
|
||||
bool RenderTicket::IsFinished(bool lock)
|
||||
{
|
||||
if (lock) {
|
||||
lock_.lock();
|
||||
}
|
||||
|
||||
bool finished = finished_;
|
||||
|
||||
if (lock) {
|
||||
lock_.unlock();
|
||||
}
|
||||
|
||||
return finished;
|
||||
}
|
||||
|
||||
bool RenderTicket::WasCancelled()
|
||||
{
|
||||
QMutexLocker locker(&lock_);
|
||||
|
||||
return cancelled_;
|
||||
}
|
||||
|
||||
void RenderTicket::Start()
|
||||
{
|
||||
QMutexLocker locker(&lock_);
|
||||
|
||||
if (!started_ && !finished_) {
|
||||
started_ = true;
|
||||
}
|
||||
}
|
||||
|
||||
void RenderTicket::Finish(QVariant result, bool cancelled)
|
||||
{
|
||||
QMutexLocker locker(&lock_);
|
||||
|
||||
if (started_ && !finished_) {
|
||||
finished_ = true;
|
||||
cancelled_ = cancelled;
|
||||
|
||||
result_ = result;
|
||||
|
||||
wait_.wakeAll();
|
||||
|
||||
locker.unlock();
|
||||
|
||||
emit Finished();
|
||||
}
|
||||
}
|
||||
|
||||
void RenderTicket::Cancel()
|
||||
{
|
||||
QMutexLocker locker(&lock_);
|
||||
|
||||
if (!finished_) {
|
||||
cancelled_ = true;
|
||||
|
||||
if (!started_) {
|
||||
finished_ = true;
|
||||
|
||||
wait_.wakeAll();
|
||||
|
||||
locker.unlock();
|
||||
|
||||
emit Finished();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
OLIVE_NAMESPACE_EXIT
|
||||
@@ -0,0 +1,96 @@
|
||||
/***
|
||||
|
||||
Olive - Non-Linear Video Editor
|
||||
Copyright (C) 2019 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/timerange.h"
|
||||
#include "node/output/viewer/viewer.h"
|
||||
|
||||
OLIVE_NAMESPACE_ENTER
|
||||
|
||||
class RenderTicket : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
RenderTicket();
|
||||
|
||||
qint64 GetJobTime() const
|
||||
{
|
||||
return job_time_;
|
||||
}
|
||||
|
||||
void SetJobTime()
|
||||
{
|
||||
job_time_ = QDateTime::currentMSecsSinceEpoch();
|
||||
}
|
||||
|
||||
void WaitForFinished();
|
||||
|
||||
QVariant Get();
|
||||
|
||||
bool IsFinished(bool lock = true);
|
||||
|
||||
bool WasCancelled();
|
||||
|
||||
QMutex* lock()
|
||||
{
|
||||
return &lock_;
|
||||
}
|
||||
|
||||
void Start();
|
||||
|
||||
void Finish(QVariant result, bool cancelled);
|
||||
|
||||
void Cancel();
|
||||
|
||||
signals:
|
||||
void Finished();
|
||||
|
||||
private:
|
||||
bool started_;
|
||||
|
||||
bool finished_;
|
||||
|
||||
bool cancelled_;
|
||||
|
||||
QVariant result_;
|
||||
|
||||
QMutex lock_;
|
||||
|
||||
QWaitCondition wait_;
|
||||
|
||||
qint64 job_time_;
|
||||
|
||||
};
|
||||
|
||||
using RenderTicketPtr = std::shared_ptr<RenderTicket>;
|
||||
|
||||
OLIVE_NAMESPACE_EXIT
|
||||
|
||||
Q_DECLARE_METATYPE(OLIVE_NAMESPACE::RenderTicketPtr)
|
||||
|
||||
#endif // RENDERTICKET_H
|
||||
@@ -0,0 +1,89 @@
|
||||
/***
|
||||
|
||||
Olive - Non-Linear Video Editor
|
||||
Copyright (C) 2019 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 "threadticketwatcher.h"
|
||||
|
||||
OLIVE_NAMESPACE_ENTER
|
||||
|
||||
RenderTicketWatcher::RenderTicketWatcher(QObject *parent) :
|
||||
QObject(parent),
|
||||
ticket_(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
void RenderTicketWatcher::SetTicket(RenderTicketPtr ticket)
|
||||
{
|
||||
// Ensure that a ticket has NOT already been set and that this ticket is NOT NULL
|
||||
Q_ASSERT(!ticket_ && ticket);
|
||||
|
||||
ticket_ = ticket;
|
||||
|
||||
QMutexLocker locker(ticket->lock());
|
||||
|
||||
if (ticket_->IsFinished(false)) {
|
||||
locker.unlock();
|
||||
emit Finished();
|
||||
} else {
|
||||
connect(ticket_.get(), &RenderTicket::Finished, this, &RenderTicketWatcher::Finished);
|
||||
}
|
||||
}
|
||||
|
||||
bool RenderTicketWatcher::WasCancelled()
|
||||
{
|
||||
if (ticket_) {
|
||||
return ticket_->WasCancelled();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
bool RenderTicketWatcher::IsFinished()
|
||||
{
|
||||
if (ticket_) {
|
||||
return ticket_->IsFinished();
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void RenderTicketWatcher::WaitForFinished()
|
||||
{
|
||||
if (ticket_) {
|
||||
ticket_->WaitForFinished();
|
||||
}
|
||||
}
|
||||
|
||||
QVariant RenderTicketWatcher::Get()
|
||||
{
|
||||
if (ticket_) {
|
||||
return ticket_->Get();
|
||||
} else {
|
||||
return QVariant();
|
||||
}
|
||||
}
|
||||
|
||||
void RenderTicketWatcher::Cancel()
|
||||
{
|
||||
if (ticket_) {
|
||||
ticket_->Cancel();
|
||||
}
|
||||
}
|
||||
|
||||
OLIVE_NAMESPACE_EXIT
|
||||
@@ -0,0 +1,61 @@
|
||||
/***
|
||||
|
||||
Olive - Non-Linear Video Editor
|
||||
Copyright (C) 2019 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 RENDERTICKETWATCHER_H
|
||||
#define RENDERTICKETWATCHER_H
|
||||
|
||||
#include "threadticket.h"
|
||||
|
||||
OLIVE_NAMESPACE_ENTER
|
||||
|
||||
class RenderTicketWatcher : public QObject
|
||||
{
|
||||
Q_OBJECT
|
||||
public:
|
||||
RenderTicketWatcher(QObject* parent = nullptr);
|
||||
|
||||
RenderTicketPtr GetTicket() const
|
||||
{
|
||||
return ticket_;
|
||||
}
|
||||
|
||||
void SetTicket(RenderTicketPtr ticket);
|
||||
|
||||
void Cancel();
|
||||
|
||||
bool WasCancelled();
|
||||
|
||||
bool IsFinished();
|
||||
|
||||
void WaitForFinished();
|
||||
|
||||
QVariant Get();
|
||||
|
||||
signals:
|
||||
void Finished();
|
||||
|
||||
private:
|
||||
RenderTicketPtr ticket_;
|
||||
|
||||
};
|
||||
|
||||
OLIVE_NAMESPACE_EXIT
|
||||
|
||||
#endif // RENDERTICKETWATCHER_H
|
||||
Reference in New Issue
Block a user