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