Skip to content

Commit

Permalink
Refactored the long lines to fit into the guidelines
Browse files Browse the repository at this point in the history
  • Loading branch information
zsolt-donca committed Mar 1, 2021
1 parent 321b5cd commit 711cf6b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 16 deletions.
4 changes: 2 additions & 2 deletions include/fps_limit.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@
struct fps_limit_state {
struct timespec last_time;

struct timespec fps_measurement_last_time;
uint64_t fps_measurement_frame_count;
struct timespec fps_last_time;
uint64_t fps_frame_count;
};

void fps_limit_measure_start(struct fps_limit_state* state, double max_fps);
Expand Down
29 changes: 16 additions & 13 deletions src/screencast/fps_limit.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,31 +34,34 @@ uint64_t fps_limit_measure_end(struct fps_limit_state* state, double max_fps) {
int64_t target_ns = (1.0 / max_fps) * TIMESPEC_NS_IN_S;
int64_t delay_ns = target_ns - elapsed_ns;
if (delay_ns > 0) {
logprint(TRACE, "fps_limit: elapsed time since the end of last measurement: %u, target %u, should delay for %u (nanoseconds)", elapsed_ns, target_ns, delay_ns);
logprint(TRACE, "fps_limit: elapsed time since the last measurement: %u, "
"target %u, should delay for %u (ns)", elapsed_ns, target_ns, delay_ns);
return delay_ns;
} else {
logprint(TRACE, "fps_limit: elapsed time since the end of last measurement: %u, target %u, target not met, should not delay. ", elapsed_ns, target_ns);
logprint(TRACE, "fps_limit: elapsed time since the last measurement: %u, "
"target %u, target not met (ns)", elapsed_ns, target_ns);
return 0;
}
}

void measure_fps(struct fps_limit_state* state, struct timespec *now) {
if (timespec_is_zero(&state->fps_measurement_last_time)) {
state->fps_measurement_last_time = *now;
if (timespec_is_zero(&state->fps_last_time)) {
state->fps_last_time = *now;
} else {
state->fps_measurement_frame_count++;
state->fps_frame_count++;

int64_t elapsed_since_last_measure_ns = timespec_diff_ns(now, &state->fps_measurement_last_time);
int64_t elapsed_ns = timespec_diff_ns(now, &state->fps_last_time);

double elapsed_since_last_measure_sec = (double) elapsed_since_last_measure_ns / (double) TIMESPEC_NS_IN_S;
if (elapsed_since_last_measure_sec >= FPS_MEASURE_PERIOD_SEC) {
double average_delay_sec = elapsed_since_last_measure_sec / state->fps_measurement_frame_count;
double average_frames_per_sec = 1.0 / average_delay_sec;
double elapsed_sec = (double) elapsed_ns / (double) TIMESPEC_NS_IN_S;
if (elapsed_sec >= FPS_MEASURE_PERIOD_SEC) {
double avg_delay_sec = elapsed_sec / state->fps_frame_count;
double avg_frames_per_sec = 1.0 / avg_delay_sec;

logprint(DEBUG, "fps_limit: average FPS in the last %0.0f seconds: %0.2f", FPS_MEASURE_PERIOD_SEC, average_frames_per_sec);
logprint(DEBUG, "fps_limit: average FPS in the last %0.0f seconds: %0.2f",
FPS_MEASURE_PERIOD_SEC, avg_frames_per_sec);

state->fps_measurement_last_time = *now;
state->fps_measurement_frame_count = 0;
state->fps_last_time = *now;
state->fps_frame_count = 0;
}
}
}
3 changes: 2 additions & 1 deletion src/screencast/wlr_screencast.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,8 @@ void xdpw_wlr_frame_free(struct xdpw_screencast_instance *cast) {

uint64_t delay_ns = fps_limit_measure_end(&cast->fps_limit, cast->ctx->max_fps);
if (delay_ns > 0) {
xdpw_add_timer(cast->ctx->state, delay_ns, (xdpw_event_loop_timer_func_t) xdpw_wlr_register_cb, cast);
xdpw_add_timer(cast->ctx->state, delay_ns,
(xdpw_event_loop_timer_func_t) xdpw_wlr_register_cb, cast);
} else {
xdpw_wlr_register_cb(cast);
}
Expand Down

0 comments on commit 711cf6b

Please sign in to comment.