Files
oak-editor/app/render/renderer.cpp
T
Mike-Solar bb40b4923e style: unify identifier naming per updated conventions
Automated with clang-tidy readability-identifier-naming (config added to
.clang-tidy) plus scripted passes, per the updated rules now documented
in CONTRIBUTING.md:

- types (class/struct/enum/alias/template params): PascalCase
- functions, variables, members: snake_case (incl. rational -> Rational)
- private/protected members: trailing underscore; static member
  variables likewise (instance_, available_themes_)
- constants and enum values: snake_case (kLinear -> k_linear,
  F32P -> f32p); ALL_CAPS reserved for macros
- macros: OAK_ prefix (OLIVE_ADD_TEST/OLIVE_ASSERT/OLIVE_CONFIG ->
  OAK_ADD_TEST/OAK_ASSERT/OAK_CONFIG, GL_PREAMBLE -> OAK_GL_PREAMBLE,
  include guards -> OAK_*)
- file names: all lowercase (Current/Plugin/OliveHost/OliveClip/
  OlivePluginInstance -> current/plugin/olivehost/oliveclip/
  oliveplugininstance)
- getters share the member name sans underscore, setters set_foo()
- Qt and third-party (OpenFX) virtual overrides and framework callbacks
  keep their original names (exempt in .clang-tidy)

Manual follow-ups required where automation could not reach:
- string-based QMetaObject/SIGNAL/SLOT references updated to renamed
  methods (AddTask, CreatedFile, DeleteSpecificFile, moveSelectionUp, ...)
- macro bodies referencing renamed methods (OLIVE_CONFIG,
  NODE_DEFAULT_DESTRUCTOR, MANAGEDDISPLAYWIDGET_*)
- self-shadowing locals renamed where signals/methods became same-named
  (size_changed, worker_count, selected_items, import param, filters)
- third_party OFX member/namespace usages restored (OFX::Host::*,
  _created, _clipPrefsDirty, createInstance, clearPersistentMessage)
- STL protocol aliases restored (const_iterator) with .clang-tidy
  ignore rules; qHash overloads restored

Full build and test suite pass: ctest 4/4, ~1960 gtest cases green.
2026-07-19 16:10:54 +08:00

195 lines
5.5 KiB
C++

/***
Olive - Non-Linear Video Editor
Copyright (C) 2022 Olive Team
Modifications Copyright (C) 2025 mikesolar
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 "renderer.h"
#include <QDateTime>
#include <QThread>
#include <QTimer>
#include <QVector2D>
namespace olive
{
Renderer::Renderer(QObject *parent)
: QObject(parent)
, lifetime_(std::make_shared<RendererLifetime>())
{
}
Renderer::~Renderer()
{
destroyed_ = true;
if (lifetime_) {
lifetime_->alive = false;
}
}
TexturePtr Renderer::create_texture(const VideoParams &params, const void *data,
int linesize)
{
QVariant v;
if (use_texture_cache) {
QMutexLocker locker(&texture_cache_lock_);
for (auto it = texture_cache_.begin(); it != texture_cache_.end();
it++) {
if (it->width == params.effective_width() &&
it->height == params.effective_height() &&
it->depth == params.effective_depth() &&
it->format == params.format() &&
it->channel_count == params.channel_count()) {
v = it->handle;
texture_cache_.erase(it);
break;
}
}
}
if (v.isNull()) {
v = create_native_texture(params.effective_width(),
params.effective_height(),
params.effective_depth(), params.format(),
params.channel_count(), data, linesize);
} else if (data) {
upload_to_texture(v, params, data, linesize);
} else {
this->flush();
}
return create_texture_from_native_handle(v, params);
}
void Renderer::destroy_texture(Texture *texture)
{
if (destroyed_) {
return;
}
if (use_texture_cache) {
// HACK: Dirty, dirty hack. OpenGL uses "contexts" to store all of its data, and each context
// can only be used by the thread that created it. However there are also "shared contexts"
// where assets from one context can be used in another. We use shared contexts so that
// textures rendered in the background can be displayed on the screen, travelling from
// a background thread to the main UI thread. However, when that texture is destroyed, it
// comes back here to be placed in the texture cache. But that leads to a race condition
// because it will call the background thread's renderer in the main thread. Since all
// assets are shared, we could technically just get the texture to call "destroy" in the
// viewer's renderer instance, but that would mean all textures would end up stranded
// there unusable by the background renderer, negating the very advantage of the texture
// cache in the first place. Therefore, we simply allow the thread calling to happen, and
// use mutexes to prevent race conditions.
//
// Presumably Vulkan would not have this issue because it allows for application-wide
// instances and multithreading.
texture_cache_lock_.lock();
texture_cache_.push_back(
{ texture->params().effective_width(),
texture->params().effective_height(),
texture->params().effective_depth(), texture->params().format(),
texture->params().channel_count(), texture->id(),
QDateTime::currentMSecsSinceEpoch() });
texture_cache_lock_.unlock();
if (QThread::currentThread() == this->thread()) {
clear_old_textures();
}
} else {
destroy_native_texture(texture->id());
}
}
QVariant Renderer::get_default_shader()
{
QMutexLocker locker(&color_cache_mutex_);
if (default_shader_.isNull()) {
default_shader_ = create_native_shader(ShaderCode(QString(), QString()));
}
return default_shader_;
}
void Renderer::destroy()
{
if (!default_shader_.isNull()) {
destroy_native_shader(default_shader_);
default_shader_.clear();
}
{
QMutexLocker locker(&color_cache_mutex_);
// Destroy the cached native shaders explicitly. The LUT textures are
// TexturePtrs whose destructors call DestroyTexture(), so the cache must
// be cleared while the renderer is still alive for those to be honored.
for (auto it = color_cache_.begin(); it != color_cache_.end(); it++) {
if (!it->compiled_shader.isNull()) {
destroy_native_shader(it->compiled_shader);
}
}
color_cache_.clear();
}
if (!interlace_texture_.isNull()) {
destroy_native_shader(interlace_texture_);
interlace_texture_.clear();
}
for (auto it = texture_cache_.begin(); it != texture_cache_.end(); it++) {
destroy_native_texture(it->handle);
}
texture_cache_.clear();
destroyed_ = true;
if (lifetime_) {
lifetime_->alive = false;
}
destroy_internal();
}
TexturePtr Renderer::create_texture_from_native_handle(const QVariant &v,
const VideoParams &params)
{
if (v.isNull()) {
return nullptr;
}
return std::make_shared<Texture>(this, v, params, lifetime_);
}
void Renderer::clear_old_textures()
{
QMutexLocker locker(&texture_cache_lock_);
for (auto it = texture_cache_.begin(); it != texture_cache_.end();) {
if (it->accessed <
QDateTime::currentMSecsSinceEpoch() - max_texture_life) {
destroy_native_texture(it->handle);
it = texture_cache_.erase(it);
} else {
it++;
}
}
}
} // namespace olive