| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. | 2 * Copyright (c) 2016 The WebRTC project authors. All Rights Reserved. |
| 3 * | 3 * |
| 4 * Use of this source code is governed by a BSD-style license | 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 | 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 | 6 * tree. An additional intellectual property rights grant can be found |
| 7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
| 8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
| 9 */ | 9 */ |
| 10 | 10 |
| 11 #include "webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.h" | 11 #include "webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.h" |
| 12 | 12 |
| 13 #include <windows.h> | 13 #include <windows.h> |
| 14 | 14 |
| 15 #include <algorithm> | 15 #include <algorithm> |
| 16 #include <string> | 16 #include <string> |
| 17 | 17 |
| 18 #include "webrtc/base/checks.h" | 18 #include "webrtc/base/checks.h" |
| 19 #include "webrtc/base/logging.h" |
| 19 #include "webrtc/base/timeutils.h" | 20 #include "webrtc/base/timeutils.h" |
| 20 #include "webrtc/modules/desktop_capture/desktop_capture_types.h" | 21 #include "webrtc/modules/desktop_capture/desktop_capture_types.h" |
| 21 #include "webrtc/modules/desktop_capture/win/dxgi_frame.h" | 22 #include "webrtc/modules/desktop_capture/win/dxgi_frame.h" |
| 22 #include "webrtc/modules/desktop_capture/win/screen_capture_utils.h" | 23 #include "webrtc/modules/desktop_capture/win/screen_capture_utils.h" |
| 23 #include "webrtc/system_wrappers/include/sleep.h" | 24 #include "webrtc/system_wrappers/include/sleep.h" |
| 24 | 25 |
| 25 namespace webrtc { | 26 namespace webrtc { |
| 26 | 27 |
| 27 // static | 28 // static |
| 28 DxgiDuplicatorController* DxgiDuplicatorController::Instance() { | 29 rtc::scoped_refptr<DxgiDuplicatorController> |
| 30 DxgiDuplicatorController::Instance() { |
| 29 // The static instance won't be deleted to ensure it can be used by other | 31 // The static instance won't be deleted to ensure it can be used by other |
| 30 // threads even during program exiting. | 32 // threads even during program exiting. |
| 31 static DxgiDuplicatorController* instance = new DxgiDuplicatorController(); | 33 static DxgiDuplicatorController* instance = new DxgiDuplicatorController(); |
| 32 return instance; | 34 return rtc::scoped_refptr<DxgiDuplicatorController>(instance); |
| 33 } | 35 } |
| 34 | 36 |
| 35 DxgiDuplicatorController::DxgiDuplicatorController() = default; | 37 DxgiDuplicatorController::DxgiDuplicatorController() |
| 38 : refcount_(0) {} |
| 36 | 39 |
| 37 DxgiDuplicatorController::~DxgiDuplicatorController() { | 40 void DxgiDuplicatorController::AddRef() { |
| 38 rtc::CritScope lock(&lock_); | 41 int refcount = (++refcount_); |
| 39 Deinitialize(); | 42 RTC_DCHECK(refcount > 0); |
| 43 } |
| 44 |
| 45 void DxgiDuplicatorController::Release() { |
| 46 int refcount = (--refcount_); |
| 47 RTC_DCHECK(refcount >= 0); |
| 48 if (refcount == 0) { |
| 49 LOG(LS_WARNING) << "Count of references reaches zero, " |
| 50 "DxgiDuplicatorController will be unloaded."; |
| 51 Unload(); |
| 52 } |
| 40 } | 53 } |
| 41 | 54 |
| 42 bool DxgiDuplicatorController::IsSupported() { | 55 bool DxgiDuplicatorController::IsSupported() { |
| 43 rtc::CritScope lock(&lock_); | 56 rtc::CritScope lock(&lock_); |
| 44 return Initialize(); | 57 return Initialize(); |
| 45 } | 58 } |
| 46 | 59 |
| 47 bool DxgiDuplicatorController::RetrieveD3dInfo(D3dInfo* info) { | 60 bool DxgiDuplicatorController::RetrieveD3dInfo(D3dInfo* info) { |
| 48 rtc::CritScope lock(&lock_); | 61 rtc::CritScope lock(&lock_); |
| 49 if (!Initialize()) { | 62 if (!Initialize()) { |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 122 // do not need to deinitialize. | 135 // do not need to deinitialize. |
| 123 return Result::INVALID_MONITOR_ID; | 136 return Result::INVALID_MONITOR_ID; |
| 124 } | 137 } |
| 125 | 138 |
| 126 // If the |monitor_id| is valid, but DoDuplicateUnlocked() failed, something | 139 // If the |monitor_id| is valid, but DoDuplicateUnlocked() failed, something |
| 127 // must be wrong from capturer APIs. We should Deinitialize(). | 140 // must be wrong from capturer APIs. We should Deinitialize(). |
| 128 Deinitialize(); | 141 Deinitialize(); |
| 129 return Result::DUPLICATION_FAILED; | 142 return Result::DUPLICATION_FAILED; |
| 130 } | 143 } |
| 131 | 144 |
| 145 void DxgiDuplicatorController::Unload() { |
| 146 rtc::CritScope lock(&lock_); |
| 147 Deinitialize(); |
| 148 } |
| 149 |
| 132 void DxgiDuplicatorController::Unregister(const Context* const context) { | 150 void DxgiDuplicatorController::Unregister(const Context* const context) { |
| 133 rtc::CritScope lock(&lock_); | 151 rtc::CritScope lock(&lock_); |
| 134 if (ContextExpired(context)) { | 152 if (ContextExpired(context)) { |
| 135 // The Context has not been setup after a recent initialization, so it | 153 // The Context has not been setup after a recent initialization, so it |
| 136 // should not been registered in duplicators. | 154 // should not been registered in duplicators. |
| 137 return; | 155 return; |
| 138 } | 156 } |
| 139 for (size_t i = 0; i < duplicators_.size(); i++) { | 157 for (size_t i = 0; i < duplicators_.size(); i++) { |
| 140 duplicators_[i].Unregister(&context->contexts[i]); | 158 duplicators_[i].Unregister(&context->contexts[i]); |
| 141 } | 159 } |
| (...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 void DxgiDuplicatorController::TranslateRect() { | 390 void DxgiDuplicatorController::TranslateRect() { |
| 373 const DesktopVector position = | 391 const DesktopVector position = |
| 374 DesktopVector().subtract(desktop_rect_.top_left()); | 392 DesktopVector().subtract(desktop_rect_.top_left()); |
| 375 desktop_rect_.Translate(position); | 393 desktop_rect_.Translate(position); |
| 376 for (auto& duplicator : duplicators_) { | 394 for (auto& duplicator : duplicators_) { |
| 377 duplicator.TranslateRect(position); | 395 duplicator.TranslateRect(position); |
| 378 } | 396 } |
| 379 } | 397 } |
| 380 | 398 |
| 381 } // namespace webrtc | 399 } // namespace webrtc |
| OLD | NEW |