Chromium Code Reviews| Index: webrtc/modules/desktop_capture/win/dxgi_duplicator_container.cc |
| diff --git a/webrtc/modules/desktop_capture/win/dxgi_duplicator_container.cc b/webrtc/modules/desktop_capture/win/dxgi_duplicator_container.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..cacbba03c1d8cb2e76f5f86c75136155a3528ac9 |
| --- /dev/null |
| +++ b/webrtc/modules/desktop_capture/win/dxgi_duplicator_container.cc |
| @@ -0,0 +1,206 @@ |
| +/* |
| + * 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_duplicator_container.h" |
| + |
| +#include <windows.h> |
| + |
| +#include "webrtc/base/checks.h" |
| + |
| +namespace webrtc { |
| + |
| +bool DxgiDuplicatorContainer::SingleEntry::Enter() { |
|
Sergey Ulanov
2016/07/08 22:36:54
I don't think you really need SingleEntry.
Hzj_jie
2016/07/11 00:55:00
Same as below.
|
| + rtc::CritScope lock(&lock_); |
| + if (entered_) { |
| + return false; |
| + } |
| + entered_ = true; |
| + return true; |
| +} |
| + |
| +void DxgiDuplicatorContainer::SingleEntry::Exit() { |
| + rtc::CritScope lock(&lock_); |
| + RTC_DCHECK(entered_); |
| + entered_ = false; |
| +} |
| + |
| +bool DxgiDuplicatorContainer::SingleEntry::Hold() const { |
| + rtc::CritScope lock(&lock_); |
| + return entered_; |
| +} |
| + |
| +class DxgiDuplicatorContainer::AutoExit { |
| + public: |
| + explicit AutoExit(DxgiDuplicatorContainer::SingleEntry* gate) : gate_(gate) { |
| + RTC_DCHECK(gate_); |
| + } |
| + |
| + ~AutoExit() { gate_->Exit(); } |
| + |
| + private: |
| + DxgiDuplicatorContainer::SingleEntry* gate_; |
| +}; |
| + |
| +/* static */ DxgiDuplicatorContainer* DxgiDuplicatorContainer::Instance() { |
|
Sergey Ulanov
2016/07/08 22:36:54
Move the comment to a separate line:
// static
Dx
Hzj_jie
2016/07/11 00:55:00
Done.
|
| + // The static instance won't be deleted to ensure it can be used by other |
| + // threads even during program exiting. |
| + static DxgiDuplicatorContainer* instance = new DxgiDuplicatorContainer(); |
| + return instance; |
| +} |
| + |
| +DxgiDuplicatorContainer::~DxgiDuplicatorContainer() { |
| + rtc::CritScope lock(&lock_); |
| + Deinitialize(); |
| +} |
| + |
| +bool DxgiDuplicatorContainer::Prepare() { |
| + // If the instance has been initialized already, does nothing and returns |
| + // true. |
| + { |
| + rtc::CritScope lock(&lock_); |
| + if (!devices_.empty()) { |
| + return true; |
| + } |
| + } |
| + |
| + if (initializing_.Enter()) { |
|
Sergey Ulanov
2016/07/08 22:36:54
I think you can implement the same thing without S
Hzj_jie
2016/07/11 00:55:00
Hold on, this logic won't correct.
If the Event is
|
| + AutoExit exit(&initializing_); |
| + rtc::CritScope lock(&lock_); |
| + if (!devices_.empty()) { |
| + // Another thread has initialized this instance before this thread reaches |
| + // line initializing_.Enter(), so does nothing and returns true. |
|
Sergey Ulanov
2016/07/08 22:36:55
How is that possible?
Hzj_jie
2016/07/11 00:55:00
Another thread has called initializing_.Exit befor
|
| + return true; |
| + } |
| + |
| + if (DoInitialize()) { |
| + return true; |
| + } |
| + |
| + Deinitialize(); |
| + return false; |
| + } else { |
| + // Some other thread is initializing, wait for its finish and check the |
| + // result. |
| + while (initializing_.Hold()) { |
| + rtc::CritScope lock(&lock_); |
| + } |
| + return !devices_.empty(); |
|
Sergey Ulanov
2016/07/08 22:36:54
Here the case when initialization has failed and w
Sergey Ulanov
2016/07/08 22:36:54
don't you need to hold the lock_ when verifying th
Hzj_jie
2016/07/11 00:55:00
If there is no device for any reason, DxgiDuplicat
Hzj_jie
2016/07/11 00:55:00
Yes, I missed.
|
| + } |
| +} |
| + |
| +void DxgiDuplicatorContainer::Deinitialize() { |
| + desktop_rect_ = DesktopRect(); |
| + duplicators_.clear(); |
| + devices_.clear(); |
| +} |
| + |
| +bool DxgiDuplicatorContainer::DoInitialize() { |
| + RTC_DCHECK(desktop_rect_.is_empty()); |
| + RTC_DCHECK(duplicators_.empty()); |
| + RTC_DCHECK(devices_.empty()); |
| + |
| + devices_ = D3dDevice::EnumDevices(); |
|
Sergey Ulanov
2016/07/08 22:36:54
We enumerate devices only once here, but they can
Hzj_jie
2016/07/11 00:55:00
Yes, that's the reason we call Prepare each time b
|
| + if (devices_.empty()) { |
| + return false; |
| + } |
| + |
| + for (size_t i = 0; i < devices_.size(); i++) { |
| + duplicators_.emplace_back(); |
| + if (!duplicators_.back().Initialize(devices_[i])) { |
| + return false; |
| + } |
| + if (desktop_rect_.is_empty()) { |
| + desktop_rect_ = duplicators_.back().desktop_rect(); |
| + } else { |
| + desktop_rect_.JoinWith(duplicators_.back().desktop_rect()); |
| + } |
| + } |
| + |
| + HDC hdc = GetDC(nullptr); |
| + // Use old DPI value if failed. |
| + if (hdc != nullptr) { |
| + dpi_.set(GetDeviceCaps(hdc, LOGPIXELSX), GetDeviceCaps(hdc, LOGPIXELSY)); |
|
Sergey Ulanov
2016/07/08 22:36:55
dpi can change dynamically, but this class won't d
Hzj_jie
2016/07/11 00:55:00
Same as above, we will reset the dpi_ once display
|
| + ReleaseDC(nullptr, hdc); |
| + } |
| + |
| + return true; |
| +} |
| + |
| +DesktopRect DxgiDuplicatorContainer::ScreenRect(int id) const { |
| + if (id < 0) { |
|
Sergey Ulanov
2016/07/08 22:36:54
DCHECK(id >= 0)
Hzj_jie
2016/07/11 00:55:00
Done.
|
| + return DesktopRect(); |
| + } |
| + rtc::CritScope lock(&lock_); |
| + for (size_t i = 0; i < duplicators_.size(); i++) { |
| + if (id >= duplicators_[i].screen_count()) { |
| + id -= duplicators_[i].screen_count(); |
| + } else { |
| + return duplicators_[i].ScreenRect(id); |
| + } |
| + } |
| + return DesktopRect(); |
| +} |
| + |
| +int DxgiDuplicatorContainer::ScreenCount() const { |
| + rtc::CritScope lock(&lock_); |
| + int result = 0; |
| + for (size_t i = 0; i < duplicators_.size(); i++) { |
| + result += duplicators_[i].screen_count(); |
| + } |
| + return result; |
| +} |
| + |
| +bool DxgiDuplicatorContainer::Duplicate(DesktopFrame* target, |
| + const DesktopFrame* last_frame) { |
| + RTC_DCHECK(target); |
| + if (last_frame != nullptr && !target->size().equals(last_frame->size())) { |
| + return false; |
| + } |
| + target->mutable_updated_region()->Clear(); |
| + rtc::CritScope lock(&lock_); |
| + for (size_t i = 0; i < duplicators_.size(); i++) { |
| + if (!duplicators_[i].Duplicate(target, last_frame)) { |
| + Deinitialize(); |
| + return false; |
| + } |
| + } |
| + target->set_dpi(dpi()); |
| + return true; |
| +} |
| + |
| +bool DxgiDuplicatorContainer::Duplicate(int id, |
| + DesktopFrame* target, |
| + const DesktopFrame* last_frame) { |
| + RTC_DCHECK(target); |
| + if (id < 0) { |
| + return false; |
| + } |
| + if (last_frame != nullptr && !target->size().equals(last_frame->size())) { |
| + return false; |
| + } |
| + target->mutable_updated_region()->Clear(); |
| + rtc::CritScope lock(&lock_); |
| + for (size_t i = 0; i < duplicators_.size(); i++) { |
| + if (id >= duplicators_[i].screen_count()) { |
| + id -= duplicators_[i].screen_count(); |
| + } else { |
| + if (duplicators_[i].Duplicate(id, target, last_frame)) { |
| + target->set_dpi(dpi()); |
| + return true; |
| + } |
| + Deinitialize(); |
| + return false; |
| + } |
| + } |
| + // id >= ScreenCount() |
| + return false; |
| +} |
| + |
| +} // namespace webrtc |