libdragon
surface.h
Go to the documentation of this file.
1
52#ifndef __LIBDRAGON_SURFACE_H
53#define __LIBDRAGON_SURFACE_H
54
55#include <stdint.h>
56#include <stddef.h>
57#include <stdbool.h>
58
59#ifdef __cplusplus
60extern "C" {
61#endif
62
64// Macro to create a texture format, combining the RDP native "fmt/size" tuple.
65// This macro is used to genearte the #tex_format_t enums creating identifiers
66// which are easy to convert back into RDP native fields.
67#define _RDP_FORMAT_CODE(rdp_fmt, rdp_size) (((rdp_fmt)<<2)|(rdp_size))
69
74#define TEX_FORMAT_BITDEPTH(fmt) (4 << ((fmt) & 0x3))
82#define TEX_FORMAT_PIX2BYTES(fmt, pixels) ((((pixels) << (((fmt) & 3) + 2)) + 7) >> 3)
89#define TEX_FORMAT_BYTES2PIX(fmt, bytes) (((bytes) << 1) >> ((fmt) & 3))
90
101typedef enum {
103
104 FMT_RGBA16 = _RDP_FORMAT_CODE(0, 2),
105 FMT_RGBA32 = _RDP_FORMAT_CODE(0, 3),
106 FMT_YUV16 = _RDP_FORMAT_CODE(1, 2),
107 FMT_CI4 = _RDP_FORMAT_CODE(2, 0),
108 FMT_CI8 = _RDP_FORMAT_CODE(2, 1),
109 FMT_IA4 = _RDP_FORMAT_CODE(3, 0),
110 FMT_IA8 = _RDP_FORMAT_CODE(3, 1),
111 FMT_IA16 = _RDP_FORMAT_CODE(3, 2),
112 FMT_I4 = _RDP_FORMAT_CODE(4, 0),
113 FMT_I8 = _RDP_FORMAT_CODE(4, 1),
115
117const char* tex_format_name(tex_format_t fmt);
118
119#define SURFACE_FLAGS_TEXFORMAT 0x1F
120#define SURFACE_FLAGS_OWNEDBUFFER 0x20
121
134typedef struct surface_s
135{
136 uint16_t flags;
137 uint16_t width;
138 uint16_t height;
139 uint16_t stride;
140 void *buffer;
141} surface_t;
142
165inline surface_t surface_make(void *buffer, tex_format_t format, uint32_t width, uint32_t height, uint32_t stride) {
166 return (surface_t){
167 .flags = format,
168 .width = width,
169 .height = height,
170 .stride = stride,
171 .buffer = buffer,
172 };
173}
174
192inline surface_t surface_make_linear(void *buffer, tex_format_t format, uint32_t width, uint32_t height) {
193 return surface_make(buffer, format, width, height, TEX_FORMAT_PIX2BYTES(format, width));
194}
195
213surface_t surface_alloc(tex_format_t format, uint32_t width, uint32_t height);
214
230 uint32_t x0, uint32_t y0, uint32_t width, uint32_t height);
231
244void surface_free(surface_t *surface);
245
253{
254 return (tex_format_t)(surface->flags & SURFACE_FLAGS_TEXFORMAT);
255}
256
263inline bool surface_has_owned_buffer(const surface_t *surface)
264{
265 return surface->buffer != NULL && surface->flags & SURFACE_FLAGS_OWNEDBUFFER;
266}
267#ifdef __cplusplus
268}
269#endif
270
271#endif
tex_format_t
Pixel format enum.
Definition: surface.h:101
@ FMT_RGBA32
Format RGBA 8888 (32-bit)
Definition: surface.h:105
@ FMT_CI8
Format CI8: color index 8-bit (paletted, 1 index per byte)
Definition: surface.h:108
@ FMT_YUV16
Format YUV2 4:2:2 (data interleaved as YUYV)
Definition: surface.h:106
@ FMT_IA4
Format IA4: 3-bit intensity + 1-bit alpha (4-bit per pixel)
Definition: surface.h:109
@ FMT_I8
Format I8: 8-bit intensity (8-bit per pixel)
Definition: surface.h:113
@ FMT_IA16
Format IA16: 8-bit intensity + 8-bit alpha (16-bit per pixel)
Definition: surface.h:111
@ FMT_NONE
Placeholder for no format defined.
Definition: surface.h:102
@ FMT_RGBA16
Format RGBA 5551 (16-bit)
Definition: surface.h:104
@ FMT_CI4
Format CI4: color index 4-bit (paletted, 2 indices per byte)
Definition: surface.h:107
@ FMT_I4
Format I4: 4-bit intensity (4-bit per pixel)
Definition: surface.h:112
@ FMT_IA8
Format IA8: 4-bit intensity + 4-bit alpha (8-bit per pixel)
Definition: surface.h:110
bool surface_has_owned_buffer(const surface_t *surface)
Checks whether this surface owns the buffer that it contains.
Definition: surface.h:263
uint16_t stride
Stride in bytes (length of a row)
Definition: surface.h:139
#define SURFACE_FLAGS_OWNEDBUFFER
Set if the buffer must be freed.
Definition: surface.h:120
void * buffer
Buffer pointer.
Definition: surface.h:140
uint16_t width
Width in pixels.
Definition: surface.h:137
surface_t surface_make_sub(surface_t *parent, uint32_t x0, uint32_t y0, uint32_t width, uint32_t height)
Initialize a surface_t structure, pointing to a rectangular portion of another surface.
Definition: surface.c:56
surface_t surface_make(void *buffer, tex_format_t format, uint32_t width, uint32_t height, uint32_t stride)
Initialize a surface_t structure with the provided buffer.
Definition: surface.h:165
surface_t surface_make_linear(void *buffer, tex_format_t format, uint32_t width, uint32_t height)
Initialize a surface_t structure with the provided linear buffer.
Definition: surface.h:192
#define TEX_FORMAT_PIX2BYTES(fmt, pixels)
Convert the specified number of pixels to bytes.
Definition: surface.h:82
uint16_t height
Height in pixels.
Definition: surface.h:138
void surface_free(surface_t *surface)
Free the buffer allocated in a surface.
Definition: surface.c:48
surface_t surface_alloc(tex_format_t format, uint32_t width, uint32_t height)
Allocate a new surface in memory.
Definition: surface.c:31
const char * tex_format_name(tex_format_t fmt)
Return the name of the texture format as a string (for debugging purposes)
Definition: surface.c:13
tex_format_t surface_get_format(const surface_t *surface)
Returns the pixel format of a surface.
Definition: surface.h:252
#define SURFACE_FLAGS_TEXFORMAT
Pixel format of the surface.
Definition: surface.h:119
uint16_t flags
Flags (including pixel format)
Definition: surface.h:136
A surface buffer for graphics.
Definition: surface.h:135