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