xref: /aosp_15_r20/external/webrtc/modules/desktop_capture/win/d3d_device.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
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 "modules/desktop_capture/win/desktop_capture_utils.h"
16 #include "rtc_base/logging.h"
17 
18 namespace webrtc {
19 
20 using Microsoft::WRL::ComPtr;
21 
22 D3dDevice::D3dDevice() = default;
23 D3dDevice::D3dDevice(const D3dDevice& other) = default;
24 D3dDevice::D3dDevice(D3dDevice&& other) = default;
25 D3dDevice::~D3dDevice() = default;
26 
Initialize(const ComPtr<IDXGIAdapter> & adapter)27 bool D3dDevice::Initialize(const ComPtr<IDXGIAdapter>& adapter) {
28   dxgi_adapter_ = adapter;
29   if (!dxgi_adapter_) {
30     RTC_LOG(LS_WARNING) << "An empty IDXGIAdapter instance has been received.";
31     return false;
32   }
33 
34   D3D_FEATURE_LEVEL feature_level;
35   // Default feature levels contain D3D 9.1 through D3D 11.0.
36   _com_error error = D3D11CreateDevice(
37       adapter.Get(), D3D_DRIVER_TYPE_UNKNOWN, nullptr,
38       D3D11_CREATE_DEVICE_BGRA_SUPPORT | D3D11_CREATE_DEVICE_SINGLETHREADED,
39       nullptr, 0, D3D11_SDK_VERSION, d3d_device_.GetAddressOf(), &feature_level,
40       context_.GetAddressOf());
41   if (error.Error() != S_OK || !d3d_device_ || !context_) {
42     RTC_LOG(LS_WARNING) << "D3D11CreateDevice returned: "
43                         << desktop_capture::utils::ComErrorToString(error);
44     return false;
45   }
46 
47   if (feature_level < D3D_FEATURE_LEVEL_11_0) {
48     RTC_LOG(LS_WARNING)
49         << "D3D11CreateDevice returned an instance without DirectX 11 support, "
50         << "level " << feature_level << ". Following initialization may fail.";
51     // D3D_FEATURE_LEVEL_11_0 is not officially documented on MSDN to be a
52     // requirement of Dxgi duplicator APIs.
53   }
54 
55   error = d3d_device_.As(&dxgi_device_);
56   if (error.Error() != S_OK || !dxgi_device_) {
57     RTC_LOG(LS_WARNING)
58         << "ID3D11Device is not an implementation of IDXGIDevice, "
59         << "this usually means the system does not support DirectX "
60         << "11. Error received: "
61         << desktop_capture::utils::ComErrorToString(error);
62     return false;
63   }
64 
65   return true;
66 }
67 
68 // static
EnumDevices()69 std::vector<D3dDevice> D3dDevice::EnumDevices() {
70   ComPtr<IDXGIFactory1> factory;
71   _com_error error =
72       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                         << desktop_capture::utils::ComErrorToString(error);
77     return std::vector<D3dDevice>();
78   }
79 
80   std::vector<D3dDevice> result;
81   for (int i = 0;; i++) {
82     ComPtr<IDXGIAdapter> adapter;
83     error = factory->EnumAdapters(i, adapter.GetAddressOf());
84     if (error.Error() == S_OK) {
85       D3dDevice device;
86       if (device.Initialize(adapter)) {
87         result.push_back(std::move(device));
88       }
89     } else if (error.Error() == DXGI_ERROR_NOT_FOUND) {
90       break;
91     } else {
92       RTC_LOG(LS_WARNING)
93           << "IDXGIFactory1::EnumAdapters returned an unexpected error: "
94           << desktop_capture::utils::ComErrorToString(error);
95     }
96   }
97   return result;
98 }
99 
100 }  // namespace webrtc
101