wp_presentation: deduplicate code

This commit is contained in:
Aleksey Samoilov 2025-05-26 19:51:31 +04:00
parent 68d911085a
commit d46407a6e8
4 changed files with 25 additions and 38 deletions

View File

@ -483,9 +483,11 @@ static void presentation_handle_clock_id(void *data,
gfx_ctx_wayland_data_t *wl = data;
if (clock_id == CLOCK_MONOTONIC || clock_id == CLOCK_MONOTONIC_RAW)
{
wl->present_clock = true;
wl->present_clock_id = (clockid_t)clock_id;
}
}
static void presentation_feedback_sync_output(void *data,
struct wp_presentation_feedback *feedback,
@ -571,6 +573,27 @@ void wl_request_presentation_feedback(gfx_ctx_wayland_data_t *wl)
wl_list_insert(&wl->feedbacks, &fb->link);
}
void wait_for_next_frame(gfx_ctx_wayland_data_t *wl)
{
if (!wl->present_clock || wl->refresh_interval <= 0 || !wl->is_presented)
return;
struct timespec now;
clockid_t clock_type = (wl->present_clock_id == CLOCK_MONOTONIC ||
wl->present_clock_id == CLOCK_MONOTONIC_RAW)
? wl->present_clock_id
: CLOCK_MONOTONIC;
clock_gettime(clock_type, &now);
int64_t current_time = now.tv_sec * 1000000LL + now.tv_nsec / 1000;
int64_t next_frame = wl->last_ust + (wl->refresh_interval / 1000);
if (current_time < next_frame) {
int64_t sleep_us = next_frame - current_time;
const int64_t max_sleep = 1000000;
usleep(sleep_us > max_sleep ? max_sleep : sleep_us);
}
}
static int create_shm_file(off_t size)
{
int fd, ret;

View File

@ -513,25 +513,6 @@ static const struct wl_callback_listener wl_surface_frame_listener = {
.done = wl_surface_frame_done,
};
static void wait_for_next_frame(gfx_ctx_wayland_data_t *wl)
{
if (!wl->present_clock || wl->refresh_interval <= 0 || !wl->is_presented)
return;
struct timespec now;
clockid_t clock_type = (wl->present_clock_id == CLOCK_MONOTONIC ||
wl->present_clock_id == CLOCK_MONOTONIC_RAW)
? wl->present_clock_id
: CLOCK_MONOTONIC;
int64_t current_time = now.tv_sec * 1000000LL + now.tv_nsec / 1000;
int64_t next_frame = wl->last_ust + (wl->refresh_interval / 1000);
if (current_time < next_frame) {
int64_t sleep_us = next_frame - current_time;
usleep(sleep_us);
}
}
static void gfx_ctx_wl_swap_buffers(void *data)
{
#ifdef HAVE_EGL

View File

@ -256,25 +256,6 @@ static void *gfx_ctx_wl_get_context_data(void *data)
return &wl->vk.context;
}
static void wait_for_next_frame(gfx_ctx_wayland_data_t *wl)
{
if (!wl->present_clock || wl->refresh_interval <= 0)
return;
struct timespec now;
clockid_t clock_type = (wl->present_clock_id == CLOCK_MONOTONIC ||
wl->present_clock_id == CLOCK_MONOTONIC_RAW)
? wl->present_clock_id
: CLOCK_MONOTONIC;
int64_t current_time = now.tv_sec * 1000000LL + now.tv_nsec / 1000;
int64_t next_frame = wl->last_ust + (wl->refresh_interval / 1000);
if (current_time < next_frame) {
int64_t sleep_us = next_frame - current_time;
usleep(sleep_us);
}
}
static void gfx_ctx_wl_swap_buffers(void *data)
{
gfx_ctx_wayland_data_t *wl = (gfx_ctx_wayland_data_t*)data;

View File

@ -268,6 +268,8 @@ void flush_wayland_fd(void *data);
void wl_request_presentation_feedback(gfx_ctx_wayland_data_t *wl);
void wait_for_next_frame(gfx_ctx_wayland_data_t *wl);
extern const struct wl_keyboard_listener keyboard_listener;
extern const struct wl_pointer_listener pointer_listener;