2018-12-15 16:11:41 -05:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
// Copyright (c) Yuxuan Shui <yshuiv7@gmail.com>
|
|
|
|
|
|
|
|
/// Function pointers to init VSync modes.
|
|
|
|
|
|
|
|
#include "common.h"
|
|
|
|
#include "log.h"
|
2018-12-15 21:48:02 -05:00
|
|
|
|
|
|
|
#ifdef CONFIG_OPENGL
|
2019-02-08 17:34:53 -05:00
|
|
|
#include "backend/gl/glx.h"
|
2018-12-15 16:11:41 -05:00
|
|
|
#include "opengl.h"
|
2018-12-15 21:48:02 -05:00
|
|
|
#endif
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2018-12-21 17:25:24 -05:00
|
|
|
#ifdef CONFIG_VSYNC_DRM
|
|
|
|
#include <drm.h>
|
|
|
|
#include <errno.h>
|
2019-03-10 08:34:37 -04:00
|
|
|
#include <fcntl.h>
|
2018-12-21 17:25:24 -05:00
|
|
|
#include <sys/ioctl.h>
|
2019-03-10 08:34:37 -04:00
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/types.h>
|
2018-12-21 17:25:24 -05:00
|
|
|
#endif
|
|
|
|
|
2019-01-18 18:30:44 -05:00
|
|
|
#include "config.h"
|
2018-12-15 16:11:41 -05:00
|
|
|
#include "vsync.h"
|
|
|
|
|
2018-12-21 17:25:24 -05:00
|
|
|
#ifdef CONFIG_VSYNC_DRM
|
|
|
|
/**
|
|
|
|
* Wait for next VSync, DRM method.
|
|
|
|
*
|
2019-03-10 08:34:37 -04:00
|
|
|
* Stolen from:
|
|
|
|
* https://github.com/MythTV/mythtv/blob/master/mythtv/libs/libmythtv/vsync.cpp
|
2018-12-21 17:25:24 -05:00
|
|
|
*/
|
2019-03-10 08:34:37 -04:00
|
|
|
static int vsync_drm_wait(session_t *ps) {
|
|
|
|
int ret = -1;
|
|
|
|
drm_wait_vblank_t vbl;
|
2018-12-21 17:25:24 -05:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
vbl.request.type = _DRM_VBLANK_RELATIVE, vbl.request.sequence = 1;
|
2018-12-21 17:25:24 -05:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
do {
|
|
|
|
ret = ioctl(ps->drm_fd, DRM_IOCTL_WAIT_VBLANK, &vbl);
|
2019-03-30 05:07:21 -04:00
|
|
|
vbl.request.type &= ~(uint)_DRM_VBLANK_RELATIVE;
|
2019-03-10 08:34:37 -04:00
|
|
|
} while (ret && errno == EINTR);
|
2018-12-21 17:25:24 -05:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
if (ret)
|
|
|
|
log_error("VBlank ioctl did not work, unimplemented in this drmver?");
|
2018-12-21 17:25:24 -05:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
return ret;
|
2018-12-21 17:25:24 -05:00
|
|
|
}
|
|
|
|
|
2018-12-15 16:11:41 -05:00
|
|
|
/**
|
|
|
|
* Initialize DRM VSync.
|
|
|
|
*
|
|
|
|
* @return true for success, false otherwise
|
|
|
|
*/
|
2019-03-10 08:34:37 -04:00
|
|
|
static bool vsync_drm_init(session_t *ps) {
|
|
|
|
// Should we always open card0?
|
|
|
|
if (ps->drm_fd < 0 && (ps->drm_fd = open("/dev/dri/card0", O_RDWR)) < 0) {
|
|
|
|
log_error("Failed to open device.");
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
if (vsync_drm_wait(ps))
|
|
|
|
return false;
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
return true;
|
2018-12-15 16:11:41 -05:00
|
|
|
}
|
2019-03-10 10:55:17 -04:00
|
|
|
#endif
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2019-03-10 10:55:17 -04:00
|
|
|
#ifdef CONFIG_OPENGL
|
2018-12-15 16:11:41 -05:00
|
|
|
/**
|
|
|
|
* Initialize OpenGL VSync.
|
|
|
|
*
|
2019-03-10 08:34:37 -04:00
|
|
|
* Stolen from:
|
|
|
|
* http://git.tuxfamily.org/?p=ccm/cairocompmgr.git;a=commitdiff;h=efa4ceb97da501e8630ca7f12c99b1dce853c73e
|
2018-12-15 16:11:41 -05:00
|
|
|
* Possible original source: http://www.inb.uni-luebeck.de/~boehme/xvideo_sync.html
|
|
|
|
*
|
|
|
|
* @return true for success, false otherwise
|
|
|
|
*/
|
2019-03-10 08:34:37 -04:00
|
|
|
static bool vsync_opengl_init(session_t *ps) {
|
2023-06-29 00:39:36 -04:00
|
|
|
if (!ensure_glx_context(ps)) {
|
2019-03-10 08:34:37 -04:00
|
|
|
return false;
|
2023-06-29 00:39:36 -04:00
|
|
|
}
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
return glxext.has_GLX_SGI_video_sync;
|
2018-12-15 16:11:41 -05:00
|
|
|
}
|
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
static bool vsync_opengl_oml_init(session_t *ps) {
|
2023-06-29 00:39:36 -04:00
|
|
|
if (!ensure_glx_context(ps)) {
|
2019-03-10 08:34:37 -04:00
|
|
|
return false;
|
2023-06-29 00:39:36 -04:00
|
|
|
}
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
return glxext.has_GLX_OML_sync_control;
|
2018-12-15 16:11:41 -05:00
|
|
|
}
|
|
|
|
|
2019-03-30 05:07:21 -04:00
|
|
|
static inline bool vsync_opengl_swc_swap_interval(session_t *ps, int interval) {
|
2023-06-29 00:39:36 -04:00
|
|
|
if (glxext.has_GLX_MESA_swap_control) {
|
2019-03-30 05:07:21 -04:00
|
|
|
return glXSwapIntervalMESA((uint)interval) == 0;
|
2023-06-29 00:39:36 -04:00
|
|
|
}
|
|
|
|
if (glxext.has_GLX_SGI_swap_control) {
|
2019-03-10 08:34:37 -04:00
|
|
|
return glXSwapIntervalSGI(interval) == 0;
|
2023-06-29 00:39:36 -04:00
|
|
|
}
|
|
|
|
if (glxext.has_GLX_EXT_swap_control) {
|
2019-03-10 08:34:37 -04:00
|
|
|
GLXDrawable d = glXGetCurrentDrawable();
|
|
|
|
if (d == None) {
|
|
|
|
// We don't have a context??
|
|
|
|
return false;
|
|
|
|
}
|
2023-06-29 00:39:36 -04:00
|
|
|
glXSwapIntervalEXT(ps->c.dpy, glXGetCurrentDrawable(), interval);
|
2019-03-10 08:34:37 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
2018-12-15 16:11:41 -05:00
|
|
|
}
|
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
static bool vsync_opengl_swc_init(session_t *ps) {
|
|
|
|
if (!bkend_use_glx(ps)) {
|
2019-03-10 10:55:17 -04:00
|
|
|
log_error("OpenGL swap control requires the GLX backend.");
|
2019-03-10 08:34:37 -04:00
|
|
|
return false;
|
|
|
|
}
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
if (!vsync_opengl_swc_swap_interval(ps, 1)) {
|
|
|
|
log_error("Failed to load a swap control extension.");
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
return true;
|
2018-12-15 16:11:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait for next VSync, OpenGL method.
|
|
|
|
*/
|
2019-07-24 21:27:02 -04:00
|
|
|
static int vsync_opengl_wait(session_t *ps attr_unused) {
|
2019-03-10 08:34:37 -04:00
|
|
|
unsigned vblank_count = 0;
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2019-03-10 08:34:37 -04:00
|
|
|
glXGetVideoSyncSGI(&vblank_count);
|
|
|
|
glXWaitVideoSyncSGI(2, (vblank_count + 1) % 2, &vblank_count);
|
|
|
|
return 0;
|
2018-12-15 16:11:41 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Wait for next VSync, OpenGL OML method.
|
|
|
|
*
|
|
|
|
* https://mail.gnome.org/archives/clutter-list/2012-November/msg00031.html
|
|
|
|
*/
|
2019-03-10 08:34:37 -04:00
|
|
|
static int vsync_opengl_oml_wait(session_t *ps) {
|
|
|
|
int64_t ust = 0, msc = 0, sbc = 0;
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2023-06-29 00:39:36 -04:00
|
|
|
glXGetSyncValuesOML(ps->c.dpy, ps->reg_win, &ust, &msc, &sbc);
|
|
|
|
glXWaitForMscOML(ps->c.dpy, ps->reg_win, 0, 2, (msc + 1) % 2, &ust, &msc, &sbc);
|
2019-03-10 08:34:37 -04:00
|
|
|
return 0;
|
2018-12-15 16:11:41 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize current VSync method.
|
|
|
|
*/
|
|
|
|
bool vsync_init(session_t *ps) {
|
2019-02-08 19:16:47 -05:00
|
|
|
#ifdef CONFIG_OPENGL
|
2019-03-10 10:55:17 -04:00
|
|
|
if (bkend_use_glx(ps)) {
|
|
|
|
// Mesa turns on swap control by default, undo that
|
2019-03-10 08:34:37 -04:00
|
|
|
vsync_opengl_swc_swap_interval(ps, 0);
|
2019-03-10 10:55:17 -04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifdef CONFIG_VSYNC_DRM
|
|
|
|
log_warn("The DRM vsync method is deprecated, please don't enable it.");
|
2019-02-08 19:16:47 -05:00
|
|
|
#endif
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2019-03-10 10:55:17 -04:00
|
|
|
if (!ps->o.vsync) {
|
2019-03-10 08:34:37 -04:00
|
|
|
return true;
|
2019-03-10 10:55:17 -04:00
|
|
|
}
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2019-03-10 10:55:17 -04:00
|
|
|
#ifdef CONFIG_OPENGL
|
|
|
|
if (bkend_use_glx(ps)) {
|
|
|
|
if (!vsync_opengl_swc_init(ps)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
ps->vsync_wait = NULL; // glXSwapBuffers will automatically wait
|
|
|
|
// for vsync, we don't need to do anything.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2019-03-10 10:55:17 -04:00
|
|
|
// Oh no, we are not using glx backend.
|
|
|
|
// Throwing things at wall.
|
|
|
|
#ifdef CONFIG_OPENGL
|
|
|
|
if (vsync_opengl_oml_init(ps)) {
|
|
|
|
log_info("Using the opengl-oml vsync method");
|
|
|
|
ps->vsync_wait = vsync_opengl_oml_wait;
|
|
|
|
return true;
|
|
|
|
}
|
2018-12-15 16:11:41 -05:00
|
|
|
|
2019-03-10 10:55:17 -04:00
|
|
|
if (vsync_opengl_init(ps)) {
|
|
|
|
log_info("Using the opengl vsync method");
|
|
|
|
ps->vsync_wait = vsync_opengl_wait;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef CONFIG_VSYNC_DRM
|
|
|
|
if (vsync_drm_init(ps)) {
|
|
|
|
log_info("Using the drm vsync method");
|
|
|
|
ps->vsync_wait = vsync_drm_wait;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
log_error("No supported vsync method found for this backend");
|
|
|
|
return false;
|
2018-12-15 16:11:41 -05:00
|
|
|
}
|