OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include "webrtc/modules/desktop_capture/win/d3d_device.h" | 11 #include "webrtc/modules/desktop_capture/win/d3d_device.h" |
12 | 12 |
13 #include <utility> | 13 #include <utility> |
14 | 14 |
15 #include "webrtc/system_wrappers/include/logging.h" | |
16 | |
17 namespace webrtc { | 15 namespace webrtc { |
18 | 16 |
19 using Microsoft::WRL::ComPtr; | 17 using Microsoft::WRL::ComPtr; |
20 | 18 |
21 D3dDevice::D3dDevice() = default; | 19 D3dDevice::D3dDevice() = default; |
22 D3dDevice::D3dDevice(const D3dDevice& other) = default; | 20 D3dDevice::D3dDevice(const D3dDevice& other) = default; |
23 D3dDevice::D3dDevice(D3dDevice&& other) = default; | 21 D3dDevice::D3dDevice(D3dDevice&& other) = default; |
24 D3dDevice::~D3dDevice() = default; | 22 D3dDevice::~D3dDevice() = default; |
25 | 23 |
26 bool D3dDevice::Initialize(const ComPtr<IDXGIAdapter>& adapter) { | 24 bool D3dDevice::Initialize(const ComPtr<IDXGIAdapter>& adapter) { |
27 dxgi_adapter_ = adapter; | 25 dxgi_adapter_ = adapter; |
28 if (!dxgi_adapter_) { | 26 if (!dxgi_adapter_) { |
29 LOG(LS_WARNING) << "An empty IDXGIAdapter instance has been received."; | |
30 return false; | 27 return false; |
31 } | 28 } |
32 | 29 |
33 D3D_FEATURE_LEVEL feature_level; | 30 D3D_FEATURE_LEVEL feature_level; |
34 // Default feature levels contain D3D 9.1 through D3D 11.0. | 31 // Default feature levels contain D3D 9.1 through D3D 11.0. |
35 _com_error error = D3D11CreateDevice( | 32 _com_error error = D3D11CreateDevice( |
36 adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, | 33 adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr, |
37 D3D11_CREATE_DEVICE_BGRA_SUPPORT | D3D11_CREATE_DEVICE_SINGLETHREADED, | 34 D3D11_CREATE_DEVICE_BGRA_SUPPORT | D3D11_CREATE_DEVICE_SINGLETHREADED, |
38 nullptr, 0, D3D11_SDK_VERSION, d3d_device_.GetAddressOf(), &feature_level, | 35 nullptr, 0, D3D11_SDK_VERSION, d3d_device_.GetAddressOf(), &feature_level, |
39 context_.GetAddressOf()); | 36 context_.GetAddressOf()); |
40 if (error.Error() != S_OK || !d3d_device_ || !context_) { | 37 if (error.Error() != S_OK || !d3d_device_ || !context_) { |
41 LOG(LS_WARNING) << "D3D11CreateDeivce returns error " | |
42 << error.ErrorMessage() << " with code " << error.Error(); | |
43 return false; | 38 return false; |
44 } | 39 } |
45 | 40 |
46 if (feature_level < D3D_FEATURE_LEVEL_11_0) { | 41 if (feature_level < D3D_FEATURE_LEVEL_11_0) { |
47 LOG(LS_WARNING) << "D3D11CreateDevice returns an instance without DirectX " | |
48 "11 support, level " << feature_level | |
49 << ". Following initialization may fail."; | |
50 // D3D_FEATURE_LEVEL_11_0 is not officially documented on MSDN to be a | 42 // D3D_FEATURE_LEVEL_11_0 is not officially documented on MSDN to be a |
51 // requirement of Dxgi duplicator APIs. | 43 // requirement of Dxgi duplicator APIs. |
52 } | 44 } |
53 | 45 |
54 error = d3d_device_.As(&dxgi_device_); | 46 error = d3d_device_.As(&dxgi_device_); |
55 if (error.Error() != S_OK || !dxgi_device_) { | 47 if (error.Error() != S_OK || !dxgi_device_) { |
56 LOG(LS_WARNING) << "ID3D11Device is not an implementation of IDXGIDevice, " | |
57 "this usually means the system does not support DirectX " | |
58 "11. Error " | |
59 << error.ErrorMessage() << " with code " << error.Error(); | |
60 return false; | 48 return false; |
61 } | 49 } |
62 | 50 |
63 return true; | 51 return true; |
64 } | 52 } |
65 | 53 |
66 // static | 54 // static |
67 std::vector<D3dDevice> D3dDevice::EnumDevices() { | 55 std::vector<D3dDevice> D3dDevice::EnumDevices() { |
68 ComPtr<IDXGIFactory1> factory; | 56 ComPtr<IDXGIFactory1> factory; |
69 _com_error error = CreateDXGIFactory1(__uuidof(IDXGIFactory1), | 57 _com_error error = CreateDXGIFactory1(__uuidof(IDXGIFactory1), |
70 reinterpret_cast<void**>(factory.GetAddressOf())); | 58 reinterpret_cast<void**>(factory.GetAddressOf())); |
71 if (error.Error() != S_OK || !factory) { | 59 if (error.Error() != S_OK || !factory) { |
72 return std::vector<D3dDevice>(); | 60 return std::vector<D3dDevice>(); |
73 } | 61 } |
74 | 62 |
75 std::vector<D3dDevice> result; | 63 std::vector<D3dDevice> result; |
76 for (int i = 0;; i++) { | 64 for (int i = 0;; i++) { |
77 ComPtr<IDXGIAdapter> adapter; | 65 ComPtr<IDXGIAdapter> adapter; |
78 error = factory->EnumAdapters(i, adapter.GetAddressOf()); | 66 error = factory->EnumAdapters(i, adapter.GetAddressOf()); |
79 if (error.Error() == S_OK) { | 67 if (error.Error() == S_OK) { |
80 D3dDevice device; | 68 D3dDevice device; |
81 if (!device.Initialize(adapter)) { | 69 if (!device.Initialize(adapter)) { |
82 return std::vector<D3dDevice>(); | 70 return std::vector<D3dDevice>(); |
83 } | 71 } |
84 result.push_back(std::move(device)); | 72 result.push_back(std::move(device)); |
85 } else if (error.Error() == DXGI_ERROR_NOT_FOUND) { | 73 } else if (error.Error() == DXGI_ERROR_NOT_FOUND) { |
86 break; | 74 break; |
87 } else { | 75 } else { |
88 LOG(LS_WARNING) << "IDXGIFactory1::EnumAdapters returns an unexpected " | |
89 "error " | |
90 << error.ErrorMessage() << " with code " << error.Error(); | |
91 return std::vector<D3dDevice>(); | 76 return std::vector<D3dDevice>(); |
92 } | 77 } |
93 } | 78 } |
94 return result; | 79 return result; |
95 } | 80 } |
96 | 81 |
97 } // namespace webrtc | 82 } // namespace webrtc |
OLD | NEW |