render: implemented frame pool
Optimization
This commit is contained in:
+27
-3
@@ -26,14 +26,22 @@
|
|||||||
#include <QtMath>
|
#include <QtMath>
|
||||||
|
|
||||||
#include "common/oiioutils.h"
|
#include "common/oiioutils.h"
|
||||||
|
#include "render/framemanager.h"
|
||||||
|
|
||||||
namespace olive {
|
namespace olive {
|
||||||
|
|
||||||
Frame::Frame() :
|
Frame::Frame() :
|
||||||
|
data_(nullptr),
|
||||||
|
data_size_(0),
|
||||||
timestamp_(0)
|
timestamp_(0)
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Frame::~Frame()
|
||||||
|
{
|
||||||
|
destroy();
|
||||||
|
}
|
||||||
|
|
||||||
FramePtr Frame::Create()
|
FramePtr Frame::Create()
|
||||||
{
|
{
|
||||||
return std::make_shared<Frame>();
|
return std::make_shared<Frame>();
|
||||||
@@ -66,7 +74,7 @@ Color Frame::get_pixel(int x, int y) const
|
|||||||
|
|
||||||
int byte_offset = y * linesize_bytes() + x * video_params().GetBytesPerPixel();
|
int byte_offset = y * linesize_bytes() + x * video_params().GetBytesPerPixel();
|
||||||
|
|
||||||
return Color(data_.data() + byte_offset, video_params().format(), video_params().channel_count());
|
return Color(reinterpret_cast<const char*>(data_ + byte_offset), video_params().format(), video_params().channel_count());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Frame::contains_pixel(int x, int y) const
|
bool Frame::contains_pixel(int x, int y) const
|
||||||
@@ -82,7 +90,7 @@ void Frame::set_pixel(int x, int y, const Color &c)
|
|||||||
|
|
||||||
int byte_offset = y * linesize_bytes() + x * video_params().GetBytesPerPixel();
|
int byte_offset = y * linesize_bytes() + x * video_params().GetBytesPerPixel();
|
||||||
|
|
||||||
c.toData(data_.data() + byte_offset, video_params().format(), video_params().channel_count());
|
c.toData(reinterpret_cast<char*>(data_ + byte_offset), video_params().format(), video_params().channel_count());
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Frame::allocate()
|
bool Frame::allocate()
|
||||||
@@ -93,11 +101,27 @@ bool Frame::allocate()
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
data_.resize(VideoParams::GetBufferSize(linesize_, height(), params_.format(), params_.channel_count()));
|
if (is_allocated()) {
|
||||||
|
// Already allocated
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
data_size_ = VideoParams::GetBufferSize(linesize_, height(), params_.format(), params_.channel_count());
|
||||||
|
data_ = FrameManager::Allocate(data_size_);
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Frame::destroy()
|
||||||
|
{
|
||||||
|
if (is_allocated()) {
|
||||||
|
FrameManager::Deallocate(data_size_, data_);
|
||||||
|
|
||||||
|
data_size_ = 0;
|
||||||
|
data_ = nullptr;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
FramePtr Frame::convert(VideoParams::Format format) const
|
FramePtr Frame::convert(VideoParams::Format format) const
|
||||||
{
|
{
|
||||||
// Create new params with destination format
|
// Create new params with destination format
|
||||||
|
|||||||
+12
-9
@@ -24,6 +24,7 @@
|
|||||||
#include <memory>
|
#include <memory>
|
||||||
#include <QVector>
|
#include <QVector>
|
||||||
|
|
||||||
|
#include "common/define.h"
|
||||||
#include "common/rational.h"
|
#include "common/rational.h"
|
||||||
#include "render/color.h"
|
#include "render/color.h"
|
||||||
#include "render/videoparams.h"
|
#include "render/videoparams.h"
|
||||||
@@ -41,6 +42,10 @@ class Frame
|
|||||||
public:
|
public:
|
||||||
Frame();
|
Frame();
|
||||||
|
|
||||||
|
~Frame();
|
||||||
|
|
||||||
|
DISABLE_COPY_MOVE(Frame)
|
||||||
|
|
||||||
static FramePtr Create();
|
static FramePtr Create();
|
||||||
|
|
||||||
const VideoParams& video_params() const;
|
const VideoParams& video_params() const;
|
||||||
@@ -102,7 +107,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
char* data()
|
char* data()
|
||||||
{
|
{
|
||||||
return data_.data();
|
return data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -110,7 +115,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
const char* const_data() const
|
const char* const_data() const
|
||||||
{
|
{
|
||||||
return data_.constData();
|
return data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -127,16 +132,13 @@ public:
|
|||||||
*/
|
*/
|
||||||
bool is_allocated() const
|
bool is_allocated() const
|
||||||
{
|
{
|
||||||
return !data_.isEmpty();
|
return data_;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Destroy a memory buffer allocated with allocate()
|
* @brief Destroy a memory buffer allocated with allocate()
|
||||||
*/
|
*/
|
||||||
void destroy()
|
void destroy();
|
||||||
{
|
|
||||||
data_.clear();
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Returns the size of the array returned in data() in bytes
|
* @brief Returns the size of the array returned in data() in bytes
|
||||||
@@ -145,7 +147,7 @@ public:
|
|||||||
*/
|
*/
|
||||||
int allocated_size() const
|
int allocated_size() const
|
||||||
{
|
{
|
||||||
return data_.size();
|
return data_size_;
|
||||||
}
|
}
|
||||||
|
|
||||||
FramePtr convert(VideoParams::Format format) const;
|
FramePtr convert(VideoParams::Format format) const;
|
||||||
@@ -153,7 +155,8 @@ public:
|
|||||||
private:
|
private:
|
||||||
VideoParams params_;
|
VideoParams params_;
|
||||||
|
|
||||||
QByteArray data_;
|
char* data_;
|
||||||
|
int data_size_;
|
||||||
|
|
||||||
rational timestamp_;
|
rational timestamp_;
|
||||||
|
|
||||||
|
|||||||
@@ -51,6 +51,7 @@
|
|||||||
#include "panel/project/project.h"
|
#include "panel/project/project.h"
|
||||||
#include "panel/viewer/viewer.h"
|
#include "panel/viewer/viewer.h"
|
||||||
#include "render/diskmanager.h"
|
#include "render/diskmanager.h"
|
||||||
|
#include "render/framemanager.h"
|
||||||
#include "render/rendermanager.h"
|
#include "render/rendermanager.h"
|
||||||
#ifdef USE_OTIO
|
#ifdef USE_OTIO
|
||||||
#include "task/project/loadotio/loadotio.h"
|
#include "task/project/loadotio/loadotio.h"
|
||||||
@@ -140,6 +141,9 @@ void Core::Start()
|
|||||||
// Initialize RenderManager
|
// Initialize RenderManager
|
||||||
RenderManager::CreateInstance();
|
RenderManager::CreateInstance();
|
||||||
|
|
||||||
|
// Initialize FrameManager
|
||||||
|
FrameManager::CreateInstance();
|
||||||
|
|
||||||
//
|
//
|
||||||
// Start application
|
// Start application
|
||||||
//
|
//
|
||||||
@@ -184,6 +188,8 @@ void Core::Stop()
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
FrameManager::DestroyInstance();
|
||||||
|
|
||||||
RenderManager::DestroyInstance();
|
RenderManager::DestroyInstance();
|
||||||
|
|
||||||
MenuShared::DestroyInstance();
|
MenuShared::DestroyInstance();
|
||||||
|
|||||||
@@ -33,6 +33,8 @@ set(OLIVE_SOURCES
|
|||||||
render/diskmanager.h
|
render/diskmanager.h
|
||||||
render/framehashcache.cpp
|
render/framehashcache.cpp
|
||||||
render/framehashcache.h
|
render/framehashcache.h
|
||||||
|
render/framemanager.cpp
|
||||||
|
render/framemanager.h
|
||||||
render/managedcolor.cpp
|
render/managedcolor.cpp
|
||||||
render/managedcolor.h
|
render/managedcolor.h
|
||||||
render/playbackcache.cpp
|
render/playbackcache.cpp
|
||||||
|
|||||||
@@ -0,0 +1,129 @@
|
|||||||
|
/***
|
||||||
|
|
||||||
|
Olive - Non-Linear Video Editor
|
||||||
|
Copyright (C) 2021 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 "framemanager.h"
|
||||||
|
|
||||||
|
#include <QDateTime>
|
||||||
|
#include <QDebug>
|
||||||
|
|
||||||
|
namespace olive {
|
||||||
|
|
||||||
|
FrameManager* FrameManager::instance_ = nullptr;
|
||||||
|
const int FrameManager::kFrameLifetime = 5000;
|
||||||
|
|
||||||
|
void FrameManager::CreateInstance()
|
||||||
|
{
|
||||||
|
instance_ = new FrameManager();
|
||||||
|
}
|
||||||
|
|
||||||
|
void FrameManager::DestroyInstance()
|
||||||
|
{
|
||||||
|
delete instance_;
|
||||||
|
instance_ = nullptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
FrameManager *FrameManager::instance()
|
||||||
|
{
|
||||||
|
return instance_;
|
||||||
|
}
|
||||||
|
|
||||||
|
char *FrameManager::Allocate(int size)
|
||||||
|
{
|
||||||
|
if (instance()) {
|
||||||
|
return instance()->AllocateFromPool(size);
|
||||||
|
} else {
|
||||||
|
return new char[size];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void FrameManager::Deallocate(int size, char *buffer)
|
||||||
|
{
|
||||||
|
if (instance()) {
|
||||||
|
instance()->DeallocateToPool(size, buffer);
|
||||||
|
} else {
|
||||||
|
delete [] buffer;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FrameManager::FrameManager()
|
||||||
|
{
|
||||||
|
clear_timer_.setInterval(kFrameLifetime);
|
||||||
|
connect(&clear_timer_, &QTimer::timeout, this, &FrameManager::GarbageCollection);
|
||||||
|
clear_timer_.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
char *FrameManager::AllocateFromPool(int size)
|
||||||
|
{
|
||||||
|
QMutexLocker locker(&mutex_);
|
||||||
|
|
||||||
|
std::list<Buffer>& buffer_list = pool_[size];
|
||||||
|
char* buf = nullptr;
|
||||||
|
|
||||||
|
if (buffer_list.empty()) {
|
||||||
|
buf = new char[size];
|
||||||
|
} else {
|
||||||
|
// Take this buffer from the list
|
||||||
|
buf = buffer_list.front().data;
|
||||||
|
buffer_list.pop_front();
|
||||||
|
}
|
||||||
|
|
||||||
|
return buf;
|
||||||
|
}
|
||||||
|
|
||||||
|
void FrameManager::DeallocateToPool(int size, char *buffer)
|
||||||
|
{
|
||||||
|
QMutexLocker locker(&mutex_);
|
||||||
|
|
||||||
|
std::list<Buffer>& buffer_list = pool_[size];
|
||||||
|
|
||||||
|
buffer_list.push_back({QDateTime::currentMSecsSinceEpoch(), buffer});
|
||||||
|
}
|
||||||
|
|
||||||
|
void FrameManager::GarbageCollection()
|
||||||
|
{
|
||||||
|
QMutexLocker locker(&mutex_);
|
||||||
|
|
||||||
|
qint64 min_life = QDateTime::currentMSecsSinceEpoch() - kFrameLifetime;
|
||||||
|
|
||||||
|
for (auto it=pool_.begin(); it!=pool_.end(); it++) {
|
||||||
|
std::list<Buffer>& list = it->second;
|
||||||
|
|
||||||
|
while (list.size() > 0 && list.front().time < min_life) {
|
||||||
|
delete [] list.front().data;
|
||||||
|
list.pop_front();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
FrameManager::~FrameManager()
|
||||||
|
{
|
||||||
|
QMutexLocker locker(&mutex_);
|
||||||
|
|
||||||
|
for (auto it=pool_.begin(); it!=pool_.end(); it++) {
|
||||||
|
std::list<Buffer>& list = it->second;
|
||||||
|
for (auto jt=list.begin(); jt!=list.end(); jt++) {
|
||||||
|
delete [] (*jt).data;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pool_.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,91 @@
|
|||||||
|
/***
|
||||||
|
|
||||||
|
Olive - Non-Linear Video Editor
|
||||||
|
Copyright (C) 2021 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 FRAMEMANAGER_H
|
||||||
|
#define FRAMEMANAGER_H
|
||||||
|
|
||||||
|
#include <QObject>
|
||||||
|
#include <QTimer>
|
||||||
|
|
||||||
|
namespace olive {
|
||||||
|
|
||||||
|
class FrameManager : public QObject
|
||||||
|
{
|
||||||
|
Q_OBJECT
|
||||||
|
public:
|
||||||
|
static void CreateInstance();
|
||||||
|
|
||||||
|
static void DestroyInstance();
|
||||||
|
|
||||||
|
static FrameManager* instance();
|
||||||
|
|
||||||
|
static char* Allocate(int size);
|
||||||
|
|
||||||
|
static void Deallocate(int size, char* buffer);
|
||||||
|
|
||||||
|
private:
|
||||||
|
FrameManager();
|
||||||
|
|
||||||
|
virtual ~FrameManager() override;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Allocate buffer
|
||||||
|
*
|
||||||
|
* Caller takes ownership of buffer and can delete it if they want. It can also be returned to
|
||||||
|
* the manager with Deallocate and potentially be re-used later.
|
||||||
|
*
|
||||||
|
* Thread-safe.
|
||||||
|
*/
|
||||||
|
char* AllocateFromPool(int size);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Deallocate buffer
|
||||||
|
*
|
||||||
|
* Manager will take ownership and buffer will stay allocated for some time in case it can be
|
||||||
|
* re-used.
|
||||||
|
*
|
||||||
|
* Thread-safe.
|
||||||
|
*/
|
||||||
|
void DeallocateToPool(int size, char* buffer);
|
||||||
|
|
||||||
|
static FrameManager* instance_;
|
||||||
|
|
||||||
|
static const int kFrameLifetime;
|
||||||
|
|
||||||
|
struct Buffer
|
||||||
|
{
|
||||||
|
qint64 time;
|
||||||
|
char* data;
|
||||||
|
};
|
||||||
|
|
||||||
|
std::map< int, std::list<Buffer> > pool_;
|
||||||
|
|
||||||
|
QMutex mutex_;
|
||||||
|
|
||||||
|
QTimer clear_timer_;
|
||||||
|
|
||||||
|
private slots:
|
||||||
|
void GarbageCollection();
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif // FRAMEMANAGER_H
|
||||||
@@ -455,7 +455,6 @@ void ViewerWidget::UpdateTextureFromNode()
|
|||||||
|
|
||||||
// Check playback queue for a frame
|
// Check playback queue for a frame
|
||||||
if (IsPlaying()) {
|
if (IsPlaying()) {
|
||||||
|
|
||||||
// We still run the playback queue even when FrameExistsAtTime returns false because we might be
|
// We still run the playback queue even when FrameExistsAtTime returns false because we might be
|
||||||
// playing backwards and about to start showing frames, so the queue should be prepared for
|
// playing backwards and about to start showing frames, so the queue should be prepared for
|
||||||
// that.
|
// that.
|
||||||
|
|||||||
@@ -21,6 +21,7 @@
|
|||||||
#include "viewerplaybacktimer.h"
|
#include "viewerplaybacktimer.h"
|
||||||
|
|
||||||
#include <QDateTime>
|
#include <QDateTime>
|
||||||
|
#include <QtMath>
|
||||||
|
|
||||||
namespace olive {
|
namespace olive {
|
||||||
|
|
||||||
@@ -29,14 +30,14 @@ void ViewerPlaybackTimer::Start(const int64_t &start_timestamp, const int &playb
|
|||||||
start_msec_ = QDateTime::currentMSecsSinceEpoch();
|
start_msec_ = QDateTime::currentMSecsSinceEpoch();
|
||||||
start_timestamp_ = start_timestamp;
|
start_timestamp_ = start_timestamp;
|
||||||
playback_speed_ = playback_speed;
|
playback_speed_ = playback_speed;
|
||||||
timebase_ = timebase;
|
timebase_ = timebase * 1000;
|
||||||
}
|
}
|
||||||
|
|
||||||
int64_t ViewerPlaybackTimer::GetTimestampNow() const
|
int64_t ViewerPlaybackTimer::GetTimestampNow() const
|
||||||
{
|
{
|
||||||
int64_t real_time = QDateTime::currentMSecsSinceEpoch() - start_msec_;
|
int64_t real_time = QDateTime::currentMSecsSinceEpoch() - start_msec_;
|
||||||
|
|
||||||
int64_t frames_since_start = qRound(static_cast<double>(real_time) / (timebase_ * 1000));
|
int64_t frames_since_start = qFloor(static_cast<double>(real_time) / (timebase_));
|
||||||
|
|
||||||
return start_timestamp_ + frames_since_start * playback_speed_;
|
return start_timestamp_ + frames_since_start * playback_speed_;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user