| 1 | /* |
| 2 | * Copyright © Microsoft Corporation |
| 3 | * |
| 4 | * Permission is hereby granted, free of charge, to any person obtaining a |
| 5 | * copy of this software and associated documentation files (the "Software"), |
| 6 | * to deal in the Software without restriction, including without limitation |
| 7 | * the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 8 | * and/or sell copies of the Software, and to permit persons to whom the |
| 9 | * Software is furnished to do so, subject to the following conditions: |
| 10 | * |
| 11 | * The above copyright notice and this permission notice (including the next |
| 12 | * paragraph) shall be included in all copies or substantial portions of the |
| 13 | * Software. |
| 14 | * |
| 15 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 18 | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 | * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 21 | * IN THE SOFTWARE. |
| 22 | */ |
| 23 | |
| 24 | #include "vaapi_display_win32.h" |
| 25 | |
| 26 | #include <directx/dxcore_interface.h> |
| 27 | #include <stdbool.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | |
| 31 | static const char* g_device_name; |
| 32 | |
| 33 | void dxcore_resolve_adapter(const char* adapter_string, |
| 34 | /*out*/ bool* ptr_device_found, |
| 35 | /*out*/ LUID* ptr_adapter_luid) { |
| 36 | int selected_adapter_index = -1; |
| 37 | IDXCoreAdapterFactory* factory = nullptr; |
| 38 | IDXCoreAdapterList* adapter_list = nullptr; |
| 39 | IDXCoreAdapter* adapter = nullptr; |
| 40 | typedef HRESULT(WINAPI * PFN_CREATE_DXCORE_ADAPTER_FACTORY)(REFIID riid, |
| 41 | void** ppFactory); |
| 42 | PFN_CREATE_DXCORE_ADAPTER_FACTORY DXCoreCreateAdapterFactory; |
| 43 | HRESULT hr = S_OK; |
| 44 | |
| 45 | memset(ptr_adapter_luid, 0, sizeof(LUID)); |
| 46 | *ptr_device_found = false; |
| 47 | |
| 48 | HMODULE dxcore_mod = LoadLibraryA("DXCore.DLL"); |
| 49 | if (!dxcore_mod) { |
| 50 | fprintf(stderr, "Failed to load DXCore.DLL to enumerate adapters.\n"); |
| 51 | goto fail; |
| 52 | } |
| 53 | |
| 54 | DXCoreCreateAdapterFactory = |
| 55 | (PFN_CREATE_DXCORE_ADAPTER_FACTORY)GetProcAddress( |
| 56 | dxcore_mod, "DXCoreCreateAdapterFactory"); |
| 57 | if (!DXCoreCreateAdapterFactory) { |
| 58 | fprintf(stderr, |
| 59 | "Failed to load DXCoreCreateAdapterFactory from DXCore.DLL.\n"); |
| 60 | goto fail; |
| 61 | } |
| 62 | |
| 63 | hr = DXCoreCreateAdapterFactory(IID_IDXCoreAdapterFactory, (void**)&factory); |
| 64 | if (FAILED(hr)) { |
| 65 | fprintf(stderr, "DXCoreCreateAdapterFactory failed: %lx\n", hr); |
| 66 | goto fail; |
| 67 | } |
| 68 | |
| 69 | hr = |
| 70 | factory->CreateAdapterList(1, &DXCORE_ADAPTER_ATTRIBUTE_D3D12_GRAPHICS, |
| 71 | IID_IDXCoreAdapterList, (void**)&adapter_list); |
| 72 | if (FAILED(hr)) { |
| 73 | fprintf(stderr, "CreateAdapterList failed: %lx\n", hr); |
| 74 | goto fail; |
| 75 | } |
| 76 | |
| 77 | if (adapter_string && |
| 78 | (sscanf_s(adapter_string, "%d", &selected_adapter_index) != 1)) { |
| 79 | fprintf(stderr, "Invalid device index received for -hwaccel_device %s\n", |
| 80 | adapter_string ? adapter_string : ""); |
| 81 | } |
| 82 | |
| 83 | if (!adapter_string) |
| 84 | fprintf(stdout, "Available devices for --display win32:\n"); |
| 85 | for (int i = 0; i < adapter_list->GetAdapterCount(); i++) { |
| 86 | if (SUCCEEDED(adapter_list->GetAdapter(i, IID_IDXCoreAdapter, |
| 87 | (void**)&adapter))) { |
| 88 | size_t desc_size = 0; |
| 89 | if (FAILED(adapter->GetPropertySize( |
| 90 | DXCoreAdapterProperty::DriverDescription, &desc_size))) { |
| 91 | adapter->Release(); |
| 92 | continue; |
| 93 | } |
| 94 | |
| 95 | char* adapter_name = (char*)malloc(desc_size); |
| 96 | if (!adapter_name) { |
| 97 | adapter->Release(); |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | if (FAILED(adapter->GetProperty(DXCoreAdapterProperty::DriverDescription, |
| 102 | desc_size, adapter_name))) { |
| 103 | free(adapter_name); |
| 104 | adapter->Release(); |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | LUID cur_adapter_luid = {0, 0}; |
| 109 | if (FAILED(adapter->GetProperty(DXCoreAdapterProperty::InstanceLuid, |
| 110 | &cur_adapter_luid))) { |
| 111 | free(adapter_name); |
| 112 | adapter->Release(); |
| 113 | continue; |
| 114 | } |
| 115 | |
| 116 | if (selected_adapter_index == i) { |
| 117 | *ptr_adapter_luid = cur_adapter_luid; |
| 118 | *ptr_device_found = true; |
| 119 | } |
| 120 | |
| 121 | if (!adapter_string) |
| 122 | fprintf(stdout, |
| 123 | "\tDevice Index: %d Device LUID: %lu %ld - Device Name: %s\n", |
| 124 | i, cur_adapter_luid.LowPart, cur_adapter_luid.HighPart, |
| 125 | adapter_name); |
| 126 | free(adapter_name); |
| 127 | adapter->Release(); |
| 128 | } |
| 129 | } |
| 130 | |
| 131 | fail: |
| 132 | if (adapter_list) |
| 133 | adapter_list->Release(); |
| 134 | if (factory) |
| 135 | factory->Release(); |
| 136 | if (dxcore_mod) |
| 137 | FreeLibrary(dxcore_mod); |
| 138 | } |
| 139 | |
| 140 | static VADisplay va_open_display_win32(void) { |
| 141 | LUID adapter_luid = {0, 0}; |
| 142 | bool device_found = false; |
| 143 | if (g_device_name) { |
| 144 | bool print_devices = (0 == strcmp(g_device_name, "help")); |
| 145 | dxcore_resolve_adapter(print_devices ? NULL : g_device_name, &device_found, |
| 146 | &adapter_luid); |
| 147 | if (print_devices) { |
| 148 | exit(0); |
| 149 | } else if (g_device_name && !device_found) { |
| 150 | fprintf(stderr, |
| 151 | "Could not find device %s for --display win32. Please try " |
| 152 | "--device help for a list of available devices.\n", |
| 153 | g_device_name); |
| 154 | exit(0); |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | // Adapter automatic selection supported by sending NULL adapter to |
| 159 | // vaGetDisplayWin32 |
| 160 | return vaGetDisplayWin32(device_found ? &adapter_luid : NULL); |
| 161 | } |
| 162 | |
| 163 | namespace livekit_ffi { |
| 164 | |
| 165 | VaapiDisplayWin32::VaapiDisplayWin32() : va_display_(nullptr) { |
| 166 | putenv("LIBVA_DRIVER_NAME=vaon12"); |
| 167 | putenv("LIBVA_DRIVERS_PATH=."); |
| 168 | } |
| 169 | |
| 170 | VaapiDisplayWin32::~VaapiDisplayWin32() { |
| 171 | Close(); |
| 172 | } |
| 173 | |
| 174 | bool VaapiDisplayWin32::Open() { |
| 175 | Close(); |
| 176 | va_display_ = va_open_display_win32(); |
| 177 | if (!va_display_) { |
| 178 | fprintf(stderr, "Failed to open VA display\n"); |
| 179 | return false; |
| 180 | } |
| 181 | |
| 182 | int major_ver = 0; |
| 183 | int minor_ver = 0; |
| 184 | VAStatus va_status = vaInitialize(va_display_, &major_ver, &minor_ver); |
| 185 | if (va_status != VA_STATUS_SUCCESS) { |
| 186 | fprintf(stderr, "Failed to initialize VA display\n"); |
| 187 | vaTerminate(va_display_); |
| 188 | va_display_ = nullptr; |
| 189 | return false; |
| 190 | } |
| 191 | return true; |
| 192 | } |
| 193 | |
| 194 | bool VaapiDisplayWin32::isOpen() const { |
| 195 | return va_display_ != nullptr; |
| 196 | } |
| 197 | |
| 198 | void VaapiDisplayWin32::Close() { |
| 199 | if (va_display_) { |
| 200 | vaTerminate(va_display_); |
| 201 | va_display_ = nullptr; |
| 202 | } |
| 203 | } |
| 204 | |
| 205 | } // namespace livekit_ffi |