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 "filter_glsl_manager.h"
24 #include <movit/init.h>
25 #include <movit/util.h>
26 #include <movit/effect_chain.h>
27 #include <movit/resource_pool.h>
28 #include "mlt_movit_input.h"
29 #include "mlt_flip_effect.h"
30 #include <mlt++/MltEvent.h>
31 #include <mlt++/MltProducer.h>
34 #include <framework/mlt_factory.h>
37 #if defined(__DARWIN__)
38 #include <OpenGL/OpenGL.h>
45 using namespace movit;
47 void dec_ref_and_delete(GlslManager *p)
49 if (p->dec_ref() == 0) {
54 GlslManager::GlslManager()
55 : Mlt::Filter( mlt_filter_new() )
56 , resource_pool(new ResourcePool())
62 mlt_filter filter = get_filter();
64 // Set the mlt_filter child in case we choose to override virtual functions.
66 add_ref(mlt_global_properties());
68 mlt_events_register( get_properties(), "init glsl", NULL );
69 mlt_events_register( get_properties(), "close glsl", NULL );
70 initEvent = listen("init glsl", this, (mlt_listener) GlslManager::onInit);
71 closeEvent = listen("close glsl", this, (mlt_listener) GlslManager::onClose);
75 GlslManager::~GlslManager()
77 mlt_log_debug(get_service(), "%s\n", __FUNCTION__);
79 // XXX If there is still a frame holding a reference to a texture after this
80 // destructor is called, then it will crash in release_texture().
81 // while (texture_list.peek_back())
82 // delete (glsl_texture) texture_list.pop_back();
85 if (prev_sync != NULL) {
86 glDeleteSync( prev_sync );
88 while (syncs_to_delete.count() > 0) {
89 GLsync sync = (GLsync) syncs_to_delete.pop_front();
95 void GlslManager::add_ref(mlt_properties properties)
98 mlt_properties_set_data(properties, "glslManager", this, 0,
99 (mlt_destructor) dec_ref_and_delete, NULL);
102 GlslManager* GlslManager::get_instance()
104 return (GlslManager*) mlt_properties_get_data(mlt_global_properties(), "glslManager", 0);
107 glsl_texture GlslManager::get_texture(int width, int height, GLint internal_format)
110 for (int i = 0; i < texture_list.count(); ++i) {
111 glsl_texture tex = (glsl_texture) texture_list.peek(i);
112 if (!tex->used && (tex->width == width) && (tex->height == height) && (tex->internal_format == internal_format)) {
113 glBindTexture(GL_TEXTURE_2D, tex->texture);
114 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
115 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
116 glBindTexture( GL_TEXTURE_2D, 0);
125 glGenTextures(1, &tex);
130 glsl_texture gtex = new glsl_texture_s;
132 glDeleteTextures(1, &tex);
136 glBindTexture( GL_TEXTURE_2D, tex );
137 glTexImage2D( GL_TEXTURE_2D, 0, internal_format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
138 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
139 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
140 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
141 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
142 glBindTexture( GL_TEXTURE_2D, 0 );
146 gtex->height = height;
147 gtex->internal_format = internal_format;
150 texture_list.push_back(gtex);
155 void GlslManager::release_texture(glsl_texture texture)
160 void GlslManager::delete_sync(GLsync sync)
162 // We do not know which thread we are called from, and we can only
163 // delete this if we are in one with a valid OpenGL context.
164 // Thus, store it for later deletion in render_frame_texture().
165 GlslManager* g = GlslManager::get_instance();
167 g->syncs_to_delete.push_back(sync);
171 glsl_pbo GlslManager::get_pbo(int size)
176 glGenBuffers(1, &pb);
182 pbo = new glsl_pbo_s;
184 glDeleteBuffers(1, &pb);
191 if (size > pbo->size) {
192 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo->pbo);
193 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, size, NULL, GL_STREAM_DRAW);
194 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
201 void GlslManager::cleanupContext()
204 while (texture_list.peek_back()) {
205 glsl_texture texture = (glsl_texture) texture_list.peek_back();
206 glDeleteTextures(1, &texture->texture);
208 texture_list.pop_back();
211 glDeleteBuffers(1, &pbo->pbo);
218 void GlslManager::onInit( mlt_properties owner, GlslManager* filter )
220 mlt_log_debug( filter->get_service(), "%s\n", __FUNCTION__ );
222 std::string path = std::string(mlt_environment("MLT_APPDIR")).append("\\share\\movit");
223 #elif defined(__DARWIN__) && defined(RELOCATABLE)
224 std::string path = std::string(mlt_environment("MLT_APPDIR")).append("/share/movit");
226 std::string path = std::string(getenv("MLT_MOVIT_PATH") ? getenv("MLT_MOVIT_PATH") : SHADERDIR);
228 bool success = init_movit( path, MOVIT_DEBUG_ON );
229 filter->set( "glsl_supported", success );
232 void GlslManager::onClose( mlt_properties owner, GlslManager *filter )
234 filter->cleanupContext();
237 void GlslManager::onServiceChanged( mlt_properties owner, mlt_service aservice )
239 Mlt::Service service( aservice );
241 service.set( "movit chain", NULL, 0 );
245 void GlslManager::onPropertyChanged( mlt_properties owner, mlt_service service, const char* property )
247 if ( property && std::string( property ) == "disable" )
248 onServiceChanged( owner, service );
253 mlt_filter filter_glsl_manager_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
255 GlslManager* g = GlslManager::get_instance();
259 g = new GlslManager();
260 return g->get_filter();
265 static void deleteChain( GlslChain* chain )
267 // The Input* is owned by the EffectChain, but the MltInput* is not.
268 // Thus, we have to delete it here.
269 for (std::map<mlt_producer, MltInput*>::iterator input_it = chain->inputs.begin();
270 input_it != chain->inputs.end();
272 delete input_it->second;
274 delete chain->effect_chain;
278 void* GlslManager::get_frame_specific_data( mlt_service service, mlt_frame frame, const char *key, int *length )
280 const char *unique_id = mlt_properties_get( MLT_SERVICE_PROPERTIES(service), "_unique_id" );
282 snprintf( buf, sizeof(buf), "%s_%s", key, unique_id );
283 return mlt_properties_get_data( MLT_FRAME_PROPERTIES(frame), buf, length );
286 int GlslManager::set_frame_specific_data( mlt_service service, mlt_frame frame, const char *key, void *value, int length, mlt_destructor destroy, mlt_serialiser serialise )
288 const char *unique_id = mlt_properties_get( MLT_SERVICE_PROPERTIES(service), "_unique_id" );
290 snprintf( buf, sizeof(buf), "%s_%s", key, unique_id );
291 return mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), buf, value, length, destroy, serialise );
294 void GlslManager::set_chain( mlt_service service, GlslChain* chain )
296 mlt_properties_set_data( MLT_SERVICE_PROPERTIES(service), "_movit chain", chain, 0, (mlt_destructor) deleteChain, NULL );
299 GlslChain* GlslManager::get_chain( mlt_service service )
301 return (GlslChain*) mlt_properties_get_data( MLT_SERVICE_PROPERTIES(service), "_movit chain", NULL );
304 Effect* GlslManager::get_effect( mlt_service service, mlt_frame frame )
306 return (Effect*) get_frame_specific_data( service, frame, "_movit effect", NULL );
309 Effect* GlslManager::set_effect( mlt_service service, mlt_frame frame, Effect* effect )
311 set_frame_specific_data( service, frame, "_movit effect", effect, 0, NULL, NULL );
315 MltInput* GlslManager::get_input( mlt_producer producer, mlt_frame frame )
317 return (MltInput*) get_frame_specific_data( MLT_PRODUCER_SERVICE(producer), frame, "_movit input", NULL );
320 MltInput* GlslManager::set_input( mlt_producer producer, mlt_frame frame, MltInput* input )
322 set_frame_specific_data( MLT_PRODUCER_SERVICE(producer), frame, "_movit input", input, 0, NULL, NULL );
326 uint8_t* GlslManager::get_input_pixel_pointer( mlt_producer producer, mlt_frame frame )
328 return (uint8_t*) get_frame_specific_data( MLT_PRODUCER_SERVICE(producer), frame, "_movit input pp", NULL );
331 uint8_t* GlslManager::set_input_pixel_pointer( mlt_producer producer, mlt_frame frame, uint8_t* image )
333 set_frame_specific_data( MLT_PRODUCER_SERVICE(producer), frame, "_movit input pp", image, 0, NULL, NULL );
337 mlt_service GlslManager::get_effect_input( mlt_service service, mlt_frame frame )
339 return (mlt_service) get_frame_specific_data( service, frame, "_movit effect input", NULL );
342 void GlslManager::set_effect_input( mlt_service service, mlt_frame frame, mlt_service input_service )
344 set_frame_specific_data( service, frame, "_movit effect input", input_service, 0, NULL, NULL );
347 void GlslManager::get_effect_secondary_input( mlt_service service, mlt_frame frame, mlt_service *input_service, mlt_frame *input_frame)
349 *input_service = (mlt_service) get_frame_specific_data( service, frame, "_movit effect secondary input", NULL );
350 *input_frame = (mlt_frame) get_frame_specific_data( service, frame, "_movit effect secondary input frame", NULL );
353 void GlslManager::set_effect_secondary_input( mlt_service service, mlt_frame frame, mlt_service input_service, mlt_frame input_frame )
355 set_frame_specific_data( service, frame, "_movit effect secondary input", input_service, 0, NULL, NULL );
356 set_frame_specific_data( service, frame, "_movit effect secondary input frame", input_frame, 0, NULL, NULL );
359 void GlslManager::get_effect_third_input( mlt_service service, mlt_frame frame, mlt_service *input_service, mlt_frame *input_frame)
361 *input_service = (mlt_service) get_frame_specific_data( service, frame, "_movit effect third input", NULL );
362 *input_frame = (mlt_frame) get_frame_specific_data( service, frame, "_movit effect third input frame", NULL );
365 void GlslManager::set_effect_third_input( mlt_service service, mlt_frame frame, mlt_service input_service, mlt_frame input_frame )
367 set_frame_specific_data( service, frame, "_movit effect third input", input_service, 0, NULL, NULL );
368 set_frame_specific_data( service, frame, "_movit effect third input frame", input_frame, 0, NULL, NULL );
371 int GlslManager::render_frame_texture(EffectChain *chain, mlt_frame frame, int width, int height, uint8_t **image)
373 glsl_texture texture = get_texture( width, height, GL_RGBA8 );
379 glGenFramebuffers( 1, &fbo );
381 glBindFramebuffer( GL_FRAMEBUFFER, fbo );
383 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
385 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
389 while (syncs_to_delete.count() > 0) {
390 GLsync sync = (GLsync) syncs_to_delete.pop_front();
391 glDeleteSync( sync );
395 // Make sure we never have more than one frame pending at any time.
396 // This ensures we do not swamp the GPU with so much work
397 // that we cannot actually display the frames we generate.
398 if (prev_sync != NULL) {
400 glClientWaitSync( prev_sync, 0, GL_TIMEOUT_IGNORED );
401 glDeleteSync( prev_sync );
403 chain->render_to_fbo( fbo, width, height );
404 prev_sync = glFenceSync( GL_SYNC_GPU_COMMANDS_COMPLETE, 0 );
405 GLsync sync = glFenceSync( GL_SYNC_GPU_COMMANDS_COMPLETE, 0 );
408 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
410 glDeleteFramebuffers( 1, &fbo );
413 *image = (uint8_t*) &texture->texture;
414 mlt_frame_set_image( frame, *image, 0, NULL );
415 mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), "movit.convert.texture", texture, 0,
416 (mlt_destructor) GlslManager::release_texture, NULL );
417 mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), "movit.convert.fence", sync, 0,
418 (mlt_destructor) GlslManager::delete_sync, NULL );
423 int GlslManager::render_frame_rgba(EffectChain *chain, mlt_frame frame, int width, int height, uint8_t **image)
425 glsl_texture texture = get_texture( width, height, GL_RGBA8 );
430 // Use a PBO to hold the data we read back with glReadPixels().
431 // (Intel/DRI goes into a slow path if we don't read to PBO.)
432 int img_size = width * height * 4;
433 glsl_pbo pbo = get_pbo( img_size );
435 release_texture(texture);
441 glGenFramebuffers( 1, &fbo );
443 glBindFramebuffer( GL_FRAMEBUFFER, fbo );
445 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
447 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
450 chain->render_to_fbo( fbo, width, height );
453 glBindFramebuffer( GL_FRAMEBUFFER, fbo );
455 glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, pbo->pbo );
457 glBufferData( GL_PIXEL_PACK_BUFFER_ARB, img_size, NULL, GL_STREAM_READ );
459 glReadPixels( 0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0) );
463 uint8_t* buf = (uint8_t*) glMapBuffer( GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY );
465 *image = (uint8_t*) mlt_pool_alloc( img_size );
466 mlt_frame_set_image( frame, *image, img_size, mlt_pool_release );
467 memcpy( *image, buf, img_size );
469 // Convert BGRA to RGBA
470 register uint8_t *p = *image;
471 register int n = width * height + 1;
478 // Release PBO and FBO
479 glUnmapBuffer( GL_PIXEL_PACK_BUFFER_ARB );
481 glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, 0 );
483 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
485 glBindTexture( GL_TEXTURE_2D, 0 );
487 mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), "movit.convert.texture", texture, 0,
488 (mlt_destructor) GlslManager::release_texture, NULL);
489 glDeleteFramebuffers( 1, &fbo );
495 void GlslManager::lock_service( mlt_frame frame )
497 Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );
501 void GlslManager::unlock_service( mlt_frame frame )
503 Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );