From cfa2806e89d3e50508599015094d44f0c880715e Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sun, 14 May 2023 08:41:55 +0200 Subject: [PATCH] code: Migrate source::shader to new dynamic loader --- source/plugin.cpp | 9 -------- source/sources/source-shader.cpp | 37 ++++++++++++++++---------------- source/sources/source-shader.hpp | 6 +----- 3 files changed, 20 insertions(+), 32 deletions(-) diff --git a/source/plugin.cpp b/source/plugin.cpp index d3f6f28..b0b1114 100644 --- a/source/plugin.cpp +++ b/source/plugin.cpp @@ -50,9 +50,6 @@ #ifdef ENABLE_SOURCE_MIRROR #include "sources/source-mirror.hpp" #endif -#ifdef ENABLE_SOURCE_SHADER -#include "sources/source-shader.hpp" -#endif #ifdef ENABLE_FRONTEND #include "ui/ui.hpp" @@ -189,9 +186,6 @@ MODULE_EXPORT bool obs_module_load(void) { #ifdef ENABLE_SOURCE_MIRROR streamfx::source::mirror::mirror_factory::initialize(); -#endif -#ifdef ENABLE_SOURCE_SHADER - streamfx::source::shader::shader_factory::initialize(); #endif } @@ -215,9 +209,6 @@ MODULE_EXPORT void obs_module_unload(void) { #ifdef ENABLE_SOURCE_MIRROR streamfx::source::mirror::mirror_factory::finalize(); -#endif -#ifdef ENABLE_SOURCE_SHADER - streamfx::source::shader::shader_factory::finalize(); #endif } diff --git a/source/sources/source-shader.cpp b/source/sources/source-shader.cpp index 6083a29..2b84198 100644 --- a/source/sources/source-shader.cpp +++ b/source/sources/source-shader.cpp @@ -171,26 +171,27 @@ bool shader_factory::on_manual_open(obs_properties_t* props, obs_property_t* pro } #endif -std::shared_ptr _source_shader_factory_instance = nullptr; - -void streamfx::source::shader::shader_factory::initialize() +std::shared_ptr shader_factory::instance() { - try { - if (!_source_shader_factory_instance) - _source_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::source::shader::shader_factory::finalize() -{ - _source_shader_factory_instance.reset(); -} +static std::shared_ptr loader_instance; -std::shared_ptr streamfx::source::shader::shader_factory::get() -{ - return _source_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/sources/source-shader.hpp b/source/sources/source-shader.hpp index 6cc4be3..a5407dc 100644 --- a/source/sources/source-shader.hpp +++ b/source/sources/source-shader.hpp @@ -52,10 +52,6 @@ namespace streamfx::source::shader { #endif public: // Singleton - static void initialize(); - - static void finalize(); - - static std::shared_ptr get(); + static std::shared_ptr instance(); }; } // namespace streamfx::source::shader