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