]> git.sesse.net Git - mlt/blob - src/modules/opengl/filter_glsl_manager.cpp
Add the new opengl module (opengl branch).
[mlt] / src / modules / opengl / filter_glsl_manager.cpp
1 /*
2  * filter_glsl_manager.cpp
3  * Copyright (C) 2011-2012 Christophe Thommeret <hftom@free.fr>
4  * Copyright (C) 2013 Dan Dennedy <dan@dennedy.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software Foundation,
18  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19  */
20
21 #include <stdlib.h>
22 #include <string>
23 #include "glsl_manager.h"
24 #include <movit/init.h>
25 #include <movit/effect_chain.h>
26 #include "mlt_movit_input.h"
27 #include "mlt_flip_effect.h"
28
29 extern "C" {
30 #include <framework/mlt_factory.h>
31 }
32
33 #ifdef WIN32
34 #define SYS_gettid (224)
35 #define syscall(X) (((X == SYS_gettid) && GetCurrentThreadId()) || 0)
36 #else
37 #include <unistd.h>
38 #include <sys/syscall.h>
39 #endif
40
41 void deleteManager(GlslManager *p)
42 {
43         delete p;
44 }
45
46 GlslManager::GlslManager()
47         : Mlt::Filter( mlt_filter_new() )
48         , pbo(0)
49 {
50         mlt_filter filter = get_filter();
51         if ( filter ) {
52                 // Set the mlt_filter child in case we choose to override virtual functions.
53                 filter->child = this;
54                 mlt_properties_set_data(mlt_global_properties(), "glslManager", this, 0,
55                         (mlt_destructor) deleteManager, NULL);
56
57                 mlt_events_register( get_properties(), "init glsl", NULL );
58                 listen("init glsl", this, (mlt_listener) GlslManager::onInit);
59         }
60 }
61
62 GlslManager::~GlslManager()
63 {
64         mlt_log_debug(get_service(), "%s\n", __FUNCTION__);
65         while (fbo_list.peek_back())
66                 delete (glsl_fbo) fbo_list.pop_back();
67         while (texture_list.peek_back())
68                 delete (glsl_texture) texture_list.pop_back();
69         delete pbo;
70 }
71
72 GlslManager* GlslManager::get_instance()
73 {
74         return (GlslManager*) mlt_properties_get_data(mlt_global_properties(), "glslManager", 0);
75 }
76
77 glsl_fbo GlslManager::get_fbo(int width, int height)
78 {
79         for (int i = 0; i < fbo_list.count(); ++i) {
80                 glsl_fbo fbo = (glsl_fbo) fbo_list.peek(i);
81                 if (!fbo->used && (fbo->width == width) && (fbo->height == height)) {
82                         fbo->used = 1;
83                         return fbo;
84                 }
85         }
86         GLuint fb = 0;
87         glGenFramebuffers(1, &fb);
88         if (!fb)
89                 return NULL;
90
91         glsl_fbo fbo = new glsl_fbo_s;
92         if (!fbo) {
93                 glDeleteFramebuffers(1, &fb);
94                 return NULL;
95         }
96         fbo->fbo = fb;
97         fbo->width = width;
98         fbo->height = height;
99         fbo->used = 1;
100         fbo_list.push_back(fbo);
101         return fbo;
102 }
103
104 void GlslManager::release_fbo(glsl_fbo fbo)
105 {
106         fbo->used = 0;
107 }
108
109 glsl_texture GlslManager::get_texture(int width, int height, GLint internal_format)
110 {
111         for (int i = 0; i < texture_list.count(); ++i) {
112                 glsl_texture tex = (glsl_texture) texture_list.peek(i);
113                 if (!tex->used && (tex->width == width) && (tex->height == height) && (tex->internal_format == internal_format)) {
114                         glBindTexture(GL_TEXTURE_2D, tex->texture);
115                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
116                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
117                         glBindTexture( GL_TEXTURE_2D, 0);
118                         tex->used = 1;
119                         return tex;
120                 }
121         }
122         GLuint tex = 0;
123         glGenTextures(1, &tex);
124         if (!tex)
125                 return NULL;
126
127         glsl_texture gtex = new glsl_texture_s;
128         if (!gtex) {
129                 glDeleteTextures(1, &tex);
130                 return NULL;
131         }
132         glBindTexture( GL_TEXTURE_2D, tex );
133         glTexImage2D( GL_TEXTURE_2D, 0, internal_format, width, height, 0, internal_format, GL_UNSIGNED_BYTE, NULL );
134     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
135     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
136     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
137     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
138     glBindTexture( GL_TEXTURE_2D, 0 );
139
140         gtex->texture = tex;
141         gtex->width = width;
142         gtex->height = height;
143         gtex->internal_format = internal_format;
144         gtex->used = 1;
145         texture_list.push_back(gtex);
146         return gtex;
147 }
148
149 void GlslManager::release_texture(glsl_texture texture)
150 {
151         texture->used = 0;
152 }
153
154 glsl_pbo GlslManager::get_pbo(int size)
155 {
156         if (!pbo) {
157                 GLuint pb = 0;
158                 glGenBuffers(1, &pb);
159                 if (!pb)
160                         return NULL;
161
162                 pbo = new glsl_pbo_s;
163                 if (!pbo) {
164                         glDeleteBuffers(1, &pb);
165                         return NULL;
166                 }
167                 pbo->pbo = pb;
168         }
169         if (size > pbo->size) {
170                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo->pbo);
171                 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, size, NULL, GL_STREAM_DRAW);
172                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
173                 pbo->size = size;
174         }
175         return pbo;
176 }
177
178 void GlslManager::onInit( mlt_properties owner, GlslManager* filter )
179 {
180         mlt_log_verbose( filter->get_service(), "%s: %d\n", __FUNCTION__, syscall(SYS_gettid) );
181 #ifdef WIN32
182         std::string path = std::string(mlt_environment("MLT_APPDIR")).append("\\share\\movit");
183 #elif defined(__DARWIN__) && defined(RELOCATABLE)
184         std::string path = std::string(mlt_environment("MLT_APPDIR")).append("/share/movit");
185 #else
186         std::string path = std::string(getenv("MLT_MOVIT_PATH") ? getenv("MLT_MOVIT_PATH") : SHADERDIR);
187 #endif
188         ::init_movit( path, mlt_log_get_level() == MLT_LOG_DEBUG? MOVIT_DEBUG_ON : MOVIT_DEBUG_OFF );
189         filter->set( "glsl_supported", movit_initialized );
190 }
191
192 extern "C" {
193
194 mlt_filter filter_glsl_manager_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
195 {
196         GlslManager* g = GlslManager::get_instance();
197         if (g)
198                 g->inc_ref();
199         else
200                 g = new GlslManager();
201         return g->get_filter();
202 }
203
204 } // extern "C"
205
206 static void deleteChain( EffectChain* chain )
207 {
208         delete chain;
209 }
210
211 bool GlslManager::init_chain( mlt_service service )
212 {
213         bool error = true;
214         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
215         EffectChain* chain = (EffectChain*) mlt_properties_get_data( properties, "movit chain", NULL );
216         if ( !chain ) {
217                 mlt_profile profile = mlt_service_profile( service );
218                 Input* input = new MltInput( profile->width, profile->height );
219                 chain = new EffectChain( profile->display_aspect_num, profile->display_aspect_den );
220                 chain->add_input( input );
221                 mlt_properties_set_data( properties, "movit chain", chain, 0, (mlt_destructor) deleteChain, NULL );
222                 mlt_properties_set_data( properties, "movit input", input, 0, NULL, NULL );
223                 mlt_properties_set_int( properties, "_movit finalized", 0 );
224                 error = false;
225         }
226         return error;
227 }
228
229 EffectChain* GlslManager::get_chain( mlt_service service )
230 {
231         return (EffectChain*) mlt_properties_get_data( MLT_SERVICE_PROPERTIES(service), "movit chain", NULL );
232 }
233
234 MltInput *GlslManager::get_input( mlt_service service )
235 {
236         return (MltInput*) mlt_properties_get_data( MLT_SERVICE_PROPERTIES(service), "movit input", NULL );
237 }
238
239 void GlslManager::reset_finalized( mlt_service service )
240 {
241         mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_movit finalized", 0 );
242 }
243
244 Effect* GlslManager::get_effect( mlt_filter filter, mlt_frame frame )
245 {
246         mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
247         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
248         char *unique_id = mlt_properties_get( MLT_FILTER_PROPERTIES(filter), "_unique_id" );
249         return (Effect*) mlt_properties_get_data( properties, unique_id, NULL );
250 }
251
252 Effect* GlslManager::add_effect( mlt_filter filter, mlt_frame frame, Effect* effect )
253 {
254         mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
255         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
256         char *unique_id = mlt_properties_get( MLT_FILTER_PROPERTIES(filter), "_unique_id" );
257         EffectChain* chain = (EffectChain*) mlt_properties_get_data( properties, "movit chain", NULL );
258         chain->add_effect( effect );
259         mlt_properties_set_data( properties, unique_id, effect, 0, NULL, NULL );
260         return effect;
261 }
262
263 Effect* GlslManager::add_effect( mlt_filter filter, mlt_frame frame, Effect* effect, Effect* input_b )
264 {
265         mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
266         mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
267         char *unique_id = mlt_properties_get( MLT_FILTER_PROPERTIES(filter), "_unique_id" );
268         EffectChain* chain = (EffectChain*) mlt_properties_get_data( properties, "movit chain", NULL );
269         chain->add_effect( effect, chain->last_added_effect(),
270                 input_b? input_b : chain->last_added_effect() );
271         mlt_properties_set_data( properties, unique_id, effect, 0, NULL, NULL );
272         return effect;
273 }
274
275 void GlslManager::render( mlt_service service, void* chain, GLuint fbo, int width, int height )
276 {
277         EffectChain* effect_chain = (EffectChain*) chain;
278         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
279         if ( !mlt_properties_get_int( properties, "_movit finalized" ) ) {
280                 mlt_properties_set_int( properties, "_movit finalized", 1 );
281                 effect_chain->add_effect( new Mlt::VerticalFlip() );
282                 effect_chain->finalize();
283         }
284         effect_chain->render_to_fbo( fbo, width, height );
285 }