update SDL3 to latest commit

This commit is contained in:
Sasha Szpakowski
2024-10-06 20:30:44 -03:00
parent 1b0fdc338b
commit 9e5eb1b018
1426 changed files with 242456 additions and 103496 deletions
+174 -89
View File
@@ -11,9 +11,9 @@
*/
#define SDL_MAIN_USE_CALLBACKS 1
#include <SDL3/SDL_main.h>
#include <SDL3/SDL_test.h>
#include <SDL3/SDL_test_common.h>
#include <SDL3/SDL_main.h>
static SDL_Window *window = NULL;
static SDL_Renderer *renderer = NULL;
@@ -21,27 +21,66 @@ static SDLTest_CommonState *state = NULL;
static SDL_Camera *camera = NULL;
static SDL_CameraSpec spec;
static SDL_Texture *texture = NULL;
static SDL_bool texture_updated = SDL_FALSE;
static bool texture_updated = false;
static SDL_Surface *frame_current = NULL;
static SDL_CameraDeviceID front_camera = 0;
static SDL_CameraDeviceID back_camera = 0;
static SDL_CameraID front_camera = 0;
static SDL_CameraID back_camera = 0;
int SDL_AppInit(void **appstate, int argc, char *argv[])
/* For frequency logging */
static Uint64 last_log_time = 0;
static int iterate_count = 0;
static int frame_count = 0;
static void PrintCameraSpecs(SDL_CameraID camera_id)
{
SDL_CameraSpec **specs = SDL_GetCameraSupportedFormats(camera_id, NULL);
if (specs) {
int i;
SDL_Log("Available formats:\n");
for (i = 0; specs[i]; ++i) {
const SDL_CameraSpec *s = specs[i];
SDL_Log(" %dx%d %.2f FPS %s\n", s->width, s->height, (float)s->framerate_numerator / s->framerate_denominator, SDL_GetPixelFormatName(s->format));
}
SDL_free(specs);
}
}
SDL_AppResult SDL_AppInit(void **appstate, int argc, char *argv[])
{
char window_title[128];
int devcount = 0;
int i;
const char *camera_name = NULL;
/* Initialize test framework */
state = SDLTest_CommonCreateState(argv, SDL_INIT_VIDEO | SDL_INIT_CAMERA);
if (!state) {
return -1;
return SDL_APP_FAILURE;
}
/* Enable standard application logging */
SDL_LogSetPriority(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO);
/* Parse commandline */
for (i = 1; i < argc;) {
int consumed;
if (!SDLTest_CommonDefaultArgs(state, argc, argv)) {
return -1;
consumed = SDLTest_CommonArg(state, i);
if (!consumed) {
if (SDL_strcmp(argv[i], "--camera") == 0 && argv[i+1]) {
camera_name = argv[i+1];
consumed = 2;
}
}
if (consumed <= 0) {
static const char *options[] = {
"[--camera name]",
NULL,
};
SDLTest_CommonLogUsage(state, argv[0], options);
SDL_Quit();
SDLTest_CommonDestroyState(state);
return 1;
}
i += consumed;
}
state->num_windows = 1;
@@ -49,34 +88,34 @@ int SDL_AppInit(void **appstate, int argc, char *argv[])
/* Load the SDL library */
if (!SDLTest_CommonInit(state)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s", SDL_GetError());
return -1;
return SDL_APP_FAILURE;
}
window = state->windows[0];
if (!window) {
SDL_Log("Couldn't create window: %s", SDL_GetError());
return -1;
return SDL_APP_FAILURE;
}
SDL_LogSetAllPriority(SDL_LOG_PRIORITY_VERBOSE);
renderer = state->renderers[0];
if (!renderer) {
/* SDL_Log("Couldn't create renderer: %s", SDL_GetError()); */
return -1;
return SDL_APP_FAILURE;
}
SDL_CameraDeviceID *devices = SDL_GetCameraDevices(&devcount);
SDL_CameraID *devices = SDL_GetCameras(&devcount);
if (!devices) {
SDL_Log("SDL_GetCameraDevices failed: %s", SDL_GetError());
return -1;
SDL_Log("SDL_GetCameras failed: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
SDL_CameraID camera_id = 0;
SDL_Log("Saw %d camera devices.", devcount);
for (i = 0; i < devcount; i++) {
const SDL_CameraDeviceID device = devices[i];
char *name = SDL_GetCameraDeviceName(device);
const SDL_CameraPosition position = SDL_GetCameraDevicePosition(device);
const SDL_CameraID device = devices[i];
const char *name = SDL_GetCameraName(device);
const SDL_CameraPosition position = SDL_GetCameraPosition(device);
const char *posstr = "";
if (position == SDL_CAMERA_POSITION_FRONT_FACING) {
front_camera = device;
@@ -85,33 +124,46 @@ int SDL_AppInit(void **appstate, int argc, char *argv[])
back_camera = device;
posstr = "[back-facing] ";
}
if (camera_name && SDL_strcasecmp(name, camera_name) == 0) {
camera_id = device;
}
SDL_Log(" - Camera #%d: %s %s", i, posstr, name);
SDL_free(name);
}
const SDL_CameraDeviceID devid = front_camera ? front_camera : devices[0]; /* no front-facing? just take the first one. */
if (!camera_id) {
if (camera_name) {
SDL_Log("Could not find camera \"%s\"", camera_name);
return SDL_APP_FAILURE;
}
if (front_camera) {
camera_id = front_camera;
} else if (devcount > 0) {
camera_id = devices[0];
}
}
SDL_free(devices);
if (!devid) {
if (!camera_id) {
SDL_Log("No cameras available?");
return -1;
return SDL_APP_FAILURE;
}
SDL_CameraSpec *pspec = NULL;
#if 0 /* just for edge-case testing purposes, ignore. */
pspec = &spec;
spec.width = 100 /*1280 * 2*/;
spec.height = 100 /*720 * 2*/;
spec.format = SDL_PIXELFORMAT_YUY2 /*SDL_PIXELFORMAT_RGBA8888*/;
#endif
PrintCameraSpecs(camera_id);
camera = SDL_OpenCameraDevice(devid, pspec);
SDL_CameraSpec *pspec = &spec;
spec.framerate_numerator = 30;
spec.framerate_denominator = 1;
camera = SDL_OpenCamera(camera_id, pspec);
if (!camera) {
SDL_Log("Failed to open camera device: %s", SDL_GetError());
return -1;
return SDL_APP_FAILURE;
}
return 0; /* start the main app loop. */
SDL_snprintf(window_title, sizeof (window_title), "testcamera: %s (%s)", SDL_GetCameraName(camera_id), SDL_GetCurrentCameraDriver());
SDL_SetWindowTitle(window, window_title);
return SDL_APP_CONTINUE;
}
@@ -119,12 +171,12 @@ static int FlipCamera(void)
{
static Uint64 last_flip = 0;
if ((SDL_GetTicks() - last_flip) < 3000) { /* must wait at least 3 seconds between flips. */
return 0;
return SDL_APP_CONTINUE;
}
if (camera) {
const SDL_CameraDeviceID current = SDL_GetCameraInstanceID(camera);
SDL_CameraDeviceID nextcam = 0;
const SDL_CameraID current = SDL_GetCameraID(camera);
SDL_CameraID nextcam = 0;
if (current == front_camera) {
nextcam = back_camera;
} else if (current == back_camera) {
@@ -146,30 +198,30 @@ static int FlipCamera(void)
texture = NULL; /* will rebuild when new camera is approved. */
}
camera = SDL_OpenCameraDevice(nextcam, NULL);
camera = SDL_OpenCamera(nextcam, NULL);
if (!camera) {
SDL_Log("Failed to open camera device: %s", SDL_GetError());
return -1;
return SDL_APP_FAILURE;
}
last_flip = SDL_GetTicks();
}
}
return 0;
return SDL_APP_CONTINUE;
}
int SDL_AppEvent(void *appstate, const SDL_Event *event)
SDL_AppResult SDL_AppEvent(void *appstate, SDL_Event *event)
{
switch (event->type) {
case SDL_EVENT_KEY_DOWN: {
const SDL_Keycode sym = event->key.keysym.sym;
const SDL_Keycode sym = event->key.key;
if (sym == SDLK_ESCAPE || sym == SDLK_AC_BACK) {
SDL_Log("Key : Escape!");
return 1;
return SDL_APP_SUCCESS;
} else if (sym == SDLK_SPACE) {
FlipCamera();
return 0;
return SDL_APP_CONTINUE;
}
break;
}
@@ -179,29 +231,25 @@ int SDL_AppEvent(void *appstate, const SDL_Event *event)
return FlipCamera();
case SDL_EVENT_QUIT:
SDL_Log("Ctlr+C : Quit!");
SDL_Log("Quit!");
return 1;
case SDL_EVENT_CAMERA_DEVICE_APPROVED:
SDL_Log("Camera approved!");
if (SDL_GetCameraFormat(camera, &spec) < 0) {
SDL_Log("Couldn't get camera spec: %s", SDL_GetError());
return -1;
}
/* Create texture with appropriate format */
SDL_assert(texture == NULL);
texture = SDL_CreateTexture(renderer, spec.format, SDL_TEXTUREACCESS_STATIC, spec.width, spec.height);
if (!texture) {
SDL_Log("Couldn't create texture: %s", SDL_GetError());
return -1;
SDL_CameraSpec camera_spec;
SDL_GetCameraFormat(camera, &camera_spec);
float fps = 0;
if (camera_spec.framerate_denominator != 0) {
fps = (float)camera_spec.framerate_numerator / (float)camera_spec.framerate_denominator;
}
SDL_Log("Camera Spec: %dx%d %.2f FPS %s",
camera_spec.width, camera_spec.height, fps, SDL_GetPixelFormatName(camera_spec.format));
break;
case SDL_EVENT_CAMERA_DEVICE_DENIED:
SDL_Log("Camera denied!");
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Camera permission denied!", "User denied access to the camera!", window);
return -1;
return SDL_APP_FAILURE;
default:
break;
}
@@ -209,49 +257,89 @@ int SDL_AppEvent(void *appstate, const SDL_Event *event)
return SDLTest_CommonEventMainCallbacks(state, event);
}
int SDL_AppIterate(void *appstate)
SDL_AppResult SDL_AppIterate(void *appstate)
{
iterate_count++;
Uint64 current_time = SDL_GetTicks();
/* If a minute has passed, log the frequencies and reset the counters */
if (current_time - last_log_time >= 60000) {
SDL_Log("SDL_AppIterate() called %d times in the last minute", iterate_count);
float fps = (float)frame_count / 60.0f;
SDL_Log("SDL_AcquireCameraFrame() FPS: %.2f", fps);
iterate_count = 0;
frame_count = 0;
last_log_time = current_time;
}
SDL_SetRenderDrawColor(renderer, 0x99, 0x99, 0x99, 255);
SDL_RenderClear(renderer);
if (texture) { /* if not NULL, camera is ready to go. */
int win_w, win_h, tw, th;
SDL_FRect d;
Uint64 timestampNS = 0;
SDL_Surface *frame_next = camera ? SDL_AcquireCameraFrame(camera, &timestampNS) : NULL;
int win_w, win_h;
SDL_FRect d;
Uint64 timestampNS = 0;
SDL_Surface *frame_next = camera ? SDL_AcquireCameraFrame(camera, &timestampNS) : NULL;
#if 0
if (frame_next) {
SDL_Log("frame: %p at %" SDL_PRIu64, (void*)frame_next->pixels, timestampNS);
#if 0
if (frame_next) {
SDL_Log("frame: %p at %" SDL_PRIu64, frame_next->pixels, timestampNS);
}
#endif
if (frame_next) {
frame_count++;
if (frame_current) {
SDL_ReleaseCameraFrame(camera, frame_current);
}
#endif
if (frame_next) {
if (frame_current) {
if (SDL_ReleaseCameraFrame(camera, frame_current) < 0) {
SDL_Log("err SDL_ReleaseCameraFrame: %s", SDL_GetError());
}
/* It's not needed to keep the frame once updated the texture is updated.
* But in case of 0-copy, it's needed to have the frame while using the texture.
*/
frame_current = frame_next;
texture_updated = false;
}
if (frame_current) {
if (!texture ||
texture->w != frame_current->w || texture->h != frame_current->h) {
/* Resize the window to match */
SDL_SetWindowSize(window, frame_current->w, frame_current->h);
if (texture) {
SDL_DestroyTexture(texture);
}
/* It's not needed to keep the frame once updated the texture is updated.
* But in case of 0-copy, it's needed to have the frame while using the texture.
*/
frame_current = frame_next;
texture_updated = SDL_FALSE;
SDL_Colorspace colorspace = SDL_GetSurfaceColorspace(frame_current);
/* Create texture with appropriate format */
SDL_PropertiesID props = SDL_CreateProperties();
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_FORMAT_NUMBER, frame_current->format);
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_COLORSPACE_NUMBER, colorspace);
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_ACCESS_NUMBER, SDL_TEXTUREACCESS_STREAMING);
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_WIDTH_NUMBER, frame_current->w);
SDL_SetNumberProperty(props, SDL_PROP_TEXTURE_CREATE_HEIGHT_NUMBER, frame_current->h);
texture = SDL_CreateTextureWithProperties(renderer, props);
SDL_DestroyProperties(props);
if (!texture) {
SDL_Log("Couldn't create texture: %s", SDL_GetError());
return SDL_APP_FAILURE;
}
}
/* Update SDL_Texture with last video frame (only once per new frame) */
if (frame_current && !texture_updated) {
SDL_UpdateTexture(texture, NULL, frame_current->pixels, frame_current->pitch);
texture_updated = SDL_TRUE;
texture_updated = true;
}
SDL_QueryTexture(texture, NULL, NULL, &tw, &th);
SDL_GetRenderOutputSize(renderer, &win_w, &win_h);
d.x = (float) ((win_w - tw) / 2);
d.y = (float) ((win_h - th) / 2);
d.w = (float) tw;
d.h = (float) th;
d.x = ((win_w - texture->w) / 2.0f);
d.y = ((win_h - texture->h) / 2.0f);
d.w = (float)texture->w;
d.h = (float)texture->h;
SDL_RenderTexture(renderer, texture, NULL, &d);
}
@@ -259,16 +347,13 @@ int SDL_AppIterate(void *appstate)
SDL_RenderPresent(renderer);
return 0; /* keep iterating. */
return SDL_APP_CONTINUE;
}
void SDL_AppQuit(void *appstate)
void SDL_AppQuit(void *appstate, SDL_AppResult result)
{
SDL_ReleaseCameraFrame(camera, frame_current);
SDL_CloseCamera(camera);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDLTest_CommonQuit(state);
}