2010-01-10 12:28:24 -05:00
|
|
|
/*
|
|
|
|
* Cogl
|
|
|
|
*
|
|
|
|
* An object oriented GL/GLES Abstraction/Utility Layer
|
|
|
|
*
|
|
|
|
* Copyright (C) 2010 Intel Corporation.
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Lesser General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Lesser General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Lesser General Public
|
2010-03-01 07:56:10 -05:00
|
|
|
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*
|
2010-01-10 12:28:24 -05:00
|
|
|
*
|
|
|
|
* Authors:
|
|
|
|
* Damien Lespiau <damien.lespiau@intel.com>
|
2010-07-05 11:14:00 -04:00
|
|
|
* Robert Bragg <robert@linux.intel.com>
|
2010-01-10 12:28:24 -05:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* For an overview of the functionality implemented here, please see
|
|
|
|
* cogl-buffer.h, which contains the gtk-doc section overview for the
|
|
|
|
* Pixel Buffers API.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <glib.h>
|
|
|
|
|
|
|
|
#include "cogl-internal.h"
|
|
|
|
#include "cogl-util.h"
|
2010-11-04 18:25:52 -04:00
|
|
|
#include "cogl-context-private.h"
|
2010-01-10 12:28:24 -05:00
|
|
|
#include "cogl-handle.h"
|
2011-03-02 10:19:57 -05:00
|
|
|
#include "cogl-pixel-buffer-private.h"
|
2010-01-10 12:28:24 -05:00
|
|
|
|
|
|
|
/*
|
|
|
|
* GL/GLES compatibility defines for the buffer API:
|
|
|
|
*/
|
|
|
|
|
2010-07-05 18:24:34 -04:00
|
|
|
#ifndef GL_PIXEL_PACK_BUFFER
|
|
|
|
#define GL_PIXEL_PACK_BUFFER 0x88EB
|
|
|
|
#endif
|
|
|
|
#ifndef GL_PIXEL_UNPACK_BUFFER
|
|
|
|
#define GL_PIXEL_UNPACK_BUFFER 0x88EC
|
|
|
|
#endif
|
|
|
|
#ifndef GL_ARRAY_BUFFER
|
|
|
|
#define GL_ARRAY_BUFFER 0x8892
|
|
|
|
#endif
|
|
|
|
#ifndef GL_ELEMENT_ARRAY_BUFFER
|
|
|
|
#define GL_ARRAY_BUFFER 0x8893
|
|
|
|
#endif
|
2011-01-13 10:54:50 -05:00
|
|
|
#ifndef GL_READ_ONLY
|
|
|
|
#define GL_READ_ONLY 0x88B8
|
|
|
|
#endif
|
|
|
|
#ifndef GL_WRITE_ONLY
|
|
|
|
#define GL_WRITE_ONLY 0x88B9
|
|
|
|
#endif
|
|
|
|
#ifndef GL_READ_WRITE
|
|
|
|
#define GL_READ_WRITE 0x88BA
|
|
|
|
#endif
|
2010-07-05 18:24:34 -04:00
|
|
|
|
2010-07-03 18:56:44 -04:00
|
|
|
/* XXX:
|
|
|
|
* The CoglHandle macros don't support any form of inheritance, so for
|
|
|
|
* now we implement the CoglObject support for the CoglBuffer
|
|
|
|
* abstract class manually.
|
|
|
|
*/
|
|
|
|
|
2012-02-06 12:08:58 -05:00
|
|
|
static GSList *_cogl_buffer_types;
|
|
|
|
|
2010-07-03 18:56:44 -04:00
|
|
|
void
|
2012-01-24 11:24:26 -05:00
|
|
|
_cogl_buffer_register_buffer_type (const CoglObjectClass *klass)
|
2010-07-03 18:56:44 -04:00
|
|
|
{
|
2012-02-06 12:08:58 -05:00
|
|
|
_cogl_buffer_types = g_slist_prepend (_cogl_buffer_types, (void *) klass);
|
2010-07-03 18:56:44 -04:00
|
|
|
}
|
|
|
|
|
2010-01-10 12:28:24 -05:00
|
|
|
gboolean
|
2010-06-30 13:06:04 -04:00
|
|
|
cogl_is_buffer (const void *object)
|
2010-01-10 12:28:24 -05:00
|
|
|
{
|
2010-07-03 18:56:44 -04:00
|
|
|
const CoglHandleObject *obj = object;
|
|
|
|
GSList *l;
|
2010-01-10 12:28:24 -05:00
|
|
|
|
2010-07-03 18:56:44 -04:00
|
|
|
if (object == NULL)
|
2010-01-10 12:28:24 -05:00
|
|
|
return FALSE;
|
|
|
|
|
2012-02-06 12:08:58 -05:00
|
|
|
for (l = _cogl_buffer_types; l; l = l->next)
|
2012-01-24 11:24:26 -05:00
|
|
|
if (l->data == obj->klass)
|
2010-07-03 18:56:44 -04:00
|
|
|
return TRUE;
|
|
|
|
|
|
|
|
return FALSE;
|
2010-01-10 12:28:24 -05:00
|
|
|
}
|
|
|
|
|
2010-07-05 20:21:43 -04:00
|
|
|
static GLenum
|
|
|
|
convert_bind_target_to_gl_target (CoglBufferBindTarget target)
|
|
|
|
{
|
|
|
|
switch (target)
|
|
|
|
{
|
|
|
|
case COGL_BUFFER_BIND_TARGET_PIXEL_PACK:
|
|
|
|
return GL_PIXEL_PACK_BUFFER;
|
|
|
|
case COGL_BUFFER_BIND_TARGET_PIXEL_UNPACK:
|
|
|
|
return GL_PIXEL_UNPACK_BUFFER;
|
2011-03-02 10:01:41 -05:00
|
|
|
case COGL_BUFFER_BIND_TARGET_ATTRIBUTE_BUFFER:
|
2010-07-05 20:21:43 -04:00
|
|
|
return GL_ARRAY_BUFFER;
|
2011-03-02 18:31:19 -05:00
|
|
|
case COGL_BUFFER_BIND_TARGET_INDEX_BUFFER:
|
2010-07-05 20:21:43 -04:00
|
|
|
return GL_ELEMENT_ARRAY_BUFFER;
|
|
|
|
default:
|
|
|
|
g_return_val_if_reached (COGL_BUFFER_BIND_TARGET_PIXEL_UNPACK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-25 14:14:25 -05:00
|
|
|
static void *
|
|
|
|
_cogl_buffer_bind_no_create (CoglBuffer *buffer,
|
|
|
|
CoglBufferBindTarget target)
|
|
|
|
{
|
|
|
|
CoglContext *ctx = buffer->context;
|
|
|
|
|
|
|
|
_COGL_RETURN_VAL_IF_FAIL (buffer != NULL, NULL);
|
|
|
|
|
|
|
|
/* Don't allow binding the buffer to multiple targets at the same time */
|
|
|
|
_COGL_RETURN_VAL_IF_FAIL (ctx->current_buffer[buffer->last_target] != buffer,
|
|
|
|
NULL);
|
|
|
|
|
|
|
|
/* Don't allow nesting binds to the same target */
|
|
|
|
_COGL_RETURN_VAL_IF_FAIL (ctx->current_buffer[target] == NULL, NULL);
|
|
|
|
|
|
|
|
buffer->last_target = target;
|
|
|
|
ctx->current_buffer[target] = buffer;
|
|
|
|
|
|
|
|
if (buffer->flags & COGL_BUFFER_FLAG_BUFFER_OBJECT)
|
|
|
|
{
|
|
|
|
GLenum gl_target = convert_bind_target_to_gl_target (buffer->last_target);
|
|
|
|
GE( ctx, glBindBuffer (gl_target, buffer->gl_handle) );
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
return buffer->data;
|
|
|
|
}
|
|
|
|
|
2012-02-06 12:08:58 -05:00
|
|
|
static GLenum
|
|
|
|
_cogl_buffer_hints_to_gl_enum (CoglBufferUsageHint usage_hint,
|
|
|
|
CoglBufferUpdateHint update_hint)
|
|
|
|
{
|
|
|
|
/* usage hint is always TEXTURE for now */
|
|
|
|
if (update_hint == COGL_BUFFER_UPDATE_HINT_STATIC)
|
|
|
|
return GL_STATIC_DRAW;
|
|
|
|
if (update_hint == COGL_BUFFER_UPDATE_HINT_DYNAMIC)
|
|
|
|
return GL_DYNAMIC_DRAW;
|
|
|
|
/* OpenGL ES 1.1 and 2 only know about STATIC_DRAW and DYNAMIC_DRAW */
|
|
|
|
#ifdef HAVE_COGL_GL
|
|
|
|
if (update_hint == COGL_BUFFER_UPDATE_HINT_STREAM)
|
|
|
|
return GL_STREAM_DRAW;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return GL_STATIC_DRAW;
|
|
|
|
}
|
|
|
|
|
2012-02-25 14:14:25 -05:00
|
|
|
static void
|
|
|
|
bo_recreate_store (CoglBuffer *buffer)
|
|
|
|
{
|
|
|
|
GLenum gl_target;
|
|
|
|
GLenum gl_enum;
|
|
|
|
|
|
|
|
/* This assumes the buffer is already bound */
|
|
|
|
|
|
|
|
gl_target = convert_bind_target_to_gl_target (buffer->last_target);
|
|
|
|
gl_enum = _cogl_buffer_hints_to_gl_enum (buffer->usage_hint,
|
|
|
|
buffer->update_hint);
|
|
|
|
|
|
|
|
GE( buffer->context, glBufferData (gl_target,
|
|
|
|
buffer->size,
|
|
|
|
NULL,
|
|
|
|
gl_enum) );
|
|
|
|
buffer->store_created = TRUE;
|
|
|
|
}
|
|
|
|
|
2010-11-04 12:01:23 -04:00
|
|
|
static void *
|
2010-07-05 20:21:43 -04:00
|
|
|
bo_map (CoglBuffer *buffer,
|
|
|
|
CoglBufferAccess access,
|
|
|
|
CoglBufferMapHint hints)
|
|
|
|
{
|
|
|
|
guint8 *data;
|
|
|
|
CoglBufferBindTarget target;
|
|
|
|
GLenum gl_target;
|
2012-02-06 12:08:58 -05:00
|
|
|
CoglContext *ctx = buffer->context;
|
2010-07-05 20:21:43 -04:00
|
|
|
|
2011-01-13 10:54:50 -05:00
|
|
|
if ((access & COGL_BUFFER_ACCESS_READ) &&
|
2011-10-12 17:31:12 -04:00
|
|
|
!cogl_has_feature (ctx, COGL_FEATURE_ID_MAP_BUFFER_FOR_READ))
|
2011-01-13 10:54:50 -05:00
|
|
|
return NULL;
|
|
|
|
if ((access & COGL_BUFFER_ACCESS_WRITE) &&
|
2011-10-12 17:31:12 -04:00
|
|
|
!cogl_has_feature (ctx, COGL_FEATURE_ID_MAP_BUFFER_FOR_WRITE))
|
2011-01-13 10:54:50 -05:00
|
|
|
return NULL;
|
|
|
|
|
2010-07-05 20:21:43 -04:00
|
|
|
target = buffer->last_target;
|
2012-02-25 14:14:25 -05:00
|
|
|
_cogl_buffer_bind_no_create (buffer, target);
|
2010-07-05 20:21:43 -04:00
|
|
|
|
|
|
|
gl_target = convert_bind_target_to_gl_target (target);
|
|
|
|
|
|
|
|
/* create an empty store if we don't have one yet. creating the store
|
|
|
|
* lazily allows the user of the CoglBuffer to set a hint before the
|
|
|
|
* store is created. */
|
2010-10-26 11:06:46 -04:00
|
|
|
if (!buffer->store_created || (hints & COGL_BUFFER_MAP_HINT_DISCARD))
|
2012-02-25 14:14:25 -05:00
|
|
|
bo_recreate_store (buffer);
|
2010-07-05 20:21:43 -04:00
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
GE_RET( data, ctx, glMapBuffer (gl_target,
|
|
|
|
_cogl_buffer_access_to_gl_enum (access)) );
|
2010-07-05 20:21:43 -04:00
|
|
|
if (data)
|
2010-10-12 06:46:29 -04:00
|
|
|
buffer->flags |= COGL_BUFFER_FLAG_MAPPED;
|
2010-07-05 20:21:43 -04:00
|
|
|
|
|
|
|
_cogl_buffer_unbind (buffer);
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
bo_unmap (CoglBuffer *buffer)
|
|
|
|
{
|
2012-02-06 12:08:58 -05:00
|
|
|
CoglContext *ctx = buffer->context;
|
2010-07-05 20:21:43 -04:00
|
|
|
|
2012-02-25 14:14:25 -05:00
|
|
|
_cogl_buffer_bind_no_create (buffer, buffer->last_target);
|
2010-07-05 20:21:43 -04:00
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
GE( ctx, glUnmapBuffer (convert_bind_target_to_gl_target
|
|
|
|
(buffer->last_target)) );
|
2010-10-12 06:46:29 -04:00
|
|
|
buffer->flags &= ~COGL_BUFFER_FLAG_MAPPED;
|
2010-07-05 20:21:43 -04:00
|
|
|
|
|
|
|
_cogl_buffer_unbind (buffer);
|
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
bo_set_data (CoglBuffer *buffer,
|
|
|
|
unsigned int offset,
|
2010-11-04 12:01:23 -04:00
|
|
|
const void *data,
|
2010-07-05 20:21:43 -04:00
|
|
|
unsigned int size)
|
|
|
|
{
|
|
|
|
CoglBufferBindTarget target;
|
|
|
|
GLenum gl_target;
|
2012-02-06 12:08:58 -05:00
|
|
|
CoglContext *ctx = buffer->context;
|
2010-07-05 20:21:43 -04:00
|
|
|
|
|
|
|
target = buffer->last_target;
|
|
|
|
_cogl_buffer_bind (buffer, target);
|
|
|
|
|
|
|
|
gl_target = convert_bind_target_to_gl_target (target);
|
|
|
|
|
2011-07-06 16:51:00 -04:00
|
|
|
GE( ctx, glBufferSubData (gl_target, offset, size, data) );
|
2010-07-05 20:21:43 -04:00
|
|
|
|
|
|
|
_cogl_buffer_unbind (buffer);
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Fallback path, buffer->data points to a malloc'ed buffer.
|
|
|
|
*/
|
|
|
|
|
2010-11-04 12:01:23 -04:00
|
|
|
static void *
|
2010-07-05 20:21:43 -04:00
|
|
|
malloc_map (CoglBuffer *buffer,
|
|
|
|
CoglBufferAccess access,
|
|
|
|
CoglBufferMapHint hints)
|
|
|
|
{
|
2010-10-12 06:46:29 -04:00
|
|
|
buffer->flags |= COGL_BUFFER_FLAG_MAPPED;
|
2010-07-05 20:21:43 -04:00
|
|
|
return buffer->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
malloc_unmap (CoglBuffer *buffer)
|
|
|
|
{
|
2010-10-12 06:46:29 -04:00
|
|
|
buffer->flags &= ~COGL_BUFFER_FLAG_MAPPED;
|
2010-07-05 20:21:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
malloc_set_data (CoglBuffer *buffer,
|
|
|
|
unsigned int offset,
|
2010-11-04 12:01:23 -04:00
|
|
|
const void *data,
|
2010-07-05 20:21:43 -04:00
|
|
|
unsigned int size)
|
|
|
|
{
|
|
|
|
memcpy (buffer->data + offset, data, size);
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
2010-01-10 12:28:24 -05:00
|
|
|
void
|
|
|
|
_cogl_buffer_initialize (CoglBuffer *buffer,
|
2012-02-06 12:08:58 -05:00
|
|
|
CoglContext *context,
|
cogl: improves header and coding style consistency
We've had complaints that our Cogl code/headers are a bit "special" so
this is a first pass at tidying things up by giving them some
consistency. These changes are all consistent with how new code in Cogl
is being written, but the style isn't consistently applied across all
code yet.
There are two parts to this patch; but since each one required a large
amount of effort to maintain tidy indenting it made sense to combine the
changes to reduce the time spent re indenting the same lines.
The first change is to use a consistent style for declaring function
prototypes in headers. Cogl headers now consistently use this style for
prototypes:
return_type
cogl_function_name (CoglType arg0,
CoglType arg1);
Not everyone likes this style, but it seems that most of the currently
active Cogl developers agree on it.
The second change is to constrain the use of redundant glib data types
in Cogl. Uses of gint, guint, gfloat, glong, gulong and gchar have all
been replaced with int, unsigned int, float, long, unsigned long and char
respectively. When talking about pixel data; use of guchar has been
replaced with guint8, otherwise unsigned char can be used.
The glib types that we continue to use for portability are gboolean,
gint{8,16,32,64}, guint{8,16,32,64} and gsize.
The general intention is that Cogl should look palatable to the widest
range of C programmers including those outside the Gnome community so
- especially for the public API - we want to minimize the number of
foreign looking typedefs.
2010-02-09 20:57:32 -05:00
|
|
|
unsigned int size,
|
2010-07-05 20:21:43 -04:00
|
|
|
gboolean use_malloc,
|
2010-07-05 18:24:34 -04:00
|
|
|
CoglBufferBindTarget default_target,
|
2010-01-10 12:28:24 -05:00
|
|
|
CoglBufferUsageHint usage_hint,
|
|
|
|
CoglBufferUpdateHint update_hint)
|
|
|
|
{
|
2012-02-06 12:08:58 -05:00
|
|
|
buffer->context = cogl_object_ref (context);
|
2010-10-26 11:06:46 -04:00
|
|
|
buffer->flags = COGL_BUFFER_FLAG_NONE;
|
|
|
|
buffer->store_created = FALSE;
|
|
|
|
buffer->size = size;
|
|
|
|
buffer->last_target = default_target;
|
|
|
|
buffer->usage_hint = usage_hint;
|
|
|
|
buffer->update_hint = update_hint;
|
|
|
|
buffer->data = NULL;
|
2010-10-26 14:08:51 -04:00
|
|
|
buffer->immutable_ref = 0;
|
2010-01-10 12:28:24 -05:00
|
|
|
|
2010-07-05 20:21:43 -04:00
|
|
|
if (use_malloc)
|
|
|
|
{
|
|
|
|
buffer->vtable.map = malloc_map;
|
|
|
|
buffer->vtable.unmap = malloc_unmap;
|
|
|
|
buffer->vtable.set_data = malloc_set_data;
|
2010-01-10 12:28:24 -05:00
|
|
|
|
2010-07-05 20:21:43 -04:00
|
|
|
buffer->data = g_malloc (size);
|
|
|
|
}
|
|
|
|
else
|
2010-07-05 18:24:34 -04:00
|
|
|
{
|
2010-07-05 20:21:43 -04:00
|
|
|
buffer->vtable.map = bo_map;
|
|
|
|
buffer->vtable.unmap = bo_unmap;
|
|
|
|
buffer->vtable.set_data = bo_set_data;
|
|
|
|
|
2012-02-06 12:08:58 -05:00
|
|
|
GE( context, glGenBuffers (1, &buffer->gl_handle) );
|
2010-10-12 06:46:29 -04:00
|
|
|
buffer->flags |= COGL_BUFFER_FLAG_BUFFER_OBJECT;
|
2010-07-05 18:24:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-05 20:21:43 -04:00
|
|
|
void
|
|
|
|
_cogl_buffer_fini (CoglBuffer *buffer)
|
2010-07-05 18:24:34 -04:00
|
|
|
{
|
2011-10-13 17:34:30 -04:00
|
|
|
_COGL_RETURN_IF_FAIL (!(buffer->flags & COGL_BUFFER_FLAG_MAPPED));
|
|
|
|
_COGL_RETURN_IF_FAIL (buffer->immutable_ref == 0);
|
2010-11-14 22:59:24 -05:00
|
|
|
|
|
|
|
if (buffer->flags & COGL_BUFFER_FLAG_BUFFER_OBJECT)
|
2012-02-06 12:08:58 -05:00
|
|
|
GE( buffer->context, glDeleteBuffers (1, &buffer->gl_handle) );
|
2010-11-14 22:59:24 -05:00
|
|
|
else
|
|
|
|
g_free (buffer->data);
|
2012-02-06 12:08:58 -05:00
|
|
|
|
|
|
|
cogl_object_unref (buffer->context);
|
2010-07-05 18:24:34 -04:00
|
|
|
}
|
|
|
|
|
2010-01-10 12:28:24 -05:00
|
|
|
GLenum
|
|
|
|
_cogl_buffer_access_to_gl_enum (CoglBufferAccess access)
|
|
|
|
{
|
|
|
|
if ((access & COGL_BUFFER_ACCESS_READ_WRITE) == COGL_BUFFER_ACCESS_READ_WRITE)
|
|
|
|
return GL_READ_WRITE;
|
|
|
|
else if (access & COGL_BUFFER_ACCESS_WRITE)
|
|
|
|
return GL_WRITE_ONLY;
|
|
|
|
else
|
|
|
|
return GL_READ_ONLY;
|
|
|
|
}
|
|
|
|
|
2010-10-12 08:14:17 -04:00
|
|
|
void *
|
2010-07-05 18:24:34 -04:00
|
|
|
_cogl_buffer_bind (CoglBuffer *buffer, CoglBufferBindTarget target)
|
2010-01-10 12:28:24 -05:00
|
|
|
{
|
2012-02-25 14:14:25 -05:00
|
|
|
void *ret;
|
2010-01-10 12:28:24 -05:00
|
|
|
|
2012-02-25 14:14:25 -05:00
|
|
|
ret = _cogl_buffer_bind_no_create (buffer, target);
|
2010-07-05 18:24:34 -04:00
|
|
|
|
2012-02-25 14:14:25 -05:00
|
|
|
/* create an empty store if we don't have one yet. creating the store
|
|
|
|
* lazily allows the user of the CoglBuffer to set a hint before the
|
|
|
|
* store is created. */
|
|
|
|
if ((buffer->flags & COGL_BUFFER_FLAG_BUFFER_OBJECT) &&
|
|
|
|
!buffer->store_created)
|
|
|
|
bo_recreate_store (buffer);
|
2010-07-05 18:24:34 -04:00
|
|
|
|
2012-02-25 14:14:25 -05:00
|
|
|
return ret;
|
2010-07-05 18:24:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_cogl_buffer_unbind (CoglBuffer *buffer)
|
|
|
|
{
|
2012-02-06 12:08:58 -05:00
|
|
|
CoglContext *ctx = buffer->context;
|
2010-07-05 18:24:34 -04:00
|
|
|
|
2011-10-13 17:34:30 -04:00
|
|
|
_COGL_RETURN_IF_FAIL (buffer != NULL);
|
2010-07-05 18:24:34 -04:00
|
|
|
|
|
|
|
/* the unbind should pair up with a previous bind */
|
2011-10-13 17:34:30 -04:00
|
|
|
_COGL_RETURN_IF_FAIL (ctx->current_buffer[buffer->last_target] == buffer);
|
2010-07-05 18:24:34 -04:00
|
|
|
|
2010-10-12 06:46:29 -04:00
|
|
|
if (buffer->flags & COGL_BUFFER_FLAG_BUFFER_OBJECT)
|
2010-01-10 12:28:24 -05:00
|
|
|
{
|
2010-07-05 20:21:43 -04:00
|
|
|
GLenum gl_target = convert_bind_target_to_gl_target (buffer->last_target);
|
2011-07-06 16:51:00 -04:00
|
|
|
GE( ctx, glBindBuffer (gl_target, 0) );
|
2010-01-10 12:28:24 -05:00
|
|
|
}
|
|
|
|
|
2010-07-05 18:24:34 -04:00
|
|
|
ctx->current_buffer[buffer->last_target] = NULL;
|
2010-01-10 12:28:24 -05:00
|
|
|
}
|
|
|
|
|
cogl: improves header and coding style consistency
We've had complaints that our Cogl code/headers are a bit "special" so
this is a first pass at tidying things up by giving them some
consistency. These changes are all consistent with how new code in Cogl
is being written, but the style isn't consistently applied across all
code yet.
There are two parts to this patch; but since each one required a large
amount of effort to maintain tidy indenting it made sense to combine the
changes to reduce the time spent re indenting the same lines.
The first change is to use a consistent style for declaring function
prototypes in headers. Cogl headers now consistently use this style for
prototypes:
return_type
cogl_function_name (CoglType arg0,
CoglType arg1);
Not everyone likes this style, but it seems that most of the currently
active Cogl developers agree on it.
The second change is to constrain the use of redundant glib data types
in Cogl. Uses of gint, guint, gfloat, glong, gulong and gchar have all
been replaced with int, unsigned int, float, long, unsigned long and char
respectively. When talking about pixel data; use of guchar has been
replaced with guint8, otherwise unsigned char can be used.
The glib types that we continue to use for portability are gboolean,
gint{8,16,32,64}, guint{8,16,32,64} and gsize.
The general intention is that Cogl should look palatable to the widest
range of C programmers including those outside the Gnome community so
- especially for the public API - we want to minimize the number of
foreign looking typedefs.
2010-02-09 20:57:32 -05:00
|
|
|
unsigned int
|
2010-06-30 13:06:04 -04:00
|
|
|
cogl_buffer_get_size (CoglBuffer *buffer)
|
2010-01-10 12:28:24 -05:00
|
|
|
{
|
2010-05-27 18:40:40 -04:00
|
|
|
if (!cogl_is_buffer (buffer))
|
2010-01-10 12:28:24 -05:00
|
|
|
return 0;
|
|
|
|
|
2010-05-27 18:40:40 -04:00
|
|
|
return COGL_BUFFER (buffer)->size;
|
2010-01-10 12:28:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-06-30 13:06:04 -04:00
|
|
|
cogl_buffer_set_update_hint (CoglBuffer *buffer,
|
|
|
|
CoglBufferUpdateHint hint)
|
2010-01-10 12:28:24 -05:00
|
|
|
{
|
2010-05-27 18:40:40 -04:00
|
|
|
if (!cogl_is_buffer (buffer))
|
2010-01-10 12:28:24 -05:00
|
|
|
return;
|
|
|
|
|
|
|
|
if (G_UNLIKELY (hint > COGL_BUFFER_UPDATE_HINT_STREAM))
|
|
|
|
hint = COGL_BUFFER_UPDATE_HINT_STATIC;
|
|
|
|
|
2010-05-27 18:40:40 -04:00
|
|
|
buffer->update_hint = hint;
|
2010-01-10 12:28:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
CoglBufferUpdateHint
|
2010-06-30 13:06:04 -04:00
|
|
|
cogl_buffer_get_update_hint (CoglBuffer *buffer)
|
2010-01-10 12:28:24 -05:00
|
|
|
{
|
2010-05-27 18:40:40 -04:00
|
|
|
if (!cogl_is_buffer (buffer))
|
2010-01-10 12:28:24 -05:00
|
|
|
return FALSE;
|
|
|
|
|
2010-05-27 18:40:40 -04:00
|
|
|
return buffer->update_hint;
|
2010-01-10 12:28:24 -05:00
|
|
|
}
|
|
|
|
|
2010-10-26 14:08:51 -04:00
|
|
|
static void
|
|
|
|
warn_about_midscene_changes (void)
|
|
|
|
{
|
|
|
|
static gboolean seen = FALSE;
|
|
|
|
if (!seen)
|
|
|
|
{
|
|
|
|
g_warning ("Mid-scene modification of buffers has "
|
|
|
|
"undefined results\n");
|
|
|
|
seen = TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-04 12:01:23 -04:00
|
|
|
void *
|
2010-07-05 11:14:00 -04:00
|
|
|
cogl_buffer_map (CoglBuffer *buffer,
|
|
|
|
CoglBufferAccess access,
|
|
|
|
CoglBufferMapHint hints)
|
2010-01-10 12:28:24 -05:00
|
|
|
{
|
2011-10-13 17:34:30 -04:00
|
|
|
_COGL_RETURN_VAL_IF_FAIL (cogl_is_buffer (buffer), NULL);
|
2010-10-26 14:08:51 -04:00
|
|
|
|
|
|
|
if (G_UNLIKELY (buffer->immutable_ref))
|
|
|
|
warn_about_midscene_changes ();
|
2010-01-10 12:28:24 -05:00
|
|
|
|
2010-10-12 06:46:29 -04:00
|
|
|
if (buffer->flags & COGL_BUFFER_FLAG_MAPPED)
|
2010-01-10 12:28:24 -05:00
|
|
|
return buffer->data;
|
|
|
|
|
2010-07-05 20:21:43 -04:00
|
|
|
buffer->data = buffer->vtable.map (buffer, access, hints);
|
2010-01-10 12:28:24 -05:00
|
|
|
return buffer->data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2010-06-30 13:06:04 -04:00
|
|
|
cogl_buffer_unmap (CoglBuffer *buffer)
|
2010-01-10 12:28:24 -05:00
|
|
|
{
|
2010-05-27 18:40:40 -04:00
|
|
|
if (!cogl_is_buffer (buffer))
|
2010-01-10 12:28:24 -05:00
|
|
|
return;
|
|
|
|
|
2010-10-12 06:46:29 -04:00
|
|
|
if (!(buffer->flags & COGL_BUFFER_FLAG_MAPPED))
|
2010-01-10 12:28:24 -05:00
|
|
|
return;
|
|
|
|
|
2010-07-05 20:21:43 -04:00
|
|
|
buffer->vtable.unmap (buffer);
|
2010-01-10 12:28:24 -05:00
|
|
|
}
|
|
|
|
|
2011-01-13 10:35:30 -05:00
|
|
|
void *
|
|
|
|
_cogl_buffer_map_for_fill_or_fallback (CoglBuffer *buffer)
|
|
|
|
{
|
2012-02-06 12:08:58 -05:00
|
|
|
CoglContext *ctx = buffer->context;
|
2011-01-13 10:35:30 -05:00
|
|
|
void *ret;
|
|
|
|
|
2011-10-13 17:34:30 -04:00
|
|
|
_COGL_RETURN_VAL_IF_FAIL (!ctx->buffer_map_fallback_in_use, NULL);
|
2011-01-13 10:35:30 -05:00
|
|
|
|
|
|
|
ctx->buffer_map_fallback_in_use = TRUE;
|
|
|
|
|
|
|
|
ret = cogl_buffer_map (buffer,
|
|
|
|
COGL_BUFFER_ACCESS_WRITE,
|
|
|
|
COGL_BUFFER_MAP_HINT_DISCARD);
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* If the map fails then we'll use a temporary buffer to fill
|
|
|
|
the data and then upload it using cogl_buffer_set_data when
|
|
|
|
the buffer is unmapped. The temporary buffer is shared to
|
|
|
|
avoid reallocating it every time */
|
|
|
|
g_byte_array_set_size (ctx->buffer_map_fallback_array, buffer->size);
|
|
|
|
|
|
|
|
buffer->flags |= COGL_BUFFER_FLAG_MAPPED_FALLBACK;
|
|
|
|
|
|
|
|
return ctx->buffer_map_fallback_array->data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_cogl_buffer_unmap_for_fill_or_fallback (CoglBuffer *buffer)
|
|
|
|
{
|
2012-02-06 12:08:58 -05:00
|
|
|
CoglContext *ctx = buffer->context;
|
2011-01-13 10:35:30 -05:00
|
|
|
|
2011-10-13 17:34:30 -04:00
|
|
|
_COGL_RETURN_IF_FAIL (ctx->buffer_map_fallback_in_use);
|
2011-01-13 10:35:30 -05:00
|
|
|
|
|
|
|
ctx->buffer_map_fallback_in_use = FALSE;
|
|
|
|
|
|
|
|
if ((buffer->flags & COGL_BUFFER_FLAG_MAPPED_FALLBACK))
|
|
|
|
{
|
|
|
|
cogl_buffer_set_data (buffer, 0,
|
|
|
|
ctx->buffer_map_fallback_array->data,
|
|
|
|
buffer->size);
|
|
|
|
buffer->flags &= ~COGL_BUFFER_FLAG_MAPPED_FALLBACK;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
cogl_buffer_unmap (buffer);
|
|
|
|
}
|
|
|
|
|
2010-01-10 12:28:24 -05:00
|
|
|
gboolean
|
2010-06-30 13:06:04 -04:00
|
|
|
cogl_buffer_set_data (CoglBuffer *buffer,
|
|
|
|
gsize offset,
|
2010-11-04 12:01:23 -04:00
|
|
|
const void *data,
|
2010-06-30 13:06:04 -04:00
|
|
|
gsize size)
|
2010-01-10 12:28:24 -05:00
|
|
|
{
|
2011-10-13 17:34:30 -04:00
|
|
|
_COGL_RETURN_VAL_IF_FAIL (cogl_is_buffer (buffer), FALSE);
|
|
|
|
_COGL_RETURN_VAL_IF_FAIL ((offset + size) <= buffer->size, FALSE);
|
2010-01-10 12:28:24 -05:00
|
|
|
|
2010-10-26 14:08:51 -04:00
|
|
|
if (G_UNLIKELY (buffer->immutable_ref))
|
|
|
|
warn_about_midscene_changes ();
|
|
|
|
|
2010-07-05 20:21:43 -04:00
|
|
|
return buffer->vtable.set_data (buffer, offset, data, size);
|
2010-01-10 12:28:24 -05:00
|
|
|
}
|
2010-10-26 14:08:51 -04:00
|
|
|
|
|
|
|
CoglBuffer *
|
|
|
|
_cogl_buffer_immutable_ref (CoglBuffer *buffer)
|
|
|
|
{
|
2011-10-13 17:34:30 -04:00
|
|
|
_COGL_RETURN_VAL_IF_FAIL (cogl_is_buffer (buffer), NULL);
|
2010-10-26 14:08:51 -04:00
|
|
|
|
|
|
|
buffer->immutable_ref++;
|
|
|
|
return buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
_cogl_buffer_immutable_unref (CoglBuffer *buffer)
|
|
|
|
{
|
2011-10-13 17:34:30 -04:00
|
|
|
_COGL_RETURN_IF_FAIL (cogl_is_buffer (buffer));
|
|
|
|
_COGL_RETURN_IF_FAIL (buffer->immutable_ref > 0);
|
2010-10-26 14:08:51 -04:00
|
|
|
|
|
|
|
buffer->immutable_ref--;
|
|
|
|
}
|
|
|
|
|