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, cogl_attribute_buffer_new_with_size (CoglContext *context,
size_t bytes) size_t bytes)
{ {
CoglAttributeBuffer *buffer = g_object_new (COGL_TYPE_ATTRIBUTE_BUFFER, NULL); CoglAttributeBuffer *buffer;
/* parent's constructor */ buffer = g_object_new (COGL_TYPE_ATTRIBUTE_BUFFER,
_cogl_buffer_initialize (COGL_BUFFER (buffer), "context", context,
context, "size", bytes,
bytes, "default-target", COGL_BUFFER_BIND_TARGET_ATTRIBUTE_BUFFER,
COGL_BUFFER_BIND_TARGET_ATTRIBUTE_BUFFER, "usage-hint", COGL_BUFFER_USAGE_HINT_ATTRIBUTE_BUFFER,
COGL_BUFFER_USAGE_HINT_ATTRIBUTE_BUFFER, "update-hint", COGL_BUFFER_UPDATE_HINT_STATIC,
COGL_BUFFER_UPDATE_HINT_STATIC); NULL);
return buffer; return buffer;
} }

View File

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

View File

@ -50,38 +50,20 @@
G_DEFINE_ABSTRACT_TYPE (CoglBuffer, cogl_buffer, G_TYPE_OBJECT) G_DEFINE_ABSTRACT_TYPE (CoglBuffer, cogl_buffer, G_TYPE_OBJECT)
static void enum
cogl_buffer_dispose (GObject *object)
{ {
CoglBuffer *buffer = COGL_BUFFER (object); PROP_0,
g_return_if_fail (!(buffer->flags & COGL_BUFFER_FLAG_MAPPED)); PROP_CONTEXT,
g_return_if_fail (buffer->immutable_ref == 0); PROP_SIZE,
PROP_DEFAULT_TARGET,
PROP_USAGE_HINT,
PROP_UPDATE_HINT,
if (buffer->flags & COGL_BUFFER_FLAG_BUFFER_OBJECT) PROP_LAST
buffer->context->driver_vtable->buffer_destroy (buffer); };
else
g_free (buffer->data);
G_OBJECT_CLASS (cogl_buffer_parent_class)->dispose (object); static GParamSpec *obj_props[PROP_LAST];
}
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;
}
/* /*
* Fallback path, buffer->data points to a malloc'ed buffer. * Fallback path, buffer->data points to a malloc'ed buffer.
@ -116,49 +98,132 @@ malloc_set_data (CoglBuffer *buffer,
return TRUE; return TRUE;
} }
void static void
_cogl_buffer_initialize (CoglBuffer *buffer, cogl_buffer_dispose (GObject *object)
CoglContext *ctx,
size_t size,
CoglBufferBindTarget default_target,
CoglBufferUsageHint usage_hint,
CoglBufferUpdateHint update_hint)
{ {
gboolean use_malloc = FALSE; CoglBuffer *buffer = COGL_BUFFER (object);
buffer->context = ctx; g_return_if_fail (!(buffer->flags & COGL_BUFFER_FLAG_MAPPED));
buffer->size = size; g_return_if_fail (buffer->immutable_ref == 0);
buffer->last_target = default_target;
buffer->usage_hint = usage_hint;
buffer->update_hint = update_hint;
if (default_target == COGL_BUFFER_BIND_TARGET_PIXEL_PACK || if (buffer->flags & COGL_BUFFER_FLAG_BUFFER_OBJECT)
default_target == COGL_BUFFER_BIND_TARGET_PIXEL_UNPACK) buffer->context->driver_vtable->buffer_destroy (buffer);
{
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);
}
else 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; case PROP_CONTEXT:
buffer->unmap = ctx->driver_vtable->buffer_unmap; buffer->context = g_value_get_object (value);
buffer->set_data = ctx->driver_vtable->buffer_set_data; 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 unsigned int
cogl_buffer_get_size (CoglBuffer *buffer) cogl_buffer_get_size (CoglBuffer *buffer)
{ {

View File

@ -55,15 +55,14 @@ cogl_index_buffer_init (CoglIndexBuffer *buffer)
CoglIndexBuffer * CoglIndexBuffer *
cogl_index_buffer_new (CoglContext *context, size_t bytes) cogl_index_buffer_new (CoglContext *context, size_t bytes)
{ {
CoglIndexBuffer *indices = g_object_new (COGL_TYPE_INDEX_BUFFER, NULL); CoglIndexBuffer *indices;
/* 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);
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; return indices;
} }

View File

@ -67,16 +67,15 @@ _cogl_pixel_buffer_new (CoglContext *context,
const void *data, const void *data,
GError **error) GError **error)
{ {
CoglPixelBuffer *pixel_buffer = g_object_new (COGL_TYPE_PIXEL_BUFFER, NULL); CoglPixelBuffer *pixel_buffer;
/* 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);
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) if (data)
{ {