kmsdrm: Keep fd around if we can drop master

Modern kernels (v5.8+) allow non-root usage of drmDropMaster(), so
we can hold on to our fd after dropping master on it. This fixes
populating drm_fd in the KMSDRM SysWMinfo when using Vulkan.

Also add a missing error check for open() while we're here.
This commit is contained in:
Cameron Gutman 2024-09-21 14:20:08 -05:00 committed by Sam Lantinga
parent a4a8a29075
commit dab4f856c1
1 changed files with 21 additions and 10 deletions

View File

@ -533,7 +533,7 @@ static drmModeModeInfo *KMSDRM_GetClosestDisplayMode(SDL_VideoDisplay *display,
/* Deinitializes the driverdata of the SDL Displays in the SDL display list. */
static void KMSDRM_DeinitDisplays(_THIS)
{
SDL_VideoData *viddata = ((SDL_VideoData *)_this->driverdata);
SDL_DisplayData *dispdata;
int num_displays, i;
@ -557,6 +557,11 @@ static void KMSDRM_DeinitDisplays(_THIS)
dispdata->crtc = NULL;
}
}
if (viddata->drm_fd >= 0) {
close(viddata->drm_fd);
viddata->drm_fd = -1;
}
}
static uint32_t KMSDRM_CrtcGetPropId(uint32_t drm_fd,
@ -912,8 +917,6 @@ cleanup:
/* Initializes the list of SDL displays: we build a new display for each
connecter connector we find.
Inoffeensive for VK compatibility, except we must leave the drm_fd
closed when we get to the end of this function.
This is to be called early, in VideoInit(), because it gets us
the videomode information, which SDL needs immediately after VideoInit(). */
static int KMSDRM_InitDisplays(_THIS)
@ -986,10 +989,13 @@ static int KMSDRM_InitDisplays(_THIS)
/* Block for Vulkan compatibility. */
/***********************************/
/* THIS IS FOR VULKAN! Leave the FD closed, so VK can work.
Will reopen this in CreateWindow, but only if requested a non-VK window. */
/* Vulkan requires DRM master on its own FD to work, so try to drop master
on our FD. This will only work without root on kernels v5.8 and later.
If it doesn't work, just close the FD and we'll reopen it later. */
if (KMSDRM_drmDropMaster(viddata->drm_fd) < 0) {
close(viddata->drm_fd);
viddata->drm_fd = -1;
}
cleanup:
if (resources) {
@ -1017,10 +1023,15 @@ static int KMSDRM_GBMInit(_THIS, SDL_DisplayData *dispdata)
SDL_VideoData *viddata = (SDL_VideoData *)_this->driverdata;
int ret = 0;
/* Reopen the FD! */
/* Reopen the FD if we weren't able to drop master on the original one */
if (viddata->drm_fd < 0) {
viddata->drm_fd = open(viddata->devpath, O_RDWR | O_CLOEXEC);
if (viddata->drm_fd < 0) {
return SDL_SetError("Could not reopen %s", viddata->devpath);
}
}
/* Set the FD we just opened as current DRM master. */
/* Set the FD as current DRM master. */
KMSDRM_drmSetMaster(viddata->drm_fd);
/* Create the GBM device. */