From 1f557a502bc1cd0b0421124888cc0ec1def6a070 Mon Sep 17 00:00:00 2001 From: Robert Mader Date: Mon, 10 Aug 2020 14:24:58 +0200 Subject: [PATCH] cogl/context: Add API to create named pipelines In certain situations it's desirable to keep pipelines around for the whole lifetime of the session. In order to not leak them and properly clean them up on shutdown, introduce a new mechanism to create named pipelines that are bound to their correstponding context and may be used across file boundries. https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/1372 --- cogl/cogl/cogl-context-private.h | 2 ++ cogl/cogl/cogl-context.c | 30 +++++++++++++++++++++++++++++ cogl/cogl/cogl-context.h | 33 ++++++++++++++++++++++++++++++++ 3 files changed, 65 insertions(+) diff --git a/cogl/cogl/cogl-context-private.h b/cogl/cogl/cogl-context-private.h index 2d68793a0..74206a444 100644 --- a/cogl/cogl/cogl-context-private.h +++ b/cogl/cogl/cogl-context-private.h @@ -256,6 +256,8 @@ struct _CoglContext CoglPollSource *fences_poll_source; CoglList fences; + GHashTable *named_pipelines; + /* This defines a list of function pointers that Cogl uses from either GL or GLES. All functions are accessed indirectly through these pointers rather than linking to them directly */ diff --git a/cogl/cogl/cogl-context.c b/cogl/cogl/cogl-context.c index c05123580..1275439b6 100644 --- a/cogl/cogl/cogl-context.c +++ b/cogl/cogl/cogl-context.c @@ -308,6 +308,9 @@ cogl_context_new (CoglDisplay *display, _cogl_list_init (&context->fences); + context->named_pipelines = + g_hash_table_new_full (NULL, NULL, NULL, cogl_object_unref); + return context; } @@ -382,6 +385,9 @@ _cogl_context_free (CoglContext *context) cogl_object_unref (context->display); + g_hash_table_remove_all (context->named_pipelines); + g_hash_table_destroy (context->named_pipelines); + g_free (context); } @@ -472,3 +478,27 @@ cogl_context_format_supports_upload (CoglContext *ctx, { return ctx->texture_driver->format_supports_upload (ctx, format); } + +void +cogl_context_set_named_pipeline (CoglContext *context, + CoglPipelineKey *key, + CoglPipeline *pipeline) +{ + if (pipeline) + { + g_debug ("Adding named pipeline %s", *key); + g_hash_table_insert (context->named_pipelines, (gpointer) key, pipeline); + } + else + { + g_debug ("Removing named pipeline %s", *key); + g_hash_table_remove (context->named_pipelines, (gpointer) key); + } +} + +CoglPipeline * +cogl_context_get_named_pipeline (CoglContext *context, + CoglPipelineKey *key) +{ + return g_hash_table_lookup (context->named_pipelines, key); +} diff --git a/cogl/cogl/cogl-context.h b/cogl/cogl/cogl-context.h index e5882e7dd..1baa4145e 100644 --- a/cogl/cogl/cogl-context.h +++ b/cogl/cogl/cogl-context.h @@ -44,6 +44,7 @@ typedef struct _CoglContext CoglContext; #include #include +#include #include #include @@ -367,6 +368,38 @@ cogl_get_graphics_reset_status (CoglContext *context); COGL_EXPORT gboolean cogl_context_is_hardware_accelerated (CoglContext *context); +typedef const char * const CoglPipelineKey; + +/** + * cogl_context_set_named_pipeline: + * @context: a #CoglContext pointer + * @key: a #CoglPipelineKey pointer + * @pipeline: (nullable): a #CoglPipeline to associate with the @context and + * @key + * + * Associate a #CoglPipeline with a @context and @key. This will not take a new + * reference to the @pipeline, but will unref all associated pipelines when + * the @context gets destroyed. Similarly, if a pipeline gets overwritten, + * it will get unreffed as well. + */ +COGL_EXPORT void +cogl_context_set_named_pipeline (CoglContext *context, + CoglPipelineKey *key, + CoglPipeline *pipeline); + +/** + * cogl_context_get_named_pipeline: + * @context: a #CoglContext pointer + * @key: a #CoglPipelineKey pointer + * + * Return value: (transfer none): The #CoglPipeline associated with the + * given @context and @key, or %NULL if no such #CoglPipeline + * was found. + */ +COGL_EXPORT CoglPipeline * +cogl_context_get_named_pipeline (CoglContext *context, + CoglPipelineKey *key); + G_END_DECLS #endif /* __COGL_CONTEXT_H__ */