renderer: use shared backend when caching
I think there was an earlier commit with a similar name but turns out I'd only done foundational work in that commit and never actually properly set it up. Of course once I did, there were several issues that needed fixing to make it work correctly, but now it works as expected. Heavily optimizes larger projects by allowing cache jobs to only copy what has changed.
This commit is contained in:
+5
-3
@@ -1007,7 +1007,7 @@ void NodeInput::set_is_keyframable(bool k)
|
|||||||
keyframable_ = k;
|
keyframable_ = k;
|
||||||
}
|
}
|
||||||
|
|
||||||
void NodeInput::CopyValues(NodeInput *source, NodeInput *dest, bool include_connections)
|
void NodeInput::CopyValues(NodeInput *source, NodeInput *dest, bool include_connections, bool traverse_arrays)
|
||||||
{
|
{
|
||||||
Q_ASSERT(source->id() == dest->id());
|
Q_ASSERT(source->id() == dest->id());
|
||||||
|
|
||||||
@@ -1037,8 +1037,10 @@ void NodeInput::CopyValues(NodeInput *source, NodeInput *dest, bool include_conn
|
|||||||
|
|
||||||
dst_array->SetSize(src_array->GetSize());
|
dst_array->SetSize(src_array->GetSize());
|
||||||
|
|
||||||
for (int i=0;i<dst_array->GetSize();i++) {
|
if (traverse_arrays) {
|
||||||
CopyValues(src_array->At(i), dst_array->At(i), include_connections);
|
for (int i=0;i<dst_array->GetSize();i++) {
|
||||||
|
CopyValues(src_array->At(i), dst_array->At(i), include_connections);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
+1
-1
@@ -235,7 +235,7 @@ public:
|
|||||||
/**
|
/**
|
||||||
* @brief Copy all values including keyframe information and connections from another NodeInput
|
* @brief Copy all values including keyframe information and connections from another NodeInput
|
||||||
*/
|
*/
|
||||||
static void CopyValues(NodeInput* source, NodeInput* dest, bool include_connections = true);
|
static void CopyValues(NodeInput* source, NodeInput* dest, bool include_connections = true, bool traverse_arrays = true);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Set an arbitrary property on this input to influence a UI representation's behavior
|
* @brief Set an arbitrary property on this input to influence a UI representation's behavior
|
||||||
|
|||||||
@@ -113,19 +113,20 @@ void RenderBackend::ClearVideoQueue()
|
|||||||
render_queue_.clear();
|
render_queue_.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
QFuture<QVector<QByteArray> > RenderBackend::Hash(const QVector<rational> ×)
|
RenderTicketPtr RenderBackend::Hash(const QVector<rational> ×)
|
||||||
{
|
{
|
||||||
return QtConcurrent::run(&pool_, [this](const QVector<rational> &t){
|
if (!viewer_node_) {
|
||||||
QVector<QByteArray> hashes(t.size());
|
return nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
for (int i=0;i<hashes.size();i++) {
|
RenderTicketPtr ticket = std::make_shared<RenderTicket>(RenderTicket::kTypeHash,
|
||||||
hashes[i] = HashNode(copied_viewer_node_->texture_input()->get_connected_node(),
|
QVariant::fromValue(times));
|
||||||
video_params_,
|
|
||||||
t.at(i));
|
|
||||||
}
|
|
||||||
|
|
||||||
return hashes;
|
render_queue_.push_back(ticket);
|
||||||
}, times);
|
|
||||||
|
QMetaObject::invokeMethod(this, "RunNextJob", Qt::QueuedConnection);
|
||||||
|
|
||||||
|
return ticket;
|
||||||
}
|
}
|
||||||
|
|
||||||
RenderTicketPtr RenderBackend::RenderFrame(const rational &time)
|
RenderTicketPtr RenderBackend::RenderFrame(const rational &time)
|
||||||
@@ -135,7 +136,7 @@ RenderTicketPtr RenderBackend::RenderFrame(const rational &time)
|
|||||||
}
|
}
|
||||||
|
|
||||||
RenderTicketPtr ticket = std::make_shared<RenderTicket>(RenderTicket::kTypeVideo,
|
RenderTicketPtr ticket = std::make_shared<RenderTicket>(RenderTicket::kTypeVideo,
|
||||||
TimeRange(time, time));
|
QVariant::fromValue(time));
|
||||||
|
|
||||||
render_queue_.push_back(ticket);
|
render_queue_.push_back(ticket);
|
||||||
|
|
||||||
@@ -151,7 +152,7 @@ RenderTicketPtr RenderBackend::RenderAudio(const TimeRange &r)
|
|||||||
}
|
}
|
||||||
|
|
||||||
RenderTicketPtr ticket = std::make_shared<RenderTicket>(RenderTicket::kTypeAudio,
|
RenderTicketPtr ticket = std::make_shared<RenderTicket>(RenderTicket::kTypeAudio,
|
||||||
r);
|
QVariant::fromValue(r));
|
||||||
|
|
||||||
render_queue_.push_back(ticket);
|
render_queue_.push_back(ticket);
|
||||||
|
|
||||||
@@ -215,6 +216,11 @@ void RenderBackend::NodeGraphChanged(NodeInput *source)
|
|||||||
for (int i=0; i<graph_update_queue_.size(); i++) {
|
for (int i=0; i<graph_update_queue_.size(); i++) {
|
||||||
NodeInput* queued_input = graph_update_queue_.at(i);
|
NodeInput* queued_input = graph_update_queue_.at(i);
|
||||||
|
|
||||||
|
// If this input is already queued, nothing to be done
|
||||||
|
if (source == queued_input) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// Check if this dependency graph is already queued
|
// Check if this dependency graph is already queued
|
||||||
if (source->parentNode()->OutputsTo(queued_input, true)) {
|
if (source->parentNode()->OutputsTo(queued_input, true)) {
|
||||||
// In which case, no further copy is necessary
|
// In which case, no further copy is necessary
|
||||||
@@ -300,7 +306,7 @@ void RenderBackend::RunNextJob()
|
|||||||
|
|
||||||
worker->SetVideoParams(video_params_);
|
worker->SetVideoParams(video_params_);
|
||||||
worker->SetAudioParams(audio_params_);
|
worker->SetAudioParams(audio_params_);
|
||||||
worker->SetVideoDownloadMatrix(video_download_matrix_);
|
worker->SetVideoDownloadMatrix(video_dwnload_matrix_);
|
||||||
worker->SetRenderMode(render_mode_);
|
worker->SetRenderMode(render_mode_);
|
||||||
if (preview_job_time_) {
|
if (preview_job_time_) {
|
||||||
worker->EnablePreviewGeneration(viewer_node_->audio_playback_cache(), preview_job_time_);
|
worker->EnablePreviewGeneration(viewer_node_->audio_playback_cache(), preview_job_time_);
|
||||||
@@ -311,13 +317,21 @@ void RenderBackend::RunNextJob()
|
|||||||
render_queue_.pop_front();
|
render_queue_.pop_front();
|
||||||
|
|
||||||
switch (ticket->GetType()) {
|
switch (ticket->GetType()) {
|
||||||
|
case RenderTicket::kTypeHash:
|
||||||
|
QtConcurrent::run(&pool_,
|
||||||
|
worker,
|
||||||
|
&RenderWorker::Hash,
|
||||||
|
ticket,
|
||||||
|
copied_viewer_node_,
|
||||||
|
ticket->GetTime().value<QVector<rational> >());
|
||||||
|
break;
|
||||||
case RenderTicket::kTypeVideo:
|
case RenderTicket::kTypeVideo:
|
||||||
QtConcurrent::run(&pool_,
|
QtConcurrent::run(&pool_,
|
||||||
worker,
|
worker,
|
||||||
&RenderWorker::RenderFrame,
|
&RenderWorker::RenderFrame,
|
||||||
ticket,
|
ticket,
|
||||||
copied_viewer_node_,
|
copied_viewer_node_,
|
||||||
ticket->GetTime().in());
|
ticket->GetTime().value<rational>());
|
||||||
break;
|
break;
|
||||||
case RenderTicket::kTypeAudio:
|
case RenderTicket::kTypeAudio:
|
||||||
QtConcurrent::run(&pool_,
|
QtConcurrent::run(&pool_,
|
||||||
@@ -325,7 +339,7 @@ void RenderBackend::RunNextJob()
|
|||||||
&RenderWorker::RenderAudio,
|
&RenderWorker::RenderAudio,
|
||||||
ticket,
|
ticket,
|
||||||
copied_viewer_node_,
|
copied_viewer_node_,
|
||||||
ticket->GetTime());
|
ticket->GetTime().value<TimeRange>());
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -337,27 +351,25 @@ void RenderBackend::RunNextJob()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//#define PRINT_UPDATE_QUEUE_INFO
|
||||||
void RenderBackend::ProcessUpdateQueue()
|
void RenderBackend::ProcessUpdateQueue()
|
||||||
{
|
{
|
||||||
|
#ifdef PRINT_UPDATE_QUEUE_INFO
|
||||||
|
qint64 t = QDateTime::currentMSecsSinceEpoch();
|
||||||
|
qDebug() << "Processing update queue of" << graph_update_queue_.size() << "elements:";
|
||||||
|
#endif
|
||||||
|
|
||||||
while (!graph_update_queue_.isEmpty()) {
|
while (!graph_update_queue_.isEmpty()) {
|
||||||
CopyNodeInputValue(graph_update_queue_.takeFirst());
|
NodeInput* i = graph_update_queue_.takeFirst();
|
||||||
}
|
#ifdef PRINT_UPDATE_QUEUE_INFO
|
||||||
}
|
qDebug() << " " << i->parentNode()->id() << i->id();
|
||||||
|
#endif
|
||||||
QByteArray RenderBackend::HashNode(const Node *n, const VideoParams ¶ms, const rational &time)
|
CopyNodeInputValue(i);
|
||||||
{
|
|
||||||
QCryptographicHash hasher(QCryptographicHash::Sha1);
|
|
||||||
|
|
||||||
// Embed video parameters into this hash
|
|
||||||
hasher.addData(reinterpret_cast<const char*>(¶ms.effective_width()), sizeof(int));
|
|
||||||
hasher.addData(reinterpret_cast<const char*>(¶ms.effective_height()), sizeof(int));
|
|
||||||
hasher.addData(reinterpret_cast<const char*>(¶ms.format()), sizeof(PixelFormat::Format));
|
|
||||||
|
|
||||||
if (n) {
|
|
||||||
n->Hash(hasher, time);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return hasher.result();
|
#ifdef PRINT_UPDATE_QUEUE_INFO
|
||||||
|
qDebug() << "Update queue took:" << (QDateTime::currentMSecsSinceEpoch() - t);
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void RenderBackend::WorkerFinished()
|
void RenderBackend::WorkerFinished()
|
||||||
@@ -386,6 +398,7 @@ void RenderBackend::CopyNodeInputValue(NodeInput *input)
|
|||||||
// Copy the standard/keyframe values between these two inputs
|
// Copy the standard/keyframe values between these two inputs
|
||||||
NodeInput::CopyValues(input,
|
NodeInput::CopyValues(input,
|
||||||
our_copy,
|
our_copy,
|
||||||
|
false,
|
||||||
false);
|
false);
|
||||||
|
|
||||||
// Handle connections
|
// Handle connections
|
||||||
|
|||||||
@@ -69,12 +69,10 @@ public:
|
|||||||
|
|
||||||
void ProcessUpdateQueue();
|
void ProcessUpdateQueue();
|
||||||
|
|
||||||
static QByteArray HashNode(const Node* n, const VideoParams& params, const rational& time);
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Asynchronously generate a hash at a given time
|
* @brief Asynchronously generate a hash at a given time
|
||||||
*/
|
*/
|
||||||
QFuture<QVector<QByteArray> > Hash(const QVector<rational> ×);
|
RenderTicketPtr Hash(const QVector<rational> ×);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Asynchronously generate a frame at a given time
|
* @brief Asynchronously generate a frame at a given time
|
||||||
|
|||||||
@@ -22,7 +22,7 @@
|
|||||||
|
|
||||||
OLIVE_NAMESPACE_ENTER
|
OLIVE_NAMESPACE_ENTER
|
||||||
|
|
||||||
RenderTicket::RenderTicket(Type type, const TimeRange &time) :
|
RenderTicket::RenderTicket(Type type, const QVariant &time) :
|
||||||
finished_(false),
|
finished_(false),
|
||||||
cancelled_(false),
|
cancelled_(false),
|
||||||
time_(time),
|
time_(time),
|
||||||
|
|||||||
@@ -35,13 +35,14 @@ class RenderTicket : public QObject
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
public:
|
public:
|
||||||
enum Type {
|
enum Type {
|
||||||
|
kTypeHash,
|
||||||
kTypeVideo,
|
kTypeVideo,
|
||||||
kTypeAudio
|
kTypeAudio
|
||||||
};
|
};
|
||||||
|
|
||||||
RenderTicket(Type type, const TimeRange& time);
|
RenderTicket(Type type, const QVariant& time);
|
||||||
|
|
||||||
const TimeRange& GetTime() const
|
const QVariant& GetTime() const
|
||||||
{
|
{
|
||||||
return time_;
|
return time_;
|
||||||
}
|
}
|
||||||
@@ -82,7 +83,7 @@ private:
|
|||||||
|
|
||||||
QWaitCondition wait_;
|
QWaitCondition wait_;
|
||||||
|
|
||||||
TimeRange time_;
|
QVariant time_;
|
||||||
|
|
||||||
Type type_;
|
Type type_;
|
||||||
|
|
||||||
|
|||||||
@@ -21,13 +21,13 @@
|
|||||||
#include "renderworker.h"
|
#include "renderworker.h"
|
||||||
|
|
||||||
#include <QDir>
|
#include <QDir>
|
||||||
|
#include <QThread>
|
||||||
|
|
||||||
#include "audio/audiovisualwaveform.h"
|
#include "audio/audiovisualwaveform.h"
|
||||||
#include "common/functiontimer.h"
|
#include "common/functiontimer.h"
|
||||||
#include "config/config.h"
|
#include "config/config.h"
|
||||||
#include "node/block/clip/clip.h"
|
#include "node/block/clip/clip.h"
|
||||||
#include "task/conform/conform.h"
|
#include "task/conform/conform.h"
|
||||||
#include "renderbackend.h"
|
|
||||||
|
|
||||||
OLIVE_NAMESPACE_ENTER
|
OLIVE_NAMESPACE_ENTER
|
||||||
|
|
||||||
@@ -40,6 +40,37 @@ RenderWorker::RenderWorker(RenderBackend* parent) :
|
|||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void RenderWorker::Hash(RenderTicketPtr ticket, ViewerOutput *viewer, const QVector<rational> ×)
|
||||||
|
{
|
||||||
|
QVector<QByteArray> hashes(times.size());
|
||||||
|
|
||||||
|
for (int i=0;i<hashes.size();i++) {
|
||||||
|
hashes[i] = HashNode(viewer->texture_input()->get_connected_node(),
|
||||||
|
video_params_,
|
||||||
|
times.at(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
ticket->Finish(QVariant::fromValue(hashes));
|
||||||
|
|
||||||
|
emit FinishedJob();
|
||||||
|
}
|
||||||
|
|
||||||
|
QByteArray RenderWorker::HashNode(const Node *n, const VideoParams ¶ms, const rational &time)
|
||||||
|
{
|
||||||
|
QCryptographicHash hasher(QCryptographicHash::Sha1);
|
||||||
|
|
||||||
|
// Embed video parameters into this hash
|
||||||
|
hasher.addData(reinterpret_cast<const char*>(¶ms.effective_width()), sizeof(int));
|
||||||
|
hasher.addData(reinterpret_cast<const char*>(¶ms.effective_height()), sizeof(int));
|
||||||
|
hasher.addData(reinterpret_cast<const char*>(¶ms.format()), sizeof(PixelFormat::Format));
|
||||||
|
|
||||||
|
if (n) {
|
||||||
|
n->Hash(hasher, time);
|
||||||
|
}
|
||||||
|
|
||||||
|
return hasher.result();
|
||||||
|
}
|
||||||
|
|
||||||
void RenderWorker::RenderFrame(RenderTicketPtr ticket, ViewerOutput* viewer, const rational &time)
|
void RenderWorker::RenderFrame(RenderTicketPtr ticket, ViewerOutput* viewer, const rational &time)
|
||||||
{
|
{
|
||||||
NodeValueTable table = ProcessInput(viewer->texture_input(),
|
NodeValueTable table = ProcessInput(viewer->texture_input(),
|
||||||
@@ -237,7 +268,7 @@ QVariant RenderWorker::ProcessFrameGeneration(const Node* node, const GenerateJo
|
|||||||
QVariant RenderWorker::GetCachedFrame(const Node* node, const rational& time)
|
QVariant RenderWorker::GetCachedFrame(const Node* node, const rational& time)
|
||||||
{
|
{
|
||||||
if (node->id() == QStringLiteral("org.olivevideoeditor.Olive.videoinput")) {
|
if (node->id() == QStringLiteral("org.olivevideoeditor.Olive.videoinput")) {
|
||||||
QByteArray hash = RenderBackend::HashNode(node, video_params(), time);
|
QByteArray hash = HashNode(node, video_params(), time);
|
||||||
|
|
||||||
QString fn = FrameHashCache::CachePathName(hash);
|
QString fn = FrameHashCache::CachePathName(hash);
|
||||||
|
|
||||||
|
|||||||
@@ -84,6 +84,8 @@ public:
|
|||||||
preview_job_time_ = job_time;
|
preview_job_time_ = job_time;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Hash(RenderTicketPtr ticket, ViewerOutput* viewer, const QVector<rational>& times);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Render the frame at this time
|
* @brief Render the frame at this time
|
||||||
*
|
*
|
||||||
@@ -146,6 +148,8 @@ signals:
|
|||||||
private:
|
private:
|
||||||
DecoderPtr ResolveDecoderFromInput(StreamPtr stream);
|
DecoderPtr ResolveDecoderFromInput(StreamPtr stream);
|
||||||
|
|
||||||
|
static QByteArray HashNode(const Node* n, const VideoParams& params, const rational& time);
|
||||||
|
|
||||||
RenderBackend* parent_;
|
RenderBackend* parent_;
|
||||||
|
|
||||||
VideoParams video_params_;
|
VideoParams video_params_;
|
||||||
|
|||||||
@@ -107,11 +107,13 @@ void RenderTask::Render(const TimeRangeList& video_range,
|
|||||||
|
|
||||||
total_length += video_frame_sz * times.size();
|
total_length += video_frame_sz * times.size();
|
||||||
|
|
||||||
QFuture<QVector<QByteArray> > hash_future = backend_->Hash(times);
|
RenderTicketPtr hash_future = backend_->Hash(times);
|
||||||
hashes = hash_future.result();
|
hashes = hash_future->Get().value<QVector<QByteArray> >();
|
||||||
|
|
||||||
for (int i=0;i<times.size();i++) {
|
if (!hash_future->WasCancelled()) {
|
||||||
frame_queue.push_back({times.at(i), hashes.at(i)});
|
for (int i=0;i<times.size();i++) {
|
||||||
|
frame_queue.push_back({times.at(i), hashes.at(i)});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -214,7 +214,7 @@ void ViewerWidget::ConnectNodeInternal(ViewerOutput *n)
|
|||||||
|
|
||||||
void ViewerWidget::DisconnectNodeInternal(ViewerOutput *n)
|
void ViewerWidget::DisconnectNodeInternal(ViewerOutput *n)
|
||||||
{
|
{
|
||||||
Pause();
|
PauseInternal();
|
||||||
|
|
||||||
if (cache_background_task_ == our_cache_background_task_) {
|
if (cache_background_task_ == our_cache_background_task_) {
|
||||||
StopAllBackgroundCacheTasks(true);
|
StopAllBackgroundCacheTasks(true);
|
||||||
@@ -646,7 +646,8 @@ RenderTicketPtr ViewerWidget::GetFrame(const rational &t, bool clear_render_queu
|
|||||||
// Frame has been cached, grab the frame
|
// Frame has been cached, grab the frame
|
||||||
QString cache_fn = GetConnectedNode()->video_frame_cache()->CachePathName(cached_hash);
|
QString cache_fn = GetConnectedNode()->video_frame_cache()->CachePathName(cached_hash);
|
||||||
|
|
||||||
RenderTicketPtr ticket = std::make_shared<RenderTicket>(RenderTicket::kTypeVideo, TimeRange(t, t));
|
RenderTicketPtr ticket = std::make_shared<RenderTicket>(RenderTicket::kTypeVideo,
|
||||||
|
QVariant::fromValue(t));
|
||||||
QtConcurrent::run(DecodeCachedImage, ticket, cache_fn, t);
|
QtConcurrent::run(DecodeCachedImage, ticket, cache_fn, t);
|
||||||
return ticket;
|
return ticket;
|
||||||
|
|
||||||
@@ -875,10 +876,7 @@ void ViewerWidget::StartBackgroundCaching()
|
|||||||
cache_wait_timer_.start();
|
cache_wait_timer_.start();
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
cache_background_task_ = new CacheTask(GetConnectedNode(),
|
cache_background_task_ = new CacheTask(renderer_, false);
|
||||||
GetConnectedNode()->video_params(),
|
|
||||||
GetConnectedNode()->audio_params(),
|
|
||||||
false);
|
|
||||||
|
|
||||||
our_cache_background_task_ = cache_background_task_;
|
our_cache_background_task_ = cache_background_task_;
|
||||||
|
|
||||||
@@ -1223,7 +1221,7 @@ void ViewerWidget::ViewerInvalidatedRange()
|
|||||||
StopAllBackgroundCacheTasks(false);
|
StopAllBackgroundCacheTasks(false);
|
||||||
|
|
||||||
if (!(qApp->mouseButtons() & Qt::LeftButton)) {
|
if (!(qApp->mouseButtons() & Qt::LeftButton)) {
|
||||||
StartBackgroundCaching();
|
cache_wait_timer_.start();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user