mirror of
https://github.com/Genymobile/scrcpy.git
synced 2025-08-03 14:49:29 +00:00
Wrap tick API
This avoids to use the SDL timer API directly, and will allow to handle generic ticks (possibly negative).
This commit is contained in:
parent
e9096e3e34
commit
1dcd3f295d
4 changed files with 27 additions and 10 deletions
|
@ -1,7 +1,6 @@
|
||||||
#include "fps_counter.h"
|
#include "fps_counter.h"
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <SDL2/SDL_timer.h>
|
|
||||||
|
|
||||||
#include "util/log.h"
|
#include "util/log.h"
|
||||||
|
|
||||||
|
@ -82,11 +81,11 @@ run_fps_counter(void *data) {
|
||||||
sc_cond_wait(&counter->state_cond, &counter->mutex);
|
sc_cond_wait(&counter->state_cond, &counter->mutex);
|
||||||
}
|
}
|
||||||
while (!counter->interrupted && is_started(counter)) {
|
while (!counter->interrupted && is_started(counter)) {
|
||||||
uint32_t now = SDL_GetTicks();
|
sc_tick now = sc_tick_now();
|
||||||
check_interval_expired(counter, now);
|
check_interval_expired(counter, now);
|
||||||
|
|
||||||
assert(counter->next_timestamp > now);
|
assert(counter->next_timestamp > now);
|
||||||
uint32_t remaining = counter->next_timestamp - now;
|
sc_tick remaining = counter->next_timestamp - now;
|
||||||
|
|
||||||
// ignore the reason (timeout or signaled), we just loop anyway
|
// ignore the reason (timeout or signaled), we just loop anyway
|
||||||
sc_cond_timedwait(&counter->state_cond, &counter->mutex, remaining);
|
sc_cond_timedwait(&counter->state_cond, &counter->mutex, remaining);
|
||||||
|
@ -99,7 +98,7 @@ run_fps_counter(void *data) {
|
||||||
bool
|
bool
|
||||||
fps_counter_start(struct fps_counter *counter) {
|
fps_counter_start(struct fps_counter *counter) {
|
||||||
sc_mutex_lock(&counter->mutex);
|
sc_mutex_lock(&counter->mutex);
|
||||||
counter->next_timestamp = SDL_GetTicks() + FPS_COUNTER_INTERVAL_MS;
|
counter->next_timestamp = sc_tick_now() + FPS_COUNTER_INTERVAL_MS;
|
||||||
counter->nr_rendered = 0;
|
counter->nr_rendered = 0;
|
||||||
counter->nr_skipped = 0;
|
counter->nr_skipped = 0;
|
||||||
sc_mutex_unlock(&counter->mutex);
|
sc_mutex_unlock(&counter->mutex);
|
||||||
|
@ -165,7 +164,7 @@ fps_counter_add_rendered_frame(struct fps_counter *counter) {
|
||||||
}
|
}
|
||||||
|
|
||||||
sc_mutex_lock(&counter->mutex);
|
sc_mutex_lock(&counter->mutex);
|
||||||
uint32_t now = SDL_GetTicks();
|
sc_tick now = sc_tick_now();
|
||||||
check_interval_expired(counter, now);
|
check_interval_expired(counter, now);
|
||||||
++counter->nr_rendered;
|
++counter->nr_rendered;
|
||||||
sc_mutex_unlock(&counter->mutex);
|
sc_mutex_unlock(&counter->mutex);
|
||||||
|
@ -178,7 +177,7 @@ fps_counter_add_skipped_frame(struct fps_counter *counter) {
|
||||||
}
|
}
|
||||||
|
|
||||||
sc_mutex_lock(&counter->mutex);
|
sc_mutex_lock(&counter->mutex);
|
||||||
uint32_t now = SDL_GetTicks();
|
sc_tick now = sc_tick_now();
|
||||||
check_interval_expired(counter, now);
|
check_interval_expired(counter, now);
|
||||||
++counter->nr_skipped;
|
++counter->nr_skipped;
|
||||||
sc_mutex_unlock(&counter->mutex);
|
sc_mutex_unlock(&counter->mutex);
|
||||||
|
|
|
@ -24,7 +24,7 @@ struct fps_counter {
|
||||||
bool interrupted;
|
bool interrupted;
|
||||||
unsigned nr_rendered;
|
unsigned nr_rendered;
|
||||||
unsigned nr_skipped;
|
unsigned nr_skipped;
|
||||||
uint32_t next_timestamp;
|
sc_tick next_timestamp;
|
||||||
};
|
};
|
||||||
|
|
||||||
bool
|
bool
|
||||||
|
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
#include <assert.h>
|
#include <assert.h>
|
||||||
#include <SDL2/SDL_thread.h>
|
#include <SDL2/SDL_thread.h>
|
||||||
|
#include <SDL2/SDL_timer.h>
|
||||||
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
|
|
||||||
|
@ -123,8 +124,12 @@ sc_cond_wait(sc_cond *cond, sc_mutex *mutex) {
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, uint32_t ms) {
|
sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, sc_tick ms) {
|
||||||
int r = SDL_CondWaitTimeout(cond->cond, mutex->mutex, ms);
|
if (ms < 0) {
|
||||||
|
return false; // timeout
|
||||||
|
}
|
||||||
|
|
||||||
|
int r = SDL_CondWaitTimeout(cond->cond, mutex->mutex, (uint32_t) ms);
|
||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
if (r < 0) {
|
if (r < 0) {
|
||||||
LOGC("Could not wait on condition with timeout: %s", SDL_GetError());
|
LOGC("Could not wait on condition with timeout: %s", SDL_GetError());
|
||||||
|
@ -163,3 +168,11 @@ sc_cond_broadcast(sc_cond *cond) {
|
||||||
(void) r;
|
(void) r;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
sc_tick
|
||||||
|
sc_tick_now(void) {
|
||||||
|
// SDL ticks is an unsigned 32 bits, but this is an implementation detail.
|
||||||
|
// It wraps if the program runs for more than ~49 days, but in practice we
|
||||||
|
// can assume it does not.
|
||||||
|
return (sc_tick) SDL_GetTicks();
|
||||||
|
}
|
||||||
|
|
|
@ -16,6 +16,8 @@ typedef int sc_thread_fn(void *);
|
||||||
typedef unsigned sc_thread_id;
|
typedef unsigned sc_thread_id;
|
||||||
typedef atomic_uint sc_atomic_thread_id;
|
typedef atomic_uint sc_atomic_thread_id;
|
||||||
|
|
||||||
|
typedef int64_t sc_tick;
|
||||||
|
|
||||||
typedef struct sc_thread {
|
typedef struct sc_thread {
|
||||||
SDL_Thread *thread;
|
SDL_Thread *thread;
|
||||||
} sc_thread;
|
} sc_thread;
|
||||||
|
@ -72,7 +74,7 @@ sc_cond_wait(sc_cond *cond, sc_mutex *mutex);
|
||||||
|
|
||||||
// return true on signaled, false on timeout
|
// return true on signaled, false on timeout
|
||||||
bool
|
bool
|
||||||
sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, uint32_t ms);
|
sc_cond_timedwait(sc_cond *cond, sc_mutex *mutex, sc_tick ms);
|
||||||
|
|
||||||
void
|
void
|
||||||
sc_cond_signal(sc_cond *cond);
|
sc_cond_signal(sc_cond *cond);
|
||||||
|
@ -80,4 +82,7 @@ sc_cond_signal(sc_cond *cond);
|
||||||
void
|
void
|
||||||
sc_cond_broadcast(sc_cond *cond);
|
sc_cond_broadcast(sc_cond *cond);
|
||||||
|
|
||||||
|
sc_tick
|
||||||
|
sc_tick_now(void);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue