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 gsize _debug;
39
40 initializeOpenGLFunctions();
41
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);
46 }
47
48 g_weak_ref_init (&this->qt_context_ref_, NULL);
49 gst_video_info_init (&this->v_info);
50
51 this->buffer_ = NULL;
52 this->buffer_was_bound = FALSE;
53 this->sync_buffer_ = gst_buffer_new ();
54 this->dummy_tex_id_ = 0;
55 }
56
~GstQSGTexture()57 GstQSGTexture::~GstQSGTexture ()
58 {
59 g_weak_ref_clear (&this->qt_context_ref_);
60 gst_buffer_replace (&this->buffer_, NULL);
61 gst_buffer_replace (&this->sync_buffer_, NULL);
62 this->buffer_was_bound = FALSE;
63 if (this->dummy_tex_id_ && QOpenGLContext::currentContext ()) {
64 QOpenGLContext::currentContext ()->functions ()->glDeleteTextures (1,
65 &this->dummy_tex_id_);
66 }
67 }
68
69 /* only called from the streaming thread with scene graph thread blocked */
70 void
setCaps(GstCaps * caps)71 GstQSGTexture::setCaps (GstCaps * caps)
72 {
73 GST_LOG ("%p setCaps %" GST_PTR_FORMAT, this, caps);
74
75 gst_video_info_from_caps (&this->v_info, caps);
76 }
77
78 /* only called from the streaming thread with scene graph thread blocked */
79 gboolean
setBuffer(GstBuffer * buffer)80 GstQSGTexture::setBuffer (GstBuffer * buffer)
81 {
82 GST_LOG ("%p setBuffer %" GST_PTR_FORMAT, this, buffer);
83 /* FIXME: update more state here */
84 if (!gst_buffer_replace (&this->buffer_, buffer))
85 return FALSE;
86
87 this->buffer_was_bound = FALSE;
88
89 g_weak_ref_set (&this->qt_context_ref_, gst_gl_context_get_current ());
90
91 return TRUE;
92 }
93
94 /* only called from the streaming thread with scene graph thread blocked */
95 GstBuffer *
getBuffer(gboolean * was_bound)96 GstQSGTexture::getBuffer (gboolean * was_bound)
97 {
98 GstBuffer *buffer = NULL;
99
100 if (this->buffer_)
101 buffer = gst_buffer_ref (this->buffer_);
102 if (was_bound)
103 *was_bound = this->buffer_was_bound;
104
105 return buffer;
106 }
107
108 /* only called from qt's scene graph render thread */
109 void
bind()110 GstQSGTexture::bind ()
111 {
112 const GstGLFuncs *gl;
113 GstGLContext *context, *qt_context;
114 GstGLSyncMeta *sync_meta;
115 GstMemory *mem;
116 guint tex_id;
117 gboolean use_dummy_tex = TRUE;
118
119 qt_context = GST_GL_CONTEXT (g_weak_ref_get (&this->qt_context_ref_));
120 if (!qt_context)
121 goto out;
122
123 if (!this->buffer_)
124 goto out;
125 if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
126 goto out;
127
128 this->mem_ = gst_buffer_peek_memory (this->buffer_, 0);
129 if (!this->mem_)
130 goto out;
131
132 gl = qt_context->gl_vtable;
133
134 /* FIXME: should really lock the memory to prevent write access */
135 if (!gst_video_frame_map (&this->v_frame, &this->v_info, this->buffer_,
136 (GstMapFlags) (GST_MAP_READ | GST_MAP_GL))) {
137 g_assert_not_reached ();
138 goto out;
139 }
140
141 mem = gst_buffer_peek_memory (this->buffer_, 0);
142 g_assert (gst_is_gl_memory (mem));
143
144 context = ((GstGLBaseMemory *)mem)->context;
145
146 sync_meta = gst_buffer_get_gl_sync_meta (this->sync_buffer_);
147 if (!sync_meta)
148 sync_meta = gst_buffer_add_gl_sync_meta (context, this->sync_buffer_);
149
150 gst_gl_sync_meta_set_sync_point (sync_meta, context);
151
152 gst_gl_sync_meta_wait (sync_meta, qt_context);
153
154 tex_id = *(guint *) this->v_frame.data[0];
155 GST_LOG ("%p binding Qt texture %u", this, tex_id);
156
157 gl->BindTexture (GL_TEXTURE_2D, tex_id);
158
159 gst_video_frame_unmap (&this->v_frame);
160
161 /* Texture was successfully bound, so we do not need
162 * to use the dummy texture */
163 use_dummy_tex = FALSE;
164
165 this->buffer_was_bound = TRUE;
166
167 out:
168 gst_clear_object (&qt_context);
169
170 if (G_UNLIKELY (use_dummy_tex)) {
171 QOpenGLContext *qglcontext = QOpenGLContext::currentContext ();
172 QOpenGLFunctions *funcs = qglcontext->functions ();
173
174 /* Create dummy texture if not already present.
175 * Use the Qt OpenGL functions instead of the GstGL ones,
176 * since we are using the Qt OpenGL context here, and we must
177 * be able to delete the texture in the destructor. */
178 if (this->dummy_tex_id_ == 0) {
179 /* Make this a black 64x64 pixel RGBA texture.
180 * This size and format is supported pretty much everywhere, so these
181 * are a safe pick. (64 pixel sidelength must be supported according
182 * to the GLES2 spec, table 6.18.)
183 * Set min/mag filters to GL_LINEAR to make sure no mipmapping is used. */
184 const int tex_sidelength = 64;
185 std::vector < guint8 > dummy_data (tex_sidelength * tex_sidelength * 4, 0);
186
187 funcs->glGenTextures (1, &this->dummy_tex_id_);
188 funcs->glBindTexture (GL_TEXTURE_2D, this->dummy_tex_id_);
189 funcs->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
190 funcs->glTexParameteri (GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
191 funcs->glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA, tex_sidelength,
192 tex_sidelength, 0, GL_RGBA, GL_UNSIGNED_BYTE, &dummy_data[0]);
193 }
194
195 g_assert (this->dummy_tex_id_ != 0);
196
197 funcs->glBindTexture (GL_TEXTURE_2D, this->dummy_tex_id_);
198 GST_LOG ("%p binding fallback dummy Qt texture %u", this, this->dummy_tex_id_);
199 }
200 }
201
202 /* can be called from any thread */
203 int
textureId() const204 GstQSGTexture::textureId () const
205 {
206 int tex_id = 0;
207
208 if (this->buffer_) {
209 GstMemory *mem = gst_buffer_peek_memory (this->buffer_, 0);
210
211 tex_id = ((GstGLMemory *) mem)->tex_id;
212 }
213
214 GST_LOG ("%p get texture id %u", this, tex_id);
215
216 return tex_id;
217 }
218
219 /* can be called from any thread */
220 QSize
textureSize() const221 GstQSGTexture::textureSize () const
222 {
223 if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
224 return QSize (0, 0);
225
226 GST_TRACE ("%p get texture size %ux%u", this, this->v_info.width,
227 this->v_info.height);
228
229 return QSize (this->v_info.width, this->v_info.height);
230 }
231
232 /* can be called from any thread */
233 bool
hasAlphaChannel() const234 GstQSGTexture::hasAlphaChannel () const
235 {
236 const bool has_alpha = GST_VIDEO_FORMAT_INFO_HAS_ALPHA(this->v_info.finfo);
237
238 GST_LOG ("%p get has alpha channel %u", this, has_alpha);
239
240 return has_alpha;
241 }
242
243 /* can be called from any thread */
244 bool
hasMipmaps() const245 GstQSGTexture::hasMipmaps () const
246 {
247 return false;
248 }
249