]> git.sesse.net Git - mlt/blob - src/modules/opengl/filter_glsl_manager.cpp
Fix tiny memory leak in GlslManager (coverity-1026795).
[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 "glsl_manager.h"
24 #include <movit/init.h>
25 #include <movit/effect_chain.h>
26 #include "mlt_movit_input.h"
27 #include "mlt_flip_effect.h"
28 #include <mlt++/MltEvent.h>
29 #include <mlt++/MltProducer.h>
30
31 extern "C" {
32 #include <framework/mlt_factory.h>
33 }
34
35 void deleteManager(GlslManager *p)
36 {
37         delete p;
38 }
39
40 GlslManager::GlslManager()
41         : Mlt::Filter( mlt_filter_new() )
42         , pbo(0)
43         , initEvent(0)
44 {
45         mlt_filter filter = get_filter();
46         if ( filter ) {
47                 // Set the mlt_filter child in case we choose to override virtual functions.
48                 filter->child = this;
49                 mlt_properties_set_data(mlt_global_properties(), "glslManager", this, 0,
50                         (mlt_destructor) deleteManager, NULL);
51
52                 mlt_events_register( get_properties(), "init glsl", NULL );
53                 initEvent = listen("init glsl", this, (mlt_listener) GlslManager::onInit);
54         }
55 }
56
57 GlslManager::~GlslManager()
58 {
59         mlt_log_debug(get_service(), "%s\n", __FUNCTION__);
60         while (fbo_list.peek_back())
61                 delete (glsl_fbo) fbo_list.pop_back();
62         while (texture_list.peek_back())
63                 delete (glsl_texture) texture_list.pop_back();
64         delete pbo;
65         delete initEvent;
66 }
67
68 GlslManager* GlslManager::get_instance()
69 {
70         return (GlslManager*) mlt_properties_get_data(mlt_global_properties(), "glslManager", 0);
71 }
72
73 glsl_fbo GlslManager::get_fbo(int width, int height)
74 {
75         for (int i = 0; i < fbo_list.count(); ++i) {
76                 glsl_fbo fbo = (glsl_fbo) fbo_list.peek(i);
77                 if (!fbo->used && (fbo->width == width) && (fbo->height == height)) {
78                         fbo->used = 1;
79                         return fbo;
80                 }
81         }
82         GLuint fb = 0;
83         glGenFramebuffers(1, &fb);
84         if (!fb)
85                 return NULL;
86
87         glsl_fbo fbo = new glsl_fbo_s;
88         if (!fbo) {
89                 glDeleteFramebuffers(1, &fb);
90                 return NULL;
91         }
92         fbo->fbo = fb;
93         fbo->width = width;
94         fbo->height = height;
95         fbo->used = 1;
96         fbo_list.push_back(fbo);
97         return fbo;
98 }
99
100 void GlslManager::release_fbo(glsl_fbo fbo)
101 {
102         fbo->used = 0;
103 }
104
105 glsl_texture GlslManager::get_texture(int width, int height, GLint internal_format)
106 {
107         for (int i = 0; i < texture_list.count(); ++i) {
108                 glsl_texture tex = (glsl_texture) texture_list.peek(i);
109                 if (!tex->used && (tex->width == width) && (tex->height == height) && (tex->internal_format == internal_format)) {
110                         glBindTexture(GL_TEXTURE_2D, tex->texture);
111                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
112                         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
113                         glBindTexture( GL_TEXTURE_2D, 0);
114                         tex->used = 1;
115                         return tex;
116                 }
117         }
118         GLuint tex = 0;
119         glGenTextures(1, &tex);
120         if (!tex)
121                 return NULL;
122
123         glsl_texture gtex = new glsl_texture_s;
124         if (!gtex) {
125                 glDeleteTextures(1, &tex);
126                 return NULL;
127         }
128         glBindTexture( GL_TEXTURE_2D, tex );
129         glTexImage2D( GL_TEXTURE_2D, 0, internal_format, width, height, 0, internal_format, GL_UNSIGNED_BYTE, NULL );
130     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE );
131     glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE );
132     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
133     glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
134     glBindTexture( GL_TEXTURE_2D, 0 );
135
136         gtex->texture = tex;
137         gtex->width = width;
138         gtex->height = height;
139         gtex->internal_format = internal_format;
140         gtex->used = 1;
141         texture_list.push_back(gtex);
142         return gtex;
143 }
144
145 void GlslManager::release_texture(glsl_texture texture)
146 {
147         texture->used = 0;
148 }
149
150 glsl_pbo GlslManager::get_pbo(int size)
151 {
152         if (!pbo) {
153                 GLuint pb = 0;
154                 glGenBuffers(1, &pb);
155                 if (!pb)
156                         return NULL;
157
158                 pbo = new glsl_pbo_s;
159                 if (!pbo) {
160                         glDeleteBuffers(1, &pb);
161                         return NULL;
162                 }
163                 pbo->pbo = pb;
164         }
165         if (size > pbo->size) {
166                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, pbo->pbo);
167                 glBufferData(GL_PIXEL_UNPACK_BUFFER_ARB, size, NULL, GL_STREAM_DRAW);
168                 glBindBuffer(GL_PIXEL_UNPACK_BUFFER_ARB, 0);
169                 pbo->size = size;
170         }
171         return pbo;
172 }
173
174 void GlslManager::onInit( mlt_properties owner, GlslManager* filter )
175 {
176         mlt_log_debug( filter->get_service(), "%s\n", __FUNCTION__ );
177 #ifdef WIN32
178         std::string path = std::string(mlt_environment("MLT_APPDIR")).append("\\share\\movit");
179 #elif defined(__DARWIN__) && defined(RELOCATABLE)
180         std::string path = std::string(mlt_environment("MLT_APPDIR")).append("/share/movit");
181 #else
182         std::string path = std::string(getenv("MLT_MOVIT_PATH") ? getenv("MLT_MOVIT_PATH") : SHADERDIR);
183 #endif
184         ::init_movit( path, mlt_log_get_level() == MLT_LOG_DEBUG? MOVIT_DEBUG_ON : MOVIT_DEBUG_OFF );
185         filter->set( "glsl_supported", movit_initialized );
186 }
187
188 void GlslManager::onServiceChanged( mlt_properties owner, mlt_service aservice )
189 {
190         Mlt::Service service( aservice );
191         service.lock();
192         service.set( "movit chain", NULL, 0 );
193         service.set( "movit input", NULL, 0 );
194         // Destroy the effect list.
195         GlslManager::get_instance()->set( service.get( "_unique_id" ), NULL, 0 );
196         service.unlock();
197 }
198
199 void GlslManager::onPropertyChanged( mlt_properties owner, mlt_service service, const char* property )
200 {
201         if ( property && std::string( property ) == "disable" )
202                 onServiceChanged( owner, service );
203 }
204
205 extern "C" {
206
207 mlt_filter filter_glsl_manager_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
208 {
209         GlslManager* g = GlslManager::get_instance();
210         if (g)
211                 g->inc_ref();
212         else
213                 g = new GlslManager();
214         return g->get_filter();
215 }
216
217 } // extern "C"
218
219 Mlt::Properties GlslManager::effect_list( Mlt::Service& service )
220 {
221         char *unique_id =  service.get( "_unique_id" );
222         mlt_properties properties = (mlt_properties) get_data( unique_id );
223         if ( !properties ) {
224                 properties = mlt_properties_new();
225                 set( unique_id, properties, 0, (mlt_destructor) mlt_properties_close );
226         }
227         Mlt::Properties p( properties );
228         return p;
229 }
230
231 static void deleteChain( EffectChain* chain )
232 {
233         delete chain;
234 }
235
236 bool GlslManager::init_chain( mlt_service aservice )
237 {
238         bool error = true;
239         Mlt::Service service( aservice );
240         EffectChain* chain = (EffectChain*) service.get_data( "movit chain" );
241         if ( !chain ) {
242                 mlt_profile profile = mlt_service_profile( aservice );
243                 Input* input = new MltInput( profile->width, profile->height );
244                 chain = new EffectChain( profile->display_aspect_num, profile->display_aspect_den );
245                 chain->add_input( input );
246                 service.set( "movit chain", chain, 0, (mlt_destructor) deleteChain );
247                 service.set( "movit input", input, 0 );
248                 service.set( "_movit finalized", 0 );
249                 service.listen( "service-changed", aservice, (mlt_listener) GlslManager::onServiceChanged );
250                 service.listen( "property-changed", aservice, (mlt_listener) GlslManager::onPropertyChanged );
251                 error = false;
252         }
253         return error;
254 }
255
256 EffectChain* GlslManager::get_chain( mlt_service service )
257 {
258         return (EffectChain*) mlt_properties_get_data( MLT_SERVICE_PROPERTIES(service), "movit chain", NULL );
259 }
260
261 MltInput *GlslManager::get_input( mlt_service service )
262 {
263         return (MltInput*) mlt_properties_get_data( MLT_SERVICE_PROPERTIES(service), "movit input", NULL );
264 }
265
266 void GlslManager::reset_finalized( mlt_service service )
267 {
268         mlt_properties_set_int( MLT_SERVICE_PROPERTIES(service), "_movit finalized", 0 );
269 }
270
271 Effect* GlslManager::get_effect( mlt_filter filter, mlt_frame frame )
272 {
273         Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );
274         char *unique_id = mlt_properties_get( MLT_FILTER_PROPERTIES(filter), "_unique_id" );
275         return (Effect*) GlslManager::get_instance()->effect_list( producer ).get_data( unique_id );
276 }
277
278 Effect* GlslManager::add_effect( mlt_filter filter, mlt_frame frame, Effect* effect )
279 {
280         Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );
281         EffectChain* chain = (EffectChain*) producer.get_data( "movit chain" );
282         chain->add_effect( effect );
283         char *unique_id = mlt_properties_get( MLT_FILTER_PROPERTIES(filter), "_unique_id" );
284         GlslManager::get_instance()->effect_list( producer ).set( unique_id, effect, 0 );
285         return effect;
286 }
287
288 Effect* GlslManager::add_effect( mlt_filter filter, mlt_frame frame, Effect* effect, Effect* input_b )
289 {
290         Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );
291         EffectChain* chain = (EffectChain*) producer.get_data( "movit chain" );
292         chain->add_effect( effect, chain->last_added_effect(),
293                 input_b? input_b : chain->last_added_effect() );
294         char *unique_id = mlt_properties_get( MLT_FILTER_PROPERTIES(filter), "_unique_id" );
295         GlslManager::get_instance()->effect_list( producer ).set( unique_id, effect, 0 );
296         return effect;
297 }
298
299 void GlslManager::render( mlt_service service, void* chain, GLuint fbo, int width, int height )
300 {
301         EffectChain* effect_chain = (EffectChain*) chain;
302         mlt_properties properties = MLT_SERVICE_PROPERTIES( service );
303         if ( !mlt_properties_get_int( properties, "_movit finalized" ) ) {
304                 mlt_properties_set_int( properties, "_movit finalized", 1 );
305                 effect_chain->add_effect( new Mlt::VerticalFlip() );
306                 effect_chain->finalize();
307         }
308         effect_chain->render_to_fbo( fbo, width, height );
309 }
310
311 void GlslManager::lock_service( mlt_frame frame )
312 {
313         Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );
314         producer.lock();
315 }
316
317 void GlslManager::unlock_service( mlt_frame frame )
318 {
319         Mlt::Producer producer( mlt_producer_cut_parent( mlt_frame_get_original_producer( frame ) ) );
320         producer.unlock();
321 }