]> git.sesse.net Git - mlt/blob - src/modules/opengl/filter_glsl_manager.cpp
A little debugging.
[mlt] / src / modules / opengl / filter_glsl_manager.cpp
1 /*
2  * filter_glsl_manager.cpp
3  * Copyright (C) 2011-2012 Christophe Thommeret <hftom@free.fr>
4  * Copyright (C) 2013 Dan Dennedy <dan@dennedy.org>
5  *
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.
10  *
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.
15  *
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.
19  */
20
21 #include <stdlib.h>
22 #include <string>
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>
32
33 extern "C" {
34 #include <framework/mlt_factory.h>
35 }
36
37 #if defined(__DARWIN__)
38 #include <OpenGL/OpenGL.h>
39 #elif defined(WIN32)
40 #include <wingdi.h>
41 #else
42 #include <GL/glx.h>
43 #endif
44
45 using namespace movit;
46
47 void dec_ref_and_delete(GlslManager *p)
48 {
49         if (p->dec_ref() == 0) {
50                 delete p;
51         }
52 }
53
54 GlslManager::GlslManager()
55         : Mlt::Filter( mlt_filter_new() )
56         , resource_pool(new ResourcePool())
57         , pbo(0)
58         , initEvent(0)
59         , closeEvent(0)
60         , prev_sync(NULL)
61 {
62         mlt_filter filter = get_filter();
63         if ( filter ) {
64                 // Set the mlt_filter child in case we choose to override virtual functions.
65                 filter->child = this;
66                 add_ref(mlt_global_properties());
67
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);
72         }
73 }
74
75 GlslManager::~GlslManager()
76 {
77         mlt_log_debug(get_service(), "%s\n", __FUNCTION__);
78         cleanupContext();
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();
83         delete initEvent;
84         delete closeEvent;
85         if (prev_sync != NULL) {
86                 glDeleteSync( prev_sync );
87         }
88         while (syncs_to_delete.count() > 0) {
89                 GLsync sync = (GLsync) syncs_to_delete.pop_front();
90                 glDeleteSync( sync );
91         }
92         delete resource_pool;
93 }
94
95 void GlslManager::add_ref(mlt_properties properties)
96 {
97         inc_ref();
98         mlt_properties_set_data(properties, "glslManager", this, 0,
99             (mlt_destructor) dec_ref_and_delete, NULL);
100 }
101
102 GlslManager* GlslManager::get_instance()
103 {
104         return (GlslManager*) mlt_properties_get_data(mlt_global_properties(), "glslManager", 0);
105 }
106
107 glsl_texture GlslManager::get_texture(int width, int height, GLint internal_format)
108 {
109         lock();
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);
117                         tex->used = 1;
118                         unlock();
119                         return tex;
120                 }
121         }
122         unlock();
123
124         GLuint tex = 0;
125         glGenTextures(1, &tex);
126         if (!tex) {
127                 return NULL;
128         }
129
130         glsl_texture gtex = new glsl_texture_s;
131         if (!gtex) {
132                 glDeleteTextures(1, &tex);
133                 return NULL;
134         }
135
136         printf("created a new texture for %dx%d: %d\n", width, height, tex);
137         glBindTexture( GL_TEXTURE_2D, tex );
138         glTexImage2D( GL_TEXTURE_2D, 0, internal_format, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL );
139     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
140     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
141     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
142     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
143     glBindTexture( GL_TEXTURE_2D, 0 );
144
145         gtex->texture = tex;
146         gtex->width = width;
147         gtex->height = height;
148         gtex->internal_format = internal_format;
149         gtex->used = 1;
150         lock();
151         texture_list.push_back(gtex);
152         unlock();
153         return gtex;
154 }
155
156 void GlslManager::release_texture(glsl_texture texture)
157 {
158         texture->used = 0;
159 }
160
161 void GlslManager::delete_sync(GLsync sync)
162 {
163         // We do not know which thread we are called from, and we can only
164         // delete this if we are in one with a valid OpenGL context.
165         // Thus, store it for later deletion in render_frame_texture().
166         GlslManager* g = GlslManager::get_instance();
167         g->lock();
168         g->syncs_to_delete.push_back(sync);
169         g->unlock();
170 }
171
172 glsl_pbo GlslManager::get_pbo(int size)
173 {
174         lock();
175         if (!pbo) {
176                 GLuint pb = 0;
177                 glGenBuffers(1, &pb);
178                 if (!pb) {
179                         unlock();
180                         return NULL;
181                 }
182
183                 pbo = new glsl_pbo_s;
184                 if (!pbo) {
185                         glDeleteBuffers(1, &pb);
186                         unlock();
187                         return NULL;
188                 }
189                 pbo->pbo = pb;
190                 pbo->size = 0;
191         }
192         if (size > pbo->size) {
193                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo->pbo);
194                 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, size, NULL, GL_STREAM_DRAW);
195                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
196                 pbo->size = size;
197         }
198         unlock();
199         return pbo;
200 }
201
202 void GlslManager::cleanupContext()
203 {
204         lock();
205         while (texture_list.peek_back()) {
206                 glsl_texture texture = (glsl_texture) texture_list.peek_back();
207                 glDeleteTextures(1, &texture->texture);
208                 delete texture;
209                 texture_list.pop_back();
210         }
211         if (pbo) {
212                 glDeleteBuffers(1, &pbo->pbo);
213                 delete pbo;
214                 pbo = 0;
215         }
216         unlock();
217 }
218
219 void GlslManager::onInit( mlt_properties owner, GlslManager* filter )
220 {
221         mlt_log_debug( filter->get_service(), "%s\n", __FUNCTION__ );
222 #ifdef WIN32
223         std::string path = std::string(mlt_environment("MLT_APPDIR")).append("\\share\\movit");
224 #elif defined(__DARWIN__) && defined(RELOCATABLE)
225         std::string path = std::string(mlt_environment("MLT_APPDIR")).append("/share/movit");
226 #else
227         std::string path = std::string(getenv("MLT_MOVIT_PATH") ? getenv("MLT_MOVIT_PATH") : SHADERDIR);
228 #endif
229         bool success = init_movit( path, MOVIT_DEBUG_ON );
230         filter->set( "glsl_supported", success );
231 }
232
233 void GlslManager::onClose( mlt_properties owner, GlslManager *filter )
234 {
235         filter->cleanupContext();
236 }
237
238 void GlslManager::onServiceChanged( mlt_properties owner, mlt_service aservice )
239 {
240         Mlt::Service service( aservice );
241         service.lock();
242         service.set( "movit chain", NULL, 0 );
243         service.unlock();
244 }
245
246 void GlslManager::onPropertyChanged( mlt_properties owner, mlt_service service, const char* property )
247 {
248         if ( property && std::string( property ) == "disable" )
249                 onServiceChanged( owner, service );
250 }
251
252 extern "C" {
253
254 mlt_filter filter_glsl_manager_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
255 {
256         GlslManager* g = GlslManager::get_instance();
257         if (g)
258                 g->inc_ref();
259         else
260                 g = new GlslManager();
261         return g->get_filter();
262 }
263
264 } // extern "C"
265
266 static void deleteChain( GlslChain* chain )
267 {
268         // The Input* is owned by the EffectChain, but the MltInput* is not.
269         // Thus, we have to delete it here.
270         for (std::map<mlt_producer, MltInput*>::iterator input_it = chain->inputs.begin();
271              input_it != chain->inputs.end();
272              ++input_it) {
273                 delete input_it->second;
274         }
275         delete chain->effect_chain;
276         delete chain;
277 }
278         
279 void* GlslManager::get_frame_specific_data( mlt_service service, mlt_frame frame, const char *key, int *length )
280 {
281         const char *unique_id = mlt_properties_get( MLT_SERVICE_PROPERTIES(service), "_unique_id" );
282         char buf[256];
283         snprintf( buf, sizeof(buf), "%s_%s", key, unique_id );
284         return mlt_properties_get_data( MLT_FRAME_PROPERTIES(frame), buf, length );
285 }
286
287 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 {
289         const char *unique_id = mlt_properties_get( MLT_SERVICE_PROPERTIES(service), "_unique_id" );
290         char buf[256];
291         snprintf( buf, sizeof(buf), "%s_%s", key, unique_id );
292         return mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), buf, value, length, destroy, serialise );
293 }
294
295 void GlslManager::set_chain( mlt_service service, GlslChain* chain )
296 {
297         mlt_properties_set_data( MLT_SERVICE_PROPERTIES(service), "_movit chain", chain, 0, (mlt_destructor) deleteChain, NULL );
298 }
299
300 GlslChain* GlslManager::get_chain( mlt_service service )
301 {
302         return (GlslChain*) mlt_properties_get_data( MLT_SERVICE_PROPERTIES(service), "_movit chain", NULL );
303 }
304         
305 Effect* GlslManager::get_effect( mlt_service service, mlt_frame frame )
306 {
307         return (Effect*) get_frame_specific_data( service, frame, "_movit effect", NULL );
308 }
309
310 Effect* GlslManager::set_effect( mlt_service service, mlt_frame frame, Effect* effect )
311 {
312         set_frame_specific_data( service, frame, "_movit effect", effect, 0, NULL, NULL );
313         return effect;
314 }
315
316 MltInput* GlslManager::get_input( mlt_producer producer, mlt_frame frame )
317 {
318         return (MltInput*) get_frame_specific_data( MLT_PRODUCER_SERVICE(producer), frame, "_movit input", NULL );
319 }
320
321 MltInput* GlslManager::set_input( mlt_producer producer, mlt_frame frame, MltInput* input )
322 {
323         set_frame_specific_data( MLT_PRODUCER_SERVICE(producer), frame, "_movit input", input, 0, NULL, NULL );
324         return input;
325 }
326
327 uint8_t* GlslManager::get_input_pixel_pointer( mlt_producer producer, mlt_frame frame )
328 {
329         return (uint8_t*) get_frame_specific_data( MLT_PRODUCER_SERVICE(producer), frame, "_movit input pp", NULL );
330 }
331
332 uint8_t* GlslManager::set_input_pixel_pointer( mlt_producer producer, mlt_frame frame, uint8_t* image )
333 {
334         set_frame_specific_data( MLT_PRODUCER_SERVICE(producer), frame, "_movit input pp", image, 0, NULL, NULL );
335         return image;
336 }
337
338 mlt_service GlslManager::get_effect_input( mlt_service service, mlt_frame frame )
339 {
340         return (mlt_service) get_frame_specific_data( service, frame, "_movit effect input", NULL );
341 }
342
343 void GlslManager::set_effect_input( mlt_service service, mlt_frame frame, mlt_service input_service )
344 {
345         set_frame_specific_data( service, frame, "_movit effect input", input_service, 0, NULL, NULL );
346 }
347
348 void GlslManager::get_effect_secondary_input( mlt_service service, mlt_frame frame, mlt_service *input_service, mlt_frame *input_frame)
349 {
350         *input_service = (mlt_service) get_frame_specific_data( service, frame, "_movit effect secondary input", NULL );
351         *input_frame = (mlt_frame) get_frame_specific_data( service, frame, "_movit effect secondary input frame", NULL );
352 }
353
354 void GlslManager::set_effect_secondary_input( mlt_service service, mlt_frame frame, mlt_service input_service, mlt_frame input_frame )
355 {
356         set_frame_specific_data( service, frame, "_movit effect secondary input", input_service, 0, NULL, NULL );
357         set_frame_specific_data( service, frame, "_movit effect secondary input frame", input_frame, 0, NULL, NULL );
358 }
359
360 void GlslManager::get_effect_third_input( mlt_service service, mlt_frame frame, mlt_service *input_service, mlt_frame *input_frame)
361 {
362         *input_service = (mlt_service) get_frame_specific_data( service, frame, "_movit effect third input", NULL );
363         *input_frame = (mlt_frame) get_frame_specific_data( service, frame, "_movit effect third input frame", NULL );
364 }
365
366 void GlslManager::set_effect_third_input( mlt_service service, mlt_frame frame, mlt_service input_service, mlt_frame input_frame )
367 {
368         set_frame_specific_data( service, frame, "_movit effect third input", input_service, 0, NULL, NULL );
369         set_frame_specific_data( service, frame, "_movit effect third input frame", input_frame, 0, NULL, NULL );
370 }
371
372 int GlslManager::render_frame_texture(EffectChain *chain, mlt_frame frame, int width, int height, uint8_t **image)
373 {
374         glsl_texture texture = get_texture( width, height, GL_RGBA8 );
375         if (!texture) {
376                 return 1;
377         }
378
379         GLuint fbo;
380         glGenFramebuffers( 1, &fbo );
381         check_error();
382         glBindFramebuffer( GL_FRAMEBUFFER, fbo );
383         check_error();
384         glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
385         check_error();
386         glBindFramebuffer( GL_FRAMEBUFFER, 0 );
387         check_error();
388
389         lock();
390         while (syncs_to_delete.count() > 0) {
391                 GLsync sync = (GLsync) syncs_to_delete.pop_front();
392                 glDeleteSync( sync );
393         }
394         unlock();
395
396         // Make sure we never have more than one frame pending at any time.
397         // This ensures we do not swamp the GPU with so much work
398         // that we cannot actually display the frames we generate.
399         if (prev_sync != NULL) {
400                 glFlush();
401                 glClientWaitSync( prev_sync, 0, GL_TIMEOUT_IGNORED );
402                 glDeleteSync( prev_sync );
403         }
404         chain->render_to_fbo( fbo, width, height );
405         prev_sync = glFenceSync( GL_SYNC_GPU_COMMANDS_COMPLETE, 0 );
406         GLsync sync = glFenceSync( GL_SYNC_GPU_COMMANDS_COMPLETE, 0 );
407
408         check_error();
409         glBindFramebuffer( GL_FRAMEBUFFER, 0 );
410         check_error();
411         glDeleteFramebuffers( 1, &fbo );
412         check_error();
413
414         *image = (uint8_t*) &texture->texture;
415         mlt_frame_set_image( frame, *image, 0, NULL );
416         mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), "movit.convert.texture", texture, 0,
417                 (mlt_destructor) GlslManager::release_texture, NULL );
418         mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), "movit.convert.fence", sync, 0,
419                 (mlt_destructor) GlslManager::delete_sync, NULL );
420
421         return 0;
422 }
423
424 int GlslManager::render_frame_rgba(EffectChain *chain, mlt_frame frame, int width, int height, uint8_t **image)
425 {
426         glsl_texture texture = get_texture( width, height, GL_RGBA8 );
427         if (!texture) {
428                 return 1;
429         }
430
431         // Use a PBO to hold the data we read back with glReadPixels().
432         // (Intel/DRI goes into a slow path if we don't read to PBO.)
433         int img_size = width * height * 4;
434         glsl_pbo pbo = get_pbo( img_size );
435         if (!pbo) {
436                 release_texture(texture);
437                 return 1;
438         }
439
440         // Set the FBO
441         GLuint fbo;
442         glGenFramebuffers( 1, &fbo );
443         check_error();
444         glBindFramebuffer( GL_FRAMEBUFFER, fbo );
445         check_error();
446         glFramebufferTexture2D( GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, texture->texture, 0 );
447         check_error();
448         glBindFramebuffer( GL_FRAMEBUFFER, 0 );
449         check_error();
450
451         chain->render_to_fbo( fbo, width, height );
452
453         // Read FBO into PBO
454         glBindFramebuffer( GL_FRAMEBUFFER, fbo );
455         check_error();
456         glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, pbo->pbo );
457         check_error();
458         glBufferData( GL_PIXEL_PACK_BUFFER_ARB, img_size, NULL, GL_STREAM_READ );
459         check_error();
460         glReadPixels( 0, 0, width, height, GL_BGRA, GL_UNSIGNED_BYTE, BUFFER_OFFSET(0) );
461         check_error();
462
463         // Copy from PBO
464         uint8_t* buf = (uint8_t*) glMapBuffer( GL_PIXEL_PACK_BUFFER_ARB, GL_READ_ONLY );
465         check_error();
466         *image = (uint8_t*) mlt_pool_alloc( img_size );
467         mlt_frame_set_image( frame, *image, img_size, mlt_pool_release );
468         memcpy( *image, buf, img_size );
469
470         // Convert BGRA to RGBA
471         register uint8_t *p = *image;
472         register int n = width * height + 1;
473         while ( --n ) {
474                 uint8_t b = p[0];
475                 *p = p[2]; p += 2;
476                 *p = b; p += 2;
477         }
478
479         // Release PBO and FBO
480         glUnmapBuffer( GL_PIXEL_PACK_BUFFER_ARB );
481         check_error();
482         glBindBuffer( GL_PIXEL_PACK_BUFFER_ARB, 0 );
483         check_error();
484         glBindFramebuffer( GL_FRAMEBUFFER, 0 );
485         check_error();
486         glBindTexture( GL_TEXTURE_2D, 0 );
487         check_error();
488         mlt_properties_set_data( MLT_FRAME_PROPERTIES(frame), "movit.convert.texture", texture, 0,
489                 (mlt_destructor) GlslManager::release_texture, NULL);
490         glDeleteFramebuffers( 1, &fbo );
491         check_error();
492
493         return 0;
494 }
495
496 void GlslManager::lock_service( mlt_frame frame )
497 {
498         Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );
499         producer.lock();
500 }
501
502 void GlslManager::unlock_service( mlt_frame frame )
503 {
504         Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );
505         producer.unlock();
506 }