From aaaa3896c09369ee0610992f14ab5a845e040315 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=A9o=20Lam?= Date: Wed, 21 Oct 2020 21:34:38 +0200 Subject: VideoCommon: Get rid of the global g_available_video_backends Replace it with a function-local static that is initialized on first use. This gets rid of a global variable and removes the need for manual initialization in UICommon. This commit also replaces the weird find_if that looks for a non-null unique_ptr with a simple "is vector empty" check considering that none of the pointers can be null by construction. --- Source/Core/VideoCommon/VideoBackendBase.cpp | 58 ++++++++++++++-------------- 1 file changed, 30 insertions(+), 28 deletions(-) (limited to 'Source/Core/VideoCommon/VideoBackendBase.cpp') diff --git a/Source/Core/VideoCommon/VideoBackendBase.cpp b/Source/Core/VideoCommon/VideoBackendBase.cpp index a8633892bd..d793727ebc 100644 --- a/Source/Core/VideoCommon/VideoBackendBase.cpp +++ b/Source/Core/VideoCommon/VideoBackendBase.cpp @@ -58,9 +58,7 @@ #include "VideoCommon/VideoConfig.h" #include "VideoCommon/VideoState.h" -std::vector> g_available_video_backends; VideoBackendBase* g_video_backend = nullptr; -static VideoBackendBase* s_default_backend = nullptr; #ifdef _WIN32 #include @@ -200,6 +198,14 @@ u16 VideoBackendBase::Video_GetBoundingBox(int index) return result; } +static VideoBackendBase* GetDefaultVideoBackend() +{ + const auto& backends = VideoBackendBase::GetAvailableBackends(); + if (backends.empty()) + return nullptr; + return backends.front().get(); +} + // This function is called at static initialization, so we can't rely on s_default_backend being set std::string VideoBackendBase::GetDefaultBackendName() { @@ -212,49 +218,45 @@ std::string VideoBackendBase::GetDefaultBackendName() #endif } -void VideoBackendBase::PopulateList() +const std::vector>& VideoBackendBase::GetAvailableBackends() { - // OGL > D3D11 > D3D12 > Vulkan > SW > Null + static auto s_available_backends = [] { + std::vector> backends; + + // OGL > D3D11 > D3D12 > Vulkan > SW > Null #ifdef HAS_OPENGL - g_available_video_backends.push_back(std::make_unique()); + backends.push_back(std::make_unique()); #endif #ifdef _WIN32 - g_available_video_backends.push_back(std::make_unique()); - g_available_video_backends.push_back(std::make_unique()); + backends.push_back(std::make_unique()); + backends.push_back(std::make_unique()); #endif - g_available_video_backends.push_back(std::make_unique()); + backends.push_back(std::make_unique()); #ifdef HAS_OPENGL - g_available_video_backends.push_back(std::make_unique()); + backends.push_back(std::make_unique()); #endif - g_available_video_backends.push_back(std::make_unique()); - - const auto iter = - std::find_if(g_available_video_backends.begin(), g_available_video_backends.end(), - [](const auto& backend) { return backend != nullptr; }); + backends.push_back(std::make_unique()); - if (iter == g_available_video_backends.end()) - return; + if (!backends.empty()) + g_video_backend = backends.front().get(); - s_default_backend = iter->get(); - g_video_backend = iter->get(); -} - -void VideoBackendBase::ClearList() -{ - g_available_video_backends.clear(); + return backends; + }(); + return s_available_backends; } void VideoBackendBase::ActivateBackend(const std::string& name) { // If empty, set it to the default backend (expected behavior) if (name.empty()) - g_video_backend = s_default_backend; + g_video_backend = GetDefaultVideoBackend(); - const auto iter = - std::find_if(g_available_video_backends.begin(), g_available_video_backends.end(), - [&name](const auto& backend) { return name == backend->GetName(); }); + const auto& backends = GetAvailableBackends(); + const auto iter = std::find_if(backends.begin(), backends.end(), [&name](const auto& backend) { + return name == backend->GetName(); + }); - if (iter == g_available_video_backends.end()) + if (iter == backends.end()) return; g_video_backend = iter->get(); -- cgit v1.2.3