| 1 | /* |
| 2 | * Copyright (C) 2018-2023 Intel Corporation |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "shared/test/common/fixtures/memory_management_fixture.h" |
| 9 | |
| 10 | #include "opencl/source/sharings/gl/linux/gl_sharing_linux.h" |
| 11 | #include "opencl/source/sharings/gl/linux/lin_enable_gl.h" |
| 12 | #include "opencl/test/unit_test/mocks/mock_context.h" |
| 13 | |
| 14 | #include "gtest/gtest.h" |
| 15 | |
| 16 | using namespace NEO; |
| 17 | |
| 18 | class GlSharingEnablerTests : public ::testing::Test { |
| 19 | public: |
| 20 | void SetUp() override { |
| 21 | factory.reset(new GlSharingBuilderFactory()); |
| 22 | ASSERT_NE(nullptr, factory.get()); |
| 23 | } |
| 24 | |
| 25 | std::unique_ptr<GlSharingBuilderFactory> factory; |
| 26 | }; |
| 27 | |
| 28 | TEST_F(GlSharingEnablerTests, givenGlFactoryWhenAskedThenExtensionsAreReturned) { |
| 29 | auto ext = factory->getExtensions(nullptr); |
| 30 | EXPECT_GT(ext.length(), 0u); |
| 31 | EXPECT_STRNE("", ext.c_str()); |
| 32 | } |
| 33 | |
| 34 | TEST_F(GlSharingEnablerTests, givenGlFactoryWhenAskedThenGlobalIcdIsConfigured) { |
| 35 | class IcdRestore { |
| 36 | public: |
| 37 | IcdRestore() { icdSnapshot = icdGlobalDispatchTable; } |
| 38 | ~IcdRestore() { icdGlobalDispatchTable = icdSnapshot; } |
| 39 | decltype(icdGlobalDispatchTable) icdSnapshot; |
| 40 | }; |
| 41 | // we play with global table, so first save state then restore it with use of RAII |
| 42 | IcdRestore icdRestore; |
| 43 | // clear ICD table |
| 44 | icdGlobalDispatchTable.clCreateFromGLBuffer = nullptr; |
| 45 | icdGlobalDispatchTable.clCreateFromGLTexture = nullptr; |
| 46 | icdGlobalDispatchTable.clCreateFromGLTexture2D = nullptr; |
| 47 | icdGlobalDispatchTable.clCreateFromGLTexture3D = nullptr; |
| 48 | icdGlobalDispatchTable.clCreateFromGLRenderbuffer = nullptr; |
| 49 | icdGlobalDispatchTable.clGetGLObjectInfo = nullptr; |
| 50 | icdGlobalDispatchTable.clGetGLTextureInfo = nullptr; |
| 51 | icdGlobalDispatchTable.clEnqueueAcquireGLObjects = nullptr; |
| 52 | icdGlobalDispatchTable.clEnqueueReleaseGLObjects = nullptr; |
| 53 | icdGlobalDispatchTable.clCreateEventFromGLsyncKHR = nullptr; |
| 54 | icdGlobalDispatchTable.clGetGLContextInfoKHR = nullptr; |
| 55 | |
| 56 | factory->fillGlobalDispatchTable(); |
| 57 | EXPECT_NE(nullptr, icdGlobalDispatchTable.clCreateFromGLBuffer); |
| 58 | EXPECT_NE(nullptr, icdGlobalDispatchTable.clCreateFromGLTexture); |
| 59 | EXPECT_NE(nullptr, icdGlobalDispatchTable.clCreateFromGLTexture2D); |
| 60 | EXPECT_NE(nullptr, icdGlobalDispatchTable.clCreateFromGLTexture3D); |
| 61 | EXPECT_NE(nullptr, icdGlobalDispatchTable.clCreateFromGLRenderbuffer); |
| 62 | EXPECT_NE(nullptr, icdGlobalDispatchTable.clGetGLObjectInfo); |
| 63 | EXPECT_NE(nullptr, icdGlobalDispatchTable.clGetGLTextureInfo); |
| 64 | EXPECT_NE(nullptr, icdGlobalDispatchTable.clEnqueueAcquireGLObjects); |
| 65 | EXPECT_NE(nullptr, icdGlobalDispatchTable.clEnqueueReleaseGLObjects); |
| 66 | EXPECT_NE(nullptr, icdGlobalDispatchTable.clCreateEventFromGLsyncKHR); |
| 67 | EXPECT_NE(nullptr, icdGlobalDispatchTable.clGetGLContextInfoKHR); |
| 68 | } |
| 69 | |
| 70 | TEST_F(GlSharingEnablerTests, givenGlFactoryWhenAskedThenBuilderIsCreated) { |
| 71 | auto builder = factory->createContextBuilder(); |
| 72 | EXPECT_NE(nullptr, builder); |
| 73 | } |
| 74 | |
| 75 | TEST_F(GlSharingEnablerTests, givenGlBuilderWhenUnknownPropertyThenFalseIsReturned) { |
| 76 | auto builder = factory->createContextBuilder(); |
| 77 | ASSERT_NE(nullptr, builder); |
| 78 | |
| 79 | cl_context_properties property = CL_CONTEXT_PLATFORM; |
| 80 | cl_context_properties value; |
| 81 | auto res = builder->processProperties(property, value); |
| 82 | EXPECT_FALSE(res); |
| 83 | } |
| 84 | |
| 85 | TEST_F(GlSharingEnablerTests, givenGlBuilderWhenInvalidPropertyThenFalseIsReturned) { |
| 86 | auto builder = factory->createContextBuilder(); |
| 87 | ASSERT_NE(nullptr, builder); |
| 88 | |
| 89 | cl_context_properties property = CL_CGL_SHAREGROUP_KHR; |
| 90 | cl_context_properties value; |
| 91 | auto res = builder->processProperties(property, value); |
| 92 | EXPECT_FALSE(res); |
| 93 | } |
| 94 | |
| 95 | TEST_F(GlSharingEnablerTests, givenGlBuilderWhenValidPropertyThenTrueIsReturned) { |
| 96 | cl_context_properties props[] = {CL_GL_CONTEXT_KHR, CL_EGL_DISPLAY_KHR}; |
| 97 | for (auto currProperty : props) { |
| 98 | auto builder = factory->createContextBuilder(); |
| 99 | ASSERT_NE(nullptr, builder); |
| 100 | |
| 101 | cl_context_properties property = currProperty; |
| 102 | cl_context_properties value = 0x10000; |
| 103 | auto res = builder->processProperties(property, value); |
| 104 | EXPECT_TRUE(res); |
| 105 | |
| 106 | // repeat to check if we don't allocate twice |
| 107 | auto prevAllocations = MemoryManagement::numAllocations.load(); |
| 108 | res = builder->processProperties(property, value); |
| 109 | EXPECT_TRUE(res); |
| 110 | auto currAllocations = MemoryManagement::numAllocations.load(); |
| 111 | EXPECT_EQ(prevAllocations, currAllocations); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | TEST_F(GlSharingEnablerTests, givenGlBuilderWhenNoPropertiesThenFinalizerReturnsTrue) { |
| 116 | auto builder = factory->createContextBuilder(); |
| 117 | ASSERT_NE(nullptr, builder); |
| 118 | |
| 119 | MockContext context; |
| 120 | int32_t errcodeRet = CL_SUCCESS; |
| 121 | auto res = builder->finalizeProperties(context, errcodeRet); |
| 122 | EXPECT_TRUE(res); |
| 123 | EXPECT_EQ(CL_SUCCESS, errcodeRet); |
| 124 | } |
| 125 | |
| 126 | TEST_F(GlSharingEnablerTests, givenGlBuilderWhenInvalidPropertiesThenFinalizerReturnsTrue) { |
| 127 | auto builder = factory->createContextBuilder(); |
| 128 | ASSERT_NE(nullptr, builder); |
| 129 | |
| 130 | cl_context_properties property = CL_CONTEXT_PLATFORM; |
| 131 | cl_context_properties value; |
| 132 | auto res = builder->processProperties(property, value); |
| 133 | EXPECT_FALSE(res); |
| 134 | |
| 135 | MockContext context; |
| 136 | int32_t errcodeRet = CL_SUCCESS; |
| 137 | res = builder->finalizeProperties(context, errcodeRet); |
| 138 | EXPECT_TRUE(res); |
| 139 | EXPECT_EQ(CL_SUCCESS, errcodeRet); |
| 140 | } |
| 141 | |
| 142 | TEST_F(GlSharingEnablerTests, givenGlBuilderWhenNullHandleThenFinalizerReturnsTrueAndNoSharingRegistered) { |
| 143 | auto builder = factory->createContextBuilder(); |
| 144 | ASSERT_NE(nullptr, builder); |
| 145 | |
| 146 | cl_context_properties property = CL_GL_CONTEXT_KHR; |
| 147 | cl_context_properties value = 0x0; |
| 148 | auto res = builder->processProperties(property, value); |
| 149 | EXPECT_TRUE(res); |
| 150 | |
| 151 | MockContext context; |
| 152 | int32_t errcodeRet = CL_SUCCESS; |
| 153 | res = builder->finalizeProperties(context, errcodeRet); |
| 154 | EXPECT_TRUE(res); |
| 155 | EXPECT_EQ(CL_SUCCESS, errcodeRet); |
| 156 | |
| 157 | auto sharing = context.getSharing<GLSharingFunctions>(); |
| 158 | EXPECT_EQ(nullptr, sharing); |
| 159 | } |
| 160 | TEST_F(GlSharingEnablerTests, givenGlBuilderWhenHandleThenFinalizerReturnsTrueAndSharingIsRegistered) { |
| 161 | auto builder = factory->createContextBuilder(); |
| 162 | ASSERT_NE(nullptr, builder); |
| 163 | |
| 164 | cl_context_properties property = CL_GL_CONTEXT_KHR; |
| 165 | cl_context_properties value = 0x1000; |
| 166 | auto res = builder->processProperties(property, value); |
| 167 | EXPECT_TRUE(res); |
| 168 | |
| 169 | MockContext context; |
| 170 | int32_t errcodeRet = CL_SUCCESS; |
| 171 | res = builder->finalizeProperties(context, errcodeRet); |
| 172 | EXPECT_TRUE(res); |
| 173 | EXPECT_EQ(CL_SUCCESS, errcodeRet); |
| 174 | |
| 175 | auto sharing = context.getSharing<GLSharingFunctions>(); |
| 176 | EXPECT_NE(nullptr, sharing); |
| 177 | } |