From b108362ee105c1a6d69b194b7ac1e9167ae309cd Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Thu, 26 May 2022 02:58:55 +0200 Subject: [PATCH] gfx/source-texture: Update to new classes --- source/filters/filter-blur.cpp | 3 +- source/filters/filter-dynamic-mask.cpp | 30 +++------ source/filters/filter-dynamic-mask.hpp | 4 +- source/gfx/gfx-source-texture.cpp | 86 +++++++------------------- source/gfx/gfx-source-texture.hpp | 50 ++------------- 5 files changed, 39 insertions(+), 134 deletions(-) diff --git a/source/filters/filter-blur.cpp b/source/filters/filter-blur.cpp index b4f249c..728273f 100644 --- a/source/filters/filter-blur.cpp +++ b/source/filters/filter-blur.cpp @@ -377,7 +377,8 @@ void blur_instance::video_tick(float) } else if (_mask.type == mask_type::Source) { if (_mask.source.name_old != _mask.source.name) { try { - _mask.source.source_texture = std::make_shared(_mask.source.name, _self); + _mask.source.source_texture = std::make_shared( + ::streamfx::obs::weak_source(_mask.source.name), ::streamfx::obs::weak_source(_self)); _mask.source.is_scene = (obs_scene_from_source(_mask.source.source_texture->get_object()) != nullptr); _mask.source.name_old = _mask.source.name; } catch (...) { diff --git a/source/filters/filter-dynamic-mask.cpp b/source/filters/filter-dynamic-mask.cpp index ca1b79e..5797a15 100644 --- a/source/filters/filter-dynamic-mask.cpp +++ b/source/filters/filter-dynamic-mask.cpp @@ -169,7 +169,7 @@ void dynamic_mask_instance::update(obs_data_t* settings) void dynamic_mask_instance::save(obs_data_t* settings) { if (_input) { - obs_data_set_string(settings, ST_KEY_INPUT, obs_source_get_name(_input->get())); + obs_data_set_string(settings, ST_KEY_INPUT, _input.lock().name().data()); } for (auto kv1 : channel_translations) { @@ -197,13 +197,6 @@ void dynamic_mask_instance::save(obs_data_t* settings) } } -void dynamic_mask_instance::input_renamed(obs::deprecated_source*, std::string old_name, std::string new_name) -{ - obs_data_t* settings = obs_source_get_settings(_self); - obs_data_set_string(settings, ST_KEY_INPUT, new_name.c_str()); - obs_source_update(_self, settings); -} - void dynamic_mask_instance::video_tick(float) { _have_input_texture = false; @@ -217,11 +210,12 @@ void dynamic_mask_instance::video_render(gs_effect_t* in_effect) obs_source_t* target = obs_filter_get_target(_self); uint32_t width = obs_source_get_base_width(target); uint32_t height = obs_source_get_base_height(target); + auto input = _input.lock(); if (!_self || !parent || !target || !width || !height || !_input || !_input_capture || !_effect) { obs_source_skip_video_filter(_self); return; - } else if (!_input->width() || !_input->height()) { + } else if (!input.width() || !input.height()) { obs_source_skip_video_filter(_self); return; } @@ -276,7 +270,7 @@ void dynamic_mask_instance::video_render(gs_effect_t* in_effect) obs_source_get_name(_input_capture->get_object())}; #endif - _input_texture = _input_capture->render(_input->width(), _input->height()); + _input_texture = _input_capture->render(input.width(), input.height()); _have_input_texture = true; } @@ -371,13 +365,13 @@ void dynamic_mask_instance::video_render(gs_effect_t* in_effect) void dynamic_mask_instance::enum_active_sources(obs_source_enum_proc_t enum_callback, void* param) { if (_input) - enum_callback(_self, _input->get(), param); + enum_callback(_self, _input.lock().get(), param); } void dynamic_mask_instance::enum_all_sources(obs_source_enum_proc_t enum_callback, void* param) { if (_input) - enum_callback(_self, _input->get(), param); + enum_callback(_self, _input.lock().get(), param); } void streamfx::filter::dynamic_mask::dynamic_mask_instance::show() @@ -385,7 +379,7 @@ void streamfx::filter::dynamic_mask::dynamic_mask_instance::show() if (!_input || !obs_source_showing(obs_filter_get_parent(_self))) return; - _input_vs = std::make_shared(_input->get()); + _input_vs = std::make_shared(_input.lock().get()); } void streamfx::filter::dynamic_mask::dynamic_mask_instance::hide() @@ -398,7 +392,7 @@ void streamfx::filter::dynamic_mask::dynamic_mask_instance::activate() if (!_input || !obs_source_active(obs_filter_get_parent(_self))) return; - _input_ac = std::make_shared(_input->get()); + _input_ac = std::make_shared(_input.lock().get()); } void streamfx::filter::dynamic_mask::dynamic_mask_instance::deactivate() @@ -414,15 +408,11 @@ try { } // Acquire a reference to the actual source. - auto input = std::make_shared(std::string(name), true, true); + ::streamfx::obs::source input = name; // Acquire a texture renderer for the source, with the parent source as the parent. auto capture = std::make_shared(input, obs_filter_get_parent(_self)); - // Listed to the rename event. - input->events.rename += std::bind(&dynamic_mask_instance::input_renamed, this, std::placeholders::_1, - std::placeholders::_2, std::placeholders::_3); - // Update our local storage. _input = input; _input_capture = capture; @@ -442,7 +432,7 @@ try { void dynamic_mask_instance::release() { - _input.reset(); + _input = {}; _input_capture.reset(); deactivate(); diff --git a/source/filters/filter-dynamic-mask.hpp b/source/filters/filter-dynamic-mask.hpp index da1eb31..fb152d7 100644 --- a/source/filters/filter-dynamic-mask.hpp +++ b/source/filters/filter-dynamic-mask.hpp @@ -41,7 +41,7 @@ namespace streamfx::filter::dynamic_mask { std::shared_ptr _filter_texture; bool _have_input_texture; - std::shared_ptr _input; + ::streamfx::obs::weak_source _input; std::shared_ptr _input_capture; std::shared_ptr _input_texture; std::shared_ptr _input_vs; @@ -73,8 +73,6 @@ namespace streamfx::filter::dynamic_mask { virtual void update(obs_data_t* settings) override; virtual void save(obs_data_t* settings) override; - void input_renamed(obs::deprecated_source* src, std::string old_name, std::string new_name); - virtual void video_tick(float_t _time) override; virtual void video_render(gs_effect_t* effect) override; diff --git a/source/gfx/gfx-source-texture.cpp b/source/gfx/gfx-source-texture.cpp index ab12632..733b7e6 100644 --- a/source/gfx/gfx-source-texture.cpp +++ b/source/gfx/gfx-source-texture.cpp @@ -18,94 +18,52 @@ #include "gfx-source-texture.hpp" #include #include "obs/gs/gs-helper.hpp" +#include "obs/obs-tools.hpp" streamfx::gfx::source_texture::~source_texture() { if (_child && _parent) { - obs_source_remove_active_child(_parent->get(), _child->get()); + obs_source_remove_active_child(_parent.get(), _child.get()); } - - _parent.reset(); - _child.reset(); } -streamfx::gfx::source_texture::source_texture(obs_source_t* parent) +streamfx::gfx::source_texture::source_texture(streamfx::obs::weak_source child, streamfx::obs::weak_source parent) + : _child(child.lock()), _parent(parent.lock()) { - if (!parent) { - throw std::invalid_argument("_parent must not be null"); + // Verify that 'child' and 'parent' exist. + if (!_child || !_parent) { + throw std::invalid_argument("Child or Parent does not exist."); } - _parent = std::make_shared(parent, false, false); - _rt = std::make_shared(GS_RGBA, GS_ZS_NONE); + + // Verify that 'child' does not contain 'parent'. + if (::streamfx::obs::tools::source_find_source(_child, _parent)) { + throw std::runtime_error("Child contains Parent"); + } else if (!obs_source_add_active_child(_parent.get(), _child.get())) { + throw std::runtime_error("Child contains Parent"); + } + + _rt = std::make_shared(GS_RGBA, GS_ZS_NONE); } -streamfx::gfx::source_texture::source_texture(obs_source_t* _source, obs_source_t* _parent) : source_texture(_parent) -{ - if (!_source) { - throw std::invalid_argument("source must not be null"); - } - if (!obs_source_add_active_child(_parent, _source)) { - throw std::runtime_error("_parent is contained in _child"); - } - _child = std::make_shared(_source, true, true); -} - -streamfx::gfx::source_texture::source_texture(const char* _name, obs_source_t* _parent) : source_texture(_parent) -{ - if (!_name) { - throw std::invalid_argument("name must not be null"); - } - _child = std::make_shared(_name, true, true); - if (!obs_source_add_active_child(_parent, _child->get())) { - throw std::runtime_error("_parent is contained in _child"); - } -} - -streamfx::gfx::source_texture::source_texture(std::string _name, obs_source_t* _parent) - : source_texture(_name.c_str(), _parent) -{} - -streamfx::gfx::source_texture::source_texture(std::shared_ptr pchild, - std::shared_ptr pparent) -{ - if (!pchild) { - throw std::invalid_argument("_child must not be null"); - } - if (!pparent) { - throw std::invalid_argument("_parent must not be null"); - } - if (!obs_source_add_active_child(pparent->get(), pchild->get())) { - throw std::runtime_error("_parent is contained in _child"); - } - this->_child = pchild; - this->_parent = pparent; - this->_rt = std::make_shared(GS_RGBA, GS_ZS_NONE); -} - -streamfx::gfx::source_texture::source_texture(std::shared_ptr _child, - obs_source_t* _parent) - : source_texture(_child, std::make_shared(_parent, false, false)) -{} - obs_source_t* streamfx::gfx::source_texture::get_object() { if (_child) { - return _child->get(); + return _child.get(); } return nullptr; } obs_source_t* streamfx::gfx::source_texture::get_parent() { - return _parent->get(); + return _parent.get(); } void streamfx::gfx::source_texture::clear() { if (_child && _parent) { - obs_source_remove_active_child(_parent->get(), _child->get()); + obs_source_remove_active_child(_parent.get(), _child.get()); } - _child->clear(); - _child.reset(); + _child = {}; } std::shared_ptr streamfx::gfx::source_texture::render(std::size_t width, std::size_t height) @@ -116,7 +74,7 @@ std::shared_ptr streamfx::gfx::source_texture::rende if ((height == 0) || (height >= 16384)) { throw std::runtime_error("Height too large or too small."); } - if (_child->destroyed() || _parent->destroyed()) { + if (!_child || !_parent) { return nullptr; } @@ -130,7 +88,7 @@ std::shared_ptr streamfx::gfx::source_texture::rende vec4_zero(&black); gs_ortho(0, static_cast(width), 0, static_cast(height), 0, 1); gs_clear(GS_CLEAR_COLOR, &black, 0, 0); - obs_source_video_render(_child->get()); + obs_source_video_render(_child.get()); } std::shared_ptr tex; diff --git a/source/gfx/gfx-source-texture.hpp b/source/gfx/gfx-source-texture.hpp index 0b00349..c460b7a 100644 --- a/source/gfx/gfx-source-texture.hpp +++ b/source/gfx/gfx-source-texture.hpp @@ -20,26 +20,18 @@ #include #include "obs/gs/gs-rendertarget.hpp" #include "obs/gs/gs-texture.hpp" -#include "obs/obs-source.hpp" +#include "obs/obs-weak-source.hpp" namespace streamfx::gfx { class source_texture { - std::shared_ptr _parent; - std::shared_ptr _child; + streamfx::obs::source _parent; + streamfx::obs::source _child; std::shared_ptr _rt; - source_texture(obs_source_t* parent); - public: ~source_texture(); - source_texture(obs_source_t* src, obs_source_t* parent); - source_texture(const char* name, obs_source_t* parent); - source_texture(std::string name, obs_source_t* parent); - - source_texture(std::shared_ptr child, - std::shared_ptr parent); - source_texture(std::shared_ptr child, obs_source_t* parent); + source_texture(streamfx::obs::weak_source child, streamfx::obs::weak_source parent); public /*copy*/: source_texture(source_texture const& other) = delete; @@ -58,38 +50,4 @@ namespace streamfx::gfx { obs_source_t* get_object(); obs_source_t* get_parent(); }; - - class source_texture_factory { - friend class source_texture; - - std::map, std::weak_ptr> _cache; - - public: - source_texture_factory(); - ~source_texture_factory(); - - std::shared_ptr get_source_texture(std::shared_ptr source); - - protected: - void free_source_texture(std::shared_ptr source); - - private: // Singleton - static std::shared_ptr factory_instance; - - public: // Singleton - static void initialize() - { - factory_instance = std::make_shared(); - } - - static void finalize() - { - factory_instance.reset(); - } - - static std::shared_ptr get() - { - return factory_instance; - } - }; } // namespace streamfx::gfx