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
#include <string>
#include <type_traits>
namespace OCLRT {
@ -15,10 +16,25 @@ class OsLibrary {
OsLibrary() = default;
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;
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 bool isLoaded() = 0;
};

View File

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

View File

@ -12,7 +12,7 @@
#include "GL/gl.h"
#include "GL/glext.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 <mutex>
@ -207,6 +207,7 @@ class GLSharingFunctions : public SharingFunctions {
}
protected:
std::unique_ptr<OsLibrary> glLibrary = nullptr;
GLType GLHDCType = 0;
GLContext GLHGLRCHandle = 0;
GLContext GLHGLRCHandleBkpCtx = 0;
@ -239,9 +240,6 @@ class GLSharingFunctions : public SharingFunctions {
PFNglArbSyncObjectSignal pfnGlArbSyncObjectSignal = 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
std::mutex glArbEventMutex;
std::unordered_map<Event *, GlArbSyncEvent *> glArbEventMapping;

View File

@ -292,12 +292,9 @@ class MockGLSharingFunctions : public GLSharingFunctions {
((ContextInfo *)pContextInfo)->DeviceHandle = 2;
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() {
glGetString = (PFNglGetString)glGetStringTest;
glGetStringi = (PFNglGetStringi)glGetStringiTest;

View File

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