Clone Source; dynamic memory for delay, connection line to origin

This commit is contained in:
Bruno Herbelin
2022-04-16 01:33:41 +02:00
parent 0aed9fc306
commit c043026764
3 changed files with 171 additions and 127 deletions

View File

@@ -23,6 +23,7 @@
#include <glm/gtc/matrix_transform.hpp> #include <glm/gtc/matrix_transform.hpp>
#include "Log.h" #include "Log.h"
#include "defines.h"
#include "Resource.h" #include "Resource.h"
#include "Visitor.h" #include "Visitor.h"
#include "FrameBuffer.h" #include "FrameBuffer.h"
@@ -34,7 +35,7 @@ const char* CloneSource::cloning_provenance_label[2] = { "Original texture", "Po
CloneSource::CloneSource(Source *origin, uint64_t id) : Source(id), origin_(origin), cloningsurface_(nullptr), CloneSource::CloneSource(Source *origin, uint64_t id) : Source(id), origin_(origin), cloningsurface_(nullptr),
read_index_(0), write_index_(0), delay_(0.0), paused_(false), provenance_(CLONE_TEXTURE) to_delete_(nullptr), delay_(0.0), paused_(false), provenance_(CLONE_TEXTURE)
{ {
// initial name copies the origin name: diplucates are namanged in session // initial name copies the origin name: diplucates are namanged in session
name_ = origin->name(); name_ = origin->name();
@@ -43,11 +44,13 @@ CloneSource::CloneSource(Source *origin, uint64_t id) : Source(id), origin_(orig
symbol_ = new Symbol(Symbol::CLONE, glm::vec3(0.75f, 0.75f, 0.01f)); symbol_ = new Symbol(Symbol::CLONE, glm::vec3(0.75f, 0.75f, 0.01f));
symbol_->scale_.y = 1.5f; symbol_->scale_.y = 1.5f;
// init array // init connecting line
stack_.fill(nullptr); connection_ = new DotLine;
elapsed_stack_.fill(0.0); connection_->color = glm::vec4(COLOR_DEFAULT_SOURCE, 0.5f);
timestamps_.fill(0); connection_->target = origin->groups_[View::MIXING]->translation_;
groups_[View::MIXING]->attach(connection_);
// init timer
timer_ = g_timer_new (); timer_ = g_timer_new ();
} }
@@ -57,9 +60,10 @@ CloneSource::~CloneSource()
origin_->clones_.remove(this); origin_->clones_.remove(this);
// delete all frame buffers // delete all frame buffers
for (size_t i = 0; i < stack_.size(); ++i){ while (!images_.empty()) {
if ( stack_[i] != nullptr ) if (images_.front() != nullptr)
delete stack_[i]; delete images_.front();
images_.pop();
} }
if (cloningsurface_) if (cloningsurface_)
@@ -87,16 +91,16 @@ void CloneSource::init()
// frame buffers where to draw frames from the origin source // frame buffers where to draw frames from the origin source
glm::vec3 res = origin_->frame()->resolution(); glm::vec3 res = origin_->frame()->resolution();
for (size_t i = 0; i < stack_.size(); ++i){ images_.push( new FrameBuffer( res, origin_->frame()->use_alpha() ) );
stack_[i] = new FrameBuffer( res, origin_->frame()->use_alpha() ); timestamps_.push( origin_->playtime() );
} elapsed_.push( 0.f );
// set initial texture surface
texturesurface_->setTextureIndex( stack_[read_index_]->texture() );
// activate elapsed-timer // activate elapsed-timer
g_timer_start(timer_); g_timer_start(timer_);
// set initial texture surface
texturesurface_->setTextureIndex( images_.front()->texture() );
// create render Frame buffer matching size of images // create render Frame buffer matching size of images
FrameBuffer *renderbuffer = new FrameBuffer( res, true); FrameBuffer *renderbuffer = new FrameBuffer( res, true);
@@ -131,10 +135,10 @@ void CloneSource::setActive (bool on)
// change visibility of active surface (show preview of origin when inactive) // change visibility of active surface (show preview of origin when inactive)
if (activesurface_) { if (activesurface_) {
if (active_) if (active_ || images_.empty())
activesurface_->setTextureIndex(Resource::getTextureTransparent()); activesurface_->setTextureIndex(Resource::getTextureTransparent());
else else
activesurface_->setTextureIndex(stack_[read_index_]->texture()); activesurface_->setTextureIndex(images_.front()->texture());
} }
} }
} }
@@ -143,44 +147,59 @@ void CloneSource::update(float dt)
{ {
Source::update(dt); Source::update(dt);
if (!paused_ && origin_ && cloningsurface_ != nullptr) { if (origin_) {
if (!paused_ && cloningsurface_ != nullptr) {
// if temporary FBO was pending to be deleted, delete it now
if (to_delete_ != nullptr) {
delete to_delete_;
to_delete_ = nullptr;
}
// What time is it?
double now = g_timer_elapsed (timer_, NULL); double now = g_timer_elapsed (timer_, NULL);
// increment enplacement of write index // is the total buffer of images longer than delay ?
write_index_ = (write_index_+1)%(stack_.size()); if ( !images_.empty() && now - elapsed_.front() > delay_ ) {
elapsed_stack_[write_index_] = now; // remember FBO to be reused if needed (see below) or deleted later
timestamps_[write_index_] = origin_->playtime(); to_delete_ = images_.front();
// remove element from queue (front)
images_.pop();
elapsed_.pop();
timestamps_.pop();
}
// CLONE_RENDER : blit rendered framebuffer in the stack // add image to queue to accumulate buffer images until delay reached
if ( images_.empty() || now - elapsed_.front() < delay_ + (dt * 0.001) ) {
// create a FBO if none can be reused (from above)
if (to_delete_ == nullptr)
to_delete_ = new FrameBuffer( origin_->frame()->resolution(), origin_->frame()->use_alpha() );
// add element to queue (back)
images_.push( to_delete_ );
elapsed_.push( now );
timestamps_.push( origin_->playtime() );
// to_delete_ FBO is now used, should not be deleted
to_delete_ = nullptr;
}
// CLONE_RENDER : blit rendered framebuffer in the newest image (back)
if (provenance_ == CLONE_RENDER) if (provenance_ == CLONE_RENDER)
origin_->frame()->blit(stack_[write_index_]); origin_->frame()->blit(images_.back());
// CLONE_TEXTURE : render origin texture in the stack // CLONE_TEXTURE : render origin texture in the the newest image (back)
else { else {
stack_[write_index_]->begin(); images_.back()->begin();
cloningsurface_->draw(glm::identity<glm::mat4>(), stack_[write_index_]->projection()); cloningsurface_->draw(glm::identity<glm::mat4>(), images_.back()->projection());
stack_[write_index_]->end(); images_.back()->end();
} }
// define emplacement of read index // update the source surface to be rendered with the oldest image (front)
if (delay_ < 0.001) texturesurface_->setTextureIndex( images_.front()->texture() );
// minimal difference if no delay
read_index_ = write_index_;
else
{
// starting where we are at, get the next index that satisfies the delay
size_t previous_index = read_index_;
while ( now - elapsed_stack_[read_index_] > delay_) {
// usually, one frame increment suffice
read_index_ = (read_index_ + 1 )%(stack_.size());
// break the loop if running infinite (never happens)
if (previous_index == read_index_)
break;
}
} }
// update the source surface to be rendered // update connection line target to position of origin source
texturesurface_->setTextureIndex( stack_[read_index_]->texture() ); connection_->target = glm::inverse( GlmToolkit::transform(groups_[View::MIXING]->translation_, glm::vec3(0), groups_[View::MIXING]->scale_) ) *
glm::vec4(origin_->groups_[View::MIXING]->translation_, 1.f);
} }
} }
@@ -212,22 +231,46 @@ bool CloneSource::playable () const
void CloneSource::replay() void CloneSource::replay()
{ {
// clear to_delete_ FBO if pending
if (to_delete_ != nullptr) {
delete to_delete_;
to_delete_ = nullptr;
}
// remove all images except the one in the back (newest)
while (images_.size() > 1) {
// do not delete immediately the (oldest) front image (the FBO is currently displayed)
if (to_delete_ == nullptr)
to_delete_ = images_.front();
// delete other FBO (unused)
else if (images_.front() != nullptr)
delete images_.front();
images_.pop();
}
// remove all timing
while (!elapsed_.empty())
elapsed_.pop();
// reset elapsed timer to 0
g_timer_reset(timer_); g_timer_reset(timer_);
elapsed_stack_.fill(0.0); elapsed_.push(0.);
write_index_ = 0;
read_index_ = 1; // remove all timestamps
while (!timestamps_.empty())
timestamps_.pop();
timestamps_.push(0);
} }
guint64 CloneSource::playtime () const guint64 CloneSource::playtime () const
{ {
return timestamps_[read_index_]; return timestamps_.front();
} }
uint CloneSource::texture() const uint CloneSource::texture() const
{ {
if (cloningsurface_ != nullptr) if (cloningsurface_ != nullptr && !images_.empty())
return stack_[read_index_]->texture(); return images_.front()->texture();
else else
return Resource::getTextureTransparent(); return Resource::getTextureTransparent();
} }

