code: Migrate source::shader to new dynamic loader

This commit is contained in:
Michael Fabian 'Xaymar' Dirks 2023-05-14 08:41:55 +02:00 committed by Xaymar
parent 765e663180
commit cfa2806e89
3 changed files with 20 additions and 32 deletions

View file

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

View file

@ -171,26 +171,27 @@ bool shader_factory::on_manual_open(obs_properties_t* props, obs_property_t* pro
}
#endif
std::shared_ptr<shader_factory> _source_shader_factory_instance = nullptr;
void streamfx::source::shader::shader_factory::initialize()
std::shared_ptr<shader_factory> shader_factory::instance()
{
try {
if (!_source_shader_factory_instance)
_source_shader_factory_instance = std::make_shared<shader_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<shader_factory> winst;
static std::mutex mtx;
std::unique_lock<decltype(mtx)> lock(mtx);
auto instance = winst.lock();
if (!instance) {
instance = std::shared_ptr<shader_factory>(new shader_factory());
winst = instance;
}
return instance;
}
void streamfx::source::shader::shader_factory::finalize()
{
_source_shader_factory_instance.reset();
}
static std::shared_ptr<shader_factory> loader_instance;
std::shared_ptr<shader_factory> 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);

View file

@ -52,10 +52,6 @@ namespace streamfx::source::shader {
#endif
public: // Singleton
static void initialize();
static void finalize();
static std::shared_ptr<shader_factory> get();
static std::shared_ptr<shader_factory> instance();
};
} // namespace streamfx::source::shader