From 1e31f6fe773083605bad4c42494ac8654c94d0db Mon Sep 17 00:00:00 2001 From: Cameron Gutman Date: Sun, 31 Jan 2021 15:03:40 -0600 Subject: [PATCH] Fix initialization of DRM backend with X running --- app/streaming/video/ffmpeg-renderers/drm.cpp | 63 +++++++++----------- app/streaming/video/ffmpeg-renderers/drm.h | 1 + 2 files changed, 30 insertions(+), 34 deletions(-) diff --git a/app/streaming/video/ffmpeg-renderers/drm.cpp b/app/streaming/video/ffmpeg-renderers/drm.cpp index d6799458..3fd0563d 100644 --- a/app/streaming/video/ffmpeg-renderers/drm.cpp +++ b/app/streaming/video/ffmpeg-renderers/drm.cpp @@ -20,13 +20,10 @@ extern "C" { #include -#ifndef SDL_VIDEO_DRIVER_KMSDRM -#error DRM renderer requires SDL built with --enable-video-kmsdrm -#endif - DrmRenderer::DrmRenderer() : m_HwContext(nullptr), m_DrmFd(-1), + m_SdlOwnsDrmFd(false), m_CrtcId(0), m_PlaneId(0), m_CurrentFbId(0) @@ -43,12 +40,9 @@ DrmRenderer::~DrmRenderer() av_buffer_unref(&m_HwContext); } -#if !SDL_VERSION_ATLEAST(2, 0, 15) - // This is owned by us on SDL 2.0.14 and earlier - if (m_DrmFd != -1) { + if (!m_SdlOwnsDrmFd && m_DrmFd != -1) { close(m_DrmFd); } -#endif } bool DrmRenderer::prepareDecoderContext(AVCodecContext* context, AVDictionary**) @@ -77,34 +71,35 @@ bool DrmRenderer::initialize(PDECODER_PARAMETERS params) return false; } - if (info.subsystem != SDL_SYSWM_KMSDRM) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, - "Unexpected subsystem in DRM renderer: %d", - info.subsystem); - return false; - } - - SDL_assert(info.info.kmsdrm.drm_fd >= 0); - m_DrmFd = info.info.kmsdrm.drm_fd; -#else - const char* device = SDL_getenv("DRM_DEV"); - - if (device == nullptr) { - device = "/dev/dri/card0"; - } - - SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, - "Opening DRM device: %s", - device); - - m_DrmFd = open(device, O_RDWR | O_CLOEXEC); - if (m_DrmFd < 0) { - SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, - "Failed to open DRM device: %d", - errno); - return false; + if (info.subsystem == SDL_SYSWM_KMSDRM) { + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, + "Sharing DRM FD with SDL"); + + SDL_assert(info.info.kmsdrm.drm_fd >= 0); + m_DrmFd = info.info.kmsdrm.drm_fd; + m_SdlOwnsDrmFd = true; } + else #endif + { + const char* device = SDL_getenv("DRM_DEV"); + + if (device == nullptr) { + device = "/dev/dri/card0"; + } + + SDL_LogInfo(SDL_LOG_CATEGORY_APPLICATION, + "Opening DRM device: %s", + device); + + m_DrmFd = open(device, O_RDWR | O_CLOEXEC); + if (m_DrmFd < 0) { + SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, + "Failed to open DRM device: %d", + errno); + return false; + } + } drmModeRes* resources = drmModeGetResources(m_DrmFd); if (resources == nullptr) { diff --git a/app/streaming/video/ffmpeg-renderers/drm.h b/app/streaming/video/ffmpeg-renderers/drm.h index 1ef5a328..17fd86a6 100644 --- a/app/streaming/video/ffmpeg-renderers/drm.h +++ b/app/streaming/video/ffmpeg-renderers/drm.h @@ -24,6 +24,7 @@ public: private: AVBufferRef* m_HwContext; int m_DrmFd; + bool m_SdlOwnsDrmFd; uint32_t m_CrtcId; uint32_t m_PlaneId; uint32_t m_CurrentFbId;