qmlglsrc: use glBlitFramebuffer to copy texture for GLES3.0
[platform/upstream/gst-plugins-good.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   GLuint fbo;
71
72   /* frames that qmlview rendered in its gl thread */
73   quint64 frames_rendered;
74   quint64 start;
75   quint64 stop;
76 };
77
78 class InitQtGLContext : public QRunnable
79 {
80 public:
81   InitQtGLContext(QtGLWindow *window);
82   void run();
83
84 private:
85   QtGLWindow *window_;
86 };
87
88 InitQtGLContext::InitQtGLContext(QtGLWindow *window) :
89   window_(window)
90 {
91 }
92
93 void InitQtGLContext::run()
94 {
95   window_->onSceneGraphInitialized();
96 }
97
98 QtGLWindow::QtGLWindow ( QWindow * parent, QQuickWindow *src ) :
99   QQuickWindow( parent ), source (src)
100 {
101   QGuiApplication *app = static_cast<QGuiApplication *> (QCoreApplication::instance ());
102   static volatile gsize _debug;
103
104   g_assert (app != NULL);
105
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);
109   }
110
111   this->priv = g_new0 (QtGLWindowPrivate, 1);
112
113   g_mutex_init (&this->priv->lock);
114   g_cond_init (&this->priv->update_cond);
115
116   this->priv->display = gst_qt_get_gl_display();
117
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);
123   else
124     connect (source, SIGNAL(sceneGraphInitialized()), this, SLOT(onSceneGraphInitialized()), Qt::DirectConnection);
125
126   connect (source, SIGNAL(sceneGraphInvalidated()), this, SLOT(onSceneGraphInvalidated()), Qt::DirectConnection);
127
128   GST_DEBUG ("%p init Qt Window", this->priv->display);
129 }
130
131 QtGLWindow::~QtGLWindow()
132 {
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);
140   g_free (this->priv);
141   this->priv = NULL;
142 }
143
144 void
145 QtGLWindow::beforeRendering()
146 {
147   unsigned int width, height;
148
149   g_mutex_lock (&this->priv->lock);
150
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);
155   }
156
157   if (!fbo && !this->priv->useDefaultFbo) {
158
159     width = source->width();
160     height = source->height();
161
162     GST_DEBUG ("create new framebuffer object %dX%d", width, height);
163
164     fbo.reset(new QOpenGLFramebufferObject (width, height,
165           QOpenGLFramebufferObject::NoAttachment, GL_TEXTURE_2D, GL_RGBA));
166
167     source->setRenderTarget(fbo.data());
168   } else if (this->priv->useDefaultFbo) {
169     GST_DEBUG ("use default fbo for render target");
170     fbo.reset(NULL);
171     source->setRenderTarget(NULL);
172   }
173
174   g_mutex_unlock (&this->priv->lock);
175 }
176
177
178 void
179 QtGLWindow::afterRendering()
180 {
181   GstVideoFrame gl_frame;
182   GstVideoInfo *info;
183   GstGLContext *context;
184   gboolean ret;
185   guint width, height;
186   const GstGLFuncs *gl;
187   GLuint dst_tex;
188
189   g_mutex_lock (&this->priv->lock);
190
191   this->priv->frames_rendered++;
192
193   if(!this->priv->buffer || this->priv->updated == TRUE) {
194     GST_DEBUG ("skip this frame");
195     g_mutex_unlock (&this->priv->lock);
196     return;
197   }
198
199   GST_DEBUG ("copy buffer %p",this->priv->buffer);
200
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;
205
206   gst_gl_context_activate (context, TRUE);
207   gl = context->gl_vtable;
208
209   ret = gst_video_frame_map (&gl_frame, info, this->priv->buffer,
210       (GstMapFlags) (GST_MAP_WRITE | GST_MAP_GL));
211
212   if (!ret) {
213     this->priv->buffer = NULL;
214     GST_ERROR ("Failed to map video frame");
215     goto errors;
216   }
217
218   gl->BindFramebuffer (GL_READ_FRAMEBUFFER, this->source->renderTargetId());
219
220   ret = gst_gl_context_check_framebuffer_status (context);
221   if (!ret) {
222     GST_ERROR ("FBO errors");
223     goto errors;
224   }
225
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);
229
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);
235
236     ret = gst_gl_context_check_framebuffer_status (context);
237     if (!ret) {
238       GST_ERROR ("FBO errors");
239       goto errors;
240     }
241     gl->ReadBuffer (GL_COLOR_ATTACHMENT0);
242     gl->BlitFramebuffer (0, 0, width, height,
243         0, 0, width, height,
244         GL_COLOR_BUFFER_BIT, GL_LINEAR);
245   } else {
246     gl->CopyTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, 0, 0, width, height, 0);
247   }
248   
249   GST_DEBUG ("rendering finished");
250
251 errors:
252   gl->BindFramebuffer (GL_FRAMEBUFFER, 0);
253   gst_video_frame_unmap (&gl_frame);
254
255   gst_gl_context_activate (context, FALSE);
256
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);
261 }
262
263 void
264 QtGLWindow::aboutToQuit()
265 {
266   g_mutex_lock (&this->priv->lock);
267
268   this->priv->updated = TRUE;
269   this->priv->quit = TRUE;
270   g_cond_signal (&this->priv->update_cond);
271
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);
275
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);
278
279   g_mutex_unlock (&this->priv->lock);
280 }
281
282 void
283 QtGLWindow::onSceneGraphInitialized()
284 {
285   GST_DEBUG ("scene graph initialization with Qt GL context %p",
286       this->source->openglContext ());
287
288   this->priv->initted = gst_qt_get_gl_wrapcontext (this->priv->display,
289       &this->priv->other_context, NULL);
290
291   if (this->priv->initted && this->priv->other_context) {
292     const GstGLFuncs *gl;
293
294     gst_gl_context_activate (this->priv->other_context, TRUE);
295     gl = this->priv->other_context->gl_vtable;
296
297     gl->GenFramebuffers (1, &this->priv->fbo);
298
299     gst_gl_context_activate (this->priv->other_context, FALSE);
300   }
301
302   GST_DEBUG ("%p created wrapped GL context %" GST_PTR_FORMAT, this,
303       this->priv->other_context);
304 }
305
306 void
307 QtGLWindow::onSceneGraphInvalidated()
308 {
309   GST_DEBUG ("scene graph invalidated");
310
311   if (this->priv->fbo && this->priv->other_context) {
312     const GstGLFuncs *gl;
313
314     gst_gl_context_activate (this->priv->other_context, TRUE);
315     gl = this->priv->other_context->gl_vtable;
316
317     gl->DeleteFramebuffers (1, &this->priv->fbo);
318
319     gst_gl_context_activate (this->priv->other_context, FALSE);
320   }
321 }
322
323 bool
324 QtGLWindow::getGeometry(int * width, int * height)
325 {
326   if (width == NULL || height == NULL)
327     return FALSE;
328
329   *width = this->source->width();
330   *height = this->source->height();
331
332   return TRUE;
333 }
334
335 GstGLContext *
336 qt_window_get_qt_context (QtGLWindow * qt_window)
337 {
338   g_return_val_if_fail (qt_window != NULL, NULL);
339
340   if (!qt_window->priv->other_context)
341     return NULL;
342
343   return (GstGLContext *) gst_object_ref (qt_window->priv->other_context);
344 }
345
346 GstGLDisplay *
347 qt_window_get_display (QtGLWindow * qt_window)
348 {
349   g_return_val_if_fail (qt_window != NULL, NULL);
350
351   if (!qt_window->priv->display)
352     return NULL;
353
354   return (GstGLDisplay *) gst_object_ref (qt_window->priv->display);
355 }
356
357 gboolean
358 qt_window_is_scenegraph_initialized (QtGLWindow * qt_window)
359 {
360   g_return_val_if_fail (qt_window != NULL, FALSE);
361
362   return qt_window->priv->initted;
363 }
364
365 gboolean
366 qt_window_set_caps (QtGLWindow * qt_window, GstCaps * caps)
367 {
368   GstVideoInfo v_info;
369
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);
373
374   if (qt_window->priv->caps && gst_caps_is_equal_fixed (qt_window->priv->caps, caps))
375     return TRUE;
376
377   if (!gst_video_info_from_caps (&v_info, caps))
378     return FALSE;
379
380   g_mutex_lock (&qt_window->priv->lock);
381
382   gst_caps_replace (&qt_window->priv->caps, caps);
383
384   qt_window->priv->v_info = v_info;
385
386   g_mutex_unlock (&qt_window->priv->lock);
387
388   return TRUE;
389 }
390
391 gboolean
392 qt_window_set_buffer (QtGLWindow * qt_window, GstBuffer * buffer)
393 {
394   g_return_val_if_fail (qt_window != NULL, FALSE);
395   g_return_val_if_fail (qt_window->priv->initted, FALSE);
396   gboolean ret;
397
398   g_mutex_lock (&qt_window->priv->lock);
399
400   if (qt_window->priv->quit){
401     GST_DEBUG("about to quit, drop this buffer");
402     g_mutex_unlock (&qt_window->priv->lock);
403     return TRUE;
404   }
405
406   qt_window->priv->updated = FALSE;
407   qt_window->priv->buffer = buffer;
408
409   while (!qt_window->priv->updated) 
410     g_cond_wait (&qt_window->priv->update_cond, &qt_window->priv->lock);
411   
412   ret = qt_window->priv->result;
413
414   g_mutex_unlock (&qt_window->priv->lock);
415
416   return ret;
417 }
418
419 void
420 qt_window_use_default_fbo (QtGLWindow * qt_window, gboolean useDefaultFbo)
421 {
422   g_return_if_fail (qt_window != NULL);
423
424   g_mutex_lock (&qt_window->priv->lock);
425
426   GST_DEBUG ("set to use default fbo %d", useDefaultFbo);
427   qt_window->priv->useDefaultFbo = useDefaultFbo;
428
429   g_mutex_unlock (&qt_window->priv->lock);
430 }