qml: activate the wrapped context when binding
[platform/upstream/gst-plugins-good.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   gst_gl_context_activate (this->qt_context_, TRUE);
91
92   if (!this->buffer_)
93     goto out;
94   if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
95     goto out;
96
97   this->mem_ = gst_buffer_peek_memory (this->buffer_, 0);
98   if (!this->mem_)
99     goto out;
100
101   g_assert (this->qt_context_);
102   gl = this->qt_context_->gl_vtable;
103
104   /* FIXME: should really lock the memory to prevent write access */
105   if (!gst_video_frame_map (&this->v_frame, &this->v_info, this->buffer_,
106         (GstMapFlags) (GST_MAP_READ | GST_MAP_GL))) {
107     g_assert_not_reached ();
108     goto out;
109   }
110
111   mem = gst_buffer_peek_memory (this->buffer_, 0);
112   g_assert (gst_is_gl_memory (mem));
113
114   context = ((GstGLBaseBuffer *)mem)->context;
115
116   sync_meta = gst_buffer_get_gl_sync_meta (this->sync_buffer_);
117   if (!sync_meta)
118     sync_meta = gst_buffer_add_gl_sync_meta (context, this->sync_buffer_);
119
120   gst_gl_sync_meta_set_sync_point (sync_meta, context);
121
122   gst_gl_sync_meta_wait (sync_meta, this->qt_context_);
123
124   tex_id = *(guint *) this->v_frame.data[0];
125   GST_LOG ("%p binding Qt texture %u", this, tex_id);
126
127   gl->BindTexture (GL_TEXTURE_2D, tex_id);
128
129   gst_video_frame_unmap (&this->v_frame);
130
131 out:
132   gst_gl_context_activate (this->qt_context_, FALSE);
133 }
134
135 /* can be called from any thread */
136 int
137 GstQSGTexture::textureId () const
138 {
139   int tex_id = 0;
140
141   if (this->buffer_) {
142     GstMemory *mem = gst_buffer_peek_memory (this->buffer_, 0);
143
144     tex_id = ((GstGLMemory *) mem)->tex_id;
145   }
146
147   GST_LOG ("%p get texture id %u", this, tex_id);
148
149   return tex_id;
150 }
151
152 /* can be called from any thread */
153 QSize
154 GstQSGTexture::textureSize () const
155 {
156   if (GST_VIDEO_INFO_FORMAT (&this->v_info) == GST_VIDEO_FORMAT_UNKNOWN)
157     return QSize (0, 0);
158
159   GST_TRACE ("%p get texture size %ux%u", this, this->v_info.width,
160       this->v_info.height);
161
162   return QSize (this->v_info.width, this->v_info.height);
163 }
164
165 /* can be called from any thread */
166 bool
167 GstQSGTexture::hasAlphaChannel () const
168 {
169   /* FIXME: support RGB textures */
170   return true;
171 }
172
173 /* can be called from any thread */
174 bool
175 GstQSGTexture::hasMipmaps () const
176 {
177   return false;
178 }