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

@@ -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);