1 /*
2 * GStreamer
3 * Copyright (C) 2015 Matthew Waters <matthew@centricular.com>
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 <vector>
26 #include <stdio.h>
27
28 #include <gst/video/video.h>
29 #include <gst/gl/gl.h>
30 #include <gst/gl/gstglfuncs.h>
31 #include "gstqsgtexture.h"
32
33 #define GST_CAT_DEFAULT gst_qsg_texture_debug
34 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
35
GstQSGTexture()36 GstQSGTexture::GstQSGTexture ()
37 {
38 static volatile gsize _debug;
39
40 initializeOpenGLFunctions();
41
42 if (g_once_init_enter ((unsigned long *)&_debug)) {
43 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtqsgtexture", 0,
44 "Qt Scenegraph Texture");
45 g_once_init_leave (&_debug, 1);
46 }
47
48 gst_video_info_init (&this->v_info);
49 this->buffer_ = NULL;
50 this->qt_context_ = NULL;
51 this->sync_buffer_ = gst_buffer_new ();
52 this->dummy_tex_id_ = 0;
53 }
54
~GstQSGTexture()55 GstQSGTexture::~GstQSGTexture ()
56 {
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_);
62 }
63 }
64
65 /* only called from the streaming thread with scene graph thread blocked */
66 void
setCaps(GstCaps * caps)67 GstQSGTexture::setCaps (GstCaps * caps)
68 {
69 GST_LOG ("%p setCaps %" GST_PTR_FORMAT, this, caps);
70
71 gst_video_info_from_caps (&this->v_info, caps);
72 }
73
74 /* only called from the streaming thread with scene graph thread blocked */
75 gboolean
setBuffer(GstBuffer * buffer)76 GstQSGTexture::setBuffer (GstBuffer * buffer)
77 {
78 GST_LOG ("%p setBuffer %" GST_PTR_FORMAT, this, buffer);
79 /* FIXME: update more state here */
80 if (!gst_buffer_replace (&this->buffer_, buffer))
81 return FALSE;
82
83 this->qt_context_ = gst_gl_context_get_current ();
84
85 return TRUE;
86 }
87
88 /* only called from qt's scene graph render thread */
89 void
bind()90 GstQSGTexture::bind ()
91 {
92 const GstGLFuncs *gl;
93 GstGLContext *context;
94 GstGLSyncMeta *sync_meta;
95 GstMemory *mem;
96 guint tex_id;
97 gboolean use_dummy_tex = TRUE;
98
99 if (!this->qt_context_)
100 return;
101
102 gst_gl_context_activate (this->qt_context_, TRUE);
103
104 if (!this->buffer_)
105 goto out;
106 if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
107 goto out;
108
109 this->mem_ = gst_buffer_peek_memory (this->buffer_, 0);
110 if (!this->mem_)
111 goto out;
112
113 g_assert (this->qt_context_);
114 gl = this->qt_context_->gl_vtable;
115
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 ();
120 goto out;
121 }
122
123 mem = gst_buffer_peek_memory (this->buffer_, 0);
124 g_assert (gst_is_gl_memory (mem));
125
126 context = ((GstGLBaseMemory *)mem)->context;
127
128 sync_meta = gst_buffer_get_gl_sync_meta (this->sync_buffer_);
129 if (!sync_meta)
130 sync_meta = gst_buffer_add_gl_sync_meta (context, this->sync_buffer_);
131
132 gst_gl_sync_meta_set_sync_point (sync_meta, context);
133
134 gst_gl_sync_meta_wait (sync_meta, this->qt_context_);
135
136 tex_id = *(guint *) this->v_frame.data[0];
137 GST_LOG ("%p binding Qt texture %u", this, tex_id);
138
139 gl->BindTexture (GL_TEXTURE_2D, tex_id);
140
141 gst_video_frame_unmap (&this->v_frame);
142
143 /* Texture was successfully bound, so we do not need
144 * to use the dummy texture */
145 use_dummy_tex = FALSE;
146
147 out:
148 if (G_UNLIKELY (use_dummy_tex)) {
149 QOpenGLContext *qglcontext = QOpenGLContext::currentContext ();
150 QOpenGLFunctions *funcs = qglcontext->functions ();
151
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);
164
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]);
171 }
172
173 g_assert (this->dummy_tex_id_ != 0);
174
175 funcs->glBindTexture (GL_TEXTURE_2D, this->dummy_tex_id_);
176 }
177
178 gst_gl_context_activate (this->qt_context_, FALSE);
179 }
180
181 /* can be called from any thread */
182 int
textureId() const183 GstQSGTexture::textureId () const
184 {
185 int tex_id = 0;
186
187 if (this->buffer_) {
188 GstMemory *mem = gst_buffer_peek_memory (this->buffer_, 0);
189
190 tex_id = ((GstGLMemory *) mem)->tex_id;
191 }
192
193 GST_LOG ("%p get texture id %u", this, tex_id);
194
195 return tex_id;
196 }
197
198 /* can be called from any thread */
199 QSize
textureSize() const200 GstQSGTexture::textureSize () const
201 {
202 if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
203 return QSize (0, 0);
204
205 GST_TRACE ("%p get texture size %ux%u", this, this->v_info.width,
206 this->v_info.height);
207
208 return QSize (this->v_info.width, this->v_info.height);
209 }
210
211 /* can be called from any thread */
212 bool
hasAlphaChannel() const213 GstQSGTexture::hasAlphaChannel () const
214 {
215 /* FIXME: support RGB textures */
216 return true;
217 }
218
219 /* can be called from any thread */
220 bool
hasMipmaps() const221 GstQSGTexture::hasMipmaps () const
222 {
223 return false;
224 }
225