1 // Modern effects for a modern Streamer
2 // Copyright (C) 2017 Michael Fabian Dirks
3 //
4 // This program is free software; you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation; either version 2 of the License, or
7 // (at your option) any later version.
8 //
9 // This program is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with this program; if not, write to the Free Software
16 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
17
18 #include "gfx-source-texture.hpp"
19 #include <stdexcept>
20 #include "obs/gs/gs-helper.hpp"
21
~source_texture()22 gfx::source_texture::~source_texture()
23 {
24 if (_child && _parent) {
25 obs_source_remove_active_child(_parent->get(), _child->get());
26 }
27
28 _parent.reset();
29 _child.reset();
30 }
31
source_texture(obs_source_t * parent)32 gfx::source_texture::source_texture(obs_source_t* parent)
33 {
34 if (!parent) {
35 throw std::invalid_argument("_parent must not be null");
36 }
37 _parent = std::make_shared<obs::deprecated_source>(parent, false, false);
38 _rt = std::make_shared<gs::rendertarget>(GS_RGBA, GS_ZS_NONE);
39 }
40
source_texture(obs_source_t * _source,obs_source_t * _parent)41 gfx::source_texture::source_texture(obs_source_t* _source, obs_source_t* _parent) : source_texture(_parent)
42 {
43 if (!_source) {
44 throw std::invalid_argument("source must not be null");
45 }
46 if (!obs_source_add_active_child(_parent, _source)) {
47 throw std::runtime_error("_parent is contained in _child");
48 }
49 _child = std::make_shared<obs::deprecated_source>(_source, true, true);
50 }
51
source_texture(const char * _name,obs_source_t * _parent)52 gfx::source_texture::source_texture(const char* _name, obs_source_t* _parent) : source_texture(_parent)
53 {
54 if (!_name) {
55 throw std::invalid_argument("name must not be null");
56 }
57 _child = std::make_shared<obs::deprecated_source>(_name, true, true);
58 if (!obs_source_add_active_child(_parent, _child->get())) {
59 throw std::runtime_error("_parent is contained in _child");
60 }
61 }
62
source_texture(std::string _name,obs_source_t * _parent)63 gfx::source_texture::source_texture(std::string _name, obs_source_t* _parent) : source_texture(_name.c_str(), _parent)
64 {}
65
source_texture(std::shared_ptr<obs::deprecated_source> pchild,std::shared_ptr<obs::deprecated_source> pparent)66 gfx::source_texture::source_texture(std::shared_ptr<obs::deprecated_source> pchild,
67 std::shared_ptr<obs::deprecated_source> pparent)
68 {
69 if (!pchild) {
70 throw std::invalid_argument("_child must not be null");
71 }
72 if (!pparent) {
73 throw std::invalid_argument("_parent must not be null");
74 }
75 if (!obs_source_add_active_child(pparent->get(), pchild->get())) {
76 throw std::runtime_error("_parent is contained in _child");
77 }
78 this->_child = pchild;
79 this->_parent = pparent;
80 this->_rt = std::make_shared<gs::rendertarget>(GS_RGBA, GS_ZS_NONE);
81 }
82
source_texture(std::shared_ptr<obs::deprecated_source> _child,obs_source_t * _parent)83 gfx::source_texture::source_texture(std::shared_ptr<obs::deprecated_source> _child, obs_source_t* _parent)
84 : source_texture(_child, std::make_shared<obs::deprecated_source>(_parent, false, false))
85 {}
86
get_object()87 obs_source_t* gfx::source_texture::get_object()
88 {
89 if (_child) {
90 return _child->get();
91 }
92 return nullptr;
93 }
94
get_parent()95 obs_source_t* gfx::source_texture::get_parent()
96 {
97 return _parent->get();
98 }
99
clear()100 void gfx::source_texture::clear()
101 {
102 if (_child && _parent) {
103 obs_source_remove_active_child(_parent->get(), _child->get());
104 }
105 _child->clear();
106 _child.reset();
107 }
108
render(std::size_t width,std::size_t height)109 std::shared_ptr<gs::texture> gfx::source_texture::render(std::size_t width, std::size_t height)
110 {
111 if ((width == 0) || (width >= 16384)) {
112 throw std::runtime_error("Width too large or too small.");
113 }
114 if ((height == 0) || (height >= 16384)) {
115 throw std::runtime_error("Height too large or too small.");
116 }
117 if (_child->destroyed() || _parent->destroyed()) {
118 return nullptr;
119 }
120
121 if (_child) {
122 #ifdef ENABLE_PROFILING
123 auto cctr =
124 gs::debug_marker(gs::debug_color_capture, "gfx::source_texture '%s'", obs_source_get_name(_child->get()));
125 #endif
126 auto op = _rt->render(static_cast<uint32_t>(width), static_cast<uint32_t>(height));
127 vec4 black;
128 vec4_zero(&black);
129 gs_ortho(0, static_cast<float>(width), 0, static_cast<float_t>(height), 0, 1);
130 gs_clear(GS_CLEAR_COLOR, &black, 0, 0);
131 obs_source_video_render(_child->get());
132 }
133
134 std::shared_ptr<gs::texture> tex;
135 _rt->get_texture(tex);
136 return tex;
137 }
138