Replace RecursiveCriticalSection with Mutex in DxgiDuplicatorController

Bug: webrtc:11567
Change-Id: I6d59de7ca60b69765118787fff023c485b1f405e
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/207160
Reviewed-by: Jamie Walch <jamiewalch@chromium.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#33264}
This commit is contained in:
Niels Möller 2021-02-12 13:46:18 +01:00 committed by Commit Bot
parent 410c99847b
commit 563fbc1dc5
2 changed files with 35 additions and 35 deletions

View File

@ -85,14 +85,14 @@ void DxgiDuplicatorController::Release() {
} }
bool DxgiDuplicatorController::IsSupported() { bool DxgiDuplicatorController::IsSupported() {
rtc::CritScope lock(&lock_); MutexLock lock(&mutex_);
return Initialize(); return Initialize();
} }
bool DxgiDuplicatorController::RetrieveD3dInfo(D3dInfo* info) { bool DxgiDuplicatorController::RetrieveD3dInfo(D3dInfo* info) {
bool result = false; bool result = false;
{ {
rtc::CritScope lock(&lock_); MutexLock lock(&mutex_);
result = Initialize(); result = Initialize();
*info = d3d_info_; *info = d3d_info_;
} }
@ -116,7 +116,7 @@ DxgiDuplicatorController::Result DxgiDuplicatorController::DuplicateMonitor(
} }
DesktopVector DxgiDuplicatorController::dpi() { DesktopVector DxgiDuplicatorController::dpi() {
rtc::CritScope lock(&lock_); MutexLock lock(&mutex_);
if (Initialize()) { if (Initialize()) {
return dpi_; return dpi_;
} }
@ -124,7 +124,7 @@ DesktopVector DxgiDuplicatorController::dpi() {
} }
int DxgiDuplicatorController::ScreenCount() { int DxgiDuplicatorController::ScreenCount() {
rtc::CritScope lock(&lock_); MutexLock lock(&mutex_);
if (Initialize()) { if (Initialize()) {
return ScreenCountUnlocked(); return ScreenCountUnlocked();
} }
@ -133,7 +133,7 @@ int DxgiDuplicatorController::ScreenCount() {
bool DxgiDuplicatorController::GetDeviceNames( bool DxgiDuplicatorController::GetDeviceNames(
std::vector<std::string>* output) { std::vector<std::string>* output) {
rtc::CritScope lock(&lock_); MutexLock lock(&mutex_);
if (Initialize()) { if (Initialize()) {
GetDeviceNamesUnlocked(output); GetDeviceNamesUnlocked(output);
return true; return true;
@ -145,7 +145,7 @@ DxgiDuplicatorController::Result DxgiDuplicatorController::DoDuplicate(
DxgiFrame* frame, DxgiFrame* frame,
int monitor_id) { int monitor_id) {
RTC_DCHECK(frame); RTC_DCHECK(frame);
rtc::CritScope lock(&lock_); MutexLock lock(&mutex_);
// The dxgi components and APIs do not update the screen resolution without // The dxgi components and APIs do not update the screen resolution without
// a reinitialization. So we use the GetDC() function to retrieve the screen // a reinitialization. So we use the GetDC() function to retrieve the screen
@ -198,12 +198,12 @@ DxgiDuplicatorController::Result DxgiDuplicatorController::DoDuplicate(
} }
void DxgiDuplicatorController::Unload() { void DxgiDuplicatorController::Unload() {
rtc::CritScope lock(&lock_); MutexLock lock(&mutex_);
Deinitialize(); Deinitialize();
} }
void DxgiDuplicatorController::Unregister(const Context* const context) { void DxgiDuplicatorController::Unregister(const Context* const context) {
rtc::CritScope lock(&lock_); MutexLock lock(&mutex_);
if (ContextExpired(context)) { if (ContextExpired(context)) {
// The Context has not been setup after a recent initialization, so it // The Context has not been setup after a recent initialization, so it
// should not been registered in duplicators. // should not been registered in duplicators.

View File

@ -25,7 +25,7 @@
#include "modules/desktop_capture/win/dxgi_adapter_duplicator.h" #include "modules/desktop_capture/win/dxgi_adapter_duplicator.h"
#include "modules/desktop_capture/win/dxgi_context.h" #include "modules/desktop_capture/win/dxgi_context.h"
#include "modules/desktop_capture/win/dxgi_frame.h" #include "modules/desktop_capture/win/dxgi_frame.h"
#include "rtc_base/deprecated/recursive_critical_section.h" #include "rtc_base/synchronization/mutex.h"
namespace webrtc { namespace webrtc {
@ -142,71 +142,71 @@ class DxgiDuplicatorController {
Result DoDuplicate(DxgiFrame* frame, int monitor_id); Result DoDuplicate(DxgiFrame* frame, int monitor_id);
// Unload all the DXGI components and releases the resources. This function // Unload all the DXGI components and releases the resources. This function
// wraps Deinitialize() with |lock_|. // wraps Deinitialize() with |mutex_|.
void Unload(); void Unload();
// Unregisters Context from this instance and all DxgiAdapterDuplicator(s) // Unregisters Context from this instance and all DxgiAdapterDuplicator(s)
// it owns. // it owns.
void Unregister(const Context* const context); void Unregister(const Context* const context);
// All functions below should be called in |lock_| locked scope and should be // All functions below should be called in |mutex_| locked scope and should be
// after a successful Initialize(). // after a successful Initialize().
// If current instance has not been initialized, executes DoInitialize() // If current instance has not been initialized, executes DoInitialize()
// function, and returns initialize result. Otherwise directly returns true. // function, and returns initialize result. Otherwise directly returns true.
// This function may calls Deinitialize() if initialization failed. // This function may calls Deinitialize() if initialization failed.
bool Initialize() RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); bool Initialize() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Does the real initialization work, this function should only be called in // Does the real initialization work, this function should only be called in
// Initialize(). // Initialize().
bool DoInitialize() RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); bool DoInitialize() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Clears all COM components referred by this instance. So next Duplicate() // Clears all COM components referred by this instance. So next Duplicate()
// call will eventually initialize this instance again. // call will eventually initialize this instance again.
void Deinitialize() RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); void Deinitialize() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// A helper function to check whether a Context has been expired. // A helper function to check whether a Context has been expired.
bool ContextExpired(const Context* const context) const bool ContextExpired(const Context* const context) const
RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Updates Context if needed. // Updates Context if needed.
void Setup(Context* context) RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); void Setup(Context* context) RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
bool DoDuplicateUnlocked(Context* context, bool DoDuplicateUnlocked(Context* context,
int monitor_id, int monitor_id,
SharedDesktopFrame* target) SharedDesktopFrame* target)
RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Captures all monitors. // Captures all monitors.
bool DoDuplicateAll(Context* context, SharedDesktopFrame* target) bool DoDuplicateAll(Context* context, SharedDesktopFrame* target)
RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Captures one monitor. // Captures one monitor.
bool DoDuplicateOne(Context* context, bool DoDuplicateOne(Context* context,
int monitor_id, int monitor_id,
SharedDesktopFrame* target) SharedDesktopFrame* target)
RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// The minimum GetNumFramesCaptured() returned by |duplicators_|. // The minimum GetNumFramesCaptured() returned by |duplicators_|.
int64_t GetNumFramesCaptured() const RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); int64_t GetNumFramesCaptured() const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Returns a DesktopSize to cover entire |desktop_rect_|. // Returns a DesktopSize to cover entire |desktop_rect_|.
DesktopSize desktop_size() const RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); DesktopSize desktop_size() const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Returns the size of one screen. |id| should be >= 0. If system does not // Returns the size of one screen. |id| should be >= 0. If system does not
// support DXGI based capturer, or |id| is greater than the total screen count // support DXGI based capturer, or |id| is greater than the total screen count
// of all the Duplicators, this function returns an empty DesktopRect. // of all the Duplicators, this function returns an empty DesktopRect.
DesktopRect ScreenRect(int id) const RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); DesktopRect ScreenRect(int id) const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
int ScreenCountUnlocked() const RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); int ScreenCountUnlocked() const RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
void GetDeviceNamesUnlocked(std::vector<std::string>* output) const void GetDeviceNamesUnlocked(std::vector<std::string>* output) const
RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Returns the desktop size of the selected screen |monitor_id|. Setting // Returns the desktop size of the selected screen |monitor_id|. Setting
// |monitor_id| < 0 to return the entire screen size. // |monitor_id| < 0 to return the entire screen size.
DesktopSize SelectedDesktopSize(int monitor_id) const DesktopSize SelectedDesktopSize(int monitor_id) const
RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Retries DoDuplicateAll() for several times until GetNumFramesCaptured() is // Retries DoDuplicateAll() for several times until GetNumFramesCaptured() is
// large enough. Returns false if DoDuplicateAll() returns false, or // large enough. Returns false if DoDuplicateAll() returns false, or
@ -214,31 +214,31 @@ class DxgiDuplicatorController {
// According to http://crbug.com/682112, dxgi capturer returns a black frame // According to http://crbug.com/682112, dxgi capturer returns a black frame
// during first several capture attempts. // during first several capture attempts.
bool EnsureFrameCaptured(Context* context, SharedDesktopFrame* target) bool EnsureFrameCaptured(Context* context, SharedDesktopFrame* target)
RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// Moves |desktop_rect_| and all underlying |duplicators_|, putting top left // Moves |desktop_rect_| and all underlying |duplicators_|, putting top left
// corner of the desktop at (0, 0). This is necessary because DXGI_OUTPUT_DESC // corner of the desktop at (0, 0). This is necessary because DXGI_OUTPUT_DESC
// may return negative coordinates. Called from DoInitialize() after all // may return negative coordinates. Called from DoInitialize() after all
// DxgiAdapterDuplicator and DxgiOutputDuplicator instances are initialized. // DxgiAdapterDuplicator and DxgiOutputDuplicator instances are initialized.
void TranslateRect() RTC_EXCLUSIVE_LOCKS_REQUIRED(lock_); void TranslateRect() RTC_EXCLUSIVE_LOCKS_REQUIRED(mutex_);
// The count of references which are now "living". // The count of references which are now "living".
std::atomic_int refcount_; std::atomic_int refcount_;
// This lock must be locked whenever accessing any of the following objects. // This lock must be locked whenever accessing any of the following objects.
rtc::RecursiveCriticalSection lock_; Mutex mutex_;
// A self-incremented integer to compare with the one in Context. It ensures // A self-incremented integer to compare with the one in Context. It ensures
// a Context instance is always initialized after DxgiDuplicatorController. // a Context instance is always initialized after DxgiDuplicatorController.
int identity_ RTC_GUARDED_BY(lock_) = 0; int identity_ RTC_GUARDED_BY(mutex_) = 0;
DesktopRect desktop_rect_ RTC_GUARDED_BY(lock_); DesktopRect desktop_rect_ RTC_GUARDED_BY(mutex_);
DesktopVector dpi_ RTC_GUARDED_BY(lock_); DesktopVector dpi_ RTC_GUARDED_BY(mutex_);
std::vector<DxgiAdapterDuplicator> duplicators_ RTC_GUARDED_BY(lock_); std::vector<DxgiAdapterDuplicator> duplicators_ RTC_GUARDED_BY(mutex_);
D3dInfo d3d_info_ RTC_GUARDED_BY(lock_); D3dInfo d3d_info_ RTC_GUARDED_BY(mutex_);
DisplayConfigurationMonitor display_configuration_monitor_ DisplayConfigurationMonitor display_configuration_monitor_
RTC_GUARDED_BY(lock_); RTC_GUARDED_BY(mutex_);
// A number to indicate how many succeeded duplications have been performed. // A number to indicate how many succeeded duplications have been performed.
uint32_t succeeded_duplications_ RTC_GUARDED_BY(lock_) = 0; uint32_t succeeded_duplications_ RTC_GUARDED_BY(mutex_) = 0;
}; };
} // namespace webrtc } // namespace webrtc