code: Migrate filter::upscaling to new dynamic loader

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2023-05-14 08:56:34 +02:00 committed by Xaymar
parent 0556db97df
commit a4a18ebc3f
3 changed files with 22 additions and 30 deletions

View file

@ -146,7 +146,7 @@ void upscaling_instance::update(obs_data_t* data)
// Check if the user changed which Denoising provider we use. // Check if the user changed which Denoising provider we use.
upscaling_provider provider = static_cast<upscaling_provider>(obs_data_get_int(data, ST_KEY_PROVIDER)); upscaling_provider provider = static_cast<upscaling_provider>(obs_data_get_int(data, ST_KEY_PROVIDER));
if (provider == upscaling_provider::AUTOMATIC) { 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. // 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() upscaling_provider streamfx::filter::upscaling::upscaling_factory::find_ideal_provider()
{ {
for (auto v : provider_priority) { for (auto v : provider_priority) {
if (upscaling_factory::get()->is_provider_available(v)) { if (upscaling_factory::instance()->is_provider_available(v)) {
return v; return v;
break; break;
} }
@ -626,26 +626,27 @@ upscaling_provider streamfx::filter::upscaling::upscaling_factory::find_ideal_pr
return upscaling_provider::AUTOMATIC; return upscaling_provider::AUTOMATIC;
} }
std::shared_ptr<upscaling_factory> _video_superresolution_factory_instance = nullptr; std::shared_ptr<upscaling_factory> upscaling_factory::instance()
void upscaling_factory::initialize()
{ {
try { static std::weak_ptr<upscaling_factory> winst;
if (!_video_superresolution_factory_instance) static std::mutex mtx;
_video_superresolution_factory_instance = std::make_shared<upscaling_factory>();
} catch (const std::exception& ex) { std::unique_lock<decltype(mtx)> lock(mtx);
D_LOG_ERROR("Failed to initialize due to error: %s", ex.what()); auto instance = winst.lock();
} catch (...) { if (!instance) {
D_LOG_ERROR("Failed to initialize due to unknown error.", ""); instance = std::shared_ptr<upscaling_factory>(new upscaling_factory());
winst = instance;
} }
return instance;
} }
void upscaling_factory::finalize() static std::shared_ptr<upscaling_factory> loader_instance;
{
_video_superresolution_factory_instance.reset();
}
std::shared_ptr<upscaling_factory> upscaling_factory::get() static auto loader = streamfx::loader(
{ []() { // Initalizer
return _video_superresolution_factory_instance; loader_instance = upscaling_factory::instance();
} },
[]() { // Finalizer
loader_instance.reset();
},
streamfx::loader_priority::NORMAL);

View file

@ -109,7 +109,7 @@ namespace streamfx::filter::upscaling {
public: // Singleton public: // Singleton
static void initialize(); static void initialize();
static void finalize(); 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 } // namespace streamfx::filter::upscaling

View file

@ -25,9 +25,6 @@
#ifdef ENABLE_FILTER_DISPLACEMENT #ifdef ENABLE_FILTER_DISPLACEMENT
#include "filters/filter-displacement.hpp" #include "filters/filter-displacement.hpp"
#endif #endif
#ifdef ENABLE_FILTER_UPSCALING
#include "filters/filter-upscaling.hpp"
#endif
#ifdef ENABLE_FRONTEND #ifdef ENABLE_FRONTEND
#include "ui/ui.hpp" #include "ui/ui.hpp"
@ -136,9 +133,6 @@ MODULE_EXPORT bool obs_module_load(void)
#endif #endif
#ifdef ENABLE_FILTER_DISPLACEMENT #ifdef ENABLE_FILTER_DISPLACEMENT
streamfx::filter::displacement::displacement_factory::initialize(); streamfx::filter::displacement::displacement_factory::initialize();
#endif
#ifdef ENABLE_FILTER_UPSCALING
streamfx::filter::upscaling::upscaling_factory::initialize();
#endif #endif
} }
@ -168,9 +162,6 @@ MODULE_EXPORT void obs_module_unload(void)
#endif #endif
#ifdef ENABLE_FILTER_DISPLACEMENT #ifdef ENABLE_FILTER_DISPLACEMENT
streamfx::filter::displacement::displacement_factory::finalize(); streamfx::filter::displacement::displacement_factory::finalize();
#endif
#ifdef ENABLE_FILTER_UPSCALING
streamfx::filter::upscaling::upscaling_factory::finalize();
#endif #endif
} }