summaryrefslogtreecommitdiff
path: root/cogl/cogl-object-private.h
blob: 7955a35cc2e0c923857023711bac1dda67e5df98 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/*
 * Cogl
 *
 * A Low Level GPU Graphics and Utilities API
 *
 * Copyright (C) 2008,2009,2010 Intel Corporation.
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use, copy,
 * modify, merge, publish, distribute, sublicense, and/or sell copies
 * of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *
 * Authors:
 *   Robert Bragg <robert@linux.intel.com>
 */

#ifndef __COGL_OBJECT_PRIVATE_H
#define __COGL_OBJECT_PRIVATE_H

#include <glib.h>

#include "cogl-types.h"
#include "cogl-object.h"
#include "cogl-debug.h"

/* For compatability until all components have been converted */
typedef struct _CoglObjectClass CoglHandleClass;
typedef struct _CoglObject      CoglHandleObject;

/* XXX: sadly we didn't fully consider when we copied the cairo API
 * for _set_user_data that the callback doesn't get a pointer to the
 * instance which is desired in most cases. This means you tend to end
 * up creating micro allocations for the private data just so you can
 * pair up the data of interest with the original instance for
 * identification when it is later destroyed.
 *
 * Internally we use a small hack to avoid needing these micro
 * allocations by actually passing the instance as a second argument
 * to the callback */
typedef void (*CoglUserDataDestroyInternalCallback) (void *user_data,
                                                     void *instance);

typedef struct _CoglObjectClass
{
#ifdef COGL_HAS_GTYPE_SUPPORT
  GTypeClass base_class;
#endif
  const char *name;
  void *virt_free;
  void *virt_unref;
} CoglObjectClass;

#define COGL_OBJECT_N_PRE_ALLOCATED_USER_DATA_ENTRIES 2

typedef struct
{
  CoglUserDataKey *key;
  void *user_data;
  CoglUserDataDestroyInternalCallback destroy;
} CoglUserDataEntry;

/* All Cogl objects inherit from this base object by adding a member:
 *
 *   CoglObject _parent;
 *
 * at the top of its main structure. This structure is initialized
 * when you call _cogl_#type_name#_object_new (new_object);
 */
struct _CoglObject
{
  CoglObjectClass  *klass; /* equivalent to GTypeInstance */

  CoglUserDataEntry user_data_entry[
    COGL_OBJECT_N_PRE_ALLOCATED_USER_DATA_ENTRIES];
  GArray           *user_data_array;
  int               n_user_data_entries;

  unsigned int      ref_count;
};

/* Helper macro to encapsulate the common code for COGL reference
   counted objects */

#ifdef COGL_OBJECT_DEBUG

#define _COGL_OBJECT_DEBUG_NEW(type_name, obj)                          \
  COGL_NOTE (OBJECT, "COGL " G_STRINGIFY (type_name) " NEW   %p %i",    \
             (obj), (obj)->ref_count)

#define _COGL_OBJECT_DEBUG_REF(type_name, object)       G_STMT_START {  \
  CoglObject *__obj = (CoglObject *)object;                             \
  COGL_NOTE (OBJECT, "COGL %s REF %p %i",                               \
             (__obj)->klass->name,                                      \
             (__obj), (__obj)->ref_count);              } G_STMT_END

#define _COGL_OBJECT_DEBUG_UNREF(type_name, object)     G_STMT_START {  \
  CoglObject *__obj = (CoglObject *)object;                             \
  COGL_NOTE (OBJECT, "COGL %s UNREF %p %i",                             \
             (__obj)->klass->name,                                      \
             (__obj), (__obj)->ref_count - 1);          } G_STMT_END

#define COGL_OBJECT_DEBUG_FREE(obj)                                     \
  COGL_NOTE (OBJECT, "COGL %s FREE %p",                                 \
             (obj)->klass->name, (obj))

#else /* !COGL_OBJECT_DEBUG */

#define _COGL_OBJECT_DEBUG_NEW(type_name, obj)
#define _COGL_OBJECT_DEBUG_REF(type_name, obj)
#define _COGL_OBJECT_DEBUG_UNREF(type_name, obj)
#define COGL_OBJECT_DEBUG_FREE(obj)

