| 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 | 16 |
| 17 #include "webrtc/base/checks.h" | 17 #include "webrtc/base/checks.h" |
| 18 #include "webrtc/modules/desktop_capture/desktop_capture_types.h" |
| 19 #include "webrtc/modules/desktop_capture/win/screen_capture_utils.h" |
| 18 | 20 |
| 19 namespace webrtc { | 21 namespace webrtc { |
| 20 | 22 |
| 21 DxgiDuplicatorController::Context::Context() {} | 23 DxgiDuplicatorController::Context::Context() {} |
| 22 | 24 |
| 23 DxgiDuplicatorController::Context::~Context() { | 25 DxgiDuplicatorController::Context::~Context() { |
| 24 DxgiDuplicatorController::Instance()->Unregister(this); | 26 DxgiDuplicatorController::Instance()->Unregister(this); |
| 25 } | 27 } |
| 26 | 28 |
| 27 // static | 29 // static |
| 28 DxgiDuplicatorController* DxgiDuplicatorController::Instance() { | 30 DxgiDuplicatorController* 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 instance; |
| 33 } | 35 } |
| 34 | 36 |
| 35 DxgiDuplicatorController::DxgiDuplicatorController() = default; | 37 DxgiDuplicatorController::DxgiDuplicatorController() = default; |
| 36 | 38 |
| 37 DxgiDuplicatorController::~DxgiDuplicatorController() { | 39 DxgiDuplicatorController::~DxgiDuplicatorController() { |
| 38 rtc::CritScope lock(&lock_); | 40 rtc::CritScope lock(&lock_); |
| 39 Deinitialize(); | 41 Deinitialize(); |
| 40 } | 42 } |
| 41 | 43 |
| 42 bool DxgiDuplicatorController::IsSupported() { | 44 bool DxgiDuplicatorController::IsSupported() { |
| 43 rtc::CritScope lock(&lock_); | 45 rtc::CritScope lock(&lock_); |
| 44 return Initialize(); | 46 return Initialize(); |
| 45 } | 47 } |
| 46 | 48 |
| 49 void DxgiDuplicatorController::Reset() { |
| 50 rtc::CritScope lock(&lock_); |
| 51 Deinitialize(); |
| 52 } |
| 53 |
| 47 bool DxgiDuplicatorController::RetrieveD3dInfo(D3dInfo* info) { | 54 bool DxgiDuplicatorController::RetrieveD3dInfo(D3dInfo* info) { |
| 48 rtc::CritScope lock(&lock_); | 55 rtc::CritScope lock(&lock_); |
| 49 if (!Initialize()) { | 56 if (!Initialize()) { |
| 50 return false; | 57 return false; |
| 51 } | 58 } |
| 52 *info = d3d_info_; | 59 *info = d3d_info_; |
| 53 return true; | 60 return true; |
| 54 } | 61 } |
| 55 | 62 |
| 56 DesktopVector DxgiDuplicatorController::dpi() { | 63 DesktopVector DxgiDuplicatorController::dpi() { |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 ReleaseDC(nullptr, hdc); | 182 ReleaseDC(nullptr, hdc); |
| 176 } | 183 } |
| 177 | 184 |
| 178 identity_++; | 185 identity_++; |
| 179 return true; | 186 return true; |
| 180 } | 187 } |
| 181 | 188 |
| 182 void DxgiDuplicatorController::Deinitialize() { | 189 void DxgiDuplicatorController::Deinitialize() { |
| 183 desktop_rect_ = DesktopRect(); | 190 desktop_rect_ = DesktopRect(); |
| 184 duplicators_.clear(); | 191 duplicators_.clear(); |
| 192 resolution_change_detector_.Reset(); |
| 185 } | 193 } |
| 186 | 194 |
| 187 bool DxgiDuplicatorController::ContextExpired( | 195 bool DxgiDuplicatorController::ContextExpired( |
| 188 const Context* const context) const { | 196 const Context* const context) const { |
| 189 return context->identity_ != identity_ || | 197 return context->identity_ != identity_ || |
| 190 context->contexts_.size() != duplicators_.size(); | 198 context->contexts_.size() != duplicators_.size(); |
| 191 } | 199 } |
| 192 | 200 |
| 193 void DxgiDuplicatorController::Setup(Context* context) { | 201 void DxgiDuplicatorController::Setup(Context* context) { |
| 194 if (ContextExpired(context)) { | 202 if (ContextExpired(context)) { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 212 RTC_DCHECK_GE(monitor_id, 0); | 220 RTC_DCHECK_GE(monitor_id, 0); |
| 213 return DoDuplicate(context, monitor_id, target); | 221 return DoDuplicate(context, monitor_id, target); |
| 214 } | 222 } |
| 215 | 223 |
| 216 bool DxgiDuplicatorController::DoDuplicate(Context* context, | 224 bool DxgiDuplicatorController::DoDuplicate(Context* context, |
| 217 int monitor_id, | 225 int monitor_id, |
| 218 SharedDesktopFrame* target) { | 226 SharedDesktopFrame* target) { |
| 219 RTC_DCHECK(target); | 227 RTC_DCHECK(target); |
| 220 target->mutable_updated_region()->Clear(); | 228 target->mutable_updated_region()->Clear(); |
| 221 rtc::CritScope lock(&lock_); | 229 rtc::CritScope lock(&lock_); |
| 230 if (DoDuplicateUnlocked(context, monitor_id, target)) { |
| 231 return true; |
| 232 } |
| 233 Deinitialize(); |
| 234 return false; |
| 235 } |
| 236 |
| 237 bool DxgiDuplicatorController::DoDuplicateUnlocked(Context* context, |
| 238 int monitor_id, |
| 239 SharedDesktopFrame* target) { |
| 222 if (!Initialize()) { | 240 if (!Initialize()) { |
| 223 // Cannot initialize COM components now, display mode may be changing. | 241 // Cannot initialize COM components now, display mode may be changing. |
| 224 return false; | 242 return false; |
| 225 } | 243 } |
| 226 | 244 |
| 245 if (resolution_change_detector_.IsChanged( |
| 246 GetScreenRect(kFullDesktopScreenId, L"").size())) { |
| 247 // Resolution of entire screen has been changed, which usually means a new |
| 248 // monitor has been attached or one has been removed. The simplest way is to |
| 249 // Deinitialize() and returns false to indicate downstream components. |
| 250 return false; |
| 251 } |
| 252 |
| 227 Setup(context); | 253 Setup(context); |
| 228 if (monitor_id < 0) { | 254 if (monitor_id < 0) { |
| 229 // Capture entire screen. | 255 // Capture entire screen. |
| 230 for (size_t i = 0; i < duplicators_.size(); i++) { | 256 for (size_t i = 0; i < duplicators_.size(); i++) { |
| 231 if (!duplicators_[i].Duplicate(&context->contexts_[i], target)) { | 257 if (!duplicators_[i].Duplicate(&context->contexts_[i], target)) { |
| 232 Deinitialize(); | |
| 233 return false; | 258 return false; |
| 234 } | 259 } |
| 235 } | 260 } |
| 236 target->set_dpi(dpi()); | 261 target->set_dpi(dpi()); |
| 237 return true; | 262 return true; |
| 238 } | 263 } |
| 239 | 264 |
| 240 // Capture one monitor. | 265 // Capture one monitor. |
| 241 for (size_t i = 0; i < duplicators_.size() && i < context->contexts_.size(); | 266 for (size_t i = 0; i < duplicators_.size() && i < context->contexts_.size(); |
| 242 i++) { | 267 i++) { |
| 243 if (monitor_id >= duplicators_[i].screen_count()) { | 268 if (monitor_id >= duplicators_[i].screen_count()) { |
| 244 monitor_id -= duplicators_[i].screen_count(); | 269 monitor_id -= duplicators_[i].screen_count(); |
| 245 } else { | 270 } else { |
| 246 if (duplicators_[i].DuplicateMonitor(&context->contexts_[i], monitor_id, | 271 if (duplicators_[i].DuplicateMonitor(&context->contexts_[i], monitor_id, |
| 247 target)) { | 272 target)) { |
| 248 target->set_dpi(dpi()); | 273 target->set_dpi(dpi()); |
| 249 return true; | 274 return true; |
| 250 } | 275 } |
| 251 Deinitialize(); | |
| 252 return false; | 276 return false; |
| 253 } | 277 } |
| 254 } | 278 } |
| 255 // id >= ScreenCount(). This is a user error, so we do not need to | 279 // id >= ScreenCount(). This is a user error, so we do not need to |
| 256 // deinitialize. | 280 // deinitialize. |
| 257 return false; | 281 return false; |
| 258 } | 282 } |
| 259 | 283 |
| 260 } // namespace webrtc | 284 } // namespace webrtc |
| OLD | NEW |