diff --git a/gfx/common/wayland_common.c b/gfx/common/wayland_common.c index e31b409cab..f527d4db5c 100644 --- a/gfx/common/wayland_common.c +++ b/gfx/common/wayland_common.c @@ -483,8 +483,10 @@ 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, @@ -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; diff --git a/gfx/drivers_context/wayland_ctx.c b/gfx/drivers_context/wayland_ctx.c index b0c2c39d40..8d32fd4e7b 100644 --- a/gfx/drivers_context/wayland_ctx.c +++ b/gfx/drivers_context/wayland_ctx.c @@ -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 diff --git a/gfx/drivers_context/wayland_vk_ctx.c b/gfx/drivers_context/wayland_vk_ctx.c index a6489f9e21..f9c4cb053f 100644 --- a/gfx/drivers_context/wayland_vk_ctx.c +++ b/gfx/drivers_context/wayland_vk_ctx.c @@ -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; diff --git a/input/common/wayland_common.h b/input/common/wayland_common.h index a6add17966..6e365f830b 100644 --- a/input/common/wayland_common.h +++ b/input/common/wayland_common.h @@ -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;