From 1059e5c0270bfdafcdea4dad69a3f223d6060f6f Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sun, 14 May 2023 08:49:10 +0200 Subject: [PATCH] code: Migrate filter::dynamic_mask to new dynamic loader --- source/filters/filter-dynamic-mask.cpp | 37 +++++++++++++------------- source/filters/filter-dynamic-mask.hpp | 2 +- source/plugin.cpp | 9 ------- 3 files changed, 20 insertions(+), 28 deletions(-) diff --git a/source/filters/filter-dynamic-mask.cpp b/source/filters/filter-dynamic-mask.cpp index ec71e006..37950c76 100644 --- a/source/filters/filter-dynamic-mask.cpp +++ b/source/filters/filter-dynamic-mask.cpp @@ -822,26 +822,27 @@ bool dynamic_mask_factory::on_manual_open(obs_properties_t* props, obs_property_ } #endif -std::shared_ptr _filter_dynamic_mask_factory_instance = nullptr; - -void streamfx::filter::dynamic_mask::dynamic_mask_factory::initialize() +std::shared_ptr dynamic_mask_factory::instance() { - try { - if (!_filter_dynamic_mask_factory_instance) - _filter_dynamic_mask_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 dynamic_mask_factory()); + winst = instance; } + return instance; } -void streamfx::filter::dynamic_mask::dynamic_mask_factory::finalize() -{ - _filter_dynamic_mask_factory_instance.reset(); -} +static std::shared_ptr loader_instance; -std::shared_ptr streamfx::filter::dynamic_mask::dynamic_mask_factory::get() -{ - return _filter_dynamic_mask_factory_instance; -} +static auto loader = streamfx::loader( + []() { // Initalizer + loader_instance = dynamic_mask_factory::instance(); + }, + []() { // Finalizer + loader_instance.reset(); + }, + streamfx::loader_priority::NORMAL); diff --git a/source/filters/filter-dynamic-mask.hpp b/source/filters/filter-dynamic-mask.hpp index ceba4954..cbff522a 100644 --- a/source/filters/filter-dynamic-mask.hpp +++ b/source/filters/filter-dynamic-mask.hpp @@ -133,6 +133,6 @@ namespace streamfx::filter::dynamic_mask { static void finalize(); - static std::shared_ptr get(); + static std::shared_ptr instance(); }; } // namespace streamfx::filter::dynamic_mask diff --git a/source/plugin.cpp b/source/plugin.cpp index a08969c3..ae52063d 100644 --- a/source/plugin.cpp +++ b/source/plugin.cpp @@ -28,9 +28,6 @@ #ifdef ENABLE_FILTER_DISPLACEMENT #include "filters/filter-displacement.hpp" #endif -#ifdef ENABLE_FILTER_DYNAMIC_MASK -#include "filters/filter-dynamic-mask.hpp" -#endif #ifdef ENABLE_FILTER_SDF_EFFECTS #include "filters/filter-sdf-effects.hpp" #endif @@ -156,9 +153,6 @@ MODULE_EXPORT bool obs_module_load(void) #ifdef ENABLE_FILTER_DISPLACEMENT streamfx::filter::displacement::displacement_factory::initialize(); #endif -#ifdef ENABLE_FILTER_DYNAMIC_MASK - streamfx::filter::dynamic_mask::dynamic_mask_factory::initialize(); -#endif #ifdef ENABLE_FILTER_SDF_EFFECTS streamfx::filter::sdf_effects::sdf_effects_factory::initialize(); #endif @@ -214,9 +208,6 @@ MODULE_EXPORT void obs_module_unload(void) #ifdef ENABLE_FILTER_DISPLACEMENT streamfx::filter::displacement::displacement_factory::finalize(); #endif -#ifdef ENABLE_FILTER_DYNAMIC_MASK - streamfx::filter::dynamic_mask::dynamic_mask_factory::finalize(); -#endif #ifdef ENABLE_FILTER_SDF_EFFECTS streamfx::filter::sdf_effects::sdf_effects_factory::finalize(); #endif