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.
27 #include <gst/video/video.h>
28 #include <gst/gl/gl.h>
29 #include "gstqsgtexture.h"
31 #define GST_CAT_DEFAULT gst_qsg_texture_debug
32 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
34 GstQSGTexture::GstQSGTexture ()
36 static volatile gsize _debug;
38 initializeOpenGLFunctions();
40 if (g_once_init_enter (&_debug)) {
41 GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "qtqsgtexture", 0,
42 "Qt Scenegraph Texture");
43 g_once_init_leave (&_debug, 1);
46 gst_video_info_init (&this->v_info);
48 this->sync_buffer_ = gst_buffer_new ();
51 GstQSGTexture::~GstQSGTexture ()
53 gst_buffer_replace (&this->buffer_, NULL);
54 gst_buffer_replace (&this->sync_buffer_, NULL);
57 /* only called from the streaming thread with scene graph thread blocked */
59 GstQSGTexture::setCaps (GstCaps * caps)
61 GST_LOG ("%p setCaps %" GST_PTR_FORMAT, this, caps);
63 gst_video_info_from_caps (&this->v_info, caps);
66 /* only called from the streaming thread with scene graph thread blocked */
68 GstQSGTexture::setBuffer (GstBuffer * buffer)
70 GST_LOG ("%p setBuffer %" GST_PTR_FORMAT, this, buffer);
71 /* FIXME: update more state here */
72 if (!gst_buffer_replace (&this->buffer_, buffer))
75 this->qt_context_ = gst_gl_context_get_current ();
80 /* only called from qt's scene graph render thread */
82 GstQSGTexture::bind ()
85 GstGLContext *context;
86 GstGLSyncMeta *sync_meta;
92 if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
95 this->mem_ = gst_buffer_peek_memory (this->buffer_, 0);
99 g_assert (this->qt_context_);
100 gl = this->qt_context_->gl_vtable;
102 /* FIXME: should really lock the memory to prevent write access */
103 if (!gst_video_frame_map (&this->v_frame, &this->v_info, this->buffer_,
104 (GstMapFlags) (GST_MAP_READ | GST_MAP_GL))) {
105 g_assert_not_reached ();
109 mem = gst_buffer_peek_memory (this->buffer_, 0);
110 g_assert (gst_is_gl_memory (mem));
112 context = ((GstGLBaseBuffer *)mem)->context;
114 sync_meta = gst_buffer_get_gl_sync_meta (this->sync_buffer_);
116 sync_meta = gst_buffer_add_gl_sync_meta (context, this->sync_buffer_);
118 gst_gl_sync_meta_set_sync_point (sync_meta, context);
120 gst_gl_sync_meta_wait (sync_meta, this->qt_context_);
122 tex_id = *(guint *) this->v_frame.data[0];
123 GST_LOG ("%p binding Qt texture %u", this, tex_id);
125 gl->BindTexture (GL_TEXTURE_2D, tex_id);
127 gst_video_frame_unmap (&this->v_frame);
130 /* can be called from any thread */
132 GstQSGTexture::textureId () const
137 GstMemory *mem = gst_buffer_peek_memory (this->buffer_, 0);
139 tex_id = ((GstGLMemory *) mem)->tex_id;
142 GST_LOG ("%p get texture id %u", this, tex_id);
147 /* can be called from any thread */
149 GstQSGTexture::textureSize () const
151 if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
154 GST_TRACE ("%p get texture size %ux%u", this, this->v_info.width,
155 this->v_info.height);
157 return QSize (this->v_info.width, this->v_info.height);
160 /* can be called from any thread */
162 GstQSGTexture::hasAlphaChannel () const
164 /* FIXME: support RGB textures */
168 /* can be called from any thread */
170 GstQSGTexture::hasMipmaps () const