From 65533b807c9f77fdcc141415ecfd5ac778e7b0d8 Mon Sep 17 00:00:00 2001 From: Matthew Waters Date: Fri, 8 Jan 2016 18:36:54 +1100 Subject: [PATCH] gl: add a gstglquery object for arbitrary GL queries Currently only GL_TIME_ELAPSED and GL_TIMESTAMP are supported --- gst-libs/gst/gl/Makefile.am | 2 + gst-libs/gst/gl/gl.h | 1 + gst-libs/gst/gl/glprototypes/Makefile.am | 3 +- gst-libs/gst/gl/glprototypes/all_functions.h | 1 + gst-libs/gst/gl/glprototypes/query.h | 63 +++++++ gst-libs/gst/gl/gstgl_fwd.h | 2 + gst-libs/gst/gl/gstglquery.c | 265 +++++++++++++++++++++++++++ gst-libs/gst/gl/gstglquery.h | 140 ++++++++++++++ tests/check/libs/gstglquery.c | 150 +++++++++++++++ 9 files changed, 626 insertions(+), 1 deletion(-) create mode 100644 gst-libs/gst/gl/glprototypes/query.h create mode 100644 gst-libs/gst/gl/gstglquery.c create mode 100644 gst-libs/gst/gl/gstglquery.h create mode 100644 tests/check/libs/gstglquery.c diff --git a/gst-libs/gst/gl/Makefile.am b/gst-libs/gst/gl/Makefile.am index edc1370..fcf788f 100644 --- a/gst-libs/gst/gl/Makefile.am +++ b/gst-libs/gst/gl/Makefile.am @@ -34,6 +34,7 @@ libgstgl_@GST_API_VERSION@_la_SOURCES = \ gstglsyncmeta.c \ gstglviewconvert.c \ gstgloverlaycompositor.c \ + gstglquery.c \ gstglsl_private.h \ utils/opengl_versions.h @@ -64,6 +65,7 @@ libgstgl_@GST_API_VERSION@include_HEADERS = \ gstglsyncmeta.h \ gstglviewconvert.h \ gstgloverlaycompositor.h \ + gstglquery.h \ gstgl_fwd.h \ gstgl_enums.h \ gl.h diff --git a/gst-libs/gst/gl/gl.h b/gst-libs/gst/gl/gl.h index dd54565..75a1563 100644 --- a/gst-libs/gst/gl/gl.h +++ b/gst-libs/gst/gl/gl.h @@ -52,5 +52,6 @@ #include #include #include +#include #endif /* __GST_GL_H__ */ diff --git a/gst-libs/gst/gl/glprototypes/Makefile.am b/gst-libs/gst/gl/glprototypes/Makefile.am index 6ea90db..54ebc27 100644 --- a/gst-libs/gst/gl/glprototypes/Makefile.am +++ b/gst-libs/gst/gl/glprototypes/Makefile.am @@ -15,5 +15,6 @@ prototype_HEADERS = \ debug.h \ vao.h \ sync.h \ - buffers.h + buffers.h \ + query.h diff --git a/gst-libs/gst/gl/glprototypes/all_functions.h b/gst-libs/gst/gl/glprototypes/all_functions.h index c6af415..9f7e4f1 100644 --- a/gst-libs/gst/gl/glprototypes/all_functions.h +++ b/gst-libs/gst/gl/glprototypes/all_functions.h @@ -30,3 +30,4 @@ #include "vao.h" #include "sync.h" #include "buffers.h" +#include "query.h" diff --git a/gst-libs/gst/gl/glprototypes/query.h b/gst-libs/gst/gl/glprototypes/query.h new file mode 100644 index 0000000..09f1ba2 --- /dev/null +++ b/gst-libs/gst/gl/glprototypes/query.h @@ -0,0 +1,63 @@ +/* + * GStreamer + * Copyright (C) 2016 Matthew Waters + * + * 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. + */ + +GST_GL_EXT_BEGIN (timer_query, + GST_GL_API_OPENGL3, + 3, 3, + 3, 0, + "ARB:\0ANGLE\0EXT\0", + "timer_query\0disjoint_timer_query\0") +GST_GL_EXT_FUNCTION (void, GenQueries, + (GLsizei n, + GLuint *ids)) +GST_GL_EXT_FUNCTION (void, DeleteQueries, + (GLsizei n, + GLuint *ids)) +GST_GL_EXT_FUNCTION (GLboolean, IsQuery, + (GLuint id)) +GST_GL_EXT_FUNCTION (void, BeginQuery, + (GLenum target, + GLuint id)) +GST_GL_EXT_FUNCTION (void, EndQuery, + (GLenum target)) +GST_GL_EXT_FUNCTION (void, QueryCounter, + (GLuint id, + GLenum target)) +GST_GL_EXT_FUNCTION (void, GetQueryiv, + (GLenum target, + GLenum pname, + GLint *params)) +GST_GL_EXT_FUNCTION (void, GetQueryObjectiv, + (GLuint id, + GLenum pname, + GLint *params)) +GST_GL_EXT_FUNCTION (void, GetQueryObjectuiv, + (GLuint id, + GLenum pname, + GLuint *params)) +GST_GL_EXT_FUNCTION (void, GetQueryObjecti64v, + (GLuint id, + GLenum pname, + GLint64 *params)) +GST_GL_EXT_FUNCTION (void, GetQueryObjectui64v, + (GLuint id, + GLenum pname, + GLuint64 *params)) +GST_GL_EXT_END () diff --git a/gst-libs/gst/gl/gstgl_fwd.h b/gst-libs/gst/gl/gstgl_fwd.h index 2a8a798..fb64ff6 100644 --- a/gst-libs/gst/gl/gstgl_fwd.h +++ b/gst-libs/gst/gl/gstgl_fwd.h @@ -89,6 +89,8 @@ typedef struct _GstGLViewConvertPrivate GstGLViewConvertPrivate; typedef struct _GstGLOverlayCompositor GstGLOverlayCompositor; typedef struct _GstGLOverlayCompositorClass GstGLOverlayCompositorClass; +typedef struct _GstGLQuery GstGLQuery; + #include #ifdef G_DEFINE_AUTOPTR_CLEANUP_FUNC diff --git a/gst-libs/gst/gl/gstglquery.c b/gst-libs/gst/gl/gstglquery.c new file mode 100644 index 0000000..4e8378f --- /dev/null +++ b/gst-libs/gst/gl/gstglquery.c @@ -0,0 +1,265 @@ +/* + * GStreamer + * Copyright (C) 2016 Matthew Waters + * + * 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 + +#include + +#include "gstglquery.h" + +#ifndef GL_TIME_ELAPSED +#define GL_TIME_ELAPSED 0x88BF +#endif + +#ifndef GL_TIMESTAMP +#define GL_TIMESTAMP 0x8E28 +#endif + +#define GST_CAT_DEFAULT gst_gl_query_debug +GST_DEBUG_CATEGORY_STATIC (GST_CAT_DEFAULT); + +static void +_init_debug (void) +{ + static volatile gsize _init = 0; + + if (g_once_init_enter (&_init)) { + GST_DEBUG_CATEGORY_INIT (GST_CAT_DEFAULT, "glquery", 0, "glquery element"); + g_once_init_leave (&_init, 1); + } +} + +static const gchar * +_query_type_to_string (guint query_type) +{ + switch (query_type) { + case GST_GL_QUERY_TIME_ELAPSED: + case GL_TIME_ELAPSED: + return "time elapsed"; + case GL_TIMESTAMP: + case GST_GL_QUERY_TIMESTAMP: + return "timestamp"; + default: + return "unknown"; + } +} + +static guint +_gst_gl_query_type_to_gl (GstGLQueryType query_type) +{ + if (query_type == GST_GL_QUERY_TIME_ELAPSED) + return GL_TIME_ELAPSED; + if (query_type == GST_GL_QUERY_TIMESTAMP) + return GL_TIMESTAMP; + + g_assert_not_reached (); + return 0; +} + +static gboolean +_query_type_supports_counter (guint gl_query_type) +{ + return gl_query_type == GL_TIMESTAMP; +} + +static gboolean +_query_type_supports_begin_end (guint gl_query_type) +{ + return gl_query_type == GL_TIME_ELAPSED; +} + +static gboolean +_context_supports_query_type (GstGLContext * context, guint gl_query_type) +{ + return gl_query_type != 0 && context->gl_vtable->GenQueries != NULL; +} + +static gchar * +_log_time (gpointer user_data) +{ + GstGLQuery *query = user_data; + gint64 result; + + result = gst_gl_query_result (query); + + return gst_info_strdup_printf ("%" GST_TIME_FORMAT, GST_TIME_ARGS (result)); +} + +void +gst_gl_query_init (GstGLQuery * query, GstGLContext * context, + GstGLQueryType query_type) +{ + const GstGLFuncs *gl; + + g_return_if_fail (query != NULL); + g_return_if_fail (GST_IS_GL_CONTEXT (context)); + gl = context->gl_vtable; + + memset (query, 0, sizeof (*query)); + + _init_debug (); + + query->context = gst_object_ref (context); + query->query_type = _gst_gl_query_type_to_gl (query_type); + query->supported = _context_supports_query_type (context, query->query_type); + + if (query->supported) + gl->GenQueries (1, &query->query_id); + + gst_gl_async_debug_init (&query->debug); + query->debug.callback = _log_time; + query->debug.user_data = query; +} + +void +gst_gl_query_unset (GstGLQuery * query) +{ + const GstGLFuncs *gl; + + g_return_if_fail (query != NULL); + if (query->start_called) + g_critical ("Unsetting a running query. This may not be what you wanted." + "Be sure to pair calls to gst_gl_query_start() and gst_gl_query_end()"); + + GST_TRACE ("%p unsetting query %u", query, query->query_id); + + gl = query->context->gl_vtable; + + /* unset the debug object as it may callback to print the last message */ + gst_gl_async_debug_unset (&query->debug); + + if (query->query_id) + gl->DeleteQueries (1, &query->query_id); + + if (query->context) + gst_object_unref (query->context); +} + +GstGLQuery * +gst_gl_query_new (GstGLContext * context, GstGLQueryType query_type) +{ + GstGLQuery *query = g_new0 (GstGLQuery, 1); + + gst_gl_query_init (query, context, query_type); + + return query; +} + +void +gst_gl_query_free (GstGLQuery * query) +{ + g_return_if_fail (query != NULL); + + gst_gl_query_unset (query); + g_free (query); +} + +void +gst_gl_query_start (GstGLQuery * query) +{ + const GstGLFuncs *gl; + + g_return_if_fail (query != NULL); + g_return_if_fail (_query_type_supports_begin_end (query->query_type)); + + if (!query->supported) + return; + + query->start_called = TRUE; + gst_gl_async_debug_output_log_msg (&query->debug); + + GST_TRACE ("%p start query type \'%s\' id %u", query, + _query_type_to_string (query->query_type), query->query_id); + + gl = query->context->gl_vtable; + gl->BeginQuery (query->query_type, query->query_id); +} + +void +gst_gl_query_end (GstGLQuery * query) +{ + const GstGLFuncs *gl; + + g_return_if_fail (query != NULL); + g_return_if_fail (_query_type_supports_begin_end (query->query_type)); + g_return_if_fail (query->start_called); + + if (!query->supported) + return; + + GST_TRACE ("%p end query type \'%s\' id %u", query, + _query_type_to_string (query->query_type), query->query_id); + + gl = query->context->gl_vtable; + + gl->EndQuery (query->query_type); + query->start_called = FALSE; +} + +void +gst_gl_query_counter (GstGLQuery * query) +{ + const GstGLFuncs *gl; + + g_return_if_fail (query != NULL); + g_return_if_fail (_query_type_supports_counter (query->query_type)); + + if (!query->supported) + return; + + GST_TRACE ("%p query counter type \'%s\' id %u", query, + _query_type_to_string (query->query_type), query->query_id); + + gst_gl_async_debug_output_log_msg (&query->debug); + + gl = query->context->gl_vtable; + gl->QueryCounter (query->query_id, query->query_type); +} + +guint64 +gst_gl_query_result (GstGLQuery * query) +{ + const GstGLFuncs *gl; + guint64 ret; + + g_return_val_if_fail (query != NULL, 0); + g_return_val_if_fail (!query->start_called, 0); + + if (!query->supported) + return 0; + + gl = query->context->gl_vtable; + if (gl->GetQueryObjectui64v) { + gl->GetQueryObjectui64v (query->query_id, GL_QUERY_RESULT, &ret); + } else { + guint tmp; + gl->GetQueryObjectuiv (query->query_id, GL_QUERY_RESULT, &tmp); + ret = tmp; + } + + GST_TRACE ("%p get result %" G_GUINT64_FORMAT " type \'%s\' id %u", query, + ret, _query_type_to_string (query->query_type), query->query_id); + + return ret; +} diff --git a/gst-libs/gst/gl/gstglquery.h b/gst-libs/gst/gl/gstglquery.h new file mode 100644 index 0000000..9cf06fa --- /dev/null +++ b/gst-libs/gst/gl/gstglquery.h @@ -0,0 +1,140 @@ +/* + * GStreamer + * Copyright (C) 2016 Matthew Waters + * + * 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. + */ + +#ifndef __GST_GL_QUERY_H__ +#define __GST_GL_QUERY_H__ + +#include + +G_BEGIN_DECLS + +typedef enum +{ + GST_GL_QUERY_NONE, + GST_GL_QUERY_TIME_ELAPSED, + GST_GL_QUERY_TIMESTAMP, +} GstGLQueryType; + +struct _GstGLQuery +{ + /* */ + GstGLContext * context; + guint query_type; + guint query_id; + gboolean supported; + + gboolean start_called; + GstGLAsyncDebug debug; +}; + +void gst_gl_query_init (GstGLQuery * query, + GstGLContext * context, + GstGLQueryType query_type); +void gst_gl_query_unset (GstGLQuery * query); +GstGLQuery * gst_gl_query_new (GstGLContext * context, + GstGLQueryType query_type); +void gst_gl_query_free (GstGLQuery * query); + +void gst_gl_query_start (GstGLQuery * query); +void gst_gl_query_end (GstGLQuery * query); +void gst_gl_query_counter (GstGLQuery * query); +guint64 gst_gl_query_result (GstGLQuery * query); + +#define gst_gl_query_start_log_valist(query,cat,level,object,format,varargs) \ + G_STMT_START { \ + GST_GL_ASYNC_CAT_LEVEL_LOG_valist (&(query)->debug, cat, level, object, format, varargs); \ + gst_gl_async_debug_freeze (&(query)->debug); \ + gst_gl_query_start (query); \ + gst_gl_async_debug_thaw (&(query)->debug); \ + } G_STMT_END + +#define gst_gl_query_counter_log_valist(query,cat,level,object,format,varargs) \ + G_STMT_START { \ + GST_GL_ASYNC_CAT_LEVEL_LOG_valist (&(query)->debug, cat, level, object, format, varargs); \ + gst_gl_async_debug_freeze (&(query)->debug); \ + gst_gl_query_counter (query); \ + gst_gl_async_debug_thaw (&(query)->debug); \ + } G_STMT_END + +#if G_HAVE_ISO_VARARGS + +#define gst_gl_query_start_log(query,cat,level,object,format,...) \ + G_STMT_START { \ + GST_GL_ASYNC_CAT_LEVEL_LOG (&(query)->debug, cat, level, object, format, __VA_ARGS__); \ + gst_gl_async_debug_freeze (&(query)->debug); \ + gst_gl_query_start (query); \ + gst_gl_async_debug_thaw (&(query)->debug); \ + } G_STMT_END +#define gst_gl_query_counter_log(query,cat,level,object,format,...) \ + G_STMT_START { \ + GST_GL_ASYNC_CAT_LEVEL_LOG (&(query)->debug, cat, level, object, format, __VA_ARGS__); \ + gst_gl_async_debug_freeze (&(query)->debug); \ + gst_gl_query_counter (query); \ + gst_gl_async_debug_thaw (&(query)->debug); \ + } G_STMT_END + +#else /* G_HAVE_ISO_VARARGS */ +#if G_HAVE_GNUC_VARARGS + +#define gst_gl_query_start_log(query,cat,level,object,format,args...) \ + G_STMT_START { \ + GST_GL_ASYNC_CAT_LEVEL_LOG (&(query)->debug, cat, level, object, format, ##args); \ + gst_gl_async_debug_freeze (&(query)->debug); \ + gst_gl_query_start (query); \ + gst_gl_async_debug_thaw (&(query)->debug); \ + } G_STMT_END +#define gst_gl_query_counter_log(query,cat,level,object,format,args...) \ + G_STMT_START { \ + GST_GL_ASYNC_CAT_LEVEL_LOG (&(query)->debug, cat, level, object, format, ##args); \ + gst_gl_async_debug_freeze (&(query)->debug); \ + gst_gl_query_counter (query); \ + gst_gl_async_debug_thaw (&(query)->debug); \ + } G_STMT_END + +#else /* G_HAVE_GNUC_VARARGS */ + +static inline void +gst_gl_query_start_log(GstGLQuery * query, GstDebugCategory * cat, + GstDebugLevel level, GObject * object, const gchar * format, ...) +{ + va_list varargs; + + va_start (varargs, format); + gst_gl_query_start_log_valist (query, cat, level, object, format, varargs); + va_end (varargs); +} + +static inline void +gst_gl_query_counter_log(GstGLQuery * query, GstDebugCategory * cat, + GstDebugLevel level, GObject * object, const gchar * format, ...) +{ + va_list varargs; + + va_start (varargs, format); + gst_gl_query_counter_log_valist (query, cat, level, object, format, varargs); + va_end (varargs); +} + +#endif /* G_HAVE_GNUC_VARARGS */ +#endif /* G_HAVE_ISO_VARARGS */ + +G_END_DECLS + +#endif /* __GST_GL_QUERY_H__ */ diff --git a/tests/check/libs/gstglquery.c b/tests/check/libs/gstglquery.c new file mode 100644 index 0000000..2dfa240 --- /dev/null +++ b/tests/check/libs/gstglquery.c @@ -0,0 +1,150 @@ +/* GStreamer + * Copyright (C) 2016 Matthew Waters + * + * 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 + +#include + +#include + +static GstGLDisplay *display; +static GstGLContext *context; + +GST_DEBUG_CATEGORY_STATIC (gst_test_debug_cat); + +static void +setup (void) +{ + GError *error = NULL; + + display = gst_gl_display_new (); + context = gst_gl_context_new (display); + + gst_gl_context_create (context, NULL, &error); + + fail_if (error != NULL, "Error creating context: %s\n", + error ? error->message : "Unknown Error"); +} + +static void +teardown (void) +{ + gst_object_unref (display); + gst_object_unref (context); +} + +static void +_test_query_gl (GstGLContext * context, gpointer data) +{ + GstGLQuery *q1, q2; + + q1 = gst_gl_query_new (context, GST_GL_QUERY_TIME_ELAPSED); + fail_if (q1 == NULL); + + gst_gl_query_start_log (q1, NULL, GST_LEVEL_ERROR, NULL, "%s", + "1. testing query proxy-logging"); + gst_gl_query_end (q1); + /* GST_GL_QUERY_TIME_ELAPSED doesn't supported counter() */ + ASSERT_CRITICAL (gst_gl_query_counter (q1)); + gst_gl_query_result (q1); + + gst_gl_query_free (q1); + + gst_gl_query_init (&q2, context, GST_GL_QUERY_TIMESTAMP); + + /* GST_GL_QUERY_TIMESTAMP doesn't supported start()/end() */ + ASSERT_CRITICAL (gst_gl_query_start (&q2)); + ASSERT_CRITICAL (gst_gl_query_end (&q2)); + + gst_gl_query_counter_log (&q2, gst_test_debug_cat, GST_LEVEL_ERROR, NULL, + "%s", "2. testing query proxy-logging works from _unset()"); + gst_gl_query_result (&q2); + + gst_gl_query_unset (&q2); + + /* no usage */ + gst_gl_query_init (&q2, context, GST_GL_QUERY_TIMESTAMP); + gst_gl_query_unset (&q2); + + /* test mismatched start()/free() */ + q1 = gst_gl_query_new (context, GST_GL_QUERY_TIME_ELAPSED); + fail_if (q1 == NULL); + + gst_gl_query_start (q1); + + ASSERT_CRITICAL (gst_gl_query_free (q1)); + + /* test mismatched start()/result() */ + q1 = gst_gl_query_new (context, GST_GL_QUERY_TIME_ELAPSED); + fail_if (q1 == NULL); + + gst_gl_query_start (q1); + ASSERT_CRITICAL (gst_gl_query_result (q1)); + gst_gl_query_end (q1); + + gst_gl_query_free (q1); + + /* test mismatched end() */ + q1 = gst_gl_query_new (context, GST_GL_QUERY_TIME_ELAPSED); + fail_if (q1 == NULL); + ASSERT_CRITICAL (gst_gl_query_end (q1)); + gst_gl_query_free (q1); + + /* test double end() */ + q1 = gst_gl_query_new (context, GST_GL_QUERY_TIME_ELAPSED); + fail_if (q1 == NULL); + + gst_gl_query_start (q1); + gst_gl_query_end (q1); + ASSERT_CRITICAL (gst_gl_query_end (q1)); + + gst_gl_query_free (q1); + + /* double start is allowed */ +} + +GST_START_TEST (test_query) +{ + gst_gl_context_thread_add (context, (GstGLContextThreadFunc) _test_query_gl, + NULL); +} + +GST_END_TEST; + +static Suite * +gst_gl_upload_suite (void) +{ + Suite *s = suite_create ("GstGLQuery"); + TCase *tc_chain = tcase_create ("glquery"); + + GST_DEBUG_CATEGORY_INIT (gst_test_debug_cat, "test-debug", 0, + "proxy-logging test debug"); + + suite_add_tcase (s, tc_chain); + tcase_add_checked_fixture (tc_chain, setup, teardown); + tcase_add_test (tc_chain, test_query); + + return s; +} + +GST_CHECK_MAIN (gst_gl_upload); -- 2.7.4