Chromium Code Reviews| Index: webrtc/modules/desktop_capture/win/dxgi_adapter_duplicator.cc |
| diff --git a/webrtc/modules/desktop_capture/win/dxgi_adapter_duplicator.cc b/webrtc/modules/desktop_capture/win/dxgi_adapter_duplicator.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..61cb0f8b0b107decc0d543b5d8202f380354141e |
| --- /dev/null |
| +++ b/webrtc/modules/desktop_capture/win/dxgi_adapter_duplicator.cc |
| @@ -0,0 +1,104 @@ |
| +/* |
| + * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| + * |
| + * Use of this source code is governed by a BSD-style license |
| + * that can be found in the LICENSE file in the root of the source |
| + * tree. An additional intellectual property rights grant can be found |
| + * in the file PATENTS. All contributing project authors may |
| + * be found in the AUTHORS file in the root of the source tree. |
| + */ |
| + |
| +#include "webrtc/modules/desktop_capture/win/dxgi_adapter_duplicator.h" |
| + |
| +#include <comdef.h> |
| +#include <DXGI.h> |
| + |
| +#include "webrtc/base/logging.h" |
| + |
| +namespace webrtc { |
| + |
| +using Microsoft::WRL::ComPtr; |
| + |
| +namespace { |
| + |
| +bool IsValidRect(const RECT& rect) { |
| + return rect.left >= 0 && rect.top >= 0 && |
| + rect.right > rect.left && rect.bottom > rect.top; |
| +} |
| + |
| +} // namespace |
| + |
| +bool DxgiAdapterDuplicator::Initialize(const D3dDevice& device) { |
| + if (DoInitialize(device)) { |
| + return true; |
| + } |
| + duplicators_.clear(); |
| + return false; |
| +} |
| + |
| +bool DxgiAdapterDuplicator::DoInitialize(const D3dDevice& device) { |
| + for (int i = 0;; i++) { |
| + ComPtr<IDXGIOutput> output; |
| + _com_error error = device.dxgi_adapter()->EnumOutputs( |
| + i, output.GetAddressOf()); |
| + if (error.Error() == S_OK && output) { |
| + DXGI_OUTPUT_DESC desc; |
| + error = _com_error(output->GetDesc(&desc)); |
| + if (error.Error() == S_OK) { |
| + if (desc.AttachedToDesktop && IsValidRect(desc.DesktopCoordinates)) { |
| + ComPtr<IDXGIOutput1> output1; |
| + error = _com_error(output.As(&output1)); |
| + if (error.Error() != S_OK || !output1) { |
| + LOG(LS_WARNING) << "Failed to convert IDXGIOutput to IDXGIOutput1, " |
| + "this usually means the system does not support " |
| + "DirectX 11"; |
| + return false; |
| + } |
| + duplicators_.emplace_back(device, output1, desc); |
| + if (!duplicators_.back().Initialize()) { |
| + return false; |
| + } |
| + if (desktop_rect_.is_empty()) { |
| + desktop_rect_ = duplicators_.back().desktop_rect(); |
| + } else { |
| + desktop_rect_.JoinWith(duplicators_.back().desktop_rect()); |
| + } |
| + } |
| + } else { |
| + LOG(LS_WARNING) << "Failed to get output description of device " << i |
| + << ", ignore."; |
| + } |
| + } else if (error.Error() == DXGI_ERROR_NOT_FOUND) { |
| + return true; |
|
Sergey Ulanov
2016/07/08 22:36:54
break; and return true after the loop
Hzj_jie
2016/07/11 00:54:59
Done.
|
| + } else { |
| + return false; |
|
Sergey Ulanov
2016/07/08 22:36:53
handle error cases first:
if (error.Error() == DX
Hzj_jie
2016/07/11 00:54:59
Done.
|
| + } |
| + } |
| +} |
| + |
| +bool DxgiAdapterDuplicator::Duplicate( |
| + DesktopFrame* target, const DesktopFrame* last_frame) { |
|
Sergey Ulanov
2016/07/08 22:36:53
one argument per line, please
(clang-format should
Hzj_jie
2016/07/11 00:54:59
Done.
|
| + for (size_t i = 0; i < duplicators_.size(); i++) { |
|
Sergey Ulanov
2016/07/08 22:36:53
for (auto& duplicator: duplicators_)...
Hzj_jie
2016/07/11 00:54:59
Oh, I thought for each and auto& are both discoura
Sergey Ulanov
2016/07/20 19:05:17
Nope, see https://chromium-cpp.appspot.com/
auto s
Hzj_jie
2016/07/22 18:43:43
Got it, I have updated both this file and DxgiDupl
|
| + if (!duplicators_[i].Duplicate(target, last_frame)) { |
| + return false; |
| + } |
| + } |
| + return true; |
| +} |
| + |
| +bool DxgiAdapterDuplicator::Duplicate( |
| + int id, DesktopFrame* target, const DesktopFrame* last_frame) { |
|
Sergey Ulanov
2016/07/08 22:36:53
one argument per line, please
(clang-format should
Hzj_jie
2016/07/11 00:54:59
Done.
|
| + if (id < 0 || id >= static_cast<int>(duplicators_.size())) { |
| + return false; |
| + } |
| + return duplicators_[id].Duplicate(target, last_frame, DesktopVector()); |
| +} |
| + |
| +DesktopRect DxgiAdapterDuplicator::ScreenRect(int id) const { |
| + if (id < 0 || id >= static_cast<int>(duplicators_.size())) { |
| + return DesktopRect(); |
| + } |
| + return duplicators_[id].desktop_rect(); |
| +} |
| + |
| +} // namespace webrtc |