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 <QtCore/QRunnable>
35 #include <QtGui/QGuiApplication>
36 #include <QtQuick/QQuickWindow>
37 #include <QOpenGLFramebufferObject>
38 
39 /* compatability definitions... */
40 #ifndef GL_READ_FRAMEBUFFER
41 #define GL_READ_FRAMEBUFFER 0x8CA8
42 #endif
43 #ifndef GL_DRAW_FRAMEBUFFER
44 #define GL_DRAW_FRAMEBUFFER 0x8CA9
45 #endif
46 
47 /**
48  * SECTION:
49  *
50  * #QtGLWindow is an #QQuickWindow that grab QtQuick view to GStreamer OpenGL video buffers.
51  */
52 
53 GST_DEBUG_CATEGORY_STATIC (qt_window_debug);
54 #define GST_CAT_DEFAULT qt_window_debug
55 
56 struct _QtGLWindowPrivate
57 {
58   GMutex lock;
59   GCond update_cond;
60 
61   GstBuffer *buffer;
62   GstCaps *caps;
63   GstVideoInfo v_info;
64 
65   gboolean initted;
66   gboolean updated;
67   gboolean quit;
68   gboolean result;
69   gboolean useDefaultFbo;
70 
71   GstGLDisplay *display;
72   GstGLContext *other_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 
82 class InitQtGLContext : public QRunnable
83 {
84 public:
85   InitQtGLContext(QtGLWindow *window);
86   void run();
87 
88 private:
89   QtGLWindow *window_;
90 };
91 
InitQtGLContext(QtGLWindow * window)92 InitQtGLContext::InitQtGLContext(QtGLWindow *window) :
93   window_(window)
94 {
95 }
96 
run()97 void InitQtGLContext::run()
98 {
99   window_->onSceneGraphInitialized();
100 }
101 
QtGLWindow(QWindow * parent,QQuickWindow * src)102 QtGLWindow::QtGLWindow ( QWindow * parent, QQuickWindow *src ) :
103   QQuickWindow( parent ), source (src)
104 {
105   QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
106   static volatile gsize _debug;
107 
108   g_assert (app != NULL);
109 
110   if (g_once_init_enter ((unsigned long *)&_debug)) {
111     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwindow", 0, "Qt GL QuickWindow");
112     g_once_init_leave (&_debug, 1);
113   }
114 
115   this->priv = g_new0 (QtGLWindowPrivate, 1);
116 
117   g_mutex_init (&this->priv->lock);
118   g_cond_init (&this->priv->update_cond);
119 
120   this->priv->display = gst_qt_get_gl_display();
121 
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);
127   else
128     connect (source, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
129 
130   connect (source, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
131 
132   GST_DEBUG ("%p init Qt Window", this->priv->display);
133 }
134 
~QtGLWindow()135 QtGLWindow::~QtGLWindow()
136 {
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);
144   g_free (this->priv);
145   this->priv = NULL;
146 }
147 
148 void
beforeRendering()149 QtGLWindow::beforeRendering()
150 {
151   unsigned int width, height;
152 
153   g_mutex_lock (&this->priv->lock);
154 
155   static volatile gsize once = 0;
156   if (g_once_init_enter((unsigned long *)&once)) {
157     this->priv->start = QDateTime::currentDateTime().toMSecsSinceEpoch();
158     g_once_init_leave(&once,1);
159   }
160 
161   if (!fbo && !this->priv->useDefaultFbo) {
162 
163     width = source->width();
164     height = source->height();
165 
166     GST_DEBUG ("create new framebuffer object %dX%d", width, height);
167 
168     fbo.reset(new QOpenGLFramebufferObject (width, height,
169           QOpenGLFramebufferObject::NoAttachment, GL_TEXTURE_2D, GL_RGBA));
170 
171     source->setRenderTarget(fbo.data());
172   } else if (this->priv->useDefaultFbo) {
173     GST_DEBUG ("use default fbo for render target");
174     fbo.reset(NULL);
175     source->setRenderTarget(NULL);
176   }
177 
178   g_mutex_unlock (&this->priv->lock);
179 }
180 
181 
182 void
afterRendering()183 QtGLWindow::afterRendering()
184 {
185   GstVideoFrame gl_frame;
186   GstVideoInfo *info;
187   GstGLContext *context;
188   gboolean ret;
189   guint width, height;
190   const GstGLFuncs *gl;
191   GLuint dst_tex;
192 
193   g_mutex_lock (&this->priv->lock);
194 
195   this->priv->frames_rendered++;
196 
197   if(!this->priv->buffer || this->priv->updated == TRUE) {
198     GST_DEBUG ("skip this frame");
199     g_mutex_unlock (&this->priv->lock);
200     return;
201   }
202 
203   GST_DEBUG ("copy buffer %p",this->priv->buffer);
204 
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;
209 
210   gst_gl_context_activate (context, TRUE);
211   gl = context->gl_vtable;
212 
213   ret = gst_video_frame_map (&gl_frame, info, this->priv->buffer,
214       (GstMapFlags) (GST_MAP_WRITE | GST_MAP_GL));
215 
216   if (!ret) {
217     this->priv->buffer = NULL;
218     GST_ERROR ("Failed to map video frame");
219     goto errors;
220   }
221 
222   gl->BindFramebuffer (GL_READ_FRAMEBUFFER, this->source->renderTargetId());
223 
224   ret = gst_gl_context_check_framebuffer_status (context, GL_READ_FRAMEBUFFER);
225   if (!ret) {
226     GST_ERROR ("FBO errors");
227     goto errors;
228   }
229 
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);
233 
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);
239 
240     ret = gst_gl_context_check_framebuffer_status (context, GL_DRAW_FRAMEBUFFER);
241     if (!ret) {
242       GST_ERROR ("FBO errors");
243       goto errors;
244     }
245     gl->ReadBuffer (GL_COLOR_ATTACHMENT0);
246     gl->BlitFramebuffer (0, 0, width, height,
247         0, 0, width, height,
248         GL_COLOR_BUFFER_BIT, GL_LINEAR);
249   } else {
250     gl->CopyTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, width, height, 0);
251   }
252 
253   GST_DEBUG ("rendering finished");
254 
255 errors:
256   gl->BindFramebuffer (GL_FRAMEBUFFER, 0);
257   gst_video_frame_unmap (&gl_frame);
258 
259   gst_gl_context_activate (context, FALSE);
260 
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);
265 }
266 
267 void
aboutToQuit()268 QtGLWindow::aboutToQuit()
269 {
270   g_mutex_lock (&this->priv->lock);
271 
272   this->priv->updated = TRUE;
273   this->priv->quit = TRUE;
274   g_cond_signal (&this->priv->update_cond);
275 
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);
279 
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);
282 
283   g_mutex_unlock (&this->priv->lock);
284 }
285 
286 void
onSceneGraphInitialized()287 QtGLWindow::onSceneGraphInitialized()
288 {
289   GST_DEBUG ("scene graph initialization with Qt GL context %p",
290       this->source->openglContext ());
291 
292   this->priv->initted = gst_qt_get_gl_wrapcontext (this->priv->display,
293       &this->priv->other_context, NULL);
294 
295   if (this->priv->initted && this->priv->other_context) {
296     const GstGLFuncs *gl;
297 
298     gst_gl_context_activate (this->priv->other_context, TRUE);
299     gl = this->priv->other_context->gl_vtable;
300 
301     gl->GenFramebuffers (1, &this->priv->fbo);
302 
303     gst_gl_context_activate (this->priv->other_context, FALSE);
304   }
305 
306   GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
307       this->priv->other_context);
308 }
309 
310 void
onSceneGraphInvalidated()311 QtGLWindow::onSceneGraphInvalidated()
312 {
313   GST_DEBUG ("scene graph invalidated");
314 
315   if (this->priv->fbo && this->priv->other_context) {
316     const GstGLFuncs *gl;
317 
318     gst_gl_context_activate (this->priv->other_context, TRUE);
319     gl = this->priv->other_context->gl_vtable;
320 
321     gl->DeleteFramebuffers (1, &this->priv->fbo);
322 
323     gst_gl_context_activate (this->priv->other_context, FALSE);
324   }
325 }
326 
327 bool
getGeometry(int * width,int * height)328 QtGLWindow::getGeometry(int * width, int * height)
329 {
330   if (width == NULL || height == NULL)
331     return FALSE;
332 
333   *width = this->source->width();
334   *height = this->source->height();
335 
336   return TRUE;
337 }
338 
339 GstGLContext *
qt_window_get_qt_context(QtGLWindow * qt_window)340 qt_window_get_qt_context (QtGLWindow * qt_window)
341 {
342   g_return_val_if_fail (qt_window != NULL, NULL);
343 
344   if (!qt_window->priv->other_context)
345     return NULL;
346 
347   return (GstGLContext *) gst_object_ref (qt_window->priv->other_context);
348 }
349 
350 GstGLDisplay *
qt_window_get_display(QtGLWindow * qt_window)351 qt_window_get_display (QtGLWindow * qt_window)
352 {
353   g_return_val_if_fail (qt_window != NULL, NULL);
354 
355   if (!qt_window->priv->display)
356     return NULL;
357 
358   return (GstGLDisplay *) gst_object_ref (qt_window->priv->display);
359 }
360 
361 gboolean
qt_window_is_scenegraph_initialized(QtGLWindow * qt_window)362 qt_window_is_scenegraph_initialized (QtGLWindow * qt_window)
363 {
364   g_return_val_if_fail (qt_window != NULL, FALSE);
365 
366   return qt_window->priv->initted;
367 }
368 
369 gboolean
qt_window_set_caps(QtGLWindow * qt_window,GstCaps * caps)370 qt_window_set_caps (QtGLWindow * qt_window, GstCaps * caps)
371 {
372   GstVideoInfo v_info;
373 
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);
377 
378   if (qt_window->priv->caps && gst_caps_is_equal_fixed (qt_window->priv->caps, caps))
379     return TRUE;
380 
381   if (!gst_video_info_from_caps (&v_info, caps))
382     return FALSE;
383 
384   g_mutex_lock (&qt_window->priv->lock);
385 
386   gst_caps_replace (&qt_window->priv->caps, caps);
387 
388   qt_window->priv->v_info = v_info;
389 
390   g_mutex_unlock (&qt_window->priv->lock);
391 
392   return TRUE;
393 }
394 
395 gboolean
qt_window_set_buffer(QtGLWindow * qt_window,GstBuffer * buffer)396 qt_window_set_buffer (QtGLWindow * qt_window, GstBuffer * buffer)
397 {
398   g_return_val_if_fail (qt_window != NULL, FALSE);
399   g_return_val_if_fail (qt_window->priv->initted, FALSE);
400   gboolean ret;
401 
402   g_mutex_lock (&qt_window->priv->lock);
403 
404   if (qt_window->priv->quit){
405     GST_DEBUG("about to quit, drop this buffer");
406     g_mutex_unlock (&qt_window->priv->lock);
407     return TRUE;
408   }
409 
410   qt_window->priv->updated = FALSE;
411   qt_window->priv->buffer = buffer;
412 
413   while (!qt_window->priv->updated)
414     g_cond_wait (&qt_window->priv->update_cond, &qt_window->priv->lock);
415 
416   ret = qt_window->priv->result;
417 
418   g_mutex_unlock (&qt_window->priv->lock);
419 
420   return ret;
421 }
422 
423 void
qt_window_use_default_fbo(QtGLWindow * qt_window,gboolean useDefaultFbo)424 qt_window_use_default_fbo (QtGLWindow * qt_window, gboolean useDefaultFbo)
425 {
426   g_return_if_fail (qt_window != NULL);
427 
428   g_mutex_lock (&qt_window->priv->lock);
429 
430   GST_DEBUG ("set to use default fbo %d", useDefaultFbo);
431   qt_window->priv->useDefaultFbo = useDefaultFbo;
432 
433   g_mutex_unlock (&qt_window->priv->lock);
434 }
435