libdragon
Loading...
Searching...
No Matches
surface.h
Go to the documentation of this file.
1
54#ifndef __LIBDRAGON_SURFACE_H
55#define __LIBDRAGON_SURFACE_H
56
57#include <stdint.h>
58#include <stddef.h>
59#include <stdbool.h>
60
61#ifdef __cplusplus
62extern "C" {
63#endif
64
66// Macro to create a texture format, combining the RDP native "fmt/size" tuple.
67// This macro is used to genearte the #tex_format_t enums creating identifiers
68// which are easy to convert back into RDP native fields.
69#define _RDP_FORMAT_CODE(rdp_fmt, rdp_size) (((rdp_fmt)<<2)|(rdp_size))
71
76#define TEX_FORMAT_BITDEPTH(fmt) (4 << ((fmt) & 0x3))
84#define TEX_FORMAT_PIX2BYTES(fmt, pixels) ((((pixels) << (((fmt) & 3) + 2)) + 7) >> 3)
91#define TEX_FORMAT_BYTES2PIX(fmt, bytes) (((bytes) << 1) >> ((fmt) & 3))
92
105typedef enum {
107
108 FMT_RGBA16 = _RDP_FORMAT_CODE(0, 2),
109 FMT_RGBA32 = _RDP_FORMAT_CODE(0, 3),
110 FMT_YUV16 = _RDP_FORMAT_CODE(1, 2),
111 FMT_CI4 = _RDP_FORMAT_CODE(2, 0),
112 FMT_CI8 = _RDP_FORMAT_CODE(2, 1),
113 FMT_IA4 = _RDP_FORMAT_CODE(3, 0),
114 FMT_IA8 = _RDP_FORMAT_CODE(3, 1),
115 FMT_IA16 = _RDP_FORMAT_CODE(3, 2),
116 FMT_I4 = _RDP_FORMAT_CODE(4, 0),
117 FMT_I8 = _RDP_FORMAT_CODE(4, 1),
119
121const char* tex_format_name(tex_format_t fmt);
122
123#define SURFACE_FLAGS_TEXFORMAT 0x001F
124#define SURFACE_FLAGS_OWNEDBUFFER 0x0020
125#define SURFACE_FLAGS_TEXINDEX 0x0F00
126
139typedef struct surface_s
140{
141 uint16_t flags;
142 uint16_t width;
143 uint16_t height;
144 uint16_t stride;
145 void *buffer;
146} surface_t;
147
170inline surface_t surface_make(void *buffer, tex_format_t format, uint16_t width, uint16_t height, uint16_t stride) {
171 return (surface_t){
172 .flags = format,
173 .width = width,
174 .height = height,
175 .stride = stride,
176 .buffer = buffer,
177 };
178}
179
197inline surface_t surface_make_linear(void *buffer, tex_format_t format, uint16_t width, uint16_t height) {
198 return surface_make(buffer, format, width, height, TEX_FORMAT_PIX2BYTES(format, width));
199}
200
218surface_t surface_alloc(tex_format_t format, uint16_t width, uint16_t height);
219
235 uint16_t x0, uint16_t y0, uint16_t width, uint16_t height);
236
249void surface_free(surface_t *surface);
250
258{
259 return (tex_format_t)(surface->flags & SURFACE_FLAGS_TEXFORMAT);
260}
261
268inline bool surface_has_owned_buffer(const surface_t *surface)
269{
270 return surface->buffer != NULL && surface->flags & SURFACE_FLAGS_OWNEDBUFFER;
271}
272
295inline surface_t surface_make_placeholder(int index, tex_format_t format, uint16_t width, uint16_t height, uint16_t stride) {
296 return (surface_t){
297 .flags = (uint16_t)(format | ((index & 0xF) << 8)),
298 .width = width,
299 .height = height,
300 .stride = stride,
301 .buffer = NULL,
302 };
303}
304
320inline surface_t surface_make_placeholder_linear(int index, tex_format_t format, uint16_t width, uint16_t height) {
321 return surface_make_placeholder(index, format, width, height, TEX_FORMAT_PIX2BYTES(format, width));
322}
323
334inline int surface_get_placeholder_index(const surface_t *surface)
335{
336 return (surface->flags >> 8) & 0xF;
337}
338
339
340#ifdef __cplusplus
341}
342#endif
343
344#endif
tex_format_t
Pixel format enum.
Definition surface.h:105
@ FMT_RGBA32
Format RGBA 8888 (32-bit)
Definition surface.h:109
@ FMT_CI8
Format CI8: color index 8-bit (paletted, 1 index per byte)
Definition surface.h:112
@ FMT_YUV16
Format YUV2 4:2:2 (data interleaved as YUYV)
Definition surface.h:110
@ FMT_IA4
Format IA4: 3-bit intensity + 1-bit alpha (4-bit per pixel)
Definition surface.h:113
@ FMT_I8
Format I8: 8-bit intensity (8-bit per pixel)
Definition surface.h:117
@ FMT_IA16
Format IA16: 8-bit intensity + 8-bit alpha (16-bit per pixel)
Definition surface.h:115
@ FMT_NONE
Placeholder for no format defined.
Definition surface.h:106
@ FMT_RGBA16
Format RGBA 5551 (16-bit)
Definition surface.h:108
@ FMT_CI4
Format CI4: color index 4-bit (paletted, 2 indices per byte)
Definition surface.h:111
@ FMT_I4
Format I4: 4-bit intensity (4-bit per pixel)
Definition surface.h:116
@ FMT_IA8
Format IA8: 4-bit intensity + 4-bit alpha (8-bit per pixel)
Definition surface.h:114
bool surface_has_owned_buffer(const surface_t *surface)
Checks whether this surface owns the buffer that it contains.
Definition surface.h:268
uint16_t stride
Stride in bytes (length of a row)
Definition surface.h:144
#define SURFACE_FLAGS_OWNEDBUFFER
Set if the buffer must be freed.
Definition surface.h:124
surface_t surface_make_sub(surface_t *parent, uint16_t x0, uint16_t y0, uint16_t width, uint16_t height)
Initialize a surface_t structure, pointing to a rectangular portion of another surface.
Definition surface.c:58
void * buffer
Buffer pointer.
Definition surface.h:145
surface_t surface_make(void *buffer, tex_format_t format, uint16_t width, uint16_t height, uint16_t stride)
Initialize a surface_t structure with the provided buffer.
Definition surface.h:170
uint16_t width
Width in pixels.
Definition surface.h:142
int surface_get_placeholder_index(const surface_t *surface)
Returns the lookup index of a placeholder surface.
Definition surface.h:334
surface_t surface_alloc(tex_format_t format, uint16_t width, uint16_t height)
Allocate a new surface in memory.
Definition surface.c:33
surface_t surface_make_placeholder_linear(int index, tex_format_t format, uint16_t width, uint16_t height)
Create a linear placeholder surface, that can be used during rdpq block recording.
Definition surface.h:320
#define TEX_FORMAT_PIX2BYTES(fmt, pixels)
Convert the specified number of pixels to bytes.
Definition surface.h:84
uint16_t height
Height in pixels.
Definition surface.h:143
surface_t surface_make_linear(void *buffer, tex_format_t format, uint16_t width, uint16_t height)
Initialize a surface_t structure with the provided linear buffer.
Definition surface.h:197
void surface_free(surface_t *surface)
Free the buffer allocated in a surface.
Definition surface.c:50
surface_t surface_make_placeholder(int index, tex_format_t format, uint16_t width, uint16_t height, uint16_t stride)
Create a placeholder surface, that can be used during rdpq block recording.
Definition surface.h:295
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:15
tex_format_t surface_get_format(const surface_t *surface)
Returns the pixel format of a surface.
Definition surface.h:257
#define SURFACE_FLAGS_TEXFORMAT
Pixel format of the surface.
Definition surface.h:123
uint16_t flags
Flags (including pixel format)
Definition surface.h:141
A surface buffer for graphics.
Definition surface.h:140