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 <QtCore/QRunnable>
35 #include <QtGui/QGuiApplication>
36 #include <QtQuick/QQuickWindow>
37 #include <QOpenGLFramebufferObject>
39 /* compatability definitions... */
40 #ifndef GL_READ_FRAMEBUFFER
41 #define GL_READ_FRAMEBUFFER 0x8CA8
43 #ifndef GL_DRAW_FRAMEBUFFER
44 #define GL_DRAW_FRAMEBUFFER 0x8CA9
50 * #QtGLWindow is an #QQuickWindow that grab QtQuick view to GStreamer OpenGL video buffers.
53 GST_DEBUG_CATEGORY_STATIC (qt_window_debug);
54 #define GST_CAT_DEFAULT qt_window_debug
56 struct _QtGLWindowPrivate
69 gboolean useDefaultFbo;
71 GstGLDisplay *display;
72 GstGLContext *other_context;
76 /* frames that qmlview rendered in its gl thread */
77 quint64 frames_rendered;
82 class InitQtGLContext : public QRunnable
85 InitQtGLContext(QtGLWindow *window);
92 InitQtGLContext::InitQtGLContext(QtGLWindow *window) :
97 void InitQtGLContext::run()
99 window_->onSceneGraphInitialized();
102 QtGLWindow::QtGLWindow ( QWindow * parent, QQuickWindow *src ) :
103 QQuickWindow( parent ), source (src)
105 QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
106 static volatile gsize _debug;
108 g_assert (app != NULL);
110 if (g_once_init_enter (&_debug)) {
111 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwindow", 0, "Qt GL QuickWindow");
112 g_once_init_leave (&_debug, 1);
115 this->priv = g_new0 (QtGLWindowPrivate, 1);
117 g_mutex_init (&this->priv->lock);
118 g_cond_init (&this->priv->update_cond);
120 this->priv->display = gst_qt_get_gl_display();
122 connect (source, SIGNAL(beforeRendering()), this, SLOT(beforeRendering()), Qt::DirectConnection);
123 connect (source, SIGNAL(afterRendering()), this, SLOT(afterRendering()), Qt::DirectConnection);
124 connect (app, SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()), Qt::DirectConnection);
125 if (source->isSceneGraphInitialized())
126 source->scheduleRenderJob(new InitQtGLContext(this), QQuickWindow::BeforeSynchronizingStage);
128 connect (source, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
130 connect (source, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
132 GST_DEBUG ("%p init Qt Window", this->priv->display);
135 QtGLWindow::~QtGLWindow()
137 GST_DEBUG ("deinit Qt Window");
138 g_mutex_clear (&this->priv->lock);
139 g_cond_clear (&this->priv->update_cond);
140 if (this->priv->other_context)
141 gst_object_unref(this->priv->other_context);
142 if (this->priv->display)
143 gst_object_unref(this->priv->display);
149 QtGLWindow::beforeRendering()
151 unsigned int width, height;
153 g_mutex_lock (&this->priv->lock);
155 static volatile gsize once = 0;
156 if (g_once_init_enter(&once)) {
157 this->priv->start = QDateTime::currentDateTime().toMSecsSinceEpoch();
158 g_once_init_leave(&once,1);
161 if (!fbo && !this->priv->useDefaultFbo) {
163 width = source->width();
164 height = source->height();
166 GST_DEBUG ("create new framebuffer object %dX%d", width, height);
168 fbo.reset(new QOpenGLFramebufferObject (width, height,
169 QOpenGLFramebufferObject::NoAttachment, GL_TEXTURE_2D, GL_RGBA));
171 source->setRenderTarget(fbo.data());
172 } else if (this->priv->useDefaultFbo) {
173 GST_DEBUG ("use default fbo for render target");
175 source->setRenderTarget(NULL);
178 g_mutex_unlock (&this->priv->lock);
183 QtGLWindow::afterRendering()
185 GstVideoFrame gl_frame;
187 GstGLContext *context;
190 const GstGLFuncs *gl;
193 g_mutex_lock (&this->priv->lock);
195 this->priv->frames_rendered++;
197 if(!this->priv->buffer || this->priv->updated == TRUE) {
198 GST_DEBUG ("skip this frame");
199 g_mutex_unlock (&this->priv->lock);
203 GST_DEBUG ("copy buffer %p",this->priv->buffer);
205 width = GST_VIDEO_INFO_WIDTH (&this->priv->v_info);
206 height = GST_VIDEO_INFO_HEIGHT (&this->priv->v_info);
207 info = &this->priv->v_info;
208 context = this->priv->other_context;
210 gst_gl_context_activate (context, TRUE);
211 gl = context->gl_vtable;
213 ret = gst_video_frame_map (&gl_frame, info, this->priv->buffer,
214 (GstMapFlags) (GST_MAP_WRITE | GST_MAP_GL));
217 this->priv->buffer = NULL;
218 GST_ERROR ("Failed to map video frame");
222 gl->BindFramebuffer (GL_READ_FRAMEBUFFER, this->source->renderTargetId());
224 ret = gst_gl_context_check_framebuffer_status (context, GL_READ_FRAMEBUFFER);
226 GST_ERROR ("FBO errors");
230 dst_tex = *(guint *) gl_frame.data[0];
231 GST_DEBUG ("qml render target id %d, render to tex %d %dX%d",
232 this->source->renderTargetId(), dst_tex, width,height);
234 gl->BindTexture (GL_TEXTURE_2D, dst_tex);
235 if (gl->BlitFramebuffer) {
236 gl->BindFramebuffer (GL_DRAW_FRAMEBUFFER, this->priv->fbo);
237 gl->FramebufferTexture2D (GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
238 GL_TEXTURE_2D, dst_tex, 0);
240 ret = gst_gl_context_check_framebuffer_status (context, GL_DRAW_FRAMEBUFFER);
242 GST_ERROR ("FBO errors");
245 gl->ReadBuffer (GL_COLOR_ATTACHMENT0);
246 gl->BlitFramebuffer (0, 0, width, height,
248 GL_COLOR_BUFFER_BIT, GL_LINEAR);
250 gl->CopyTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, width, height, 0);
253 GST_DEBUG ("rendering finished");
256 gl->BindFramebuffer (GL_FRAMEBUFFER, 0);
257 gst_video_frame_unmap (&gl_frame);
259 gst_gl_context_activate (context, FALSE);
261 this->priv->result = ret;
262 this->priv->updated = TRUE;
263 g_cond_signal (&this->priv->update_cond);
264 g_mutex_unlock (&this->priv->lock);
268 QtGLWindow::aboutToQuit()
270 g_mutex_lock (&this->priv->lock);
272 this->priv->updated = TRUE;
273 this->priv->quit = TRUE;
274 g_cond_signal (&this->priv->update_cond);
276 this->priv->stop = QDateTime::currentDateTime().toMSecsSinceEpoch();
277 qint64 duration = this->priv->stop - this->priv->start;
278 float fps = ((float)this->priv->frames_rendered / duration * 1000);
280 GST_DEBUG("about to quit, total refresh frames (%lld) in (%0.3f) seconds, fps: %0.3f",
281 this->priv->frames_rendered, (float)duration / 1000, fps);
283 g_mutex_unlock (&this->priv->lock);
287 QtGLWindow::onSceneGraphInitialized()
289 GST_DEBUG ("scene graph initialization with Qt GL context %p",
290 this->source->openglContext ());
292 this->priv->initted = gst_qt_get_gl_wrapcontext (this->priv->display,
293 &this->priv->other_context, NULL);
295 if (this->priv->initted && this->priv->other_context) {
296 const GstGLFuncs *gl;
298 gst_gl_context_activate (this->priv->other_context, TRUE);
299 gl = this->priv->other_context->gl_vtable;
301 gl->GenFramebuffers (1, &this->priv->fbo);
303 gst_gl_context_activate (this->priv->other_context, FALSE);
306 GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
307 this->priv->other_context);
311 QtGLWindow::onSceneGraphInvalidated()
313 GST_DEBUG ("scene graph invalidated");
315 if (this->priv->fbo && this->priv->other_context) {
316 const GstGLFuncs *gl;
318 gst_gl_context_activate (this->priv->other_context, TRUE);
319 gl = this->priv->other_context->gl_vtable;
321 gl->DeleteFramebuffers (1, &this->priv->fbo);
323 gst_gl_context_activate (this->priv->other_context, FALSE);
328 QtGLWindow::getGeometry(int * width, int * height)
330 if (width == NULL || height == NULL)
333 *width = this->source->width();
334 *height = this->source->height();
340 qt_window_get_qt_context (QtGLWindow * qt_window)
342 g_return_val_if_fail (qt_window != NULL, NULL);
344 if (!qt_window->priv->other_context)
347 return (GstGLContext *) gst_object_ref (qt_window->priv->other_context);
351 qt_window_get_display (QtGLWindow * qt_window)
353 g_return_val_if_fail (qt_window != NULL, NULL);
355 if (!qt_window->priv->display)
358 return (GstGLDisplay *) gst_object_ref (qt_window->priv->display);
362 qt_window_is_scenegraph_initialized (QtGLWindow * qt_window)
364 g_return_val_if_fail (qt_window != NULL, FALSE);
366 return qt_window->priv->initted;
370 qt_window_set_caps (QtGLWindow * qt_window, GstCaps * caps)
374 g_return_val_if_fail (qt_window != NULL, FALSE);
375 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
376 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
378 if (qt_window->priv->caps && gst_caps_is_equal_fixed (qt_window->priv->caps, caps))
381 if (!gst_video_info_from_caps (&v_info, caps))
384 g_mutex_lock (&qt_window->priv->lock);
386 gst_caps_replace (&qt_window->priv->caps, caps);
388 qt_window->priv->v_info = v_info;
390 g_mutex_unlock (&qt_window->priv->lock);
396 qt_window_set_buffer (QtGLWindow * qt_window, GstBuffer * buffer)
398 g_return_val_if_fail (qt_window != NULL, FALSE);
399 g_return_val_if_fail (qt_window->priv->initted, FALSE);
402 g_mutex_lock (&qt_window->priv->lock);
404 if (qt_window->priv->quit){
405 GST_DEBUG("about to quit, drop this buffer");
406 g_mutex_unlock (&qt_window->priv->lock);
410 qt_window->priv->updated = FALSE;
411 qt_window->priv->buffer = buffer;
413 while (!qt_window->priv->updated)
414 g_cond_wait (&qt_window->priv->update_cond, &qt_window->priv->lock);
416 ret = qt_window->priv->result;
418 g_mutex_unlock (&qt_window->priv->lock);
424 qt_window_use_default_fbo (QtGLWindow * qt_window, gboolean useDefaultFbo)
426 g_return_if_fail (qt_window != NULL);
428 g_mutex_lock (&qt_window->priv->lock);
430 GST_DEBUG ("set to use default fbo %d", useDefaultFbo);
431 qt_window->priv->useDefaultFbo = useDefaultFbo;
433 g_mutex_unlock (&qt_window->priv->lock);