| 1 |
/* |
| 2 |
* Copyright (C) 2018-2023 Intel Corporation |
| 3 |
* |
| 4 |
* SPDX-License-Identifier: MIT |
| 5 |
* |
| 6 |
*/ |
| 7 |
|
| 8 |
#include "va_sharing_functions.h" |
| 9 |
|
| 10 |
#include "shared/source/debug_settings/debug_settings_manager.h" |
| 11 |
|
| 12 |
#include "opencl/source/sharings/va/va_surface.h" |
| 13 |
|
| 14 |
#include <dlfcn.h> |
| 15 |
|
| 16 |
namespace Os { |
| 17 |
extern const char *libvaDllName; |
| 18 |
} |
| 19 |
|
| 20 |
namespace NEO { |
| 21 |
std::function<void *(const char *, int)> VASharingFunctions::fdlopen = dlopen; |
| 22 |
std::function<void *(void *handle, const char *symbol)> VASharingFunctions::fdlsym = dlsym; |
| 23 |
std::function<int(void *handle)> VASharingFunctions::fdlclose = dlclose; |
| 24 |
|
| 25 |
VASharingFunctions::VASharingFunctions(VADisplay vaDisplay) : vaDisplay(vaDisplay) { |
| 26 |
initFunctions(); |
| 27 |
}; |
| 28 |
|
| 29 |
VASharingFunctions::~VASharingFunctions() { |
| 30 |
if (libHandle != nullptr) { |
| 31 |
fdlclose(libHandle); |
| 32 |
libHandle = nullptr; |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
bool VASharingFunctions::isVaLibraryAvailable() { |
| 37 |
auto lib = fdlopen(Os::libvaDllName, RTLD_LAZY); |
| 38 |
if (lib) { |
| 39 |
fdlclose(lib); |
| 40 |
return true; |
| 41 |
} |
| 42 |
return false; |
| 43 |
} |
| 44 |
|
| 45 |
void VASharingFunctions::initFunctions() { |
| 46 |
bool enableVaLibCalls = true; |
| 47 |
if (debugManager.flags.EnableVaLibCalls.get() != -1) { |
| 48 |
enableVaLibCalls = !!debugManager.flags.EnableVaLibCalls.get(); |
| 49 |
} |
| 50 |
|
| 51 |
if (enableVaLibCalls) { |
| 52 |
libHandle = fdlopen(Os::libvaDllName, RTLD_LAZY); |
| 53 |
if (libHandle) { |
| 54 |
vaDisplayIsValidPFN = reinterpret_cast<VADisplayIsValidPFN>(fdlsym(libHandle, "vaDisplayIsValid")); |
| 55 |
vaDeriveImagePFN = reinterpret_cast<VADeriveImagePFN>(fdlsym(libHandle, "vaDeriveImage")); |
| 56 |
vaDestroyImagePFN = reinterpret_cast<VADestroyImagePFN>(fdlsym(libHandle, "vaDestroyImage")); |
| 57 |
vaSyncSurfacePFN = reinterpret_cast<VASyncSurfacePFN>(fdlsym(libHandle, "vaSyncSurface")); |
| 58 |
vaGetLibFuncPFN = reinterpret_cast<VAGetLibFuncPFN>(fdlsym(libHandle, "vaGetLibFunc")); |
| 59 |
vaExtGetSurfaceHandlePFN = reinterpret_cast<VAExtGetSurfaceHandlePFN>(getLibFunc("DdiMedia_ExtGetSurfaceHandle")); |
| 60 |
vaExportSurfaceHandlePFN = reinterpret_cast<VAExportSurfaceHandlePFN>(fdlsym(libHandle, "vaExportSurfaceHandle")); |
| 61 |
vaQueryImageFormatsPFN = reinterpret_cast<VAQueryImageFormatsPFN>(fdlsym(libHandle, "vaQueryImageFormats")); |
| 62 |
vaMaxNumImageFormatsPFN = reinterpret_cast<VAMaxNumImageFormatsPFN>(fdlsym(libHandle, "vaMaxNumImageFormats")); |
| 63 |
|
| 64 |
} else { |
| 65 |
vaDisplayIsValidPFN = nullptr; |
| 66 |
vaDeriveImagePFN = nullptr; |
| 67 |
vaDestroyImagePFN = nullptr; |
| 68 |
vaSyncSurfacePFN = nullptr; |
| 69 |
vaGetLibFuncPFN = nullptr; |
| 70 |
vaExtGetSurfaceHandlePFN = nullptr; |
| 71 |
vaExportSurfaceHandlePFN = nullptr; |
| 72 |
vaQueryImageFormatsPFN = nullptr; |
| 73 |
vaMaxNumImageFormatsPFN = nullptr; |
| 74 |
} |
| 75 |
} |
| 76 |
} |
| 77 |
|
| 78 |
void VASharingFunctions::querySupportedVaImageFormats(VADisplay vaDisplay) { |
| 79 |
int maxFormats = this->maxNumImageFormats(vaDisplay); |
| 80 |
if (maxFormats > 0) { |
| 81 |
std::unique_ptr<VAImageFormat[]> allVaFormats(new VAImageFormat[maxFormats]); |
| 82 |
auto result = this->queryImageFormats(vaDisplay, allVaFormats.get(), &maxFormats); |
| 83 |
if (result == VA_STATUS_SUCCESS) { |
| 84 |
for (int i = 0; i < maxFormats; i++) { |
| 85 |
if (VASurface::isSupportedFourCCTwoPlaneFormat(allVaFormats[i].fourcc)) { |
| 86 |
supported2PlaneFormats.emplace_back(allVaFormats[i]); |
| 87 |
} else if (VASurface::isSupportedFourCCThreePlaneFormat(allVaFormats[i].fourcc)) { |
| 88 |
supported3PlaneFormats.emplace_back(allVaFormats[i]); |
| 89 |
} else if (VASurface::isSupportedPackedFormat(allVaFormats[i].fourcc)) { |
| 90 |
supportedPackedFormats.emplace_back(allVaFormats[i]); |
| 91 |
} |
| 92 |
} |
| 93 |
} |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
cl_int VASharingFunctions::getSupportedFormats(cl_mem_flags flags, |
| 98 |
cl_mem_object_type imageType, |
| 99 |
cl_uint plane, |
| 100 |
cl_uint numEntries, |
| 101 |
VAImageFormat *formats, |
| 102 |
cl_uint *numImageFormats) { |
| 103 |
if (flags != CL_MEM_READ_ONLY && flags != CL_MEM_WRITE_ONLY && flags != CL_MEM_READ_WRITE && |
| 104 |
flags != CL_MEM_KERNEL_READ_AND_WRITE) { |
| 105 |
return CL_INVALID_VALUE; |
| 106 |
} |
| 107 |
|
| 108 |
if (imageType != CL_MEM_OBJECT_IMAGE2D) { |
| 109 |
return CL_SUCCESS; |
| 110 |
} |
| 111 |
|
| 112 |
if (numImageFormats != nullptr) { |
| 113 |
if (plane == 2) { |
| 114 |
*numImageFormats = static_cast<cl_uint>(supported3PlaneFormats.size()); |
| 115 |
} else if (plane < 2) { |
| 116 |
*numImageFormats = static_cast<cl_uint>(supported2PlaneFormats.size() + supported3PlaneFormats.size() + supportedPackedFormats.size()); |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
if (plane == 2) { |
| 121 |
if (formats != nullptr && supported3PlaneFormats.size() > 0) { |
| 122 |
uint32_t elementsToCopy = std::min(numEntries, static_cast<uint32_t>(supported3PlaneFormats.size())); |
| 123 |
memcpy_s(formats, elementsToCopy * sizeof(VAImageFormat), &supported3PlaneFormats[0], elementsToCopy * sizeof(VAImageFormat)); |
| 124 |
} |
| 125 |
} else if (plane < 2) { |
| 126 |
if (formats != nullptr && (supported2PlaneFormats.size() > 0 || supported3PlaneFormats.size() > 0 || supportedPackedFormats.size() > 0)) { |
| 127 |
uint32_t elementsToCopy = std::min(numEntries, static_cast<uint32_t>(supported2PlaneFormats.size() + supported3PlaneFormats.size() + supportedPackedFormats.size())); |
| 128 |
std::vector<VAImageFormat> tmpFormats; |
| 129 |
tmpFormats.insert(tmpFormats.end(), supported2PlaneFormats.begin(), supported2PlaneFormats.end()); |
| 130 |
tmpFormats.insert(tmpFormats.end(), supported3PlaneFormats.begin(), supported3PlaneFormats.end()); |
| 131 |
tmpFormats.insert(tmpFormats.end(), supportedPackedFormats.begin(), supportedPackedFormats.end()); |
| 132 |
memcpy_s(formats, elementsToCopy * sizeof(VAImageFormat), &tmpFormats[0], elementsToCopy * sizeof(VAImageFormat)); |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
return CL_SUCCESS; |
| 137 |
} |
| 138 |
} // namespace NEO |