Skip to content

Commit

Permalink
vo: use mp_poll wrapper in wait_events when applicable
Browse files Browse the repository at this point in the history
On linux, several platforms poll for events over a fd. This has ms
accuracy, but mpv's timer is in ns now so lots of precision is lost. We
can use an mp_poll wrapper to use ppoll instead which takes a timespec
directly with nanosecond precision. On systems without ppoll this falls
back to old poll behavior. On wayland, we don't actually use this
because ppoll completely messes up the event loop for some unknown
reason.
  • Loading branch information
Dudemanguy committed Sep 29, 2023
1 parent a041e5d commit f09acbc
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 6 deletions.
3 changes: 3 additions & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,9 @@ if posix and not darwin
'osdep/timer-linux.c')
endif

features += {'ppoll': cc.has_function('ppoll', args: '-D_GNU_SOURCE',
prefix: '#include <poll.h>')}

cd_devices = {
'windows': 'D:',
'cygwin': 'D:',
Expand Down
11 changes: 11 additions & 0 deletions osdep/poll_wrapper.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,20 @@
#include <sys/select.h>
#include <stdio.h>

#include "config.h"
#include "poll_wrapper.h"
#include "timer.h"


int mp_poll(struct pollfd *fds, int nfds, int64_t timeout_ns)
{
#if HAVE_PPOLL
struct timespec ts = mp_time_ns_to_realtime(timeout_ns);
return ppoll(fds, nfds, &ts, NULL);
#endif
return poll(fds, nfds, timeout_ns / 1e6);
}

// poll shim that supports device files on macOS.
int polldev(struct pollfd fds[], nfds_t nfds, int timeout)
{
Expand Down
5 changes: 5 additions & 0 deletions osdep/poll_wrapper.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#pragma once

#include <poll.h>
#include <stdint.h>

// Behaves like poll(3) but works for device files on macOS.
// Only supports POLLIN and POLLOUT.
int polldev(struct pollfd fds[], nfds_t nfds, int timeout);

// Generic polling wrapper. It will try and use higher resolution
// polling (ppoll) if available.
int mp_poll(struct pollfd *fds, int nfds, int64_t timeout_ns);
9 changes: 5 additions & 4 deletions video/out/drm_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "common/msg.h"
#include "options/m_config.h"
#include "osdep/io.h"
#include "osdep/poll_wrapper.h"
#include "osdep/timer.h"
#include "misc/ctype.h"
#include "video/out/vo.h"
Expand Down Expand Up @@ -283,12 +284,12 @@ static void vt_switcher_destroy(struct vt_switcher *s)
close(vt_switcher_pipe[1]);
}

static void vt_switcher_poll(struct vt_switcher *s, int timeout_ms)
static void vt_switcher_poll(struct vt_switcher *s, int timeout_ns)
{
struct pollfd fds[1] = {
{ .events = POLLIN, .fd = vt_switcher_pipe[0] },
};
poll(fds, 1, timeout_ms);
mp_poll(fds, 1, timeout_ns);
if (!fds[0].revents)
return;

Expand Down Expand Up @@ -1309,8 +1310,8 @@ void vo_drm_wait_events(struct vo *vo, int64_t until_time_ns)
struct vo_drm_state *drm = vo->drm;
if (drm->vt_switcher_active) {
int64_t wait_ns = until_time_ns - mp_time_ns();
int timeout_ms = MPCLAMP((wait_ns + 500000) / 1e6, 0, 10000);
vt_switcher_poll(&drm->vt_switcher, timeout_ms);
int64_t timeout_ns = MPCLAMP((wait_ns + 500000), 0, 1e7);
vt_switcher_poll(&drm->vt_switcher, timeout_ns);
} else {
vo_wait_default(vo, until_time_ns);
}
Expand Down
5 changes: 3 additions & 2 deletions video/out/x11_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
#include "vo.h"
#include "win_state.h"
#include "osdep/io.h"
#include "osdep/poll_wrapper.h"
#include "osdep/timer.h"
#include "osdep/subprocess.h"

Expand Down Expand Up @@ -2177,9 +2178,9 @@ void vo_x11_wait_events(struct vo *vo, int64_t until_time_ns)
{ .fd = x11->wakeup_pipe[0], .events = POLLIN },
};
int64_t wait_ns = until_time_ns - mp_time_ns();
int timeout_ms = MPCLAMP((wait_ns + 999000) / 1e6, 0, 10000);
int64_t timeout_ns = MPCLAMP((wait_ns + 999000), 0, 1e7);

poll(fds, 2, timeout_ms);
mp_poll(fds, 2, timeout_ns);

if (fds[1].revents & POLLIN)
mp_flush_wakeup_pipe(x11->wakeup_pipe[0]);
Expand Down

0 comments on commit f09acbc

Please sign in to comment.