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
|
|
|
|
|
2018-03-08 10:28:23 +00:00
|
|
|
#include "gfx-source-texture.h"
|
2018-03-05 14:17:34 +00:00
|
|
|
|
2018-03-20 11:50:17 +00:00
|
|
|
gfx::source_texture::~source_texture() {
|
2018-04-24 11:48:42 +00:00
|
|
|
obs_source_remove_active_child(m_parent, m_source);
|
2018-03-05 14:17:34 +00:00
|
|
|
if (m_source) {
|
|
|
|
obs_source_release(m_source);
|
|
|
|
m_source = nullptr;
|
|
|
|
}
|
|
|
|
m_rt = nullptr;
|
|
|
|
}
|
|
|
|
|
2018-04-24 11:48:42 +00:00
|
|
|
gfx::source_texture::source_texture(obs_source_t* parent) {
|
2018-03-20 11:43:37 +00:00
|
|
|
m_rt = std::make_shared<gs::rendertarget>(GS_RGBA, GS_ZS_NONE);
|
2018-04-24 11:48:42 +00:00
|
|
|
m_parent = parent;
|
2018-03-05 14:17:34 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 11:50:17 +00:00
|
|
|
obs_source_t* gfx::source_texture::get_object() {
|
2018-03-05 15:19:42 +00:00
|
|
|
return m_source;
|
|
|
|
}
|
|
|
|
|
2018-04-24 11:48:42 +00:00
|
|
|
obs_source_t* gfx::source_texture::get_parent() {
|
|
|
|
return m_parent;
|
|
|
|
}
|
|
|
|
|
|
|
|
gfx::source_texture::source_texture(const char* name, obs_source_t* parent) : source_texture(parent) {
|
2018-03-05 14:17:34 +00:00
|
|
|
m_source = obs_get_source_by_name(name);
|
|
|
|
if (!m_source) {
|
|
|
|
throw std::invalid_argument("No such source.");
|
|
|
|
}
|
2018-04-24 11:48:42 +00:00
|
|
|
if (!obs_source_add_active_child(m_parent, m_source)) {
|
|
|
|
throw std::runtime_error("Recursion is not allowed.");
|
|
|
|
}
|
2018-03-05 14:17:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-24 11:48:42 +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
|
|
|
|
2018-04-24 11:48:42 +00:00
|
|
|
gfx::source_texture::source_texture(obs_source_t* src, obs_source_t* parent) : source_texture(parent) {
|
2018-03-05 14:17:34 +00:00
|
|
|
m_source = src;
|
|
|
|
if (!m_source) {
|
|
|
|
throw std::invalid_argument("No such source.");
|
|
|
|
}
|
2018-04-24 11:48:42 +00:00
|
|
|
if (!obs_source_add_active_child(m_parent, m_source)) {
|
|
|
|
throw std::runtime_error("Recursion is not allowed.");
|
|
|
|
}
|
2018-03-05 14:17:34 +00:00
|
|
|
}
|
|
|
|
|
2018-03-20 11:50:17 +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 (!m_source) {
|
|
|
|
throw std::invalid_argument("Missing source to render.");
|
|
|
|
}
|
|
|
|
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.");
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
2018-03-20 11:50:17 +00:00
|
|
|
auto op = m_rt->render((uint32_t)width, (uint32_t)height);
|
2018-03-05 14:17:34 +00:00
|
|
|
vec4 black; vec4_zero(&black);
|
|
|
|
gs_ortho(0, (float_t)width, 0, (float_t)height, 0, 1);
|
|
|
|
gs_clear(GS_CLEAR_COLOR, &black, 0, 0);
|
|
|
|
obs_source_video_render(m_source);
|
|
|
|
}
|
|
|
|
|
2018-03-20 11:43:37 +00:00
|
|
|
std::shared_ptr<gs::texture> tex;
|
2018-03-20 11:50:17 +00:00
|
|
|
m_rt->get_texture(tex);
|
2018-03-05 14:17:34 +00:00
|
|
|
return tex;
|
|
|
|
}
|