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.
upscaling_provider provider = static_cast<upscaling_provider>(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<upscaling_factory> _video_superresolution_factory_instance = nullptr;
void upscaling_factory::initialize()
std::shared_ptr<upscaling_factory> upscaling_factory::instance()
{
try {
if (!_video_superresolution_factory_instance)
_video_superresolution_factory_instance = std::make_shared<upscaling_factory>();
} 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<upscaling_factory> winst;
static std::mutex mtx;
std::unique_lock<decltype(mtx)> lock(mtx);
auto instance = winst.lock();
if (!instance) {
instance = std::shared_ptr<upscaling_factory>(new upscaling_factory());
winst = instance;
}
return instance;
}
void upscaling_factory::finalize()
{
_video_superresolution_factory_instance.reset();
}
static std::shared_ptr<upscaling_factory> loader_instance;
std::shared_ptr<upscaling_factory> 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);

View file

@ -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

View file

@ -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
}