Debian Codegraph @

opencl/test/unit_test/sharings/va/va_sharing_enable_tests.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2018-2025 Intel Corporation
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 */
7
8 #include "shared/source/os_interface/driver_info.h"
9 #include "shared/test/common/fixtures/memory_management_fixture.h"
10 #include "shared/test/common/helpers/variable_backup.h"
11
12 #include "opencl/source/sharings/va/enable_va.h"
13 #include "opencl/source/sharings/va/va_sharing_functions.h"
14 #include "opencl/test/unit_test/mocks/mock_context.h"
15
16 #include "gtest/gtest.h"
17
18 using namespace NEO;
19
20 static int vaDisplayIsValidRet = 1;
21 extern "C" int vaDisplayIsValid(VADisplay vaDisplay) {
22 return vaDisplayIsValidRet;
23 }
24
25 class MockDriverInfo : public DriverInfo {
26 public:
27 MockDriverInfo(bool imageSupport) : DriverInfo(DriverInfoType::unknown), imageSupport(imageSupport) {}
28 bool getMediaSharingSupport() override { return imageSupport; };
29 bool imageSupport = true;
30 };
31
32 class VaSharingEnablerTests : public MemoryManagementFixture,
33 public ::testing::Test {
34 public:
35 void SetUp() override {
36 MemoryManagementFixture::setUp();
37 factory.reset(new VaSharingBuilderFactory());
38 ASSERT_NE(nullptr, factory.get());
39 }
40
41 void TearDown() override {
42 factory.reset(nullptr);
43 MemoryManagementFixture::tearDown();
44 }
45 std::unique_ptr<VaSharingBuilderFactory> factory;
46 };
47
48 TEST_F(VaSharingEnablerTests, givenVaFactoryWhenImagesUnsupportedOrLibVaUnavailableThenNoExtensionIsReturned) {
49 // hijack dlopen function
50 VariableBackup<std::function<void *(const char *, int)>> bkp(&VASharingFunctions::fdlopen);
51 bkp = [&](const char *filename, int flag) -> void * {
52 // no libva in system
53 return nullptr;
54 };
55 for (bool imagesSupported : {false, true}) {
56 MockDriverInfo driverInfo(imagesSupported);
57 auto ext = factory->getExtensions(&driverInfo);
58 EXPECT_EQ(0u, ext.length());
59 EXPECT_STREQ("", ext.c_str());
60 }
61 }
62
63 TEST_F(VaSharingEnablerTests, givenVaFactoryWhenImagesSupportedAndLibVaAvailableThenExtensionStringIsReturned) {
64 VariableBackup<std::function<void *(const char *, int)>> bkpOpen(&VASharingFunctions::fdlopen);
65 bkpOpen = [&](const char *filename, int flag) -> void * {
66 return this;
67 };
68 VariableBackup<std::function<int(void *)>> bkpClose(&VASharingFunctions::fdlclose);
69 bkpClose = [&](void *handle) -> int {
70 return 0;
71 };
72 VariableBackup<std::function<void *(void *, const char *)>> bkpSym(&VASharingFunctions::fdlsym);
73 bkpSym = [&](void *handle, const char *symbol) -> void * {
74 return nullptr;
75 };
76 for (bool imagesSupported : {false, true}) {
77 MockDriverInfo driverInfo(imagesSupported);
78 auto ext = factory->getExtensions(&driverInfo);
79 EXPECT_STREQ(imagesSupported ? "cl_intel_va_api_media_sharing " : "", ext.c_str());
80 }
81 }
82
83 TEST_F(VaSharingEnablerTests, givenVaFactoryWhenAskedThenGlobalIcdIsConfigured) {
84 class CrtRestore {
85 public:
86 CrtRestore() {
87 crtSnapshot = crtGlobalDispatchTable;
88 }
89 ~CrtRestore() {
90 crtGlobalDispatchTable = crtSnapshot;
91 }
92 decltype(crtGlobalDispatchTable) crtSnapshot;
93 };
94
95 // we play with global table, so first save state then restore it with use of RAII
96 CrtRestore crtRestore;
97 crtGlobalDispatchTable.clCreateFromVA_APIMediaSurfaceINTEL = nullptr;
98 crtGlobalDispatchTable.clEnqueueReleaseVA_APIMediaSurfacesINTEL = nullptr;
99 crtGlobalDispatchTable.clEnqueueAcquireVA_APIMediaSurfacesINTEL = nullptr;
100 crtGlobalDispatchTable.clGetDeviceIDsFromVA_APIMediaAdapterINTEL = nullptr;
101 factory->fillGlobalDispatchTable();
102 EXPECT_NE(nullptr, crtGlobalDispatchTable.clCreateFromVA_APIMediaSurfaceINTEL);
103 EXPECT_NE(nullptr, crtGlobalDispatchTable.clEnqueueAcquireVA_APIMediaSurfacesINTEL);
104 EXPECT_NE(nullptr, crtGlobalDispatchTable.clEnqueueAcquireVA_APIMediaSurfacesINTEL);
105 EXPECT_NE(nullptr, crtGlobalDispatchTable.clGetDeviceIDsFromVA_APIMediaAdapterINTEL);
106 }
107
108 TEST_F(VaSharingEnablerTests, givenVaFactoryWhenAskedThenBuilderIsCreated) {
109 auto builder = factory->createContextBuilder();
110 EXPECT_NE(nullptr, builder);
111 }
112
113 TEST_F(VaSharingEnablerTests, givenVaBuilderWhenUnknownPropertyThenFalseIsReturned) {
114 auto builder = factory->createContextBuilder();
115 ASSERT_NE(nullptr, builder);
116
117 cl_context_properties property = CL_CONTEXT_PLATFORM;
118 cl_context_properties value;
119 auto res = builder->processProperties(property, value);
120 EXPECT_FALSE(res);
121 }
122
123 TEST_F(VaSharingEnablerTests, givenVaBuilderWhenValidPropertyThenTrueIsReturned) {
124 auto builder = factory->createContextBuilder();
125 ASSERT_NE(nullptr, builder);
126
127 cl_context_properties property = CL_CONTEXT_VA_API_DISPLAY_INTEL;
128 cl_context_properties value = 0x1243;
129 auto res = builder->processProperties(property, value);
130 EXPECT_TRUE(res);
131
132 // repeat to check if we don't allocate twice
133 auto prevAllocations = MemoryManagement::numAllocations.load();
134 res = builder->processProperties(property, value);
135 EXPECT_TRUE(res);
136 auto currAllocations = MemoryManagement::numAllocations.load();
137 EXPECT_EQ(prevAllocations, currAllocations);
138 }
139
140 TEST_F(VaSharingEnablerTests, givenVaBuilderWhenNoPropertiesThenFinalizerReturnsTrue) {
141 auto builder = factory->createContextBuilder();
142 ASSERT_NE(nullptr, builder);
143
144 MockContext context;
145 int32_t errcodeRet = CL_SUCCESS;
146 auto res = builder->finalizeProperties(context, errcodeRet);
147 EXPECT_TRUE(res);
148 EXPECT_EQ(CL_SUCCESS, errcodeRet);
149 }
150
151 TEST_F(VaSharingEnablerTests, givenVaBuilderWhenInvalidPropertiesThenFinalizerReturnsTrue) {
152 auto builder = factory->createContextBuilder();
153 ASSERT_NE(nullptr, builder);
154
155 cl_context_properties property = CL_CONTEXT_PLATFORM;
156 cl_context_properties value;
157 auto res = builder->processProperties(property, value);
158 EXPECT_FALSE(res);
159
160 MockContext context;
161 int32_t errcodeRet = CL_SUCCESS;
162 res = builder->finalizeProperties(context, errcodeRet);
163 EXPECT_TRUE(res);
164 EXPECT_EQ(CL_SUCCESS, errcodeRet);
165 }
166
167 TEST_F(VaSharingEnablerTests, givenVaBuilderWhenValidPropertyButInvalidDisplayThenFinalizerReturnsFalseAndErrcode) {
168 auto builder = factory->createContextBuilder();
169 ASSERT_NE(nullptr, builder);
170
171 vaDisplayIsValidRet = 0;
172 cl_context_properties property = CL_CONTEXT_VA_API_DISPLAY_INTEL;
173 cl_context_properties value = 0x10000;
174 auto res = builder->processProperties(property, value);
175 EXPECT_TRUE(res);
176
177 MockContext context;
178 int32_t errcodeRet = CL_SUCCESS;
179 res = builder->finalizeProperties(context, errcodeRet);
180 EXPECT_FALSE(res);
181 EXPECT_EQ(CL_INVALID_VA_API_MEDIA_ADAPTER_INTEL, errcodeRet);
182 }
183
184 TEST_F(VaSharingEnablerTests, givenVaBuilderWhenValidPropertyButValidDisplayThenFinalizerReturnsTrue) {
185 auto builder = factory->createContextBuilder();
186 ASSERT_NE(nullptr, builder);
187
188 vaDisplayIsValidRet = 1;
189 cl_context_properties property = CL_CONTEXT_VA_API_DISPLAY_INTEL;
190 cl_context_properties value = 0x10000;
191 auto res = builder->processProperties(property, value);
192 EXPECT_TRUE(res);
193
194 MockContext context;
195 int32_t errcodeRet = CL_SUCCESS;
196 res = builder->finalizeProperties(context, errcodeRet);
197 EXPECT_TRUE(res);
198 EXPECT_EQ(CL_SUCCESS, errcodeRet);
199 }