Skip to content

Commit 51ecef7

Browse files
Refactor of glcl sharing
new pattern to load gl functions from dll Change-Id: I6f39945d3c53b5a169b0829f36b2102c3ef5e18a
1 parent 526a3a6 commit 51ecef7

File tree

6 files changed

+84
-47
lines changed

6 files changed

+84
-47
lines changed

runtime/os_interface/os_library.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#pragma once
99
#include <string>
10+
#include <type_traits>
1011

1112
namespace OCLRT {
1213

@@ -15,10 +16,25 @@ class OsLibrary {
1516
OsLibrary() = default;
1617

1718
public:
19+
struct ConvertibleProcAddr {
20+
template <typename T>
21+
operator T *() const {
22+
static_assert(std::is_function<T>::value, "Cannot convert to non-function and non-void* type");
23+
return reinterpret_cast<T *>(ptr);
24+
}
25+
26+
operator void *() const {
27+
return ptr;
28+
}
29+
void *ptr = nullptr;
30+
};
1831
virtual ~OsLibrary() = default;
1932

2033
static OsLibrary *load(const std::string &name);
2134

35+
ConvertibleProcAddr operator[](const std::string &name) {
36+
return ConvertibleProcAddr{getProcAddress(name)};
37+
}
2238
virtual void *getProcAddress(const std::string &procName) = 0;
2339
virtual bool isLoaded() = 0;
2440
};

runtime/os_interface/windows/gl/gl_sharing_win.cpp

Lines changed: 22 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -31,30 +31,28 @@ GLSharingFunctions::~GLSharingFunctions() {
3131
}
3232

3333
GLboolean GLSharingFunctions::initGLFunctions() {
34-
GLGetCurrentContext = reinterpret_cast<PFNOGLGetCurrentContext>(loadGlFunction("wglGetCurrentContext", GLHDCType));
35-
GLGetCurrentDisplay = reinterpret_cast<PFNOGLGetCurrentDisplay>(loadGlFunction("wglGetCurrentDC", GLHDCType));
36-
glGetString = reinterpret_cast<PFNglGetString>(loadGlFunction("glGetString", GLHDCType));
37-
glGetIntegerv = reinterpret_cast<PFNglGetIntegerv>(loadGlFunction("glGetIntegerv", GLHDCType));
38-
39-
pfnWglCreateContext = reinterpret_cast<PFNwglCreateContext>(loadGlFunction("wglCreateContext", GLHDCType));
40-
pfnWglDeleteContext = reinterpret_cast<PFNwglDeleteContext>(loadGlFunction("wglDeleteContext", GLHDCType));
41-
42-
pfnWglShareLists = reinterpret_cast<PFNwglShareLists>(loadGlFunction("wglShareLists", GLHDCType));
43-
44-
auto wglGetProcAddressFuncPtr = reinterpret_cast<PROC(WINAPI *)(LPCSTR)>(loadGlFunction("wglGetProcAddress", GLHDCType));
45-
GLSetSharedOCLContextState = reinterpret_cast<PFNOGLSetSharedOCLContextStateINTEL>(wglGetProcAddressFuncPtr("wglSetSharedOCLContextStateINTEL"));
46-
GLAcquireSharedBuffer = reinterpret_cast<PFNOGLAcquireSharedBufferINTEL>(wglGetProcAddressFuncPtr("wglAcquireSharedBufferINTEL"));
47-
GLReleaseSharedBuffer = reinterpret_cast<PFNOGLReleaseSharedBufferINTEL>(wglGetProcAddressFuncPtr("wglReleaseSharedBufferINTEL"));
48-
GLAcquireSharedRenderBuffer = reinterpret_cast<PFNOGLAcquireSharedRenderBufferINTEL>(wglGetProcAddressFuncPtr("wglAcquireSharedRenderBufferINTEL"));
49-
GLReleaseSharedRenderBuffer = reinterpret_cast<PFNOGLReleaseSharedRenderBufferINTEL>(wglGetProcAddressFuncPtr("wglReleaseSharedRenderBufferINTEL"));
50-
GLAcquireSharedTexture = reinterpret_cast<PFNOGLAcquireSharedTextureINTEL>(wglGetProcAddressFuncPtr("wglAcquireSharedTextureINTEL"));
51-
GLReleaseSharedTexture = reinterpret_cast<PFNOGLReleaseSharedTextureINTEL>(wglGetProcAddressFuncPtr("wglReleaseSharedTextureINTEL"));
52-
GLRetainSync = reinterpret_cast<PFNOGLRetainSyncINTEL>(wglGetProcAddressFuncPtr("wglRetainSyncINTEL"));
53-
GLReleaseSync = reinterpret_cast<PFNOGLReleaseSyncINTEL>(wglGetProcAddressFuncPtr("wglReleaseSyncINTEL"));
54-
GLGetSynciv = reinterpret_cast<PFNOGLGetSyncivINTEL>(wglGetProcAddressFuncPtr("wglGetSyncivINTEL"));
55-
glGetStringi = reinterpret_cast<PFNglGetStringi>(wglGetProcAddressFuncPtr("glGetStringi"));
56-
this->wglMakeCurrent = reinterpret_cast<PFNwglMakeCurrent>(loadGlFunction("wglMakeCurrent", GLHDCType));
57-
34+
glLibrary.reset(OsLibrary::load(Os::openglDllName));
35+
if (glLibrary->isLoaded()) {
36+
GLGetCurrentContext = (*glLibrary)["wglGetCurrentContext"];
37+
GLGetCurrentDisplay = (*glLibrary)["wglGetCurrentDC"];
38+
glGetString = (*glLibrary)["glGetString"];
39+
glGetIntegerv = (*glLibrary)["glGetIntegerv"];
40+
pfnWglCreateContext = (*glLibrary)["wglCreateContext"];
41+
pfnWglDeleteContext = (*glLibrary)["wglDeleteContext"];
42+
pfnWglShareLists = (*glLibrary)["wglShareLists"];
43+
GLSetSharedOCLContextState = (*glLibrary)["wglSetSharedOCLContextStateINTEL"];
44+
GLAcquireSharedBuffer = (*glLibrary)["wglAcquireSharedBufferINTEL"];
45+
GLReleaseSharedBuffer = (*glLibrary)["wglReleaseSharedBufferINTEL"];
46+
GLAcquireSharedRenderBuffer = (*glLibrary)["wglAcquireSharedRenderBufferINTEL"];
47+
GLReleaseSharedRenderBuffer = (*glLibrary)["wglReleaseSharedRenderBufferINTEL"];
48+
GLAcquireSharedTexture = (*glLibrary)["wglAcquireSharedTextureINTEL"];
49+
GLReleaseSharedTexture = (*glLibrary)["wglReleaseSharedTextureINTEL"];
50+
GLRetainSync = (*glLibrary)["wglRetainSyncINTEL"];
51+
GLReleaseSync = (*glLibrary)["wglReleaseSyncINTEL"];
52+
GLGetSynciv = (*glLibrary)["wglGetSyncivINTEL"];
53+
glGetStringi = (*glLibrary)["glGetStringi"];
54+
this->wglMakeCurrent = (*glLibrary)["wglMakeCurrent"];
55+
}
5856
this->pfnGlArbSyncObjectCleanup = cleanupArbSyncObject;
5957
this->pfnGlArbSyncObjectSetup = setupArbSyncObject;
6058
this->pfnGlArbSyncObjectSignal = signalArbSyncObject;
@@ -119,12 +117,6 @@ bool GLSharingFunctions::isOpenGlSharingSupported() {
119117
return true;
120118
}
121119

122-
void *GLSharingFunctions::loadGlFunction(const char *functionName, uint32_t hdc) {
123-
124-
HMODULE module = LoadLibraryA(Os::openglDllName);
125-
return reinterpret_cast<PFNglGetString>(GetProcAddress(module, functionName));
126-
}
127-
128120
void GLSharingFunctions::createBackupContext() {
129121
if (pfnWglCreateContext) {
130122
GLHGLRCHandleBkpCtx = pfnWglCreateContext(GLHDCHandle);

runtime/sharings/gl/gl_sharing.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
#include "GL/gl.h"
1313
#include "GL/glext.h"
1414
#include "runtime/sharings/sharing.h"
15-
#include "gl/gl_sharing_os.h"
15+
#include "runtime/os_interface/windows/gl/gl_sharing_os.h"
1616

1717
#include <functional>
1818
#include <mutex>
@@ -207,6 +207,7 @@ class GLSharingFunctions : public SharingFunctions {
207207
}
208208

209209
protected:
210+
std::unique_ptr<OsLibrary> glLibrary = nullptr;
210211
GLType GLHDCType = 0;
211212
GLContext GLHGLRCHandle = 0;
212213
GLContext GLHGLRCHandleBkpCtx = 0;
@@ -239,9 +240,6 @@ class GLSharingFunctions : public SharingFunctions {
239240
PFNglArbSyncObjectSignal pfnGlArbSyncObjectSignal = nullptr;
240241
PFNglArbSyncObjectWaitServer pfnGlArbSyncObjectWaitServer = nullptr;
241242

242-
// loading OGL libraries for OGL_OCL sharing
243-
void *loadGlFunction(const char *functionName, uint32_t hdc);
244-
245243
// support for GL_ARB_cl_event
246244
std::mutex glArbEventMutex;
247245
std::unordered_map<Event *, GlArbSyncEvent *> glArbEventMapping;

unit_tests/mocks/gl/mock_gl_sharing.h

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -292,12 +292,9 @@ class MockGLSharingFunctions : public GLSharingFunctions {
292292
((ContextInfo *)pContextInfo)->DeviceHandle = 2;
293293
return GLSetSharedOCLContextStateReturnedValue;
294294
}
295+
using GLSharingFunctions::glGetIntegerv;
296+
using GLSharingFunctions::glGetString;
295297

296-
void *loadGlFunction(const char *FunctionName, DWORD HDC) { return GLSharingFunctions::loadGlFunction(FunctionName, HDC); };
297-
298-
void setGetStringFcn(PFNglGetString fcn) { glGetString = fcn; }
299-
300-
void setglGetIntegervToNull() { glGetIntegerv = nullptr; }
301298
MockGLSharingFunctions() {
302299
glGetString = (PFNglGetString)glGetStringTest;
303300
glGetStringi = (PFNglGetStringi)glGetStringiTest;

unit_tests/os_interface/os_library_tests.cpp

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,39 @@ TEST_F(OSLibraryTest, testFailNew) {
9191
};
9292
injectFailures(method);
9393
}
94+
95+
TEST(OsLibrary, whenCallingIndexOperatorThenObjectConvertibleToFunctionOrVoidPointerIsReturned) {
96+
struct MockOsLibrary : OsLibrary {
97+
void *getProcAddress(const std::string &procName) override {
98+
lastRequestedProcName = procName;
99+
return ptrToReturn;
100+
}
101+
bool isLoaded() override { return true; }
102+
103+
void *ptrToReturn = nullptr;
104+
std::string lastRequestedProcName;
105+
};
106+
107+
MockOsLibrary lib;
108+
109+
int varA, varB, varC;
110+
int *addrA = &varA, *addrB = &varB, *addrC = &varC;
111+
112+
using FunctionTypeA = void (*)(int *, float);
113+
using FunctionTypeB = int (*)();
114+
115+
lib.ptrToReturn = addrA;
116+
FunctionTypeA functionA = lib["funcA"];
117+
EXPECT_STREQ("funcA", lib.lastRequestedProcName.c_str());
118+
EXPECT_EQ(reinterpret_cast<void *>(addrA), reinterpret_cast<void *>(functionA));
119+
120+
lib.ptrToReturn = addrB;
121+
FunctionTypeB functionB = lib["funcB"];
122+
EXPECT_STREQ("funcB", lib.lastRequestedProcName.c_str());
123+
EXPECT_EQ(reinterpret_cast<void *>(addrB), reinterpret_cast<void *>(functionB));
124+
125+
lib.ptrToReturn = addrC;
126+
void *rawPtr = lib["funcC"];
127+
EXPECT_STREQ("funcC", lib.lastRequestedProcName.c_str());
128+
EXPECT_EQ(reinterpret_cast<void *>(addrC), rawPtr);
129+
}

unit_tests/sharings/gl/gl_sharing_tests.cpp

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,10 @@
3232
#include "test.h"
3333
#include "gl/gl_sharing_os.h"
3434

35+
namespace Os {
36+
extern const char *openglDllName;
37+
}
38+
3539
using namespace OCLRT;
3640
bool MockGLSharingFunctions::SharingEnabled = false;
3741
const char *MockGLSharingFunctions::arrayStringi[2] = {"GL_OES_framebuffer_object", "GL_EXT_framebuffer_object"};
@@ -650,12 +654,6 @@ TEST(glSharingBasicTest, GivenSharingFunctionsWhenItIsConstructedThenOglContextF
650654
EXPECT_EQ(1, GLSetSharedOCLContextStateCalled);
651655
}
652656

653-
TEST(glSharingBasicTest, givenInvalidFunctionNameWhenLoadGLFunctionThenReturnNullptr) {
654-
MockGLSharingFunctions glSharingFunctions;
655-
auto fPointer = glSharingFunctions.loadGlFunction("BadFunctionName", 0);
656-
EXPECT_EQ(nullptr, fPointer);
657-
}
658-
659657
TEST(glSharingBasicTest, givenInvalidExtensionNameWhenCheckGLExtensionSupportedThenReturnFalse) {
660658
GLSharingFunctions glSharingFunctions;
661659
bool RetVal = glSharingFunctions.isOpenGlExtensionSupported("InvalidExtensionName");
@@ -664,7 +662,7 @@ TEST(glSharingBasicTest, givenInvalidExtensionNameWhenCheckGLExtensionSupportedT
664662

665663
TEST(glSharingBasicTest, givenglGetIntegervIsNullWhenCheckGLExtensionSupportedThenReturnFalse) {
666664
MockGLSharingFunctions glSharingFunctions;
667-
glSharingFunctions.setglGetIntegervToNull();
665+
glSharingFunctions.glGetIntegerv = nullptr;
668666
bool RetVal = glSharingFunctions.isOpenGlExtensionSupported("InvalidExtensionName");
669667
EXPECT_FALSE(RetVal);
670668
}
@@ -687,7 +685,7 @@ TEST(glSharingBasicTest, givenVendorisNullWhenCheckGLSharingSupportedThenReturnF
687685
};
688686

689687
MockGLSharingFunctions glSharingFunctions;
690-
glSharingFunctions.setGetStringFcn(invalidGetStringFcn);
688+
glSharingFunctions.glGetString = invalidGetStringFcn;
691689

692690
bool RetVal = glSharingFunctions.isOpenGlSharingSupported();
693691
EXPECT_FALSE(RetVal);

0 commit comments

Comments
 (0)