gl/api: improve the to/from string for GstGLAPI/GstGLPlatform
authorMatthew Waters <matthew@centricular.com>
Tue, 8 Sep 2020 05:53:27 +0000 (15:53 +1000)
committerMatthew Waters <matthew@centricular.com>
Thu, 13 May 2021 05:35:23 +0000 (15:35 +1000)
With unit tests now!

Part-of: <https://gitlab.freedesktop.org/gstreamer/gst-plugins-base/-/merge_requests/809>

gst-libs/gst/gl/gstglapi.c
tests/check/libs/gstglapi.c [new file with mode: 0644]
tests/check/meson.build

index 1129a32..6f5dedc 100644 (file)
@@ -107,8 +107,10 @@ gst_gl_api_from_string (const gchar * apis_s)
   GstGLAPI ret = GST_GL_API_NONE;
   gchar *apis = (gchar *) apis_s;
 
-  if (!apis || apis[0] == '\0') {
+  if (!apis || apis[0] == '\0' || g_strcmp0 (apis_s, "any") == 0) {
     ret = GST_GL_API_ANY;
+  } else if (g_strcmp0 (apis_s, "none") == 0) {
+    ret = GST_GL_API_NONE;
   } else {
     while (apis) {
       if (apis[0] == '\0') {
@@ -147,6 +149,7 @@ gchar *
 gst_gl_platform_to_string (GstGLPlatform platform)
 {
   GString *str = NULL;
+  gboolean first_set = FALSE;
   gchar *ret;
 
   if (platform == GST_GL_PLATFORM_NONE) {
@@ -159,22 +162,24 @@ gst_gl_platform_to_string (GstGLPlatform platform)
 
   str = g_string_new ("");
 
-  if (platform & GST_GL_PLATFORM_GLX) {
-    str = g_string_append (str, "glx ");
-  }
-  if (platform & GST_GL_PLATFORM_EGL) {
-    str = g_string_append (str, "egl ");
-  }
-  if (platform & GST_GL_PLATFORM_WGL) {
-    str = g_string_append (str, "wgl ");
-  }
-  if (platform & GST_GL_PLATFORM_CGL) {
-    str = g_string_append (str, "cgl ");
+#define ADD_PLATFORM(flag,str__) \
+  if (platform & flag) { \
+    if (first_set) \
+      g_string_append_c (str, ' '); \
+    str = g_string_append (str, str__); \
+    first_set = TRUE; \
   }
+  ADD_PLATFORM (GST_GL_PLATFORM_GLX, "glx");
+  ADD_PLATFORM (GST_GL_PLATFORM_EGL, "egl");
+  ADD_PLATFORM (GST_GL_PLATFORM_WGL, "wgl");
+  ADD_PLATFORM (GST_GL_PLATFORM_CGL, "cgl");
+  ADD_PLATFORM (GST_GL_PLATFORM_EAGL, "eagl");
+
+#undef ADD_PLATFORM
 
 out:
-  if (!str)
-    str = g_string_new ("unknown");
+  if (g_strcmp0 (str->str, "") == 0)
+    str = g_string_append (str, "unknown");
 
   ret = g_string_free (str, FALSE);
   return ret;
@@ -192,8 +197,10 @@ gst_gl_platform_from_string (const gchar * platform_s)
   GstGLPlatform ret = GST_GL_PLATFORM_NONE;
   gchar *platform = (gchar *) platform_s;
 
-  if (!platform || platform[0] == '\0') {
+  if (!platform || platform[0] == '\0' || g_strcmp0 (platform_s, "any") == 0) {
     ret = GST_GL_PLATFORM_ANY;
+  } else if (g_strcmp0 (platform_s, "none") == 0) {
+    ret = GST_GL_PLATFORM_NONE;
   } else {
     while (platform) {
       if (platform[0] == '\0') {
@@ -212,6 +219,9 @@ gst_gl_platform_from_string (const gchar * platform_s)
       } else if (g_strstr_len (platform, 3, "cgl")) {
         ret |= GST_GL_PLATFORM_CGL;
         platform = &platform[3];
+      } else if (g_strstr_len (platform, 4, "eagl")) {
+        ret |= GST_GL_PLATFORM_EAGL;
+        platform = &platform[4];
       } else {
         GST_ERROR ("Error parsing \'%s\'", platform);
         break;
diff --git a/tests/check/libs/gstglapi.c b/tests/check/libs/gstglapi.c
new file mode 100644 (file)
index 0000000..59bf060
--- /dev/null
@@ -0,0 +1,139 @@
+/* GStreamer
+ *
+ * Copyright (C) 2014 Matthew Waters <matthew@centricular.com>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
+ * Boston, MA 02110-1301, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#  include "config.h"
+#endif
+
+#include <gst/check/gstcheck.h>
+
+#include <gst/gl/gl.h>
+
+/* *INDENT-OFF* */
+static struct
+{
+  GstGLAPI api;
+  const gchar *str;
+} api_strings[] = {
+  {GST_GL_API_OPENGL, "opengl"},
+  {GST_GL_API_OPENGL3, "opengl3"},
+  {GST_GL_API_GLES1, "gles1"},
+  {GST_GL_API_GLES2, "gles2"},
+  {GST_GL_API_ANY, "any"},
+  {GST_GL_API_NONE, "none"},
+};
+
+static struct
+{
+  GstGLAPI api;
+  const gchar *str;
+} from_api_strings[] = {
+  {GST_GL_API_ANY, ""},
+  {GST_GL_API_ANY, NULL},
+  {GST_GL_API_NONE, "invalid-api"},
+};
+/* *INDENT-ON* */
+
+GST_START_TEST (gl_api_serialization)
+{
+  int i;
+
+  for (i = 0; i < G_N_ELEMENTS (api_strings); i++) {
+    gchar *new_str;
+
+    new_str = gst_gl_api_to_string (api_strings[i].api);
+    GST_DEBUG ("\'%s\' ?= \'%s\'", new_str, api_strings[i].str);
+    fail_unless_equals_int (0, g_strcmp0 (new_str, api_strings[i].str));
+    fail_unless_equals_int (gst_gl_api_from_string (api_strings[i].str),
+        api_strings[i].api);
+    g_free (new_str);
+  }
+
+  for (i = 0; i < G_N_ELEMENTS (from_api_strings); i++) {
+    fail_unless_equals_int (gst_gl_api_from_string (from_api_strings[i].str),
+        from_api_strings[i].api);
+  }
+}
+
+GST_END_TEST;
+
+/* *INDENT-OFF* */
+static struct
+{
+  GstGLPlatform platform;
+  const gchar *str;
+} platform_strings[] = {
+  {GST_GL_PLATFORM_GLX, "glx"},
+  {GST_GL_PLATFORM_EGL, "egl"},
+  {GST_GL_PLATFORM_WGL, "wgl"},
+  {GST_GL_PLATFORM_CGL, "cgl"},
+  {GST_GL_PLATFORM_EAGL, "eagl"},
+  {GST_GL_PLATFORM_ANY, "any"},
+  {GST_GL_PLATFORM_NONE, "none"},
+};
+
+static struct
+{
+  GstGLPlatform platform;
+  const gchar *str;
+} from_platform_strings[] = {
+  {GST_GL_PLATFORM_ANY, ""},
+  {GST_GL_PLATFORM_ANY, NULL},
+  {GST_GL_PLATFORM_NONE, "invalid-platform"},
+};
+/* *INDENT-ON* */
+
+GST_START_TEST (gl_platform_serialization)
+{
+  int i;
+
+  for (i = 0; i < G_N_ELEMENTS (platform_strings); i++) {
+    gchar *new_str;
+
+    new_str = gst_gl_platform_to_string (platform_strings[i].platform);
+    GST_DEBUG ("\'%s\' ?= \'%s\'", new_str, platform_strings[i].str);
+    fail_unless_equals_int (0, g_strcmp0 (new_str, platform_strings[i].str));
+    fail_unless_equals_int (gst_gl_platform_from_string (platform_strings
+            [i].str), platform_strings[i].platform);
+    g_free (new_str);
+  }
+
+  for (i = 0; i < G_N_ELEMENTS (from_platform_strings); i++) {
+    fail_unless_equals_int (gst_gl_platform_from_string (from_platform_strings
+            [i].str), from_platform_strings[i].platform);
+  }
+}
+
+GST_END_TEST;
+
+static Suite *
+gst_gl_color_convert_suite (void)
+{
+  Suite *s = suite_create ("GstGLAPI");
+  TCase *tc_chain = tcase_create ("api");
+
+  suite_add_tcase (s, tc_chain);
+  tcase_add_test (tc_chain, gl_platform_serialization);
+  tcase_add_test (tc_chain, gl_api_serialization);
+
+  return s;
+}
+
+GST_CHECK_MAIN (gst_gl_color_convert);
index 839f29e..834b6c3 100644 (file)
@@ -87,6 +87,7 @@ endif
 # FIXME: Unstable on Windows
 if build_gstgl and host_machine.system() != 'windows'
   base_tests += [
+    [ 'libs/gstglapi.c', not build_gstgl, [gstgl_dep]],
     [ 'libs/gstglcolorconvert.c', not build_gstgl, [gstgl_dep, gstglproto_dep]],
     [ 'libs/gstglcontext.c', not build_gstgl, [gstgl_dep, gstglproto_dep]],
     [ 'libs/gstglfeature.c', not build_gstgl, [gstgl_dep, gstglproto_dep]],