Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(518)

Side by Side Diff: webrtc/modules/desktop_capture/win/dxgi_duplicator_controller.cc

Issue 2933893003: Add reference counter of DxgiDuplicatorController to unload DXGI components (Closed)
Patch Set: unique_ptr -> scoped_refptr Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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);
Sergey Ulanov 2017/06/19 18:52:43 Does this value need to be static?
Hzj_jie 2017/06/19 20:19:46 Since we have only one DxgiDuplicatorController in
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 return Reference(Instance());
42 }
43
35 DxgiDuplicatorController::DxgiDuplicatorController() = default; 44 DxgiDuplicatorController::DxgiDuplicatorController() = default;
36 45
37 DxgiDuplicatorController::~DxgiDuplicatorController() { 46 void DxgiDuplicatorController::AddRef() const {
38 rtc::CritScope lock(&lock_); 47 int refcount = (++refcount_);
39 Deinitialize(); 48 RTC_DCHECK(refcount > 0);
49 }
50
51 void DxgiDuplicatorController::Release() {
52 int refcount = (--refcount_);
53 RTC_DCHECK(refcount >= 0);
54 if (refcount == 0) {
55 LOG(LS_WARNING) << "Count of references reaches zero, "
56 "DxgiDuplicatorController will be unloaded.";
57 Unload();
58 }
40 } 59 }
41 60
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()) {
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698