3 * Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 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 * Library General Public License for more details.
15 * You should have received a copy of the GNU Library General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
27 #include <gst/video/video.h>
28 #include <gst/gl/gstglfuncs.h>
30 #include "gstqsgtexture.h"
31 #include "gstqtglutility.h"
33 #include <QtCore/QDateTime>
34 #include <QtGui/QGuiApplication>
35 #include <QtQuick/QQuickWindow>
36 #include <QOpenGLFramebufferObject>
38 /* compatibility definitions... */
39 #ifndef GL_READ_FRAMEBUFFER
40 #define GL_READ_FRAMEBUFFER 0x8CA8
42 #ifndef GL_DRAW_FRAMEBUFFER
43 #define GL_DRAW_FRAMEBUFFER 0x8CA9
49 * #QtGLWindow is an #QQuickWindow that grab QtQuick view to GStreamer OpenGL video buffers.
52 GST_DEBUG_CATEGORY_STATIC (qt_window_debug);
53 #define GST_CAT_DEFAULT qt_window_debug
55 struct _QtGLWindowPrivate
68 gboolean useDefaultFbo;
70 GstGLDisplay *display;
71 GstGLContext *other_context;
72 GstGLContext *context;
76 /* frames that qmlview rendered in its gl thread */
77 quint64 frames_rendered;
82 QtGLWindow::QtGLWindow ( QWindow * parent, QQuickWindow *src ) :
83 QQuickWindow( parent ), source (src)
85 QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
88 g_assert (app != NULL);
90 if (g_once_init_enter (&_debug)) {
91 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwindow", 0, "Qt GL QuickWindow");
92 g_once_init_leave (&_debug, 1);
95 this->priv = g_new0 (QtGLWindowPrivate, 1);
97 g_mutex_init (&this->priv->lock);
98 g_cond_init (&this->priv->update_cond);
100 this->priv->display = gst_qt_get_gl_display(FALSE);
102 connect (source, SIGNAL(beforeRendering()), this, SLOT(beforeRendering()), Qt::DirectConnection);
103 connect (source, SIGNAL(afterRendering()), this, SLOT(afterRendering()), Qt::DirectConnection);
104 connect (app, SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()), Qt::DirectConnection);
105 if (source->isSceneGraphInitialized())
106 source->scheduleRenderJob(new RenderJob(std::bind(&QtGLWindow::onSceneGraphInitialized, this)), QQuickWindow::BeforeSynchronizingStage);
108 connect (source, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
110 connect (source, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
112 GST_DEBUG ("%p init Qt Window", this->priv->display);
115 QtGLWindow::~QtGLWindow()
117 GST_DEBUG ("deinit Qt Window");
118 g_mutex_clear (&this->priv->lock);
119 g_cond_clear (&this->priv->update_cond);
120 if (this->priv->other_context)
121 gst_object_unref(this->priv->other_context);
122 if (this->priv->display)
123 gst_object_unref(this->priv->display);
124 if (this->priv->context)
125 gst_object_unref(this->priv->context);
126 if (this->priv->caps)
127 gst_caps_unref(this->priv->caps);
133 QtGLWindow::beforeRendering()
135 unsigned int width, height;
137 g_mutex_lock (&this->priv->lock);
139 static gsize once = 0;
140 if (g_once_init_enter(&once)) {
141 this->priv->start = QDateTime::currentDateTime().toMSecsSinceEpoch();
142 g_once_init_leave(&once,1);
145 if (!fbo && !this->priv->useDefaultFbo) {
147 width = source->width();
148 height = source->height();
150 GST_DEBUG ("create new framebuffer object %dX%d", width, height);
152 fbo.reset(new QOpenGLFramebufferObject (width, height,
153 QOpenGLFramebufferObject::CombinedDepthStencil, GL_TEXTURE_2D, GL_RGBA));
155 source->setRenderTarget(fbo.data());
156 } else if (this->priv->useDefaultFbo) {
157 GST_DEBUG ("use default fbo for render target");
159 source->setRenderTarget(NULL);
162 g_mutex_unlock (&this->priv->lock);
167 QtGLWindow::afterRendering()
169 GstVideoFrame gl_frame;
171 GstGLContext *context;
174 const GstGLFuncs *gl;
176 GstGLSyncMeta *sync_meta;
178 g_mutex_lock (&this->priv->lock);
180 this->priv->frames_rendered++;
182 if(!this->priv->buffer || this->priv->updated == TRUE) {
183 GST_DEBUG ("skip this frame");
184 g_mutex_unlock (&this->priv->lock);
188 GST_DEBUG ("copy buffer %p",this->priv->buffer);
190 width = GST_VIDEO_INFO_WIDTH (&this->priv->v_info);
191 height = GST_VIDEO_INFO_HEIGHT (&this->priv->v_info);
192 info = &this->priv->v_info;
193 context = this->priv->other_context;
195 gst_gl_context_activate (context, TRUE);
196 gl = context->gl_vtable;
198 ret = gst_video_frame_map (&gl_frame, info, this->priv->buffer,
199 (GstMapFlags) (GST_MAP_WRITE | GST_MAP_GL));
202 this->priv->buffer = NULL;
203 GST_ERROR ("Failed to map video frame");
207 gl->BindFramebuffer (GL_READ_FRAMEBUFFER, this->source->renderTargetId());
209 ret = gst_gl_context_check_framebuffer_status (context, GL_READ_FRAMEBUFFER);
211 GST_ERROR ("FBO errors");
215 dst_tex = *(guint *) gl_frame.data[0];
216 GST_DEBUG ("qml render target id %d, render to tex %d %dX%d",
217 this->source->renderTargetId(), dst_tex, width,height);
219 gl->BindTexture (GL_TEXTURE_2D, dst_tex);
220 if (gl->BlitFramebuffer) {
221 gl->BindFramebuffer (GL_DRAW_FRAMEBUFFER, this->priv->fbo);
222 gl->FramebufferTexture2D (GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
223 GL_TEXTURE_2D, dst_tex, 0);
225 ret = gst_gl_context_check_framebuffer_status (context, GL_DRAW_FRAMEBUFFER);
227 GST_ERROR ("FBO errors");
230 if (this->priv->useDefaultFbo)
231 gl->ReadBuffer (GL_BACK);
233 gl->ReadBuffer (GL_COLOR_ATTACHMENT0);
234 gl->BlitFramebuffer (0, 0, width, height,
236 GL_COLOR_BUFFER_BIT, GL_LINEAR);
238 gl->CopyTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, width, height, 0);
241 gl->BindFramebuffer (GL_FRAMEBUFFER, 0);
242 gst_video_frame_unmap (&gl_frame);
244 if (this->priv->context) {
245 sync_meta = gst_buffer_get_gl_sync_meta (this->priv->buffer);
247 sync_meta = gst_buffer_add_gl_sync_meta (this->priv->context, this->priv->buffer);
249 gst_gl_sync_meta_set_sync_point (sync_meta, context);
252 GST_DEBUG ("rendering finished");
255 gst_gl_context_activate (context, FALSE);
257 this->priv->result = ret;
258 this->priv->updated = TRUE;
259 g_cond_signal (&this->priv->update_cond);
260 g_mutex_unlock (&this->priv->lock);
264 gl->BindFramebuffer (GL_FRAMEBUFFER, 0);
265 gst_video_frame_unmap (&gl_frame);
270 QtGLWindow::aboutToQuit()
272 g_mutex_lock (&this->priv->lock);
274 this->priv->updated = TRUE;
275 this->priv->quit = TRUE;
276 g_cond_signal (&this->priv->update_cond);
278 this->priv->stop = QDateTime::currentDateTime().toMSecsSinceEpoch();
279 qint64 duration = this->priv->stop - this->priv->start;
280 float fps = ((float)this->priv->frames_rendered / duration * 1000);
282 GST_DEBUG("about to quit, total refresh frames (%lld) in (%0.3f) seconds, fps: %0.3f",
283 this->priv->frames_rendered, (float)duration / 1000, fps);
285 g_mutex_unlock (&this->priv->lock);
289 QtGLWindow::onSceneGraphInitialized()
291 GST_DEBUG ("scene graph initialization with Qt GL context %p",
292 this->source->openglContext ());
294 this->priv->initted = gst_qt_get_gl_wrapcontext (this->priv->display,
295 &this->priv->other_context, &this->priv->context);
297 if (this->priv->initted && this->priv->other_context) {
298 const GstGLFuncs *gl;
300 gst_gl_context_activate (this->priv->other_context, TRUE);
301 gl = this->priv->other_context->gl_vtable;
303 gl->GenFramebuffers (1, &this->priv->fbo);
305 gst_gl_context_activate (this->priv->other_context, FALSE);
308 GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
309 this->priv->other_context);
313 QtGLWindow::onSceneGraphInvalidated()
315 GST_DEBUG ("scene graph invalidated");
317 if (this->priv->fbo && this->priv->other_context) {
318 const GstGLFuncs *gl;
320 gst_gl_context_activate (this->priv->other_context, TRUE);
321 gl = this->priv->other_context->gl_vtable;
323 gl->DeleteFramebuffers (1, &this->priv->fbo);
325 gst_gl_context_activate (this->priv->other_context, FALSE);
330 QtGLWindow::getGeometry(int * width, int * height)
332 if (width == NULL || height == NULL)
335 double scale = this->source->effectiveDevicePixelRatio();
336 *width = this->source->width() * scale;
337 *height = this->source->height() * scale;
339 GST_LOG("Window width %d height %d scale %f", *width, *height,
346 qt_window_get_qt_context (QtGLWindow * qt_window)
348 g_return_val_if_fail (qt_window != NULL, NULL);
350 if (!qt_window->priv->other_context)
353 return (GstGLContext *) gst_object_ref (qt_window->priv->other_context);
357 qt_window_get_display (QtGLWindow * qt_window)
359 g_return_val_if_fail (qt_window != NULL, NULL);
361 if (!qt_window->priv->display)
364 return (GstGLDisplay *) gst_object_ref (qt_window->priv->display);
368 qt_window_get_context (QtGLWindow * qt_window)
370 g_return_val_if_fail (qt_window != NULL, NULL);
372 if (!qt_window->priv->context)
375 return (GstGLContext *) gst_object_ref (qt_window->priv->context);
379 qt_window_set_context (QtGLWindow * qt_window, GstGLContext * context)
381 g_return_val_if_fail (qt_window != NULL, FALSE);
383 if (qt_window->priv->context && qt_window->priv->context != context)
386 gst_object_replace ((GstObject **) &qt_window->priv->context, (GstObject *) context);
392 qt_window_is_scenegraph_initialized (QtGLWindow * qt_window)
394 g_return_val_if_fail (qt_window != NULL, FALSE);
396 return qt_window->priv->initted;
400 qt_window_set_caps (QtGLWindow * qt_window, GstCaps * caps)
404 g_return_val_if_fail (qt_window != NULL, FALSE);
405 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
406 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
408 if (qt_window->priv->caps && gst_caps_is_equal_fixed (qt_window->priv->caps, caps))
411 if (!gst_video_info_from_caps (&v_info, caps))
414 g_mutex_lock (&qt_window->priv->lock);
416 gst_caps_replace (&qt_window->priv->caps, caps);
418 qt_window->priv->v_info = v_info;
420 g_mutex_unlock (&qt_window->priv->lock);
426 qt_window_set_buffer (QtGLWindow * qt_window, GstBuffer * buffer)
428 g_return_val_if_fail (qt_window != NULL, FALSE);
429 g_return_val_if_fail (qt_window->priv->initted, FALSE);
432 g_mutex_lock (&qt_window->priv->lock);
434 if (qt_window->priv->quit){
435 GST_DEBUG("about to quit, drop this buffer");
436 g_mutex_unlock (&qt_window->priv->lock);
440 qt_window->priv->updated = FALSE;
441 qt_window->priv->buffer = buffer;
443 while (!qt_window->priv->updated)
444 g_cond_wait (&qt_window->priv->update_cond, &qt_window->priv->lock);
446 ret = qt_window->priv->result;
448 g_mutex_unlock (&qt_window->priv->lock);
454 qt_window_use_default_fbo (QtGLWindow * qt_window, gboolean useDefaultFbo)
456 g_return_if_fail (qt_window != NULL);
458 g_mutex_lock (&qt_window->priv->lock);
460 GST_DEBUG ("set to use default fbo %d", useDefaultFbo);
461 qt_window->priv->useDefaultFbo = useDefaultFbo;
463 g_mutex_unlock (&qt_window->priv->lock);
467 qt_window_stop(QtGLWindow* qt_window)
469 g_mutex_lock(&qt_window->priv->lock);
471 GST_DEBUG("stop window");
472 qt_window->priv->updated = TRUE;
473 g_cond_signal(&qt_window->priv->update_cond);
475 g_mutex_unlock(&qt_window->priv->lock);