3 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
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.
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.
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.
28 #include <gst/video/video.h>
29 #include <gst/gl/gl.h>
30 #include <gst/gl/gstglfuncs.h>
31 #include "gstqsgtexture.h"
33 #define GST_CAT_DEFAULT gst_qsg_texture_debug
34 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
36 GstQSGTexture::GstQSGTexture ()
38 static volatile gsize _debug;
40 initializeOpenGLFunctions();
42 if (g_once_init_enter (&_debug)) {
43 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtqsgtexture", 0,
44 "Qt Scenegraph Texture");
45 g_once_init_leave (&_debug, 1);
48 gst_video_info_init (&this->v_info);
50 this->qt_context_ = NULL;
51 this->sync_buffer_ = gst_buffer_new ();
52 this->dummy_tex_id_ = 0;
55 GstQSGTexture::~GstQSGTexture ()
57 gst_buffer_replace (&this->buffer_, NULL);
58 gst_buffer_replace (&this->sync_buffer_, NULL);
59 if (this->dummy_tex_id_ && QOpenGLContext::currentContext ()) {
60 QOpenGLContext::currentContext ()->functions ()->glDeleteTextures (1,
61 &this->dummy_tex_id_);
65 /* only called from the streaming thread with scene graph thread blocked */
67 GstQSGTexture::setCaps (GstCaps * caps)
69 GST_LOG ("%p setCaps %" GST_PTR_FORMAT, this, caps);
71 gst_video_info_from_caps (&this->v_info, caps);
74 /* only called from the streaming thread with scene graph thread blocked */
76 GstQSGTexture::setBuffer (GstBuffer * buffer)
78 GST_LOG ("%p setBuffer %" GST_PTR_FORMAT, this, buffer);
79 /* FIXME: update more state here */
80 if (!gst_buffer_replace (&this->buffer_, buffer))
83 this->qt_context_ = gst_gl_context_get_current ();
88 /* only called from qt's scene graph render thread */
90 GstQSGTexture::bind ()
93 GstGLContext *context;
94 GstGLSyncMeta *sync_meta;
97 gboolean use_dummy_tex = TRUE;
99 if (!this->qt_context_)
102 gst_gl_context_activate (this->qt_context_, TRUE);
106 if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
109 this->mem_ = gst_buffer_peek_memory (this->buffer_, 0);
113 g_assert (this->qt_context_);
114 gl = this->qt_context_->gl_vtable;
116 /* FIXME: should really lock the memory to prevent write access */
117 if (!gst_video_frame_map (&this->v_frame, &this->v_info, this->buffer_,
118 (GstMapFlags) (GST_MAP_READ | GST_MAP_GL))) {
119 g_assert_not_reached ();
123 mem = gst_buffer_peek_memory (this->buffer_, 0);
124 g_assert (gst_is_gl_memory (mem));
126 context = ((GstGLBaseMemory *)mem)->context;
128 sync_meta = gst_buffer_get_gl_sync_meta (this->sync_buffer_);
130 sync_meta = gst_buffer_add_gl_sync_meta (context, this->sync_buffer_);
132 gst_gl_sync_meta_set_sync_point (sync_meta, context);
134 gst_gl_sync_meta_wait (sync_meta, this->qt_context_);
136 tex_id = *(guint *) this->v_frame.data[0];
137 GST_LOG ("%p binding Qt texture %u", this, tex_id);
139 gl->BindTexture (GL_TEXTURE_2D, tex_id);
141 gst_video_frame_unmap (&this->v_frame);
143 /* Texture was successfully bound, so we do not need
144 * to use the dummy texture */
145 use_dummy_tex = FALSE;
148 if (G_UNLIKELY (use_dummy_tex)) {
149 QOpenGLContext *qglcontext = QOpenGLContext::currentContext ();
150 QOpenGLFunctions *funcs = qglcontext->functions ();
152 /* Create dummy texture if not already present.
153 * Use the Qt OpenGL functions instead of the GstGL ones,
154 * since we are using the Qt OpenGL context here, and we must
155 * be able to delete the texture in the destructor. */
156 if (this->dummy_tex_id_ == 0) {
157 /* Make this a black 64x64 pixel RGBA texture.
158 * This size and format is supported pretty much everywhere, so these
159 * are a safe pick. (64 pixel sidelength must be supported according
160 * to the GLES2 spec, table 6.18.)
161 * Set min/mag filters to GL_LINEAR to make sure no mipmapping is used. */
162 const int tex_sidelength = 64;
163 std::vector < guint8 > dummy_data (tex_sidelength * tex_sidelength * 4, 0);
165 funcs->glGenTextures (1, &this->dummy_tex_id_);
166 funcs->glBindTexture (GL_TEXTURE_2D, this->dummy_tex_id_);
167 funcs->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
168 funcs->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
169 funcs->glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_sidelength,
170 tex_sidelength, 0, GL_RGBA, GL_UNSIGNED_BYTE, &dummy_data[0]);
173 g_assert (this->dummy_tex_id_ != 0);
175 funcs->glBindTexture (GL_TEXTURE_2D, this->dummy_tex_id_);
178 gst_gl_context_activate (this->qt_context_, FALSE);
181 /* can be called from any thread */
183 GstQSGTexture::textureId () const
188 GstMemory *mem = gst_buffer_peek_memory (this->buffer_, 0);
190 tex_id = ((GstGLMemory *) mem)->tex_id;
193 GST_LOG ("%p get texture id %u", this, tex_id);
198 /* can be called from any thread */
200 GstQSGTexture::textureSize () const
202 if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
205 GST_TRACE ("%p get texture size %ux%u", this, this->v_info.width,
206 this->v_info.height);
208 return QSize (this->v_info.width, this->v_info.height);
211 /* can be called from any thread */
213 GstQSGTexture::hasAlphaChannel () const
215 /* FIXME: support RGB textures */
219 /* can be called from any thread */
221 GstQSGTexture::hasMipmaps () const