~oem-solutions-group/unity-2d/clutter-1.0

« back to all changes in this revision

Viewing changes to clutter/cogl/cogl/cogl-util.c

  • Committer: Bazaar Package Importer
  • Author(s): Emilio Pozuelo Monfort
  • Date: 2010-03-21 13:27:56 UTC
  • mto: (2.1.3 experimental)
  • mto: This revision was merged to the branch mainline in revision 8.
  • Revision ID: james.westby@ubuntu.com-20100321132756-nf8yd30yxo3zzwcm
Tags: upstream-1.2.2
ImportĀ upstreamĀ versionĀ 1.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Cogl
 
3
 *
 
4
 * An object oriented GL/GLES Abstraction/Utility Layer
 
5
 *
 
6
 * Copyright (C) 2007,2008,2009,2010 Intel Corporation.
 
7
 *
 
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.
 
12
 *
 
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.
 
17
 *
 
18
 * You should have received a copy of the GNU Lesser General Public
 
19
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
 
20
 *
 
21
 *
 
22
 */
 
23
 
 
24
#ifdef HAVE_CONFIG_H
 
25
#include "config.h"
 
26
#endif
 
27
 
 
28
#include <glib-object.h>
 
29
#include <gobject/gvaluecollector.h>
 
30
 
 
31
#include "cogl.h"
 
32
 
 
33
#include "cogl-fixed.h"
 
34
#include "cogl-internal.h"
 
35
#include "cogl-material.h"
 
36
#include "cogl-offscreen.h"
 
37
#include "cogl-shader.h"
 
38
#include "cogl-texture.h"
 
39
#include "cogl-types.h"
 
40
#include "cogl-handle.h"
 
41
#include "cogl-util.h"
 
42
 
 
43
/**
 
44
 * cogl_util_next_p2:
 
45
 * @a: Value to get the next power
 
46
 *
 
47
 * Calculates the next power greater than @a.
 
48
 *
 
49
 * Return value: The next power after @a.
 
50
 */
 
51
int
 
52
cogl_util_next_p2 (int a)
 
53
{
 
54
  int rval = 1;
 
55
 
 
56
  while (rval < a)
 
57
    rval <<= 1;
 
58
 
 
59
  return rval;
 
60
}
 
61
 
 
62
/* gtypes */
 
63
 
 
64
CoglHandle
 
65
cogl_handle_ref (CoglHandle handle)
 
66
{
 
67
  CoglHandleObject *obj = (CoglHandleObject *)handle;
 
68
 
 
69
  g_return_val_if_fail (handle != COGL_INVALID_HANDLE, COGL_INVALID_HANDLE);
 
70
 
 
71
  obj->ref_count++;
 
72
  return handle;
 
73
}
 
74
 
 
75
void
 
76
cogl_handle_unref (CoglHandle handle)
 
77
{
 
78
  CoglHandleObject *obj = (CoglHandleObject *)handle;
 
79
 
 
80
  g_return_if_fail (handle != COGL_INVALID_HANDLE);
 
81
  g_return_if_fail (obj->ref_count > 0);
 
82
 
 
83
  if (--obj->ref_count < 1)
 
84
    {
 
85
      void (*free_func)(void *obj);
 
86
 
 
87
      COGL_HANDLE_DEBUG_FREE (obj);
 
88
      free_func = obj->klass->virt_free;
 
89
      free_func (obj);
 
90
    }
 
91
}
 
92
 
 
93
GType
 
94
cogl_handle_get_type (void)
 
95
{
 
96
  static GType our_type = 0;
 
97
 
 
98
  if (G_UNLIKELY (our_type == 0))
 
99
    our_type = g_boxed_type_register_static (g_intern_static_string ("CoglHandle"),
 
100
                                            (GBoxedCopyFunc) cogl_handle_ref,
 
101
                                            (GBoxedFreeFunc) cogl_handle_unref);
 
102
 
 
103
  return our_type;
 
104
}
 
105
 
 
106
/*
 
107
 * CoglFixed
 
108
 */
 
109
 
 
110
static GTypeInfo _info = {
 
111
  0,
 
112
  NULL,
 
113
  NULL,
 
114
  NULL,
 
115
  NULL,
 
116
  NULL,
 
117
  0,
 
118
  0,
 
119
  NULL,
 
120
  NULL,
 
121
};
 
122
 
 
123
static GTypeFundamentalInfo _finfo = { 0, };
 
124
 
 
125
static void
 
126
cogl_value_init_fixed (GValue *value)
 
127
{
 
128
  value->data[0].v_int = 0;
 
129
}
 
130
 
 
131
static void
 
132
cogl_value_copy_fixed (const GValue *src,
 
133
                       GValue       *dest)
 
134
{
 
135
  dest->data[0].v_int = src->data[0].v_int;
 
136
}
 