View File

@@ -1,9 +1,7 @@
#ifndef CLONESOURCE_H #ifndef CLONESOURCE_H
#define CLONESOURCE_H #define CLONESOURCE_H
#include <array> #include <queue>
#define DELAY_ARRAY_SIZE 130
#include "Source.h" #include "Source.h"
@@ -54,20 +52,23 @@ protected:
void init() override; void init() override;
Source *origin_; Source *origin_;
// cloning & stack of past frames // cloning & queue of past frames
std::array<FrameBuffer *, DELAY_ARRAY_SIZE> stack_; std::queue<FrameBuffer *> images_;
Surface *cloningsurface_; Surface *cloningsurface_;
size_t read_index_, write_index_; FrameBuffer *to_delete_;
// time management // time management
GTimer *timer_; GTimer *timer_;
std::array<double, DELAY_ARRAY_SIZE> elapsed_stack_; std::queue<double> elapsed_;
std::array<guint64, DELAY_ARRAY_SIZE> timestamps_; std::queue<guint64> timestamps_;
double delay_; double delay_;
// control // control
bool paused_; bool paused_;
CloneSourceProvenance provenance_; CloneSourceProvenance provenance_;
// connecting line
class DotLine *connection_;
}; };

View File

@@ -12,70 +12,70 @@ property uchar alpha
element face 160 element face 160
property list uchar uint vertex_indices property list uchar uint vertex_indices
end_header end_header
0.036901 -0.024656 0.000000 255 255 255 20 0.036901 -0.024656 0.000000 255 255 255 200
0.030366 -0.030365 0.000000 255 255 255 20 0.030366 -0.030365 0.000000 255 255 255 200
0.035706 -0.023858 0.000000 255 255 255 20 0.035706 -0.023858 0.000000 255 255 255 200
-0.043527 0.008658 0.000000 255 255 255 20 -0.043527 0.008658 0.000000 255 255 255 200
-0.042944 0.000000 0.000000 255 255 255 20 -0.042944 0.000000 0.000000 255 255 255 200
-0.044380 0.000000 0.000000 255 255 255 20 -0.044380 0.000000 0.000000 255 255 255 200
0.039675 -0.016434 0.000000 255 255 255 20 0.039675 -0.016434 0.000000 255 255 255 200
0.041002 -0.016983 0.000000 255 255 255 20 0.041002 -0.016983 0.000000 255 255 255 200
-0.042118 -0.008378 0.000000 255 255 255 20 -0.042118 -0.008378 0.000000 255 255 255 200
-0.043527 -0.008658 0.000000 255 255 255 20 -0.043527 -0.008658 0.000000 255 255 255 200
0.043527 -0.008658 0.000000 255 255 255 20 0.043527 -0.008658 0.000000 255 255 255 200
0.042118 -0.008378 0.000000 255 255 255 20 0.042118 -0.008378 0.000000 255 255 255 200
-0.039675 -0.016434 0.000000 255 255 255 20 -0.039675 -0.016434 0.000000 255 255 255 200
-0.041002 -0.016983 0.000000 255 255 255 20 -0.041002 -0.016983 0.000000 255 255 255 200
0.044380 0.000000 0.000000 255 255 255 20 0.044380 0.000000 0.000000 255 255 255 200
0.042943 0.000000 0.000000 255 255 255 20 0.042943 0.000000 0.000000 255 255 255 200
-0.035706 -0.023858 0.000000 255 255 255 20 -0.035706 -0.023858 0.000000 255 255 255 200
-0.036901 -0.024656 0.000000 255 255 255 20 -0.036901 -0.024656 0.000000 255 255 255 200
0.043527 0.008658 0.000000 255 255 255 20 0.043527 0.008658 0.000000 255 255 255 200
0.042118 0.008378 0.000000 255 255 255 20 0.042118 0.008378 0.000000 255 255 255 200
-0.030366 -0.030366 0.000000 255 255 255 20 -0.030366 -0.030366 0.000000 255 255 255 200
-0.031381 -0.031381 0.000000 255 255 255 20 -0.031381 -0.031381 0.000000 255 255 255 200
0.041002 0.016984 0.000000 255 255 255 20 0.041002 0.016984 0.000000 255 255 255 200
0.039675 0.016434 0.000000 255 255 255 20 0.039675 0.016434 0.000000 255 255 255 200
-0.023858 -0.035706 0.000000 255 255 255 20 -0.023858 -0.035706 0.000000 255 255 255 200
-0.024656 -0.036900 0.000000 255 255 255 20 -0.024656 -0.036900 0.000000 255 255 255 200
0.035706 0.023858 0.000000 255 255 255 20 0.035706 0.023858 0.000000 255 255 255 200
0.036901 0.024656 0.000000 255 255 255 20 0.036901 0.024656 0.000000 255 255 255 200
-0.016434 -0.039675 0.000000 255 255 255 20 -0.016434 -0.039675 0.000000 255 255 255 200
-0.016984 -0.041002 0.000000 255 255 255 20 -0.016984 -0.041002 0.000000 255 255 255 200
-0.008658 0.043527 0.000000 255 255 255 20 -0.008658 0.043527 0.000000 255 255 255 200
-0.000000 0.042944 0.000000 255 255 255 20 -0.000000 0.042944 0.000000 255 255 255 200
-0.008378 0.042119 0.000000 255 255 255 20 -0.008378 0.042119 0.000000 255 255 255 200
0.031381 0.031382 0.000000 255 255 255 20 0.031381 0.031382 0.000000 255 255 255 200
0.030366 0.030366 0.000000 255 255 255 20 0.030366 0.030366 0.000000 255 255 255 200
-0.008378 -0.042118 0.000000 255 255 255 20 -0.008378 -0.042118 0.000000 255 255 255 200
-0.008658 -0.043527 0.000000 255 255 255 20 -0.008658 -0.043527 0.000000 255 255 255 200
-0.016434 0.039675 0.000000 255 255 255 20 -0.016434 0.039675 0.000000 255 255 255 200
-0.016984 0.041002 0.000000 255 255 255 20 -0.016984 0.041002 0.000000 255 255 255 200
0.024656 0.036901 0.000000 255 255 255 20 0.024656 0.036901 0.000000 255 255 255 200
0.023858 0.035706 0.000000 255 255 255 20 0.023858 0.035706 0.000000 255 255 255 200
-0.000000 -0.042943 0.000000 255 255 255 20 -0.000000 -0.042943 0.000000 255 255 255 200
-0.000000 -0.044380 0.000000 255 255 255 20 -0.000000 -0.044380 0.000000 255 255 255 200
-0.023858 0.035706 0.000000 255 255 255 20 -0.023858 0.035706 0.000000 255 255 255 200
-0.024656 0.036901 0.000000 255 255 255 20 -0.024656 0.036901 0.000000 255 255 255 200
0.016983 0.041002 0.000000 255 255 255 20 0.016983 0.041002 0.000000 255 255 255 200
0.016434 0.039675 0.000000 255 255 255 20 0.016434 0.039675 0.000000 255 255 255 200
0.008378 -0.042118 0.000000 255 255 255 20 0.008378 -0.042118 0.000000 255 255 255 200
0.008658 -0.043527 0.000000 255 255 255 20 0.008658 -0.043527 0.000000 255 255 255 200
-0.031381 0.031382 0.000000 255 255 255 20 -0.031381 0.031382 0.000000 255 255 255 200
-0.030366 0.030366 0.000000 255 255 255 20 -0.030366 0.030366 0.000000 255 255 255 200
0.008658 0.043527 0.000000 255 255 255 20 0.008658 0.043527 0.000000 255 255 255 200
0.008378 0.042119 0.000000 255 255 255 20 0.008378 0.042119 0.000000 255 255 255 200
0.016983 -0.041002 0.000000 255 255 255 20 0.016983 -0.041002 0.000000 255 255 255 200
0.016434 -0.039674 0.000000 255 255 255 20 0.016434 -0.039674 0.000000 255 255 255 200
-0.035706 0.023858 0.000000 255 255 255 20 -0.035706 0.023858 0.000000 255 255 255 200
-0.036901 0.024656 0.000000 255 255 255 20 -0.036901 0.024656 0.000000 255 255 255 200
-0.000000 0.044380 0.000000 255 255 255 20 -0.000000 0.044380 0.000000 255 255 255 200
0.023858 -0.035706 0.000000 255 255 255 20 0.023858 -0.035706 0.000000 255 255 255 200
0.024656 -0.036900 0.000000 255 255 255 20 0.024656 -0.036900 0.000000 255 255 255 200
-0.039675 0.016434 0.000000 255 255 255 20 -0.039675 0.016434 0.000000 255 255 255 200
-0.041002 0.016984 0.000000 255 255 255 20 -0.041002 0.016984 0.000000 255 255 255 200
0.031381 -0.031381 0.000000 255 255 255 20 0.031381 -0.031381 0.000000 255 255 255 200
-0.042118 0.008378 0.000000 255 255 255 20 -0.042118 0.008378 0.000000 255 255 255 200
0.034992 -0.023381 0.000000 255 255 255 255 0.034992 -0.023381 0.000000 255 255 255 255
0.038881 -0.016105 0.000000 255 255 255 255 0.038881 -0.016105 0.000000 255 255 255 255
-0.041276 -0.008210 0.000000 255 255 255 255 -0.041276 -0.008210 0.000000 255 255 255 255