Refactor of glcl sharing

new pattern to load gl functions from dll

Change-Id: I6f39945d3c53b5a169b0829f36b2102c3ef5e18a
This commit is contained in:
Katarzyna Cencelewska
2018-09-25 09:19:30 +02:00
committed by sys_ocldev
parent 526a3a664b
commit 51ecef7ec2
6 changed files with 84 additions and 47 deletions

View File

@@ -7,6 +7,7 @@
#pragma once #pragma once
#include <string> #include <string>
#include <type_traits>
namespace OCLRT { namespace OCLRT {
@@ -15,10 +16,25 @@ class OsLibrary {
OsLibrary() = default; OsLibrary() = default;
public: public:
struct ConvertibleProcAddr {
template <typename T>
operator T *() const {
static_assert(std::is_function<T>::value, "Cannot convert to non-function and non-void* type");
return reinterpret_cast<T *>(ptr);
}
operator void *() const {
return ptr;
}
void *ptr = nullptr;
};
virtual ~OsLibrary() = default; virtual ~OsLibrary() = default;
static OsLibrary *load(const std::string &name); static OsLibrary *load(const std::string &name);
ConvertibleProcAddr operator[](const std::string &name) {
return ConvertibleProcAddr{getProcAddress(name)};
}
virtual void *getProcAddress(const std::string &procName) = 0; virtual void *getProcAddress(const std::string &procName) = 0;
virtual bool isLoaded() = 0; virtual bool isLoaded() = 0;
}; };

View File

@@ -31,30 +31,28 @@ GLSharingFunctions::~GLSharingFunctions() {
} }
GLboolean GLSharingFunctions::initGLFunctions() { GLboolean GLSharingFunctions::initGLFunctions() {
GLGetCurrentContext = reinterpret_cast<PFNOGLGetCurrentContext>(loadGlFunction("wglGetCurrentContext", GLHDCType)); glLibrary.reset(OsLibrary::load(Os::openglDllName));
GLGetCurrentDisplay = reinterpret_cast<PFNOGLGetCurrentDisplay>(loadGlFunction("wglGetCurrentDC", GLHDCType)); if (glLibrary->isLoaded()) {
glGetString = reinterpret_cast<PFNglGetString>(loadGlFunction("glGetString", GLHDCType)); GLGetCurrentContext = (*glLibrary)["wglGetCurrentContext"];
glGetIntegerv = reinterpret_cast<PFNglGetIntegerv>(loadGlFunction("glGetIntegerv", GLHDCType)); GLGetCurrentDisplay = (*glLibrary)["wglGetCurrentDC"];
glGetString = (*glLibrary)["glGetString"];
pfnWglCreateContext = reinterpret_cast<PFNwglCreateContext>(loadGlFunction("wglCreateContext", GLHDCType)); glGetIntegerv = (*glLibrary)["glGetIntegerv"];
pfnWglDeleteContext = reinterpret_cast<PFNwglDeleteContext>(loadGlFunction("wglDeleteContext", GLHDCType)); pfnWglCreateContext = (*glLibrary)["wglCreateContext"];
pfnWglDeleteContext = (*glLibrary)["wglDeleteContext"];
pfnWglShareLists = reinterpret_cast<PFNwglShareLists>(loadGlFunction("wglShareLists", GLHDCType)); pfnWglShareLists = (*glLibrary)["wglShareLists"];
GLSetSharedOCLContextState = (*glLibrary)["wglSetSharedOCLContextStateINTEL"];
auto wglGetProcAddressFuncPtr = reinterpret_cast<PROC(WINAPI *)(LPCSTR)>(loadGlFunction("wglGetProcAddress", GLHDCType)); GLAcquireSharedBuffer = (*glLibrary)["wglAcquireSharedBufferINTEL"];
GLSetSharedOCLContextState = reinterpret_cast<PFNOGLSetSharedOCLContextStateINTEL>(wglGetProcAddressFuncPtr("wglSetSharedOCLContextStateINTEL")); GLReleaseSharedBuffer = (*glLibrary)["wglReleaseSharedBufferINTEL"];
GLAcquireSharedBuffer = reinterpret_cast<PFNOGLAcquireSharedBufferINTEL>(wglGetProcAddressFuncPtr("wglAcquireSharedBufferINTEL")); GLAcquireSharedRenderBuffer = (*glLibrary)["wglAcquireSharedRenderBufferINTEL"];
GLReleaseSharedBuffer = reinterpret_cast<PFNOGLReleaseSharedBufferINTEL>(wglGetProcAddressFuncPtr("wglReleaseSharedBufferINTEL")); GLReleaseSharedRenderBuffer = (*glLibrary)["wglReleaseSharedRenderBufferINTEL"];
GLAcquireSharedRenderBuffer = reinterpret_cast<PFNOGLAcquireSharedRenderBufferINTEL>(wglGetProcAddressFuncPtr("wglAcquireSharedRenderBufferINTEL")); GLAcquireSharedTexture = (*glLibrary)["wglAcquireSharedTextureINTEL"];
GLReleaseSharedRenderBuffer = reinterpret_cast<PFNOGLReleaseSharedRenderBufferINTEL>(wglGetProcAddressFuncPtr("wglReleaseSharedRenderBufferINTEL")); GLReleaseSharedTexture = (*glLibrary)["wglReleaseSharedTextureINTEL"];
GLAcquireSharedTexture = reinterpret_cast<PFNOGLAcquireSharedTextureINTEL>(wglGetProcAddressFuncPtr("wglAcquireSharedTextureINTEL")); GLRetainSync = (*glLibrary)["wglRetainSyncINTEL"];
GLReleaseSharedTexture = reinterpret_cast<PFNOGLReleaseSharedTextureINTEL>(wglGetProcAddressFuncPtr("wglReleaseSharedTextureINTEL")); GLReleaseSync = (*glLibrary)["wglReleaseSyncINTEL"];
GLRetainSync = reinterpret_cast<PFNOGLRetainSyncINTEL>(wglGetProcAddressFuncPtr("wglRetainSyncINTEL")); GLGetSynciv = (*glLibrary)["wglGetSyncivINTEL"];
GLReleaseSync = reinterpret_cast<PFNOGLReleaseSyncINTEL>(wglGetProcAddressFuncPtr("wglReleaseSyncINTEL")); glGetStringi = (*glLibrary)["glGetStringi"];
GLGetSynciv = reinterpret_cast<PFNOGLGetSyncivINTEL>(wglGetProcAddressFuncPtr("wglGetSyncivINTEL")); this->wglMakeCurrent = (*glLibrary)["wglMakeCurrent"];
glGetStringi = reinterpret_cast<PFNglGetStringi>(wglGetProcAddressFuncPtr("glGetStringi")); }
this->wglMakeCurrent = reinterpret_cast<PFNwglMakeCurrent>(loadGlFunction("wglMakeCurrent", GLHDCType));
this->pfnGlArbSyncObjectCleanup = cleanupArbSyncObject; this->pfnGlArbSyncObjectCleanup = cleanupArbSyncObject;
this->pfnGlArbSyncObjectSetup = setupArbSyncObject; this->pfnGlArbSyncObjectSetup = setupArbSyncObject;
this->pfnGlArbSyncObjectSignal = signalArbSyncObject; this->pfnGlArbSyncObjectSignal = signalArbSyncObject;
@@ -119,12 +117,6 @@ bool GLSharingFunctions::isOpenGlSharingSupported() {
return true; return true;
} }
void *GLSharingFunctions::loadGlFunction(const char *functionName, uint32_t hdc) {
HMODULE module = LoadLibraryA(Os::openglDllName);
return reinterpret_cast<PFNglGetString>(GetProcAddress(module, functionName));
}
void GLSharingFunctions::createBackupContext() { void GLSharingFunctions::createBackupContext() {
if (pfnWglCreateContext) { if (pfnWglCreateContext) {
GLHGLRCHandleBkpCtx = pfnWglCreateContext(GLHDCHandle); GLHGLRCHandleBkpCtx = pfnWglCreateContext(GLHDCHandle);

View File

@@ -12,7 +12,7 @@
#include "GL/gl.h" #include "GL/gl.h"
#include "GL/glext.h" #include "GL/glext.h"
#include "runtime/sharings/sharing.h" #include "runtime/sharings/sharing.h"
#include "gl/gl_sharing_os.h" #include "runtime/os_interface/windows/gl/gl_sharing_os.h"
#include <functional> #include <functional>
#include <mutex> #include <mutex>
@@ -207,6 +207,7 @@ class GLSharingFunctions : public SharingFunctions {
} }
protected: protected:
std::unique_ptr<OsLibrary> glLibrary = nullptr;
GLType GLHDCType = 0; GLType GLHDCType = 0;
GLContext GLHGLRCHandle = 0; GLContext GLHGLRCHandle = 0;
GLContext GLHGLRCHandleBkpCtx = 0; GLContext GLHGLRCHandleBkpCtx = 0;
@@ -239,9 +240,6 @@ class GLSharingFunctions : public SharingFunctions {
PFNglArbSyncObjectSignal pfnGlArbSyncObjectSignal = nullptr; PFNglArbSyncObjectSignal pfnGlArbSyncObjectSignal = nullptr;
PFNglArbSyncObjectWaitServer pfnGlArbSyncObjectWaitServer = nullptr; PFNglArbSyncObjectWaitServer pfnGlArbSyncObjectWaitServer = nullptr;
// loading OGL libraries for OGL_OCL sharing
void *loadGlFunction(const char *functionName, uint32_t hdc);
// support for GL_ARB_cl_event // support for GL_ARB_cl_event
std::mutex glArbEventMutex; std::mutex glArbEventMutex;
std::unordered_map<Event *, GlArbSyncEvent *> glArbEventMapping; std::unordered_map<Event *, GlArbSyncEvent *> glArbEventMapping;

View File

@@ -292,12 +292,9 @@ class MockGLSharingFunctions : public GLSharingFunctions {
((ContextInfo *)pContextInfo)->DeviceHandle = 2; ((ContextInfo *)pContextInfo)->DeviceHandle = 2;
return GLSetSharedOCLContextStateReturnedValue; return GLSetSharedOCLContextStateReturnedValue;
} }
using GLSharingFunctions::glGetIntegerv;
using GLSharingFunctions::glGetString;
void *loadGlFunction(const char *FunctionName, DWORD HDC) { return GLSharingFunctions::loadGlFunction(FunctionName, HDC); };
void setGetStringFcn(PFNglGetString fcn) { glGetString = fcn; }
void setglGetIntegervToNull() { glGetIntegerv = nullptr; }
MockGLSharingFunctions() { MockGLSharingFunctions() {
glGetString = (PFNglGetString)glGetStringTest; glGetString = (PFNglGetString)glGetStringTest;
glGetStringi = (PFNglGetStringi)glGetStringiTest; glGetStringi = (PFNglGetStringi)glGetStringiTest;

View File

@@ -91,3 +91,39 @@ TEST_F(OSLibraryTest, testFailNew) {
}; };
injectFailures(method); injectFailures(method);
} }
TEST(OsLibrary, whenCallingIndexOperatorThenObjectConvertibleToFunctionOrVoidPointerIsReturned) {
struct MockOsLibrary : OsLibrary {
void *getProcAddress(const std::string &procName) override {
lastRequestedProcName = procName;
return ptrToReturn;
}
bool isLoaded() override { return true; }
void *ptrToReturn = nullptr;
std::string lastRequestedProcName;
};
MockOsLibrary lib;
int varA, varB, varC;
int *addrA = &varA, *addrB = &varB, *addrC = &varC;
using FunctionTypeA = void (*)(int *, float);
using FunctionTypeB = int (*)();
lib.ptrToReturn = addrA;
FunctionTypeA functionA = lib["funcA"];
EXPECT_STREQ("funcA", lib.lastRequestedProcName.c_str());
EXPECT_EQ(reinterpret_cast<void *>(addrA), reinterpret_cast<void *>(functionA));
lib.ptrToReturn = addrB;
FunctionTypeB functionB = lib["funcB"];
EXPECT_STREQ("funcB", lib.lastRequestedProcName.c_str());
EXPECT_EQ(reinterpret_cast<void *>(addrB), reinterpret_cast<void *>(functionB));
lib.ptrToReturn = addrC;
void *rawPtr = lib["funcC"];
EXPECT_STREQ("funcC", lib.lastRequestedProcName.c_str());
EXPECT_EQ(reinterpret_cast<void *>(addrC), rawPtr);
}

View File

@@ -32,6 +32,10 @@
#include "test.h" #include "test.h"
#include "gl/gl_sharing_os.h" #include "gl/gl_sharing_os.h"
namespace Os {
extern const char *openglDllName;
}
using namespace OCLRT; using namespace OCLRT;
bool MockGLSharingFunctions::SharingEnabled = false; bool MockGLSharingFunctions::SharingEnabled = false;
const char *MockGLSharingFunctions::arrayStringi[2] = {"GL_OES_framebuffer_object", "GL_EXT_framebuffer_object"}; const char *MockGLSharingFunctions::arrayStringi[2] = {"GL_OES_framebuffer_object", "GL_EXT_framebuffer_object"};
@@ -650,12 +654,6 @@ TEST(glSharingBasicTest, GivenSharingFunctionsWhenItIsConstructedThenOglContextF
EXPECT_EQ(1, GLSetSharedOCLContextStateCalled); EXPECT_EQ(1, GLSetSharedOCLContextStateCalled);
} }
TEST(glSharingBasicTest, givenInvalidFunctionNameWhenLoadGLFunctionThenReturnNullptr) {
MockGLSharingFunctions glSharingFunctions;
auto fPointer = glSharingFunctions.loadGlFunction("BadFunctionName", 0);
EXPECT_EQ(nullptr, fPointer);
}
TEST(glSharingBasicTest, givenInvalidExtensionNameWhenCheckGLExtensionSupportedThenReturnFalse) { TEST(glSharingBasicTest, givenInvalidExtensionNameWhenCheckGLExtensionSupportedThenReturnFalse) {
GLSharingFunctions glSharingFunctions; GLSharingFunctions glSharingFunctions;
bool RetVal = glSharingFunctions.isOpenGlExtensionSupported("InvalidExtensionName"); bool RetVal = glSharingFunctions.isOpenGlExtensionSupported("InvalidExtensionName");
@@ -664,7 +662,7 @@ TEST(glSharingBasicTest, givenInvalidExtensionNameWhenCheckGLExtensionSupportedT
TEST(glSharingBasicTest, givenglGetIntegervIsNullWhenCheckGLExtensionSupportedThenReturnFalse) { TEST(glSharingBasicTest, givenglGetIntegervIsNullWhenCheckGLExtensionSupportedThenReturnFalse) {
MockGLSharingFunctions glSharingFunctions; MockGLSharingFunctions glSharingFunctions;
glSharingFunctions.setglGetIntegervToNull(); glSharingFunctions.glGetIntegerv = nullptr;
bool RetVal = glSharingFunctions.isOpenGlExtensionSupported("InvalidExtensionName"); bool RetVal = glSharingFunctions.isOpenGlExtensionSupported("InvalidExtensionName");
EXPECT_FALSE(RetVal); EXPECT_FALSE(RetVal);
} }
@@ -687,7 +685,7 @@ TEST(glSharingBasicTest, givenVendorisNullWhenCheckGLSharingSupportedThenReturnF
}; };
MockGLSharingFunctions glSharingFunctions; MockGLSharingFunctions glSharingFunctions;
glSharingFunctions.setGetStringFcn(invalidGetStringFcn); glSharingFunctions.glGetString = invalidGetStringFcn;
bool RetVal = glSharingFunctions.isOpenGlSharingSupported(); bool RetVal = glSharingFunctions.isOpenGlSharingSupported();
EXPECT_FALSE(RetVal); EXPECT_FALSE(RetVal);