Austin Orion | d19d0cf | 2021-01-27 17:40:21 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2020 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
| 11 | #include "rtc_base/win/create_direct3d_device.h" |
| 12 | |
| 13 | #include <libloaderapi.h> |
| 14 | #include <utility> |
| 15 | |
| 16 | namespace { |
| 17 | |
| 18 | FARPROC LoadD3D11Function(const char* function_name) { |
| 19 | static HMODULE const handle = |
| 20 | ::LoadLibraryExW(L"d3d11.dll", nullptr, LOAD_LIBRARY_SEARCH_SYSTEM32); |
| 21 | return handle ? ::GetProcAddress(handle, function_name) : nullptr; |
| 22 | } |
| 23 | |
| 24 | decltype(&::CreateDirect3D11DeviceFromDXGIDevice) |
| 25 | GetCreateDirect3D11DeviceFromDXGIDevice() { |
| 26 | static decltype(&::CreateDirect3D11DeviceFromDXGIDevice) const function = |
| 27 | reinterpret_cast<decltype(&::CreateDirect3D11DeviceFromDXGIDevice)>( |
| 28 | LoadD3D11Function("CreateDirect3D11DeviceFromDXGIDevice")); |
| 29 | return function; |
| 30 | } |
| 31 | |
| 32 | } // namespace |
| 33 | |
| 34 | namespace webrtc { |
| 35 | |
| 36 | bool ResolveCoreWinRTDirect3DDelayload() { |
| 37 | return GetCreateDirect3D11DeviceFromDXGIDevice(); |
| 38 | } |
| 39 | |
| 40 | HRESULT CreateDirect3DDeviceFromDXGIDevice( |
| 41 | IDXGIDevice* dxgi_device, |
| 42 | ABI::Windows::Graphics::DirectX::Direct3D11::IDirect3DDevice** |
| 43 | out_d3d11_device) { |
| 44 | decltype(&::CreateDirect3D11DeviceFromDXGIDevice) create_d3d11_device_func = |
| 45 | GetCreateDirect3D11DeviceFromDXGIDevice(); |
| 46 | if (!create_d3d11_device_func) |
| 47 | return E_FAIL; |
| 48 | |
| 49 | Microsoft::WRL::ComPtr<IInspectable> inspectableSurface; |
| 50 | HRESULT hr = create_d3d11_device_func(dxgi_device, &inspectableSurface); |
| 51 | if (FAILED(hr)) |
| 52 | return hr; |
| 53 | |
| 54 | return inspectableSurface->QueryInterface(IID_PPV_ARGS(out_d3d11_device)); |
| 55 | } |
| 56 | |
| 57 | } // namespace webrtc |