Skip to content

Commit

Permalink
Fixed bug 3902 - Add a specific KMSDRM hint for low latency video
Browse files Browse the repository at this point in the history
  • Loading branch information
BrandonSchaefer committed Oct 26, 2017
1 parent f24c667 commit 2ac567b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 4 deletions.
10 changes: 10 additions & 0 deletions include/SDL_hints.h
Expand Up @@ -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.
*
Expand Down
10 changes: 8 additions & 2 deletions src/video/kmsdrm/SDL_kmsdrmopengles.c
Expand Up @@ -71,7 +71,7 @@ KMSDRM_GLES_SetupCrtc(_THIS, SDL_Window * window) {
return SDL_FALSE;

}

wdata->crtc_ready = SDL_TRUE;
return SDL_TRUE;
}
Expand Down Expand Up @@ -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)",
Expand All @@ -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 <program_name>" to enable this. */
if (wdata->double_buffer) {
KMSDRM_WaitPageFlip(_this, wdata, -1);
}
}

return 0;
Expand Down
11 changes: 9 additions & 2 deletions src/video/kmsdrm/SDL_kmsdrmvideo.c
Expand Up @@ -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"

Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/video/kmsdrm/SDL_kmsdrmvideo.h
Expand Up @@ -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
Expand Down

0 comments on commit 2ac567b

Please sign in to comment.