glfilters: Don't use static variables for storing per-element state
authorSebastian Dröge <sebastian@centricular.com>
Wed, 2 Oct 2019 20:21:09 +0000 (23:21 +0300)
committerMatthew Waters <matthew@centricular.com>
Thu, 3 Oct 2019 00:49:09 +0000 (00:49 +0000)
ext/gl/effects/gstgleffectblur.c
ext/gl/gstglbumper.c
ext/gl/gstglbumper.h
ext/gl/gstglfiltercube.c
ext/gl/gstglfiltercube.h
ext/gl/gstglfilterglass.c
ext/gl/gstglfilterglass.h
ext/gl/gstglmosaic.c
ext/gl/gstglmosaic.h

index 074519a..1aa03b1 100644 (file)
 
 #include "../gstgleffects.h"
 
+static gpointer
+init_kernel (gpointer data)
+{
+  float *kernel = g_malloc (sizeof (gfloat) * 9);
+  fill_gaussian_kernel (kernel, 7, 3.f);
+  return kernel;
+}
+
 static float *
 gst_gl_effects_blur_kernel (void)
 {
   /* gaussian kernel (well, actually vector), size 9, standard
    * deviation 3.0 */
   /* FIXME: make this a runtime property */
-  static gfloat *kernel = NULL;
-  if (G_UNLIKELY (NULL == kernel)) {
-    /* 3x3 matrix */
-    kernel = g_malloc (sizeof (gfloat) * 9);
-    fill_gaussian_kernel (kernel, 7, 3.f);
-  }
-  return kernel;
+  static GOnce my_once = G_ONCE_INIT;
+
+  g_once (&my_once, init_kernel, NULL);
+  return my_once.retval;
 }
 
 void
index 2b7bd6e..dbf01ba 100644 (file)
@@ -321,6 +321,9 @@ gst_gl_bumper_reset (GstGLFilter * filter)
   if (bumper_filter->shader)
     gst_gl_context_del_shader (filter->context, bumper_filter->shader);
   bumper_filter->shader = NULL;
+  bumper_filter->xrot = 0.0;
+  bumper_filter->yrot = 0.0;
+  bumper_filter->zrot = 0.0;
 }
 
 static void
@@ -399,10 +402,6 @@ typedef struct _MeshData
 static void
 gst_gl_bumper_callback (gint width, gint height, guint texture, gpointer stuff)
 {
-  static GLfloat xrot = 0;
-  static GLfloat yrot = 0;
-  static GLfloat zrot = 0;
-
   GstGLFuncs *gl;
   GstGLBumper *bumper = GST_GL_BUMPER (stuff);
   GstGLContext *context = GST_GL_FILTER (bumper)->context;
@@ -501,9 +500,9 @@ gst_gl_bumper_callback (gint width, gint height, guint texture, gpointer stuff)
   gst_gl_shader_set_uniform_1i (bumper->shader, "texture0", 0);
   gl->BindTexture (GL_TEXTURE_2D, texture);
 
-  gl->Rotatef (xrot, 1.0f, 0.0f, 0.0f);
-  gl->Rotatef (yrot, 0.0f, 1.0f, 0.0f);
-  gl->Rotatef (zrot, 0.0f, 0.0f, 1.0f);
+  gl->Rotatef (bumper->xrot, 1.0f, 0.0f, 0.0f);
+  gl->Rotatef (bumper->yrot, 0.0f, 1.0f, 0.0f);
+  gl->Rotatef (bumper->zrot, 0.0f, 0.0f, 1.0f);
 
   gl->EnableVertexAttribArray (locTangent);
 
@@ -540,7 +539,7 @@ gst_gl_bumper_callback (gint width, gint height, guint texture, gpointer stuff)
   gl->Disable (GL_LIGHTING);
   gl->Disable (GL_COLOR_MATERIAL);
 
-  xrot += 1.0f;
-  yrot += 0.9f;
-  zrot += 0.6f;
+  bumper->xrot += 1.0f;
+  bumper->yrot += 0.9f;
+  bumper->zrot += 0.6f;
 }
index b1a1dc0..b59fea3 100644 (file)
@@ -43,6 +43,7 @@ struct _GstGLBumper
   gint bumpmap_width;
   gint bumpmap_height;
   gchar *location;
