Commit 2ac567b7156a964553c80fe64ea604fa88053fcf

Brandon Schaefer 2017-10-26T16:37:20

Fixed bug 3902 - Add a specific KMSDRM hint for low latency video

diff --git a/include/SDL_hints.h b/include/SDL_hints.h
index 007a4be..f487868 100644
--- a/include/SDL_hints.h
+++ b/include/SDL_hints.h
@@ -798,6 +798,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.
  *
  *  On some platforms, currently Windows and X11, OpenGL drivers may support
diff --git a/src/video/kmsdrm/SDL_kmsdrmopengles.c b/src/video/kmsdrm/SDL_kmsdrmopengles.c
index 7ba663f..deb7ebb 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 <program_name>" 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 ee3ac05..3181915 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 71f0de7..c4735e4 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