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 "mlt_movit_input.h"
28 #include "mlt_flip_effect.h"
29 #include <mlt++/MltEvent.h>
30 #include <mlt++/MltProducer.h>
33 #include <framework/mlt_factory.h>
36 #if defined(__DARWIN__)
37 #include <OpenGL/OpenGL.h>
44 void dec_ref_and_delete(GlslManager *p)
46 if (p->dec_ref() == 0) {
51 GlslManager::GlslManager()
52 : Mlt::Filter( mlt_filter_new() )
58 mlt_filter filter = get_filter();
60 // Set the mlt_filter child in case we choose to override virtual functions.
62 add_ref(mlt_global_properties());
64 mlt_events_register( get_properties(), "init glsl", NULL );
65 mlt_events_register( get_properties(), "close glsl", NULL );
66 initEvent = listen("init glsl", this, (mlt_listener) GlslManager::onInit);
67 closeEvent = listen("close glsl", this, (mlt_listener) GlslManager::onClose);
71 GlslManager::~GlslManager()
73 mlt_log_debug(get_service(), "%s\n", __FUNCTION__);
75 // XXX If there is still a frame holding a reference to a texture after this
76 // destructor is called, then it will crash in release_texture().
77 // while (texture_list.peek_back())
78 // delete (glsl_texture) texture_list.pop_back();
81 if (prev_sync != NULL) {
82 glDeleteSync( prev_sync );
84 while (syncs_to_delete.count() > 0) {
85 GLsync sync = (GLsync) syncs_to_delete.pop_front();
90 void GlslManager::add_ref(mlt_properties properties)
93 mlt_properties_set_data(properties, "glslManager", this, 0,
94 (mlt_destructor) dec_ref_and_delete, NULL);
97 GlslManager* GlslManager::get_instance()
99 return (GlslManager*) mlt_properties_get_data(mlt_global_properties(), "glslManager", 0);
102 glsl_fbo GlslManager::get_fbo(int width, int height)
104 #if defined(__DARWIN__)
105 CGLContextObj context = CGLGetCurrentContext();
107 HGLRC context = wglGetCurrentContext();
109 GLXContext context = glXGetCurrentContext();
113 for (int i = 0; i < fbo_list.count(); ++i) {
114 glsl_fbo fbo = (glsl_fbo) fbo_list.peek(i);
115 if (!fbo->used && (fbo->width == width) && (fbo->height == height) && (fbo->context == context)) {
124 glGenFramebuffers(1, &fb);
128 glsl_fbo fbo = new glsl_fbo_s;
130 glDeleteFramebuffers(1, &fb);
135 fbo->height = height;
137 fbo->context = context;
139 fbo_list.push_back(fbo);
144 void GlslManager::release_fbo(glsl_fbo fbo)
149 glsl_texture GlslManager::get_texture(int width, int height, GLint internal_format)
152 for (int i = 0; i < texture_list.count(); ++i) {
153 glsl_texture tex = (glsl_texture) texture_list.peek(i);
154 if (!tex->used && (tex->width == width) && (tex->height == height) && (tex->internal_format == internal_format)) {
155 glBindTexture(GL_TEXTURE_2D, tex->texture);
156 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
157 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
158 glBindTexture( GL_TEXTURE_2D, 0);
165 // Recycle a glsl_texture with deleted glTexture.
166 glsl_texture gtex = 0;
167 for (int i = 0; i < texture_list.count(); ++i) {
168 glsl_texture tex = (glsl_texture) texture_list.peek(i);
169 if (!tex->used && !tex->width && !tex->height) {
177 glGenTextures(1, &tex);
179 glDeleteTextures(1, &tex);
184 gtex = new glsl_texture_s;
189 glBindTexture( GL_TEXTURE_2D, tex );
190 glTexImage2D( GL_TEXTURE_2D, 0, internal_format, width, height, 0, internal_format, GL_UNSIGNED_BYTE, NULL );
191 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
192 glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
193 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
194 glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
195 glBindTexture( GL_TEXTURE_2D, 0 );
199 gtex->height = height;
200 gtex->internal_format = internal_format;
203 texture_list.push_back(gtex);
208 void GlslManager::release_texture(glsl_texture texture)
213 void GlslManager::delete_sync(GLsync sync)
215 // We do not know which thread we are called from, and we can only
216 // delete this if we are in one with a valid OpenGL context.
217 // Thus, store it for later deletion in render_frame_texture().
218 GlslManager* g = GlslManager::get_instance();
220 g->syncs_to_delete.push_back(sync);
224 glsl_pbo GlslManager::get_pbo(int size)
229 glGenBuffers(1, &pb);
235 pbo = new glsl_pbo_s;
237 glDeleteBuffers(1, &pb);
244 if (size > pbo->size) {
245 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo->pbo);
246 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, size, NULL, GL_STREAM_DRAW);
247 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
254 void GlslManager::cleanupContext()
257 while (fbo_list.peek_back()) {
258 glsl_fbo fbo = (glsl_fbo) fbo_list.pop_back();
259 glDeleteFramebuffers(1, &fbo->fbo);
262 for (int i = 0; i < texture_list.count(); ++i) {
263 glsl_texture texture = (glsl_texture) texture_list.peek(i);
264 glDeleteTextures(1, &texture->texture);
270 glDeleteBuffers(1, &pbo->pbo);
277 void GlslManager::onInit( mlt_properties owner, GlslManager* filter )
279 mlt_log_debug( filter->get_service(), "%s\n", __FUNCTION__ );
281 std::string path = std::string(mlt_environment("MLT_APPDIR")).append("\\share\\movit");
282 #elif defined(__DARWIN__) && defined(RELOCATABLE)
283 std::string path = std::string(mlt_environment("MLT_APPDIR")).append("/share/movit");
285 std::string path = std::string(getenv("MLT_MOVIT_PATH") ? getenv("MLT_MOVIT_PATH") : SHADERDIR);
287 ::init_movit( path, mlt_log_get_level() == MLT_LOG_DEBUG? MOVIT_DEBUG_ON : MOVIT_DEBUG_OFF );
288 filter->set( "glsl_supported", movit_initialized );
291 void GlslManager::onClose( mlt_properties owner, GlslManager *filter )
293 filter->cleanupContext();
296 void GlslManager::onServiceChanged( mlt_properties owner, mlt_service aservice )
298 Mlt::Service service( aservice );
300 service.set( "movit chain", NULL, 0 );
304 void GlslManager::onPropertyChanged( mlt_properties owner, mlt_service service, const char* property )
306 if ( property && std::string( property ) == "disable" )
307 onServiceChanged( owner, service );
312 mlt_filter filter_glsl_manager_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
314 GlslManager* g = GlslManager::get_instance();
318 g = new GlslManager();
319 return g->get_filter();
324 static void deleteChain( GlslChain* chain )
326 delete chain->effect_chain;
330 void* GlslManager::get_frame_specific_data( mlt_service service, mlt_frame frame, const char *key, int *length )
332 const char *unique_id = mlt_properties_get( MLT_SERVICE_PROPERTIES(service), "_unique_id" );
334 snprintf( buf, sizeof(buf), "%s_%s", key, unique_id );
335 return mlt_properties_get_data( MLT_FRAME_PROPERTIES(frame), buf, length );
338 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 )
340 const char *unique_id = mlt_properties_get( MLT_SERVICE_PROPERTIES(service), "_unique_id" );
342 snprintf( buf, sizeof(buf), "%s_%s", key, unique_id );
343 return mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), buf, value, length, destroy, serialise );
346 void GlslManager::set_chain( mlt_service service, GlslChain* chain )
348 mlt_properties_set_data( MLT_SERVICE_PROPERTIES(service), "_movit chain", chain, 0, (mlt_destructor) deleteChain, NULL );
351 GlslChain* GlslManager::get_chain( mlt_service service )
353 return (GlslChain*) mlt_properties_get_data( MLT_SERVICE_PROPERTIES(service), "_movit chain", NULL );
356 Effect* GlslManager::get_effect( mlt_service service, mlt_frame frame )
358 return (Effect*) get_frame_specific_data( service, frame, "_movit effect", NULL );
361 Effect* GlslManager::set_effect( mlt_service service, mlt_frame frame, Effect* effect )
363 set_frame_specific_data( service, frame, "_movit effect", effect, 0, NULL, NULL );
367 MltInput* GlslManager::get_input( mlt_producer producer, mlt_frame frame )
369 return (MltInput*) get_frame_specific_data( MLT_PRODUCER_SERVICE(producer), frame, "_movit input", NULL );
372 MltInput* GlslManager::set_input( mlt_producer producer, mlt_frame frame, MltInput* input )
374 set_frame_specific_data( MLT_PRODUCER_SERVICE(producer), frame, "_movit input", input, 0, NULL, NULL );
378 uint8_t* GlslManager::get_input_pixel_pointer( mlt_producer producer, mlt_frame frame )
380 return (uint8_t*) get_frame_specific_data( MLT_PRODUCER_SERVICE(producer), frame, "_movit input pp", NULL );
383 uint8_t* GlslManager::set_input_pixel_pointer( mlt_producer producer, mlt_frame frame, uint8_t* image )
385 set_frame_specific_data( MLT_PRODUCER_SERVICE(producer), frame, "_movit input pp", image, 0, NULL, NULL );
389 mlt_service GlslManager::get_effect_input( mlt_service service, mlt_frame frame )
391 return (mlt_service) get_frame_specific_data( service, frame, "_movit effect input", NULL );
394 void GlslManager::set_effect_input( mlt_service service, mlt_frame frame, mlt_service input_service )
396 set_frame_specific_data( service, frame, "_movit effect input", input_service, 0, NULL, NULL );
399 void GlslManager::get_effect_secondary_input( mlt_service service, mlt_frame frame, mlt_service *input_service, mlt_frame *input_frame)
401 *input_service = (mlt_service) get_frame_specific_data( service, frame, "_movit effect secondary input", NULL );
402 *input_frame = (mlt_frame) get_frame_specific_data( service, frame, "_movit effect secondary input frame", NULL );
405 void GlslManager::set_effect_secondary_input( mlt_service service, mlt_frame frame, mlt_service input_service, mlt_frame input_frame )
407 set_frame_specific_data( service, frame, "_movit effect secondary input", input_service, 0, NULL, NULL );
408 set_frame_specific_data( service, frame, "_movit effect secondary input frame", input_frame, 0, NULL, NULL );
411 int GlslManager::render_frame_texture(EffectChain *chain, mlt_frame frame, int width, int height, uint8_t **image)
413 glsl_fbo fbo = get_fbo( width, height );
415 glsl_texture texture = get_texture( width, height, GL_RGBA );
421 glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
423 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
425 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
429 while (syncs_to_delete.count() > 0) {
430 GLsync sync = (GLsync) syncs_to_delete.pop_front();
431 glDeleteSync( sync );
435 // Make sure we never have more than one frame pending at any time.
436 // This ensures we do not swamp the GPU with so much work
437 // that we cannot actually display the frames we generate.
438 if (prev_sync != NULL) {
440 glClientWaitSync( prev_sync, 0, GL_TIMEOUT_IGNORED );
441 glDeleteSync( prev_sync );
443 chain->render_to_fbo( fbo->fbo, width, height );
444 prev_sync = glFenceSync( GL_SYNC_GPU_COMMANDS_COMPLETE, 0 );
445 GLsync sync = glFenceSync( GL_SYNC_GPU_COMMANDS_COMPLETE, 0 );
448 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
452 *image = (uint8_t*) &texture->texture;
453 mlt_frame_set_image( frame, *image, 0, NULL );
454 mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), "movit.convert.texture", texture, 0,
455 (mlt_destructor) GlslManager::release_texture, NULL );
456 mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), "movit.convert.fence", sync, 0,
457 (mlt_destructor) GlslManager::delete_sync, NULL );
462 int GlslManager::render_frame_rgba(EffectChain *chain, mlt_frame frame, int width, int height, uint8_t **image)
464 glsl_fbo fbo = get_fbo( width, height );
466 glsl_texture texture = get_texture( width, height, GL_RGBA );
472 // Use a PBO to hold the data we read back with glReadPixels().
473 // (Intel/DRI goes into a slow path if we don't read to PBO.)
474 int img_size = width * height * 4;
475 glsl_pbo pbo = get_pbo( img_size );
478 release_texture(texture);
484 glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
486 glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
488 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
491 chain->render_to_fbo( fbo->fbo, width, height );
494 glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
496 glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, pbo->pbo );
498 glBufferData( GL_PIXEL_PACK_BUFFER_ARB, img_size, NULL, GL_STREAM_READ );
500 glReadPixels( 0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0) );
504 uint8_t* buf = (uint8_t*) glMapBuffer( GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY );
506 *image = (uint8_t*) mlt_pool_alloc( img_size );
507 mlt_frame_set_image( frame, *image, img_size, mlt_pool_release );
508 memcpy( *image, buf, img_size );
510 // Convert BGRA to RGBA
511 register uint8_t *p = *image;
512 register int n = width * height + 1;
519 // Release PBO and FBO
520 glUnmapBuffer( GL_PIXEL_PACK_BUFFER_ARB );
522 glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, 0 );
524 glBindFramebuffer( GL_FRAMEBUFFER, 0 );
526 glBindTexture( GL_TEXTURE_2D, 0 );
528 mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), "movit.convert.texture", texture, 0,
529 (mlt_destructor) GlslManager::release_texture, NULL);
535 void GlslManager::lock_service( mlt_frame frame )
537 Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );
541 void GlslManager::unlock_service( mlt_frame frame )
543 Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );