Merge pull request #3642 from lioncash/backend

VideoBackendBase: Store video backends as unique_ptr
This commit is contained in:
Mathew Maidment 2016-02-21 04:49:14 -05:00
commit bd148e911b
6 changed files with 40 additions and 34 deletions

View File

@ -61,7 +61,7 @@ SoftwareVideoConfigDialog::SoftwareVideoConfigDialog(wxWindow* parent, const std
wxStaticText* const label_backend = new wxStaticText(page_general, wxID_ANY, _("Backend:")); wxStaticText* const label_backend = new wxStaticText(page_general, wxID_ANY, _("Backend:"));
wxChoice* const choice_backend = new wxChoice(page_general, wxID_ANY); wxChoice* const choice_backend = new wxChoice(page_general, wxID_ANY);
for (const VideoBackendBase* backend : g_available_video_backends) for (const auto& backend : g_available_video_backends)
{ {
choice_backend->AppendString(StrToWxStr(backend->GetDisplayName())); choice_backend->AppendString(StrToWxStr(backend->GetDisplayName()));
} }

View File

@ -24,17 +24,18 @@ public:
void Event_Backend(wxCommandEvent &ev) void Event_Backend(wxCommandEvent &ev)
{ {
VideoBackendBase* new_backend = g_available_video_backends[ev.GetInt()]; auto& new_backend = g_available_video_backends[ev.GetInt()];
if (g_video_backend != new_backend) if (g_video_backend != new_backend.get())
{ {
Close(); Close();
g_video_backend = new_backend; g_video_backend = new_backend.get();
SConfig::GetInstance().m_strVideoBackend = g_video_backend->GetName(); SConfig::GetInstance().m_strVideoBackend = g_video_backend->GetName();
g_video_backend->ShowConfig(GetParent()); g_video_backend->ShowConfig(GetParent());
} }
ev.Skip(); ev.Skip();
} }
}; };

View File

@ -235,7 +235,7 @@ VideoConfigDiag::VideoConfigDiag(wxWindow* parent, const std::string &title, con
choice_backend = new wxChoice(page_general, wxID_ANY); choice_backend = new wxChoice(page_general, wxID_ANY);
RegisterControl(choice_backend, wxGetTranslation(backend_desc)); RegisterControl(choice_backend, wxGetTranslation(backend_desc));
for (const VideoBackendBase* backend : g_available_video_backends) for (const auto& backend : g_available_video_backends)
{ {
choice_backend->AppendString(wxGetTranslation(StrToWxStr(backend->GetDisplayName()))); choice_backend->AppendString(wxGetTranslation(StrToWxStr(backend->GetDisplayName())));
} }

View File

@ -82,8 +82,9 @@ public:
protected: protected:
void Event_Backend(wxCommandEvent &ev) void Event_Backend(wxCommandEvent &ev)
{ {
VideoBackendBase* new_backend = g_available_video_backends[ev.GetInt()]; auto& new_backend = g_available_video_backends[ev.GetInt()];
if (g_video_backend != new_backend)
if (g_video_backend != new_backend.get())
{ {
bool do_switch = !Core::IsRunning(); bool do_switch = !Core::IsRunning();
if (new_backend->GetName() == "Software Renderer") if (new_backend->GetName() == "Software Renderer")
@ -99,7 +100,7 @@ protected:
// reopen the dialog // reopen the dialog
Close(); Close();
g_video_backend = new_backend; g_video_backend = new_backend.get();
SConfig::GetInstance().m_strVideoBackend = g_video_backend->GetName(); SConfig::GetInstance().m_strVideoBackend = g_video_backend->GetName();
g_video_backend->ShowConfig(GetParent()); g_video_backend->ShowConfig(GetParent());

View File

@ -2,6 +2,8 @@
// Licensed under GPLv2+ // Licensed under GPLv2+
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <algorithm>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -15,7 +17,7 @@
#include "VideoCommon/VideoBackendBase.h" #include "VideoCommon/VideoBackendBase.h"
std::vector<VideoBackendBase*> g_available_video_backends; std::vector<std::unique_ptr<VideoBackendBase>> g_available_video_backends;
VideoBackendBase* g_video_backend = nullptr; VideoBackendBase* g_video_backend = nullptr;
static VideoBackendBase* s_default_backend = nullptr; static VideoBackendBase* s_default_backend = nullptr;
@ -32,48 +34,49 @@ __declspec(dllexport) DWORD NvOptimusEnablement = 1;
void VideoBackendBase::PopulateList() void VideoBackendBase::PopulateList()
{ {
VideoBackendBase* backends[4] = { nullptr };
// OGL > D3D11 > D3D12 > SW // OGL > D3D11 > D3D12 > SW
g_available_video_backends.push_back(backends[0] = new OGL::VideoBackend); g_available_video_backends.push_back(std::make_unique<OGL::VideoBackend>());
#ifdef _WIN32 #ifdef _WIN32
g_available_video_backends.push_back(backends[1] = new DX11::VideoBackend); g_available_video_backends.push_back(std::make_unique<DX11::VideoBackend>());
// More robust way to check for D3D12 support than (unreliable) OS version checks. // More robust way to check for D3D12 support than (unreliable) OS version checks.
HMODULE d3d12_module = LoadLibraryA("d3d12.dll"); HMODULE d3d12_module = LoadLibraryA("d3d12.dll");
if (d3d12_module != NULL) if (d3d12_module != nullptr)
{ {
FreeLibrary(d3d12_module); FreeLibrary(d3d12_module);
g_available_video_backends.push_back(backends[2] = new DX12::VideoBackend); g_available_video_backends.push_back(std::make_unique<DX12::VideoBackend>());
} }
#endif #endif
g_available_video_backends.push_back(backends[3] = new SW::VideoSoftware); g_available_video_backends.push_back(std::make_unique<SW::VideoSoftware>());
for (VideoBackendBase* backend : backends) const auto iter = std::find_if(g_available_video_backends.begin(), g_available_video_backends.end(), [](const auto& backend) {
{ return backend != nullptr;
if (backend) });
{
s_default_backend = g_video_backend = backend; if (iter == g_available_video_backends.end())
break; return;
}
} s_default_backend = iter->get();
g_video_backend = iter->get();
} }
void VideoBackendBase::ClearList() void VideoBackendBase::ClearList()
{ {
while (!g_available_video_backends.empty()) g_available_video_backends.clear();
{
delete g_available_video_backends.back();
g_available_video_backends.pop_back();
}
} }
void VideoBackendBase::ActivateBackend(const std::string& name) void VideoBackendBase::ActivateBackend(const std::string& name)
{ {
if (name.length() == 0) // If nullptr, set it to the default backend (expected behavior) // If empty, set it to the default backend (expected behavior)
if (name.empty())
g_video_backend = s_default_backend; g_video_backend = s_default_backend;
for (VideoBackendBase* backend : g_available_video_backends) const auto iter = std::find_if(g_available_video_backends.begin(), g_available_video_backends.end(), [&name](const auto& backend) {
if (name == backend->GetName()) return name == backend->GetName();
g_video_backend = backend; });
if (iter == g_available_video_backends.end())
return;
g_video_backend = iter->get();
} }

View File

@ -4,6 +4,7 @@
#pragma once #pragma once
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@ -99,5 +100,5 @@ protected:
bool m_invalid = false; bool m_invalid = false;
}; };
extern std::vector<VideoBackendBase*> g_available_video_backends; extern std::vector<std::unique_ptr<VideoBackendBase>> g_available_video_backends;
extern VideoBackendBase* g_video_backend; extern VideoBackendBase* g_video_backend;