Add a lock to synchronize DRM master ownership between Qt and SDL

This commit is contained in:
Cameron Gutman
2026-04-04 14:47:57 -05:00
parent 6ff3309677
commit 9496c25271
2 changed files with 41 additions and 0 deletions

View File

@@ -78,6 +78,9 @@ int g_QtCrtcConnectorCount;
bool removeSdlFd(int fd);
int takeMasterFromSdlFd(void);
void lockDrmMaster();
void unlockDrmMaster();
int drmIsMaster(int fd)
{
// Detect master by attempting something that requires master.
@@ -136,6 +139,9 @@ int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id, uint32_t flags, vo
// Call into the real thing
int err = fn_drmModePageFlip(fd, crtc_id, fb_id, flags, user_data);
if (!g_DisableDrmHooks && err == -EACCES && fd == g_QtDrmMasterFd) {
// Don't allow DRM master ownership to change
lockDrmMaster();
// If SDL took master from us, try to grab it back temporarily
int oldMasterFd = takeMasterFromSdlFd();
drmSetMaster(fd);
@@ -144,7 +150,10 @@ int drmModePageFlip(int fd, uint32_t crtc_id, uint32_t fb_id, uint32_t flags, vo
if (oldMasterFd != -1) {
drmSetMaster(oldMasterFd);
}
unlockDrmMaster();
}
return err;
}
@@ -172,6 +181,9 @@ int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req,
// Call into the real thing
int err = fn_drmModeAtomicCommit(fd, req, flags, user_data);
if (err == -EACCES && fd == g_QtDrmMasterFd) {
// Don't allow DRM master ownership to change
lockDrmMaster();
// If SDL took master from us, try to grab it back temporarily
int oldMasterFd = takeMasterFromSdlFd();
drmSetMaster(fd);
@@ -180,7 +192,10 @@ int drmModeAtomicCommit(int fd, drmModeAtomicReqPtr req,
if (oldMasterFd != -1) {
drmSetMaster(oldMasterFd);
}
unlockDrmMaster();
}
return err;
}
@@ -233,6 +248,8 @@ int close(int fd)
// If we closed the last SDL FD, restore master to the Qt FD
if (ret == 0 && lastSdlFd) {
lockDrmMaster();
if (drmSetMaster(g_QtDrmMasterFd) < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Failed to restore master to Qt DRM FD: %d",
@@ -257,6 +274,8 @@ int close(int fd)
errno);
}
}
unlockDrmMaster();
}
return ret;