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