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;
72 /* frames that qmlview rendered in its gl thread */
73 quint64 frames_rendered;
78 class InitQtGLContext : public QRunnable
81 InitQtGLContext(QtGLWindow *window);
88 InitQtGLContext::InitQtGLContext(QtGLWindow *window) :
93 void InitQtGLContext::run()
95 window_->onSceneGraphInitialized();
98 QtGLWindow::QtGLWindow ( QWindow * parent, QQuickWindow *src ) :
99 QQuickWindow( parent ), source (src)
101 QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
102 static volatile gsize _debug;
104 g_assert (app != NULL);
106 if (g_once_init_enter (&_debug)) {
107 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwindow", 0, "Qt GL QuickWindow");
108 g_once_init_leave (&_debug, 1);
111 this->priv = g_new0 (QtGLWindowPrivate, 1);
113 g_mutex_init (&this->priv->lock);
114 g_cond_init (&this->priv->update_cond);
116 this->priv->display = gst_qt_get_gl_display();
118 connect (source, SIGNAL(beforeRendering()), this, SLOT(beforeRendering()), Qt::DirectConnection);
119 connect (source, SIGNAL(afterRendering()), this, SLOT(afterRendering()), Qt::DirectConnection);
120 connect (app, SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()), Qt::DirectConnection);
121 if (source->isSceneGraphInitialized())
122 source->scheduleRenderJob(new InitQtGLContext(this), QQuickWindow::BeforeSynchronizingStage);
124 connect (source, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
126 connect (source, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
128 GST_DEBUG ("%p init Qt Window", this->priv->display);
131 QtGLWindow::~QtGLWindow()
133 GST_DEBUG ("deinit Qt Window");
134 g_mutex_clear (&this->priv->lock);
135 g_cond_clear (&this->priv->update_cond);
136 if (this->priv->other_context)
137 gst_object_unref(this->priv->other_context);
138 if (this->priv->display)
139 gst_object_unref(this->priv->display);
145 QtGLWindow::beforeRendering()
147 unsigned int width, height;
149 g_mutex_lock (&this->priv->lock);
151 static volatile gsize once = 0;
152 if (g_once_init_enter(&once)) {
153 this->priv->start = QDateTime::currentDateTime().toMSecsSinceEpoch();
154 g_once_init_leave(&once,1);
157 if (!fbo && !this->priv->useDefaultFbo) {
159 width = source->width();
160 height = source->height();
162 GST_DEBUG ("create new framebuffer object %dX%d", width, height);
164 fbo.reset(new QOpenGLFramebufferObject (width, height,
165 QOpenGLFramebufferObject::NoAttachment, GL_TEXTURE_2D, GL_RGBA));
167 source->setRenderTarget(fbo.data());
168 } else if (this->priv->useDefaultFbo) {
169 GST_DEBUG ("use default fbo for render target");
171 source->setRenderTarget(NULL);
174 g_mutex_unlock (&this->priv->lock);
179 QtGLWindow::afterRendering()
181 GstVideoFrame gl_frame;
183 GstGLContext *context;
186 const GstGLFuncs *gl;
189 g_mutex_lock (&this->priv->lock);
191 this->priv->frames_rendered++;
193 if(!this->priv->buffer || this->priv->updated == TRUE) {
194 GST_DEBUG ("skip this frame");
195 g_mutex_unlock (&this->priv->lock);
199 GST_DEBUG ("copy buffer %p",this->priv->buffer);
201 width = GST_VIDEO_INFO_WIDTH (&this->priv->v_info);
202 height = GST_VIDEO_INFO_HEIGHT (&this->priv->v_info);
203 info = &this->priv->v_info;
204 context = this->priv->other_context;
206 gst_gl_context_activate (context, TRUE);
207 gl = context->gl_vtable;
209 ret = gst_video_frame_map (&gl_frame, info, this->priv->buffer,
210 (GstMapFlags) (GST_MAP_WRITE | GST_MAP_GL));
213 this->priv->buffer = NULL;
214 GST_ERROR ("Failed to map video frame");
218 gl->BindFramebuffer (GL_READ_FRAMEBUFFER, this->source->renderTargetId());
220 ret = gst_gl_context_check_framebuffer_status (context);
222 GST_ERROR ("FBO errors");
226 dst_tex = *(guint *) gl_frame.data[0];
227 GST_DEBUG ("qml render target id %d, render to tex %d %dX%d",
228 this->source->renderTargetId(), dst_tex, width,height);
230 gl->BindTexture (GL_TEXTURE_2D, dst_tex);
231 if (gl->BlitFramebuffer) {
232 gl->BindFramebuffer (GL_DRAW_FRAMEBUFFER, this->priv->fbo);
233 gl->FramebufferTexture2D (GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
234 GL_TEXTURE_2D, dst_tex, 0);
236 ret = gst_gl_context_check_framebuffer_status (context);
238 GST_ERROR ("FBO errors");
241 gl->ReadBuffer (GL_COLOR_ATTACHMENT0);
242 gl->BlitFramebuffer (0, 0, width, height,
244 GL_COLOR_BUFFER_BIT, GL_LINEAR);
246 gl->CopyTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, width, height, 0);
249 GST_DEBUG ("rendering finished");
252 gl->BindFramebuffer (GL_FRAMEBUFFER, 0);
253 gst_video_frame_unmap (&gl_frame);
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 QtGLWindow::aboutToQuit()
266 g_mutex_lock (&this->priv->lock);
268 this->priv->updated = TRUE;
269 this->priv->quit = TRUE;
270 g_cond_signal (&this->priv->update_cond);
272 this->priv->stop = QDateTime::currentDateTime().toMSecsSinceEpoch();
273 qint64 duration = this->priv->stop - this->priv->start;
274 float fps = ((float)this->priv->frames_rendered / duration * 1000);
276 GST_DEBUG("about to quit, total refresh frames (%lld) in (%0.3f) seconds, fps: %0.3f",
277 this->priv->frames_rendered, (float)duration / 1000, fps);
279 g_mutex_unlock (&this->priv->lock);
283 QtGLWindow::onSceneGraphInitialized()
285 GST_DEBUG ("scene graph initialization with Qt GL context %p",
286 this->source->openglContext ());
288 this->priv->initted = gst_qt_get_gl_wrapcontext (this->priv->display,
289 &this->priv->other_context, NULL);
291 if (this->priv->initted && this->priv->other_context) {
292 const GstGLFuncs *gl;
294 gst_gl_context_activate (this->priv->other_context, TRUE);
295 gl = this->priv->other_context->gl_vtable;
297 gl->GenFramebuffers (1, &this->priv->fbo);
299 gst_gl_context_activate (this->priv->other_context, FALSE);
302 GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
303 this->priv->other_context);
307 QtGLWindow::onSceneGraphInvalidated()
309 GST_DEBUG ("scene graph invalidated");
311 if (this->priv->fbo && this->priv->other_context) {
312 const GstGLFuncs *gl;
314 gst_gl_context_activate (this->priv->other_context, TRUE);
315 gl = this->priv->other_context->gl_vtable;
317 gl->DeleteFramebuffers (1, &this->priv->fbo);
319 gst_gl_context_activate (this->priv->other_context, FALSE);
324 QtGLWindow::getGeometry(int * width, int * height)
326 if (width == NULL || height == NULL)
329 *width = this->source->width();
330 *height = this->source->height();
336 qt_window_get_qt_context (QtGLWindow * qt_window)
338 g_return_val_if_fail (qt_window != NULL, NULL);
340 if (!qt_window->priv->other_context)
343 return (GstGLContext *) gst_object_ref (qt_window->priv->other_context);
347 qt_window_get_display (QtGLWindow * qt_window)
349 g_return_val_if_fail (qt_window != NULL, NULL);
351 if (!qt_window->priv->display)
354 return (GstGLDisplay *) gst_object_ref (qt_window->priv->display);
358 qt_window_is_scenegraph_initialized (QtGLWindow * qt_window)
360 g_return_val_if_fail (qt_window != NULL, FALSE);
362 return qt_window->priv->initted;
366 qt_window_set_caps (QtGLWindow * qt_window, GstCaps * caps)
370 g_return_val_if_fail (qt_window != NULL, FALSE);
371 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
372 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
374 if (qt_window->priv->caps && gst_caps_is_equal_fixed (qt_window->priv->caps, caps))
377 if (!gst_video_info_from_caps (&v_info, caps))
380 g_mutex_lock (&qt_window->priv->lock);
382 gst_caps_replace (&qt_window->priv->caps, caps);
384 qt_window->priv->v_info = v_info;
386 g_mutex_unlock (&qt_window->priv->lock);
392 qt_window_set_buffer (QtGLWindow * qt_window, GstBuffer * buffer)
394 g_return_val_if_fail (qt_window != NULL, FALSE);
395 g_return_val_if_fail (qt_window->priv->initted, FALSE);
398 g_mutex_lock (&qt_window->priv->lock);
400 if (qt_window->priv->quit){
401 GST_DEBUG("about to quit, drop this buffer");
402 g_mutex_unlock (&qt_window->priv->lock);
406 qt_window->priv->updated = FALSE;
407 qt_window->priv->buffer = buffer;
409 while (!qt_window->priv->updated)
410 g_cond_wait (&qt_window->priv->update_cond, &qt_window->priv->lock);
412 ret = qt_window->priv->result;
414 g_mutex_unlock (&qt_window->priv->lock);
420 qt_window_use_default_fbo (QtGLWindow * qt_window, gboolean useDefaultFbo)
422 g_return_if_fail (qt_window != NULL);
424 g_mutex_lock (&qt_window->priv->lock);
426 GST_DEBUG ("set to use default fbo %d", useDefaultFbo);
427 qt_window->priv->useDefaultFbo = useDefaultFbo;
429 g_mutex_unlock (&qt_window->priv->lock);