mirror of
https://github.com/intel/compute-runtime.git
synced 2026-01-11 08:07:19 +08:00
Refactor of glcl sharing
new pattern to load gl and wgl functions from dll Change-Id: I71d7510a210462c09d50e03ce8a444770c017b8d
This commit is contained in:
committed by
sys_ocldev
parent
d1a80db2f2
commit
f18532c8c4
@@ -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
|
||||
|
||||
49
unit_tests/helpers/windows/gl_helper_tests.cpp
Normal file
49
unit_tests/helpers/windows/gl_helper_tests.cpp
Normal 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
|
||||
8
unit_tests/helpers/windows/mock_function.h
Normal file
8
unit_tests/helpers/windows/mock_function.h
Normal file
@@ -0,0 +1,8 @@
|
||||
/*
|
||||
* Copyright (C) 2018 Intel Corporation
|
||||
*
|
||||
* SPDX-License-Identifier: MIT
|
||||
*
|
||||
*/
|
||||
|
||||
const char *realFunction() { return "value"; }
|
||||
@@ -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;
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,4 +20,5 @@
|
||||
|
||||
LIBRARY "gdi32_mock"
|
||||
EXPORTS
|
||||
wglGetProcAddress
|
||||
wglGetProcAddress
|
||||
mockLoader
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user