]> git.sesse.net Git - mlt/blob - src/modules/qimage/consumer_qglsl.cpp
Add support for Qt 5, drop support for Qt 3 and KDE 3.
[mlt] / src / modules / qimage / consumer_qglsl.cpp
1 /*
2  * consumer_qglsl.cpp
3  * Copyright (C) 2012 Dan Dennedy <dan@dennedy.org>
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  */
19
20 #include <framework/mlt.h>
21 #include <QApplication>
22 #include <QLocale>
23 #include <QGLWidget>
24
25 static void onThreadStarted(mlt_properties owner, mlt_consumer consumer)
26 {
27         mlt_service service = MLT_CONSUMER_SERVICE(consumer);
28         mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
29         mlt_filter filter = (mlt_filter) mlt_properties_get_data(properties, "glslManager", NULL);
30         mlt_properties filter_properties = MLT_FILTER_PROPERTIES(filter);
31         QApplication* app = qApp;
32
33         mlt_log_debug(service, "%s\n", __FUNCTION__);
34 #ifdef linux
35         if ( getenv("DISPLAY") == 0 ) {
36                 mlt_log_error(service, "The qglsl consumer requires a X11 environment.\nPlease either run melt from an X session or use a fake X server like xvfb:\nxvfb-run -a melt (...)\n" );
37         } else
38 #endif
39         if (!app) {
40                 int argc = 1;
41                 char* argv[1];
42                 argv[0] = (char*) "MLT qglsl consumer";
43                 app = new QApplication(argc, argv);
44                 const char *localename = mlt_properties_get_lcnumeric(properties);
45                 QLocale::setDefault(QLocale(localename));
46         }
47         QGLWidget* renderContext = new QGLWidget;
48         renderContext->resize(0, 0);
49         renderContext->show();
50         mlt_events_fire(filter_properties, "init glsl", NULL);
51         if (!mlt_properties_get_int(filter_properties, "glsl_supported")) {
52                 mlt_log_fatal(service,
53                         "OpenGL Shading Language rendering is not supported on this machine.\n" );
54                 mlt_events_fire(properties, "consumer-fatal-error", NULL);
55         }
56         else {
57                 mlt_properties_set_data(properties, "qglslRenderContext", renderContext, 0, NULL, NULL);
58         }
59 }
60
61 static void onCleanup(mlt_properties owner, mlt_consumer consumer)
62 {
63         QGLWidget* renderContext = (QGLWidget*) mlt_properties_get_data(
64                 MLT_CONSUMER_PROPERTIES(consumer), "qglslRenderContext", NULL);
65         if (renderContext)
66                 renderContext->makeCurrent();
67         delete renderContext;
68         mlt_properties_set_data(MLT_CONSUMER_PROPERTIES(consumer),
69                 "qglslRenderContext", NULL, 0, NULL, NULL);
70 }
71
72 extern "C" {
73
74 mlt_consumer consumer_qglsl_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
75 {
76         mlt_consumer consumer = mlt_factory_consumer(profile, "multi", arg);
77         if (consumer) {
78                 mlt_filter filter = mlt_factory_filter(profile, "glsl.manager", 0);
79                 if (filter) {
80                         mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
81                         mlt_properties_set_data(properties, "glslManager", filter, 0, (mlt_destructor) mlt_filter_close, NULL);
82                         mlt_events_register( properties, "consumer-cleanup", NULL );
83                         mlt_events_listen(properties, consumer, "consumer-thread-started", (mlt_listener) onThreadStarted);
84                         mlt_events_listen(properties, consumer, "consumer-cleanup", (mlt_listener) onCleanup);
85                         return consumer;
86                 }
87                 mlt_consumer_close(consumer);
88         }
89         return NULL;
90 }
91
92 }