From 4d9ac5886d6d1754d8463d1d4a9917b737e69500 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niels=20M=C3=B6ller?= Date: Mon, 23 Oct 2017 12:31:29 +0200 Subject: [PATCH] Use C++11 static initialization, replacing Atomic32 CompareExchange. Bug: webrtc:8270 Change-Id: I328cec46be2a017a518946d19d21c242a067747d Reviewed-on: https://webrtc-review.googlesource.com/14220 Commit-Queue: Niels Moller Reviewed-by: Zijie He Reviewed-by: Karl Wiberg Cr-Commit-Position: refs/heads/master@{#20383} --- .../win/screen_capturer_win_magnifier.cc | 27 +++++++++---------- .../win/screen_capturer_win_magnifier.h | 3 --- 2 files changed, 12 insertions(+), 18 deletions(-) diff --git a/modules/desktop_capture/win/screen_capturer_win_magnifier.cc b/modules/desktop_capture/win/screen_capturer_win_magnifier.cc index 03ea99e58d..36261ab2ec 100644 --- a/modules/desktop_capture/win/screen_capturer_win_magnifier.cc +++ b/modules/desktop_capture/win/screen_capturer_win_magnifier.cc @@ -26,6 +26,15 @@ namespace webrtc { +namespace { +DWORD GetTlsIndex() { + static const DWORD tls_index = TlsAlloc(); + RTC_DCHECK(tls_index != TLS_OUT_OF_INDEXES); + return tls_index; +} + +} // namespace + // kMagnifierWindowClass has to be "Magnifier" according to the Magnification // API. The other strings can be anything. static LPCTSTR kMagnifierHostClass = L"ScreenCapturerWinMagnifierHost"; @@ -33,8 +42,6 @@ static LPCTSTR kHostWindowName = L"MagnifierHost"; static LPCTSTR kMagnifierWindowClass = L"Magnifier"; static LPCTSTR kMagnifierWindowName = L"MagnifierWindow"; -Atomic32 ScreenCapturerWinMagnifier::tls_index_(TLS_OUT_OF_INDEXES); - ScreenCapturerWinMagnifier::ScreenCapturerWinMagnifier() = default; ScreenCapturerWinMagnifier::~ScreenCapturerWinMagnifier() { // DestroyWindow must be called before MagUninitialize. magnifier_window_ is @@ -152,8 +159,7 @@ bool ScreenCapturerWinMagnifier::CaptureImage(const DesktopRect& rect) { RECT native_rect = {rect.left(), rect.top(), rect.right(), rect.bottom()}; - RTC_DCHECK(tls_index_.Value() != static_cast(TLS_OUT_OF_INDEXES)); - TlsSetValue(tls_index_.Value(), this); + TlsSetValue(GetTlsIndex(), this); // OnCaptured will be called via OnMagImageScalingCallback and fill in the // frame before set_window_source_func_ returns. result = set_window_source_func_(magnifier_window_, native_rect); @@ -177,11 +183,10 @@ BOOL ScreenCapturerWinMagnifier::OnMagImageScalingCallback( RECT unclipped, RECT clipped, HRGN dirty) { - RTC_DCHECK(tls_index_.Value() != static_cast(TLS_OUT_OF_INDEXES)); ScreenCapturerWinMagnifier* owner = reinterpret_cast( - TlsGetValue(tls_index_.Value())); - TlsSetValue(tls_index_.Value(), nullptr); + TlsGetValue(GetTlsIndex())); + TlsSetValue(GetTlsIndex(), nullptr); owner->OnCaptured(srcdata, srcheader); return TRUE; @@ -309,14 +314,6 @@ bool ScreenCapturerWinMagnifier::InitializeMagnifier() { } } - if (tls_index_.Value() == static_cast(TLS_OUT_OF_INDEXES)) { - // More than one threads may get here at the same time, but only one will - // write to tls_index_ using CompareExchange. - DWORD new_tls_index = TlsAlloc(); - if (!tls_index_.CompareExchange(new_tls_index, TLS_OUT_OF_INDEXES)) - TlsFree(new_tls_index); - } - magnifier_initialized_ = true; return true; } diff --git a/modules/desktop_capture/win/screen_capturer_win_magnifier.h b/modules/desktop_capture/win/screen_capturer_win_magnifier.h index b25633da0d..d994b1e059 100644 --- a/modules/desktop_capture/win/screen_capturer_win_magnifier.h +++ b/modules/desktop_capture/win/screen_capturer_win_magnifier.h @@ -23,7 +23,6 @@ #include "modules/desktop_capture/shared_desktop_frame.h" #include "modules/desktop_capture/win/scoped_thread_desktop.h" #include "rtc_base/constructormagic.h" -#include "system_wrappers/include/atomic32.h" namespace webrtc { @@ -99,8 +98,6 @@ class ScreenCapturerWinMagnifier : public DesktopCapturer { // Makes sure the current frame exists and matches |size|. void CreateCurrentFrameIfNecessary(const DesktopSize& size); - static Atomic32 tls_index_; - Callback* callback_ = nullptr; std::unique_ptr shared_memory_factory_; ScreenId current_screen_id_ = kFullDesktopScreenId;