#include <stdio.h>
#include <sys/mman.h>
#define WIDTH 640
#define HEIGHT 480
#define BPP 3
#include "sdl.h"
struct data {
SDL_Renderer *renderer;
SDL_Window *window;
SDL_Texture *texture;
int32_t stride;
int n_buffers;
};
static void handle_events(struct data *data)
{
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_QUIT:
break;
}
}
}
static int impl_set_io(void *object, uint32_t id, void *data, size_t size)
{
return 0;
}
static int impl_send_command(
void *
object,
const struct spa_command *command)
{
return 0;
}
static int impl_add_listener(void *object,
void *data)
{
struct data *d = object;
return 0;
}
static int impl_set_callbacks(void *object,
{
return 0;
}
static int impl_port_set_io(
void *
object,
enum spa_direction direction, uint32_t port_id,
uint32_t id, void *data, size_t size)
{
struct data *d = object;
d->io = data;
else
return -ENOENT;
return 0;
}
static int impl_port_enum_params(void *object, int seq,
uint32_t id, uint32_t start, uint32_t num,
{
struct data *d = object;
uint8_t buffer[1024];
uint32_t count = 0;
result.next = start;
result.index = result.next++;
switch (id) {
{
SDL_RendererInfo info;
if (result.index > 0)
return 0;
SDL_GetRendererInfo(d->renderer, &info);
param = sdl_build_formats(&info, &b);
break;
}
if (result.index > 0)
return 0;
break;
if (result.index > 0)
return 0;
break;
default:
return -ENOENT;
}
if (++count != num)
return 0;
}
static int port_set_format(
void *
object,
enum spa_direction direction, uint32_t port_id,
uint32_t flags,
const struct spa_pod *format)
{
struct data *d = object;
Uint32 sdl_format;
void *dest;
if (format == NULL)
return 0;
sdl_format = id_to_sdl_format(d->format.format);
if (sdl_format == SDL_PIXELFORMAT_UNKNOWN)
return -EINVAL;
d->texture = SDL_CreateTexture(d->renderer,
sdl_format,
SDL_TEXTUREACCESS_STREAMING,
d->format.size.width,
d->format.size.height);
SDL_LockTexture(d->texture, NULL, &dest, &d->stride);
SDL_UnlockTexture(d->texture);
return 0;
}
static int impl_port_set_param(void *object,
uint32_t id, uint32_t flags,
{
return port_set_format(object, direction, port_id, flags, param);
}
else
return -ENOENT;
}
static int impl_port_use_buffers(void *object,
uint32_t flags,
struct spa_buffer **buffers, uint32_t n_buffers)
{
struct data *d = object;
uint32_t i;
for (i = 0; i < n_buffers; i++)
d->buffers[i] = buffers[i];
d->n_buffers = n_buffers;
return 0;
}
static int do_render(
struct spa_loop *loop,
bool async, uint32_t seq,
const void *_data, size_t size, void *user_data)
{
struct data *d = user_data;
uint8_t *map;
void *sdata, *ddata;
int sstride, dstride, ostride;
uint32_t i;
uint8_t *src, *dst;
buf = d->buffers[d->io->buffer_id];
map = NULL;
} else
return -EINVAL;
if (SDL_LockTexture(d->texture, NULL, &ddata, &dstride) < 0) {
fprintf(stderr, "Couldn't lock texture: %s\n", SDL_GetError());
return -EIO;
}
ostride =
SPA_MIN(sstride, dstride);
src = sdata;
dst = ddata;
for (i = 0; i < d->format.size.height; i++) {
memcpy(dst, src, ostride);
src += sstride;
dst += dstride;
}
SDL_UnlockTexture(d->texture);
SDL_RenderClear(d->renderer);
SDL_RenderCopy(d->renderer, d->texture, NULL, NULL);
SDL_RenderPresent(d->renderer);
if (map)
return 0;
}
static int impl_node_process(void *object)
{
struct data *d = object;
int res;
return res;
handle_events(d);
}
.add_listener = impl_add_listener,
.set_callbacks = impl_set_callbacks,
.set_io = impl_set_io,
.send_command = impl_send_command,
.port_set_io = impl_port_set_io,
.port_enum_params = impl_port_enum_params,
.port_set_param = impl_port_set_param,
.port_use_buffers = impl_port_use_buffers,
.process = impl_node_process,
};
static int make_nodes(struct data *data)
{
&impl_node, data);
data->info.change_mask =
data->info.flags = 0;
data->info.params = data->params;
NULL,
&data->impl_node,
0);
NULL);
"spa-node-factory",
while (true) {
break;
}
"link-factory",
return 0;
}
int main(int argc, char *argv[])
{
struct data data = { 0, };
NULL), 0);
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("can't initialize SDL: %s\n", SDL_GetError());
return -1;
}
if (SDL_CreateWindowAndRenderer
(WIDTH, HEIGHT, SDL_WINDOW_RESIZABLE, &data.window, &data.renderer)) {
printf("can't create window: %s\n", SDL_GetError());
return -1;
}
if (data.core == NULL) {
printf("can't connect to core: %m\n");
return -1;
}
make_nodes(&data);
return 0;
}