Buffers

## Buffers Buffers — Common buffer functions, including data upload APIs |

## Stability Level

Unstable, unless otherwise indicated

## Functions

CoglBool | cogl_is_buffer ()
unsigned int | cogl_buffer_get_size ()
void | cogl_buffer_set_update_hint ()
CoglBufferUpdateHint | cogl_buffer_get_update_hint ()
void * | cogl_buffer_map ()
void | cogl_buffer_unmap ()
CoglBool | cogl_buffer_set_data ()
CoglPixelBuffer * | cogl_pixel_buffer_new ()
CoglBool | cogl_is_pixel_buffer ()

## Types and Values

enum | CoglBufferUpdateHint
enum | CoglBufferAccess

## Description

The CoglBuffer API provides a common interface to manipulate buffers that have been allocated either via `cogl_pixel_buffer_new()` or `cogl_attribute_buffer_new()`. The API allows you to upload data to these buffers and define usage hints that help Cogl manage your buffer optimally.

Data can either be uploaded by supplying a pointer and size so Cogl can copy your data, or you can `mmap()` a CoglBuffer and then you can copy data to the buffer directly.

One of the most common uses for CoglBuffers is to upload texture data asynchronously since the ability to mmap the buffers into the CPU makes it possible for another thread to handle the IO of loading an image file and unpacking it into the mapped buffer without blocking other Cogl operations.

## Functions

### cogl_is_buffer ()

    CoglBool
    cogl_is_buffer (void *object);

Checks whether `buffer` is a buffer object.

#### Parameters

object | a buffer object |

#### Returns

`TRUE` if the handle is a CoglBuffer, and `FALSE` otherwise

Since 1.2

Stability Level: Unstable

### cogl_buffer_get_size ()

    unsigned int
    cogl_buffer_get_size (CoglBuffer *buffer);

Retrieves the size of buffer

#### Parameters

buffer | a buffer object |

#### Returns

the size of the buffer in bytes

Since 1.2

Stability Level: Unstable

### cogl_buffer_set_update_hint ()

    void
    cogl_buffer_set_update_hint (CoglBuffer *buffer,
                                 CoglBufferUpdateHint hint);

Sets the update hint on a buffer. See CoglBufferUpdateHint for a description of the available hints.

#### Parameters

buffer | a buffer object |
hint | the new hint |

Since 1.2

Stability Level: Unstable

### cogl_buffer_get_update_hint ()

    CoglBufferUpdateHint
    cogl_buffer_get_update_hint (CoglBuffer *buffer);

Retrieves the update hints set using `cogl_buffer_set_update_hint()`

#### Parameters

buffer | a buffer object |

#### Returns

the CoglBufferUpdateHint currently used by the buffer

Since 1.2

Stability Level: Unstable

### cogl_buffer_map ()

    void *
    cogl_buffer_map (CoglBuffer *buffer,
                     CoglBufferAccess access,
                     CoglBufferMapHint hints);

Maps the buffer into the application address space for direct access. This is equivalent to calling `cogl_buffer_map_range()` with zero as the offset and the size of the entire buffer as the size.

It is strongly recommended that you pass `COGL_BUFFER_MAP_HINT_DISCARD` as a hint if you are going to replace all the buffer's data. This way if the buffer is currently being used by the GPU then the driver won't have to stall the CPU and wait for the hardware to finish because it can instead allocate a new buffer to map.

The behaviour is undefined if you access the buffer in a way conflicting with the `access` mask you pass. It is also an error to release your last reference while the buffer is mapped.

#### Parameters

buffer | a buffer object |
access | how the mapped buffer will be used by the application |
hints | A mask of CoglBufferMapHints that tell Cogl how the data will be modified once mapped. |

#### Returns

A pointer to the mapped memory or `NULL` is the call fails.

[transfer none]

Since 1.2

Stability Level: Unstable

### cogl_buffer_unmap ()

    void
    cogl_buffer_unmap (CoglBuffer *buffer);

Unmaps a buffer previously mapped by `cogl_buffer_map()`.

#### Parameters

buffer | a buffer object |

Since 1.2

Stability Level: Unstable

### cogl_buffer_set_data ()

    CoglBool
    cogl_buffer_set_data (CoglBuffer *buffer,
                          size_t offset,
                          const void *data,
                          size_t size);

Updates part of the buffer with new data from `data` . Where to put this new data is controlled by `offset` and `offset` + `data` should be less than the buffer size.

#### Parameters

buffer | a buffer object |
offset | destination offset (in bytes) in the buffer |
data | a pointer to the data to be copied into the buffer |
size | number of bytes to copy |

#### Returns

`TRUE` is the operation succeeded, `FALSE` otherwise

Since 1.2

Stability Level: Unstable

### cogl_pixel_buffer_new ()

    CoglPixelBuffer *
    cogl_pixel_buffer_new (CoglContext *context,
                           size_t size,
                           const void *data);

Declares a new CoglPixelBuffer of `size` bytes to contain arrays of pixels. Once declared, data can be set using `cogl_buffer_set_data()` or by mapping it into the application's address space using `cogl_buffer_map()`.

If `data` isn't `NULL` then `size` bytes will be read from `data` and immediately copied into the new buffer.

#### Parameters

context | A CoglContext |
size | The number of bytes to allocate for the pixel data. |
data | An optional pointer to vertex data to upload immediately |

#### Returns

a newly allocated CoglPixelBuffer.

[transfer full]

Since 1.10

Stability Level: Unstable

### cogl_is_pixel_buffer ()

    CoglBool
    cogl_is_pixel_buffer (void *object);

Checks whether `object` is a pixel buffer.

#### Parameters

object | a CoglObject to test |

#### Returns

`TRUE` if the `object` is a pixel buffer, and `FALSE` otherwise

Since 1.2

Stability Level: Unstable

## Types and Values

### enum CoglBufferUpdateHint

The update hint on a buffer allows the user to give some detail on how often the buffer data is going to be updated.

#### Members

COGL_BUFFER_UPDATE_HINT_STATIC | the buffer will not change over time |
COGL_BUFFER_UPDATE_HINT_DYNAMIC | the buffer will change from time to time |
COGL_BUFFER_UPDATE_HINT_STREAM | the buffer will be used once or a couple of times |

Since 1.2

Stability Level: Unstable

### enum CoglBufferAccess

The access hints for `cogl_buffer_set_update_hint()`

#### Members

COGL_BUFFER_ACCESS_READ | the buffer will be read |
COGL_BUFFER_ACCESS_WRITE | the buffer will written to |
COGL_BUFFER_ACCESS_READ_WRITE | the buffer will be used for both reading and writing |

Since 1.2

Stability Level: Unstable