qt: use our function table instead of directly calling gl functions
[platform/upstream/gstreamer.git] / ext / qt / gstqsgtexture.cc
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 <stdio.h>
26
27 #include <gst/video/video.h>
28 #include <gst/gl/gl.h>
29 #include "gstqsgtexture.h"
30
31 #define GST_CAT_DEFAULT gst_qsg_texture_debug
32 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
33
34 GstQSGTexture::GstQSGTexture ()
35 {
36   static volatile gsize _debug;
37
38   initializeOpenGLFunctions();
39
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);
44   }
45
46   gst_video_info_init (&this->v_info);
47   this->buffer_ = NULL;
48   this->sync_buffer_ = gst_buffer_new ();
49 }
50
51 GstQSGTexture::~GstQSGTexture ()
52 {
53   gst_buffer_replace (&this->buffer_, NULL);
54   gst_buffer_replace (&this->sync_buffer_, NULL);
55 }
56
57 /* only called from the streaming thread with scene graph thread blocked */
58 void
59 GstQSGTexture::setCaps (GstCaps * caps)
60 {
61   GST_LOG ("%p setCaps %" GST_PTR_FORMAT, this, caps);
62
63   gst_video_info_from_caps (&this->v_info, caps);
64 }
65
66 /* only called from the streaming thread with scene graph thread blocked */
67 gboolean
68 GstQSGTexture::setBuffer (GstBuffer * buffer)
69 {
70   GST_LOG ("%p setBuffer %" GST_PTR_FORMAT, this, buffer);
71   /* FIXME: update more state here */
72   if (!gst_buffer_replace (&this->buffer_, buffer))
73     return FALSE;
74
75   this->qt_context_ = gst_gl_context_get_current ();
76
77   return TRUE;
78 }
79
80 /* only called from qt's scene graph render thread */
81 void
82 GstQSGTexture::bind ()
83 {
84   const GstGLFuncs *gl;
85   GstGLContext *context;
86   GstGLSyncMeta *sync_meta;
87   GstMemory *mem;
88   guint tex_id;
89
90   if (!this->buffer_)
91     return;
92   if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
93     return;
94
95   this->mem_ = gst_buffer_peek_memory (this->buffer_, 0);
96   if (!this->mem_)
97     return;
98
99   g_assert (this->qt_context_);
100   gl = this->qt_context_->gl_vtable;
101
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 ();
106     return;
107   }
108
109   mem = gst_buffer_peek_memory (this->buffer_, 0);
110   g_assert (gst_is_gl_memory (mem));
111
112   context = ((GstGLBaseBuffer *)mem)->context;
113
114   sync_meta = gst_buffer_get_gl_sync_meta (this->sync_buffer_);
115   if (!sync_meta)
116     sync_meta = gst_buffer_add_gl_sync_meta (context, this->sync_buffer_);
117
118   gst_gl_sync_meta_set_sync_point (sync_meta, context);
119
120   gst_gl_sync_meta_wait (sync_meta, this->qt_context_);
121
122   tex_id = *(guint *) this->v_frame.data[0];
123   GST_LOG ("%p binding Qt texture %u", this, tex_id);
124
125   gl->BindTexture (GL_TEXTURE_2D, tex_id);
126
127   gst_video_frame_unmap (&this->v_frame);
128 }
129
130 /* can be called from any thread */
131 int
132 GstQSGTexture::textureId () const
133 {
134   int tex_id = 0;
135
136   if (this->buffer_) {
137     GstMemory *mem = gst_buffer_peek_memory (this->buffer_, 0);
138
139     tex_id = ((GstGLMemory *) mem)->tex_id;
140   }
141
142   GST_LOG ("%p get texture id %u", this, tex_id);
143
144   return tex_id;
145 }
146
147 /* can be called from any thread */
148 QSize
149 GstQSGTexture::textureSize () const
150 {
151   if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
152     return QSize (0, 0);
153
154   GST_TRACE ("%p get texture size %ux%u", this, this->v_info.width,
155       this->v_info.height);
156
157   return QSize (this->v_info.width, this->v_info.height);
158 }
159
160 /* can be called from any thread */
161 bool
162 GstQSGTexture::hasAlphaChannel () const
163 {
164   /* FIXME: support RGB textures */
165   return true;
166 }
167
168 /* can be called from any thread */
169 bool
170 GstQSGTexture::hasMipmaps () const
171 {
172   return false;
173 }