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

« back to all changes in this revision

Viewing changes to clutter/cogl/cogl/cogl-buffer.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) 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
 * Authors:
 
24
 *   Damien Lespiau <damien.lespiau@intel.com>
 
25
 */
 
26
 
 
27
/* For an overview of the functionality implemented here, please see
 
28
 * cogl-buffer.h, which contains the gtk-doc section overview for the
 
29
 * Pixel Buffers API.
 
30
 */
 
31
 
 
32
#ifdef HAVE_CONFIG_H
 
33
#include "config.h"
 
34
#endif
 
35
 
 
36
#include <stdio.h>
 
37
#include <string.h>
 
38
#include <glib.h>
 
39
 
 
40
#include "cogl.h"
 
41
#include "cogl-internal.h"
 
42
#include "cogl-util.h"
 
43
#include "cogl-context.h"
 
44
#include "cogl-handle.h"
 
45
#include "cogl-pixel-buffer-private.h"
 
46
 
 
47
/*
 
48
 * GL/GLES compatibility defines for the buffer API:
 
49
 */
 
50
 
 
51
#if defined (HAVE_COGL_GL)
 
52
 
 
53
#define glGenBuffers ctx->drv.pf_glGenBuffers
 
54
#define glBindBuffer ctx->drv.pf_glBindBuffer
 
55
#define glBufferData ctx->drv.pf_glBufferData
 
56
#define glBufferSubData ctx->drv.pf_glBufferSubData
 
57
#define glGetBufferSubData ctx->drv.pf_glGetBufferSubData
 
58
#define glDeleteBuffers ctx->drv.pf_glDeleteBuffers
 
59
#define glMapBuffer ctx->drv.pf_glMapBuffer
 
60
#define glUnmapBuffer ctx->drv.pf_glUnmapBuffer
 
61
#define glActiveTexture ctx->drv.pf_glActiveTexture
 
62
#define glClientActiveTexture ctx->drv.pf_glClientActiveTexture
 
63
#ifndef GL_ARRAY_BUFFER
 
64
#define GL_ARRAY_BUFFER GL_ARRAY_BUFFER_ARB
 
65
#endif
 
66
 
 
67
#elif defined (HAVE_COGL_GLES2)
 
68
 
 
69
#include "../gles/cogl-gles2-wrapper.h"
 
70
 
 
71
#endif
 
72
 
 
73
void cogl_buffer_unmap_EXP (CoglHandle handle);
 
74
 
 
75
gboolean
 
76
cogl_is_buffer_EXP (CoglHandle handle)
 
77
{
 
78
  CoglHandleObject *obj = (CoglHandleObject *) handle;
 
79
 
 
80
  if (handle == COGL_INVALID_HANDLE)
 
81
    return FALSE;
 
82
 
 
83
  return obj->klass->type == _cogl_handle_pixel_buffer_get_type ();
 
84
}
 
85
 
 
86
void
 
87
_cogl_buffer_initialize (CoglBuffer           *buffer,
 
88
                         unsigned int          size,
 
89
                         CoglBufferUsageHint   usage_hint,
 
90
                         CoglBufferUpdateHint  update_hint)
 
91
{
 
92
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
93
 
 
94
  buffer->flags       = COGL_BUFFER_FLAG_NONE;
 
95
  buffer->size        = size;
 
96
  buffer->usage_hint  = usage_hint;
 
97
  buffer->update_hint = update_hint;
 
98
  buffer->data        = NULL;
 
99
}
 
100
 
 
101
void
 
102
_cogl_buffer_fini (CoglBuffer *buffer)
 
103
{
 
104
  if (COGL_BUFFER_FLAG_IS_SET (buffer, MAPPED))
 
105
    cogl_buffer_unmap (buffer);
 
106
}
 
107
 
 
108
/* OpenGL ES 1.1 and 2 have a GL_OES_mapbuffer extension that is able to map
 
109
 * VBOs for write only, we don't support that in CoglBuffer */
 
110
#if defined (COGL_HAS_GLES)
 
111
GLenum
 
112
_cogl_buffer_access_to_gl_enum (CoglBufferAccess access)
 
113
{
 
114
  return 0;
 
115
}
 
116
#else
 
117
GLenum
 
118
_cogl_buffer_access_to_gl_enum (CoglBufferAccess access)
 
119
{
 
120
  if ((access & COGL_BUFFER_ACCESS_READ_WRITE) == COGL_BUFFER_ACCESS_READ_WRITE)
 
121
    return GL_READ_WRITE;
 
122
  else if (access & COGL_BUFFER_ACCESS_WRITE)
 
123
    return GL_WRITE_ONLY;
 
124
  else
 
125
    return GL_READ_ONLY;
 
126
}
 
127
#endif
 
128
 
 
129
/* OpenGL ES 1.1 and 2 only know about STATIC_DRAW and DYNAMIC_DRAW */
 
130
#if defined (COGL_HAS_GLES)
 
131
GLenum
 
132
_cogl_buffer_hints_to_gl_enum (CoglBufferUsageHint usage_hint,
 
133
                               CoglBufferUpdateHint update_hint)
 
134
{
 
135
  /* usage hint is always TEXTURE for now */
 
136
  if (update_hint == COGL_BUFFER_UPDATE_HINT_STATIC)
 
137
      return GL_STATIC_DRAW;
 
138
  return GL_DYNAMIC_DRAW;
 
139
}
 
140
#else
 
141
GLenum
 
142
_cogl_buffer_hints_to_gl_enum (CoglBufferUsageHint  usage_hint,
 
143
                               CoglBufferUpdateHint update_hint)
 
