2018-03-05 14:17:34 +00:00
|
|
|
// Modern effects for a modern Streamer
|
|
|
|
// Copyright (C) 2017 Michael Fabian Dirks
|
|
|
|
//
|
|
|
|
// This program is free software; you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation; either version 2 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License
|
|
|
|
// along with this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
|
2019-01-14 10:23:21 +00:00
|
|
|
#include "gfx-source-texture.hpp"
|
2018-03-05 14:17:34 +00:00
|
|
|
|
2018-09-28 21:31:21 +00:00
|
|
|
gfx::source_texture::~source_texture()
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
if (_child && _parent) {
|
|
|
|
obs_source_remove_active_child(_parent->get(), _child->get());
|
2018-03-05 14:17:34 +00:00
|
|
|
}
|
2018-11-07 13:09:03 +00:00
|
|
|
|
2019-08-04 14:20:26 +00:00
|
|
|
_parent.reset();
|
|
|
|
_child.reset();
|
2018-03-05 14:17:34 +00:00
|
|
|
}
|
|
|
|
|
2019-08-04 18:29:04 +00:00
|
|
|
gfx::source_texture::source_texture(obs_source_t* parent)
|
2018-09-28 21:31:21 +00:00
|
|
|
{
|
2019-08-04 18:29:04 +00:00
|
|
|
if (!parent) {
|
2019-08-04 14:20:26 +00:00
|
|
|
throw std::invalid_argument("_parent must not be null");
|
2018-11-07 13:09:03 +00:00
|
|
|
}
|
2019-08-24 10:59:32 +00:00
|
|
|
_parent = std::make_shared<obs::source>(parent, false, false);
|
|
|
|
_rt = std::make_shared<gs::rendertarget>(GS_RGBA, GS_ZS_NONE);
|
2018-11-07 13:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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)) {
|
2019-08-04 14:20:26 +00:00
|
|
|
throw std::runtime_error("_parent is contained in _child");
|
2018-11-07 13:09:03 +00:00
|
|
|
}
|
2019-08-04 14:20:26 +00:00
|
|
|
_child = std::make_shared<obs::source>(_source, true, true);
|
2018-03-05 14:17:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 22:54:18 +00:00
|
|
|
gfx::source_texture::source_texture(const char* _name, obs_source_t* _parent) : source_texture(_parent)
|
2018-09-28 21:31:21 +00:00
|
|
|
{
|
2018-11-07 13:09:03 +00:00
|
|
|
if (!_name) {
|
|
|
|
throw std::invalid_argument("name must not be null");
|
|
|
|
}
|
2019-08-04 14:20:26 +00:00
|
|
|
_child = std::make_shared<obs::source>(_name, true, true);
|
|
|
|
if (!obs_source_add_active_child(_parent, _child->get())) {
|
|
|
|
throw std::runtime_error("_parent is contained in _child");
|
2018-04-24 11:48:42 +00:00
|
|
|
}
|
2018-03-05 14:17:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-30 22:54:18 +00:00
|
|
|
gfx::source_texture::source_texture(std::string _name, obs_source_t* _parent) : source_texture(_name.c_str(), _parent)
|
|
|
|
{}
|
2018-03-05 14:17:34 +00:00
|
|
|
|
2019-04-19 07:42:15 +00:00
|
|
|
gfx::source_texture::source_texture(std::shared_ptr<obs::source> pchild, std::shared_ptr<obs::source> pparent)
|
2018-09-28 21:31:21 +00:00
|
|
|
{
|
2019-04-19 07:42:15 +00:00
|
|
|
if (!pchild) {
|
2019-08-04 14:20:26 +00:00
|
|
|
throw std::invalid_argument("_child must not be null");
|
2018-03-05 14:17:34 +00:00
|
|
|
}
|
2019-04-19 07:42:15 +00:00
|
|
|
if (!pparent) {
|
2019-08-04 14:20:26 +00:00
|
|
|
throw std::invalid_argument("_parent must not be null");
|
2018-04-24 11:48:42 +00:00
|
|
|
}
|
2019-04-19 07:42:15 +00:00
|
|
|
if (!obs_source_add_active_child(pparent->get(), pchild->get())) {
|
2019-08-04 14:20:26 +00:00
|
|
|
throw std::runtime_error("_parent is contained in _child");
|
2018-11-07 13:09:03 +00:00
|
|
|
}
|
2019-08-24 10:59:32 +00:00
|
|
|
this->_child = pchild;
|
|
|
|
this->_parent = pparent;
|
|
|
|
this->_rt = std::make_shared<gs::rendertarget>(GS_RGBA, GS_ZS_NONE);
|
2018-09-30 22:54:18 +00:00
|
|
|
}
|
|
|
|
|
2019-04-19 07:42:15 +00:00
|
|
|
gfx::source_texture::source_texture(std::shared_ptr<obs::source> _child, obs_source_t* _parent)
|
|
|
|
: source_texture(_child, std::make_shared<obs::source>(_parent, false, false))
|
2018-11-07 13:09:03 +00:00
|
|
|
{}
|
|
|
|
|
2018-09-30 22:54:18 +00:00
|
|
|
obs_source_t* gfx::source_texture::get_object()
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
if (_child) {
|
|
|
|
return _child->get();
|
2018-11-07 13:09:03 +00:00
|
|
|
}
|
|
|
|
return nullptr;
|
2018-09-30 22:54:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
obs_source_t* gfx::source_texture::get_parent()
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
return _parent->get();
|
2018-03-05 14:17:34 +00:00
|
|
|
}
|
|
|
|
|
2018-11-07 13:09:03 +00:00
|
|
|
void gfx::source_texture::clear()
|
2018-09-28 21:31:21 +00:00
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
if (_child && _parent) {
|
|
|
|
obs_source_remove_active_child(_parent->get(), _child->get());
|
2018-03-05 14:17:34 +00:00
|
|
|
}
|
2019-08-04 14:20:26 +00:00
|
|
|
_child->clear();
|
|
|
|
_child.reset();
|
2018-11-07 13:09:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<gs::texture> gfx::source_texture::render(size_t width, size_t height)
|
|
|
|
{
|
2018-03-05 14:17:34 +00:00
|
|
|
if ((width == 0) || (width >= 16384)) {
|
|
|
|
throw std::runtime_error("Width too large or too small.");
|
|
|
|
}
|
|
|
|
if ((height == 0) || (height >= 16384)) {
|
|
|
|
throw std::runtime_error("Height too large or too small.");
|
|
|
|
}
|
2019-08-04 14:20:26 +00:00
|
|
|
if (_child->destroyed() || _parent->destroyed()) {
|
2018-11-08 03:25:43 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
2018-03-05 14:17:34 +00:00
|
|
|
|
|
|
|
{
|
2019-08-04 14:20:26 +00:00
|
|
|
auto op = _rt->render((uint32_t)width, (uint32_t)height);
|
2018-09-28 21:31:21 +00:00
|
|
|
vec4 black;
|
|
|
|
vec4_zero(&black);
|
2018-03-05 14:17:34 +00:00
|
|
|
gs_ortho(0, (float_t)width, 0, (float_t)height, 0, 1);
|
|
|
|
gs_clear(GS_CLEAR_COLOR, &black, 0, 0);
|
2019-08-04 14:20:26 +00:00
|
|
|
if (_child) {
|
|
|
|
obs_source_video_render(_child->get());
|
2018-11-07 13:09:03 +00:00
|
|
|
}
|
2018-03-05 14:17:34 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 11:43:37 +00:00
|
|
|
std::shared_ptr<gs::texture> tex;
|
2019-08-04 14:20:26 +00:00
|
|
|
_rt->get_texture(tex);
|
2018-03-05 14:17:34 +00:00
|
|
|
return tex;
|
|
|
|
}
|