Use sc_prefix for fps counter

This commit is contained in:
Romain Vimont 2022-02-17 19:55:14 +01:00
parent 85edba20e7
commit 03705b828b
5 changed files with 42 additions and 42 deletions

View file

@ -4,10 +4,10 @@
#include "util/log.h"
#define FPS_COUNTER_INTERVAL SC_TICK_FROM_SEC(1)
#define SC_FPS_COUNTER_INTERVAL SC_TICK_FROM_SEC(1)
bool
fps_counter_init(struct fps_counter *counter) {
sc_fps_counter_init(struct sc_fps_counter *counter) {
bool ok = sc_mutex_init(&counter->mutex);
if (!ok) {
return false;
@ -27,26 +27,26 @@ fps_counter_init(struct fps_counter *counter) {
}
void
fps_counter_destroy(struct fps_counter *counter) {
sc_fps_counter_destroy(struct sc_fps_counter *counter) {
sc_cond_destroy(&counter->state_cond);
sc_mutex_destroy(&counter->mutex);
}
static inline bool
is_started(struct fps_counter *counter) {
is_started(struct sc_fps_counter *counter) {
return atomic_load_explicit(&counter->started, memory_order_acquire);
}
static inline void
set_started(struct fps_counter *counter, bool started) {
set_started(struct sc_fps_counter *counter, bool started) {
atomic_store_explicit(&counter->started, started, memory_order_release);
}
// must be called with mutex locked
static void
display_fps(struct fps_counter *counter) {
display_fps(struct sc_fps_counter *counter) {
unsigned rendered_per_second =
counter->nr_rendered * SC_TICK_FREQ / FPS_COUNTER_INTERVAL;
counter->nr_rendered * SC_TICK_FREQ / SC_FPS_COUNTER_INTERVAL;
if (counter->nr_skipped) {
LOGI("%u fps (+%u frames skipped)", rendered_per_second,
counter->nr_skipped);
@ -57,7 +57,7 @@ display_fps(struct fps_counter *counter) {
// must be called with mutex locked
static void
check_interval_expired(struct fps_counter *counter, sc_tick now) {
check_interval_expired(struct sc_fps_counter *counter, sc_tick now) {
if (now < counter->next_timestamp) {
return;
}
@ -67,13 +67,13 @@ check_interval_expired(struct fps_counter *counter, sc_tick now) {
counter->nr_skipped = 0;
// add a multiple of the interval
uint32_t elapsed_slices =
(now - counter->next_timestamp) / FPS_COUNTER_INTERVAL + 1;
counter->next_timestamp += FPS_COUNTER_INTERVAL * elapsed_slices;
(now - counter->next_timestamp) / SC_FPS_COUNTER_INTERVAL + 1;
counter->next_timestamp += SC_FPS_COUNTER_INTERVAL * elapsed_slices;
}
static int
run_fps_counter(void *data) {
struct fps_counter *counter = data;
struct sc_fps_counter *counter = data;
sc_mutex_lock(&counter->mutex);
while (!counter->interrupted) {
@ -94,9 +94,9 @@ run_fps_counter(void *data) {
}
bool
fps_counter_start(struct fps_counter *counter) {
sc_fps_counter_start(struct sc_fps_counter *counter) {
sc_mutex_lock(&counter->mutex);
counter->next_timestamp = sc_tick_now() + FPS_COUNTER_INTERVAL;
counter->next_timestamp = sc_tick_now() + SC_FPS_COUNTER_INTERVAL;
counter->nr_rendered = 0;
counter->nr_skipped = 0;
sc_mutex_unlock(&counter->mutex);
@ -121,18 +121,18 @@ fps_counter_start(struct fps_counter *counter) {
}
void
fps_counter_stop(struct fps_counter *counter) {
sc_fps_counter_stop(struct sc_fps_counter *counter) {
set_started(counter, false);
sc_cond_signal(&counter->state_cond);
}
bool
fps_counter_is_started(struct fps_counter *counter) {
sc_fps_counter_is_started(struct sc_fps_counter *counter) {
return is_started(counter);
}
void
fps_counter_interrupt(struct fps_counter *counter) {
sc_fps_counter_interrupt(struct sc_fps_counter *counter) {
if (!counter->thread_started) {
return;
}
@ -145,7 +145,7 @@ fps_counter_interrupt(struct fps_counter *counter) {
}
void
fps_counter_join(struct fps_counter *counter) {
sc_fps_counter_join(struct sc_fps_counter *counter) {
if (counter->thread_started) {
// interrupted must be set by the thread calling join(), so no need to
// lock for the assertion
@ -156,7 +156,7 @@ fps_counter_join(struct fps_counter *counter) {
}
void
fps_counter_add_rendered_frame(struct fps_counter *counter) {
sc_fps_counter_add_rendered_frame(struct sc_fps_counter *counter) {
if (!is_started(counter)) {
return;
}
@ -169,7 +169,7 @@ fps_counter_add_rendered_frame(struct fps_counter *counter) {
}
void
fps_counter_add_skipped_frame(struct fps_counter *counter) {
sc_fps_counter_add_skipped_frame(struct sc_fps_counter *counter) {
if (!is_started(counter)) {
return;
}

View file

@ -9,7 +9,7 @@
#include "util/thread.h"
struct fps_counter {
struct sc_fps_counter {
sc_thread thread;
sc_mutex mutex;
sc_cond state_cond;
@ -28,32 +28,32 @@ struct fps_counter {
};
bool
fps_counter_init(struct fps_counter *counter);
sc_fps_counter_init(struct sc_fps_counter *counter);
void
fps_counter_destroy(struct fps_counter *counter);
sc_fps_counter_destroy(struct sc_fps_counter *counter);
bool
fps_counter_start(struct fps_counter *counter);
sc_fps_counter_start(struct sc_fps_counter *counter);
void
fps_counter_stop(struct fps_counter *counter);
sc_fps_counter_stop(struct sc_fps_counter *counter);
bool
fps_counter_is_started(struct fps_counter *counter);
sc_fps_counter_is_started(struct sc_fps_counter *counter);
// request to stop the thread (on quit)
// must be called before fps_counter_join()
// must be called before sc_fps_counter_join()
void
fps_counter_interrupt(struct fps_counter *counter);
sc_fps_counter_interrupt(struct sc_fps_counter *counter);
void
fps_counter_join(struct fps_counter *counter);
sc_fps_counter_join(struct sc_fps_counter *counter);
void
fps_counter_add_rendered_frame(struct fps_counter *counter);
sc_fps_counter_add_rendered_frame(struct sc_fps_counter *counter);
void
fps_counter_add_skipped_frame(struct fps_counter *counter);
sc_fps_counter_add_skipped_frame(struct sc_fps_counter *counter);
#endif

View file

@ -242,14 +242,14 @@ set_screen_power_mode(struct sc_controller *controller,
}
static void
switch_fps_counter_state(struct fps_counter *fps_counter) {
switch_fps_counter_state(struct sc_fps_counter *fps_counter) {
// the started state can only be written from the current thread, so there
// is no ToCToU issue
if (fps_counter_is_started(fps_counter)) {
fps_counter_stop(fps_counter);
if (sc_fps_counter_is_started(fps_counter)) {
sc_fps_counter_stop(fps_counter);
LOGI("FPS counter stopped");
} else {
if (fps_counter_start(fps_counter)) {
if (sc_fps_counter_start(fps_counter)) {
LOGI("FPS counter started");
} else {
LOGE("FPS counter starting failed");

View file

@ -347,7 +347,7 @@ sc_video_buffer_on_new_frame(struct sc_video_buffer *vb, bool previous_skipped,
bool need_new_event;
if (previous_skipped) {
fps_counter_add_skipped_frame(&screen->fps_counter);
sc_fps_counter_add_skipped_frame(&screen->fps_counter);
// The EVENT_NEW_FRAME triggered for the previous frame will consume
// this new frame instead, unless the previous event failed
need_new_event = screen->event_failed;
@ -402,7 +402,7 @@ sc_screen_init(struct sc_screen *screen,
goto error_destroy_video_buffer;
}
if (!fps_counter_init(&screen->fps_counter)) {
if (!sc_fps_counter_init(&screen->fps_counter)) {
goto error_stop_and_join_video_buffer;
}
@ -534,7 +534,7 @@ error_destroy_renderer:
error_destroy_window:
SDL_DestroyWindow(screen->window);
error_destroy_fps_counter:
fps_counter_destroy(&screen->fps_counter);
sc_fps_counter_destroy(&screen->fps_counter);
error_stop_and_join_video_buffer:
sc_video_buffer_stop(&screen->vb);
sc_video_buffer_join(&screen->vb);
@ -573,13 +573,13 @@ sc_screen_hide_window(struct sc_screen *screen) {
void
sc_screen_interrupt(struct sc_screen *screen) {
sc_video_buffer_stop(&screen->vb);
fps_counter_interrupt(&screen->fps_counter);
sc_fps_counter_interrupt(&screen->fps_counter);
}
void
sc_screen_join(struct sc_screen *screen) {
sc_video_buffer_join(&screen->vb);
fps_counter_join(&screen->fps_counter);
sc_fps_counter_join(&screen->fps_counter);
}
void
@ -591,7 +591,7 @@ sc_screen_destroy(struct sc_screen *screen) {
SDL_DestroyTexture(screen->texture);
SDL_DestroyRenderer(screen->renderer);
SDL_DestroyWindow(screen->window);
fps_counter_destroy(&screen->fps_counter);
sc_fps_counter_destroy(&screen->fps_counter);
sc_video_buffer_destroy(&screen->vb);
}
@ -701,7 +701,7 @@ sc_screen_update_frame(struct sc_screen *screen) {
sc_video_buffer_consume(&screen->vb, screen->frame);
AVFrame *frame = screen->frame;
fps_counter_add_rendered_frame(&screen->fps_counter);
sc_fps_counter_add_rendered_frame(&screen->fps_counter);
struct sc_size new_frame_size = {frame->width, frame->height};
if (!prepare_for_frame(screen, new_frame_size)) {

View file

@ -26,7 +26,7 @@ struct sc_screen {
struct sc_input_manager im;
struct sc_video_buffer vb;
struct fps_counter fps_counter;
struct sc_fps_counter fps_counter;
// The initial requested window properties
struct {