OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (c) 2016 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 "webrtc/modules/desktop_capture/win/d3d_device.h" |
| 12 |
| 13 #include <utility> |
| 14 |
| 15 #include "webrtc/system_wrappers/include/logging.h" |
| 16 |
| 17 namespace webrtc { |
| 18 |
| 19 using Microsoft::WRL::ComPtr; |
| 20 |
| 21 D3dDevice::D3dDevice() = default; |
| 22 D3dDevice::~D3dDevice() = default; |
| 23 |
| 24 bool D3dDevice::Initialize(const ComPtr<IDXGIAdapter>& adapter) { |
| 25 dxgi_adapter_ = adapter; |
| 26 if (!dxgi_adapter_) { |
| 27 LOG(LS_WARNING) << "An empty IDXGIAdapter instance has been received."; |
| 28 return false; |
| 29 } |
| 30 |
| 31 D3D_FEATURE_LEVEL feature_level; |
| 32 _com_error error = D3D11CreateDevice( |
| 33 adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, |
| 34 D3D11_CREATE_DEVICE_BGRA_SUPPORT | D3D11_CREATE_DEVICE_SINGLETHREADED, |
| 35 nullptr, 0, D3D11_SDK_VERSION, d3d_device_.GetAddressOf(), &feature_level, |
| 36 context_.GetAddressOf()); |
| 37 if (error.Error() != S_OK || !d3d_device_ || !context_) { |
| 38 LOG(LS_WARNING) << "D3D11CreateDeivce returns error " |
| 39 << error.ErrorMessage() << " with code " << error.Error(); |
| 40 return false; |
| 41 } |
| 42 |
| 43 if (feature_level < D3D_FEATURE_LEVEL_11_0) { |
| 44 LOG(LS_WARNING) << "D3D11CreateDevice returns an instance without DirectX " |
| 45 "11 support, level " |
| 46 << feature_level; |
| 47 return false; |
| 48 } |
| 49 |
| 50 error = _com_error(d3d_device_.As(&dxgi_device_)); |
| 51 if (error.Error() != S_OK || !dxgi_device_) { |
| 52 LOG(LS_WARNING) << "ID3D11Device is not an implementation of IDXGIDevice, " |
| 53 "this usually means the system does not support DirectX " |
| 54 "11"; |
| 55 return false; |
| 56 } |
| 57 |
| 58 return true; |
| 59 } |
| 60 |
| 61 // static |
| 62 std::vector<D3dDevice> D3dDevice::EnumDevices() { |
| 63 ComPtr<IDXGIFactory1> factory; |
| 64 _com_error error = _com_error( |
| 65 CreateDXGIFactory1(__uuidof(IDXGIFactory1), |
| 66 reinterpret_cast<void**>(factory.GetAddressOf()))); |
| 67 if (error.Error() != S_OK || !factory) { |
| 68 return std::vector<D3dDevice>(); |
| 69 } |
| 70 |
| 71 std::vector<D3dDevice> result; |
| 72 for (int i = 0;; i++) { |
| 73 ComPtr<IDXGIAdapter> adapter; |
| 74 error = _com_error(factory->EnumAdapters(i, adapter.GetAddressOf())); |
| 75 if (error.Error() == S_OK) { |
| 76 D3dDevice device; |
| 77 if (!device.Initialize(adapter)) { |
| 78 return std::vector<D3dDevice>(); |
| 79 } |
| 80 result.push_back(std::move(device)); |
| 81 } else if (error.Error() == DXGI_ERROR_NOT_FOUND) { |
| 82 break; |
| 83 } else { |
| 84 LOG(LS_WARNING) << "IDXGIFactory1::EnumAdapters returns an unexpected " |
| 85 "error " |
| 86 << error.ErrorMessage() << " with code " << error.Error(); |
| 87 return std::vector<D3dDevice>(); |
| 88 } |
| 89 } |
| 90 return result; |
| 91 } |
| 92 |
| 93 } // namespace webrtc |
OLD | NEW |