Skip to content

Commit

Permalink
WIP: www: Calculate refresh rate
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander Shishkin <[email protected]>
  • Loading branch information
virtuoso committed Sep 22, 2024
1 parent b5b6a53 commit 36d895b
Showing 1 changed file with 48 additions and 7 deletions.
55 changes: 48 additions & 7 deletions core/display-www.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,55 @@ bool gl_does_vao(void)
return false;
}

static int refresh_rate = 0;

int gl_refresh_rate(void)
{
/*
* XXX: Ugh, I know.
* But this is not much worse than dynamic calculation.
*/
return 60;
return refresh_rate;
}

struct calc_refresh_rate_priv {
display_update update_fn;
void *update_data;
};

#define AVG_FRAMES 20
EMSCRIPTEN_KEEPALIVE void __calc_refresh_rate(void *data)
{
static struct timespec ts_start, ts_end, ts_delta;
struct calc_refresh_rate_priv *priv = data;
static long total = 0;
static int frame = 0;

if (!frame) {
clock_gettime(CLOCK_MONOTONIC, &ts_start);
frame++;
return;
} else if (frame < AVG_FRAMES) {
clock_gettime(CLOCK_MONOTONIC, &ts_end);
timespec_diff(&ts_start, &ts_end, &ts_delta);
ts_start = ts_end;
total += ts_delta.tv_nsec;
frame++;
return;
}

total /= AVG_FRAMES - 1;
refresh_rate = 1000000000L / total;
dbg("### refresh rate: %d\n", refresh_rate);
emscripten_cancel_main_loop();
emscripten_set_main_loop_arg(priv->update_fn, priv->update_data, 0, false);
}

static void calc_refresh_rate(display_update update_fn, void *data)
{
static struct calc_refresh_rate_priv priv;

priv.update_fn = update_fn;
priv.update_data = data;

emscripten_set_main_loop_timing(EM_TIMING_RAF, 1);
emscripten_set_main_loop_arg(__calc_refresh_rate, &priv, 0, false);
}

void gl_request_exit(void)
Expand Down Expand Up @@ -108,7 +150,6 @@ void gl_init(const char *title, int width, int height, display_update update_fn,
msg("GL context: %d Extensions: '%s'\n", context, exts);
EM_ASM(runtime_ready = true;);
gl_get_sizes(NULL, NULL);
emscripten_set_main_loop_arg(update_fn, data, 0, 0);
emscripten_set_main_loop_timing(EM_TIMING_RAF, 1);
calc_refresh_rate(update_fn, data);
//resize_fn(width, height);
}

0 comments on commit 36d895b

Please sign in to comment.