| 1 | /* |
| 2 | * Copyright (C) 2018-2023 Intel Corporation |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include "shared/test/common/helpers/debug_manager_state_restore.h" |
| 9 | #include "shared/test/common/helpers/variable_backup.h" |
| 10 | |
| 11 | #include "opencl/source/sharings/va/va_sharing_functions.h" |
| 12 | #include "opencl/test/unit_test/sharings/va/mock_va_sharing.h" |
| 13 | |
| 14 | #include "gtest/gtest.h" |
| 15 | |
| 16 | #include <functional> |
| 17 | #include <memory> |
| 18 | |
| 19 | using namespace NEO; |
| 20 | |
| 21 | class VASharingFunctionsTested : public VASharingFunctions { |
| 22 | public: |
| 23 | VASharingFunctionsTested() : VASharingFunctions(nullptr) {} |
| 24 | |
| 25 | bool wereFunctionsAssigned() const { |
| 26 | return vaDisplayIsValidPFN != nullptr && |
| 27 | vaDeriveImagePFN != nullptr && |
| 28 | vaDestroyImagePFN != nullptr && |
| 29 | vaSyncSurfacePFN != nullptr && |
| 30 | vaGetLibFuncPFN != nullptr && |
| 31 | vaExtGetSurfaceHandlePFN != nullptr; |
| 32 | } |
| 33 | |
| 34 | bool wereFunctionsAssignedNull() const { |
| 35 | return vaDisplayIsValidPFN == nullptr && |
| 36 | vaDeriveImagePFN == nullptr && |
| 37 | vaDestroyImagePFN == nullptr && |
| 38 | vaSyncSurfacePFN == nullptr && |
| 39 | vaGetLibFuncPFN == nullptr && |
| 40 | vaExtGetSurfaceHandlePFN == nullptr; |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | TEST(VASharingFunctions, GivenInitFunctionsWhenDLOpenFailsThenFunctionsAreNull) { |
| 45 | VariableBackup<decltype(VASharingFunctions::fdlopen)> dlopenBackup(&VASharingFunctions::fdlopen); |
| 46 | VariableBackup<decltype(VASharingFunctions::fdlsym)> dlsymBackup(&VASharingFunctions::fdlsym); |
| 47 | VariableBackup<decltype(VASharingFunctions::fdlclose)> dlcloseBackup(&VASharingFunctions::fdlclose); |
| 48 | |
| 49 | VASharingFunctions::fdlopen = [&](const char *filename, int flag) -> void * { |
| 50 | return nullptr; |
| 51 | }; |
| 52 | |
| 53 | VASharingFunctions::fdlsym = [&](void *handle, const char *symbol) -> void * { |
| 54 | return nullptr; |
| 55 | }; |
| 56 | |
| 57 | VASharingFunctions::fdlclose = [&](void *handle) -> int { |
| 58 | return 0; |
| 59 | }; |
| 60 | |
| 61 | VASharingFunctionsTested functions; |
| 62 | EXPECT_TRUE(functions.wereFunctionsAssignedNull()); |
| 63 | } |
| 64 | |
| 65 | void *getLibFunc(VADisplay vaDisplay, const char *func) { |
| 66 | return reinterpret_cast<void *>(uintptr_t(0xdeadbeef)); |
| 67 | } |
| 68 | |
| 69 | TEST(VASharingFunctions, GivenInitFunctionsWhenDLOpenSuccedsThenFunctionsAreNotNull) { |
| 70 | VariableBackup<decltype(VASharingFunctions::fdlopen)> dlopenBackup(&VASharingFunctions::fdlopen); |
| 71 | VariableBackup<decltype(VASharingFunctions::fdlsym)> dlsymBackup(&VASharingFunctions::fdlsym); |
| 72 | VariableBackup<decltype(VASharingFunctions::fdlclose)> dlcloseBackup(&VASharingFunctions::fdlclose); |
| 73 | |
| 74 | std::unique_ptr<uint32_t> valib(new uint32_t); |
| 75 | ASSERT_NE(nullptr, valib.get()); |
| 76 | |
| 77 | VASharingFunctions::fdlopen = [&](const char *filename, int flag) -> void * { |
| 78 | return valib.get(); |
| 79 | }; |
| 80 | |
| 81 | VASharingFunctions::fdlsym = [&](void *handle, const char *symbol) -> void * { |
| 82 | return (void *)getLibFunc; |
| 83 | }; |
| 84 | |
| 85 | VASharingFunctions::fdlclose = [&](void *handle) -> int { |
| 86 | return 0; |
| 87 | }; |
| 88 | |
| 89 | VASharingFunctionsTested functions; |
| 90 | EXPECT_TRUE(functions.wereFunctionsAssigned()); |
| 91 | } |
| 92 | |
| 93 | TEST(VASharingFunctions, GivenInitFunctionsWhenEnableVaLibCallsThenFunctionsAreAssigned) { |
| 94 | DebugManagerStateRestore restorer; |
| 95 | |
| 96 | VariableBackup<decltype(VASharingFunctions::fdlopen)> dlopenBackup(&VASharingFunctions::fdlopen); |
| 97 | VariableBackup<decltype(VASharingFunctions::fdlsym)> dlsymBackup(&VASharingFunctions::fdlsym); |
| 98 | VariableBackup<decltype(VASharingFunctions::fdlclose)> dlcloseBackup(&VASharingFunctions::fdlclose); |
| 99 | |
| 100 | std::unique_ptr<uint32_t> valib(new uint32_t); |
| 101 | ASSERT_NE(nullptr, valib.get()); |
| 102 | |
| 103 | VASharingFunctions::fdlopen = [&](const char *filename, int flag) -> void * { |
| 104 | return valib.get(); |
| 105 | }; |
| 106 | |
| 107 | VASharingFunctions::fdlsym = [&](void *handle, const char *symbol) -> void * { |
| 108 | return (void *)getLibFunc; |
| 109 | }; |
| 110 | |
| 111 | VASharingFunctions::fdlclose = [&](void *handle) -> int { |
| 112 | return 0; |
| 113 | }; |
| 114 | |
| 115 | EXPECT_EQ(-1, debugManager.flags.EnableVaLibCalls.get()); |
| 116 | VASharingFunctionsTested functionsWithDefaultVaLibCalls; |
| 117 | EXPECT_TRUE(functionsWithDefaultVaLibCalls.wereFunctionsAssigned()); |
| 118 | |
| 119 | debugManager.flags.EnableVaLibCalls.set(1); |
| 120 | VASharingFunctionsTested functionsWithEnabledVaLibCalls; |
| 121 | EXPECT_TRUE(functionsWithEnabledVaLibCalls.wereFunctionsAssigned()); |
| 122 | } |
| 123 | |
| 124 | TEST(VASharingFunctions, GivenFunctionsWhenNoLibvaThenDlcloseNotCalled) { |
| 125 | VariableBackup<decltype(VASharingFunctions::fdlopen)> dlopenBackup(&VASharingFunctions::fdlopen); |
| 126 | VariableBackup<decltype(VASharingFunctions::fdlsym)> dlsymBackup(&VASharingFunctions::fdlsym); |
| 127 | VariableBackup<decltype(VASharingFunctions::fdlclose)> dlcloseBackup(&VASharingFunctions::fdlclose); |
| 128 | |
| 129 | uint32_t closeCalls = 0; |
| 130 | |
| 131 | VASharingFunctions::fdlopen = [&](const char *filename, int flag) -> void * { |
| 132 | return nullptr; |
| 133 | }; |
| 134 | |
| 135 | VASharingFunctions::fdlsym = [&](void *handle, const char *symbol) -> void * { |
| 136 | return nullptr; |
| 137 | }; |
| 138 | |
| 139 | VASharingFunctions::fdlclose = [&](void *handle) -> int { |
| 140 | closeCalls++; |
| 141 | return 0; |
| 142 | }; |
| 143 | |
| 144 | { |
| 145 | // we need this to properly track closeCalls |
| 146 | VASharingFunctionsTested functions; |
| 147 | } |
| 148 | EXPECT_EQ(0u, closeCalls); |
| 149 | } |
| 150 | |
| 151 | TEST(VASharingFunctions, GivenFunctionsWhenLibvaLoadedThenDlcloseIsCalled) { |
| 152 | VariableBackup<decltype(VASharingFunctions::fdlopen)> dlopenBackup(&VASharingFunctions::fdlopen); |
| 153 | VariableBackup<decltype(VASharingFunctions::fdlsym)> dlsymBackup(&VASharingFunctions::fdlsym); |
| 154 | VariableBackup<decltype(VASharingFunctions::fdlclose)> dlcloseBackup(&VASharingFunctions::fdlclose); |
| 155 | |
| 156 | std::unique_ptr<uint32_t> valib(new uint32_t); |
| 157 | ASSERT_NE(nullptr, valib.get()); |
| 158 | |
| 159 | uint32_t closeCalls = 0; |
| 160 | |
| 161 | VASharingFunctions::fdlopen = [&](const char *filename, int flag) -> void * { |
| 162 | return valib.get(); |
| 163 | }; |
| 164 | |
| 165 | VASharingFunctions::fdlsym = [&](void *handle, const char *symbol) -> void * { |
| 166 | return nullptr; |
| 167 | }; |
| 168 | |
| 169 | VASharingFunctions::fdlclose = [&](void *handle) -> int { |
| 170 | if (handle == valib.get()) { |
| 171 | closeCalls++; |
| 172 | } |
| 173 | return 0; |
| 174 | }; |
| 175 | |
| 176 | { |
| 177 | // we need this to properly track closeCalls |
| 178 | VASharingFunctionsTested functions; |
| 179 | } |
| 180 | EXPECT_EQ(1u, closeCalls); |
| 181 | } |
| 182 | |
| 183 | TEST(VASharingFunctions, givenEnabledExtendedVaFormatsWhenQueryingSupportedFormatsThenAllSupportedFormatsAreStored) { |
| 184 | DebugManagerStateRestore restore; |
| 185 | debugManager.flags.EnableExtendedVaFormats.set(true); |
| 186 | |
| 187 | VASharingFunctionsMock sharingFunctions; |
| 188 | |
| 189 | sharingFunctions.querySupportedVaImageFormats(VADisplay(1)); |
| 190 | |
| 191 | EXPECT_EQ(3u, sharingFunctions.supported2PlaneFormats.size()); |
| 192 | EXPECT_EQ(1u, sharingFunctions.supported3PlaneFormats.size()); |
| 193 | |
| 194 | size_t allFormatsFound = 0; |
| 195 | for (const auto &supported2PlaneFormat : sharingFunctions.supported2PlaneFormats) { |
| 196 | if (supported2PlaneFormat.fourcc == VA_FOURCC_NV12 || supported2PlaneFormat.fourcc == VA_FOURCC_P010 || supported2PlaneFormat.fourcc == VA_FOURCC_P016) { |
| 197 | allFormatsFound++; |
| 198 | } |
| 199 | } |
| 200 | for (const auto &supported3PlaneFormat : sharingFunctions.supported3PlaneFormats) { |
| 201 | if (supported3PlaneFormat.fourcc == VA_FOURCC_RGBP) { |
| 202 | allFormatsFound++; |
| 203 | } |
| 204 | } |
| 205 | EXPECT_EQ(4u, allFormatsFound); |
| 206 | } |