From 8bec18600f98fe96b930525315309fdc1ec5c17c Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sun, 14 May 2023 08:45:23 +0200 Subject: [PATCH] code: Migrate filter::shader to new dynamic loader --- source/filters/filter-shader.cpp | 37 ++++++++++++++++---------------- source/filters/filter-shader.hpp | 2 +- source/plugin.cpp | 9 -------- 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/source/filters/filter-shader.cpp b/source/filters/filter-shader.cpp index af8ca44a..3398d55e 100644 --- a/source/filters/filter-shader.cpp +++ b/source/filters/filter-shader.cpp @@ -217,26 +217,27 @@ bool shader_factory::on_manual_open(obs_properties_t* props, obs_property_t* pro } #endif -std::shared_ptr _filter_shader_factory_instance = nullptr; - -void streamfx::filter::shader::shader_factory::initialize() +std::shared_ptr shader_factory::instance() { - try { - if (!_filter_shader_factory_instance) - _filter_shader_factory_instance = std::make_shared(); - } catch (const std::exception& ex) { - D_LOG_ERROR("Failed to initialize due to error: %s", ex.what()); - } catch (...) { - D_LOG_ERROR("Failed to initialize due to unknown error.", ""); + static std::weak_ptr winst; + static std::mutex mtx; + + std::unique_lock lock(mtx); + auto instance = winst.lock(); + if (!instance) { + instance = std::shared_ptr(new shader_factory()); + winst = instance; } + return instance; } -void streamfx::filter::shader::shader_factory::finalize() -{ - _filter_shader_factory_instance.reset(); -} +static std::shared_ptr loader_instance; -std::shared_ptr streamfx::filter::shader::shader_factory::get() -{ - return _filter_shader_factory_instance; -} +static auto loader = streamfx::loader( + []() { // Initalizer + loader_instance = shader_factory::instance(); + }, + []() { // Finalizer + loader_instance.reset(); + }, + streamfx::loader_priority::NORMAL); diff --git a/source/filters/filter-shader.hpp b/source/filters/filter-shader.hpp index 9ace1976..3b174e8c 100644 --- a/source/filters/filter-shader.hpp +++ b/source/filters/filter-shader.hpp @@ -57,6 +57,6 @@ namespace streamfx::filter::shader { static void finalize(); - static std::shared_ptr get(); + static std::shared_ptr instance(); }; } // namespace streamfx::filter::shader diff --git a/source/plugin.cpp b/source/plugin.cpp index b0b11147..ca6d3a91 100644 --- a/source/plugin.cpp +++ b/source/plugin.cpp @@ -34,9 +34,6 @@ #ifdef ENABLE_FILTER_SDF_EFFECTS #include "filters/filter-sdf-effects.hpp" #endif -#ifdef ENABLE_FILTER_SHADER -#include "filters/filter-shader.hpp" -#endif #ifdef ENABLE_FILTER_TRANSFORM #include "filters/filter-transform.hpp" #endif @@ -168,9 +165,6 @@ MODULE_EXPORT bool obs_module_load(void) #ifdef ENABLE_FILTER_SDF_EFFECTS streamfx::filter::sdf_effects::sdf_effects_factory::initialize(); #endif -#ifdef ENABLE_FILTER_SHADER - streamfx::filter::shader::shader_factory::initialize(); -#endif #ifdef ENABLE_FILTER_TRANSFORM streamfx::filter::transform::transform_factory::initialize(); #endif @@ -232,9 +226,6 @@ MODULE_EXPORT void obs_module_unload(void) #ifdef ENABLE_FILTER_SDF_EFFECTS streamfx::filter::sdf_effects::sdf_effects_factory::finalize(); #endif -#ifdef ENABLE_FILTER_SHADER - streamfx::filter::shader::shader_factory::finalize(); -#endif #ifdef ENABLE_FILTER_TRANSFORM streamfx::filter::transform::transform_factory::finalize(); #endif