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/QRunnable>
33 #include <QtGui/QGuiApplication>
34 #include <QtQuick/QQuickWindow>
35 #include <QOpenGLFramebufferObject>
37 /* compatability definitions... */
38 #ifndef GL_READ_FRAMEBUFFER
39 #define GL_READ_FRAMEBUFFER 0x8CA8
45 * #QtGLWindow is an #QQuickWindow that grab QtQuick view to GStreamer OpenGL video buffers.
48 GST_DEBUG_CATEGORY_STATIC (qt_window_debug);
49 #define GST_CAT_DEFAULT qt_window_debug
51 struct _QtGLWindowPrivate
64 gboolean useDefaultFbo;
66 GstGLDisplay *display;
67 GstGLContext *other_context;
69 /* frames that qmlview rendered in its gl thread */
70 guint64 frames_rendered;
73 class InitQtGLContext : public QRunnable
76 InitQtGLContext(QtGLWindow *window);
83 InitQtGLContext::InitQtGLContext(QtGLWindow *window) :
88 void InitQtGLContext::run()
90 window_->onSceneGraphInitialized();
93 QtGLWindow::QtGLWindow ( QWindow * parent, QQuickWindow *src ) :
94 QQuickWindow( parent ), source (src)
96 QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
97 static volatile gsize _debug;
99 g_assert (app != NULL);
101 if (g_once_init_enter (&_debug)) {
102 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwindow", 0, "Qt GL QuickWindow");
103 g_once_init_leave (&_debug, 1);
106 this->priv = g_new0 (QtGLWindowPrivate, 1);
108 g_mutex_init (&this->priv->lock);
109 g_cond_init (&this->priv->update_cond);
111 this->priv->display = gst_qt_get_gl_display();
113 connect (source, SIGNAL(beforeRendering()), this, SLOT(beforeRendering()), Qt::DirectConnection);
114 connect (source, SIGNAL(afterRendering()), this, SLOT(afterRendering()), Qt::DirectConnection);
115 connect (app, SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()), Qt::DirectConnection);
116 if (source->isSceneGraphInitialized())
117 source->scheduleRenderJob(new InitQtGLContext(this), QQuickWindow::BeforeSynchronizingStage);
119 connect (source, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
121 GST_DEBUG ("%p init Qt Window", this->priv->display);
124 QtGLWindow::~QtGLWindow()
126 GST_DEBUG ("deinit Qt Window");
127 g_mutex_clear (&this->priv->lock);
128 g_cond_clear (&this->priv->update_cond);
129 if (this->priv->other_context)
130 gst_object_unref(this->priv->other_context);
131 if (this->priv->display)
132 gst_object_unref(this->priv->display);
138 QtGLWindow::beforeRendering()
140 unsigned int width, height;
142 g_mutex_lock (&this->priv->lock);
144 if (!fbo && !this->priv->useDefaultFbo) {
146 width = source->width();
147 height = source->height();
149 GST_DEBUG ("create new framebuffer object %dX%d", width, height);
151 fbo.reset(new QOpenGLFramebufferObject (width, height,
152 QOpenGLFramebufferObject::NoAttachment, GL_TEXTURE_2D, GL_RGBA));
154 source->setRenderTarget(fbo.data());
157 g_mutex_unlock (&this->priv->lock);
162 QtGLWindow::afterRendering()
164 GstVideoFrame gl_frame;
166 GstGLContext *context;
169 const GstGLFuncs *gl;
172 g_mutex_lock (&this->priv->lock);
174 this->priv->frames_rendered++;
176 if(!this->priv->buffer || this->priv->updated == TRUE) {
177 GST_DEBUG ("skip this frame");
178 g_mutex_unlock (&this->priv->lock);
182 GST_DEBUG ("copy buffer %p",this->priv->buffer);
184 width = GST_VIDEO_INFO_WIDTH (&this->priv->v_info);
185 height = GST_VIDEO_INFO_HEIGHT (&this->priv->v_info);
186 info = &this->priv->v_info;
187 context = this->priv->other_context;
189 gst_gl_context_activate (context, TRUE);
190 gl = context->gl_vtable;
192 ret = gst_video_frame_map (&gl_frame, info, this->priv->buffer,
193 (GstMapFlags) (GST_MAP_WRITE | GST_MAP_GL));
196 this->priv->buffer = NULL;
197 GST_ERROR ("Failed to map video frame");
201 gl->BindFramebuffer (GL_READ_FRAMEBUFFER, this->source->renderTargetId());
203 ret = gst_gl_context_check_framebuffer_status (context);
205 GST_ERROR ("FBO errors");
209 dst_tex = *(guint *) gl_frame.data[0];
210 GST_DEBUG ("qml render target id %d, render to tex %d %dX%d",
211 this->source->renderTargetId(), dst_tex, width,height);
213 gl->BindTexture (GL_TEXTURE_2D, dst_tex);
214 gl->CopyTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, width, height, 0);
216 GST_DEBUG ("rendering finished");
219 gl->BindFramebuffer (GL_FRAMEBUFFER, 0);
220 gst_video_frame_unmap (&gl_frame);
222 gst_gl_context_activate (context, FALSE);
224 this->priv->result = ret;
225 this->priv->updated = TRUE;
226 g_cond_signal (&this->priv->update_cond);
227 g_mutex_unlock (&this->priv->lock);
231 QtGLWindow::aboutToQuit()
233 g_mutex_lock (&this->priv->lock);
235 this->priv->updated = TRUE;
236 this->priv->quit = TRUE;
237 g_cond_signal (&this->priv->update_cond);
239 g_mutex_unlock (&this->priv->lock);
241 GST_DEBUG("about to quit");
245 QtGLWindow::onSceneGraphInitialized()
247 GST_DEBUG ("scene graph initialization with Qt GL context %p",
248 this->source->openglContext ());
250 this->priv->initted = gst_qt_get_gl_wrapcontext (this->priv->display,
251 &this->priv->other_context, NULL);
253 GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
254 this->priv->other_context);
258 QtGLWindow::getGeometry(int * width, int * height)
260 if (width == NULL || height == NULL)
263 *width = this->source->width();
264 *height = this->source->height();
270 qt_window_get_qt_context (QtGLWindow * qt_window)
272 g_return_val_if_fail (qt_window != NULL, NULL);
274 if (!qt_window->priv->other_context)
277 return (GstGLContext *) gst_object_ref (qt_window->priv->other_context);
281 qt_window_get_display (QtGLWindow * qt_window)
283 g_return_val_if_fail (qt_window != NULL, NULL);
285 if (!qt_window->priv->display)
288 return (GstGLDisplay *) gst_object_ref (qt_window->priv->display);
292 qt_window_is_scenegraph_initialized (QtGLWindow * qt_window)
294 g_return_val_if_fail (qt_window != NULL, FALSE);
296 return qt_window->priv->initted;
300 qt_window_set_caps (QtGLWindow * qt_window, GstCaps * caps)
304 g_return_val_if_fail (qt_window != NULL, FALSE);
305 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
306 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
308 if (qt_window->priv->caps && gst_caps_is_equal_fixed (qt_window->priv->caps, caps))
311 if (!gst_video_info_from_caps (&v_info, caps))
314 g_mutex_lock (&qt_window->priv->lock);
316 gst_caps_replace (&qt_window->priv->caps, caps);
318 qt_window->priv->v_info = v_info;
320 g_mutex_unlock (&qt_window->priv->lock);
326 qt_window_set_buffer (QtGLWindow * qt_window, GstBuffer * buffer)
328 g_return_val_if_fail (qt_window != NULL, FALSE);
329 g_return_val_if_fail (qt_window->priv->initted, FALSE);
332 g_mutex_lock (&qt_window->priv->lock);
334 if (qt_window->priv->quit){
335 GST_DEBUG("about to quit, drop this buffer");
336 g_mutex_unlock (&qt_window->priv->lock);
340 qt_window->priv->updated = FALSE;
341 qt_window->priv->buffer = buffer;
343 while (!qt_window->priv->updated)
344 g_cond_wait (&qt_window->priv->update_cond, &qt_window->priv->lock);
346 ret = qt_window->priv->result;
348 g_mutex_unlock (&qt_window->priv->lock);
354 qt_window_use_default_fbo (QtGLWindow * qt_window, gboolean useDefaultFbo)
356 g_return_if_fail (qt_window != NULL);
358 g_mutex_lock (&qt_window->priv->lock);
360 GST_DEBUG ("set to use default fbo %d", useDefaultFbo);
361 qt_window->priv->useDefaultFbo = useDefaultFbo;
363 g_mutex_unlock (&qt_window->priv->lock);
367 qt_window_get_total_frames (QtGLWindow * qt_window, guint64 *frames)
369 g_return_if_fail (qt_window != NULL);
371 g_mutex_lock (&qt_window->priv->lock);
373 *frames = qt_window->priv->frames_rendered;
375 g_mutex_unlock (&qt_window->priv->lock);