Datasets:
Download src/sdl_adapter.c from PureOne/ChronoSheaf-Input-Lag: direct link, hf CLI and curl.
- Browser
- Download file 2.15 kB
-
https://huggingface.co/datasets/PureOne/ChronoSheaf-Input-Lag/resolve/main/src/sdl_adapter.c
- Command line
-
hf download hf://datasets/PureOne/ChronoSheaf-Input-Lag/src/sdl_adapter.c
-
curl -L -o sdl_adapter.c https://huggingface.co/datasets/PureOne/ChronoSheaf-Input-Lag/resolve/main/src/sdl_adapter.c
2.15 kB
| /* SDL2's presentation functions have a single main-thread contract. */ | |
| typedef void (*present_fn)(void *); | |
| static present_fn render_next, gl_next; | |
| static pthread_once_t loaders = PTHREAD_ONCE_INIT; | |
| static _Thread_local struct { | |
| void *chain; | |
| int kind, depth; | |
| int64_t waited; | |
| xr_gate gate; | |
| } thread_state; | |
| static void load_backends(void) { | |
| *(void **)(&render_next) = dlsym(RTLD_NEXT, "SDL_RenderPresent"); | |
| *(void **)(&gl_next) = dlsym(RTLD_NEXT, "SDL_GL_SwapWindow"); | |
| } | |
| static int64_t now_us(void) { | |
| struct timespec ts; | |
| if (clock_gettime(CLOCK_MONOTONIC, &ts)) return 0; | |
| return (int64_t)ts.tv_sec*1000000 + ts.tv_nsec/1000; | |
| } | |
| static void do_present(present_fn fn, void *object, int kind) { | |
| if (!fn) { | |
| static const char line[] = "Xireun requires the SDL2 shared library in this process.\n"; | |
| (void)write(2,line,sizeof(line)-1); abort(); | |
| } | |
| if (thread_state.depth) { fn(object); return; } | |
| thread_state.depth++; | |
| if (object != thread_state.chain || kind != thread_state.kind) { | |
| xr_reset(&thread_state.gate); | |
| thread_state.chain = object; thread_state.kind = kind; thread_state.waited = 0; | |
| } | |
| int64_t begin = now_us(); | |
| fn(object); | |
| int64_t end = now_us(); | |
| int64_t delay = xr_after(&thread_state.gate, begin, end, thread_state.waited); | |
| thread_state.waited = 0; | |
| if (delay > 0) { | |
| int64_t start = now_us(), due = start+delay; | |
| struct timespec t = { (time_t)(due/1000000), (long)(due%1000000)*1000 }; | |
| int rc; | |
| do { rc=clock_nanosleep(CLOCK_MONOTONIC,TIMER_ABSTIME,&t,NULL); } while (rc==EINTR); | |
| thread_state.waited=now_us()-start; | |
| if (rc) xr_cool(&thread_state.gate); | |
| } | |
| thread_state.depth--; | |
| } | |
| void SDL_RenderPresent(void *renderer) { | |
| pthread_once(&loaders,load_backends); | |
| do_present(render_next,renderer,1); | |
| } | |
| void SDL_GL_SwapWindow(void *window) { | |
| pthread_once(&loaders,load_backends); | |
| do_present(gl_next,window,2); | |
| } | |