|
| 1 | +// Copyright (C) <2021> Intel Corporation |
| 2 | +// |
| 3 | +// SPDX-License-Identifier: Apache-2.0 |
| 4 | + |
| 5 | +#ifndef OWT_BASE_WIN_D3D11_MANAGER_H |
| 6 | +#define OWT_BASE_WIN_D3D11_MANAGER_H |
| 7 | + |
| 8 | +#include <atlbase.h> |
| 9 | +#include <dxgi1_2.h> |
| 10 | +#include <d3d11_2.h> |
| 11 | +#include <mfobjects.h> |
| 12 | +#include <mfapi.h> |
| 13 | +#include "webrtc/rtc_base/logging.h" |
| 14 | +#include "webrtc/rtc_base/ref_count.h" |
| 15 | + |
| 16 | +namespace owt { |
| 17 | +namespace base { |
| 18 | + |
| 19 | +class D3D11Manager : public rtc::RefCountInterface { |
| 20 | + public: |
| 21 | + D3D11Manager() |
| 22 | + : manager_(nullptr), |
| 23 | + device_(nullptr), |
| 24 | + ctx_(nullptr), |
| 25 | + adapter_(nullptr), |
| 26 | + mt_(nullptr), |
| 27 | + reset_token_(0) {} |
| 28 | + |
| 29 | + ~D3D11Manager() = default; |
| 30 | + |
| 31 | + bool Init() { |
| 32 | + HRESULT hr = MFCreateDXGIDeviceManager(&reset_token_, &manager_); |
| 33 | + if (FAILED(hr)) { |
| 34 | + RTC_LOG(LS_ERROR) << "MFCreateDxgiDeviceManager failed with error hr:" |
| 35 | + << hr; |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + CComPtr<IDXGIFactory2> factory; |
| 40 | + hr = CreateDXGIFactory1(__uuidof(IDXGIFactory2), (void**)(&factory)); |
| 41 | + if (FAILED(hr)) { |
| 42 | + RTC_LOG(LS_ERROR) << "CreateDxgiFactory failed with error hr:" << hr; |
| 43 | + return false; |
| 44 | + } |
| 45 | + |
| 46 | + hr = factory->EnumAdapters(0, &adapter_); |
| 47 | + if (FAILED(hr)) { |
| 48 | + RTC_LOG(LS_ERROR) << "EnumAdapters failed with error hr:" << hr; |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + UINT creation_flags = D3D11_CREATE_DEVICE_VIDEO_SUPPORT | D3D11_CREATE_DEVICE_BGRA_SUPPORT; |
| 53 | + |
| 54 | + D3D_FEATURE_LEVEL feature_levels_in[] = { |
| 55 | + D3D_FEATURE_LEVEL_11_1, D3D_FEATURE_LEVEL_11_0, D3D_FEATURE_LEVEL_10_1, |
| 56 | + D3D_FEATURE_LEVEL_10_0}; |
| 57 | + |
| 58 | + D3D_FEATURE_LEVEL feature_levels_out; |
| 59 | + hr = D3D11CreateDevice(nullptr, D3D_DRIVER_TYPE_HARDWARE, nullptr, |
| 60 | + creation_flags, feature_levels_in, |
| 61 | + ARRAYSIZE(feature_levels_in), D3D11_SDK_VERSION, |
| 62 | + &device_, &feature_levels_out, &ctx_); |
| 63 | + if (FAILED(hr)) { |
| 64 | + RTC_LOG(LS_ERROR) << "D3D11 CreateDevice failed with error hr:" << hr; |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + hr = manager_->ResetDevice(device_, reset_token_); |
| 69 | + if (FAILED(hr)) { |
| 70 | + RTC_LOG(LS_ERROR) << "ResetDevice failed with error hr:" << hr; |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + hr = device_->QueryInterface(__uuidof(ID3D10Multithread), (void**)(&mt_)); |
| 75 | + if (FAILED(hr)) { |
| 76 | + RTC_LOG(LS_ERROR) << "CreateDxgiFactory failed with error hr:" << hr; |
| 77 | + return false; |
| 78 | + } |
| 79 | + |
| 80 | + hr = mt_->SetMultithreadProtected(true); |
| 81 | + if (FAILED(hr)) { |
| 82 | + RTC_LOG(LS_ERROR) << "SetMultithread Protection failed with error hr:" |
| 83 | + << hr; |
| 84 | + return false; |
| 85 | + } |
| 86 | + |
| 87 | + return true; |
| 88 | + } |
| 89 | + |
| 90 | + CComPtr<IMFDXGIDeviceManager> GetManager() { |
| 91 | + return manager_; |
| 92 | + } |
| 93 | + |
| 94 | + CComPtr<ID3D11Device> GetDevice() { |
| 95 | + return device_; |
| 96 | + } |
| 97 | + |
| 98 | + CComPtr<ID3D11DeviceContext> GetDeviceContext() { |
| 99 | + return ctx_; |
| 100 | + } |
| 101 | + |
| 102 | + CComPtr<ID3D10Multithread> GetMultiThread() { |
| 103 | + return mt_; |
| 104 | + } |
| 105 | + |
| 106 | + private: |
| 107 | + CComPtr<IMFDXGIDeviceManager> manager_; |
| 108 | + CComPtr<ID3D11Device> device_; |
| 109 | + CComPtr<ID3D11DeviceContext> ctx_; |
| 110 | + CComQIPtr<IDXGIAdapter> adapter_; |
| 111 | + CComPtr<ID3D10Multithread> mt_; |
| 112 | + UINT reset_token_; |
| 113 | +}; |
| 114 | +} // namespace base |
| 115 | +} // namespace owt |
| 116 | +#endif //OWT_BASE_WIN_D3D11_MANAGER_H |
0 commit comments