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(const D3dDevice& other) = default;
23 D3dDevice::D3dDevice(D3dDevice&& other) = default;
24 D3dDevice::~D3dDevice() = default;
25 
Initialize(const ComPtr<IDXGIAdapter> & adapter)26 bool D3dDevice::Initialize(const ComPtr<IDXGIAdapter>& adapter) {
27   dxgi_adapter_ = adapter;
28   if (!dxgi_adapter_) {
29     LOG(LS_WARNING) << "An empty IDXGIAdapter instance has been received.";
30     return false;
31   }
32 
33   D3D_FEATURE_LEVEL feature_level;
34   // Default feature levels contain D3D 9.1 through D3D 11.0.
35   _com_error error = D3D11CreateDevice(
36       adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr,
37       D3D11_CREATE_DEVICE_BGRA_SUPPORT | D3D11_CREATE_DEVICE_SINGLETHREADED,
38       nullptr, 0, D3D11_SDK_VERSION, d3d_device_.GetAddressOf(), &feature_level,
39       context_.GetAddressOf());
40   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;
44   }
45 
46   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
51     // requirement of Dxgi duplicator APIs.
52   }
53 
54   error = d3d_device_.As(&dxgi_device_);
55   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;
61   }
62 
63   return true;
64 }
65 
66 // static
EnumDevices()67 std::vector<D3dDevice> D3dDevice::EnumDevices() {
68   ComPtr<IDXGIFactory1> factory;
69   _com_error error = CreateDXGIFactory1(__uuidof(IDXGIFactory1),
70       reinterpret_cast<void**>(factory.GetAddressOf()));
71   if (error.Error() != S_OK || !factory) {
72     return std::vector<D3dDevice>();
73   }
74 
75   std::vector<D3dDevice> result;
76   for (int i = 0;; i++) {
77     ComPtr<IDXGIAdapter> adapter;
78     error = factory->EnumAdapters(i, adapter.GetAddressOf());
79     if (error.Error() == S_OK) {
80       D3dDevice device;
81       if (!device.Initialize(adapter)) {
82         return std::vector<D3dDevice>();
83       }
84       result.push_back(std::move(device));
85     } else if (error.Error() == DXGI_ERROR_NOT_FOUND) {
86       break;
87     } else {
88       LOG(LS_WARNING) << "IDXGIFactory1::EnumAdapters returns an unexpected "
89                          "error "
90                       << error.ErrorMessage() << " with code " << error.Error();
91       return std::vector<D3dDevice>();
92     }
93   }
94   return result;
95 }
96 
97 }  // namespace webrtc
98