Debian Codegraph @

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

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