1 #include <QObject>
2 #include <QQmlEngine>
3 #include <QQmlComponent>
4 #include <QWindow>
5 #include <QQuickRenderControl>
6 #include <QQuickWindow>
7 #include <QQuickItem>
8 #include <QOpenGLContext>
9 #include <QOpenGLFunctions>
10 #include <QOpenGLFramebufferObject>
11 #include <QAnimationDriver>
12
13 #include <gst/gl/gl.h>
14 #include <gst/gl/gstglfuncs.h>
15
16 #include "qtglrenderer.h"
17 #include "gstqtglutility.h"
18
19 #define GST_CAT_DEFAULT gst_qt_gl_renderer_debug
20 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
21
22 static void
init_debug(void)23 init_debug (void)
24 {
25 static gsize _debug;
26
27 if (g_once_init_enter (&_debug)) {
28 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglrenderer", 0,
29 "Qt OpenGL Renderer");
30 g_once_init_leave (&_debug, 1);
31 }
32 }
33
34 /* Needs to be based on QWindow otherwise (at least) windows and nvidia
35 * proprietary on linux does not work
36 * We also need to override the size handling to get the correct output size
37 */
38 class GstBackingSurface : public QWindow
39 {
40 public:
41 GstBackingSurface();
42 ~GstBackingSurface();
43
44 void setSize (int width, int height);
45 QSize size() const override;
46
47 private:
48 QSize m_size;
49 };
50
GstBackingSurface()51 GstBackingSurface::GstBackingSurface()
52 : m_size(QSize())
53 {
54 /* we do OpenGL things so need an OpenGL surface */
55 setSurfaceType(QSurface::OpenGLSurface);
56 }
57
~GstBackingSurface()58 GstBackingSurface::~GstBackingSurface()
59 {
60 }
61
size() const62 QSize GstBackingSurface::size () const
63 {
64 return m_size;
65 }
66
setSize(int width,int height)67 void GstBackingSurface::setSize (int width, int height)
68 {
69 m_size = QSize (width, height);
70 }
71
72 class GstAnimationDriver : public QAnimationDriver
73 {
74 public:
75 GstAnimationDriver();
76
77 void setNextTime(qint64 ms);
78 void advance() override;
79 qint64 elapsed() const override;
80 private:
81 qint64 m_elapsed;
82 qint64 m_next;
83 };
84
GstAnimationDriver()85 GstAnimationDriver::GstAnimationDriver()
86 : m_elapsed(0),
87 m_next(0)
88 {
89 }
90
advance()91 void GstAnimationDriver::advance()
92 {
93 m_elapsed = m_next;
94 advanceAnimation();
95 }
96
elapsed() const97 qint64 GstAnimationDriver::elapsed() const
98 {
99 return m_elapsed;
100 }
101
setNextTime(qint64 ms)102 void GstAnimationDriver::setNextTime(qint64 ms)
103 {
104 m_next = ms;
105 }
106
107 typedef enum
108 {
109 STATE_ERROR = -1,
110 STATE_NEW = 0,
111 STATE_WAITING_FOR_WINDOW,
112 STATE_WINDOW_CREATED,
113 STATE_READY,
114 } SharedRenderDataState;
115
116 struct SharedRenderData
117 {
118 int refcount;
119 SharedRenderDataState state;
120 GMutex lock;
121 GCond cond;
122 GstAnimationDriver *m_animationDriver;
123 QOpenGLContext *m_context;
124 GstBackingSurface *m_surface;
125 QThread *m_renderThread;
126 };
127
128 static struct SharedRenderData *
shared_render_data_new(void)129 shared_render_data_new (void)
130 {
131 struct SharedRenderData *ret = g_new0 (struct SharedRenderData, 1);
132
133 g_atomic_int_set (&ret->refcount, 1);
134 g_mutex_init (&ret->lock);
135
136 return ret;
137 }
138
139 static void
shared_render_data_free(struct SharedRenderData * data)140 shared_render_data_free (struct SharedRenderData * data)
141 {
142 GST_DEBUG ("%p freeing shared render data", data);
143
144 g_mutex_clear (&data->lock);
145
146 if (data->m_animationDriver) {
147 data->m_animationDriver->uninstall();
148 delete data->m_animationDriver;
149 }
150 data->m_animationDriver = nullptr;
151 if (data->m_context)
152 delete data->m_context;
153 data->m_context = nullptr;
154 if (data->m_surface)
155 delete data->m_surface;
156 data->m_surface = nullptr;
157 }
158
159 static struct SharedRenderData *
shared_render_data_ref(struct SharedRenderData * data)160 shared_render_data_ref (struct SharedRenderData * data)
161 {
162 GST_TRACE ("%p reffing shared render data", data);
163 g_atomic_int_inc (&data->refcount);
164 return data;
165 }
166
167 static void
shared_render_data_unref(struct SharedRenderData * data)168 shared_render_data_unref (struct SharedRenderData * data)
169 {
170 GST_TRACE ("%p unreffing shared render data", data);
171 if (g_atomic_int_dec_and_test (&data->refcount))
172 shared_render_data_free (data);
173 }
174
175 void
deactivateContext()176 GstQuickRenderer::deactivateContext ()
177 {
178 }
179
180 void
activateContext()181 GstQuickRenderer::activateContext ()
182 {
183 }
184
185 struct FBOUserData
186 {
187 GstGLContext * context;
188 QOpenGLFramebufferObject * fbo;
189 };
190
191 static void
delete_cxx_gl_context(GstGLContext * context,struct FBOUserData * data)192 delete_cxx_gl_context (GstGLContext * context, struct FBOUserData * data)
193 {
194 GST_TRACE ("freeing Qfbo %p", data->fbo);
195 delete data->fbo;
196 }
197
198 static void
notify_fbo_delete(struct FBOUserData * data)199 notify_fbo_delete (struct FBOUserData * data)
200 {
201 gst_gl_context_thread_add (data->context,
202 (GstGLContextThreadFunc) delete_cxx_gl_context, data);
203 gst_object_unref (data->context);
204 g_free (data);
205 }
206
GstQuickRenderer()207 GstQuickRenderer::GstQuickRenderer()
208 : gl_context(NULL),
209 m_fbo(nullptr),
210 m_quickWindow(nullptr),
211 m_renderControl(nullptr),
212 m_qmlEngine(nullptr),
213 m_qmlComponent(nullptr),
214 m_rootItem(nullptr),
215 gl_allocator(NULL),
216 gl_params(NULL),
217 gl_mem(NULL),
218 m_sharedRenderData(NULL)
219 {
220 init_debug ();
221 }
222
223 static gpointer
dup_shared_render_data(gpointer data,gpointer user_data)224 dup_shared_render_data (gpointer data, gpointer user_data)
225 {
226 struct SharedRenderData *render_data = (struct SharedRenderData *) data;
227
228 if (render_data)
229 return shared_render_data_ref (render_data);
230
231 return NULL;
232 }
233
234 class CreateSurfaceEvent : public QEvent
235 {
236 public:
CreateSurfaceEvent(CreateSurfaceWorker * worker)237 CreateSurfaceEvent (CreateSurfaceWorker * worker)
238 : QEvent(CreateSurfaceEvent::type())
239 {
240 m_worker = worker;
241 }
242
~CreateSurfaceEvent()243 ~CreateSurfaceEvent()
244 {
245 GST_TRACE ("%p destroying create surface event", this);
246 delete m_worker;
247 }
248
type()249 static QEvent::Type type()
250 {
251 if (customEventType == QEvent::None) {
252 int generatedType = QEvent::registerEventType();
253 customEventType = static_cast<QEvent::Type>(generatedType);
254 }
255 return customEventType;
256 }
257
258 private:
259 static QEvent::Type customEventType;
260 CreateSurfaceWorker *m_worker;
261 };
262
263 QEvent::Type CreateSurfaceEvent::customEventType = QEvent::None;
264
265
CreateSurfaceWorker(struct SharedRenderData * rdata)266 CreateSurfaceWorker::CreateSurfaceWorker (struct SharedRenderData * rdata)
267 {
268 m_sharedRenderData = shared_render_data_ref (rdata);
269 }
270
~CreateSurfaceWorker()271 CreateSurfaceWorker::~CreateSurfaceWorker ()
272 {
273 shared_render_data_unref (m_sharedRenderData);
274 }
275
event(QEvent * ev)276 bool CreateSurfaceWorker::event(QEvent * ev)
277 {
278 if (ev->type() == CreateSurfaceEvent::type()) {
279 GST_TRACE ("%p creating surface", m_sharedRenderData);
280 /* create the window surface in the main thread */
281 g_mutex_lock (&m_sharedRenderData->lock);
282 m_sharedRenderData->m_surface = new GstBackingSurface;
283 m_sharedRenderData->m_surface->create();
284 m_sharedRenderData->m_surface->moveToThread (m_sharedRenderData->m_renderThread);
285 GST_TRACE ("%p created surface %p", m_sharedRenderData,
286 m_sharedRenderData->m_surface);
287 g_cond_broadcast (&m_sharedRenderData->cond);
288 g_mutex_unlock (&m_sharedRenderData->lock);
289 }
290
291 return QObject::event(ev);
292 }
293
init(GstGLContext * context,GError ** error)294 bool GstQuickRenderer::init (GstGLContext * context, GError ** error)
295 {
296 g_return_val_if_fail (GST_IS_GL_CONTEXT (context), false);
297 g_return_val_if_fail (gst_gl_context_get_current () == context, false);
298
299 QVariant qt_native_context = qt_opengl_native_context_from_gst_gl_context (context);
300
301 if (qt_native_context.isNull()) {
302 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_NOT_FOUND,
303 "Could not convert from the provided GstGLContext to a Qt "
304 "native context");
305 return false;
306 }
307
308 struct SharedRenderData *render_data = NULL, *old_render_data;
309 do {
310 if (render_data)
311 shared_render_data_unref (render_data);
312
313 old_render_data = render_data = (struct SharedRenderData *)
314 g_object_dup_data (G_OBJECT (context),
315 "qt.gl.render.shared.data", dup_shared_render_data, NULL);
316 if (!render_data)
317 render_data = shared_render_data_new ();
318 } while (old_render_data != render_data
319 && !g_object_replace_data (G_OBJECT (context),
320 "qt.gl.render.shared.data", old_render_data, render_data,
321 NULL, NULL));
322 m_sharedRenderData = render_data;
323 GST_TRACE ("%p retrieved shared render data %p", this, m_sharedRenderData);
324
325 g_mutex_lock (&m_sharedRenderData->lock);
326 if (m_sharedRenderData->state == STATE_ERROR) {
327 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_NOT_FOUND,
328 "In an error state from a previous attempt");
329 g_mutex_unlock (&m_sharedRenderData->lock);
330 return false;
331 }
332
333 if (m_sharedRenderData->state != STATE_READY) {
334 /* this state handling and locking is so that two qtglrenderer's will
335 * not attempt to create an OpenGL context without freeing the previous
336 * OpenGL context and cause a leak. It also only allows one
337 * CreateSurfaceEvent() to be posted to the main thread
338 * (QCoreApplication::instance()->thread()) while still allowing
339 * multiple waiters to wait for the window to be created */
340 if (m_sharedRenderData->state == STATE_NEW) {
341 QCoreApplication *app = QCoreApplication::instance ();
342
343 if (!app) {
344 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_NOT_FOUND,
345 "Could not retrieve QCoreApplication instance");
346 m_sharedRenderData->state = STATE_ERROR;
347 g_mutex_unlock (&m_sharedRenderData->lock);
348 return false;
349 }
350
351 m_sharedRenderData->m_renderThread = QThread::currentThread();
352 m_sharedRenderData->m_context = new QOpenGLContext;
353 GST_TRACE ("%p new QOpenGLContext %p", this, m_sharedRenderData->m_context);
354 m_sharedRenderData->m_context->setNativeHandle(qt_native_context);
355
356 CreateSurfaceWorker *w = new CreateSurfaceWorker (m_sharedRenderData);
357 GST_TRACE ("%p posting create surface event to main thread with "
358 "worker %p", this, w);
359 w->moveToThread (app->thread());
360 app->postEvent (w, new CreateSurfaceEvent (w));
361 m_sharedRenderData->state = STATE_WAITING_FOR_WINDOW;
362 }
363
364 if (m_sharedRenderData->state == STATE_WAITING_FOR_WINDOW) {
365 gint64 end_time = g_get_monotonic_time () + 5 * G_TIME_SPAN_SECOND;
366 while (!m_sharedRenderData->m_surface) {
367 /* XXX: This might deadlock with the main thread if the
368 * QCoreApplication is not running and will not be able to
369 * execute. We only wait for 5 seconds until a better
370 * approach can be found here */
371 if (!g_cond_wait_until (&m_sharedRenderData->cond,
372 &m_sharedRenderData->lock, end_time)) {
373 g_set_error (error, GST_RESOURCE_ERROR,
374 GST_RESOURCE_ERROR_NOT_FOUND,
375 "Could not create Qt window within 5 seconds");
376 m_sharedRenderData->state = STATE_ERROR;
377 g_mutex_unlock (&m_sharedRenderData->lock);
378 return false;
379 }
380 }
381
382 GST_TRACE ("%p surface successfully created", this);
383 m_sharedRenderData->state = STATE_WINDOW_CREATED;
384 }
385
386 if (m_sharedRenderData->state == STATE_WINDOW_CREATED) {
387 /* Qt does some things that may require the OpenGL context current
388 * in ->create() so that it has the necessry information to create
389 * the QOpenGLContext from the native handle. This may fail if the
390 * OpenGL context is already current in another thread so we need
391 * to deactivate the context from GStreamer's thread before asking
392 * Qt to create the QOpenGLContext with ->create().
393 */
394 gst_gl_context_activate (context, FALSE);
395 m_sharedRenderData->m_context->create();
396 m_sharedRenderData->m_context->doneCurrent();
397
398 if (!m_sharedRenderData->m_context->makeCurrent(m_sharedRenderData->m_surface)) {
399 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_NOT_FOUND,
400 "Could not make Qt OpenGL context current");
401 /* try to keep the same OpenGL context state */
402 gst_gl_context_activate (context, TRUE);
403 m_sharedRenderData->state = STATE_ERROR;
404 g_mutex_unlock (&m_sharedRenderData->lock);
405 return false;
406 }
407
408 if (!gst_gl_context_activate (context, TRUE)) {
409 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_NOT_FOUND,
410 "Could not make OpenGL context current again");
411 m_sharedRenderData->state = STATE_ERROR;
412 g_mutex_unlock (&m_sharedRenderData->lock);
413 return false;
414 }
415 m_sharedRenderData->state = STATE_READY;
416 }
417 }
418
419 m_renderControl = new QQuickRenderControl();
420 /* Create a QQuickWindow that is associated with our render control. Note that this
421 * window never gets created or shown, meaning that it will never get an underlying
422 * native (platform) window.
423 */
424 m_quickWindow = new QQuickWindow(m_renderControl);
425 /* after QQuickWindow creation as QQuickRenderControl requires it */
426 m_renderControl->prepareThread (m_sharedRenderData->m_renderThread);
427 g_mutex_unlock (&m_sharedRenderData->lock);
428
429 /* Create a QML engine. */
430 m_qmlEngine = new QQmlEngine;
431 if (!m_qmlEngine->incubationController())
432 m_qmlEngine->setIncubationController(m_quickWindow->incubationController());
433
434 gl_context = static_cast<GstGLContext*>(gst_object_ref (context));
435 gl_allocator = (GstGLBaseMemoryAllocator *) gst_gl_memory_allocator_get_default (gl_context);
436 gl_params = (GstGLAllocationParams *)
437 gst_gl_video_allocation_params_new_wrapped_texture (gl_context,
438 NULL, &this->v_info, 0, NULL, GST_GL_TEXTURE_TARGET_2D, GST_GL_RGBA8,
439 0, NULL, (GDestroyNotify) notify_fbo_delete);
440
441 /* This is a gross hack relying on the internals of Qt and GStreamer
442 * however it's the only way to remove this warning on shutdown of all
443 * resources.
444 *
445 * GLib-CRITICAL **: 17:35:24.988: g_main_context_pop_thread_default: assertion 'g_queue_peek_head (stack) == context' failed
446 *
447 * The reason is that libgstgl has a GMainContext that it pushes as the
448 * thread default context. Then later, Qt pushes a thread default main
449 * context. The detruction order of the GMainContext's is reversed as
450 * GStreamer will explicitly pop the thread default main context however
451 * Qt pops when the thread is about to be destroyed. GMainContext is
452 * unhappy with the ordering of the pops.
453 */
454 GMainContext *gst_main_context = g_main_context_ref_thread_default ();
455
456 /* make Qt allocate and push a thread-default GMainContext if it is
457 * going to */
458 QEventLoop loop;
459 if (loop.processEvents())
460 GST_LOG ("pending QEvents processed");
461
462 GMainContext *qt_main_context = g_main_context_ref_thread_default ();
463
464 if (qt_main_context == gst_main_context) {
465 g_main_context_unref (qt_main_context);
466 g_main_context_unref (gst_main_context);
467 } else {
468 /* We flip the order of the GMainContext's so that the destruction
469 * order can be preserved. */
470 g_main_context_pop_thread_default (qt_main_context);
471 g_main_context_pop_thread_default (gst_main_context);
472 g_main_context_push_thread_default (qt_main_context);
473 g_main_context_push_thread_default (gst_main_context);
474 g_main_context_unref (qt_main_context);
475 g_main_context_unref (gst_main_context);
476 }
477
478 return true;
479 }
480
~GstQuickRenderer()481 GstQuickRenderer::~GstQuickRenderer()
482 {
483 gst_gl_allocation_params_free (gl_params);
484 gst_clear_object (&gl_allocator);
485 }
486
stopGL()487 void GstQuickRenderer::stopGL ()
488 {
489 GST_DEBUG ("%p stop QOpenGLContext curent: %p stored: %p", this,
490 QOpenGLContext::currentContext(), m_sharedRenderData->m_context);
491 g_assert (QOpenGLContext::currentContext() == m_sharedRenderData->m_context);
492
493 if (m_renderControl)
494 m_renderControl->invalidate();
495
496 if (m_fbo)
497 delete m_fbo;
498 m_fbo = nullptr;
499
500 QEventLoop loop;
501 if (loop.processEvents())
502 GST_LOG ("%p pending QEvents processed", this);
503
504 if (m_sharedRenderData)
505 shared_render_data_unref (m_sharedRenderData);
506 m_sharedRenderData = NULL;
507
508 /* XXX: reset the OpenGL context and drawable as Qt may have clobbered it.
509 * Fixes any attempt to access OpenGL after shutting down qmlgloverlay. */
510 gst_gl_context_activate (gl_context, FALSE);
511 gst_gl_context_activate (gl_context, TRUE);
512 }
513
cleanup()514 void GstQuickRenderer::cleanup()
515 {
516 if (gl_context)
517 gst_gl_context_thread_add (gl_context,
518 (GstGLContextThreadFunc) GstQuickRenderer::stop_c, this);
519
520 /* Delete the render control first since it will free the scenegraph resources.
521 * Destroy the QQuickWindow only afterwards. */
522 if (m_renderControl)
523 delete m_renderControl;
524 m_renderControl = nullptr;
525
526 if (m_qmlComponent)
527 delete m_qmlComponent;
528 m_qmlComponent = nullptr;
529 if (m_quickWindow)
530 delete m_quickWindow;
531 m_quickWindow = nullptr;
532 if (m_qmlEngine)
533 delete m_qmlEngine;
534 m_qmlEngine = nullptr;
535 if (m_rootItem)
536 delete m_rootItem;
537 m_rootItem = nullptr;
538
539 gst_clear_object (&gl_context);
540 }
541
ensureFbo()542 void GstQuickRenderer::ensureFbo()
543 {
544 if (m_fbo && m_fbo->size() != m_sharedRenderData->m_surface->size()) {
545 GST_INFO ("%p removing old framebuffer created with size %ix%i",
546 this, m_fbo->size().width(), m_fbo->size().height());
547 delete m_fbo;
548 m_fbo = nullptr;
549 }
550
551 if (!m_fbo) {
552 m_fbo = new QOpenGLFramebufferObject(m_sharedRenderData->m_surface->size(),
553 QOpenGLFramebufferObject::CombinedDepthStencil);
554 m_quickWindow->setRenderTarget(m_fbo);
555 GST_DEBUG ("%p new framebuffer created with size %ix%i", this,
556 m_fbo->size().width(), m_fbo->size().height());
557 }
558 }
559
560 void
renderGstGL()561 GstQuickRenderer::renderGstGL ()
562 {
563 const GstGLFuncs *gl = gl_context->gl_vtable;
564
565 GST_TRACE ("%p current QOpenGLContext %p", this,
566 QOpenGLContext::currentContext());
567 m_quickWindow->resetOpenGLState();
568
569 m_sharedRenderData->m_animationDriver->advance();
570
571 QEventLoop loop;
572 if (loop.processEvents())
573 GST_LOG ("pending QEvents processed");
574
575 loop.exit();
576
577 ensureFbo();
578
579 /* Synchronization and rendering happens here on the render thread. */
580 if (m_renderControl->sync())
581 GST_LOG ("sync successful");
582
583 /* Meanwhile on this thread continue with the actual rendering. */
584 m_renderControl->render();
585
586 GST_DEBUG ("wrapping Qfbo %p with texture %u", m_fbo, m_fbo->texture());
587 struct FBOUserData *data = g_new0 (struct FBOUserData, 1);
588 data->context = (GstGLContext *) gst_object_ref (gl_context);
589 data->fbo = m_fbo;
590 gl_params->user_data = static_cast<gpointer> (data);
591 gl_params->gl_handle = GINT_TO_POINTER (m_fbo->texture());
592 gl_mem = (GstGLMemory *) gst_gl_base_memory_alloc (gl_allocator, gl_params);
593
594 m_fbo = nullptr;
595
596 m_quickWindow->resetOpenGLState ();
597 /* Qt doesn't seem to reset this, breaking glimagesink */
598 if (gl->DrawBuffer)
599 gl->DrawBuffer (GL_BACK);
600 }
601
generateOutput(GstClockTime input_ns)602 GstGLMemory *GstQuickRenderer::generateOutput(GstClockTime input_ns)
603 {
604 m_sharedRenderData->m_animationDriver->setNextTime(input_ns / GST_MSECOND);
605
606 /* run an event loop to update any changed values for rendering */
607 QEventLoop loop;
608 if (loop.processEvents())
609 GST_LOG ("pending QEvents processed");
610
611 GST_LOG ("generating output for time %" GST_TIME_FORMAT " ms: %"
612 G_GUINT64_FORMAT, GST_TIME_ARGS (input_ns), input_ns / GST_MSECOND);
613
614 m_quickWindow->update();
615
616 /* Polishing happens on the gui thread. */
617 m_renderControl->polishItems();
618
619 /* TODO: an async version could be used where */
620 gst_gl_context_thread_add (gl_context,
621 (GstGLContextThreadFunc) GstQuickRenderer::render_gst_gl_c, this);
622
623 GstGLMemory *tmp = gl_mem;
624 gl_mem = NULL;
625
626 return tmp;
627 }
628
initializeGstGL()629 void GstQuickRenderer::initializeGstGL ()
630 {
631 GST_TRACE ("current QOpenGLContext %p", QOpenGLContext::currentContext());
632 if (!m_sharedRenderData->m_context->makeCurrent(m_sharedRenderData->m_surface)) {
633 m_errorString = "Failed to make Qt's wrapped OpenGL context current";
634 return;
635 }
636 GST_INFO ("current QOpenGLContext %p", QOpenGLContext::currentContext());
637
638 /* XXX: Avoid an assertion inside QSGDefaultRenderContext::initialize()
639 * from an unused (in this scenario) property when using multiple
640 * QQuickRenderControl's with the same QOpenGLContext.
641 *
642 * First noticed with Qt 5.15. Idea from:
643 * https://forum.qt.io/topic/55888/is-it-impossible-that-2-qquickrendercontrol-use-same-qopenglcontext/2
644 *
645 * ASSERT: "!m_gl->property(QSG_RENDERCONTEXT_PROPERTY).isValid()" in file /path/to/qt5/qtdeclarative/src/quick/scenegraph/qsgdefaultrendercontext.cpp, line 121
646 */
647 m_sharedRenderData->m_context->setProperty("_q_sgrendercontext", QVariant());
648
649 m_renderControl->initialize(m_sharedRenderData->m_context);
650
651 /* 1. QAnimationDriver's are thread-specific
652 * 2. QAnimationDriver controls the 'animation time' that the Qml scene is
653 * rendered at
654 */
655 /* FIXME: what happens with multiple qmlgloverlay elements? Do we need a
656 * shared animation driver? */
657 g_mutex_lock (&m_sharedRenderData->lock);
658 if (m_sharedRenderData->m_animationDriver == nullptr) {
659 m_sharedRenderData->m_animationDriver = new GstAnimationDriver;
660 m_sharedRenderData->m_animationDriver->install();
661 }
662 g_mutex_unlock (&m_sharedRenderData->lock);
663 /* XXX: reset the OpenGL context drawable as Qt may have clobbered it.
664 * Fixes glimagesink output where Qt replaces the Surface to use in its
665 * own MakeCurrent call. Qt does this on it's OpenGL initialisation
666 * the the rendering engine. */
667 gst_gl_context_activate (gl_context, FALSE);
668 gst_gl_context_activate (gl_context, TRUE);
669 }
670
initializeQml()671 void GstQuickRenderer::initializeQml()
672 {
673 disconnect(m_qmlComponent, &QQmlComponent::statusChanged, this,
674 &GstQuickRenderer::initializeQml);
675
676 if (m_qmlComponent->isError()) {
677 const QList<QQmlError> errorList = m_qmlComponent->errors();
678 for (const QQmlError &error : errorList)
679 m_errorString += error.toString();
680 return;
681 }
682
683 QObject *rootObject = m_qmlComponent->create();
684 if (m_qmlComponent->isError()) {
685 const QList<QQmlError> errorList = m_qmlComponent->errors();
686 for (const QQmlError &error : errorList)
687 m_errorString += error.toString();
688 delete rootObject;
689 return;
690 }
691
692 m_rootItem = qobject_cast<QQuickItem *>(rootObject);
693 if (!m_rootItem) {
694 m_errorString += "root QML item is not a QQuickItem";
695 delete rootObject;
696 return;
697 }
698
699 /* The root item is ready. Associate it with the window. */
700 m_rootItem->setParentItem(m_quickWindow->contentItem());
701
702 /* Update item and rendering related geometries. */
703 updateSizes();
704
705 /* Initialize the render control and our OpenGL resources. */
706 gst_gl_context_thread_add (gl_context,
707 (GstGLContextThreadFunc) GstQuickRenderer::initialize_gst_gl_c, this);
708 }
709
updateSizes()710 void GstQuickRenderer::updateSizes()
711 {
712 GstBackingSurface *surface =
713 static_cast<GstBackingSurface *>(m_sharedRenderData->m_surface);
714 /* Behave like SizeRootObjectToView. */
715 QSize size = surface->size();
716
717 m_rootItem->setWidth(size.width());
718 m_rootItem->setHeight(size.height());
719
720 m_quickWindow->setGeometry(0, 0, size.width(), size.height());
721
722 gst_video_info_set_format (&v_info, GST_VIDEO_FORMAT_RGBA, size.width(),
723 size.height());
724 GstGLVideoAllocationParams *params = (GstGLVideoAllocationParams *) (gl_params);
725 gst_video_info_set_format (params->v_info, GST_VIDEO_FORMAT_RGBA, size.width(),
726 size.height());
727 }
728
setSize(int w,int h)729 void GstQuickRenderer::setSize(int w, int h)
730 {
731 static_cast<GstBackingSurface *>(m_sharedRenderData->m_surface)->setSize(w, h);
732 updateSizes();
733 }
734
setQmlScene(const gchar * scene,GError ** error)735 bool GstQuickRenderer::setQmlScene (const gchar * scene, GError ** error)
736 {
737 /* replacing the scene is not supported */
738 g_return_val_if_fail (m_qmlComponent == NULL, false);
739
740 m_errorString = "";
741
742 m_qmlComponent = new QQmlComponent(m_qmlEngine);
743 /* XXX: do we need to provide a propper base name? */
744 m_qmlComponent->setData(QByteArray (scene), QUrl(""));
745 if (m_qmlComponent->isLoading())
746 /* TODO: handle async properly */
747 connect(m_qmlComponent, &QQmlComponent::statusChanged, this,
748 &GstQuickRenderer::initializeQml);
749 else
750 initializeQml();
751
752 if (m_errorString != "") {
753 QByteArray string = m_errorString.toUtf8();
754 g_set_error (error, GST_RESOURCE_ERROR, GST_RESOURCE_ERROR_SETTINGS,
755 "%s", string.constData());
756 return FALSE;
757 }
758
759 return TRUE;
760 }
761
rootItem() const762 QQuickItem * GstQuickRenderer::rootItem() const
763 {
764 return m_rootItem;
765 }
766