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