From 75aeb561bddda36ff5da6a04b9a79d40ef3e7e79 Mon Sep 17 00:00:00 2001 From: Michael Fabian 'Xaymar' Dirks Date: Mon, 5 Mar 2018 15:17:34 +0100 Subject: [PATCH] util: Add SourceTexture utility This utility class is used to quickly render and retrieve a Texture from a source. It should be used in place of manual rendering since it can be updated quickly, fixing outstanding issue in all places of the plugin instead of just one. --- CMakeLists.txt | 2 + source/util-source-texture.cpp | 70 ++++++++++++++++++++++++++++++++++ source/util-source-texture.h | 39 +++++++++++++++++++ 3 files changed, 111 insertions(+) create mode 100644 source/util-source-texture.cpp create mode 100644 source/util-source-texture.h diff --git a/CMakeLists.txt b/CMakeLists.txt index f65534bb..e1c6e950 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -52,6 +52,7 @@ SET(obs-stream-effects_HEADERS "${PROJECT_SOURCE_DIR}/source/strings.h" "${PROJECT_SOURCE_DIR}/source/util-math.h" "${PROJECT_SOURCE_DIR}/source/util-memory.h" + "${PROJECT_SOURCE_DIR}/source/util-source-texture.h" ) SET(obs-stream-effects_SOURCES "${PROJECT_SOURCE_DIR}/source/plugin.cpp" @@ -72,6 +73,7 @@ SET(obs-stream-effects_SOURCES "${PROJECT_SOURCE_DIR}/source/gs-vertexbuffer.cpp" "${PROJECT_SOURCE_DIR}/source/util-math.cpp" "${PROJECT_SOURCE_DIR}/source/util-memory.cpp" + "${PROJECT_SOURCE_DIR}/source/util-source-texture.cpp" ) SET(obs-stream-effects_LOCALE "${PROJECT_SOURCE_DIR}/data/locale/en-US.ini" diff --git a/source/util-source-texture.cpp b/source/util-source-texture.cpp new file mode 100644 index 00000000..d31dae48 --- /dev/null +++ b/source/util-source-texture.cpp @@ -0,0 +1,70 @@ +// 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 + +#include "util-source-texture.h" + +util::SourceTexture::~SourceTexture() { + if (m_source) { + obs_source_release(m_source); + m_source = nullptr; + } + m_rt = nullptr; +} + +util::SourceTexture::SourceTexture() { + m_rt = std::make_shared(GS_RGBA, GS_ZS_NONE); +} + +util::SourceTexture::SourceTexture(const char* name) : SourceTexture() { + m_source = obs_get_source_by_name(name); + if (!m_source) { + throw std::invalid_argument("No such source."); + } +} + +util::SourceTexture::SourceTexture(std::string name) : SourceTexture(name.c_str()) {} + +util::SourceTexture::SourceTexture(obs_source_t* src) : SourceTexture() { + m_source = src; + if (!m_source) { + throw std::invalid_argument("No such source."); + } +} + +std::shared_ptr util::SourceTexture::Render(size_t width, size_t height) { + 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."); + } + + { + auto op = m_rt->Render((uint32_t)width, (uint32_t)height); + 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); + } + + std::shared_ptr tex; + m_rt->GetTexture(tex); + return tex; +} diff --git a/source/util-source-texture.h b/source/util-source-texture.h new file mode 100644 index 00000000..695a7613 --- /dev/null +++ b/source/util-source-texture.h @@ -0,0 +1,39 @@ +// 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 + +#pragma once +#include +#include +#include +#include "gs-texture.h" +#include "gs-rendertarget.h" + +namespace util { + class SourceTexture { + obs_source_t* m_source; + std::shared_ptr m_rt; + + SourceTexture(); + public: + ~SourceTexture(); + SourceTexture(const char* name); + SourceTexture(std::string name); + SourceTexture(obs_source_t* src); + + std::shared_ptr Render(size_t width, size_t height); + }; +}