| 1 |
/* |
| 2 |
* Copyright (C) 2023-2024 Intel Corporation |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "opencl/source/sharings/gl/linux/gl_sharing_linux.h" |
| 9 |
|
| 10 |
#include "shared/source/os_interface/linux/sys_calls.h" |
| 11 |
|
| 12 |
#include "opencl/source/context/context.inl" |
| 13 |
#include "opencl/source/helpers/gl_helper.h" |
| 14 |
#include "opencl/source/sharings/gl/gl_arb_sync_event.h" |
| 15 |
|
| 16 |
namespace NEO { |
| 17 |
GLSharingFunctionsLinux::GLSharingFunctionsLinux(GLType glhdcType, GLContext glhglrcHandle, GLContext glhglrcHandleBkpCtx, GLDisplay glhdcHandle) |
| 18 |
: glHDCType(glhdcType), glHGLRCHandle(glhglrcHandle), glHGLRCHandleBkpCtx(glhglrcHandleBkpCtx), glHDCHandle(glhdcHandle) { |
| 19 |
GLSharingFunctionsLinux::initGLFunctions(); |
| 20 |
} |
| 21 |
GLSharingFunctionsLinux::~GLSharingFunctionsLinux() = default; |
| 22 |
|
| 23 |
bool GLSharingFunctionsLinux::isOpenGlExtensionSupported(const unsigned char *pExtensionString) { |
| 24 |
if (glGetStringi == nullptr || glGetIntegerv == nullptr) { |
| 25 |
return false; |
| 26 |
} |
| 27 |
|
| 28 |
cl_int numberOfExtensions = 0; |
| 29 |
glGetIntegerv(GL_NUM_EXTENSIONS, &numberOfExtensions); |
| 30 |
for (cl_int i = 0; i < numberOfExtensions; i++) { |
| 31 |
std::basic_string<unsigned char> pString = glGetStringi(GL_EXTENSIONS, i); |
| 32 |
if (pString == pExtensionString) { |
| 33 |
return true; |
| 34 |
} |
| 35 |
} |
| 36 |
return false; |
| 37 |
} |
| 38 |
|
| 39 |
bool GLSharingFunctionsLinux::isOpenGlSharingSupported() { |
| 40 |
|
| 41 |
std::basic_string<unsigned char> vendor = glGetString(GL_VENDOR); |
| 42 |
const unsigned char intelVendor[] = "Intel"; |
| 43 |
|
| 44 |
if ((vendor.empty()) || (vendor != intelVendor)) { |
| 45 |
return false; |
| 46 |
} |
| 47 |
std::basic_string<unsigned char> version = glGetString(GL_VERSION); |
| 48 |
if (version.empty()) { |
| 49 |
return false; |
| 50 |
} |
| 51 |
|
| 52 |
bool isOpenGLES = false; |
| 53 |
const unsigned char versionES[] = "OpenGL ES"; |
| 54 |
if (version.find(versionES) != std::string::npos) { |
| 55 |
isOpenGLES = true; |
| 56 |
} |
| 57 |
|
| 58 |
if (isOpenGLES == true) { |
| 59 |
const unsigned char versionES1[] = "OpenGL ES 1."; |
| 60 |
if (version.find(versionES1) != std::string::npos) { |
| 61 |
const unsigned char supportGLOES[] = "GL_OES_framebuffer_object"; |
| 62 |
if (isOpenGlExtensionSupported(supportGLOES) == false) { |
| 63 |
return false; |
| 64 |
} |
| 65 |
} |
| 66 |
} else { |
| 67 |
if (version[0] < '3') { |
| 68 |
const unsigned char supportGLEXT[] = "GL_EXT_framebuffer_object"; |
| 69 |
if (isOpenGlExtensionSupported(supportGLEXT) == false) { |
| 70 |
return false; |
| 71 |
} |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
return true; |
| 76 |
} |
| 77 |
|
| 78 |
GlArbSyncEvent *GLSharingFunctionsLinux::getGlArbSyncEvent(Event &baseEvent) { |
| 79 |
std::lock_guard<std::mutex> lock{glArbEventMutex}; |
| 80 |
auto it = glArbEventMapping.find(&baseEvent); |
| 81 |
if (it != glArbEventMapping.end()) { |
| 82 |
return it->second; |
| 83 |
} |
| 84 |
return nullptr; |
| 85 |
} |
| 86 |
|
| 87 |
void GLSharingFunctionsLinux::removeGlArbSyncEventMapping(Event &baseEvent) { |
| 88 |
std::lock_guard<std::mutex> lock{glArbEventMutex}; |
| 89 |
auto it = glArbEventMapping.find(&baseEvent); |
| 90 |
if (it == glArbEventMapping.end()) { |
| 91 |
DEBUG_BREAK_IF(it == glArbEventMapping.end()); |
| 92 |
return; |
| 93 |
} |
| 94 |
glArbEventMapping.erase(it); |
| 95 |
} |
| 96 |
|
| 97 |
GLboolean GLSharingFunctionsLinux::initGLFunctions() { |
| 98 |
std::unique_ptr<OsLibrary> dynLibrary(OsLibrary::loadFunc("")); |
| 99 |
|
| 100 |
GlFunctionHelper glXGetProc(dynLibrary.get(), "glXGetProcAddress"); |
| 101 |
if (glXGetProc.ready()) { |
| 102 |
glXGLInteropQueryDeviceInfo = glXGetProc["glXGLInteropQueryDeviceInfoMESA"]; |
| 103 |
glXGLInteropExportObject = glXGetProc["glXGLInteropExportObjectMESA"]; |
| 104 |
glXGLInteropFlushObjects = glXGetProc["glXGLInteropFlushObjectsMESA"]; |
| 105 |
} |
| 106 |
|
| 107 |
GlFunctionHelper eglGetProc(dynLibrary.get(), "eglGetProcAddress"); |
| 108 |
if (eglGetProc.ready()) { |
| 109 |
eglGLInteropQueryDeviceInfo = eglGetProc["eglGLInteropQueryDeviceInfoMESA"]; |
| 110 |
eglGLInteropExportObject = eglGetProc["eglGLInteropExportObjectMESA"]; |
| 111 |
eglGLInteropFlushObjects = eglGetProc["eglGLInteropFlushObjectsMESA"]; |
| 112 |
} |
| 113 |
|
| 114 |
glGetString = (*dynLibrary)["glGetString"]; |
| 115 |
glGetStringi = (*dynLibrary)["glGetStringi"]; |
| 116 |
glGetIntegerv = (*dynLibrary)["glGetIntegerv"]; |
| 117 |
|
| 118 |
this->pfnGlArbSyncObjectCleanup = cleanupArbSyncObject; |
| 119 |
this->pfnGlArbSyncObjectSetup = setupArbSyncObject; |
| 120 |
this->pfnGlArbSyncObjectSignal = signalArbSyncObject; |
| 121 |
this->pfnGlArbSyncObjectWaitServer = serverWaitForArbSyncObject; |
| 122 |
|
| 123 |
return 1; |
| 124 |
} |
| 125 |
bool GLSharingFunctionsLinux::flushObjectsAndWait(unsigned count, struct mesa_glinterop_export_in *resources, struct mesa_glinterop_flush_out *out) { |
| 126 |
/* Call MESA interop */ |
| 127 |
int retValue = flushObjects(1, resources, out); |
| 128 |
if (retValue != MESA_GLINTEROP_SUCCESS) { |
| 129 |
return false; |
| 130 |
} |
| 131 |
auto fenceFd = *out->fence_fd; |
| 132 |
/* Wait on the fence fd */ |
| 133 |
struct pollfd fp = { |
| 134 |
.fd = fenceFd, |
| 135 |
.events = POLLIN, |
| 136 |
.revents = 0, |
| 137 |
}; |
| 138 |
retValue = SysCalls::poll(&fp, 1, 1000); |
| 139 |
SysCalls::close(fenceFd); |
| 140 |
return retValue >= 0; |
| 141 |
} |
| 142 |
|
| 143 |
template GLSharingFunctionsLinux *Context::getSharing<GLSharingFunctionsLinux>(); |
| 144 |
|
| 145 |
} // namespace NEO |