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/dxgi_adapter_duplicator.h" |
| 12 |
| 13 #include <comdef.h> |
| 14 #include <DXGI.h> |
| 15 |
| 16 #include <algorithm> |
| 17 |
| 18 #include "webrtc/base/logging.h" |
| 19 |
| 20 namespace webrtc { |
| 21 |
| 22 using Microsoft::WRL::ComPtr; |
| 23 |
| 24 namespace { |
| 25 |
| 26 bool IsValidRect(const RECT& rect) { |
| 27 return rect.left >= 0 && rect.top >= 0 && |
| 28 rect.right > rect.left && rect.bottom > rect.top; |
| 29 } |
| 30 |
| 31 } // namespace |
| 32 |
| 33 bool DxgiAdapterDuplicator::Initialize(const D3dDevice& device) { |
| 34 if (DoInitialize(device)) { |
| 35 return true; |
| 36 } |
| 37 duplicators_.clear(); |
| 38 return false; |
| 39 } |
| 40 |
| 41 bool DxgiAdapterDuplicator::DoInitialize(const D3dDevice& device) { |
| 42 for (int i = 0;; i++) { |
| 43 ComPtr<IDXGIOutput> output; |
| 44 _com_error error = device.dxgi_adapter()->EnumOutputs( |
| 45 i, output.GetAddressOf()); |
| 46 if (error.Error() == DXGI_ERROR_NOT_FOUND) { |
| 47 break; |
| 48 } |
| 49 |
| 50 if (error.Error() != S_OK || !output) { |
| 51 LOG(LS_WARNING) << "IDXGIAdapter::EnumOutputs returns an unexpected " |
| 52 "result " << error.ErrorMessage() << " with error code" |
| 53 << error.Error(); |
| 54 return false; |
| 55 } |
| 56 |
| 57 DXGI_OUTPUT_DESC desc; |
| 58 error = _com_error(output->GetDesc(&desc)); |
| 59 if (error.Error() == S_OK) { |
| 60 if (desc.AttachedToDesktop && IsValidRect(desc.DesktopCoordinates)) { |
| 61 ComPtr<IDXGIOutput1> output1; |
| 62 error = _com_error(output.As(&output1)); |
| 63 if (error.Error() != S_OK || !output1) { |
| 64 LOG(LS_WARNING) << "Failed to convert IDXGIOutput to IDXGIOutput1, " |
| 65 "this usually means the system does not support " |
| 66 "DirectX 11"; |
| 67 return false; |
| 68 } |
| 69 duplicators_.emplace_back(device, output1, desc); |
| 70 if (!duplicators_.back().Initialize()) { |
| 71 return false; |
| 72 } |
| 73 if (desktop_rect_.is_empty()) { |
| 74 desktop_rect_ = duplicators_.back().desktop_rect(); |
| 75 } else { |
| 76 const DesktopRect& left = desktop_rect_; |
| 77 const DesktopRect& right = duplicators_.back().desktop_rect(); |
| 78 desktop_rect_ = DesktopRect::MakeLTRB( |
| 79 std::min(left.left(), right.left()), |
| 80 std::min(left.top(), right.top()), |
| 81 std::max(left.right(), right.right()), |
| 82 std::max(left.bottom(), right.bottom())); |
| 83 } |
| 84 } |
| 85 } else { |
| 86 LOG(LS_WARNING) << "Failed to get output description of device " << i |
| 87 << ", ignore."; |
| 88 } |
| 89 } |
| 90 return true; |
| 91 } |
| 92 |
| 93 bool DxgiAdapterDuplicator::Duplicate(DesktopFrame* target, |
| 94 const DesktopFrame* last_frame) { |
| 95 for (auto& duplicator : duplicators_) { |
| 96 if (!duplicator.Duplicate( |
| 97 target, last_frame, duplicator.desktop_rect().top_left())) { |
| 98 return false; |
| 99 } |
| 100 } |
| 101 return true; |
| 102 } |
| 103 |
| 104 bool DxgiAdapterDuplicator::Duplicate(int id, |
| 105 DesktopFrame* target, |
| 106 const DesktopFrame* last_frame) { |
| 107 if (id < 0 || id >= static_cast<int>(duplicators_.size())) { |
| 108 return false; |
| 109 } |
| 110 return duplicators_[id].Duplicate(target, last_frame, DesktopVector()); |
| 111 } |
| 112 |
| 113 DesktopRect DxgiAdapterDuplicator::ScreenRect(int id) const { |
| 114 if (id < 0 || id >= static_cast<int>(duplicators_.size())) { |
| 115 return DesktopRect(); |
| 116 } |
| 117 return duplicators_[id].desktop_rect(); |
| 118 } |
| 119 |
| 120 } // namespace webrtc |
OLD | NEW |