SDL: kmsdrm: fixed creating GBM surfaces on NVIDIA cards

From fc1c0618ded275a745852c7c4a6db374bfef9927 Mon Sep 17 00:00:00 2001
From: Sam Lantinga <[EMAIL REDACTED]>
Date: Thu, 29 May 2025 11:19:50 -0700
Subject: [PATCH] kmsdrm: fixed creating GBM surfaces on NVIDIA cards

---
 src/video/kmsdrm/SDL_kmsdrmvideo.c | 11 ++++++++---
 1 file changed, 8 insertions(+), 3 deletions(-)

diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.c b/src/video/kmsdrm/SDL_kmsdrmvideo.c
index 3de905c13a56d..adca91c9fd04f 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.c
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c
@@ -1368,9 +1368,14 @@ bool KMSDRM_CreateSurfaces(SDL_VideoDevice *_this, SDL_Window *window)
     windata->gs = KMSDRM_gbm_surface_create(viddata->gbm_dev,
                                             dispdata->mode.hdisplay, dispdata->mode.vdisplay,
                                             surface_fmt, surface_flags);
-
+    if (!windata->gs && errno == ENOSYS) {
+        // Try again without the scanout flags, needed on NVIDIA drivers
+        windata->gs = KMSDRM_gbm_surface_create(viddata->gbm_dev,
+                                                dispdata->mode.hdisplay, dispdata->mode.vdisplay,
+                                                surface_fmt, 0);
+    }
     if (!windata->gs) {
-        return SDL_SetError("Could not create GBM surface");
+        return SDL_SetError("Could not create GBM surface: %s", strerror(errno));
     }
 
     /* We can't get the EGL context yet because SDL_CreateRenderer has not been called,
@@ -1731,7 +1736,7 @@ bool KMSDRM_CreateWindow(SDL_VideoDevice *_this, SDL_Window *window, SDL_Propert
         /* Create the window surfaces with the size we have just chosen.
            Needs the window driverdata in place. */
         if (!KMSDRM_CreateSurfaces(_this, window)) {
-            return SDL_SetError("Can't create GBM/EGL surfaces on window creation.");
+            return false;
         }
     } // NON-Vulkan block ends.