+  GLfloat xrot, yrot, zrot;
 };
 
 struct _GstGLBumperClass
index c0ea031..d82c37d 100644 (file)
@@ -311,6 +311,10 @@ gst_gl_filter_cube_gl_start (GstGLBaseFilter * filter)
   gchar *frag_str;
   gboolean ret;
 
+  cube_filter->xrot = 0.0;
+  cube_filter->yrot = 0.0;
+  cube_filter->zrot = 0.0;
+
   frag_str =
       g_strdup_printf ("%s%s",
       gst_gl_shader_string_get_highest_precision (context,
@@ -433,10 +437,6 @@ _callback (gpointer stuff)
   GstGLFilterCube *cube_filter = GST_GL_FILTER_CUBE (filter);
   GstGLFuncs *gl = GST_GL_BASE_FILTER (filter)->context->gl_vtable;
 
-  static GLfloat xrot = 0;
-  static GLfloat yrot = 0;
-  static GLfloat zrot = 0;
-
   const GLfloat matrix[] = {
     0.5f, 0.0f, 0.0f, 0.0f,
     0.0f, 0.5f, 0.0f, 0.0f,
@@ -454,9 +454,12 @@ _callback (gpointer stuff)
   gl->ActiveTexture (GL_TEXTURE0);
   gl->BindTexture (GL_TEXTURE_2D, cube_filter->in_tex->tex_id);
   gst_gl_shader_set_uniform_1i (cube_filter->shader, "s_texture", 0);
-  gst_gl_shader_set_uniform_1f (cube_filter->shader, "xrot_degree", xrot);
-  gst_gl_shader_set_uniform_1f (cube_filter->shader, "yrot_degree", yrot);
-  gst_gl_shader_set_uniform_1f (cube_filter->shader, "zrot_degree", zrot);
+  gst_gl_shader_set_uniform_1f (cube_filter->shader, "xrot_degree",
+      cube_filter->xrot);
+  gst_gl_shader_set_uniform_1f (cube_filter->shader, "yrot_degree",
+      cube_filter->yrot);
+  gst_gl_shader_set_uniform_1f (cube_filter->shader, "zrot_degree",
+      cube_filter->zrot);
   gst_gl_shader_set_uniform_matrix_4fv (cube_filter->shader, "u_matrix", 1,
       GL_FALSE, matrix);
 
@@ -498,9 +501,9 @@ _callback (gpointer stuff)
 
   gl->Disable (GL_DEPTH_TEST);
 
-  xrot += 0.3f;
-  yrot += 0.2f;
-  zrot += 0.4f;
+  cube_filter->xrot += 0.3f;
+  cube_filter->yrot += 0.2f;
+  cube_filter->zrot += 0.4f;
 
   return TRUE;
 }
index 2dcd6c5..cb8830f 100644 (file)
@@ -59,6 +59,8 @@ struct _GstGLFilterCube
     GLuint             vertex_buffer;
     GLint              attr_position;
     GLint              attr_texture;
+
+    GLfloat            xrot, yrot, zrot;
 };
 
 struct _GstGLFilterCubeClass
index 31f63b5..fef81fc 100644 (file)
@@ -193,6 +193,8 @@ gst_gl_filter_glass_reset (GstBaseTransform * trans)
     gst_object_unref (glass_filter->passthrough_shader);
   glass_filter->passthrough_shader = NULL;
 
+  glass_filter->start_time = 0;
+
   return GST_BASE_TRANSFORM_CLASS (parent_class)->stop (trans);
 }
 
