cogl: Port Context away from CoglObject

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3193>
This commit is contained in:
Bilal Elmoussaoui 2023-08-17 12:08:32 +02:00 committed by Marge Bot
parent 0f54700101
commit 1da42dc3c0
9 changed files with 147 additions and 151 deletions

View File

@ -56,7 +56,7 @@ free_priv (gpointer data)
{ {
CoglPangoFontMapPriv *priv = data; CoglPangoFontMapPriv *priv = data;
cogl_object_unref (priv->ctx); g_object_unref (priv->ctx);
g_object_unref (priv->renderer); g_object_unref (priv->renderer);
g_free (priv); g_free (priv);
@ -70,7 +70,7 @@ cogl_pango_font_map_new (void)
_COGL_GET_CONTEXT (context, NULL); _COGL_GET_CONTEXT (context, NULL);
priv->ctx = cogl_object_ref (context); priv->ctx = g_object_ref (context);
/* XXX: The public pango api doesn't let us sub-class /* XXX: The public pango api doesn't let us sub-class
* PangoCairoFontMap so we attach our own private data using qdata * PangoCairoFontMap so we attach our own private data using qdata

View File

@ -78,7 +78,7 @@ _cogl_pango_pipeline_cache_new (CoglContext *ctx,
{ {
CoglPangoPipelineCache *cache = g_new (CoglPangoPipelineCache, 1); CoglPangoPipelineCache *cache = g_new (CoglPangoPipelineCache, 1);
cache->ctx = cogl_object_ref (ctx); cache->ctx = g_object_ref (ctx);
/* The key is the pipeline pointer. A reference is taken when the /* The key is the pipeline pointer. A reference is taken when the
pipeline is used as a key so we should unref it again in the pipeline is used as a key so we should unref it again in the
@ -234,7 +234,7 @@ _cogl_pango_pipeline_cache_free (CoglPangoPipelineCache *cache)
g_hash_table_destroy (cache->hash_table); g_hash_table_destroy (cache->hash_table);
cogl_object_unref (cache->ctx); g_object_unref (cache->ctx);
g_free (cache); g_free (cache);
} }

View File

@ -238,18 +238,17 @@ _cogl_pango_renderer_constructed (GObject *gobject)
} }
static void static void
cogl_pango_renderer_set_property (GObject *object, cogl_pango_renderer_set_property (GObject *object,
unsigned int prop_id, unsigned int prop_id,
const GValue *value, const GValue *value,
GParamSpec *pspec) GParamSpec *pspec)
{ {
CoglPangoRenderer *renderer = COGL_PANGO_RENDERER (object); CoglPangoRenderer *renderer = COGL_PANGO_RENDERER (object);
switch (prop_id) switch (prop_id)
{ {
case PROP_COGL_CONTEXT: case PROP_COGL_CONTEXT:
renderer->ctx = g_value_get_pointer (value); renderer->ctx = g_value_get_object (value);
cogl_object_ref (renderer->ctx);
break; break;
default: default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec); G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
@ -269,10 +268,11 @@ cogl_pango_renderer_class_init (CoglPangoRendererClass *klass)
object_class->dispose = cogl_pango_renderer_dispose; object_class->dispose = cogl_pango_renderer_dispose;
object_class->finalize = cogl_pango_renderer_finalize; object_class->finalize = cogl_pango_renderer_finalize;
pspec = g_param_spec_pointer ("context", NULL, NULL, pspec = g_param_spec_object ("context", NULL, NULL,
G_PARAM_WRITABLE | COGL_TYPE_CONTEXT,
G_PARAM_STATIC_STRINGS | G_PARAM_WRITABLE |
G_PARAM_CONSTRUCT_ONLY); G_PARAM_STATIC_STRINGS |
G_PARAM_CONSTRUCT_ONLY);
g_object_class_install_property (object_class, PROP_COGL_CONTEXT, pspec); g_object_class_install_property (object_class, PROP_COGL_CONTEXT, pspec);
@ -286,7 +286,7 @@ cogl_pango_renderer_dispose (GObject *object)
{ {
CoglPangoRenderer *priv = COGL_PANGO_RENDERER (object); CoglPangoRenderer *priv = COGL_PANGO_RENDERER (object);
cogl_clear_object (&priv->ctx); g_clear_object (&priv->ctx);
G_OBJECT_CLASS (cogl_pango_renderer_parent_class)->dispose (object); G_OBJECT_CLASS (cogl_pango_renderer_parent_class)->dispose (object);
} }

View File

@ -259,7 +259,7 @@ _cogl_attribute_new_const (CoglContext *context,
attribute->is_buffered = FALSE; attribute->is_buffered = FALSE;
attribute->normalized = FALSE; attribute->normalized = FALSE;
attribute->d.constant.context = cogl_object_ref (context); attribute->d.constant.context = g_object_ref (context);
attribute->d.constant.boxed.v.array = NULL; attribute->d.constant.boxed.v.array = NULL;

View File

@ -166,16 +166,16 @@ _cogl_bitmap_copy_subregion (CoglBitmap *src,
} }
CoglBitmap * CoglBitmap *
cogl_bitmap_new_for_data (CoglContext *context, cogl_bitmap_new_for_data (CoglContext *context,
int width, int width,
int height, int height,
CoglPixelFormat format, CoglPixelFormat format,
int rowstride, int rowstride,
uint8_t *data) uint8_t *data)
{ {
CoglBitmap *bmp; CoglBitmap *bmp;
g_return_val_if_fail (cogl_is_context (context), NULL); g_return_val_if_fail (COGL_IS_CONTEXT (context), NULL);
g_return_val_if_fail (cogl_pixel_format_get_n_planes (format) == 1, NULL); g_return_val_if_fail (cogl_pixel_format_get_n_planes (format) == 1, NULL);
/* Rowstride from width if not given */ /* Rowstride from width if not given */

View File

@ -68,7 +68,7 @@ struct _CoglTimestampQuery
struct _CoglContext struct _CoglContext
{ {
CoglObject _parent; GObject parent_instance;
CoglDisplay *display; CoglDisplay *display;

View File

@ -53,22 +53,8 @@
#include <string.h> #include <string.h>
#include <stdlib.h> #include <stdlib.h>
static void _cogl_context_free (CoglContext *context); G_DEFINE_TYPE (CoglContext, cogl_context, G_TYPE_OBJECT);
COGL_OBJECT_DEFINE (Context, context);
COGL_GTYPE_DEFINE_CLASS (Context, context);
extern void
_cogl_create_context_driver (CoglContext *context);
static CoglContext *_cogl_context = NULL;
static void
_cogl_init_feature_overrides (CoglContext *ctx)
{
if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_DISABLE_PBOS)))
COGL_FLAGS_SET (ctx->private_features, COGL_PRIVATE_FEATURE_PBOS, FALSE);
}
const CoglWinsysVtable * const CoglWinsysVtable *
_cogl_context_get_winsys (CoglContext *context) _cogl_context_get_winsys (CoglContext *context)
@ -82,6 +68,109 @@ _cogl_context_get_driver (CoglContext *context)
return context->driver_vtable; return context->driver_vtable;
} }
static void
cogl_context_dispose (GObject *object)
{
CoglContext *context = COGL_CONTEXT (object);
const CoglWinsysVtable *winsys = _cogl_context_get_winsys (context);
const CoglDriverVtable *driver = _cogl_context_get_driver (context);
winsys->context_deinit (context);
if (context->default_gl_texture_2d_tex)
cogl_object_unref (context->default_gl_texture_2d_tex);
if (context->opaque_color_pipeline)
cogl_object_unref (context->opaque_color_pipeline);
if (context->blit_texture_pipeline)
cogl_object_unref (context->blit_texture_pipeline);
if (context->swap_callback_closures)
g_hash_table_destroy (context->swap_callback_closures);
if (context->journal_flush_attributes_array)
g_array_free (context->journal_flush_attributes_array, TRUE);
if (context->journal_clip_bounds)
g_array_free (context->journal_clip_bounds, TRUE);
if (context->rectangle_byte_indices)
cogl_object_unref (context->rectangle_byte_indices);
if (context->rectangle_short_indices)
cogl_object_unref (context->rectangle_short_indices);
if (context->default_pipeline)
cogl_object_unref (context->default_pipeline);
if (context->dummy_layer_dependant)
cogl_object_unref (context->dummy_layer_dependant);
if (context->default_layer_n)
cogl_object_unref (context->default_layer_n);
if (context->default_layer_0)
cogl_object_unref (context->default_layer_0);
if (context->current_clip_stack_valid)
_cogl_clip_stack_unref (context->current_clip_stack);
g_slist_free (context->atlases);
g_hook_list_clear (&context->atlas_reorganize_callbacks);
_cogl_bitmask_destroy (&context->enabled_custom_attributes);
_cogl_bitmask_destroy (&context->enable_custom_attributes_tmp);
_cogl_bitmask_destroy (&context->changed_bits_tmp);
if (context->current_modelview_entry)
cogl_matrix_entry_unref (context->current_modelview_entry);
if (context->current_projection_entry)
cogl_matrix_entry_unref (context->current_projection_entry);
_cogl_pipeline_cache_free (context->pipeline_cache);
_cogl_sampler_cache_free (context->sampler_cache);
g_ptr_array_free (context->uniform_names, TRUE);
g_hash_table_destroy (context->uniform_name_hash);
g_hash_table_destroy (context->attribute_name_states_hash);
g_array_free (context->attribute_name_index_map, TRUE);
g_byte_array_free (context->buffer_map_fallback_array, TRUE);
driver->context_deinit (context);
g_object_unref (context->display);
g_hash_table_remove_all (context->named_pipelines);
g_hash_table_destroy (context->named_pipelines);
G_OBJECT_CLASS (cogl_context_parent_class)->dispose (object);
}
static void
cogl_context_init (CoglContext *info)
{
}
static void
cogl_context_class_init (CoglContextClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
object_class->dispose = cogl_context_dispose;
}
extern void
_cogl_create_context_driver (CoglContext *context);
static CoglContext *_cogl_context = NULL;
static void
_cogl_init_feature_overrides (CoglContext *ctx)
{
if (G_UNLIKELY (COGL_DEBUG_ENABLED (COGL_DEBUG_DISABLE_PBOS)))
COGL_FLAGS_SET (ctx->private_features, COGL_PRIVATE_FEATURE_PBOS, FALSE);
}
/* For reference: There was some deliberation over whether to have a /* For reference: There was some deliberation over whether to have a
* constructor that could throw an exception but looking at standard * constructor that could throw an exception but looking at standard
* practices with several high level OO languages including python, C++, * practices with several high level OO languages including python, C++,
@ -120,12 +209,7 @@ cogl_context_new (CoglDisplay *display,
#endif #endif
/* Allocate context memory */ /* Allocate context memory */
context = g_malloc0 (sizeof (CoglContext)); context = g_object_new (COGL_TYPE_CONTEXT, NULL);
/* Convert the context into an object immediately in case any of the
code below wants to verify that the context pointer is a valid
object */
_cogl_context_object_new (context);
/* XXX: Gross hack! /* XXX: Gross hack!
* Currently everything in Cogl just assumes there is a default * Currently everything in Cogl just assumes there is a default
@ -147,12 +231,12 @@ cogl_context_new (CoglDisplay *display,
if (!cogl_renderer_connect (renderer, error)) if (!cogl_renderer_connect (renderer, error))
{ {
cogl_object_unref (renderer); cogl_object_unref (renderer);
g_free (context); g_object_unref (context);
return NULL; return NULL;
} }
display = cogl_display_new (renderer, NULL); display = cogl_display_new (renderer, NULL);
cogl_object_unref(renderer); cogl_object_unref (renderer);
} }
else else
g_object_ref (display); g_object_ref (display);
@ -160,7 +244,7 @@ cogl_context_new (CoglDisplay *display,
if (!cogl_display_setup (display, error)) if (!cogl_display_setup (display, error))
{ {
g_object_unref (display); g_object_unref (display);
g_free (context); g_object_unref (context);
return NULL; return NULL;
} }
@ -190,7 +274,7 @@ cogl_context_new (CoglDisplay *display,
if (!context->driver_vtable->context_init (context)) if (!context->driver_vtable->context_init (context))
{ {
g_object_unref (display); g_object_unref (display);
g_free (context); g_object_unref (context);
g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED, g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
"Failed to initialize context"); "Failed to initialize context");
return NULL; return NULL;
@ -323,83 +407,6 @@ cogl_context_new (CoglDisplay *display,
return context; return context;
} }
static void
_cogl_context_free (CoglContext *context)
{
const CoglWinsysVtable *winsys = _cogl_context_get_winsys (context);
const CoglDriverVtable *driver = _cogl_context_get_driver (context);
winsys->context_deinit (context);
if (context->default_gl_texture_2d_tex)
cogl_object_unref (context->default_gl_texture_2d_tex);
if (context->opaque_color_pipeline)
cogl_object_unref (context->opaque_color_pipeline);
if (context->blit_texture_pipeline)
cogl_object_unref (context->blit_texture_pipeline);
if (context->swap_callback_closures)
g_hash_table_destroy (context->swap_callback_closures);
if (context->journal_flush_attributes_array)
g_array_free (context->journal_flush_attributes_array, TRUE);
if (context->journal_clip_bounds)
g_array_free (context->journal_clip_bounds, TRUE);
if (context->rectangle_byte_indices)
cogl_object_unref (context->rectangle_byte_indices);
if (context->rectangle_short_indices)
cogl_object_unref (context->rectangle_short_indices);
if (context->default_pipeline)
cogl_object_unref (context->default_pipeline);
if (context->dummy_layer_dependant)
cogl_object_unref (context->dummy_layer_dependant);
if (context->default_layer_n)
cogl_object_unref (context->default_layer_n);
if (context->default_layer_0)
cogl_object_unref (context->default_layer_0);
if (context->current_clip_stack_valid)
_cogl_clip_stack_unref (context->current_clip_stack);
g_slist_free (context->atlases);
g_hook_list_clear (&context->atlas_reorganize_callbacks);
_cogl_bitmask_destroy (&context->enabled_custom_attributes);
_cogl_bitmask_destroy (&context->enable_custom_attributes_tmp);
_cogl_bitmask_destroy (&context->changed_bits_tmp);
if (context->current_modelview_entry)
cogl_matrix_entry_unref (context->current_modelview_entry);
if (context->current_projection_entry)
cogl_matrix_entry_unref (context->current_projection_entry);
_cogl_pipeline_cache_free (context->pipeline_cache);
_cogl_sampler_cache_free (context->sampler_cache);
g_ptr_array_free (context->uniform_names, TRUE);
g_hash_table_destroy (context->uniform_name_hash);
g_hash_table_destroy (context->attribute_name_states_hash);
g_array_free (context->attribute_name_index_map, TRUE);
g_byte_array_free (context->buffer_map_fallback_array, TRUE);
driver->context_deinit (context);
g_object_unref (context->display);
g_hash_table_remove_all (context->named_pipelines);
g_hash_table_destroy (context->named_pipelines);
g_free (context);
}
CoglContext * CoglContext *
_cogl_context_get_default (void) _cogl_context_get_default (void)
{ {

View File

@ -52,8 +52,9 @@ typedef struct _CoglTimestampQuery CoglTimestampQuery;
G_BEGIN_DECLS G_BEGIN_DECLS
/** /**
* SECTION:cogl-context * CoglContext:
* @short_description: The top level application context. *
* The top level application context.
* *
* A #CoglContext is the top most sandbox of Cogl state for an * A #CoglContext is the top most sandbox of Cogl state for an
* application or toolkit. Its main purpose is to act as a sandbox * application or toolkit. Its main purpose is to act as a sandbox
@ -93,15 +94,14 @@ G_BEGIN_DECLS
* context.</para></note> * context.</para></note>
*/ */
#define COGL_CONTEXT(OBJECT) ((CoglContext *)OBJECT) #define COGL_TYPE_CONTEXT (cogl_context_get_type ())
/**
* cogl_context_get_gtype:
*
* Returns: a #GType that can be used with the GLib type system.
*/
COGL_EXPORT COGL_EXPORT
GType cogl_context_get_gtype (void); G_DECLARE_FINAL_TYPE (CoglContext,
cogl_context,
COGL,
CONTEXT,
GObject)
/** /**
* cogl_context_new: (constructor) (skip) * cogl_context_new: (constructor) (skip)
@ -150,17 +150,6 @@ cogl_context_get_display (CoglContext *context);
COGL_EXPORT CoglRenderer * COGL_EXPORT CoglRenderer *
cogl_context_get_renderer (CoglContext *context); cogl_context_get_renderer (CoglContext *context);
/**
* cogl_is_context:
* @object: An object or %NULL
*
* Gets whether the given object references an existing context object.
*
* Return value: %TRUE if the @object references a #CoglContext,
* %FALSE otherwise
*/
COGL_EXPORT gboolean
cogl_is_context (void *object);
/* XXX: not guarded by the EXPERIMENTAL_API defines to avoid /* XXX: not guarded by the EXPERIMENTAL_API defines to avoid
* upsetting glib-mkenums, but this can still be considered implicitly * upsetting glib-mkenums, but this can still be considered implicitly

View File

@ -173,7 +173,7 @@ cogl_framebuffer_get_property (GObject *object,
switch (prop_id) switch (prop_id)
{ {
case PROP_CONTEXT: case PROP_CONTEXT:
g_value_set_boxed (value, priv->context); g_value_set_object (value, priv->context);
break; break;
case PROP_DRIVER_CONFIG: case PROP_DRIVER_CONFIG:
g_value_set_pointer (value, &priv->driver_config); g_value_set_pointer (value, &priv->driver_config);
@ -203,7 +203,7 @@ cogl_framebuffer_set_property (GObject *object,
switch (prop_id) switch (prop_id)
{ {
case PROP_CONTEXT: case PROP_CONTEXT:
priv->context = g_value_get_boxed (value); priv->context = g_value_get_object (value);
break; break;
case PROP_DRIVER_CONFIG: case PROP_DRIVER_CONFIG:
driver_config = g_value_get_pointer (value); driver_config = g_value_get_pointer (value);
@ -381,11 +381,11 @@ cogl_framebuffer_class_init (CoglFramebufferClass *klass)
object_class->set_property = cogl_framebuffer_set_property; object_class->set_property = cogl_framebuffer_set_property;
obj_props[PROP_CONTEXT] = obj_props[PROP_CONTEXT] =
g_param_spec_boxed ("context", NULL, NULL, g_param_spec_object ("context", NULL, NULL,
COGL_TYPE_HANDLE, COGL_TYPE_CONTEXT,
G_PARAM_READWRITE | G_PARAM_READWRITE |
G_PARAM_CONSTRUCT_ONLY | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS); G_PARAM_STATIC_STRINGS);
obj_props[PROP_DRIVER_CONFIG] = obj_props[PROP_DRIVER_CONFIG] =
g_param_spec_pointer ("driver-config", NULL, NULL, g_param_spec_pointer ("driver-config", NULL, NULL,
G_PARAM_READWRITE | G_PARAM_READWRITE |