1 /*
2 * GStreamer
3 * Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.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 /**
22 * SECTION:element-glfilterapp
23 * @title: glfilterapp
24 *
25 * The resize and redraw callbacks can be set from a client code.
26 *
27 * ## CLient callbacks
28 *
29 * The graphic scene can be written from a client code through the
30 * two glfilterapp properties.
31 *
32 * ## Examples
33 * see gst-plugins-gl/tests/examples/generic/recordgraphic
34 *
35 */
36
37 #ifdef HAVE_CONFIG_H
38 #include "config.h"
39 #endif
40
41 #include "gstglfilterapp.h"
42
43 #define GST_CAT_DEFAULT gst_gl_filter_app_debug
44 GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT);
45
46 enum
47 {
48 SIGNAL_0,
49 CLIENT_DRAW_SIGNAL,
50 LAST_SIGNAL
51 };
52
53 static guint gst_gl_filter_app_signals[LAST_SIGNAL] = { 0 };
54
55 #define DEBUG_INIT \
56 GST_DEBUG_CATEGORY_INIT (gst_gl_filter_app_debug, "glfilterapp", 0, "glfilterapp element");
57
58 #define gst_gl_filter_app_parent_class parent_class
59 G_DEFINE_TYPE_WITH_CODE (GstGLFilterApp, gst_gl_filter_app,
60 GST_TYPE_GL_FILTER, DEBUG_INIT);
61
62 static void gst_gl_filter_app_set_property (GObject * object, guint prop_id,
63 const GValue * value, GParamSpec * pspec);
64 static void gst_gl_filter_app_get_property (GObject * object, guint prop_id,
65 GValue * value, GParamSpec * pspec);
66
67 static gboolean gst_gl_filter_app_set_caps (GstGLFilter * filter,
68 GstCaps * incaps, GstCaps * outcaps);
69 static gboolean gst_gl_filter_app_filter_texture (GstGLFilter * filter,
70 GstGLMemory * in_tex, GstGLMemory * out_tex);
71
72 static gboolean gst_gl_filter_app_gl_start (GstGLBaseFilter * base_filter);
73 static void gst_gl_filter_app_gl_stop (GstGLBaseFilter * base_filter);
74
75 static void
gst_gl_filter_app_class_init(GstGLFilterAppClass * klass)76 gst_gl_filter_app_class_init (GstGLFilterAppClass * klass)
77 {
78 GObjectClass *gobject_class;
79 GstElementClass *element_class;
80
81 gobject_class = (GObjectClass *) klass;
82 element_class = GST_ELEMENT_CLASS (klass);
83
84 gst_gl_filter_add_rgba_pad_templates (GST_GL_FILTER_CLASS (klass));
85
86 gobject_class->set_property = gst_gl_filter_app_set_property;
87 gobject_class->get_property = gst_gl_filter_app_get_property;
88
89 GST_GL_BASE_FILTER_CLASS (klass)->gl_start = gst_gl_filter_app_gl_start;
90 GST_GL_BASE_FILTER_CLASS (klass)->gl_stop = gst_gl_filter_app_gl_stop;
91
92 GST_GL_FILTER_CLASS (klass)->set_caps = gst_gl_filter_app_set_caps;
93 GST_GL_FILTER_CLASS (klass)->filter_texture =
94 gst_gl_filter_app_filter_texture;
95
96 /**
97 * GstGLFilterApp::client-draw:
98 * @object: the #GstGLImageSink
99 * @texture: the #guint id of the texture.
100 * @width: the #guint width of the texture.
101 * @height: the #guint height of the texture.
102 *
103 * Will be emitted before to draw the texture. The client should
104 * redraw the surface/contents with the @texture, @width and @height.
105 *
106 * Returns: whether the texture was redrawn by the signal. If not, a
107 * default redraw will occur.
108 */
109 gst_gl_filter_app_signals[CLIENT_DRAW_SIGNAL] =
110 g_signal_new ("client-draw", G_TYPE_FROM_CLASS (klass),
111 G_SIGNAL_RUN_LAST, 0, NULL, NULL, g_cclosure_marshal_generic,
112 G_TYPE_BOOLEAN, 3, G_TYPE_UINT, G_TYPE_UINT, G_TYPE_UINT);
113
114 gst_element_class_set_metadata (element_class,
115 "OpenGL application filter", "Filter/Effect",
116 "Use client callbacks to define the scene",
117 "Julien Isorce <julien.isorce@gmail.com>");
118
119 GST_GL_BASE_FILTER_CLASS (klass)->supported_gl_api =
120 GST_GL_API_OPENGL | GST_GL_API_GLES2 | GST_GL_API_OPENGL3;
121 }
122
123 static void
gst_gl_filter_app_init(GstGLFilterApp * filter)124 gst_gl_filter_app_init (GstGLFilterApp * filter)
125 {
126 }
127
128 static void
gst_gl_filter_app_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)129 gst_gl_filter_app_set_property (GObject * object, guint prop_id,
130 const GValue * value, GParamSpec * pspec)
131 {
132 switch (prop_id) {
133 default:
134 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
135 break;
136 }
137 }
138
139 static void
gst_gl_filter_app_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)140 gst_gl_filter_app_get_property (GObject * object, guint prop_id,
141 GValue * value, GParamSpec * pspec)
142 {
143 switch (prop_id) {
144 default:
145 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
146 break;
147 }
148 }
149
150 static gboolean
gst_gl_filter_app_gl_start(GstGLBaseFilter * base_filter)151 gst_gl_filter_app_gl_start (GstGLBaseFilter * base_filter)
152 {
153 GstGLFilter *filter = GST_GL_FILTER (base_filter);
154 GError *error = NULL;
155
156 if (!(filter->default_shader =
157 gst_gl_shader_new_default (base_filter->context, &error))) {
158 GST_ELEMENT_ERROR (filter, RESOURCE, NOT_FOUND, ("%s",
159 "Failed to create the default shader"), ("%s", error->message));
160 return FALSE;
161 }
162
163 return GST_GL_BASE_FILTER_CLASS (parent_class)->gl_start (base_filter);
164 }
165
166 static void
gst_gl_filter_app_gl_stop(GstGLBaseFilter * base_filter)167 gst_gl_filter_app_gl_stop (GstGLBaseFilter * base_filter)
168 {
169 GstGLFilter *filter = GST_GL_FILTER (base_filter);
170
171 if (filter->default_shader)
172 gst_object_unref (filter->default_shader);
173 filter->default_shader = NULL;
174
175 GST_GL_BASE_FILTER_CLASS (parent_class)->gl_stop (base_filter);
176 }
177
178 static gboolean
gst_gl_filter_app_set_caps(GstGLFilter * filter,GstCaps * incaps,GstCaps * outcaps)179 gst_gl_filter_app_set_caps (GstGLFilter * filter, GstCaps * incaps,
180 GstCaps * outcaps)
181 {
182 //GstGLFilterApp* app_filter = GST_GL_FILTER_APP(filter);
183
184 return TRUE;
185 }
186
187 struct glcb2
188 {
189 GstGLFilterApp *app;
190 GstGLMemory *in_tex;
191 GstGLMemory *out_tex;
192 };
193
194 static gboolean
_emit_draw_signal(gpointer data)195 _emit_draw_signal (gpointer data)
196 {
197 struct glcb2 *cb = data;
198 gboolean drawn;
199
200 g_signal_emit (cb->app, gst_gl_filter_app_signals[CLIENT_DRAW_SIGNAL], 0,
201 cb->in_tex->tex_id, gst_gl_memory_get_texture_width (cb->out_tex),
202 gst_gl_memory_get_texture_height (cb->out_tex), &drawn);
203
204 return !drawn;
205 }
206
207 static gboolean
gst_gl_filter_app_filter_texture(GstGLFilter * filter,GstGLMemory * in_tex,GstGLMemory * out_tex)208 gst_gl_filter_app_filter_texture (GstGLFilter * filter, GstGLMemory * in_tex,
209 GstGLMemory * out_tex)
210 {
211 GstGLFilterApp *app_filter = GST_GL_FILTER_APP (filter);
212 gboolean default_draw;
213 struct glcb2 cb;
214
215 cb.app = app_filter;
216 cb.in_tex = in_tex;
217 cb.out_tex = out_tex;
218
219 default_draw =
220 gst_gl_framebuffer_draw_to_texture (filter->fbo,
221 out_tex, _emit_draw_signal, &cb);
222
223 if (default_draw) {
224 gst_gl_filter_render_to_target_with_shader (filter, in_tex, out_tex,
225 filter->default_shader);
226 }
227
228 return TRUE;
229 }
230