SDL: workaround for libmali gbm_device_get_fd()

From 66b0a6ee15fc0dd2d2fd68c96278b64414995d11 Mon Sep 17 00:00:00 2001
From: Davis Mosenkovs <[EMAIL REDACTED]>
Date: Wed, 1 Dec 2021 01:33:57 +0200
Subject: [PATCH] workaround for libmali gbm_device_get_fd()

gbm_device_get_fd() in at least some libmali versions duplicates handle.
Other implementations do not do duplication. To prevent handle leak save
drm_fd in SDL_DisplayData.
---
 src/video/kmsdrm/SDL_kmsdrmmouse.c | 12 +++++-------
 src/video/kmsdrm/SDL_kmsdrmsym.h   |  1 -
 src/video/kmsdrm/SDL_kmsdrmvideo.c |  1 +
 src/video/kmsdrm/SDL_kmsdrmvideo.h |  1 +
 4 files changed, 7 insertions(+), 8 deletions(-)

diff --git a/src/video/kmsdrm/SDL_kmsdrmmouse.c b/src/video/kmsdrm/SDL_kmsdrmmouse.c
index b459090271a..231d5b1d639 100644
--- a/src/video/kmsdrm/SDL_kmsdrmmouse.c
+++ b/src/video/kmsdrm/SDL_kmsdrmmouse.c
@@ -73,6 +73,7 @@ KMSDRM_DestroyCursorBO (_THIS, SDL_VideoDisplay *display)
     if (dispdata->cursor_bo) {
         KMSDRM_gbm_bo_destroy(dispdata->cursor_bo);
         dispdata->cursor_bo = NULL;
+        dispdata->cursor_bo_drm_fd = -1;
     }
 }
 
@@ -116,6 +117,8 @@ KMSDRM_CreateCursorBO (SDL_VideoDisplay *display) {
         SDL_SetError("Could not create GBM cursor BO");
         return;
     }
+
+    dispdata->cursor_bo_drm_fd = viddata->drm_fd;
 } 
 
 /* Remove a cursor buffer from a display's DRM cursor BO. */
@@ -384,11 +387,9 @@ KMSDRM_WarpMouseGlobal(int x, int y)
 
         /* And now update the cursor graphic position on screen. */
         if (dispdata->cursor_bo) {
-            int drm_fd;
             int ret = 0;
 
-            drm_fd = KMSDRM_gbm_device_get_fd(KMSDRM_gbm_bo_get_device(dispdata->cursor_bo));
-            ret = KMSDRM_drmModeMoveCursor(drm_fd, dispdata->crtc->crtc_id, x, y);
+            ret = KMSDRM_drmModeMoveCursor(dispdata->cursor_bo_drm_fd, dispdata->crtc->crtc_id, x, y);
 
             if (ret) {
                 SDL_SetError("drmModeMoveCursor() failed.");
@@ -437,7 +438,6 @@ static void
 KMSDRM_MoveCursor(SDL_Cursor * cursor)
 {
     SDL_Mouse *mouse = SDL_GetMouse();
-    int drm_fd;
     int ret = 0;
 
     /* We must NOT call SDL_SendMouseMotion() here or we will enter recursivity!
@@ -452,9 +452,7 @@ KMSDRM_MoveCursor(SDL_Cursor * cursor)
             return;
         }
 
-        drm_fd = KMSDRM_gbm_device_get_fd(KMSDRM_gbm_bo_get_device(dispdata->cursor_bo));
-
-        ret = KMSDRM_drmModeMoveCursor(drm_fd, dispdata->crtc->crtc_id, mouse->x, mouse->y);
+        ret = KMSDRM_drmModeMoveCursor(dispdata->cursor_bo_drm_fd, dispdata->crtc->crtc_id, mouse->x, mouse->y);
 
         if (ret) {
             SDL_SetError("drmModeMoveCursor() failed.");
diff --git a/src/video/kmsdrm/SDL_kmsdrmsym.h b/src/video/kmsdrm/SDL_kmsdrmsym.h
index 90d12858200..e368c5d0de3 100644
--- a/src/video/kmsdrm/SDL_kmsdrmsym.h
+++ b/src/video/kmsdrm/SDL_kmsdrmsym.h
@@ -95,7 +95,6 @@ SDL_KMSDRM_SYM(int,drmModeSetPlane,(int fd, uint32_t plane_id, uint32_t crtc_id,
 /* Planes stuff ends. */
 
 SDL_KMSDRM_MODULE(GBM)
-SDL_KMSDRM_SYM(int,gbm_device_get_fd,(struct gbm_device *gbm))
 SDL_KMSDRM_SYM(int,gbm_device_is_format_supported,(struct gbm_device *gbm,
                                                    uint32_t format, uint32_t usage))
 SDL_KMSDRM_SYM(void,gbm_device_destroy,(struct gbm_device *gbm))
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.c b/src/video/kmsdrm/SDL_kmsdrmvideo.c
index dbc1b445525..3308dd96334 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.c
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.c
@@ -555,6 +555,7 @@ KMSDRM_AddDisplay (_THIS, drmModeConnector *connector, drmModeRes *resources) {
     /* Initialize some of the members of the new display's driverdata
        to sane values. */
     dispdata->cursor_bo = NULL;
+    dispdata->cursor_bo_drm_fd = -1;
 
     /* Since we create and show the default cursor on KMSDRM_InitMouse(),
        and we call KMSDRM_InitMouse() when we create a window, we have to know
diff --git a/src/video/kmsdrm/SDL_kmsdrmvideo.h b/src/video/kmsdrm/SDL_kmsdrmvideo.h
index b172eb90f96..566fc852dee 100644
--- a/src/video/kmsdrm/SDL_kmsdrmvideo.h
+++ b/src/video/kmsdrm/SDL_kmsdrmvideo.h
@@ -77,6 +77,7 @@ typedef struct SDL_DisplayData
        where we may not have an SDL_Cursor at all (so no SDL_Cursor driverdata).
        There's only one cursor GBM BO because we only support one cursor. */
     struct gbm_bo *cursor_bo;
+    int cursor_bo_drm_fd;
     uint64_t cursor_w, cursor_h;
 
     SDL_bool default_cursor_init;