Unify handling of DRM devices between DRM and VAAPI

SDL may not be able to give us a DRM FD for Vulkan windows.
This commit is contained in:
Cameron Gutman
2024-09-21 20:46:11 -05:00
parent 9cf305865b
commit 6f39d120cb
5 changed files with 147 additions and 75 deletions

View File

@@ -123,16 +123,33 @@ VAAPIRenderer::openDisplay(SDL_Window* window)
}
#if defined(SDL_VIDEO_DRIVER_KMSDRM) && defined(HAVE_LIBVA_DRM) && SDL_VERSION_ATLEAST(2, 0, 15)
else if (info.subsystem == SDL_SYSWM_KMSDRM) {
SDL_assert(info.info.kmsdrm.drm_fd >= 0);
// It's possible to enter this function several times as we're probing VA drivers.
// Make sure to only duplicate the DRM FD the first time through.
if (m_DrmFd < 0) {
// Try to get the FD that we're sharing with SDL
bool mustCloseFd = false;
int fd = StreamUtils::getDrmFdForWindow(window, &mustCloseFd);
if (fd < 0) {
// Try to open any DRM render node
fd = StreamUtils::getDrmFd(true);
if (fd < 0) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Failed to open DRM render node: %d",
errno);
return nullptr;
}
}
// If the KMSDRM FD is not a render node FD, open the render node for libva to use.
// Since libva 2.20, using a primary node will fail in vaGetDriverNames().
if (drmGetNodeTypeFromFd(info.info.kmsdrm.drm_fd) != DRM_NODE_RENDER) {
char* renderNodePath = drmGetRenderDeviceNameFromFd(info.info.kmsdrm.drm_fd);
if (drmGetNodeTypeFromFd(fd) != DRM_NODE_RENDER) {
char* renderNodePath = drmGetRenderDeviceNameFromFd(fd);
if (renderNodePath) {
// Don't need the primary node FD anymore
if (mustCloseFd) {
close(fd);
}
SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION,
"Opening render node for VAAPI: %s",
renderNodePath);
@@ -148,13 +165,13 @@ VAAPIRenderer::openDisplay(SDL_Window* window)
else {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"Failed to get render node path. Using the SDL FD directly.");
m_DrmFd = dup(info.info.kmsdrm.drm_fd);
m_DrmFd = mustCloseFd ? fd : dup(fd);
}
}
else {
SDL_LogWarn(SDL_LOG_CATEGORY_APPLICATION,
"KMSDRM FD is already a render node. Using the SDL FD directly.");
m_DrmFd = dup(info.info.kmsdrm.drm_fd);
m_DrmFd = mustCloseFd ? fd : dup(fd);
}
}