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>
29 #include "gstqsgtexture.h"
30 #include "gstqtglutility.h"
32 #include <QtCore/QDateTime>
33 #include <QtCore/QRunnable>
34 #include <QtGui/QGuiApplication>
35 #include <QtQuick/QQuickWindow>
36 #include <QOpenGLFramebufferObject>
38 /* compatability definitions... */
39 #ifndef GL_READ_FRAMEBUFFER
40 #define GL_READ_FRAMEBUFFER 0x8CA8
46 * #QtGLWindow is an #QQuickWindow that grab QtQuick view to GStreamer OpenGL video buffers.
49 GST_DEBUG_CATEGORY_STATIC (qt_window_debug);
50 #define GST_CAT_DEFAULT qt_window_debug
52 struct _QtGLWindowPrivate
65 gboolean useDefaultFbo;
67 GstGLDisplay *display;
68 GstGLContext *other_context;
70 /* frames that qmlview rendered in its gl thread */
71 quint64 frames_rendered;
76 class InitQtGLContext : public QRunnable
79 InitQtGLContext(QtGLWindow *window);
86 InitQtGLContext::InitQtGLContext(QtGLWindow *window) :
91 void InitQtGLContext::run()
93 window_->onSceneGraphInitialized();
96 QtGLWindow::QtGLWindow ( QWindow * parent, QQuickWindow *src ) :
97 QQuickWindow( parent ), source (src)
99 QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
100 static volatile gsize _debug;
102 g_assert (app != NULL);
104 if (g_once_init_enter (&_debug)) {
105 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwindow", 0, "Qt GL QuickWindow");
106 g_once_init_leave (&_debug, 1);
109 this->priv = g_new0 (QtGLWindowPrivate, 1);
111 g_mutex_init (&this->priv->lock);
112 g_cond_init (&this->priv->update_cond);
114 this->priv->display = gst_qt_get_gl_display();
116 connect (source, SIGNAL(beforeRendering()), this, SLOT(beforeRendering()), Qt::DirectConnection);
117 connect (source, SIGNAL(afterRendering()), this, SLOT(afterRendering()), Qt::DirectConnection);
118 connect (app, SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()), Qt::DirectConnection);
119 if (source->isSceneGraphInitialized())
120 source->scheduleRenderJob(new InitQtGLContext(this), QQuickWindow::BeforeSynchronizingStage);
122 connect (source, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
124 GST_DEBUG ("%p init Qt Window", this->priv->display);
127 QtGLWindow::~QtGLWindow()
129 GST_DEBUG ("deinit Qt Window");
130 g_mutex_clear (&this->priv->lock);
131 g_cond_clear (&this->priv->update_cond);
132 if (this->priv->other_context)
133 gst_object_unref(this->priv->other_context);
134 if (this->priv->display)
135 gst_object_unref(this->priv->display);
141 QtGLWindow::beforeRendering()
143 unsigned int width, height;
145 g_mutex_lock (&this->priv->lock);
147 static volatile gsize once = 0;
148 if (g_once_init_enter(&once)) {
149 this->priv->start = QDateTime::currentDateTime().toMSecsSinceEpoch();
150 g_once_init_leave(&once,1);
153 if (!fbo && !this->priv->useDefaultFbo) {
155 width = source->width();
156 height = source->height();
158 GST_DEBUG ("create new framebuffer object %dX%d", width, height);
160 fbo.reset(new QOpenGLFramebufferObject (width, height,
161 QOpenGLFramebufferObject::NoAttachment, GL_TEXTURE_2D, GL_RGBA));
163 source->setRenderTarget(fbo.data());
164 } else if (this->priv->useDefaultFbo) {
165 GST_DEBUG ("use default fbo for render target");
167 source->setRenderTarget(NULL);
170 g_mutex_unlock (&this->priv->lock);
175 QtGLWindow::afterRendering()
177 GstVideoFrame gl_frame;
179 GstGLContext *context;
182 const GstGLFuncs *gl;
185 g_mutex_lock (&this->priv->lock);
187 this->priv->frames_rendered++;
189 if(!this->priv->buffer || this->priv->updated == TRUE) {
190 GST_DEBUG ("skip this frame");
191 g_mutex_unlock (&this->priv->lock);
195 GST_DEBUG ("copy buffer %p",this->priv->buffer);
197 width = GST_VIDEO_INFO_WIDTH (&this->priv->v_info);
198 height = GST_VIDEO_INFO_HEIGHT (&this->priv->v_info);
199 info = &this->priv->v_info;
200 context = this->priv->other_context;
202 gst_gl_context_activate (context, TRUE);
203 gl = context->gl_vtable;
205 ret = gst_video_frame_map (&gl_frame, info, this->priv->buffer,
206 (GstMapFlags) (GST_MAP_WRITE | GST_MAP_GL));
209 this->priv->buffer = NULL;
210 GST_ERROR ("Failed to map video frame");
214 gl->BindFramebuffer (GL_READ_FRAMEBUFFER, this->source->renderTargetId());
216 ret = gst_gl_context_check_framebuffer_status (context);
218 GST_ERROR ("FBO errors");
222 dst_tex = *(guint *) gl_frame.data[0];
223 GST_DEBUG ("qml render target id %d, render to tex %d %dX%d",
224 this->source->renderTargetId(), dst_tex, width,height);
226 gl->BindTexture (GL_TEXTURE_2D, dst_tex);
227 gl->CopyTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, width, height, 0);
229 GST_DEBUG ("rendering finished");
232 gl->BindFramebuffer (GL_FRAMEBUFFER, 0);
233 gst_video_frame_unmap (&gl_frame);
235 gst_gl_context_activate (context, FALSE);
237 this->priv->result = ret;
238 this->priv->updated = TRUE;
239 g_cond_signal (&this->priv->update_cond);
240 g_mutex_unlock (&this->priv->lock);
244 QtGLWindow::aboutToQuit()
246 g_mutex_lock (&this->priv->lock);
248 this->priv->updated = TRUE;
249 this->priv->quit = TRUE;
250 g_cond_signal (&this->priv->update_cond);
252 this->priv->stop = QDateTime::currentDateTime().toMSecsSinceEpoch();
253 qint64 duration = this->priv->stop - this->priv->start;
254 float fps = ((float)this->priv->frames_rendered / duration * 1000);
256 GST_DEBUG("about to quit, total refresh frames (%lld) in (%0.3f) seconds, fps: %0.3f",
257 this->priv->frames_rendered, (float)duration / 1000, fps);
259 g_mutex_unlock (&this->priv->lock);
263 QtGLWindow::onSceneGraphInitialized()
265 GST_DEBUG ("scene graph initialization with Qt GL context %p",
266 this->source->openglContext ());
268 this->priv->initted = gst_qt_get_gl_wrapcontext (this->priv->display,
269 &this->priv->other_context, NULL);
271 GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
272 this->priv->other_context);
276 QtGLWindow::getGeometry(int * width, int * height)
278 if (width == NULL || height == NULL)
281 *width = this->source->width();
282 *height = this->source->height();
288 qt_window_get_qt_context (QtGLWindow * qt_window)
290 g_return_val_if_fail (qt_window != NULL, NULL);
292 if (!qt_window->priv->other_context)
295 return (GstGLContext *) gst_object_ref (qt_window->priv->other_context);
299 qt_window_get_display (QtGLWindow * qt_window)
301 g_return_val_if_fail (qt_window != NULL, NULL);
303 if (!qt_window->priv->display)
306 return (GstGLDisplay *) gst_object_ref (qt_window->priv->display);
310 qt_window_is_scenegraph_initialized (QtGLWindow * qt_window)
312 g_return_val_if_fail (qt_window != NULL, FALSE);
314 return qt_window->priv->initted;
318 qt_window_set_caps (QtGLWindow * qt_window, GstCaps * caps)
322 g_return_val_if_fail (qt_window != NULL, FALSE);
323 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
324 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
326 if (qt_window->priv->caps && gst_caps_is_equal_fixed (qt_window->priv->caps, caps))
329 if (!gst_video_info_from_caps (&v_info, caps))
332 g_mutex_lock (&qt_window->priv->lock);
334 gst_caps_replace (&qt_window->priv->caps, caps);
336 qt_window->priv->v_info = v_info;
338 g_mutex_unlock (&qt_window->priv->lock);
344 qt_window_set_buffer (QtGLWindow * qt_window, GstBuffer * buffer)
346 g_return_val_if_fail (qt_window != NULL, FALSE);
347 g_return_val_if_fail (qt_window->priv->initted, FALSE);
350 g_mutex_lock (&qt_window->priv->lock);
352 if (qt_window->priv->quit){
353 GST_DEBUG("about to quit, drop this buffer");
354 g_mutex_unlock (&qt_window->priv->lock);
358 qt_window->priv->updated = FALSE;
359 qt_window->priv->buffer = buffer;
361 while (!qt_window->priv->updated)
362 g_cond_wait (&qt_window->priv->update_cond, &qt_window->priv->lock);
364 ret = qt_window->priv->result;
366 g_mutex_unlock (&qt_window->priv->lock);
372 qt_window_use_default_fbo (QtGLWindow * qt_window, gboolean useDefaultFbo)
374 g_return_if_fail (qt_window != NULL);
376 g_mutex_lock (&qt_window->priv->lock);
378 GST_DEBUG ("set to use default fbo %d", useDefaultFbo);
379 qt_window->priv->useDefaultFbo = useDefaultFbo;
381 g_mutex_unlock (&qt_window->priv->lock);