1 /*
2 * GStreamer
3 * Copyright (C) 2016 Freescale Semiconductor, Inc. All rights reserved.
4 *
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.
9 *
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.
14 *
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.
19 */
20
21 #ifdef HAVE_CONFIG_H
22 #include "config.h"
23 #endif
24
25 #include <stdio.h>
26
27 #include <gst/video/video.h>
28 #include <gst/gl/gstglfuncs.h>
29 #include "qtwindow.h"
30 #include "gstqsgtexture.h"
31 #include "gstqtglutility.h"
32
33 #include <QtCore/QDateTime>
34 #include <QtGui/QGuiApplication>
35 #include <QtQuick/QQuickWindow>
36 #include <QOpenGLFramebufferObject>
37
38 /* compatibility definitions... */
39 #ifndef GL_READ_FRAMEBUFFER
40 #define GL_READ_FRAMEBUFFER 0x8CA8
41 #endif
42 #ifndef GL_DRAW_FRAMEBUFFER
43 #define GL_DRAW_FRAMEBUFFER 0x8CA9
44 #endif
45
46 /**
47 * SECTION:
48 *
49 * #QtGLWindow is an #QQuickWindow that grab QtQuick view to GStreamer OpenGL video buffers.
50 */
51
52 GST_DEBUG_CATEGORY_STATIC (qt_window_debug);
53 #define GST_CAT_DEFAULT qt_window_debug
54
55 struct _QtGLWindowPrivate
56 {
57 GMutex lock;
58 GCond update_cond;
59
60 GstBuffer *buffer;
61 GstCaps *caps;
62 GstVideoInfo v_info;
63
64 gboolean initted;
65 gboolean updated;
66 gboolean quit;
67 gboolean result;
68 gboolean useDefaultFbo;
69
70 GstGLDisplay *display;
71 GstGLContext *other_context;
72 GstGLContext *context;
73
74 GLuint fbo;
75
76 /* frames that qmlview rendered in its gl thread */
77 quint64 frames_rendered;
78 quint64 start;
79 quint64 stop;
80 };
81
QtGLWindow(QWindow * parent,QQuickWindow * src)82 QtGLWindow::QtGLWindow ( QWindow * parent, QQuickWindow *src ) :
83 QQuickWindow( parent ), source (src)
84 {
85 QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
86 static gsize _debug;
87
88 g_assert (app != NULL);
89
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);
93 }
94
95 this->priv = g_new0 (QtGLWindowPrivate, 1);
96
97 g_mutex_init (&this->priv->lock);
98 g_cond_init (&this->priv->update_cond);
99
100 this->priv->display = gst_qt_get_gl_display(FALSE);
101
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);
107 else
108 connect (source, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
109
110 connect (source, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
111
112 GST_DEBUG ("%p init Qt Window", this->priv->display);
113 }
114
~QtGLWindow()115 QtGLWindow::~QtGLWindow()
116 {
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 g_free (this->priv);
127 this->priv = NULL;
128 }
129
130 void
beforeRendering()131 QtGLWindow::beforeRendering()
132 {
133 unsigned int width, height;
134
135 g_mutex_lock (&this->priv->lock);
136
137 static gsize once = 0;
138 if (g_once_init_enter(&once)) {
139 this->priv->start = QDateTime::currentDateTime().toMSecsSinceEpoch();
140 g_once_init_leave(&once,1);
141 }
142
143 if (!fbo && !this->priv->useDefaultFbo) {
144
145 width = source->width();
146 height = source->height();
147
148 GST_DEBUG ("create new framebuffer object %dX%d", width, height);
149
150 fbo.reset(new QOpenGLFramebufferObject (width, height,
151 QOpenGLFramebufferObject::NoAttachment, GL_TEXTURE_2D, GL_RGBA));
152
153 source->setRenderTarget(fbo.data());
154 } else if (this->priv->useDefaultFbo) {
155 GST_DEBUG ("use default fbo for render target");
156 fbo.reset(NULL);
157 source->setRenderTarget(NULL);
158 }
159
160 g_mutex_unlock (&this->priv->lock);
161 }
162
163
164 void
afterRendering()165 QtGLWindow::afterRendering()
166 {
167 GstVideoFrame gl_frame;
168 GstVideoInfo *info;
169 GstGLContext *context;
170 gboolean ret;
171 guint width, height;
172 const GstGLFuncs *gl;
173 GLuint dst_tex;
174 GstGLSyncMeta *sync_meta;
175
176 g_mutex_lock (&this->priv->lock);
177
178 this->priv->frames_rendered++;
179
180 if(!this->priv->buffer || this->priv->updated == TRUE) {
181 GST_DEBUG ("skip this frame");
182 g_mutex_unlock (&this->priv->lock);
183 return;
184 }
185
186 GST_DEBUG ("copy buffer %p",this->priv->buffer);
187
188 width = GST_VIDEO_INFO_WIDTH (&this->priv->v_info);
189 height = GST_VIDEO_INFO_HEIGHT (&this->priv->v_info);
190 info = &this->priv->v_info;
191 context = this->priv->other_context;
192
193 gst_gl_context_activate (context, TRUE);
194 gl = context->gl_vtable;
195
196 ret = gst_video_frame_map (&gl_frame, info, this->priv->buffer,
197 (GstMapFlags) (GST_MAP_WRITE | GST_MAP_GL));
198
199 if (!ret) {
200 this->priv->buffer = NULL;
201 GST_ERROR ("Failed to map video frame");
202 goto errors;
203 }
204
205 gl->BindFramebuffer (GL_READ_FRAMEBUFFER, this->source->renderTargetId());
206
207 ret = gst_gl_context_check_framebuffer_status (context, GL_READ_FRAMEBUFFER);
208 if (!ret) {
209 GST_ERROR ("FBO errors");
210 goto errors;
211 }
212
213 dst_tex = *(guint *) gl_frame.data[0];
214 GST_DEBUG ("qml render target id %d, render to tex %d %dX%d",
215 this->source->renderTargetId(), dst_tex, width,height);
216
217 gl->BindTexture (GL_TEXTURE_2D, dst_tex);
218 if (gl->BlitFramebuffer) {
219 gl->BindFramebuffer (GL_DRAW_FRAMEBUFFER, this->priv->fbo);
220 gl->FramebufferTexture2D (GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
221 GL_TEXTURE_2D, dst_tex, 0);
222
223 ret = gst_gl_context_check_framebuffer_status (context, GL_DRAW_FRAMEBUFFER);
224 if (!ret) {
225 GST_ERROR ("FBO errors");
226 goto errors;
227 }
228 if (this->priv->useDefaultFbo)
229 gl->ReadBuffer (GL_BACK);
230 else
231 gl->ReadBuffer (GL_COLOR_ATTACHMENT0);
232 gl->BlitFramebuffer (0, 0, width, height,
233 0, 0, width, height,
234 GL_COLOR_BUFFER_BIT, GL_LINEAR);
235 } else {
236 gl->CopyTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, width, height, 0);
237 }
238
239 if (this->priv->context) {
240 sync_meta = gst_buffer_get_gl_sync_meta (this->priv->buffer);
241 if (!sync_meta) {
242 sync_meta = gst_buffer_add_gl_sync_meta (this->priv->context, this->priv->buffer);
243 }
244 gst_gl_sync_meta_set_sync_point (sync_meta, context);
245 }
246
247 GST_DEBUG ("rendering finished");
248
249 errors:
250 gl->BindFramebuffer (GL_FRAMEBUFFER, 0);
251 gst_video_frame_unmap (&gl_frame);
252
253 gst_gl_context_activate (context, FALSE);
254
255 this->priv->result = ret;
256 this->priv->updated = TRUE;
257 g_cond_signal (&this->priv->update_cond);
258 g_mutex_unlock (&this->priv->lock);
259 }
260
261 void
aboutToQuit()262 QtGLWindow::aboutToQuit()
263 {
264 g_mutex_lock (&this->priv->lock);
265
266 this->priv->updated = TRUE;
267 this->priv->quit = TRUE;
268 g_cond_signal (&this->priv->update_cond);
269
270 this->priv->stop = QDateTime::currentDateTime().toMSecsSinceEpoch();
271 qint64 duration = this->priv->stop - this->priv->start;
272 float fps = ((float)this->priv->frames_rendered / duration * 1000);
273
274 GST_DEBUG("about to quit, total refresh frames (%lld) in (%0.3f) seconds, fps: %0.3f",
275 this->priv->frames_rendered, (float)duration / 1000, fps);
276
277 g_mutex_unlock (&this->priv->lock);
278 }
279
280 void
onSceneGraphInitialized()281 QtGLWindow::onSceneGraphInitialized()
282 {
283 GST_DEBUG ("scene graph initialization with Qt GL context %p",
284 this->source->openglContext ());
285
286 this->priv->initted = gst_qt_get_gl_wrapcontext (this->priv->display,
287 &this->priv->other_context, &this->priv->context);
288
289 if (this->priv->initted && this->priv->other_context) {
290 const GstGLFuncs *gl;
291
292 gst_gl_context_activate (this->priv->other_context, TRUE);
293 gl = this->priv->other_context->gl_vtable;
294
295 gl->GenFramebuffers (1, &this->priv->fbo);
296
297 gst_gl_context_activate (this->priv->other_context, FALSE);
298 }
299
300 GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
301 this->priv->other_context);
302 }
303
304 void
onSceneGraphInvalidated()305 QtGLWindow::onSceneGraphInvalidated()
306 {
307 GST_DEBUG ("scene graph invalidated");
308
309 if (this->priv->fbo && this->priv->other_context) {
310 const GstGLFuncs *gl;
311
312 gst_gl_context_activate (this->priv->other_context, TRUE);
313 gl = this->priv->other_context->gl_vtable;
314
315 gl->DeleteFramebuffers (1, &this->priv->fbo);
316
317 gst_gl_context_activate (this->priv->other_context, FALSE);
318 }
319 }
320
321 bool
getGeometry(int * width,int * height)322 QtGLWindow::getGeometry(int * width, int * height)
323 {
324 if (width == NULL || height == NULL)
325 return FALSE;
326
327 *width = this->source->width();
328 *height = this->source->height();
329
330 return TRUE;
331 }
332
333 GstGLContext *
qt_window_get_qt_context(QtGLWindow * qt_window)334 qt_window_get_qt_context (QtGLWindow * qt_window)
335 {
336 g_return_val_if_fail (qt_window != NULL, NULL);
337
338 if (!qt_window->priv->other_context)
339 return NULL;
340
341 return (GstGLContext *) gst_object_ref (qt_window->priv->other_context);
342 }
343
344 GstGLDisplay *
qt_window_get_display(QtGLWindow * qt_window)345 qt_window_get_display (QtGLWindow * qt_window)
346 {
347 g_return_val_if_fail (qt_window != NULL, NULL);
348
349 if (!qt_window->priv->display)
350 return NULL;
351
352 return (GstGLDisplay *) gst_object_ref (qt_window->priv->display);
353 }
354
355 GstGLContext *
qt_window_get_context(QtGLWindow * qt_window)356 qt_window_get_context (QtGLWindow * qt_window)
357 {
358 g_return_val_if_fail (qt_window != NULL, NULL);
359
360 if (!qt_window->priv->context)
361 return NULL;
362
363 return (GstGLContext *) gst_object_ref (qt_window->priv->context);
364 }
365
366 gboolean
qt_window_set_context(QtGLWindow * qt_window,GstGLContext * context)367 qt_window_set_context (QtGLWindow * qt_window, GstGLContext * context)
368 {
369 g_return_val_if_fail (qt_window != NULL, FALSE);
370
371 if (qt_window->priv->context && qt_window->priv->context != context)
372 return FALSE;
373
374 gst_object_replace ((GstObject **) &qt_window->priv->context, (GstObject *) context);
375
376 return TRUE;
377 }
378
379 gboolean
qt_window_is_scenegraph_initialized(QtGLWindow * qt_window)380 qt_window_is_scenegraph_initialized (QtGLWindow * qt_window)
381 {
382 g_return_val_if_fail (qt_window != NULL, FALSE);
383
384 return qt_window->priv->initted;
385 }
386
387 gboolean
qt_window_set_caps(QtGLWindow * qt_window,GstCaps * caps)388 qt_window_set_caps (QtGLWindow * qt_window, GstCaps * caps)
389 {
390 GstVideoInfo v_info;
391
392 g_return_val_if_fail (qt_window != NULL, FALSE);
393 g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
394 g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
395
396 if (qt_window->priv->caps && gst_caps_is_equal_fixed (qt_window->priv->caps, caps))
397 return TRUE;
398
399 if (!gst_video_info_from_caps (&v_info, caps))
400 return FALSE;
401
402 g_mutex_lock (&qt_window->priv->lock);
403
404 gst_caps_replace (&qt_window->priv->caps, caps);
405
406 qt_window->priv->v_info = v_info;
407
408 g_mutex_unlock (&qt_window->priv->lock);
409
410 return TRUE;
411 }
412
413 gboolean
qt_window_set_buffer(QtGLWindow * qt_window,GstBuffer * buffer)414 qt_window_set_buffer (QtGLWindow * qt_window, GstBuffer * buffer)
415 {
416 g_return_val_if_fail (qt_window != NULL, FALSE);
417 g_return_val_if_fail (qt_window->priv->initted, FALSE);
418 gboolean ret;
419
420 g_mutex_lock (&qt_window->priv->lock);
421
422 if (qt_window->priv->quit){
423 GST_DEBUG("about to quit, drop this buffer");
424 g_mutex_unlock (&qt_window->priv->lock);
425 return TRUE;
426 }
427
428 qt_window->priv->updated = FALSE;
429 qt_window->priv->buffer = buffer;
430
431 while (!qt_window->priv->updated)
432 g_cond_wait (&qt_window->priv->update_cond, &qt_window->priv->lock);
433
434 ret = qt_window->priv->result;
435
436 g_mutex_unlock (&qt_window->priv->lock);
437
438 return ret;
439 }
440
441 void
qt_window_use_default_fbo(QtGLWindow * qt_window,gboolean useDefaultFbo)442 qt_window_use_default_fbo (QtGLWindow * qt_window, gboolean useDefaultFbo)
443 {
444 g_return_if_fail (qt_window != NULL);
445
446 g_mutex_lock (&qt_window->priv->lock);
447
448 GST_DEBUG ("set to use default fbo %d", useDefaultFbo);
449 qt_window->priv->useDefaultFbo = useDefaultFbo;
450
451 g_mutex_unlock (&qt_window->priv->lock);
452 }
453