From a4a18ebc3f03016cc3c02b395406ce3e87d25fce Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Sun, 14 May 2023 08:56:34 +0200 Subject: [PATCH] code: Migrate filter::upscaling to new dynamic loader --- source/filters/filter-upscaling.cpp | 41 +++++++++++++++-------------- source/filters/filter-upscaling.hpp | 2 +- source/plugin.cpp | 9 ------- 3 files changed, 22 insertions(+), 30 deletions(-) diff --git a/source/filters/filter-upscaling.cpp b/source/filters/filter-upscaling.cpp index 4c44314..824eaff 100644 --- a/source/filters/filter-upscaling.cpp +++ b/source/filters/filter-upscaling.cpp @@ -146,7 +146,7 @@ void upscaling_instance::update(obs_data_t* data) // Check if the user changed which Denoising provider we use. upscaling_provider provider = static_cast(obs_data_get_int(data, ST_KEY_PROVIDER)); if (provider == upscaling_provider::AUTOMATIC) { - provider = upscaling_factory::get()->find_ideal_provider(); + provider = upscaling_factory::instance()->find_ideal_provider(); } // Check if the provider was changed, and if so switch. @@ -618,7 +618,7 @@ bool streamfx::filter::upscaling::upscaling_factory::is_provider_available(upsca upscaling_provider streamfx::filter::upscaling::upscaling_factory::find_ideal_provider() { for (auto v : provider_priority) { - if (upscaling_factory::get()->is_provider_available(v)) { + if (upscaling_factory::instance()->is_provider_available(v)) { return v; break; } @@ -626,26 +626,27 @@ upscaling_provider streamfx::filter::upscaling::upscaling_factory::find_ideal_pr return upscaling_provider::AUTOMATIC; } -std::shared_ptr _video_superresolution_factory_instance = nullptr; - -void upscaling_factory::initialize() +std::shared_ptr upscaling_factory::instance() { - try { - if (!_video_superresolution_factory_instance) - _video_superresolution_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 upscaling_factory()); + winst = instance; } + return instance; } -void upscaling_factory::finalize() -{ - _video_superresolution_factory_instance.reset(); -} +static std::shared_ptr loader_instance; -std::shared_ptr upscaling_factory::get() -{ - return _video_superresolution_factory_instance; -} +static auto loader = streamfx::loader( + []() { // Initalizer + loader_instance = upscaling_factory::instance(); + }, + []() { // Finalizer + loader_instance.reset(); + }, + streamfx::loader_priority::NORMAL); diff --git a/source/filters/filter-upscaling.hpp b/source/filters/filter-upscaling.hpp index aac5b54..77f3a59 100644 --- a/source/filters/filter-upscaling.hpp +++ b/source/filters/filter-upscaling.hpp @@ -109,7 +109,7 @@ namespace streamfx::filter::upscaling { public: // Singleton static void initialize(); static void finalize(); - static std::shared_ptr<::streamfx::filter::upscaling::upscaling_factory> get(); + static std::shared_ptr<::streamfx::filter::upscaling::upscaling_factory> instance(); }; } // namespace streamfx::filter::upscaling diff --git a/source/plugin.cpp b/source/plugin.cpp index f9e27b3..2990c7d 100644 --- a/source/plugin.cpp +++ b/source/plugin.cpp @@ -25,9 +25,6 @@ #ifdef ENABLE_FILTER_DISPLACEMENT #include "filters/filter-displacement.hpp" #endif -#ifdef ENABLE_FILTER_UPSCALING -#include "filters/filter-upscaling.hpp" -#endif #ifdef ENABLE_FRONTEND #include "ui/ui.hpp" @@ -136,9 +133,6 @@ MODULE_EXPORT bool obs_module_load(void) #endif #ifdef ENABLE_FILTER_DISPLACEMENT streamfx::filter::displacement::displacement_factory::initialize(); -#endif -#ifdef ENABLE_FILTER_UPSCALING - streamfx::filter::upscaling::upscaling_factory::initialize(); #endif } @@ -168,9 +162,6 @@ MODULE_EXPORT void obs_module_unload(void) #endif #ifdef ENABLE_FILTER_DISPLACEMENT streamfx::filter::displacement::displacement_factory::finalize(); -#endif -#ifdef ENABLE_FILTER_UPSCALING - streamfx::filter::upscaling::upscaling_factory::finalize(); #endif }