Files
oak-editor/app/render/backend/opengl/opengltexturecache.cpp
T
itsmattkc 91c7d8aa4d implemented texture cache for performance
Creating and destroying textures is a slow process, particularly when we can
re-use them throughout most of the render chain. We now keep them stored so
they can be re-used which improves performance substantially.
2019-12-10 14:53:47 +11:00

76 lines
1.7 KiB
C++

#include "opengltexturecache.h"
OpenGLTextureCache::~OpenGLTextureCache()
{
foreach (Reference* ref, existing_references_) {
ref->ParentKilled();
}
}
OpenGLTextureCache::ReferencePtr OpenGLTextureCache::Get(const VideoRenderingParams &params, const void *data)
{
OpenGLTexturePtr texture = nullptr;
lock_.lock();
// Iterate through textures and see if we have one that matches these parameters
for (int i=0;i<available_textures_.size();i++) {
OpenGLTexturePtr test = available_textures_.at(i);
if (test->width() == params.effective_width()
&& test->height() == params.effective_height()
&& test->format() == params.format()) {
texture = test;
available_textures_.removeAt(i);
break;
}
}
lock_.unlock();
QOpenGLContext* ctx = QOpenGLContext::currentContext();
// If we didn't find a texture, we'll need to create one
if (!texture) {
texture = std::make_shared<OpenGLTexture>();
texture->Create(ctx, params.effective_width(), params.effective_height(), params.format(), data);
} else if (data) {
texture->Upload(data);
}
return std::make_shared<Reference>(this, texture);
}
void OpenGLTextureCache::Relinquish(OpenGLTextureCache::Reference *ref)
{
lock_.lock();
existing_references_.removeOne(ref);
available_textures_.append(ref->texture());
lock_.unlock();
}
OpenGLTextureCache::Reference::Reference(OpenGLTextureCache *parent, OpenGLTexturePtr texture) :
parent_(parent),
texture_(texture)
{
}
OpenGLTextureCache::Reference::~Reference()
{
if (parent_) {
parent_->Relinquish(this);
}
}
OpenGLTexturePtr OpenGLTextureCache::Reference::texture()
{
return texture_;
}
void OpenGLTextureCache::Reference::ParentKilled()
{
parent_ = nullptr;
}