#endif /* COGL_OBJECT_DEBUG */

#ifdef COGL_HAS_GTYPE_SUPPORT
#define _COGL_GTYPE_INIT_CLASS(type_name) do {                                   \
  _cogl_##type_name##_class.base_class.g_type = cogl_##type_name##_get_gtype (); \
} while (0)
#else
#define _COGL_GTYPE_INIT_CLASS(type_name)
#endif

#define COGL_OBJECT_COMMON_DEFINE_WITH_CODE(TypeName, type_name, code)  \
                                                                        \
CoglObjectClass _cogl_##type_name##_class;                              \
static unsigned long _cogl_object_##type_name##_count;                  \
                                                                        \
static inline void                                                      \
_cogl_object_##type_name##_inc (void)                                   \
{                                                                       \
  _cogl_object_##type_name##_count++;                                   \
}                                                                       \
                                                                        \
static inline void                                                      \
_cogl_object_##type_name##_dec (void)                                   \
{                                                                       \
  _cogl_object_##type_name##_count--;                                   \
}                                                                       \
                                                                        \
static void                                                             \
_cogl_object_##type_name##_indirect_free (CoglObject *obj)              \
{                                                                       \
  _cogl_##type_name##_free ((Cogl##TypeName *) obj);                    \
  _cogl_object_##type_name##_dec ();                                    \
}                                                                       \
                                                                        \