144
{
 
145
  /* usage hint is always TEXTURE for now */
 
146
  if (update_hint == COGL_BUFFER_UPDATE_HINT_STATIC)
 
147
    return GL_STATIC_DRAW;
 
148
  if (update_hint == COGL_BUFFER_UPDATE_HINT_DYNAMIC)
 
149
    return GL_DYNAMIC_DRAW;
 
150
  if (update_hint == COGL_BUFFER_UPDATE_HINT_STREAM)
 
151
    return GL_STREAM_DRAW;
 
152
 
 
153
  return GL_STATIC_DRAW;
 
154
}
 
155
#endif
 
156
 
 
157
void
 
158
_cogl_buffer_bind (CoglBuffer *buffer,
 
159
                   GLenum      target)
 
160
{
 
161
  _COGL_GET_CONTEXT (ctx, NO_RETVAL);
 
162
 
 
163
  /* Don't bind again an already bound pbo */
 
164
  if (ctx->current_pbo == buffer)
 
165
    return;
 
166
 
 
167
  if (buffer && COGL_BUFFER_FLAG_IS_SET (buffer, BUFFER_OBJECT))
 
168
    {
 
169
      GE( glBindBuffer (target, buffer->gl_handle) );
 
170
    }
 
171
  else if (buffer == NULL &&
 
172
           ctx->current_pbo &&
 
173
           COGL_BUFFER_FLAG_IS_SET (ctx->current_pbo, BUFFER_OBJECT))
 
174
    {
 
175
      GE( glBindBuffer (target, 0) );
 
176
    }
 
177
 
 
178
  ctx->current_pbo = buffer;
 
179
}
 
180
 
 
181
unsigned int
 
182
cogl_buffer_get_size_EXP (CoglHandle handle)
 
183
{
 
184
  if (!cogl_is_buffer (handle))
 
185
    return 0;
 
186
 
 
187
  return COGL_BUFFER (handle)->size;
 
188
}
 
189
 
 
190
void
 
191
cogl_buffer_set_usage_hint_EXP (CoglHandle          handle,
 
192
                                CoglBufferUsageHint hint)
 
193
{
 
194
  if (!cogl_is_buffer (handle))
 
195
    return;
 
196
 
 
197
  if (G_UNLIKELY (hint > COGL_BUFFER_USAGE_HINT_TEXTURE))
 
198
    hint = COGL_BUFFER_USAGE_HINT_TEXTURE;
 
199
 
 
200
  COGL_BUFFER (handle)->usage_hint = hint;
 
201
}
 
202
 
 
203
CoglBufferUsageHint
 
204
cogl_buffer_get_usage_hint_EXP (CoglHandle handle)
 
205
{
 
206
  if (!cogl_is_buffer (handle))
 
207
    return FALSE;
 
208
 
 
209
  return COGL_BUFFER (handle)->usage_hint;
 
210
}
 
211
 
 
212
void
 
213
cogl_buffer_set_update_hint_EXP (CoglHandle          handle,
 
214
                                 CoglBufferUpdateHint hint)
 
215
{
 
216
  if (!cogl_is_buffer (handle))
 
217
    return;
 
218
 
 
219
  if (G_UNLIKELY (hint > COGL_BUFFER_UPDATE_HINT_STREAM))
 
220
    hint = COGL_BUFFER_UPDATE_HINT_STATIC;
 
221
 
 
222
  COGL_BUFFER (handle)->update_hint = hint;
 
223
}
 
224
 
 
225
CoglBufferUpdateHint
 
226
cogl_buffer_get_update_hint_EXP (CoglHandle handle)
 
227
{
 
228
  if (!cogl_is_buffer (handle))
 
229
    return FALSE;
 
230
 
 
231
  return COGL_BUFFER (handle)->update_hint;
 
232
}
 
233
 
 
234
guint8 *
 
235
cogl_buffer_map_EXP (CoglHandle       handle,
 
236
                     CoglBufferAccess access)
 
237
{
 
238
  CoglBuffer *buffer;
 
239
 
 
240
  if (!cogl_is_buffer (handle))
 
241
    return FALSE;
 
242
 
 
243
  buffer = COGL_BUFFER (handle);
 
244
 
 
245
  if (COGL_BUFFER_FLAG_IS_SET (buffer, MAPPED))
 
246
    return buffer->data;
 
247
 
 
248
  buffer->data = buffer->vtable->map (buffer, access);
 
249
  return buffer->data;
 
250
}
 
251
 
 
252
void
 
253
cogl_buffer_unmap_EXP (CoglHandle handle)
 
254
{
 
255
  CoglBuffer *buffer;
 
256
 
 
257
  if (!cogl_is_buffer (handle))
 
258
    return;
 
259
 
 
260
  buffer = COGL_BUFFER (handle);
 
261
 
 
262
  if (!COGL_BUFFER_FLAG_IS_SET (buffer, MAPPED))
 
263
    return;
 
264
 
 
265
  buffer->vtable->unmap (buffer);
 
266
}
 
267
 
 
268
gboolean
 
269
cogl_buffer_set_data_EXP (CoglHandle    handle,
 
270
                          gsize         offset,
 
271
                          const guint8 *data,
 
272
                          gsize         size)
 
273
{
 
274
  CoglBuffer *buffer;
 
275
 
 
276
  if (!cogl_is_buffer (handle))
 
277
    return FALSE;
 
278
 
 
279
  buffer = COGL_BUFFER (handle);
 
280
 
 
281
  if (G_UNLIKELY((offset + size) > buffer->size))
 
282
    return FALSE;
 
283
 
 
284
  return buffer->vtable->set_data (buffer, offset, data, size);
 
285
}