2015-07-06 13:10:51 +00:00
|
|
|
/*
|
|
|
|
* GStreamer
|
|
|
|
* Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
|
|
|
|
* Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
2017-05-21 13:26:12 +00:00
|
|
|
#include <vector>
|
2015-07-06 13:10:51 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#include <gst/video/video.h>
|
|
|
|
#include <gst/gl/gl.h>
|
2017-08-22 11:39:43 +00:00
|
|
|
#include <gst/gl/gstglfuncs.h>
|
2015-07-06 13:10:51 +00:00
|
|
|
#include "gstqsgtexture.h"
|
|
|
|
|
|
|
|
#define GST_CAT_DEFAULT gst_qsg_texture_debug
|
|
|
|
GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
|
|
|
|
|
|
|
|
GstQSGTexture::GstQSGTexture ()
|
|
|
|
{
|
2021-03-18 08:52:53 +00:00
|
|
|
static gsize _debug;
|
2015-07-06 13:10:51 +00:00
|
|
|
|
|
|
|
initializeOpenGLFunctions();
|
|
|
|
|
|
|
|
if (g_once_init_enter (&_debug)) {
|
|
|
|
GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtqsgtexture", 0,
|
|
|
|
"Qt Scenegraph Texture");
|
|
|
|
g_once_init_leave (&_debug, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
gst_video_info_init (&this->v_info);
|
|
|
|
this->buffer_ = NULL;
|
2020-11-03 13:58:30 +00:00
|
|
|
this->buffer_was_bound = FALSE;
|
2016-09-06 05:13:39 +00:00
|
|
|
this->qt_context_ = NULL;
|
2015-08-08 15:28:03 +00:00
|
|
|
this->sync_buffer_ = gst_buffer_new ();
|
2017-05-21 13:26:12 +00:00
|
|
|
this->dummy_tex_id_ = 0;
|
2015-07-06 13:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
GstQSGTexture::~GstQSGTexture ()
|
|
|
|
{
|
|
|
|
gst_buffer_replace (&this->buffer_, NULL);
|
2015-08-08 15:28:03 +00:00
|
|
|
gst_buffer_replace (&this->sync_buffer_, NULL);
|
2020-11-03 13:58:30 +00:00
|
|
|
this->buffer_was_bound = FALSE;
|
2017-05-21 13:26:12 +00:00
|
|
|
if (this->dummy_tex_id_ && QOpenGLContext::currentContext ()) {
|
|
|
|
QOpenGLContext::currentContext ()->functions ()->glDeleteTextures (1,
|
|
|
|
&this->dummy_tex_id_);
|
|
|
|
}
|
2015-07-06 13:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* only called from the streaming thread with scene graph thread blocked */
|
|
|
|
void
|
|
|
|
GstQSGTexture::setCaps (GstCaps * caps)
|
|
|
|
{
|
|
|
|
GST_LOG ("%p setCaps %" GST_PTR_FORMAT, this, caps);
|
|
|
|
|
|
|
|
gst_video_info_from_caps (&this->v_info, caps);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* only called from the streaming thread with scene graph thread blocked */
|
2015-08-08 15:28:03 +00:00
|
|
|
gboolean
|
2015-07-06 13:10:51 +00:00
|
|
|
GstQSGTexture::setBuffer (GstBuffer * buffer)
|
|
|
|
{
|
|
|
|
GST_LOG ("%p setBuffer %" GST_PTR_FORMAT, this, buffer);
|
|
|
|
/* FIXME: update more state here */
|
2015-08-08 15:28:03 +00:00
|
|
|
if (!gst_buffer_replace (&this->buffer_, buffer))
|
|
|
|
return FALSE;
|
|
|
|
|
2020-11-03 13:58:30 +00:00
|
|
|
this->buffer_was_bound = FALSE;
|
2015-08-08 15:28:03 +00:00
|
|
|
this->qt_context_ = gst_gl_context_get_current ();
|
|
|
|
|
|
|
|
return TRUE;
|
2015-07-06 13:10:51 +00:00
|
|
|
}
|
|
|
|
|
2020-11-03 13:58:30 +00:00
|
|
|
/* only called from the streaming thread with scene graph thread blocked */
|
|
|
|
GstBuffer *
|
|
|
|
GstQSGTexture::getBuffer (gboolean * was_bound)
|
|
|
|
{
|
|
|
|
GstBuffer *buffer = NULL;
|
|
|
|
|
|
|
|
if (this->buffer_)
|
|
|
|
buffer = gst_buffer_ref (this->buffer_);
|
|
|
|
if (was_bound)
|
|
|
|
*was_bound = this->buffer_was_bound;
|
|
|
|
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
2015-07-06 13:10:51 +00:00
|
|
|
/* only called from qt's scene graph render thread */
|
|
|
|
void
|
|
|
|
GstQSGTexture::bind ()
|
|
|
|
{
|
2015-09-09 14:00:11 +00:00
|
|
|
const GstGLFuncs *gl;
|
2015-08-08 15:28:03 +00:00
|
|
|
GstGLContext *context;
|
|
|
|
GstGLSyncMeta *sync_meta;
|
|
|
|
GstMemory *mem;
|
2015-07-06 13:10:51 +00:00
|
|
|
guint tex_id;
|
2017-05-21 13:26:12 +00:00
|
|
|
gboolean use_dummy_tex = TRUE;
|
2015-07-06 13:10:51 +00:00
|
|
|
|
2016-09-06 05:13:39 +00:00
|
|
|
if (!this->qt_context_)
|
|
|
|
return;
|
|
|
|
|
2015-07-06 13:10:51 +00:00
|
|
|
if (!this->buffer_)
|
2015-11-20 00:08:37 +00:00
|
|
|
goto out;
|
2015-07-06 13:10:51 +00:00
|
|
|
if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
|
2015-11-20 00:08:37 +00:00
|
|
|
goto out;
|
2015-07-06 13:10:51 +00:00
|
|
|
|
2015-08-08 15:28:03 +00:00
|
|
|
this->mem_ = gst_buffer_peek_memory (this->buffer_, 0);
|
|
|
|
if (!this->mem_)
|
2015-11-20 00:08:37 +00:00
|
|
|
goto out;
|
2015-08-08 15:28:03 +00:00
|
|
|
|
2015-09-09 14:00:11 +00:00
|
|
|
g_assert (this->qt_context_);
|
|
|
|
gl = this->qt_context_->gl_vtable;
|
|
|
|
|
2015-07-06 13:10:51 +00:00
|
|
|
/* FIXME: should really lock the memory to prevent write access */
|
|
|
|
if (!gst_video_frame_map (&this->v_frame, &this->v_info, this->buffer_,
|
|
|
|
(GstMapFlags) (GST_MAP_READ | GST_MAP_GL))) {
|
|
|
|
g_assert_not_reached ();
|
2015-11-20 00:08:37 +00:00
|
|
|
goto out;
|
2015-07-06 13:10:51 +00:00
|
|
|
}
|
|
|
|
|
2015-08-08 15:28:03 +00:00
|
|
|
mem = gst_buffer_peek_memory (this->buffer_, 0);
|
|
|
|
g_assert (gst_is_gl_memory (mem));
|
|
|
|
|
2015-12-14 02:43:59 +00:00
|
|
|
context = ((GstGLBaseMemory *)mem)->context;
|
2015-08-08 15:28:03 +00:00
|
|
|
|
|
|
|
sync_meta = gst_buffer_get_gl_sync_meta (this->sync_buffer_);
|
|
|
|
if (!sync_meta)
|
|
|
|
sync_meta = gst_buffer_add_gl_sync_meta (context, this->sync_buffer_);
|
|
|
|
|
|
|
|
gst_gl_sync_meta_set_sync_point (sync_meta, context);
|
|
|
|
|
|
|
|
gst_gl_sync_meta_wait (sync_meta, this->qt_context_);
|
|
|
|
|
2015-07-06 13:10:51 +00:00
|
|
|
tex_id = *(guint *) this->v_frame.data[0];
|
|
|
|
GST_LOG ("%p binding Qt texture %u", this, tex_id);
|
|
|
|
|
2015-09-09 14:00:11 +00:00
|
|
|
gl->BindTexture (GL_TEXTURE_2D, tex_id);
|
2015-07-06 13:10:51 +00:00
|
|
|
|
|
|
|
gst_video_frame_unmap (&this->v_frame);
|
2015-11-20 00:08:37 +00:00
|
|
|
|
2017-05-21 13:26:12 +00:00
|
|
|
/* Texture was successfully bound, so we do not need
|
|
|
|
* to use the dummy texture */
|
|
|
|
use_dummy_tex = FALSE;
|
|
|
|
|
2020-11-03 13:58:30 +00:00
|
|
|
this->buffer_was_bound = TRUE;
|
|
|
|
|
2015-11-20 00:08:37 +00:00
|
|
|
out:
|
2017-05-21 13:26:12 +00:00
|
|
|
if (G_UNLIKELY (use_dummy_tex)) {
|
|
|
|
QOpenGLContext *qglcontext = QOpenGLContext::currentContext ();
|
|
|
|
QOpenGLFunctions *funcs = qglcontext->functions ();
|
|
|
|
|
|
|
|
/* Create dummy texture if not already present.
|
|
|
|
* Use the Qt OpenGL functions instead of the GstGL ones,
|
|
|
|
* since we are using the Qt OpenGL context here, and we must
|
|
|
|
* be able to delete the texture in the destructor. */
|
|
|
|
if (this->dummy_tex_id_ == 0) {
|
|
|
|
/* Make this a black 64x64 pixel RGBA texture.
|
|
|
|
* This size and format is supported pretty much everywhere, so these
|
|
|
|
* are a safe pick. (64 pixel sidelength must be supported according
|
|
|
|
* to the GLES2 spec, table 6.18.)
|
|
|
|
* Set min/mag filters to GL_LINEAR to make sure no mipmapping is used. */
|
|
|
|
const int tex_sidelength = 64;
|
|
|
|
std::vector < guint8 > dummy_data (tex_sidelength * tex_sidelength * 4, 0);
|
|
|
|
|
|
|
|
funcs->glGenTextures (1, &this->dummy_tex_id_);
|
|
|
|
funcs->glBindTexture (GL_TEXTURE_2D, this->dummy_tex_id_);
|
|
|
|
funcs->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
|
|
|
|
funcs->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
|
|
|
|
funcs->glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_sidelength,
|
|
|
|
tex_sidelength, 0, GL_RGBA, GL_UNSIGNED_BYTE, &dummy_data[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_assert (this->dummy_tex_id_ != 0);
|
|
|
|
|
|
|
|
funcs->glBindTexture (GL_TEXTURE_2D, this->dummy_tex_id_);
|
|
|
|
}
|
2015-07-06 13:10:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* can be called from any thread */
|
|
|
|
int
|
|
|
|
GstQSGTexture::textureId () const
|
|
|
|
{
|
|
|
|
int tex_id = 0;
|
|
|
|
|
|
|
|
if (this->buffer_) {
|
|
|
|
GstMemory *mem = gst_buffer_peek_memory (this->buffer_, 0);
|
|
|
|
|
|
|
|
tex_id = ((GstGLMemory *) mem)->tex_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
GST_LOG ("%p get texture id %u", this, tex_id);
|
|
|
|
|
|
|
|
return tex_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* can be called from any thread */
|
|
|
|
QSize
|
|
|
|
GstQSGTexture::textureSize () const
|
|
|
|
{
|
|
|
|
if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
|
|
|
|
return QSize (0, 0);
|
|
|
|
|
|
|
|
GST_TRACE ("%p get texture size %ux%u", this, this->v_info.width,
|
|
|
|
this->v_info.height);
|
|
|
|
|
|
|
|
return QSize (this->v_info.width, this->v_info.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* can be called from any thread */
|
|
|
|
bool
|
|
|
|
GstQSGTexture::hasAlphaChannel () const
|
|
|
|
{
|
|
|
|
/* FIXME: support RGB textures */
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* can be called from any thread */
|
|
|
|
bool
|
|
|
|
GstQSGTexture::hasMipmaps () const
|
|
|
|
{
|
|
|
|
return false;
|
|
|
|
}
|