qmlglsrc: some enhancements for qmlglsrc
[platform/upstream/gstreamer.git] / ext / qt / qtwindow.cc
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 "qtwindow.h"
29 #include "gstqsgtexture.h"
30 #include "gstqtglutility.h"
31
32 #include <QtCore/QDateTime>
33 #include <QtCore/QRunnable>
34 #include <QtGui/QGuiApplication>
35 #include <QtQuick/QQuickWindow>
36 #include <QOpenGLFramebufferObject>
37
38 /* compatability definitions... */
39 #ifndef GL_READ_FRAMEBUFFER
40 #define GL_READ_FRAMEBUFFER 0x8CA8
41 #endif
42
43 /**
44  * SECTION:
45  *
46  * #QtGLWindow is an #QQuickWindow that grab QtQuick view to GStreamer OpenGL video buffers.
47  */
48
49 GST_DEBUG_CATEGORY_STATIC (qt_window_debug);
50 #define GST_CAT_DEFAULT qt_window_debug
51
52 struct _QtGLWindowPrivate
53 {
54   GMutex lock;
55   GCond update_cond;
56
57   GstBuffer *buffer;
58   GstCaps *caps;
59   GstVideoInfo v_info;
60
61   gboolean initted;
62   gboolean updated;
63   gboolean quit;
64   gboolean result;
65   gboolean useDefaultFbo;
66
67   GstGLDisplay *display;
68   GstGLContext *other_context;
69
70   /* frames that qmlview rendered in its gl thread */
71   quint64 frames_rendered;
72   quint64 start;
73   quint64 stop;
74 };
75
76 class InitQtGLContext : public QRunnable
77 {
78 public:
79   InitQtGLContext(QtGLWindow *window);
80   void run();
81
82 private:
83   QtGLWindow *window_;
84 };
85
86 InitQtGLContext::InitQtGLContext(QtGLWindow *window) :
87   window_(window)
88 {
89 }
90
91 void InitQtGLContext::run()
92 {
93   window_->onSceneGraphInitialized();
94 }
95
96 QtGLWindow::QtGLWindow ( QWindow * parent, QQuickWindow *src ) :
97   QQuickWindow( parent ), source (src)
98 {
99   QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
100   static volatile gsize _debug;
101
102   g_assert (app != NULL);
103
104   if (g_once_init_enter (&_debug)) {
105     GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtglwindow", 0, "Qt GL QuickWindow");
106     g_once_init_leave (&_debug, 1);
107   }
108
109   this->priv = g_new0 (QtGLWindowPrivate, 1);
110
111   g_mutex_init (&this->priv->lock);
112   g_cond_init (&this->priv->update_cond);
113
114   this->priv->display = gst_qt_get_gl_display();
115
116   connect (source, SIGNAL(beforeRendering()), this, SLOT(beforeRendering()), Qt::DirectConnection);
117   connect (source, SIGNAL(afterRendering()), this, SLOT(afterRendering()), Qt::DirectConnection);
118   connect (app, SIGNAL(aboutToQuit()), this, SLOT(aboutToQuit()), Qt::DirectConnection);
119   if (source->isSceneGraphInitialized())
120     source->scheduleRenderJob(new InitQtGLContext(this), QQuickWindow::BeforeSynchronizingStage);
121   else
122     connect (source, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
123
124   GST_DEBUG ("%p init Qt Window", this->priv->display);
125 }
126
127 QtGLWindow::~QtGLWindow()
128 {
129   GST_DEBUG ("deinit Qt Window");
130   g_mutex_clear (&this->priv->lock);
131   g_cond_clear (&this->priv->update_cond);
132   if (this->priv->other_context)
133     gst_object_unref(this->priv->other_context);
134   if (this->priv->display)
135     gst_object_unref(this->priv->display);
136   g_free (this->priv);
137   this->priv = NULL;
138 }
139
140 void
141 QtGLWindow::beforeRendering()
142 {
143   unsigned int width, height;
144
145   g_mutex_lock (&this->priv->lock);
146
147   static volatile gsize once = 0;
148   if (g_once_init_enter(&once)) {
149     this->priv->start = QDateTime::currentDateTime().toMSecsSinceEpoch();
150     g_once_init_leave(&once,1);
151   }
152
153   if (!fbo && !this->priv->useDefaultFbo) {
154
155     width = source->width();
156     height = source->height();
157
158     GST_DEBUG ("create new framebuffer object %dX%d", width, height);
159
160     fbo.reset(new QOpenGLFramebufferObject (width, height,
161           QOpenGLFramebufferObject::NoAttachment, GL_TEXTURE_2D, GL_RGBA));
162
163     source->setRenderTarget(fbo.data());
164   } else if (this->priv->useDefaultFbo) {
165     GST_DEBUG ("use default fbo for render target");
166     fbo.reset(NULL);
167     source->setRenderTarget(NULL);
168   }
169
170   g_mutex_unlock (&this->priv->lock);
171 }
172
173
174 void
175 QtGLWindow::afterRendering()
176 {
177   GstVideoFrame gl_frame;
178   GstVideoInfo *info;
179   GstGLContext *context;
180   gboolean ret;
181   guint width, height;
182   const GstGLFuncs *gl;
183   GLuint dst_tex;
184
185   g_mutex_lock (&this->priv->lock);
186
187   this->priv->frames_rendered++;
188
189   if(!this->priv->buffer || this->priv->updated == TRUE) {
190     GST_DEBUG ("skip this frame");
191     g_mutex_unlock (&this->priv->lock);
192     return;
193   }
194
195   GST_DEBUG ("copy buffer %p",this->priv->buffer);
196
197   width = GST_VIDEO_INFO_WIDTH (&this->priv->v_info);
198   height = GST_VIDEO_INFO_HEIGHT (&this->priv->v_info);
199   info = &this->priv->v_info;
200   context = this->priv->other_context;
201
202   gst_gl_context_activate (context, TRUE);
203   gl = context->gl_vtable;
204
205   ret = gst_video_frame_map (&gl_frame, info, this->priv->buffer,
206       (GstMapFlags) (GST_MAP_WRITE | GST_MAP_GL));
207
208   if (!ret) {
209     this->priv->buffer = NULL;
210     GST_ERROR ("Failed to map video frame");
211     goto errors;
212   }
213
214   gl->BindFramebuffer (GL_READ_FRAMEBUFFER, this->source->renderTargetId());
215
216   ret = gst_gl_context_check_framebuffer_status (context);
217   if (!ret) {
218     GST_ERROR ("FBO errors");
219     goto errors;
220   }
221
222   dst_tex = *(guint *) gl_frame.data[0];
223   GST_DEBUG ("qml render target id %d, render to tex %d %dX%d", 
224       this->source->renderTargetId(), dst_tex, width,height);
225
226   gl->BindTexture (GL_TEXTURE_2D, dst_tex);
227   gl->CopyTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, width, height, 0);
228   
229   GST_DEBUG ("rendering finished");
230
231 errors:
232   gl->BindFramebuffer (GL_FRAMEBUFFER, 0);
233   gst_video_frame_unmap (&gl_frame);
234
235   gst_gl_context_activate (context, FALSE);
236
237   this->priv->result = ret;
238   this->priv->updated = TRUE;
239   g_cond_signal (&this->priv->update_cond);
240   g_mutex_unlock (&this->priv->lock);
241 }
242
243 void
244 QtGLWindow::aboutToQuit()
245 {
246   g_mutex_lock (&this->priv->lock);
247
248   this->priv->updated = TRUE;
249   this->priv->quit = TRUE;
250   g_cond_signal (&this->priv->update_cond);
251
252   this->priv->stop = QDateTime::currentDateTime().toMSecsSinceEpoch();
253   qint64 duration = this->priv->stop - this->priv->start;
254   float fps = ((float)this->priv->frames_rendered / duration * 1000);
255
256   GST_DEBUG("about to quit, total refresh frames (%lld) in (%0.3f) seconds, fps: %0.3f",
257       this->priv->frames_rendered, (float)duration / 1000, fps);
258
259   g_mutex_unlock (&this->priv->lock);
260 }
261
262 void
263 QtGLWindow::onSceneGraphInitialized()
264 {
265   GST_DEBUG ("scene graph initialization with Qt GL context %p",
266       this->source->openglContext ());
267
268   this->priv->initted = gst_qt_get_gl_wrapcontext (this->priv->display,
269       &this->priv->other_context, NULL);
270
271   GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
272       this->priv->other_context);
273 }
274
275 bool
276 QtGLWindow::getGeometry(int * width, int * height)
277 {
278   if (width == NULL || height == NULL)
279     return FALSE;
280
281   *width = this->source->width();
282   *height = this->source->height();
283
284   return TRUE;
285 }
286
287 GstGLContext *
288 qt_window_get_qt_context (QtGLWindow * qt_window)
289 {
290   g_return_val_if_fail (qt_window != NULL, NULL);
291
292   if (!qt_window->priv->other_context)
293     return NULL;
294
295   return (GstGLContext *) gst_object_ref (qt_window->priv->other_context);
296 }
297
298 GstGLDisplay *
299 qt_window_get_display (QtGLWindow * qt_window)
300 {
301   g_return_val_if_fail (qt_window != NULL, NULL);
302
303   if (!qt_window->priv->display)
304     return NULL;
305
306   return (GstGLDisplay *) gst_object_ref (qt_window->priv->display);
307 }
308
309 gboolean
310 qt_window_is_scenegraph_initialized (QtGLWindow * qt_window)
311 {
312   g_return_val_if_fail (qt_window != NULL, FALSE);
313
314   return qt_window->priv->initted;
315 }
316
317 gboolean
318 qt_window_set_caps (QtGLWindow * qt_window, GstCaps * caps)
319 {
320   GstVideoInfo v_info;
321
322   g_return_val_if_fail (qt_window != NULL, FALSE);
323   g_return_val_if_fail (GST_IS_CAPS (caps), FALSE);
324   g_return_val_if_fail (gst_caps_is_fixed (caps), FALSE);
325
326   if (qt_window->priv->caps && gst_caps_is_equal_fixed (qt_window->priv->caps, caps))
327     return TRUE;
328
329   if (!gst_video_info_from_caps (&v_info, caps))
330     return FALSE;
331
332   g_mutex_lock (&qt_window->priv->lock);
333
334   gst_caps_replace (&qt_window->priv->caps, caps);
335
336   qt_window->priv->v_info = v_info;
337
338   g_mutex_unlock (&qt_window->priv->lock);
339
340   return TRUE;
341 }
342
343 gboolean
344 qt_window_set_buffer (QtGLWindow * qt_window, GstBuffer * buffer)
345 {
346   g_return_val_if_fail (qt_window != NULL, FALSE);
347   g_return_val_if_fail (qt_window->priv->initted, FALSE);
348   gboolean ret;
349
350   g_mutex_lock (&qt_window->priv->lock);
351
352   if (qt_window->priv->quit){
353     GST_DEBUG("about to quit, drop this buffer");
354     g_mutex_unlock (&qt_window->priv->lock);
355     return TRUE;
356   }
357
358   qt_window->priv->updated = FALSE;
359   qt_window->priv->buffer = buffer;
360
361   while (!qt_window->priv->updated) 
362     g_cond_wait (&qt_window->priv->update_cond, &qt_window->priv->lock);
363   
364   ret = qt_window->priv->result;
365
366   g_mutex_unlock (&qt_window->priv->lock);
367
368   return ret;
369 }
370
371 void
372 qt_window_use_default_fbo (QtGLWindow * qt_window, gboolean useDefaultFbo)
373 {
374   g_return_if_fail (qt_window != NULL);
375
376   g_mutex_lock (&qt_window->priv->lock);
377
378   GST_DEBUG ("set to use default fbo %d", useDefaultFbo);
379   qt_window->priv->useDefaultFbo = useDefaultFbo;
380
381   g_mutex_unlock (&qt_window->priv->lock);
382 }