gltestsrc: Add other-context property
[platform/upstream/gst-plugins-base.git] / ext / gl / gstgltestsrc.c
1 /*
2  * GStreamer
3  * Copyright (C) <1999> Erik Walthinsen <omega@cse.ogi.edu>
4  * Copyright (C) 2002,2007 David A. Schleef <ds@schleef.org>
5  * Copyright (C) 2008 Julien Isorce <julien.isorce@gmail.com>
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
20  * Boston, MA 02110-1301, USA.
21  */
22
23 /**
24  * SECTION:element-gltestsrc
25  *
26  * <refsect2>
27  * <para>
28  * The gltestsrc element is used to produce test video texture.
29  * The video test produced can be controlled with the "pattern"
30  * property.
31  * </para>
32  * <title>Example launch line</title>
33  * <para>
34  * <programlisting>
35  * gst-launch -v gltestsrc pattern=smpte ! glimagesink
36  * </programlisting>
37  * Shows original SMPTE color bars in a window.
38  * </para>
39  * </refsect2>
40  */
41
42 #ifdef HAVE_CONFIG_H
43 #include "config.h"
44 #endif
45
46 #include "gstgltestsrc.h"
47 #include "gltestsrc.h"
48 #include <gst/gst-i18n-plugin.h>
49
50 #if GST_GL_HAVE_PLATFORM_EGL
51 #include <gst/gl/egl/gsteglimagememory.h>
52 #endif
53
54 #define USE_PEER_BUFFERALLOC
55
56 GST_DEBUG_CATEGORY_STATIC (gl_test_src_debug);
57 #define GST_CAT_DEFAULT gl_test_src_debug
58
59 enum
60 {
61   PROP_0,
62   PROP_OTHER_CONTEXT,
63   PROP_PATTERN,
64   PROP_TIMESTAMP_OFFSET,
65   PROP_IS_LIVE
66       /* FILL ME */
67 };
68
69 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
70     GST_PAD_SRC,
71     GST_PAD_ALWAYS,
72     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE_WITH_FEATURES
73         (GST_CAPS_FEATURE_MEMORY_GL_MEMORY,
74             "RGBA") "; "
75 #if GST_GL_HAVE_PLATFORM_EGL
76         GST_VIDEO_CAPS_MAKE_WITH_FEATURES (GST_CAPS_FEATURE_MEMORY_EGL_IMAGE,
77             "RGBA") "; "
78 #endif
79         GST_VIDEO_CAPS_MAKE_WITH_FEATURES
80         (GST_CAPS_FEATURE_META_GST_VIDEO_GL_TEXTURE_UPLOAD_META,
81             "RGBA") "; " GST_VIDEO_CAPS_MAKE (GST_GL_COLOR_CONVERT_FORMATS))
82     );
83
84 #define gst_gl_test_src_parent_class parent_class
85 G_DEFINE_TYPE (GstGLTestSrc, gst_gl_test_src, GST_TYPE_PUSH_SRC);
86
87 static void gst_gl_test_src_set_pattern (GstGLTestSrc * gltestsrc,
88     int pattern_type);
89 static void gst_gl_test_src_set_property (GObject * object, guint prop_id,
90     const GValue * value, GParamSpec * pspec);
91 static void gst_gl_test_src_get_property (GObject * object, guint prop_id,
92     GValue * value, GParamSpec * pspec);
93 static void gst_gl_test_src_dispose (GObject * object);
94
95 static gboolean gst_gl_test_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps);
96 static GstCaps *gst_gl_test_src_fixate (GstBaseSrc * bsrc, GstCaps * caps);
97
98 static gboolean gst_gl_test_src_is_seekable (GstBaseSrc * psrc);
99 static gboolean gst_gl_test_src_do_seek (GstBaseSrc * bsrc,
100     GstSegment * segment);
101 static gboolean gst_gl_test_src_query (GstBaseSrc * bsrc, GstQuery * query);
102 static void gst_gl_test_src_set_context (GstElement * element,
103     GstContext * context);
104
105 static void gst_gl_test_src_get_times (GstBaseSrc * basesrc,
106     GstBuffer * buffer, GstClockTime * start, GstClockTime * end);
107 static GstFlowReturn gst_gl_test_src_fill (GstPushSrc * psrc,
108     GstBuffer * buffer);
109 static gboolean gst_gl_test_src_start (GstBaseSrc * basesrc);
110 static gboolean gst_gl_test_src_stop (GstBaseSrc * basesrc);
111 static gboolean gst_gl_test_src_decide_allocation (GstBaseSrc * basesrc,
112     GstQuery * query);
113
114 static void gst_gl_test_src_callback (gpointer stuff);
115
116 static gboolean gst_gl_test_src_init_shader (GstGLTestSrc * gltestsrc);
117
118 #define GST_TYPE_GL_TEST_SRC_PATTERN (gst_gl_test_src_pattern_get_type ())
119 static GType
120 gst_gl_test_src_pattern_get_type (void)
121 {
122   static GType gl_test_src_pattern_type = 0;
123   static const GEnumValue pattern_types[] = {
124     {GST_GL_TEST_SRC_SMPTE, "SMPTE 100% color bars", "smpte"},
125     {GST_GL_TEST_SRC_SNOW, "Random (television snow)", "snow"},
126     {GST_GL_TEST_SRC_BLACK, "100% Black", "black"},
127     {GST_GL_TEST_SRC_WHITE, "100% White", "white"},
128     {GST_GL_TEST_SRC_RED, "Red", "red"},
129     {GST_GL_TEST_SRC_GREEN, "Green", "green"},
130     {GST_GL_TEST_SRC_BLUE, "Blue", "blue"},
131     {GST_GL_TEST_SRC_CHECKERS1, "Checkers 1px", "checkers-1"},
132     {GST_GL_TEST_SRC_CHECKERS2, "Checkers 2px", "checkers-2"},
133     {GST_GL_TEST_SRC_CHECKERS4, "Checkers 4px", "checkers-4"},
134     {GST_GL_TEST_SRC_CHECKERS8, "Checkers 8px", "checkers-8"},
135     {GST_GL_TEST_SRC_CIRCULAR, "Circular", "circular"},
136     {GST_GL_TEST_SRC_BLINK, "Blink", "blink"},
137     {GST_GL_TEST_SRC_MANDELBROT, "Mandelbrot Fractal", "mandelbrot"},
138     {0, NULL, NULL}
139   };
140
141   if (!gl_test_src_pattern_type) {
142     gl_test_src_pattern_type =
143         g_enum_register_static ("GstGLTestSrcPattern", pattern_types);
144   }
145   return gl_test_src_pattern_type;
146 }
147
148 static void
149 gst_gl_test_src_class_init (GstGLTestSrcClass * klass)
150 {
151   GObjectClass *gobject_class;
152   GstBaseSrcClass *gstbasesrc_class;
153   GstPushSrcClass *gstpushsrc_class;
154   GstElementClass *element_class;
155
156   GST_DEBUG_CATEGORY_INIT (gl_test_src_debug, "gltestsrc", 0,
157       "Video Test Source");
158
159   gobject_class = (GObjectClass *) klass;
160   gstbasesrc_class = (GstBaseSrcClass *) klass;
161   gstpushsrc_class = (GstPushSrcClass *) klass;
162   element_class = GST_ELEMENT_CLASS (klass);
163
164   gobject_class->set_property = gst_gl_test_src_set_property;
165   gobject_class->get_property = gst_gl_test_src_get_property;
166   gobject_class->dispose = gst_gl_test_src_dispose;
167
168   g_object_class_install_property (gobject_class, PROP_OTHER_CONTEXT,
169       g_param_spec_object ("other-context",
170           "External OpenGL context",
171           "Give an external OpenGL context with which to share textures",
172           GST_GL_TYPE_CONTEXT, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
173   g_object_class_install_property (gobject_class, PROP_PATTERN,
174       g_param_spec_enum ("pattern", "Pattern",
175           "Type of test pattern to generate", GST_TYPE_GL_TEST_SRC_PATTERN,
176           GST_GL_TEST_SRC_SMPTE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
177   g_object_class_install_property (gobject_class,
178       PROP_TIMESTAMP_OFFSET, g_param_spec_int64 ("timestamp-offset",
179           "Timestamp offset",
180           "An offset added to timestamps set on buffers (in ns)", G_MININT64,
181           G_MAXINT64, 0, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
182   g_object_class_install_property (gobject_class, PROP_IS_LIVE,
183       g_param_spec_boolean ("is-live", "Is Live",
184           "Whether to act as a live source", FALSE,
185           G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS));
186
187   gst_element_class_set_metadata (element_class, "Video test source",
188       "Source/Video", "Creates a test video stream",
189       "David A. Schleef <ds@schleef.org>");
190
191   gst_element_class_add_pad_template (element_class,
192       gst_static_pad_template_get (&src_factory));
193
194   element_class->set_context = gst_gl_test_src_set_context;
195
196   gstbasesrc_class->set_caps = gst_gl_test_src_setcaps;
197   gstbasesrc_class->is_seekable = gst_gl_test_src_is_seekable;
198   gstbasesrc_class->do_seek = gst_gl_test_src_do_seek;
199   gstbasesrc_class->query = gst_gl_test_src_query;
200   gstbasesrc_class->get_times = gst_gl_test_src_get_times;
201   gstbasesrc_class->start = gst_gl_test_src_start;
202   gstbasesrc_class->stop = gst_gl_test_src_stop;
203   gstbasesrc_class->fixate = gst_gl_test_src_fixate;
204   gstbasesrc_class->decide_allocation = gst_gl_test_src_decide_allocation;
205
206   gstpushsrc_class->fill = gst_gl_test_src_fill;
207 }
208
209 static void
210 gst_gl_test_src_init (GstGLTestSrc * src)
211 {
212   gst_gl_test_src_set_pattern (src, GST_GL_TEST_SRC_SMPTE);
213
214   src->timestamp_offset = 0;
215
216   /* we operate in time */
217   gst_base_src_set_format (GST_BASE_SRC (src), GST_FORMAT_TIME);
218   gst_base_src_set_live (GST_BASE_SRC (src), FALSE);
219 }
220
221 static GstCaps *
222 gst_gl_test_src_fixate (GstBaseSrc * bsrc, GstCaps * caps)
223 {
224   GstStructure *structure;
225
226   GST_DEBUG ("fixate");
227
228   caps = gst_caps_make_writable (caps);
229
230   structure = gst_caps_get_structure (caps, 0);
231
232   gst_structure_fixate_field_nearest_int (structure, "width", 320);
233   gst_structure_fixate_field_nearest_int (structure, "height", 240);
234   gst_structure_fixate_field_nearest_fraction (structure, "framerate", 30, 1);
235
236   caps = GST_BASE_SRC_CLASS (parent_class)->fixate (bsrc, caps);
237
238   return caps;
239 }
240
241 const gchar *snow_vertex_src = "attribute vec4 position; \
242     attribute vec2 uv; \
243     uniform mat4 mvp; \
244     varying vec2 out_uv; \
245     void main() \
246     { \
247        gl_Position = mvp * position; \
248        out_uv = uv; \
249     }";
250
251 const gchar *snow_fragment_src = "uniform float time; \
252     varying vec2 out_uv; \
253     \
254     float rand(vec2 co){ \
255         return fract(sin(dot(co.xy, vec2(12.9898,78.233))) * 43758.5453); \
256     } \
257     void main() \
258     { \
259       gl_FragColor = rand(time * out_uv) * vec4(1); \
260     }";
261
262 const gchar *mandelbrot_vertex_src = "attribute vec4 position; \
263     attribute vec2 uv; \
264     uniform mat4 mvp; \
265     uniform float aspect_ratio; \
266     varying vec2 fractal_position; \
267     \
268     void main() \
269     { \
270        gl_Position = mvp * position; \
271        fractal_position = vec2(uv.y - 0.8, aspect_ratio * (uv.x - 0.5)); \
272        fractal_position *= 2.5; \
273     }";
274
275 const gchar *mandelbrot_fragment_src = "uniform float time; \
276     varying vec2 fractal_position; \
277     \
278     const vec4 K = vec4(1.0, 0.66, 0.33, 3.0); \
279     \
280     vec4 hsv_to_rgb(float hue, float saturation, float value) { \
281       vec4 p = abs(fract(vec4(hue) + K) * 6.0 - K.wwww); \
282       return value * mix(K.xxxx, clamp(p - K.xxxx, 0.0, 1.0), saturation); \
283     } \
284     \
285     vec4 i_to_rgb(int i) { \
286       float hue = float(i) / 100.0 + sin(time); \
287       return hsv_to_rgb(hue, 0.5, 0.8); \
288     } \
289     \
290     vec2 pow_2_complex(vec2 c) { \
291       return vec2(c.x*c.x - c.y*c.y, 2.0 * c.x * c.y); \
292     } \
293     \
294     vec2 mandelbrot(vec2 c, vec2 c0) { \
295       return pow_2_complex(c) + c0; \
296     } \
297     \
298     vec4 iterate_pixel(vec2 position) { \
299       vec2 c = vec2(0); \
300       for (int i=0; i < 100; i++) { \
301         if (c.x*c.x + c.y*c.y > 2.0*2.0) \
302           return i_to_rgb(i); \
303         c = mandelbrot(c, position); \
304       } \
305       return vec4(0, 0, 0, 1); \
306     } \
307     \
308     void main() { \
309       gl_FragColor = iterate_pixel(fractal_position); \
310     }";
311
312
313 const gchar *checkers_vertex_src = "attribute vec4 position; \
314     uniform mat4 mvp; \
315     void main() \
316     { \
317        gl_Position = mvp * position; \
318     }";
319
320 const gchar *checkers_fragment_src = "uniform float checker_width; \
321     void main() \
322     { \
323       vec2 xy_index= floor((gl_FragCoord.xy-vec2(0.5,0.5))/checker_width); \
324       vec2 xy_mod=mod(xy_index,vec2(2.0,2.0)); \
325       float result=mod(xy_mod.x+xy_mod.y,2.0); \
326       gl_FragColor.r=step(result,0.5); \
327       gl_FragColor.g=1.0-gl_FragColor.r; \
328       gl_FragColor.ba=vec2(0,1); \
329     }";
330
331
332 static void
333 gst_gl_test_src_set_pattern (GstGLTestSrc * gltestsrc, gint pattern_type)
334 {
335   gltestsrc->pattern_type = pattern_type;
336
337   GST_DEBUG_OBJECT (gltestsrc, "setting pattern to %d", pattern_type);
338
339   switch (pattern_type) {
340     case GST_GL_TEST_SRC_SMPTE:
341       gltestsrc->make_image = gst_gl_test_src_smpte;
342       break;
343     case GST_GL_TEST_SRC_SNOW:
344       gltestsrc->vertex_src = snow_vertex_src;
345       gltestsrc->fragment_src = snow_fragment_src;
346       gltestsrc->make_image = gst_gl_test_src_shader;
347       break;
348     case GST_GL_TEST_SRC_BLACK:
349       gltestsrc->make_image = gst_gl_test_src_black;
350       break;
351     case GST_GL_TEST_SRC_WHITE:
352       gltestsrc->make_image = gst_gl_test_src_white;
353       break;
354     case GST_GL_TEST_SRC_RED:
355       gltestsrc->make_image = gst_gl_test_src_red;
356       break;
357     case GST_GL_TEST_SRC_GREEN:
358       gltestsrc->make_image = gst_gl_test_src_green;
359       break;
360     case GST_GL_TEST_SRC_BLUE:
361       gltestsrc->make_image = gst_gl_test_src_blue;
362       break;
363     case GST_GL_TEST_SRC_CHECKERS1:
364       gltestsrc->vertex_src = checkers_vertex_src;
365       gltestsrc->fragment_src = checkers_fragment_src;
366       gltestsrc->make_image = gst_gl_test_src_checkers1;
367       break;
368     case GST_GL_TEST_SRC_CHECKERS2:
369       gltestsrc->vertex_src = checkers_vertex_src;
370       gltestsrc->fragment_src = checkers_fragment_src;
371       gltestsrc->make_image = gst_gl_test_src_checkers2;
372       break;
373     case GST_GL_TEST_SRC_CHECKERS4:
374       gltestsrc->vertex_src = checkers_vertex_src;
375       gltestsrc->fragment_src = checkers_fragment_src;
376       gltestsrc->make_image = gst_gl_test_src_checkers4;
377       break;
378     case GST_GL_TEST_SRC_CHECKERS8:
379       gltestsrc->vertex_src = checkers_vertex_src;
380       gltestsrc->fragment_src = checkers_fragment_src;
381       gltestsrc->make_image = gst_gl_test_src_checkers8;
382       break;
383     case GST_GL_TEST_SRC_CIRCULAR:
384       gltestsrc->make_image = gst_gl_test_src_circular;
385       break;
386     case GST_GL_TEST_SRC_BLINK:
387       gltestsrc->make_image = gst_gl_test_src_black;
388       break;
389     case GST_GL_TEST_SRC_MANDELBROT:
390       gltestsrc->vertex_src = mandelbrot_vertex_src;
391       gltestsrc->fragment_src = mandelbrot_fragment_src;
392       gltestsrc->make_image = gst_gl_test_src_shader;
393       break;
394     default:
395       g_assert_not_reached ();
396   }
397 }
398
399 static void
400 gst_gl_test_src_dispose (GObject * object)
401 {
402   GstGLTestSrc *src = GST_GL_TEST_SRC (object);
403
404   if (src->other_context)
405     gst_object_unref (src->other_context);
406   src->other_context = NULL;
407
408   G_OBJECT_CLASS (parent_class)->dispose (object);
409 }
410
411 static void
412 gst_gl_test_src_set_property (GObject * object, guint prop_id,
413     const GValue * value, GParamSpec * pspec)
414 {
415   GstGLTestSrc *src = GST_GL_TEST_SRC (object);
416
417   switch (prop_id) {
418     case PROP_OTHER_CONTEXT:
419       if (src->other_context)
420         gst_object_unref (src->other_context);
421       src->other_context = g_value_dup_object (value);
422       break;
423     case PROP_PATTERN:
424       gst_gl_test_src_set_pattern (src, g_value_get_enum (value));
425       break;
426     case PROP_TIMESTAMP_OFFSET:
427       src->timestamp_offset = g_value_get_int64 (value);
428       break;
429     case PROP_IS_LIVE:
430       gst_base_src_set_live (GST_BASE_SRC (src), g_value_get_boolean (value));
431       break;
432     default:
433       break;
434   }
435 }
436
437 static void
438 gst_gl_test_src_get_property (GObject * object, guint prop_id,
439     GValue * value, GParamSpec * pspec)
440 {
441   GstGLTestSrc *src = GST_GL_TEST_SRC (object);
442
443   switch (prop_id) {
444     case PROP_OTHER_CONTEXT:
445       g_value_set_object (value, src->other_context);
446       break;
447     case PROP_PATTERN:
448       g_value_set_enum (value, src->pattern_type);
449       break;
450     case PROP_TIMESTAMP_OFFSET:
451       g_value_set_int64 (value, src->timestamp_offset);
452       break;
453     case PROP_IS_LIVE:
454       g_value_set_boolean (value, gst_base_src_is_live (GST_BASE_SRC (src)));
455       break;
456     default:
457       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
458       break;
459   }
460 }
461
462 static gboolean
463 gst_gl_test_src_setcaps (GstBaseSrc * bsrc, GstCaps * caps)
464 {
465   GstGLTestSrc *gltestsrc = GST_GL_TEST_SRC (bsrc);
466
467   GST_DEBUG ("setcaps");
468
469   if (!gst_video_info_from_caps (&gltestsrc->out_info, caps))
470     goto wrong_caps;
471
472   gltestsrc->negotiated = TRUE;
473
474   gst_caps_replace (&gltestsrc->out_caps, caps);
475
476   return TRUE;
477
478 /* ERRORS */
479 wrong_caps:
480   {
481     GST_WARNING ("wrong caps");
482     return FALSE;
483   }
484 }
485
486 static void
487 gst_gl_test_src_set_context (GstElement * element, GstContext * context)
488 {
489   GstGLTestSrc *src = GST_GL_TEST_SRC (element);
490
491   gst_gl_handle_set_context (element, context, &src->display);
492 }
493
494 static gboolean
495 gst_gl_test_src_query (GstBaseSrc * bsrc, GstQuery * query)
496 {
497   gboolean res = FALSE;
498   GstGLTestSrc *src;
499
500   src = GST_GL_TEST_SRC (bsrc);
501
502   switch (GST_QUERY_TYPE (query)) {
503     case GST_QUERY_CONTEXT:
504     {
505       res = gst_gl_handle_context_query ((GstElement *) src, query,
506           &src->display);
507       break;
508     }
509     case GST_QUERY_CONVERT:
510     {
511       GstFormat src_fmt, dest_fmt;
512       gint64 src_val, dest_val;
513
514       gst_query_parse_convert (query, &src_fmt, &src_val, &dest_fmt, &dest_val);
515       res =
516           gst_video_info_convert (&src->out_info, src_fmt, src_val, dest_fmt,
517           &dest_val);
518       gst_query_set_convert (query, src_fmt, src_val, dest_fmt, dest_val);
519       break;
520     }
521     default:
522       res = GST_BASE_SRC_CLASS (parent_class)->query (bsrc, query);
523   }
524
525   return res;
526 }
527
528 static void
529 gst_gl_test_src_get_times (GstBaseSrc * basesrc, GstBuffer * buffer,
530     GstClockTime * start, GstClockTime * end)
531 {
532   /* for live sources, sync on the timestamp of the buffer */
533   if (gst_base_src_is_live (basesrc)) {
534     GstClockTime timestamp = GST_BUFFER_TIMESTAMP (buffer);
535
536     if (GST_CLOCK_TIME_IS_VALID (timestamp)) {
537       /* get duration to calculate end time */
538       GstClockTime duration = GST_BUFFER_DURATION (buffer);
539
540       if (GST_CLOCK_TIME_IS_VALID (duration))
541         *end = timestamp + duration;
542       *start = timestamp;
543     }
544   } else {
545     *start = -1;
546     *end = -1;
547   }
548 }
549
550 static gboolean
551 gst_gl_test_src_do_seek (GstBaseSrc * bsrc, GstSegment * segment)
552 {
553   GstClockTime time;
554   GstGLTestSrc *src;
555
556   src = GST_GL_TEST_SRC (bsrc);
557
558   segment->time = segment->start;
559   time = segment->position;
560
561   /* now move to the time indicated */
562   if (src->out_info.fps_n) {
563     src->n_frames = gst_util_uint64_scale (time,
564         src->out_info.fps_n, src->out_info.fps_d * GST_SECOND);
565   } else
566     src->n_frames = 0;
567
568   if (src->out_info.fps_n) {
569     src->running_time = gst_util_uint64_scale (src->n_frames,
570         src->out_info.fps_d * GST_SECOND, src->out_info.fps_n);
571   } else {
572     /* FIXME : Not sure what to set here */
573     src->running_time = 0;
574   }
575
576   g_return_val_if_fail (src->running_time <= time, FALSE);
577
578   return TRUE;
579 }
580
581 static gboolean
582 gst_gl_test_src_is_seekable (GstBaseSrc * psrc)
583 {
584   /* we're seekable... */
585   return TRUE;
586 }
587
588 static gboolean
589 gst_gl_test_src_init_shader (GstGLTestSrc * gltestsrc)
590 {
591   if (gst_gl_context_get_gl_api (gltestsrc->context)) {
592     /* blocking call, wait until the opengl thread has compiled the shader */
593     if (gltestsrc->vertex_src == NULL)
594       return FALSE;
595     return gst_gl_context_gen_shader (gltestsrc->context, gltestsrc->vertex_src,
596         gltestsrc->fragment_src, &gltestsrc->shader);
597   }
598   return TRUE;
599 }
600
601 static GstFlowReturn
602 gst_gl_test_src_fill (GstPushSrc * psrc, GstBuffer * buffer)
603 {
604   GstGLTestSrc *src = GST_GL_TEST_SRC (psrc);
605   GstClockTime next_time;
606   gint width, height;
607   GstVideoFrame out_frame;
608   guint out_tex;
609   gboolean to_download =
610       gst_caps_features_is_equal (GST_CAPS_FEATURES_MEMORY_SYSTEM_MEMORY,
611       gst_caps_get_features (src->out_caps, 0));
612   GstMapFlags out_map_flags = GST_MAP_WRITE;
613
614   to_download |= !gst_is_gl_memory (gst_buffer_peek_memory (buffer, 0));
615
616   if (!to_download)
617     out_map_flags |= GST_MAP_GL;
618
619   if (G_UNLIKELY (!src->negotiated || !src->context))
620     goto not_negotiated;
621
622   width = GST_VIDEO_INFO_WIDTH (&src->out_info);
623   height = GST_VIDEO_INFO_HEIGHT (&src->out_info);
624
625   /* 0 framerate and we are at the second frame, eos */
626   if (G_UNLIKELY (GST_VIDEO_INFO_FPS_N (&src->out_info) == 0
627           && src->n_frames == 1))
628     goto eos;
629
630   if (src->pattern_type == GST_GL_TEST_SRC_BLINK) {
631     if (src->n_frames & 0x1)
632       src->make_image = gst_gl_test_src_white;
633     else
634       src->make_image = gst_gl_test_src_black;
635   }
636
637   if (!gst_video_frame_map (&out_frame, &src->out_info, buffer, out_map_flags)) {
638     return GST_FLOW_NOT_NEGOTIATED;
639   }
640
641   if (!to_download) {
642     out_tex = *(guint *) out_frame.data[0];
643   } else {
644     GST_INFO ("Output Buffer does not contain correct meta, "
645         "attempting to wrap for download");
646
647     if (!src->download)
648       src->download = gst_gl_download_new (src->context);
649
650     gst_gl_download_set_format (src->download, &out_frame.info);
651
652     if (!src->out_tex_id) {
653       gst_gl_context_gen_texture (src->context, &src->out_tex_id,
654           GST_VIDEO_FORMAT_RGBA, GST_VIDEO_FRAME_WIDTH (&out_frame),
655           GST_VIDEO_FRAME_HEIGHT (&out_frame));
656     }
657     out_tex = src->out_tex_id;
658   }
659
660   gst_buffer_replace (&src->buffer, buffer);
661
662   //blocking call, generate a FBO
663   if (!gst_gl_context_use_fbo_v2 (src->context, width, height, src->fbo,
664           src->depthbuffer, out_tex, gst_gl_test_src_callback,
665           (gpointer) src)) {
666     goto not_negotiated;
667   }
668
669   if (to_download) {
670     if (!gst_gl_download_perform_with_data (src->download, out_tex,
671             out_frame.data)) {
672       GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, ("%s",
673               "Failed to init upload format"), (NULL));
674       return FALSE;
675     }
676   }
677   gst_video_frame_unmap (&out_frame);
678
679   GST_BUFFER_TIMESTAMP (buffer) = src->timestamp_offset + src->running_time;
680   GST_BUFFER_OFFSET (buffer) = src->n_frames;
681   src->n_frames++;
682   GST_BUFFER_OFFSET_END (buffer) = src->n_frames;
683   if (src->out_info.fps_n) {
684     next_time = gst_util_uint64_scale_int (src->n_frames * GST_SECOND,
685         src->out_info.fps_d, src->out_info.fps_n);
686     GST_BUFFER_DURATION (buffer) = next_time - src->running_time;
687   } else {
688     next_time = src->timestamp_offset;
689     /* NONE means forever */
690     GST_BUFFER_DURATION (buffer) = GST_CLOCK_TIME_NONE;
691   }
692
693   src->running_time = next_time;
694
695   return GST_FLOW_OK;
696
697 not_negotiated:
698   {
699     GST_ELEMENT_ERROR (src, CORE, NEGOTIATION, (NULL),
700         (_("format wasn't negotiated before get function")));
701     return GST_FLOW_NOT_NEGOTIATED;
702   }
703 eos:
704   {
705     GST_DEBUG_OBJECT (src, "eos: 0 framerate, frame %d", (gint) src->n_frames);
706     return GST_FLOW_EOS;
707   }
708 }
709
710 static gboolean
711 gst_gl_test_src_start (GstBaseSrc * basesrc)
712 {
713   GstGLTestSrc *src = GST_GL_TEST_SRC (basesrc);
714
715   if (!gst_gl_ensure_display (src, &src->display))
716     return FALSE;
717
718   src->running_time = 0;
719   src->n_frames = 0;
720   src->negotiated = FALSE;
721
722   return TRUE;
723 }
724
725 static gboolean
726 gst_gl_test_src_stop (GstBaseSrc * basesrc)
727 {
728   GstGLTestSrc *src = GST_GL_TEST_SRC (basesrc);
729
730   gst_caps_replace (&src->out_caps, NULL);
731
732   if (src->context) {
733     if (src->shader) {
734       gst_object_unref (src->shader);
735       src->shader = NULL;
736     }
737
738     if (src->out_tex_id) {
739       gst_gl_context_del_texture (src->context, &src->out_tex_id);
740     }
741
742     if (src->download) {
743       gst_object_unref (src->download);
744       src->download = NULL;
745     }
746     //blocking call, delete the FBO
747     gst_gl_context_del_fbo (src->context, src->fbo, src->depthbuffer);
748     gst_object_unref (src->context);
749     src->context = NULL;
750   }
751
752   if (src->display) {
753     gst_object_unref (src->display);
754     src->display = NULL;
755   }
756
757   return TRUE;
758 }
759
760 static gboolean
761 gst_gl_test_src_decide_allocation (GstBaseSrc * basesrc, GstQuery * query)
762 {
763   GstGLTestSrc *src = GST_GL_TEST_SRC (basesrc);
764   GstBufferPool *pool = NULL;
765   GstStructure *config;
766   GstCaps *caps;
767   guint min, max, size;
768   gboolean update_pool;
769   GError *error = NULL;
770   guint idx;
771   guint out_width, out_height;
772   GstGLContext *other_context = NULL;
773
774   if (!gst_gl_ensure_display (src, &src->display))
775     return FALSE;
776
777   if (gst_query_find_allocation_meta (query,
778           GST_VIDEO_GL_TEXTURE_UPLOAD_META_API_TYPE, &idx)) {
779     GstGLContext *context;
780     const GstStructure *upload_meta_params;
781     gpointer handle;
782     gchar *type;
783     gchar *apis;
784
785     gst_query_parse_nth_allocation_meta (query, idx, &upload_meta_params);
786     if (upload_meta_params) {
787       if (gst_structure_get (upload_meta_params, "gst.gl.GstGLContext",
788               GST_GL_TYPE_CONTEXT, &context, NULL) && context) {
789         GstGLContext *old = src->context;
790
791         src->context = context;
792         if (old)
793           gst_object_unref (old);
794       } else if (gst_structure_get (upload_meta_params, "gst.gl.context.handle",
795               G_TYPE_POINTER, &handle, "gst.gl.context.type", G_TYPE_STRING,
796               &type, "gst.gl.context.apis", G_TYPE_STRING, &apis, NULL)
797           && handle) {
798         GstGLPlatform platform = GST_GL_PLATFORM_NONE;
799         GstGLAPI gl_apis;
800
801         GST_DEBUG ("got GL context handle 0x%p with type %s and apis %s",
802             handle, type, apis);
803
804         platform = gst_gl_platform_from_string (type);
805         gl_apis = gst_gl_api_from_string (apis);
806
807         if (gl_apis && platform)
808           other_context =
809               gst_gl_context_new_wrapped (src->display, (guintptr) handle,
810               platform, gl_apis);
811       }
812     }
813   }
814
815   if (src->other_context) {
816     if (!other_context) {
817       other_context = src->other_context;
818     } else {
819       GST_ELEMENT_WARNING (src, LIBRARY, SETTINGS,
820           ("%s", "Cannot share with more than one GL context"),
821           ("%s", "Cannot share with more than one GL context"));
822     }
823   }
824
825   if (!src->context) {
826     src->context = gst_gl_context_new (src->display);
827     if (!gst_gl_context_create (src->context, other_context, &error))
828       goto context_error;
829   }
830
831   out_width = GST_VIDEO_INFO_WIDTH (&src->out_info);
832   out_height = GST_VIDEO_INFO_HEIGHT (&src->out_info);
833
834   if (!gst_gl_context_gen_fbo (src->context, out_width, out_height,
835           &src->fbo, &src->depthbuffer))
836     goto context_error;
837
838   gst_query_parse_allocation (query, &caps, NULL);
839
840   if (gst_query_get_n_allocation_pools (query) > 0) {
841     gst_query_parse_nth_allocation_pool (query, 0, &pool, &size, &min, &max);
842
843     update_pool = TRUE;
844   } else {
845     GstVideoInfo vinfo;
846
847     gst_video_info_init (&vinfo);
848     gst_video_info_from_caps (&vinfo, caps);
849     size = vinfo.size;
850     min = max = 0;
851     update_pool = FALSE;
852   }
853
854   if (!pool)
855     pool = gst_gl_buffer_pool_new (src->context);
856
857   config = gst_buffer_pool_get_config (pool);
858   gst_buffer_pool_config_set_params (config, caps, size, min, max);
859   gst_buffer_pool_config_add_option (config, GST_BUFFER_POOL_OPTION_VIDEO_META);
860   gst_buffer_pool_set_config (pool, config);
861
862   if (update_pool)
863     gst_query_set_nth_allocation_pool (query, 0, pool, size, min, max);
864   else
865     gst_query_add_allocation_pool (query, pool, size, min, max);
866
867   gst_gl_test_src_init_shader (src);
868
869   gst_object_unref (pool);
870
871   return TRUE;
872
873 context_error:
874   {
875     GST_ELEMENT_ERROR (src, RESOURCE, NOT_FOUND, ("%s", error->message),
876         (NULL));
877     gst_object_unref (src->context);
878     src->context = NULL;
879     return FALSE;
880   }
881 }
882
883 //opengl scene
884 static void
885 gst_gl_test_src_callback (gpointer stuff)
886 {
887   GstGLTestSrc *src = GST_GL_TEST_SRC (stuff);
888
889   src->make_image (src, src->buffer, GST_VIDEO_INFO_WIDTH (&src->out_info),
890       GST_VIDEO_INFO_HEIGHT (&src->out_info));
891
892   gst_buffer_unref (src->buffer);
893   src->buffer = NULL;
894 }