cogl/cleanup: Use construct-only properties for CoglBuffer

Instead of a custom initialize function

Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/3193>
This commit is contained in:
Bilal Elmoussaoui 2023-10-26 12:41:26 +02:00 committed by Marge Bot
parent 739c59fefc
commit 6efd4a2282
5 changed files with 150 additions and 95 deletions

View File

@ -53,15 +53,15 @@ CoglAttributeBuffer *
cogl_attribute_buffer_new_with_size (CoglContext *context,
size_t bytes)
{
CoglAttributeBuffer *buffer = g_object_new (COGL_TYPE_ATTRIBUTE_BUFFER, NULL);
CoglAttributeBuffer *buffer;
/* parent's constructor */
_cogl_buffer_initialize (COGL_BUFFER (buffer),
context,
bytes,
COGL_BUFFER_BIND_TARGET_ATTRIBUTE_BUFFER,
COGL_BUFFER_USAGE_HINT_ATTRIBUTE_BUFFER,
COGL_BUFFER_UPDATE_HINT_STATIC);
buffer = g_object_new (COGL_TYPE_ATTRIBUTE_BUFFER,
"context", context,
"size", bytes,
"default-target", COGL_BUFFER_BIND_TARGET_ATTRIBUTE_BUFFER,
"usage-hint", COGL_BUFFER_USAGE_HINT_ATTRIBUTE_BUFFER,
"update-hint", COGL_BUFFER_UPDATE_HINT_STATIC,
NULL);
return buffer;
}

View File

@ -110,14 +110,6 @@ struct _CoglBufferClass
GObjectClass parent_class;
};
void
_cogl_buffer_initialize (CoglBuffer *buffer,
CoglContext *context,
size_t size,
CoglBufferBindTarget default_target,
CoglBufferUsageHint usage_hint,
CoglBufferUpdateHint update_hint);
CoglBufferUsageHint
_cogl_buffer_get_usage_hint (CoglBuffer *buffer);

View File

