only wait for vsync if under frame budget

This commit is contained in:
Shaun Inman 2023-01-13 20:12:47 -05:00
parent 1143e46d20
commit d8c96b82fa
4 changed files with 20 additions and 11 deletions

View file

@ -192,18 +192,22 @@ void GFX_clearAll(void) {
memset(gfx.map, 0, gfx.map_size);
}
static int frame_start = 0;
void GFX_startFrame(void) {
frame_start = SDL_GetTicks();
}
void GFX_flip(SDL_Surface* screen) {
static int ticks = 0;
ticks += 1;
#ifdef GFX_ENABLE_VSYNC
int arg = 1;
ioctl(gfx.fb, OWLFB_WAITFORVSYNC, &arg); // TODO: this doesn't wait but it also doesn't error out like FBIO_WAITFORVSYNC...
#define FRAME_BUDGET 17 // 60fps
if (frame_start==0 || SDL_GetTicks()-frame_start<FRAME_BUDGET) { // only wait if we're under frame budget
int arg = 1;
ioctl(gfx.fb, OWLFB_WAITFORVSYNC, &arg);
}
#endif
#ifdef GFX_ENABLE_BUFFER
// TODO: this would be moved to a thread
// I'm not clear on why that would be necessary
// if it's non-blocking and the pan will wait
// until the next vblank...
// what if the scaling was also moved to a thread?
gfx.vinfo.yoffset = gfx.buffer * SCREEN_HEIGHT;
ioctl(gfx.fb, FBIOPAN_DISPLAY, &gfx.vinfo);

View file

@ -28,6 +28,7 @@ void LOG_note(int level, const char* fmt, ...);
SDL_Surface* GFX_init(void);
void GFX_clear(SDL_Surface* screen);
void GFX_clearAll(void);
void GFX_startFrame(void);
void GFX_flip(SDL_Surface* screen);
void GFX_quit(void);

View file

@ -926,6 +926,7 @@ int main(int argc , char* argv[]) {
sec_start = SDL_GetTicks();
while (1) {
GFX_startFrame();
if (PAD_justReleased(BTN_POWER)) break; // TODO: tmp
core.run();
cpu_ticks += 1;

View file

@ -1262,6 +1262,7 @@ int main (int argc, char *argv[]) {
unsigned long cancel_start = SDL_GetTicks();
unsigned long power_start = 0;
while (!quit) {
GFX_startFrame();
unsigned long frame_start = SDL_GetTicks();
PAD_poll();
@ -1556,10 +1557,12 @@ int main (int argc, char *argv[]) {
GFX_flip(screen);
dirty = 0;
}
// // slow down to 60fps
// unsigned long frame_duration = SDL_GetTicks() - frame_start;
// #define TARGET_FRAME_DURATION 17
// if (frame_duration<TARGET_FRAME_DURATION) SDL_Delay(TARGET_FRAME_DURATION-frame_duration);
else {
// slow down to 60fps
unsigned long frame_duration = SDL_GetTicks() - frame_start;
#define TARGET_FRAME_DURATION 17
if (frame_duration<TARGET_FRAME_DURATION) SDL_Delay(TARGET_FRAME_DURATION-frame_duration);
}
}
if (version) SDL_FreeSurface(version);