Refactor of glcl sharing

new pattern to load gl and wgl functions from dll

Change-Id: I71d7510a210462c09d50e03ce8a444770c017b8d
This commit is contained in:
Katarzyna Cencelewska
2018-09-25 09:19:30 +02:00
committed by sys_ocldev
parent d1a80db2f2
commit f18532c8c4
13 changed files with 186 additions and 48 deletions

View File

@@ -87,6 +87,7 @@ set(RUNTIME_SRCS_HELPERS_WINDOWS
${CMAKE_CURRENT_SOURCE_DIR}/gmm_callbacks.inl
${CMAKE_CURRENT_SOURCE_DIR}/windows/kmd_notify_properties_windows.cpp
${CMAKE_CURRENT_SOURCE_DIR}/wddm_helper.h
${CMAKE_CURRENT_SOURCE_DIR}/windows/gl_helper.h
)
set(RUNTIME_SRCS_HELPERS_LINUX
${CMAKE_CURRENT_SOURCE_DIR}/linux/kmd_notify_properties_linux.cpp

View File

@@ -0,0 +1,31 @@
/*
* Copyright (C) 2018 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include "runtime/os_interface/os_library.h"
#include "GL/gl.h"
namespace Os {
extern const char *openglDllName;
}
namespace OCLRT {
class glFunctionHelper {
public:
glFunctionHelper::glFunctionHelper(OsLibrary *glLibrary, const std::string &functionName) {
glFunctionPtr = (*glLibrary)[functionName];
}
ConvertibleProcAddr operator[](const char *name) {
return ConvertibleProcAddr{glFunctionPtr(name)};
}
protected:
// clang-format off
PROC(__stdcall *glFunctionPtr)(LPCSTR Arg1) = nullptr;
// clang-format on
};
}; // namespace OCLRT

View File

@@ -7,9 +7,23 @@
#pragma once
#include <string>
#include <type_traits>
namespace OCLRT {
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;
};
class OsLibrary {
protected:
OsLibrary() = default;
@@ -19,6 +33,9 @@ class OsLibrary {
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

@@ -32,30 +32,31 @@ 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));
glLibrary.reset(OsLibrary::load(Os::openglDllName));
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));
if (glLibrary->isLoaded()) {
glFunctionHelper wglLibrary(glLibrary.get(), "wglGetProcAddress");
GLGetCurrentContext = (*glLibrary)["wglGetCurrentContext"];
GLGetCurrentDisplay = (*glLibrary)["wglGetCurrentDC"];
glGetString = (*glLibrary)["glGetString"];
glGetIntegerv = (*glLibrary)["glGetIntegerv"];
pfnWglCreateContext = (*glLibrary)["wglCreateContext"];
pfnWglDeleteContext = (*glLibrary)["wglDeleteContext"];
pfnWglShareLists = (*glLibrary)["wglShareLists"];
GLSetSharedOCLContextState = wglLibrary["wglSetSharedOCLContextStateINTEL"];
GLAcquireSharedBuffer = wglLibrary["wglAcquireSharedBufferINTEL"];
GLReleaseSharedBuffer = wglLibrary["wglReleaseSharedBufferINTEL"];
GLAcquireSharedRenderBuffer = wglLibrary["wglAcquireSharedRenderBufferINTEL"];
GLReleaseSharedRenderBuffer = wglLibrary["wglReleaseSharedRenderBufferINTEL"];
GLAcquireSharedTexture = wglLibrary["wglAcquireSharedTextureINTEL"];
GLReleaseSharedTexture = wglLibrary["wglReleaseSharedTextureINTEL"];
GLRetainSync = wglLibrary["wglRetainSyncINTEL"];
GLReleaseSync = wglLibrary["wglReleaseSyncINTEL"];
GLGetSynciv = wglLibrary["wglGetSyncivINTEL"];
glGetStringi = wglLibrary["glGetStringi"];
wglMakeCurrent = wglLibrary["wglMakeCurrent"];
}
this->pfnGlArbSyncObjectCleanup = cleanupArbSyncObject;
this->pfnGlArbSyncObjectSetup = setupArbSyncObject;
this->pfnGlArbSyncObjectSignal = signalArbSyncObject;
@@ -120,12 +121,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,8 @@
#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 "runtime/helpers/windows/gl_helper.h"
#include <functional>
#include <mutex>
@@ -208,6 +209,8 @@ class GLSharingFunctions : public SharingFunctions {
}
protected:
std::unique_ptr<OsLibrary> glLibrary;
GLType GLHDCType = 0;
GLContext GLHGLRCHandle = 0;
GLContext GLHGLRCHandleBkpCtx = 0;
@@ -240,9 +243,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

@@ -53,6 +53,8 @@ set(IGDRCL_SRCS_tests_helpers
set(IGDRCL_SRCS_tests_helpers_windows
${CMAKE_CURRENT_SOURCE_DIR}/windows/kmd_notify_windows_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/windows/gl_helper_tests.cpp
${CMAKE_CURRENT_SOURCE_DIR}/windows/mock_function.h
)
set(IGDRCL_SRCS_tests_helpers_linux

View File

@@ -0,0 +1,49 @@
/*
* Copyright (C) 2018 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
#include <memory>
#include "runtime/os_interface/os_library.h"
#include "runtime/os_interface/windows/windows_wrapper.h"
#include "runtime/helpers/windows/gl_helper.h"
#include "unit_tests/helpers/windows/mock_function.h"
#include "test.h"
#include "gtest/gtest.h"
typedef const char *(__cdecl *funcType)();
namespace OCLRT {
class glFunctionHelperMock : public glFunctionHelper {
public:
glFunctionHelperMock(OsLibrary *glLibrary, const std::string &functionName) : glFunctionHelper(glLibrary, functionName) {}
using glFunctionHelper::glFunctionPtr;
};
TEST(glFunctionHelper, whenCreateGlFunctionHelperThenSetGlFunctionPtrToLoadAnotherFunctions) {
std::unique_ptr<OsLibrary> glLibrary(OsLibrary::load("mock_opengl32.dll"));
EXPECT_TRUE(glLibrary->isLoaded());
glFunctionHelperMock loader(glLibrary.get(), "mockLoader");
funcType function1 = ConvertibleProcAddr{loader.glFunctionPtr("realFunction")};
funcType function2 = loader["realFunction"];
EXPECT_STREQ(function1(), function2());
}
TEST(glFunctionHelper, givenNonExistingFunctionNameWhenCreateGlFunctionHelperThenNullptr) {
std::unique_ptr<OsLibrary> glLibrary(OsLibrary::load("mock_opengl32.dll"));
EXPECT_TRUE(glLibrary->isLoaded());
glFunctionHelper loader(glLibrary.get(), "mockLoader");
funcType function = loader["nonExistingFunction"];
EXPECT_EQ(nullptr, function);
}
TEST(glFunctionHelper, givenRealFunctionNameWhenCreateGlFunctionHelperThenGetPointerToAppropriateFunction) {
std::unique_ptr<OsLibrary> glLibrary(OsLibrary::load("mock_opengl32.dll"));
EXPECT_TRUE(glLibrary->isLoaded());
glFunctionHelper loader(glLibrary.get(), "mockLoader");
funcType function = loader["realFunction"];
EXPECT_STREQ(realFunction(), function());
}
}; // namespace OCLRT

View File

@@ -0,0 +1,8 @@
/*
* Copyright (C) 2018 Intel Corporation
*
* SPDX-License-Identifier: MIT
*
*/
const char *realFunction() { return "value"; }

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

@@ -5,9 +5,15 @@
*
*/
#include <cstdint>
#include <string.h>
#include "unit_tests/helpers/windows/mock_function.h"
extern "C" {
void *__stdcall wglGetProcAddress(const char *name) { return nullptr; }
void *__stdcall mockLoader(const char *name) {
if (strcmp(name, "realFunction") == 0) {
return *realFunction;
}
return nullptr;
}
}

View File

@@ -20,4 +20,5 @@
LIBRARY "gdi32_mock"
EXPORTS
wglGetProcAddress
wglGetProcAddress
mockLoader

View File

@@ -91,3 +91,40 @@ 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;
int varB;
int varC;
using FunctionTypeA = void (*)(int *, float);
using FunctionTypeB = int (*)();
lib.ptrToReturn = &varA;
FunctionTypeA functionA = lib["funcA"];
EXPECT_STREQ("funcA", lib.lastRequestedProcName.c_str());
EXPECT_EQ(&varA, reinterpret_cast<void *>(functionA));
lib.ptrToReturn = &varB;
FunctionTypeB functionB = lib["funcB"];
EXPECT_STREQ("funcB", lib.lastRequestedProcName.c_str());
EXPECT_EQ(&varB, reinterpret_cast<void *>(functionB));
lib.ptrToReturn = &varC;
void *rawPtr = lib["funcC"];
EXPECT_STREQ("funcC", lib.lastRequestedProcName.c_str());
EXPECT_EQ(&varC, rawPtr);
}

View File

@@ -650,12 +650,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 +658,7 @@ TEST(glSharingBasicTest, givenInvalidExtensionNameWhenCheckGLExtensionSupportedT
TEST(glSharingBasicTest, givenglGetIntegervIsNullWhenCheckGLExtensionSupportedThenReturnFalse) {
MockGLSharingFunctions glSharingFunctions;
glSharingFunctions.setglGetIntegervToNull();
glSharingFunctions.glGetIntegerv = nullptr;
bool RetVal = glSharingFunctions.isOpenGlExtensionSupported("InvalidExtensionName");
EXPECT_FALSE(RetVal);
}
@@ -687,7 +681,7 @@ TEST(glSharingBasicTest, givenVendorisNullWhenCheckGLSharingSupportedThenReturnF
};
MockGLSharingFunctions glSharingFunctions;
glSharingFunctions.setGetStringFcn(invalidGetStringFcn);
glSharingFunctions.glGetString = invalidGetStringFcn;
bool RetVal = glSharingFunctions.isOpenGlSharingSupported();
EXPECT_FALSE(RetVal);