static void                                                             \
_cogl_object_##type_name##_class_init (void)                            \
{                                                                       \
  _cogl_object_##type_name##_count = 0;                                 \
                                                                        \
    if (_cogl_debug_instances == NULL)                                  \
      _cogl_debug_instances =                                           \
        g_hash_table_new (g_str_hash, g_str_equal);                     \
                                                                        \
    _cogl_##type_name##_class.virt_free =                               \
      _cogl_object_##type_name##_indirect_free;                         \
    _cogl_##type_name##_class.virt_unref =                              \
      _cogl_object_default_unref;                                       \
    _cogl_##type_name##_class.name = "Cogl"#TypeName;                   \
                                                                        \
    g_hash_table_insert (_cogl_debug_instances,                         \
                         (void *) _cogl_##type_name##_class.name,       \
                         &_cogl_object_##type_name##_count);            \
                                                                        \
    { code; }                                                           \
}                                                                       \
                                                                        \
static Cogl##TypeName *                                                 \
_cogl_##type_name##_object_new (Cogl##TypeName *new_obj)                \
{                                                                       \
  CoglObject *obj = (CoglObject *)&new_obj->_parent;                    \
  obj->ref_count = 0;                                                   \
  cogl_object_ref (obj);                                                \
  obj->n_user_data_entries = 0;                                         \
  obj->user_data_array = NULL;                                          \
                                                                        \
  obj->klass = &_cogl_##type_name##_class;                              \
  if (!obj->klass->virt_free)                                           \
    {                                                                   \
      _cogl_object_##type_name##_class_init ();                         \
    }                                                                   \
                                                                        \
  _cogl_object_##type_name##_inc ();                                    \
  _COGL_OBJECT_DEBUG_NEW (TypeName, obj);                               \
  return new_obj;                                                       \
}

#define COGL_OBJECT_DEFINE_WITH_CODE_GTYPE(TypeName, type_name, code)   \
                                                                        \
COGL_OBJECT_COMMON_DEFINE_WITH_CODE(TypeName,                           \
                                    type_name,                          \
                                    do { code; } while (0);             \
                                    _COGL_GTYPE_INIT_CLASS (type_name)) \
                                                                        \
CoglBool                                                                \
cogl_is_##type_name (void *object)                                      \
{                                                                       \
  CoglObject *obj = object;                                             \
                                                                        \
  if (object == NULL)                                                   \
    return FALSE;                                                       \
                                                                        \
  return obj->klass == &_cogl_##type_name##_class;                      \
}

#define COGL_OBJECT_DEFINE_WITH_CODE(TypeName, type_name, code)         \
                                                                        \
COGL_OBJECT_COMMON_DEFINE_WITH_CODE(TypeName, type_name, code)          \
                                                                        \
CoglBool                                                                \
cogl_is_##type_name (void *object)                                      \
{                                                                       \
  CoglObject *obj = object;                                             \
                                                                        \
  if (object == NULL)                                                   \
    return FALSE;                                                       \
                                                                        \
  return obj->klass == &_cogl_##type_name##_class;                      \
}

#define COGL_OBJECT_INTERNAL_DEFINE_WITH_CODE(TypeName, type_name, code) \
                                                                        \
COGL_OBJECT_COMMON_DEFINE_WITH_CODE(TypeName, type_name, code)          \
                                                                        \
CoglBool                                                                \
_cogl_is_##type_name (void *object)                                     \
{                                                                       \
  CoglObject *obj = object;                                             \
                                                                        \
  if (object == NULL)                                                   \
    return FALSE;                                                       \
                                                                        \
  return obj->klass == &_cogl_##type_name##_class;                      \
}

#define COGL_OBJECT_DEFINE_DEPRECATED_REF_COUNTING(type_name)   \
                                                                \
void * G_GNUC_DEPRECATED                                        \
cogl_##type_name##_ref (void *object)                           \
{                                                               \
  if (!cogl_is_##type_name (object))                            \
    return NULL;                                                \
                                                                \
  _COGL_OBJECT_DEBUG_REF (TypeName, object);                    \
                                                                \
  cogl_handle_ref (object);                                     \
                                                                \
  return object;                                                \
}                                                               \
                                                                \
void G_GNUC_DEPRECATED                                          \
cogl_##type_name##_unref (void *object)                         \
{                                                               \
  if (!cogl_is_##type_name (object))                            \
    {                                                           \
      g_warning (G_STRINGIFY (cogl_##type_name##_unref)         \
                 ": Ignoring unref of Cogl handle "             \
                 "due to type mismatch");                       \
      return;                                                   \
    }                                                           \
                                                                \
  _COGL_OBJECT_DEBUG_UNREF (TypeName, object);                  \
                                                                \
  cogl_handle_unref (object);                                   \
}

#define COGL_OBJECT_DEFINE(TypeName, type_name)                 \
  COGL_OBJECT_DEFINE_WITH_CODE_GTYPE (TypeName, type_name, (void) 0)

#define COGL_OBJECT_INTERNAL_DEFINE(TypeName, type_name)         \
  COGL_OBJECT_INTERNAL_DEFINE_WITH_CODE (TypeName, type_name, (void) 0)

/* For temporary compatability */
#define COGL_HANDLE_INTERNAL_DEFINE_WITH_CODE(TypeName, type_name, code) \
                                                                         \
COGL_OBJECT_INTERNAL_DEFINE_WITH_CODE (TypeName, type_name, code)        \
                                                                         \
static Cogl##TypeName *                                                  \
_cogl_##type_name##_handle_new (CoglHandle handle)                       \
{                                                                        \
  return _cogl_##type_name##_object_new (handle);                        \
}

#define COGL_HANDLE_DEFINE_WITH_CODE(TypeName, type_name, code)          \
                                                                         \
COGL_OBJECT_DEFINE_WITH_CODE (TypeName, type_name, code)                 \
                                                                         \
static Cogl##TypeName *                                                  \
_cogl_##type_name##_handle_new (CoglHandle handle)                       \
{                                                                        \
  return _cogl_##type_name##_object_new (handle);                        \
}

#define COGL_HANDLE_DEFINE(TypeName, type_name)                 \
  COGL_HANDLE_DEFINE_WITH_CODE (TypeName, type_name, (void) 0)

void
_cogl_object_set_user_data (CoglObject *object,
                            CoglUserDataKey *key,
                            void *user_data,
                            CoglUserDataDestroyInternalCallback destroy);

void
_cogl_object_default_unref (void *obj);

#endif /* __COGL_OBJECT_PRIVATE_H */