137
 
 
138
static char *
 
139
cogl_value_collect_fixed (GValue       *value,
 
140
                          unsigned int  n_collect_values,
 
141
                          GTypeCValue  *collect_values,
 
142
                          unsigned int  collect_flags)
 
143
{
 
144
  value->data[0].v_int = collect_values[0].v_int;
 
145
 
 
146
  return NULL;
 
147
}
 
148
 
 
149
static char *
 
150
cogl_value_lcopy_fixed (const GValue *value,
 
151
                        unsigned int  n_collect_values,
 
152
                        GTypeCValue  *collect_values,
 
153
                        unsigned int  collect_flags)
 
154
{
 
155
  gint32 *fixed_p = collect_values[0].v_pointer;
 
156
 
 
157
  if (!fixed_p)
 
158
    return g_strdup_printf ("value location for '%s' passed as NULL",
 
159
                            G_VALUE_TYPE_NAME (value));
 
160
 
 
161
  *fixed_p = value->data[0].v_int;
 
162
 
 
163
  return NULL;
 
164
}
 
165
 
 
166
static void
 
167
cogl_value_transform_fixed_int (const GValue *src,
 
168
                                GValue       *dest)
 
169
{
 
170
  dest->data[0].v_int = COGL_FIXED_TO_INT (src->data[0].v_int);
 
171
}
 
172
 
 
173
static void
 
174
cogl_value_transform_fixed_double (const GValue *src,
 
175
                                      GValue       *dest)
 
176
{
 
177
  dest->data[0].v_double = COGL_FIXED_TO_DOUBLE (src->data[0].v_int);
 
178
}
 
179
 
 
180
static void
 
181
cogl_value_transform_fixed_float (const GValue *src,
 
182
                                  GValue       *dest)
 
183
{
 
184
  dest->data[0].v_float = COGL_FIXED_TO_FLOAT (src->data[0].v_int);
 
185
}
 
186
 
 
187
static void
 
188
cogl_value_transform_int_fixed (const GValue *src,
 
189
                                GValue       *dest)
 
190
{
 
191
  dest->data[0].v_int = COGL_FIXED_FROM_INT (src->data[0].v_int);
 
192
}
 
193
 
 
194
static void
 
195
cogl_value_transform_double_fixed (const GValue *src,
 
196
                                   GValue       *dest)
 
197
{
 
198
  dest->data[0].v_int = COGL_FIXED_FROM_DOUBLE (src->data[0].v_double);
 
199
}
 
200
 
 
201
static void
 
202
cogl_value_transform_float_fixed (const GValue *src,
 
203
                                  GValue       *dest)
 
204
{
 
205
  dest->data[0].v_int = COGL_FIXED_FROM_FLOAT (src->data[0].v_float);
 
206
}
 
207
 
 
208
 
 
209
static const GTypeValueTable _cogl_fixed_value_table = {
 
210
  cogl_value_init_fixed,
 
211
  NULL,
 
212
  cogl_value_copy_fixed,
 
213
  NULL,
 
214
  "i",
 
215
  cogl_value_collect_fixed,
 
216
  "p",
 
217
  cogl_value_lcopy_fixed
 
218
};
 
219
 
 
220
GType
 
221
cogl_fixed_get_type (void)
 
222
{
 
223
  static GType _cogl_fixed_type = 0;
 
224
 
 
225
  if (G_UNLIKELY (_cogl_fixed_type == 0))
 
226
    {
 
227
      _info.value_table = & _cogl_fixed_value_table;
 
228
      _cogl_fixed_type =
 
229
        g_type_register_fundamental (g_type_fundamental_next (),
 
230
                                     g_intern_static_string ("CoglFixed"),
 
231
                                     &_info, &_finfo, 0);
 
232
 
 
233
      g_value_register_transform_func (_cogl_fixed_type, G_TYPE_INT,
 
234
                                       cogl_value_transform_fixed_int);
 
235
      g_value_register_transform_func (G_TYPE_INT, _cogl_fixed_type,
 
236
                                       cogl_value_transform_int_fixed);
 
237
 
 
238
      g_value_register_transform_func (_cogl_fixed_type, G_TYPE_FLOAT,
 
239
                                       cogl_value_transform_fixed_float);
 
240
      g_value_register_transform_func (G_TYPE_FLOAT, _cogl_fixed_type,
 
241
                                       cogl_value_transform_float_fixed);
 
242
 
 
243
      g_value_register_transform_func (_cogl_fixed_type, G_TYPE_DOUBLE,
 
244
                                       cogl_value_transform_fixed_double);
 
245
      g_value_register_transform_func (G_TYPE_DOUBLE, _cogl_fixed_type,
 
246
                                       cogl_value_transform_double_fixed);
 
247
    }
 
248
 
 
249
  return _cogl_fixed_type;
 
250
}
 
251
 
 
252