4
* An object oriented GL/GLES Abstraction/Utility Layer
6
* Copyright (C) 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 <http://www.gnu.org/licenses/>.
24
* Damien Lespiau <damien.lespiau@intel.com>
27
/* For an overview of the functionality implemented here, please see
28
* cogl-buffer.h, which contains the gtk-doc section overview for the
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"
48
* GL/GLES compatibility defines for the buffer API:
51
#if defined (HAVE_COGL_GL)
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
67
#elif defined (HAVE_COGL_GLES2)
69
#include "../gles/cogl-gles2-wrapper.h"
73
void cogl_buffer_unmap_EXP (CoglHandle handle);
76
cogl_is_buffer_EXP (CoglHandle handle)
78
CoglHandleObject *obj = (CoglHandleObject *) handle;
80
if (handle == COGL_INVALID_HANDLE)
83
return obj->klass->type == _cogl_handle_pixel_buffer_get_type ();
87
_cogl_buffer_initialize (CoglBuffer *buffer,
89
CoglBufferUsageHint usage_hint,
90
CoglBufferUpdateHint update_hint)
92
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
94
buffer->flags = COGL_BUFFER_FLAG_NONE;
96
buffer->usage_hint = usage_hint;
97
buffer->update_hint = update_hint;
102
_cogl_buffer_fini (CoglBuffer *buffer)
104
if (COGL_BUFFER_FLAG_IS_SET (buffer, MAPPED))
105
cogl_buffer_unmap (buffer);
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)
112
_cogl_buffer_access_to_gl_enum (CoglBufferAccess access)
118
_cogl_buffer_access_to_gl_enum (CoglBufferAccess access)
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;
129
/* OpenGL ES 1.1 and 2 only know about STATIC_DRAW and DYNAMIC_DRAW */
130
#if defined (COGL_HAS_GLES)
132
_cogl_buffer_hints_to_gl_enum (CoglBufferUsageHint usage_hint,
133
CoglBufferUpdateHint update_hint)
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;
142
_cogl_buffer_hints_to_gl_enum (CoglBufferUsageHint usage_hint,
143
CoglBufferUpdateHint update_hint)
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;
153
return GL_STATIC_DRAW;
158
_cogl_buffer_bind (CoglBuffer *buffer,
161
_COGL_GET_CONTEXT (ctx, NO_RETVAL);
163
/* Don't bind again an already bound pbo */
164
if (ctx->current_pbo == buffer)
167
if (buffer && COGL_BUFFER_FLAG_IS_SET (buffer, BUFFER_OBJECT))
169
GE( glBindBuffer (target, buffer->gl_handle) );
171
else if (buffer == NULL &&
173
COGL_BUFFER_FLAG_IS_SET (ctx->current_pbo, BUFFER_OBJECT))
175
GE( glBindBuffer (target, 0) );
178
ctx->current_pbo = buffer;
182
cogl_buffer_get_size_EXP (CoglHandle handle)
184
if (!cogl_is_buffer (handle))
187
return COGL_BUFFER (handle)->size;
191
cogl_buffer_set_usage_hint_EXP (CoglHandle handle,
192
CoglBufferUsageHint hint)
194
if (!cogl_is_buffer (handle))
197
if (G_UNLIKELY (hint > COGL_BUFFER_USAGE_HINT_TEXTURE))
198
hint = COGL_BUFFER_USAGE_HINT_TEXTURE;
200
COGL_BUFFER (handle)->usage_hint = hint;
204
cogl_buffer_get_usage_hint_EXP (CoglHandle handle)
206
if (!cogl_is_buffer (handle))
209
return COGL_BUFFER (handle)->usage_hint;
213
cogl_buffer_set_update_hint_EXP (CoglHandle handle,
214
CoglBufferUpdateHint hint)
216
if (!cogl_is_buffer (handle))
219
if (G_UNLIKELY (hint > COGL_BUFFER_UPDATE_HINT_STREAM))
220
hint = COGL_BUFFER_UPDATE_HINT_STATIC;
222
COGL_BUFFER (handle)->update_hint = hint;
226
cogl_buffer_get_update_hint_EXP (CoglHandle handle)
228
if (!cogl_is_buffer (handle))
231
return COGL_BUFFER (handle)->update_hint;
235
cogl_buffer_map_EXP (CoglHandle handle,
236
CoglBufferAccess access)
240
if (!cogl_is_buffer (handle))
243
buffer = COGL_BUFFER (handle);
245
if (COGL_BUFFER_FLAG_IS_SET (buffer, MAPPED))
248
buffer->data = buffer->vtable->map (buffer, access);
253
cogl_buffer_unmap_EXP (CoglHandle handle)
257
if (!cogl_is_buffer (handle))
260
buffer = COGL_BUFFER (handle);
262
if (!COGL_BUFFER_FLAG_IS_SET (buffer, MAPPED))
265
buffer->vtable->unmap (buffer);
269
cogl_buffer_set_data_EXP (CoglHandle handle,
276
if (!cogl_is_buffer (handle))
279
buffer = COGL_BUFFER (handle);
281
if (G_UNLIKELY((offset + size) > buffer->size))
284
return buffer->vtable->set_data (buffer, offset, data, size);