From 775669dbdd97a52dc32dab82c3d5efd160f9e807 Mon Sep 17 00:00:00 2001 From: "Chew, Tong Liang" Date: Mon, 9 Aug 2021 02:19:01 +0000 Subject: [PATCH] Fix the rendering to X11 failures The i915 render device node is not always in renderD128. Change the hard coded with autodetect the i915 device via ioctl Upstream-Status: Submitted https://github.com/oneapi-src/oneVPL/pull/21 Signed-off-by: Chew, Tong Liang Signed-off-by: Teng, Jin Chung --- tools/legacy/sample_common/src/vaapi_device.cpp | 24 +++++++++++++- tools/legacy/sample_common/src/vaapi_utils_x11.cpp | 38 +++++++++++++++++++--- 2 files changed, 57 insertions(+), 5 deletions(-) diff --git a/tools/legacy/sample_common/src/vaapi_device.cpp b/tools/legacy/sample_common/src/vaapi_device.cpp index b034376..f78c5aa 100644 --- a/tools/legacy/sample_common/src/vaapi_device.cpp +++ b/tools/legacy/sample_common/src/vaapi_device.cpp @@ -20,6 +20,7 @@ #include "vaapi_allocator.h" #if defined(X11_DRI3_SUPPORT) #include + #include #define ALIGN(x, y) (((x) + (y)-1) & -(y)) #define PAGE_ALIGN(x) ALIGN(x, 4096) @@ -27,6 +28,11 @@ #define VAAPI_GET_X_DISPLAY(_display) (Display*)(_display) #define VAAPI_GET_X_WINDOW(_window) (Window*)(_window) + const char* MFX_DEVICE_NODE_RENDER = "/dev/dri/renderD"; + const char* MFX_DEVICE_DRIVER_NAME = "i915"; + constexpr mfxU32 MFX_DEVICE_DRIVER_NAME_LEN = 4; + constexpr mfxU32 MFX_DEVICE_NODE_INDEX = 128; + constexpr mfxU32 MFX_DEVICE_MAX_NODES = 16; CVAAPIDeviceX11::~CVAAPIDeviceX11(void) { Close(); @@ -73,7 +79,23 @@ mfxStatus CVAAPIDeviceX11::Init(mfxHDL hWindow, mfxU16 nViews, mfxU32 nAdapterNu // it's enough to pass render node, because we only request // information from kernel via m_dri_fd - m_dri_fd = open("/dev/dri/renderD128", O_RDWR); + for (mfxU32 i = 0; i < MFX_DEVICE_MAX_NODES; ++i) { + std::string devPath = MFX_DEVICE_NODE_RENDER + std::to_string(MFX_DEVICE_NODE_INDEX + i); + m_dri_fd = open(devPath.c_str(), O_RDWR); + if (m_dri_fd < 0) continue; + + char driverName[MFX_DEVICE_DRIVER_NAME_LEN + 1] = {}; + drm_version_t version = {}; + version.name_len = MFX_DEVICE_DRIVER_NAME_LEN; + version.name = driverName; + + if(!ioctl(m_dri_fd, DRM_IOWR(0, drm_version), &version) && + !strcmp(driverName, MFX_DEVICE_DRIVER_NAME)) { + break; + } + close(m_dri_fd); + } + if (m_dri_fd < 0) { msdk_printf(MSDK_STRING("Failed to open dri device\n")); return MFX_ERR_NOT_INITIALIZED; diff --git a/tools/legacy/sample_common/src/vaapi_utils_x11.cpp b/tools/legacy/sample_common/src/vaapi_utils_x11.cpp index 9770a2f..734cd10 100644 --- a/tools/legacy/sample_common/src/vaapi_utils_x11.cpp +++ b/tools/legacy/sample_common/src/vaapi_utils_x11.cpp @@ -10,18 +10,24 @@ #include "sample_defs.h" #include - #if defined(X11_DRI3_SUPPORT) - #include - #endif + #include + #include #define VAAPI_X_DEFAULT_DISPLAY ":0.0" + const char* MFX_X11_NODE_RENDER = "/dev/dri/renderD"; + const char* MFX_X11_DRIVER_NAME = "i915"; + constexpr mfxU32 MFX_X11_DRIVER_NAME_LEN = 4; + constexpr mfxU32 MFX_X11_NODE_INDEX = 128; + constexpr mfxU32 MFX_X11_MAX_NODES = 16; + X11LibVA::X11LibVA(void) : CLibVA(MFX_LIBVA_X11), m_display(0), m_configID(VA_INVALID_ID), m_contextID(VA_INVALID_ID) { char* currentDisplay = getenv("DISPLAY"); + int fd = -1; m_display = (currentDisplay) ? m_x11lib.XOpenDisplay(currentDisplay) : m_x11lib.XOpenDisplay(VAAPI_X_DEFAULT_DISPLAY); @@ -32,7 +38,31 @@ X11LibVA::X11LibVA(void) throw std::bad_alloc(); } - m_va_dpy = m_vax11lib.vaGetDisplay(m_display); + for (mfxU32 i = 0; i < MFX_X11_MAX_NODES; ++i) { + std::string devPath = MFX_X11_NODE_RENDER + std::to_string(MFX_X11_NODE_INDEX + i); + fd = -1; + fd = open(devPath.c_str(), O_RDWR); + if (fd < 0) continue; + + char driverName[MFX_X11_DRIVER_NAME_LEN + 1] = {}; + drm_version_t version = {}; + version.name_len = MFX_X11_DRIVER_NAME_LEN; + version.name = driverName; + + if(!ioctl(fd, DRM_IOWR(0, drm_version), &version) && + !strcmp(driverName, MFX_X11_DRIVER_NAME)) { + break; + } + close(fd); + } + + if (fd < 0) { + m_x11lib.XCloseDisplay(m_display); + msdk_printf(MSDK_STRING("Failed to open adapter\n")); + throw std::bad_alloc(); + } + + m_va_dpy = m_vadrmlib.vaGetDisplayDRM(fd); if (!m_va_dpy) { m_x11lib.XCloseDisplay(m_display); msdk_printf(MSDK_STRING("Failed to get VA Display\n")); -- 2.7.4