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>
33 void deleteManager(GlslManager *p)
38 GlslManager::GlslManager()
39 : Mlt::Filter( mlt_filter_new() )
42 mlt_filter filter = get_filter();
44 // Set the mlt_filter child in case we choose to override virtual functions.
46 mlt_properties_set_data(mlt_global_properties(), "glslManager", this, 0,
47 (mlt_destructor) deleteManager, NULL);
49 mlt_events_register( get_properties(), "init glsl", NULL );
50 listen("init glsl", this, (mlt_listener) GlslManager::onInit);
54 GlslManager::~GlslManager()
56 mlt_log_debug(get_service(), "%s\n", __FUNCTION__);
57 while (fbo_list.peek_back())
58 delete (glsl_fbo) fbo_list.pop_back();
59 while (texture_list.peek_back())
60 delete (glsl_texture) texture_list.pop_back();
64 GlslManager* GlslManager::get_instance()
66 return (GlslManager*) mlt_properties_get_data(mlt_global_properties(), "glslManager", 0);
69 glsl_fbo GlslManager::get_fbo(int width, int height)
71 for (int i = 0; i < fbo_list.count(); ++i) {
72 glsl_fbo fbo = (glsl_fbo) fbo_list.peek(i);
73 if (!fbo->used && (fbo->width == width) && (fbo->height == height)) {
79 glGenFramebuffers(1, &fb);
83 glsl_fbo fbo = new glsl_fbo_s;
85 glDeleteFramebuffers(1, &fb);
92 fbo_list.push_back(fbo);
96 void GlslManager::release_fbo(glsl_fbo fbo)
101 glsl_texture GlslManager::get_texture(int width, int height, GLint internal_format)
103 for (int i = 0; i < texture_list.count(); ++i) {
104 glsl_texture tex = (glsl_texture) texture_list.peek(i);
105 if (!tex->used && (tex->width == width) && (tex->height == height) && (tex->internal_format == internal_format)) {
106 glBindTexture(GL_TEXTURE_2D, tex->texture);
107 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
108 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
109 glBindTexture( GL_TEXTURE_2D, 0);
115 glGenTextures(1, &tex);
119 glsl_texture gtex = new glsl_texture_s;
121 glDeleteTextures(1, &tex);
124 glBindTexture( GL_TEXTURE_2D, tex );
125 glTexImage2D( GL_TEXTURE_2D, 0, internal_format, width, height, 0, internal_format, GL_UNSIGNED_BYTE, NULL );
126 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
127 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
128 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
129 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
130 glBindTexture( GL_TEXTURE_2D, 0 );
134 gtex->height = height;
135 gtex->internal_format = internal_format;
137 texture_list.push_back(gtex);
141 void GlslManager::release_texture(glsl_texture texture)
146 glsl_pbo GlslManager::get_pbo(int size)
150 glGenBuffers(1, &pb);
154 pbo = new glsl_pbo_s;
156 glDeleteBuffers(1, &pb);
161 if (size > pbo->size) {
162 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo->pbo);
163 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, size, NULL, GL_STREAM_DRAW);
164 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
170 void GlslManager::onInit( mlt_properties owner, GlslManager* filter )
172 mlt_log_debug( filter->get_service(), "%s: %d\n", __FUNCTION__ );
174 std::string path = std::string(mlt_environment("MLT_APPDIR")).append("\\share\\movit");
175 #elif defined(__DARWIN__) && defined(RELOCATABLE)
176 std::string path = std::string(mlt_environment("MLT_APPDIR")).append("/share/movit");
178 std::string path = std::string(getenv("MLT_MOVIT_PATH") ? getenv("MLT_MOVIT_PATH") : SHADERDIR);
180 ::init_movit( path, mlt_log_get_level() == MLT_LOG_DEBUG? MOVIT_DEBUG_ON : MOVIT_DEBUG_OFF );
181 filter->set( "glsl_supported", movit_initialized );
186 mlt_filter filter_glsl_manager_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
188 GlslManager* g = GlslManager::get_instance();
192 g = new GlslManager();
193 return g->get_filter();
198 static void deleteChain( EffectChain* chain )
203 bool GlslManager::init_chain( mlt_service service )
206 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
207 EffectChain* chain = (EffectChain*) mlt_properties_get_data( properties, "movit chain", NULL );
209 mlt_profile profile = mlt_service_profile( service );
210 Input* input = new MltInput( profile->width, profile->height );
211 chain = new EffectChain( profile->display_aspect_num, profile->display_aspect_den );
212 chain->add_input( input );
213 mlt_properties_set_data( properties, "movit chain", chain, 0, (mlt_destructor) deleteChain, NULL );
214 mlt_properties_set_data( properties, "movit input", input, 0, NULL, NULL );
215 mlt_properties_set_int( properties, "_movit finalized", 0 );
221 EffectChain* GlslManager::get_chain( mlt_service service )
223 return (EffectChain*) mlt_properties_get_data( MLT_SERVICE_PROPERTIES(service), "movit chain", NULL );
226 MltInput *GlslManager::get_input( mlt_service service )
228 return (MltInput*) mlt_properties_get_data( MLT_SERVICE_PROPERTIES(service), "movit input", NULL );
231 void GlslManager::reset_finalized( mlt_service service )
233 mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_movit finalized", 0 );
236 Effect* GlslManager::get_effect( mlt_filter filter, mlt_frame frame )
238 mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
239 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
240 char *unique_id = mlt_properties_get( MLT_FILTER_PROPERTIES(filter), "_unique_id" );
241 return (Effect*) mlt_properties_get_data( properties, unique_id, NULL );
244 Effect* GlslManager::add_effect( mlt_filter filter, mlt_frame frame, Effect* effect )
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 EffectChain* chain = (EffectChain*) mlt_properties_get_data( properties, "movit chain", NULL );
250 chain->add_effect( effect );
251 mlt_properties_set_data( properties, unique_id, effect, 0, NULL, NULL );
255 Effect* GlslManager::add_effect( mlt_filter filter, mlt_frame frame, Effect* effect, Effect* input_b )
257 mlt_producer producer = mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) );
258 mlt_properties properties = MLT_PRODUCER_PROPERTIES( producer );
259 char *unique_id = mlt_properties_get( MLT_FILTER_PROPERTIES(filter), "_unique_id" );
260 EffectChain* chain = (EffectChain*) mlt_properties_get_data( properties, "movit chain", NULL );
261 chain->add_effect( effect, chain->last_added_effect(),
262 input_b? input_b : chain->last_added_effect() );
263 mlt_properties_set_data( properties, unique_id, effect, 0, NULL, NULL );
267 void GlslManager::render( mlt_service service, void* chain, GLuint fbo, int width, int height )
269 EffectChain* effect_chain = (EffectChain*) chain;
270 mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
271 if ( !mlt_properties_get_int( properties, "_movit finalized" ) ) {
272 mlt_properties_set_int( properties, "_movit finalized", 1 );
273 effect_chain->add_effect( new Mlt::VerticalFlip() );
274 effect_chain->finalize();
276 effect_chain->render_to_fbo( fbo, width, height );