SDL: KMSDRM: fix unsupported modifiers (a2215)

From a2215ff81c4280b8510b9543958d9d7aaaf3fad2 Mon Sep 17 00:00:00 2001
From: Substring <[EMAIL REDACTED]>
Date: Wed, 10 Jul 2024 20:23:06 +0200
Subject: [PATCH] KMSDRM: fix unsupported modifiers

(cherry picked from commit 9548bba63a5508b0cba5778d3da0036726274de0)
---
 src/video/kmsdrm/SDL_kmsdrmsym.h   |  1 +
 src/video/kmsdrm/SDL_kmsdrmvideo.c | 17 +++++++++++++++--
 src/video/kmsdrm/SDL_kmsdrmvideo.h |  4 ++++
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/src/video/kmsdrm/SDL_kmsdrmsym.h b/src/video/kmsdrm/SDL_kmsdrmsym.h
index 49680448984c1..7a99e1b6a3302 100644
--- a/src/video/kmsdrm/SDL_kmsdrmsym.h
+++ b/src/video/kmsdrm/SDL_kmsdrmsym.h
@@ -133,6 +133,7 @@ SDL_KMSDRM_SYM(uint64_t,gbm_bo_get_modifier,(struct gbm_bo *bo))
 SDL_KMSDRM_SYM(int,gbm_bo_get_plane_count,(struct gbm_bo *bo))
 SDL_KMSDRM_SYM(uint32_t,gbm_bo_get_offset,(struct gbm_bo *bo, int plane))
 SDL_KMSDRM_SYM(uint32_t,gbm_bo_get_stride_for_plane,(struct gbm_bo *bo, int plane))
+SDL_KMSDRM_SYM(union gbm_bo_handle,gbm_bo_get_handle_for_plane,(struct gbm_bo *bo, int plane);)
 
 #undef SDL_KMSDRM_MODULE
 #undef SDL_KMSDRM_SYM
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.c b/src/video/kmsdrm/SDL_kmsdrmvideo.c
index 2dab65cbccc0e..e4457921c46ae 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.c
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c
@@ -368,16 +368,29 @@ KMSDRM_FBInfo *KMSDRM_FBFromBO(_THIS, struct gbm_bo *bo)
     num_planes = KMSDRM_gbm_bo_get_plane_count(bo);
     for (int i = 0; i < num_planes; i++) {
         strides[i] = KMSDRM_gbm_bo_get_stride_for_plane(bo, i);
-        handles[i] = KMSDRM_gbm_bo_get_handle(bo).u32;
+        handles[i] = KMSDRM_gbm_bo_get_handle_for_plane(bo, i).u32;
         offsets[i] = KMSDRM_gbm_bo_get_offset(bo, i);
         modifiers[i] = modifiers[0];
     }
 
-    if (modifiers[0]) {
+    if (modifiers[0] && modifiers[0] != DRM_FORMAT_MOD_INVALID) {
         flags = DRM_MODE_FB_MODIFIERS;
     }
 
     ret = KMSDRM_drmModeAddFB2WithModifiers(viddata->drm_fd, w, h, format, handles, strides, offsets, modifiers, &fb_info->fb_id, flags);
+
+    if (ret) {
+        handles[0] = KMSDRM_gbm_bo_get_handle(bo).u32;
+        strides[0] = KMSDRM_gbm_bo_get_stride(bo);
+        offsets[0] = 0;
+        for (int i = 1; i<4; i++) {
+            handles[i] = 0;
+            strides[i] = 0;
+            offsets[i] = 0;
+        }
+        ret = KMSDRM_drmModeAddFB2(viddata->drm_fd, w, h, format, handles, strides, offsets, &fb_info->fb_id, 0);
+    }
+
     if (ret) {
         SDL_free(fb_info);
         return NULL;
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.h b/src/video/kmsdrm/SDL_kmsdrmvideo.h
index 096f00cd162d6..1fe77f697b5e3 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.h
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.h
@@ -33,6 +33,10 @@
 #include <gbm.h>
 #include <EGL/egl.h>
 
+#ifndef DRM_FORMAT_MOD_INVALID
+#define DRM_FORMAT_MOD_INVALID 0x00ffffffffffffffULL
+#endif
+
 #ifndef DRM_MODE_FB_MODIFIERS
 #define DRM_MODE_FB_MODIFIERS	2
 #endif