From 2ac567b7156a964553c80fe64ea604fa88053fcf Mon Sep 17 00:00:00 2001 From: Brandon Schaefer Date: Thu, 26 Oct 2017 16:37:20 -0700 Subject: [PATCH] Fixed bug 3902 - Add a specific KMSDRM hint for low latency video --- include/SDL_hints.h | 10 ++++++++++ src/video/kmsdrm/SDL_kmsdrmopengles.c | 10 ++++++++-- src/video/kmsdrm/SDL_kmsdrmvideo.c | 11 +++++++++-- src/video/kmsdrm/SDL_kmsdrmvideo.h | 1 + 4 files changed, 28 insertions(+), 4 deletions(-) diff --git a/include/SDL_hints.h b/include/SDL_hints.h index 007a4bee05c0d..f4878686ddeb8 100644 --- a/include/SDL_hints.h +++ b/include/SDL_hints.h @@ -797,6 +797,16 @@ extern "C" { */ #define SDL_HINT_RPI_VIDEO_LAYER "SDL_RPI_VIDEO_LAYER" +/** + * \brief Tell SDL the KMS/DRM video driver that we want double buffer only. + * + * By default KMS/DRM will use a triple buffer solution that wastes no CPU + * time on waiting for vsync after issuing a flip, but introduces a frame of + * latency. Waiting for vsync immediately after issuing a flip on the other + * hand is recommended for cases where low latency is an important factor. + */ +#define SDL_HINT_KMSDRM_DOUBLE_BUFFER "SDL_KMSDRM_DOUBLE_BUFFER" + /** * \brief A variable controlling what driver to use for OpenGL ES contexts. * diff --git a/src/video/kmsdrm/SDL_kmsdrmopengles.c b/src/video/kmsdrm/SDL_kmsdrmopengles.c index 7ba663f14f460..deb7ebbe2a05f 100644 --- a/src/video/kmsdrm/SDL_kmsdrmopengles.c +++ b/src/video/kmsdrm/SDL_kmsdrmopengles.c @@ -71,7 +71,7 @@ KMSDRM_GLES_SetupCrtc(_THIS, SDL_Window * window) { return SDL_FALSE; } - + wdata->crtc_ready = SDL_TRUE; return SDL_TRUE; } @@ -159,7 +159,7 @@ KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window) { if(!KMSDRM_GLES_SetupCrtc(_this, window)) { SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not set up CRTC for doing vsync-ed pageflips"); return 0; - } + } } /* SDL_LogDebug(SDL_LOG_CATEGORY_VIDEO, "drmModePageFlip(%d, %u, %u, DRM_MODE_PAGE_FLIP_EVENT, &wdata->waiting_for_flip)", @@ -171,6 +171,12 @@ KMSDRM_GLES_SwapWindow(_THIS, SDL_Window * window) { } else { SDL_LogError(SDL_LOG_CATEGORY_VIDEO, "Could not queue pageflip: %d", ret); } + + /* Wait immediately for vsync (as if we only had two buffers), for low input-lag scenarios. + Run your SDL2 program with "SDL_KMSDRM_DOUBLE_BUFFER=1 " to enable this. */ + if (wdata->double_buffer) { + KMSDRM_WaitPageFlip(_this, wdata, -1); + } } return 0; diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.c b/src/video/kmsdrm/SDL_kmsdrmvideo.c index ee3ac0592a567..318191507a1ce 100644 --- a/src/video/kmsdrm/SDL_kmsdrmvideo.c +++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c @@ -27,6 +27,7 @@ #include "../SDL_sysvideo.h" #include "SDL_syswm.h" #include "SDL_log.h" +#include "SDL_hints.h" #include "../../events/SDL_mouse_c.h" #include "../../events/SDL_keyboard_c.h" @@ -522,11 +523,17 @@ KMSDRM_CreateWindow(_THIS, SDL_Window * window) } #endif /* SDL_VIDEO_OPENGL_EGL */ + /* In case we want low-latency, double-buffer video, we take note here */ + wdata->double_buffer = SDL_FALSE; + if (SDL_GetHintBoolean(SDL_HINT_KMSDRM_DOUBLE_BUFFER, SDL_FALSE)) { + wdata->double_buffer = SDL_TRUE; + } + /* Window is created, but we have yet to set up CRTC to one of the GBM buffers if we want drmModePageFlip to work, and we can't do it until EGL is completely setup, because we - need to do eglSwapBuffers so we can get a valid GBM buffer object to call + need to do eglSwapBuffers so we can get a valid GBM buffer object to call drmModeSetCrtc on it. */ - wdata->crtc_ready = SDL_FALSE; + wdata->crtc_ready = SDL_FALSE; /* Setup driver data for this window */ window->driverdata = wdata; diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.h b/src/video/kmsdrm/SDL_kmsdrmvideo.h index 71f0de722b8c2..c4735e4302ddd 100644 --- a/src/video/kmsdrm/SDL_kmsdrmvideo.h +++ b/src/video/kmsdrm/SDL_kmsdrmvideo.h @@ -63,6 +63,7 @@ typedef struct SDL_WindowData struct gbm_bo *next_bo; SDL_bool waiting_for_flip; SDL_bool crtc_ready; + SDL_bool double_buffer; #if SDL_VIDEO_OPENGL_EGL EGLSurface egl_surface; #endif