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 "modules/desktop_capture/win/d3d_device.h"
12 
13 #include <utility>
14 
15 #include "rtc_base/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     RTC_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     RTC_LOG(LS_WARNING) << "D3D11CreateDeivce returns error "
42                         << error.ErrorMessage() << " with code "
43                         << error.Error();
44     return false;
45   }
46 
47   if (feature_level < D3D_FEATURE_LEVEL_11_0) {
48     RTC_LOG(LS_WARNING)
49         << "D3D11CreateDevice returns an instance without DirectX "
50            "11 support, level "
51         << feature_level << ". Following initialization may fail.";
52     // D3D_FEATURE_LEVEL_11_0 is not officially documented on MSDN to be a
53     // requirement of Dxgi duplicator APIs.
54   }
55 
56   error = d3d_device_.As(&dxgi_device_);
57   if (error.Error() != S_OK || !dxgi_device_) {
58     RTC_LOG(LS_WARNING)
59         << "ID3D11Device is not an implementation of IDXGIDevice, "
60            "this usually means the system does not support DirectX "
61            "11. Error "
62         << error.ErrorMessage() << " with code " << error.Error();
63     return false;
64   }
65 
66   return true;
67 }
68 
69 // static
EnumDevices()70 std::vector<D3dDevice> D3dDevice::EnumDevices() {
71   ComPtr<IDXGIFactory1> factory;
72   _com_error error = CreateDXGIFactory1(__uuidof(IDXGIFactory1),
73       reinterpret_cast<void**>(factory.GetAddressOf()));
74   if (error.Error() != S_OK || !factory) {
75     RTC_LOG(LS_WARNING) << "Cannot create IDXGIFactory1.";
76     return std::vector<D3dDevice>();
77   }
78 
79   std::vector<D3dDevice> result;
80   for (int i = 0;; i++) {
81     ComPtr<IDXGIAdapter> adapter;
82     error = factory->EnumAdapters(i, adapter.GetAddressOf());
83     if (error.Error() == S_OK) {
84       D3dDevice device;
85       if (device.Initialize(adapter)) {
86         result.push_back(std::move(device));
87       }
88     } else if (error.Error() == DXGI_ERROR_NOT_FOUND) {
89       break;
90     } else {
91       RTC_LOG(LS_WARNING)
92           << "IDXGIFactory1::EnumAdapters returns an unexpected "
93              "error "
94           << error.ErrorMessage() << " with code " << error.Error();
95     }
96   }
97   return result;
98 }
99 
100 }  // namespace webrtc
101