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 |
| 29 std::atomic_int DxgiDuplicatorController::refcount_(0); |
| 30 |
| 31 // static |
28 DxgiDuplicatorController* DxgiDuplicatorController::Instance() { | 32 DxgiDuplicatorController* DxgiDuplicatorController::Instance() { |
29 // The static instance won't be deleted to ensure it can be used by other | 33 // The static instance won't be deleted to ensure it can be used by other |
30 // threads even during program exiting. | 34 // threads even during program exiting. |
31 static DxgiDuplicatorController* instance = new DxgiDuplicatorController(); | 35 static DxgiDuplicatorController* instance = new DxgiDuplicatorController(); |
32 return instance; | 36 return instance; |
33 } | 37 } |
34 | 38 |
| 39 // static |
| 40 DxgiDuplicatorController::Reference DxgiDuplicatorController::GetReference() { |
| 41 Reference result(Instance()); |
| 42 int refcount = (++refcount_); |
| 43 RTC_DCHECK(refcount > 0); |
| 44 return result; |
| 45 } |
| 46 |
| 47 // static |
| 48 void DxgiDuplicatorController::ReferenceReleaser::operator()( |
| 49 DxgiDuplicatorController* instance) const { |
| 50 int refcount = (--refcount_); |
| 51 RTC_DCHECK(refcount >= 0); |
| 52 if (refcount == 0) { |
| 53 LOG(LS_WARNING) << "Count of references reaches zero, " |
| 54 "DxgiDuplicatorController will be unloaded."; |
| 55 instance->Unload(); |
| 56 } |
| 57 } |
| 58 |
35 DxgiDuplicatorController::DxgiDuplicatorController() = default; | 59 DxgiDuplicatorController::DxgiDuplicatorController() = default; |
36 | 60 |
37 DxgiDuplicatorController::~DxgiDuplicatorController() { | |
38 rtc::CritScope lock(&lock_); | |
39 Deinitialize(); | |
40 } | |
41 | |
42 bool DxgiDuplicatorController::IsSupported() { | 61 bool DxgiDuplicatorController::IsSupported() { |
43 rtc::CritScope lock(&lock_); | 62 rtc::CritScope lock(&lock_); |
44 return Initialize(); | 63 return Initialize(); |
45 } | 64 } |
46 | 65 |
47 bool DxgiDuplicatorController::RetrieveD3dInfo(D3dInfo* info) { | 66 bool DxgiDuplicatorController::RetrieveD3dInfo(D3dInfo* info) { |
48 rtc::CritScope lock(&lock_); | 67 rtc::CritScope lock(&lock_); |
49 if (!Initialize()) { | 68 if (!Initialize()) { |
50 return false; | 69 return false; |
51 } | 70 } |
(...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
122 // do not need to deinitialize. | 141 // do not need to deinitialize. |
123 return Result::INVALID_MONITOR_ID; | 142 return Result::INVALID_MONITOR_ID; |
124 } | 143 } |
125 | 144 |
126 // If the |monitor_id| is valid, but DoDuplicateUnlocked() failed, something | 145 // If the |monitor_id| is valid, but DoDuplicateUnlocked() failed, something |
127 // must be wrong from capturer APIs. We should Deinitialize(). | 146 // must be wrong from capturer APIs. We should Deinitialize(). |
128 Deinitialize(); | 147 Deinitialize(); |
129 return Result::DUPLICATION_FAILED; | 148 return Result::DUPLICATION_FAILED; |
130 } | 149 } |
131 | 150 |
| 151 void DxgiDuplicatorController::Unload() { |
| 152 rtc::CritScope lock(&lock_); |
| 153 Deinitialize(); |
| 154 } |
| 155 |
132 void DxgiDuplicatorController::Unregister(const Context* const context) { | 156 void DxgiDuplicatorController::Unregister(const Context* const context) { |
133 rtc::CritScope lock(&lock_); | 157 rtc::CritScope lock(&lock_); |
134 if (ContextExpired(context)) { | 158 if (ContextExpired(context)) { |
135 // The Context has not been setup after a recent initialization, so it | 159 // The Context has not been setup after a recent initialization, so it |
136 // should not been registered in duplicators. | 160 // should not been registered in duplicators. |
137 return; | 161 return; |
138 } | 162 } |
139 for (size_t i = 0; i < duplicators_.size(); i++) { | 163 for (size_t i = 0; i < duplicators_.size(); i++) { |
140 duplicators_[i].Unregister(&context->contexts[i]); | 164 duplicators_[i].Unregister(&context->contexts[i]); |
141 } | 165 } |
(...skipping 230 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
372 void DxgiDuplicatorController::TranslateRect() { | 396 void DxgiDuplicatorController::TranslateRect() { |
373 const DesktopVector position = | 397 const DesktopVector position = |
374 DesktopVector().subtract(desktop_rect_.top_left()); | 398 DesktopVector().subtract(desktop_rect_.top_left()); |
375 desktop_rect_.Translate(position); | 399 desktop_rect_.Translate(position); |
376 for (auto& duplicator : duplicators_) { | 400 for (auto& duplicator : duplicators_) { |
377 duplicator.TranslateRect(position); | 401 duplicator.TranslateRect(position); |
378 } | 402 } |
379 } | 403 } |
380 | 404 |
381 } // namespace webrtc | 405 } // namespace webrtc |
OLD | NEW |