@ -50,38 +50,20 @@
G_DEFINE_ABSTRACT_TYPE (CoglBuffer, cogl_buffer, G_TYPE_OBJECT)
static void
cogl_buffer_dispose (GObject *object)
enum
{
CoglBuffer *buffer = COGL_BUFFER (object);
PROP_0,
g_return_if_fail (!(buffer->flags & COGL_BUFFER_FLAG_MAPPED));
g_return_if_fail (buffer->immutable_ref == 0);
PROP_CONTEXT,
PROP_SIZE,
PROP_DEFAULT_TARGET,
PROP_USAGE_HINT,
PROP_UPDATE_HINT,
if (buffer->flags & COGL_BUFFER_FLAG_BUFFER_OBJECT)
buffer->context->driver_vtable->buffer_destroy (buffer);
else
g_free (buffer->data);
PROP_LAST
};
G_OBJECT_CLASS (cogl_buffer_parent_class)->dispose (object);
}
static void
cogl_buffer_class_init (CoglBufferClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->dispose = cogl_buffer_dispose;
}
static void
cogl_buffer_init (CoglBuffer *buffer)
{
buffer->flags = COGL_BUFFER_FLAG_NONE;
buffer->store_created = FALSE;
buffer->data = NULL;
buffer->immutable_ref = 0;
}
static GParamSpec *obj_props[PROP_LAST];
/*
* Fallback path, buffer->data points to a malloc'ed buffer.
@ -116,49 +98,132 @@ malloc_set_data (CoglBuffer *buffer,
return TRUE;
}
void
_cogl_buffer_initialize (CoglBuffer *buffer,
CoglContext *ctx,
size_t size,
CoglBufferBindTarget default_target,
CoglBufferUsageHint usage_hint,
CoglBufferUpdateHint update_hint)
static void
cogl_buffer_dispose (GObject *object)
{
gboolean use_malloc = FALSE;
CoglBuffer *buffer = COGL_BUFFER (object);
buffer->context = ctx;
buffer->size = size;
buffer->last_target = default_target;
buffer->usage_hint = usage_hint;
buffer->update_hint = update_hint;
g_return_if_fail (!(buffer->flags & COGL_BUFFER_FLAG_MAPPED));
g_return_if_fail (buffer->immutable_ref == 0);
if (default_target == COGL_BUFFER_BIND_TARGET_PIXEL_PACK ||
default_target == COGL_BUFFER_BIND_TARGET_PIXEL_UNPACK)
{
if (!_cogl_has_private_feature (ctx, COGL_PRIVATE_FEATURE_PBOS))
use_malloc = TRUE;
}
if (use_malloc)
{
buffer->map_range = malloc_map_range;
buffer->unmap = malloc_unmap;
buffer->set_data = malloc_set_data;
buffer->data = g_malloc (size);
}
if (buffer->flags & COGL_BUFFER_FLAG_BUFFER_OBJECT)
buffer->context->driver_vtable->buffer_destroy (buffer);
else
g_free (buffer->data);
G_OBJECT_CLASS (cogl_buffer_parent_class)->dispose (object);
}
static void
cogl_buffer_set_property (GObject *gobject,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
CoglBuffer *buffer = COGL_BUFFER (gobject);
switch (prop_id)
{
buffer->map_range = ctx->driver_vtable->buffer_map_range;
buffer->unmap = ctx->driver_vtable->buffer_unmap;
buffer->set_data = ctx->driver_vtable->buffer_set_data;
case PROP_CONTEXT:
buffer->context = g_value_get_object (value);
break;
ctx->driver_vtable->buffer_create (buffer);
case PROP_SIZE:
buffer->size = g_value_get_uint64 (value);
break;
buffer->flags |= COGL_BUFFER_FLAG_BUFFER_OBJECT;
case PROP_DEFAULT_TARGET:
gboolean use_malloc = FALSE;
buffer->last_target = g_value_get_uint (value);
if (buffer->last_target == COGL_BUFFER_BIND_TARGET_PIXEL_PACK ||
buffer->last_target == COGL_BUFFER_BIND_TARGET_PIXEL_UNPACK)
{
if (!_cogl_has_private_feature (buffer->context, COGL_PRIVATE_FEATURE_PBOS))
use_malloc = TRUE;
}
if (use_malloc)
{
buffer->map_range = malloc_map_range;
buffer->unmap = malloc_unmap;
buffer->set_data = malloc_set_data;
buffer->data = g_malloc (buffer->size);
}
else
{
buffer->map_range = buffer->context->driver_vtable->buffer_map_range;
buffer->unmap = buffer->context->driver_vtable->buffer_unmap;
buffer->set_data = buffer->context->driver_vtable->buffer_set_data;
buffer->context->driver_vtable->buffer_create (buffer);
buffer->flags |= COGL_BUFFER_FLAG_BUFFER_OBJECT;
}
break;
case PROP_USAGE_HINT:
buffer->usage_hint = g_value_get_uint (value);
break;
case PROP_UPDATE_HINT:
buffer->update_hint = g_value_get_uint (value);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (gobject, prop_id, pspec);
break;
}
}
static void
cogl_buffer_class_init (CoglBufferClass *klass)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
gobject_class->dispose = cogl_buffer_dispose;
gobject_class->set_property = cogl_buffer_set_property;
obj_props[PROP_CONTEXT] =
g_param_spec_object ("context", NULL, NULL,
COGL_TYPE_CONTEXT,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);
obj_props[PROP_SIZE] =
g_param_spec_uint64 ("size", NULL, NULL,
0, G_MAXINT64, 0,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);
obj_props[PROP_DEFAULT_TARGET] =
g_param_spec_uint ("default-target", NULL, NULL,
0, G_MAXINT, 0,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);
obj_props[PROP_USAGE_HINT] =
g_param_spec_uint ("usage-hint", NULL, NULL,
0, G_MAXINT, 0,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT_ONLY |
G_PARAM_STATIC_STRINGS);
obj_props[PROP_UPDATE_HINT] =
g_param_spec_uint ("update-hint", NULL, NULL,
0, G_MAXINT, 0,
G_PARAM_WRITABLE | G_PARAM_CONSTRUCT |
G_PARAM_STATIC_STRINGS);
g_object_class_install_properties (gobject_class,
PROP_LAST,
obj_props);
}
static void
cogl_buffer_init (CoglBuffer *buffer)
{
buffer->flags = COGL_BUFFER_FLAG_NONE;
buffer->store_created = FALSE;
buffer->data = NULL;
buffer->immutable_ref = 0;
}
unsigned int
cogl_buffer_get_size (CoglBuffer *buffer)
{

View File

@ -55,15 +55,14 @@ cogl_index_buffer_init (CoglIndexBuffer *buffer)
CoglIndexBuffer *
cogl_index_buffer_new (CoglContext *context, size_t bytes)
{
CoglIndexBuffer *indices = g_object_new (COGL_TYPE_INDEX_BUFFER, NULL);
/* parent's constructor */
_cogl_buffer_initialize (COGL_BUFFER (indices),
context,
bytes,
COGL_BUFFER_BIND_TARGET_INDEX_BUFFER,
COGL_BUFFER_USAGE_HINT_INDEX_BUFFER,
COGL_BUFFER_UPDATE_HINT_STATIC);
CoglIndexBuffer *indices;
indices = g_object_new (COGL_TYPE_INDEX_BUFFER,
"context", context,
"size", bytes,
"default-target", COGL_BUFFER_BIND_TARGET_INDEX_BUFFER,
"usage-hint", COGL_BUFFER_USAGE_HINT_INDEX_BUFFER,
"update-hint", COGL_BUFFER_UPDATE_HINT_STATIC,
NULL);
return indices;
}

View File

@ -67,16 +67,15 @@ _cogl_pixel_buffer_new (CoglContext *context,
const void *data,
GError **error)
{
CoglPixelBuffer *pixel_buffer = g_object_new (COGL_TYPE_PIXEL_BUFFER, NULL);
/* parent's constructor */
_cogl_buffer_initialize (COGL_BUFFER (pixel_buffer),
context,
size,
COGL_BUFFER_BIND_TARGET_PIXEL_UNPACK,
COGL_BUFFER_USAGE_HINT_TEXTURE,
COGL_BUFFER_UPDATE_HINT_STATIC);
CoglPixelBuffer *pixel_buffer;
pixel_buffer = g_object_new (COGL_TYPE_PIXEL_BUFFER,
"context", context,
"size", size,
"default-target", COGL_BUFFER_BIND_TARGET_PIXEL_UNPACK,
"usage-hint", COGL_BUFFER_USAGE_HINT_TEXTURE,
"update-hint", COGL_BUFFER_UPDATE_HINT_STATIC,
NULL);
if (data)
{