4
* A Low Level GPU Graphics and Utilities API
6
* Copyright (C) 2007,2008,2009,2010 Intel Corporation.
8
* Permission is hereby granted, free of charge, to any person
9
* obtaining a copy of this software and associated documentation
10
* files (the "Software"), to deal in the Software without
11
* restriction, including without limitation the rights to use, copy,
12
* modify, merge, publish, distribute, sublicense, and/or sell copies
13
* of the Software, and to permit persons to whom the Software is
14
* furnished to do so, subject to the following conditions:
16
* The above copyright notice and this permission notice shall be
17
* included in all copies or substantial portions of the Software.
19
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
23
* BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
24
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
25
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
* Robert Bragg <robert@linux.intel.com>
33
#include "cogl-config.h"
39
#include "cogl-util.h"
40
#include "cogl-types.h"
41
#include "cogl-object-private.h"
42
#include "cogl-gtype-private.h"
44
COGL_GTYPE_DEFINE_BASE_CLASS (Object, object);
47
cogl_object_ref (void *object)
49
CoglObject *obj = object;
51
_COGL_RETURN_VAL_IF_FAIL (object != NULL, NULL);
58
cogl_handle_ref (CoglHandle handle)
60
return cogl_object_ref (handle);
64
_cogl_object_default_unref (void *object)
66
CoglObject *obj = object;
68
_COGL_RETURN_IF_FAIL (object != NULL);
69
_COGL_RETURN_IF_FAIL (obj->ref_count > 0);
71
if (--obj->ref_count < 1)
73
void (*free_func)(void *obj);
75
if (obj->n_user_data_entries)
78
int count = MIN (obj->n_user_data_entries,
79
COGL_OBJECT_N_PRE_ALLOCATED_USER_DATA_ENTRIES);
81
for (i = 0; i < count; i++)
83
CoglUserDataEntry *entry = &obj->user_data_entry[i];
85
entry->destroy (entry->user_data, obj);
88
if (obj->user_data_array != NULL)
90
for (i = 0; i < obj->user_data_array->len; i++)
92
CoglUserDataEntry *entry =
93
&g_array_index (obj->user_data_array,
94
CoglUserDataEntry, i);
97
entry->destroy (entry->user_data, obj);
99
g_array_free (obj->user_data_array, TRUE);
103
COGL_OBJECT_DEBUG_FREE (obj);
104
free_func = obj->klass->virt_free;
110
cogl_object_unref (void *obj)
112
void (* unref_func) (void *) = ((CoglObject *) obj)->klass->virt_unref;
117
cogl_handle_unref (CoglHandle handle)
119
cogl_object_unref (handle);
123
cogl_handle_get_type (void)
125
static GType our_type = 0;
127
/* XXX: We are keeping the "CoglHandle" name for now incase it would
128
* break bindings to change to "CoglObject" */
129
if (G_UNLIKELY (our_type == 0))
130
our_type = g_boxed_type_register_static (g_intern_static_string ("CoglHandle"),
131
(GBoxedCopyFunc) cogl_object_ref,
132
(GBoxedFreeFunc) cogl_object_unref);
137
/* XXX: Unlike for cogl_object_get_user_data this code will return
138
* an empty entry if available and no entry for the given key can be
140
static CoglUserDataEntry *
141
_cogl_object_find_entry (CoglObject *object, CoglUserDataKey *key)
143
CoglUserDataEntry *entry = NULL;
147
count = MIN (object->n_user_data_entries,
148
COGL_OBJECT_N_PRE_ALLOCATED_USER_DATA_ENTRIES);
150
for (i = 0; i < count; i++)
152
CoglUserDataEntry *current = &object->user_data_entry[i];
153
if (current->key == key)
155
if (current->user_data == NULL)
159
if (G_UNLIKELY (object->user_data_array != NULL))
161
for (i = 0; i < object->user_data_array->len; i++)
163
CoglUserDataEntry *current =
164
&g_array_index (object->user_data_array, CoglUserDataEntry, i);
166
if (current->key == key)
168
if (current->user_data == NULL)
177
_cogl_object_set_user_data (CoglObject *object,
178
CoglUserDataKey *key,
180
CoglUserDataDestroyInternalCallback destroy)
182
CoglUserDataEntry new_entry;
183
CoglUserDataEntry *entry;
188
new_entry.user_data = user_data;
189
new_entry.destroy = destroy;
192
memset (&new_entry, 0, sizeof (new_entry));
194
entry = _cogl_object_find_entry (object, key);
197
if (G_LIKELY (entry->destroy))
198
entry->destroy (entry->user_data, object);
202
/* NB: Setting a value of NULL is documented to delete the
203
* corresponding entry so we can return immediately in this
205
if (user_data == NULL)
208
if (G_LIKELY (object->n_user_data_entries <
209
COGL_OBJECT_N_PRE_ALLOCATED_USER_DATA_ENTRIES))
210
entry = &object->user_data_entry[object->n_user_data_entries++];
213
if (G_UNLIKELY (object->user_data_array == NULL))
215
object->user_data_array =
216
g_array_new (FALSE, FALSE, sizeof (CoglUserDataEntry));
219
g_array_set_size (object->user_data_array,
220
object->user_data_array->len + 1);
222
&g_array_index (object->user_data_array, CoglUserDataEntry,
223
object->user_data_array->len - 1);
225
object->n_user_data_entries++;
233
cogl_object_set_user_data (CoglObject *object,
234
CoglUserDataKey *key,
236
CoglUserDataDestroyCallback destroy)
238
_cogl_object_set_user_data (object, key, user_data,
239
(CoglUserDataDestroyInternalCallback)destroy);
243
cogl_object_get_user_data (CoglObject *object, CoglUserDataKey *key)
248
count = MIN (object->n_user_data_entries,
249
COGL_OBJECT_N_PRE_ALLOCATED_USER_DATA_ENTRIES);
251
for (i = 0; i < count; i++)
253
CoglUserDataEntry *entry = &object->user_data_entry[i];
254
if (entry->key == key)
255
return entry->user_data;
258
if (object->user_data_array != NULL)
260
for (i = 0; i < object->user_data_array->len; i++)
262
CoglUserDataEntry *entry =
263
&g_array_index (object->user_data_array, CoglUserDataEntry, i);
265
if (entry->key == key)
266
return entry->user_data;
274
cogl_debug_object_foreach_type (CoglDebugObjectForeachTypeCallback func,
278
unsigned long *instance_count;
279
CoglDebugObjectTypeInfo info;
281
g_hash_table_iter_init (&iter, _cogl_debug_instances);
282
while (g_hash_table_iter_next (&iter,
284
(void *) &instance_count))
286
info.instance_count = *instance_count;
287
func (&info, user_data);
292
print_instances_cb (const CoglDebugObjectTypeInfo *info,
295
g_print ("\t%s: %lu\n", info->name, info->instance_count);
299
cogl_debug_object_print_instances (void)
301
g_print ("Cogl instances:\n");
303
cogl_debug_object_foreach_type (print_instances_cb, NULL);