4 * An object oriented GL/GLES Abstraction/Utility Layer
6 * Copyright (C) 2007,2008,2009,2010 Intel Corporation.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library. If not, see
20 * <http://www.gnu.org/licenses/>.
23 * Robert Bragg <robert@linux.intel.com>
33 #include "cogl-util.h"
34 #include "cogl-types.h"
35 #include "cogl-object-private.h"
38 cogl_object_ref (void *object)
40 CoglObject *obj = object;
42 _COGL_RETURN_VAL_IF_FAIL (object != NULL, NULL);
49 cogl_handle_ref (CoglHandle handle)
51 return cogl_object_ref (handle);
55 _cogl_object_default_unref (void *object)
57 CoglObject *obj = object;
59 _COGL_RETURN_IF_FAIL (object != NULL);
60 _COGL_RETURN_IF_FAIL (obj->ref_count > 0);
62 if (--obj->ref_count < 1)
64 void (*free_func)(void *obj);
66 if (obj->n_user_data_entries)
69 int count = MIN (obj->n_user_data_entries,
70 COGL_OBJECT_N_PRE_ALLOCATED_USER_DATA_ENTRIES);
72 for (i = 0; i < count; i++)
74 CoglUserDataEntry *entry = &obj->user_data_entry[i];
76 entry->destroy (entry->user_data, obj);
79 if (obj->user_data_array != NULL)
81 for (i = 0; i < obj->user_data_array->len; i++)
83 CoglUserDataEntry *entry =
84 &g_array_index (obj->user_data_array,
85 CoglUserDataEntry, i);
88 entry->destroy (entry->user_data, obj);
90 g_array_free (obj->user_data_array, TRUE);
94 COGL_OBJECT_DEBUG_FREE (obj);
95 free_func = obj->klass->virt_free;
101 cogl_object_unref (void *obj)
103 void (* unref_func) (void *) = ((CoglObject *) obj)->klass->virt_unref;
108 cogl_handle_unref (CoglHandle handle)
110 cogl_object_unref (handle);
114 cogl_handle_get_type (void)
116 static GType our_type = 0;
118 /* XXX: We are keeping the "CoglHandle" name for now incase it would
119 * break bindings to change to "CoglObject" */
120 if (G_UNLIKELY (our_type == 0))
121 our_type = g_boxed_type_register_static (g_intern_static_string ("CoglHandle"),
122 (GBoxedCopyFunc) cogl_object_ref,
123 (GBoxedFreeFunc) cogl_object_unref);
128 /* XXX: Unlike for cogl_object_get_user_data this code will return
129 * an empty entry if available and no entry for the given key can be
131 static CoglUserDataEntry *
132 _cogl_object_find_entry (CoglObject *object, CoglUserDataKey *key)
134 CoglUserDataEntry *entry = NULL;
138 count = MIN (object->n_user_data_entries,
139 COGL_OBJECT_N_PRE_ALLOCATED_USER_DATA_ENTRIES);
141 for (i = 0; i < count; i++)
143 CoglUserDataEntry *current = &object->user_data_entry[i];
144 if (current->key == key)
146 if (current->user_data == NULL)
150 if (G_UNLIKELY (object->user_data_array != NULL))
152 for (i = 0; i < object->user_data_array->len; i++)
154 CoglUserDataEntry *current =
155 &g_array_index (object->user_data_array, CoglUserDataEntry, i);
157 if (current->key == key)
159 if (current->user_data == NULL)
168 _cogl_object_set_user_data (CoglObject *object,
169 CoglUserDataKey *key,
171 CoglUserDataDestroyInternalCallback destroy)
173 CoglUserDataEntry new_entry;
174 CoglUserDataEntry *entry;
179 new_entry.user_data = user_data;
180 new_entry.destroy = destroy;
183 memset (&new_entry, 0, sizeof (new_entry));
185 entry = _cogl_object_find_entry (object, key);
188 if (G_LIKELY (entry->destroy))
189 entry->destroy (entry->user_data, object);
193 /* NB: Setting a value of NULL is documented to delete the
194 * corresponding entry so we can return immediately in this
196 if (user_data == NULL)
199 if (G_LIKELY (object->n_user_data_entries <
200 COGL_OBJECT_N_PRE_ALLOCATED_USER_DATA_ENTRIES))
201 entry = &object->user_data_entry[object->n_user_data_entries++];
204 if (G_UNLIKELY (object->user_data_array == NULL))
206 object->user_data_array =
207 g_array_new (FALSE, FALSE, sizeof (CoglUserDataEntry));
210 g_array_set_size (object->user_data_array,
211 object->user_data_array->len + 1);
213 &g_array_index (object->user_data_array, CoglUserDataEntry,
214 object->user_data_array->len - 1);
216 object->n_user_data_entries++;
224 cogl_object_set_user_data (CoglObject *object,
225 CoglUserDataKey *key,
227 CoglUserDataDestroyCallback destroy)
229 _cogl_object_set_user_data (object, key, user_data,
230 (CoglUserDataDestroyInternalCallback)destroy);
234 cogl_object_get_user_data (CoglObject *object, CoglUserDataKey *key)
239 count = MIN (object->n_user_data_entries,
240 COGL_OBJECT_N_PRE_ALLOCATED_USER_DATA_ENTRIES);
242 for (i = 0; i < count; i++)
244 CoglUserDataEntry *entry = &object->user_data_entry[i];
245 if (entry->key == key)
246 return entry->user_data;
249 if (object->user_data_array != NULL)
251 for (i = 0; i < object->user_data_array->len; i++)
253 CoglUserDataEntry *entry =
254 &g_array_index (object->user_data_array, CoglUserDataEntry, i);
256 if (entry->key == key)
257 return entry->user_data;
265 cogl_debug_object_foreach_type (CoglDebugObjectForeachTypeCallback func,
269 unsigned long *instance_count;
270 CoglDebugObjectTypeInfo info;
272 g_hash_table_iter_init (&iter, _cogl_debug_instances);
273 while (g_hash_table_iter_next (&iter,
275 (void *) &instance_count))
277 info.instance_count = *instance_count;
278 func (&info, user_data);
283 print_instances_cb (const CoglDebugObjectTypeInfo *info,
286 g_print ("\t%s: %lu\n", info->name, info->instance_count);
290 cogl_debug_object_print_instances (void)
292 g_print ("Cogl instances:\n");
294 cogl_debug_object_foreach_type (print_instances_cb, NULL);