]> git.sesse.net Git - mlt/blobdiff - src/modules/opengl/filter_glsl_manager.cpp
Add movit.luma transition.
[mlt] / src / modules / opengl / filter_glsl_manager.cpp
index 100c3ab1ac420c7a2906872852178a5ed2d987fc..1523b8e30ff4ecdf2ff47bd3fcbddaf19373d8f6 100644 (file)
@@ -42,6 +42,8 @@ extern "C" {
 #include <GL/glx.h>
 #endif
 
+using namespace movit;
+
 void dec_ref_and_delete(GlslManager *p)
 {
        if (p->dec_ref() == 0) {
@@ -102,53 +104,6 @@ GlslManager* GlslManager::get_instance()
        return (GlslManager*) mlt_properties_get_data(mlt_global_properties(), "glslManager", 0);
 }
 
-glsl_fbo GlslManager::get_fbo(int width, int height)
-{
-#if defined(__DARWIN__)
-       CGLContextObj context = CGLGetCurrentContext();
-#elif defined(WIN32)
-       HGLRC context = wglGetCurrentContext();
-#else
-       GLXContext context = glXGetCurrentContext();
-#endif
-
-       lock();
-       for (int i = 0; i < fbo_list.count(); ++i) {
-               glsl_fbo fbo = (glsl_fbo) fbo_list.peek(i);
-               if (!fbo->used && (fbo->width == width) && (fbo->height == height) && (fbo->context == context)) {
-                       fbo->used = 1;
-                       unlock();
-                       return fbo;
-               }
-       }
-       unlock();
-
-       GLuint fb = 0;
-       glGenFramebuffers(1, &fb);
-       if (!fb)
-               return NULL;
-
-       glsl_fbo fbo = new glsl_fbo_s;
-       if (!fbo) {
-               glDeleteFramebuffers(1, &fb);
-               return NULL;
-       }
-       fbo->fbo = fb;
-       fbo->width = width;
-       fbo->height = height;
-       fbo->used = 1;
-       fbo->context = context;
-       lock();
-       fbo_list.push_back(fbo);
-       unlock();
-       return fbo;
-}
-
-void GlslManager::release_fbo(glsl_fbo fbo)
-{
-       fbo->used = 0;
-}
-
 glsl_texture GlslManager::get_texture(int width, int height, GLint internal_format)
 {
        lock();
@@ -164,29 +119,18 @@ glsl_texture GlslManager::get_texture(int width, int height, GLint internal_form
                        return tex;
                }
        }
-
-       // Recycle a glsl_texture with deleted glTexture.
-       glsl_texture gtex = 0;
-       for (int i = 0; i < texture_list.count(); ++i) {
-               glsl_texture tex = (glsl_texture) texture_list.peek(i);
-               if (!tex->used && !tex->width && !tex->height) {
-                       gtex = tex;
-                       break;
-               }
-       }
        unlock();
 
        GLuint tex = 0;
        glGenTextures(1, &tex);
        if (!tex) {
-               glDeleteTextures(1, &tex);
                return NULL;
        }
 
+       glsl_texture gtex = new glsl_texture_s;
        if (!gtex) {
-               gtex = new glsl_texture_s;
-               if (!gtex)
-                       return NULL;
+               glDeleteTextures(1, &tex);
+               return NULL;
        }
 
        glBindTexture( GL_TEXTURE_2D, tex );
@@ -257,17 +201,11 @@ glsl_pbo GlslManager::get_pbo(int size)
 void GlslManager::cleanupContext()
 {
        lock();
-       while (fbo_list.peek_back()) {
-               glsl_fbo fbo = (glsl_fbo) fbo_list.pop_back();
-               glDeleteFramebuffers(1, &fbo->fbo);
-               delete fbo;
-       }
-       for (int i = 0; i < texture_list.count(); ++i) {
-               glsl_texture texture = (glsl_texture) texture_list.peek(i);
+       while (texture_list.peek_back()) {
+               glsl_texture texture = (glsl_texture) texture_list.peek_back();
                glDeleteTextures(1, &texture->texture);
-               texture->used = 0;
-               texture->width = 0;
-               texture->height = 0;
+               delete texture;
+               texture_list.pop_back();
        }
        if (pbo) {
                glDeleteBuffers(1, &pbo->pbo);
@@ -287,8 +225,8 @@ void GlslManager::onInit( mlt_properties owner, GlslManager* filter )
 #else
        std::string path = std::string(getenv("MLT_MOVIT_PATH") ? getenv("MLT_MOVIT_PATH") : SHADERDIR);
 #endif
-       ::init_movit( path, mlt_log_get_level() == MLT_LOG_DEBUG? MOVIT_DEBUG_ON : MOVIT_DEBUG_OFF );
-       filter->set( "glsl_supported", movit_initialized );
+       bool success = init_movit( path, mlt_log_get_level() == MLT_LOG_DEBUG? MOVIT_DEBUG_ON : MOVIT_DEBUG_OFF );
+       filter->set( "glsl_supported", success );
 }
 
 void GlslManager::onClose( mlt_properties owner, GlslManager *filter )
@@ -326,6 +264,13 @@ mlt_filter filter_glsl_manager_init( mlt_profile profile, mlt_service_type type,
 
 static void deleteChain( GlslChain* chain )
 {
+       // The Input* is owned by the EffectChain, but the MltInput* is not.
+       // Thus, we have to delete it here.
+       for (std::map<mlt_producer, MltInput*>::iterator input_it = chain->inputs.begin();
+            input_it != chain->inputs.end();
+            ++input_it) {
+               delete input_it->second;
+       }
        delete chain->effect_chain;
        delete chain;
 }
@@ -411,17 +356,29 @@ void GlslManager::set_effect_secondary_input( mlt_service service, mlt_frame fra
        set_frame_specific_data( service, frame, "_movit effect secondary input frame", input_frame, 0, NULL, NULL );
 }
 
+void GlslManager::get_effect_third_input( mlt_service service, mlt_frame frame, mlt_service *input_service, mlt_frame *input_frame)
+{
+       *input_service = (mlt_service) get_frame_specific_data( service, frame, "_movit effect third input", NULL );
+       *input_frame = (mlt_frame) get_frame_specific_data( service, frame, "_movit effect third input frame", NULL );
+}
+
+void GlslManager::set_effect_third_input( mlt_service service, mlt_frame frame, mlt_service input_service, mlt_frame input_frame )
+{
+       set_frame_specific_data( service, frame, "_movit effect third input", input_service, 0, NULL, NULL );
+       set_frame_specific_data( service, frame, "_movit effect third input frame", input_frame, 0, NULL, NULL );
+}
+
 int GlslManager::render_frame_texture(EffectChain *chain, mlt_frame frame, int width, int height, uint8_t **image)
 {
-       glsl_fbo fbo = get_fbo( width, height );
-       if (!fbo) return 1;
        glsl_texture texture = get_texture( width, height, GL_RGBA8 );
        if (!texture) {
-               release_fbo( fbo );
                return 1;
        }
 
-       glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
+       GLuint fbo;
+       glGenFramebuffers( 1, &fbo );
+       check_error();
+       glBindFramebuffer( GL_FRAMEBUFFER, fbo );
        check_error();
        glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
        check_error();
@@ -443,14 +400,15 @@ int GlslManager::render_frame_texture(EffectChain *chain, mlt_frame frame, int w
                glClientWaitSync( prev_sync, 0, GL_TIMEOUT_IGNORED );
                glDeleteSync( prev_sync );
        }
-       chain->render_to_fbo( fbo->fbo, width, height );
+       chain->render_to_fbo( fbo, width, height );
        prev_sync = glFenceSync( GL_SYNC_GPU_COMMANDS_COMPLETE, 0 );
        GLsync sync = glFenceSync( GL_SYNC_GPU_COMMANDS_COMPLETE, 0 );
 
        check_error();
        glBindFramebuffer( GL_FRAMEBUFFER, 0 );
        check_error();
-       release_fbo( fbo );
+       glDeleteFramebuffers( 1, &fbo );
+       check_error();
 
        *image = (uint8_t*) &texture->texture;
        mlt_frame_set_image( frame, *image, 0, NULL );
@@ -464,11 +422,8 @@ int GlslManager::render_frame_texture(EffectChain *chain, mlt_frame frame, int w
 
 int GlslManager::render_frame_rgba(EffectChain *chain, mlt_frame frame, int width, int height, uint8_t **image)
 {
-       glsl_fbo fbo = get_fbo( width, height );
-       if (!fbo) return 1;
        glsl_texture texture = get_texture( width, height, GL_RGBA8 );
        if (!texture) {
-               release_fbo( fbo );
                return 1;
        }
 
@@ -477,24 +432,25 @@ int GlslManager::render_frame_rgba(EffectChain *chain, mlt_frame frame, int widt
        int img_size = width * height * 4;
        glsl_pbo pbo = get_pbo( img_size );
        if (!pbo) {
-               release_fbo( fbo );
                release_texture(texture);
                return 1;
        }
 
        // Set the FBO
+       GLuint fbo;
+       glGenFramebuffers( 1, &fbo );
        check_error();
-       glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
+       glBindFramebuffer( GL_FRAMEBUFFER, fbo );
        check_error();
        glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
        check_error();
        glBindFramebuffer( GL_FRAMEBUFFER, 0 );
        check_error();
 
-       chain->render_to_fbo( fbo->fbo, width, height );
+       chain->render_to_fbo( fbo, width, height );
 
        // Read FBO into PBO
-       glBindFramebuffer( GL_FRAMEBUFFER, fbo->fbo );
+       glBindFramebuffer( GL_FRAMEBUFFER, fbo );
        check_error();
        glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, pbo->pbo );
        check_error();
@@ -530,7 +486,8 @@ int GlslManager::render_frame_rgba(EffectChain *chain, mlt_frame frame, int widt
        check_error();
        mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), "movit.convert.texture", texture, 0,
                (mlt_destructor) GlslManager::release_texture, NULL);
-       release_fbo( fbo );
+       glDeleteFramebuffers( 1, &fbo );
+       check_error();
 
        return 0;
 }