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 #pragma once
19 #include "common.hpp"
20 #include <map>
21 #include "obs/gs/gs-rendertarget.hpp"
22 #include "obs/gs/gs-texture.hpp"
23 #include "obs/obs-source.hpp"
24 
25 namespace gfx {
26 	class source_texture {
27 		std::shared_ptr<obs::deprecated_source> _parent;
28 		std::shared_ptr<obs::deprecated_source> _child;
29 
30 		std::shared_ptr<gs::rendertarget> _rt;
31 
32 		source_texture(obs_source_t* parent);
33 
34 		public:
35 		~source_texture();
36 		source_texture(obs_source_t* src, obs_source_t* parent);
37 		source_texture(const char* name, obs_source_t* parent);
38 		source_texture(std::string name, obs_source_t* parent);
39 
40 		source_texture(std::shared_ptr<obs::deprecated_source> child, std::shared_ptr<obs::deprecated_source> parent);
41 		source_texture(std::shared_ptr<obs::deprecated_source> child, obs_source_t* parent);
42 
43 		public /*copy*/:
44 		source_texture(source_texture const& other) = delete;
45 		source_texture& operator=(source_texture const& other) = delete;
46 
47 		public /*move*/:
48 		source_texture(source_texture&& other) = delete;
49 		source_texture& operator=(source_texture&& other) = delete;
50 
51 		public:
52 		std::shared_ptr<gs::texture> render(std::size_t width, std::size_t height);
53 
54 		public: // Unsafe Methods
55 		void clear();
56 
57 		obs_source_t* get_object();
58 		obs_source_t* get_parent();
59 	};
60 
61 	class source_texture_factory {
62 		friend class source_texture;
63 
64 		std::map<std::shared_ptr<obs_weak_source_t>, std::weak_ptr<source_texture>> _cache;
65 
66 		public:
67 		source_texture_factory();
68 		~source_texture_factory();
69 
70 		std::shared_ptr<source_texture> get_source_texture(std::shared_ptr<obs_source_t> source);
71 
72 		protected:
73 		void free_source_texture(std::shared_ptr<obs_source_t> source);
74 
75 		private: // Singleton
76 		static std::shared_ptr<source_texture_factory> factory_instance;
77 
78 		public: // Singleton
initialize()79 		static void initialize()
80 		{
81 			factory_instance = std::make_shared<source_texture_factory>();
82 		}
83 
finalize()84 		static void finalize()
85 		{
86 			factory_instance.reset();
87 		}
88 
get()89 		static std::shared_ptr<source_texture_factory> get()
90 		{
91 			return factory_instance;
92 		}
93 	};
94 } // namespace gfx
95