@@ -258,10 +260,7 @@ gst_gl_filter_glass_filter_texture (GstGLFilter * filter, GstGLMemory * in_tex,
 static gint64
 get_time (void)
 {
-  static GTimeVal val;
-  g_get_current_time (&val);
-
-  return (val.tv_sec * G_USEC_PER_SEC) + val.tv_usec;
+  return g_get_real_time ();
 }
 
 static void
@@ -356,7 +355,6 @@ gst_gl_filter_glass_draw_video_plane (GstGLFilter * filter,
 static gboolean
 gst_gl_filter_glass_callback (gpointer stuff)
 {
-  static gint64 start_time = 0;
   gfloat rotation;
 
   GstGLFilter *filter = GST_GL_FILTER (stuff);
@@ -367,11 +365,12 @@ gst_gl_filter_glass_callback (gpointer stuff)
   gint height = GST_VIDEO_INFO_HEIGHT (&filter->out_info);
   guint texture = glass_filter->in_tex->tex_id;
 
-  if (start_time == 0)
-    start_time = get_time ();
+  if (glass_filter->start_time == 0)
+    glass_filter->start_time = get_time ();
   else {
     gint64 time_left =
-        (glass_filter->timestamp / 1000) - (get_time () - start_time);
+        (glass_filter->timestamp / 1000) - (get_time () -
+        glass_filter->start_time);
     time_left -= 1000000 / 25;
     if (time_left > 2000) {
       GST_LOG ("escape");
@@ -384,8 +383,8 @@ gst_gl_filter_glass_callback (gpointer stuff)
   gst_gl_filter_glass_draw_background_gradient (glass_filter);
 
   //Rotation
-  if (start_time != 0) {
-    gint64 time_passed = get_time () - start_time;
+  if (glass_filter->start_time != 0) {
+    gint64 time_passed = get_time () - glass_filter->start_time;
     rotation = sin (time_passed / 1200000.0) * 45.0f;
   } else {
     rotation = 0.0f;
index 1f1c91d..bfbc6f2 100644 (file)
@@ -43,6 +43,7 @@ struct _GstGLFilterGlass
   gint64 timestamp;
   GstGLMemory *in_tex;
   GstGLMemory *out_tex;
+  gint64 start_time;
 };
 
 struct _GstGLFilterGlassClass
index 7d1851a..baf5cfc 100644 (file)
@@ -194,6 +194,9 @@ gst_gl_mosaic_reset (GstGLMixer * mixer)
 
   mosaic->attr_position_loc = -1;
   mosaic->attr_texture_loc = -1;
+  mosaic->xrot = 0.0;
+  mosaic->yrot = 0.0;
+  mosaic->zrot = 0.0;
 }
 
 static gboolean
@@ -284,10 +287,6 @@ gst_gl_mosaic_callback (gpointer stuff)
   GstGLFuncs *gl = GST_GL_BASE_MIXER (mixer)->context->gl_vtable;
   GList *walk;
 
-  static GLfloat xrot = 0;
-  static GLfloat yrot = 0;
-  static GLfloat zrot = 0;
-
   const GLfloat matrix[] = {
     0.5f, 0.0f, 0.0f, 0.0f,
     0.0f, 0.5f, 0.0f, 0.0f,
@@ -317,9 +316,9 @@ gst_gl_mosaic_callback (gpointer stuff)
   }
 
   gst_gl_shader_set_uniform_1i (mosaic->shader, "s_texture", 0);
-  gst_gl_shader_set_uniform_1f (mosaic->shader, "xrot_degree", xrot);
-  gst_gl_shader_set_uniform_1f (mosaic->shader, "yrot_degree", yrot);
-  gst_gl_shader_set_uniform_1f (mosaic->shader, "zrot_degree", zrot);
+  gst_gl_shader_set_uniform_1f (mosaic->shader, "xrot_degree", mosaic->xrot);
+  gst_gl_shader_set_uniform_1f (mosaic->shader, "yrot_degree", mosaic->yrot);
+  gst_gl_shader_set_uniform_1f (mosaic->shader, "zrot_degree", mosaic->zrot);
   gst_gl_shader_set_uniform_matrix_4fv (mosaic->shader, "u_matrix", 1,
       GL_FALSE, matrix);
 
@@ -442,9 +441,9 @@ gst_gl_mosaic_callback (gpointer stuff)
 
   gst_gl_context_clear_shader (GST_GL_BASE_MIXER (mixer)->context);
 
-  xrot += 0.6f;
-  yrot += 0.4f;
-  zrot += 0.8f;
+  mosaic->xrot += 0.6f;
+  mosaic->yrot += 0.4f;
+  mosaic->zrot += 0.8f;
 
   return TRUE;
 }
index 87966e9..6bdb249 100644 (file)
@@ -46,6 +46,7 @@ struct _GstGLMosaic
     gint attr_position_loc;
     gint attr_texture_loc;
     GstGLMemory *out_tex;
+    GLfloat xrot, yrot, zrot;
 };
 
 struct _GstGLMosaicClass