2 * filter_glsl_manager.cpp
3 * Copyright (C) 2011-2012 Christophe Thommeret <hftom@free.fr>
4 * Copyright (C) 2013 Dan Dennedy <dan@dennedy.org>
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.
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.
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.
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"
30 #include <framework/mlt_factory.h>
34 #define SYS_gettid (224)
35 #define syscall(X) (((X == SYS_gettid) && GetCurrentThreadId()) || 0)
38 #include <sys/syscall.h>
41 void deleteManager(GlslManager *p)
46 GlslManager::GlslManager()
47 : Mlt::Filter( mlt_filter_new() )
50 mlt_filter filter = get_filter();
52 // Set the mlt_filter child in case we choose to override virtual functions.
54 mlt_properties_set_data(mlt_global_properties(), "glslManager", this, 0,
55 (mlt_destructor) deleteManager, NULL);
57 mlt_events_register( get_properties(), "init glsl", NULL );
58 listen("init glsl", this, (mlt_listener) GlslManager::onInit);
62 GlslManager::~GlslManager()
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();
72 GlslManager* GlslManager::get_instance()
74 return (GlslManager*) mlt_properties_get_data(mlt_global_properties(), "glslManager", 0);
77 glsl_fbo GlslManager::get_fbo(int width, int height)
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)) {
87 glGenFramebuffers(1, &fb);
91 glsl_fbo fbo = new glsl_fbo_s;
93 glDeleteFramebuffers(1, &fb);
100 fbo_list.push_back(fbo);
104 void GlslManager::release_fbo(glsl_fbo fbo)
109 glsl_texture GlslManager::get_texture(int width, int height, GLint internal_format)
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);
123 glGenTextures(1, &tex);
127 glsl_texture gtex = new glsl_texture_s;
129 glDeleteTextures(1, &tex);
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 );
142 gtex->height = height;
143 gtex->internal_format = internal_format;
145 texture_list.push_back(gtex);
149 void GlslManager::release_texture(glsl_texture texture)
154 glsl_pbo GlslManager::get_pbo(int size)
158 glGenBuffers(1, &pb);
162 pbo = new glsl_pbo_s;
164 glDeleteBuffers(1, &pb);
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);
178 void GlslManager::onInit( mlt_properties owner, GlslManager* filter )
180 mlt_log_verbose( filter->get_service(), "%s: %d\n", __FUNCTION__, syscall(SYS_gettid) );
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");
186 std::string path = std::string(getenv("MLT_MOVIT_PATH") ? getenv("MLT_MOVIT_PATH") : SHADERDIR);
188 ::init_movit( path, mlt_log_get_level() == MLT_LOG_DEBUG? MOVIT_DEBUG_ON : MOVIT_DEBUG_OFF );
189 filter->set( "glsl_supported", movit_initialized );
194 mlt_filter filter_glsl_manager_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
196 GlslManager* g = GlslManager::get_instance();
200 g = new GlslManager();
201 return g->get_filter();
206 static void deleteChain( EffectChain* chain )
211 bool GlslManager::init_chain( mlt_service service )
214 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
215 EffectChain* chain = (EffectChain*) mlt_properties_get_data( properties, "movit chain", NULL );
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 );
229 EffectChain* GlslManager::get_chain( mlt_service service )
231 return (EffectChain*) mlt_properties_get_data( MLT_SERVICE_PROPERTIES(service), "movit chain", NULL );
234 MltInput *GlslManager::get_input( mlt_service service )
236 return (MltInput*) mlt_properties_get_data( MLT_SERVICE_PROPERTIES(service), "movit input", NULL );
239 void GlslManager::reset_finalized( mlt_service service )
241 mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_movit finalized", 0 );
244 Effect* GlslManager::get_effect( mlt_filter filter, mlt_frame frame )
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 );
252 Effect* GlslManager::add_effect( mlt_filter filter, mlt_frame frame, Effect* effect )
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 );
263 Effect* GlslManager::add_effect( mlt_filter filter, mlt_frame frame, Effect* effect, Effect* input_b )
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 );
275 void GlslManager::render( mlt_service service, void* chain, GLuint fbo, int width, int height )
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();
284 effect_chain->render_to_fbo( fbo, width, height );