From 6b6123019f8f11d852ffe1de9602c99fadd6246c Mon Sep 17 00:00:00 2001 From: Sylvain Munaut Date: Fri, 7 Feb 2025 09:59:07 +0100 Subject: [PATCH] fix: In Linux CL/GL sharing, handle failed symbols lookup In some cases we endup with GL being found but neither GLX or EGL found and this currently leads to crash. So to better handle this case (and others), we keep track if we found symbols or not. If we're asked if CL/GL sharing is supported we check the load worked. If it's during an actual context creation we check what we were given match what we could load and fail context property finalization if not. If it's during an "info" request where we don't have any properties yet, we return yes if either GLX or EGL was found. Signed-off-by: Sylvain Munaut --- .../sharings/gl/linux/gl_sharing_linux.cpp | 25 ++++++++++++++++++- .../sharings/gl/linux/gl_sharing_linux.h | 6 ++++- .../sharings/gl/linux/lin_enable_gl.cpp | 13 +++++++--- 3 files changed, 39 insertions(+), 5 deletions(-) diff --git a/opencl/source/sharings/gl/linux/gl_sharing_linux.cpp b/opencl/source/sharings/gl/linux/gl_sharing_linux.cpp index f3cd5a1452e28..4345c8d1cf7e2 100644 --- a/opencl/source/sharings/gl/linux/gl_sharing_linux.cpp +++ b/opencl/source/sharings/gl/linux/gl_sharing_linux.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2024 Intel Corporation + * Copyright (C) 2023-2025 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -37,6 +37,9 @@ bool GLSharingFunctionsLinux::isOpenGlExtensionSupported(const unsigned char *pE } bool GLSharingFunctionsLinux::isOpenGlSharingSupported() { + if (glGetString == nullptr) { + return false; + } std::basic_string vendor = glGetString(GL_VENDOR); const unsigned char intelVendor[] = "Intel"; @@ -72,6 +75,20 @@ bool GLSharingFunctionsLinux::isOpenGlSharingSupported() { } } + switch (glHDCType) { + case CL_GLX_DISPLAY_KHR: + if (!glXLoaded) + return false; + break; + case CL_EGL_DISPLAY_KHR: + if (!eglLoaded) + return false; + break; + default: + if (!glXLoaded && !eglLoaded) + return false; + } + return true; } @@ -102,6 +119,9 @@ GLboolean GLSharingFunctionsLinux::initGLFunctions() { glXGLInteropQueryDeviceInfo = glXGetProc["glXGLInteropQueryDeviceInfoMESA"]; glXGLInteropExportObject = glXGetProc["glXGLInteropExportObjectMESA"]; glXGLInteropFlushObjects = glXGetProc["glXGLInteropFlushObjectsMESA"]; + glXLoaded = ((glXGLInteropQueryDeviceInfo != nullptr) && + (glXGLInteropExportObject != nullptr) && + (glXGLInteropFlushObjects != nullptr)); } GlFunctionHelper eglGetProc(dynLibrary.get(), "eglGetProcAddress"); @@ -109,6 +129,9 @@ GLboolean GLSharingFunctionsLinux::initGLFunctions() { eglGLInteropQueryDeviceInfo = eglGetProc["eglGLInteropQueryDeviceInfoMESA"]; eglGLInteropExportObject = eglGetProc["eglGLInteropExportObjectMESA"]; eglGLInteropFlushObjects = eglGetProc["eglGLInteropFlushObjectsMESA"]; + eglLoaded = ((eglGLInteropQueryDeviceInfo != nullptr) && + (eglGLInteropExportObject != nullptr) && + (eglGLInteropFlushObjects != nullptr)); } glGetString = (*dynLibrary)["glGetString"]; diff --git a/opencl/source/sharings/gl/linux/gl_sharing_linux.h b/opencl/source/sharings/gl/linux/gl_sharing_linux.h index 778f147831e48..e88218558c49c 100644 --- a/opencl/source/sharings/gl/linux/gl_sharing_linux.h +++ b/opencl/source/sharings/gl/linux/gl_sharing_linux.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2024 Intel Corporation + * Copyright (C) 2023-2025 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -127,6 +127,10 @@ class GLSharingFunctionsLinux : public GLSharingFunctions { GLContext glHGLRCHandleBkpCtx = 0; GLDisplay glHDCHandle = 0; + // Readiness + bool glXLoaded = false; + bool eglLoaded = false; + // GL functions PFNglGetString glGetString = nullptr; PFNglGetStringi glGetStringi = nullptr; diff --git a/opencl/source/sharings/gl/linux/lin_enable_gl.cpp b/opencl/source/sharings/gl/linux/lin_enable_gl.cpp index 7f9d13be5ff76..5c72316f50f4f 100644 --- a/opencl/source/sharings/gl/linux/lin_enable_gl.cpp +++ b/opencl/source/sharings/gl/linux/lin_enable_gl.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2023-2024 Intel Corporation + * Copyright (C) 2023-2025 Intel Corporation * * SPDX-License-Identifier: MIT * @@ -54,8 +54,15 @@ bool GlSharingContextBuilder::finalizeProperties(Context &context, int32_t &errc return true; if (contextData->glHGLRCHandle) { - context.registerSharing(new GLSharingFunctionsLinux(contextData->glHDCType, contextData->glHGLRCHandle, - nullptr, contextData->glHDCHandle)); + GLSharingFunctionsLinux *sharing_fn = new GLSharingFunctionsLinux(contextData->glHDCType, + contextData->glHGLRCHandle, + nullptr, contextData->glHDCHandle); + if (!sharing_fn->isOpenGlSharingSupported()) { + delete sharing_fn; + errcodeRet = CL_INVALID_PROPERTY; + return false; + } + context.registerSharing(sharing_fn); } contextData.reset(nullptr);