3 * Copyright (C) 2012-2014 Dan Dennedy <dan@dennedy.org>
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.
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.
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
21 #include <framework/mlt.h>
22 #include <QApplication>
25 #include <QWaitCondition>
28 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
30 class GLWidget : public QGLWidget
33 QGLWidget *renderContext;
36 QWaitCondition condition;
43 : QGLWidget(0, 0, Qt::SplashScreen)
46 , isInitialized(false)
57 bool createRenderContext()
61 condition.wait(&mutex);
65 renderContext = new QGLWidget(0, this, Qt::SplashScreen);
66 renderContext->resize(0, 0);
67 renderContext->makeCurrent();
69 return renderContext->isValid();
83 #include <QOpenGLContext>
84 #include <QOffscreenSurface>
86 typedef void* ( *thread_function_t )( void* );
88 class RenderThread : public QThread
91 RenderThread(thread_function_t function, void *data)
93 , m_function(function)
96 m_context = new QOpenGLContext;
98 m_context->moveToThread(this);
99 m_surface = new QOffscreenSurface();
104 m_surface->destroy();
111 Q_ASSERT(m_context->isValid());
112 m_context->makeCurrent(m_surface);
114 m_context->doneCurrent();
119 thread_function_t m_function;
121 QOpenGLContext* m_context;
122 QOffscreenSurface* m_surface;
125 static void onThreadCreate(mlt_properties owner, mlt_consumer self,
126 RenderThread** thread, int* priority, thread_function_t function, void* data )
130 (*thread) = new RenderThread(function, data);
134 static void onThreadJoin(mlt_properties owner, mlt_consumer self, RenderThread* thread)
141 qApp->processEvents();
148 static void onThreadStarted(mlt_properties owner, mlt_consumer consumer)
150 mlt_service service = MLT_CONSUMER_SERVICE(consumer);
151 mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
152 mlt_filter filter = (mlt_filter) mlt_properties_get_data(properties, "glslManager", NULL);
153 mlt_properties filter_properties = MLT_FILTER_PROPERTIES(filter);
155 mlt_log_debug(service, "%s\n", __FUNCTION__);
156 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
157 GLWidget *widget = (GLWidget*) mlt_properties_get_data(properties, "GLWidget", NULL);
158 if (widget->createRenderContext()) {
162 mlt_events_fire(filter_properties, "init glsl", NULL);
163 if (!mlt_properties_get_int(filter_properties, "glsl_supported")) {
164 mlt_log_fatal(service,
165 "OpenGL Shading Language rendering is not supported on this machine.\n" );
166 mlt_events_fire(properties, "consumer-fatal-error", NULL);
171 static void onThreadStopped(mlt_properties owner, mlt_consumer consumer)
173 mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
174 mlt_filter filter = (mlt_filter) mlt_properties_get_data(properties, "glslManager", NULL);
175 mlt_events_fire(MLT_FILTER_PROPERTIES(filter), "close glsl", NULL);
178 static void onCleanup(mlt_properties owner, mlt_consumer consumer)
180 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
181 GLWidget* widget = (GLWidget*) mlt_properties_get_data( MLT_CONSUMER_PROPERTIES(consumer), "GLWidget", NULL);
183 mlt_properties_set_data(MLT_CONSUMER_PROPERTIES(consumer), "GLWidget", NULL, 0, NULL, NULL);
184 qApp->processEvents();
190 mlt_consumer consumer_qglsl_init( mlt_profile profile, mlt_service_type type, const char *id, char *arg )
192 mlt_consumer consumer = mlt_factory_consumer(profile, "multi", arg);
194 mlt_filter filter = mlt_factory_filter(profile, "glsl.manager", 0);
196 mlt_properties properties = MLT_CONSUMER_PROPERTIES(consumer);
197 mlt_properties_set_data(properties, "glslManager", filter, 0, (mlt_destructor) mlt_filter_close, NULL);
198 mlt_events_register( properties, "consumer-cleanup", NULL );
199 mlt_events_listen(properties, consumer, "consumer-thread-started", (mlt_listener) onThreadStarted);
200 mlt_events_listen(properties, consumer, "consumer-thread-stopped", (mlt_listener) onThreadStopped);
201 mlt_events_listen(properties, consumer, "consumer-cleanup", (mlt_listener) onCleanup);
202 if (!createQApplicationIfNeeded(MLT_CONSUMER_SERVICE(consumer))) {
203 mlt_filter_close(filter);
204 mlt_consumer_close(consumer);
207 #if QT_VERSION < QT_VERSION_CHECK(5, 0, 0)
208 mlt_properties_set_data(properties, "GLWidget", new GLWidget, 0, NULL, NULL);
210 mlt_events_listen(properties, consumer, "consumer-thread-create", (mlt_listener) onThreadCreate);
211 mlt_events_listen(properties, consumer, "consumer-thread-join", (mlt_listener) onThreadJoin);
213 qApp->processEvents();
216 mlt_consumer_close(consumer);