diff --git a/source/plugin.cpp b/source/plugin.cpp index e1e93cbe..b2e15f3b 100644 --- a/source/plugin.cpp +++ b/source/plugin.cpp @@ -57,10 +57,6 @@ #include "sources/source-shader.hpp" #endif -#ifdef ENABLE_TRANSITION_SHADER -#include "transitions/transition-shader.hpp" -#endif - #ifdef ENABLE_FRONTEND #include "ui/ui.hpp" #endif @@ -205,13 +201,6 @@ MODULE_EXPORT bool obs_module_load(void) #endif } - // Transitions - { -#ifdef ENABLE_TRANSITION_SHADER - streamfx::transition::shader::shader_factory::initialize(); -#endif - } - DLOG_INFO("Loaded Version %s", STREAMFX_VERSION_STRING); return true; } catch (std::exception const& ex) { @@ -228,13 +217,6 @@ MODULE_EXPORT void obs_module_unload(void) try { DLOG_INFO("Unloading Version %s", STREAMFX_VERSION_STRING); - // Transitions - { -#ifdef ENABLE_TRANSITION_SHADER - streamfx::transition::shader::shader_factory::finalize(); -#endif - } - // Sources { #ifdef ENABLE_SOURCE_MIRROR diff --git a/source/transitions/transition-shader.cpp b/source/transitions/transition-shader.cpp index 763d2711..f68cb5a8 100644 --- a/source/transitions/transition-shader.cpp +++ b/source/transitions/transition-shader.cpp @@ -178,26 +178,27 @@ bool shader_factory::on_manual_open(obs_properties_t* props, obs_property_t* pro } #endif -std::shared_ptr _transition_shader_factory_instance = nullptr; - -void streamfx::transition::shader::shader_factory::initialize() +std::shared_ptr shader_factory::instance() { - try { - if (!_transition_shader_factory_instance) - _transition_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::transition::shader::shader_factory::finalize() -{ - _transition_shader_factory_instance.reset(); -} +static std::shared_ptr loader_instance; -std::shared_ptr streamfx::transition::shader::shader_factory::get() -{ - return _transition_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/transitions/transition-shader.hpp b/source/transitions/transition-shader.hpp index 0d19dfdc..a5049637 100644 --- a/source/transitions/transition-shader.hpp +++ b/source/transitions/transition-shader.hpp @@ -52,10 +52,6 @@ namespace streamfx::transition::shader { #endif public: // Singleton - static void initialize(); - - static void finalize(); - - static std::shared_ptr get(); + static std::shared_ptr instance(); }; } // namespace streamfx::transition::shader