ppsspp/Windows/EmuThread.cpp

202 lines
5.2 KiB
C++
Raw Normal View History

2012-11-01 15:19:01 +00:00
// NOTE: Apologies for the quality of this code, this is really from pre-opensource Dolphin - that is, 2003.
2013-03-29 17:50:08 +00:00
#include "base/timeutil.h"
#include "base/NativeApp.h"
#include "base/mutex.h"
#include "i18n/i18n.h"
#include "util/text/utf8.h"
#include "Common/Log.h"
#include "Common/StringUtils.h"
2012-11-01 15:19:01 +00:00
#include "../Globals.h"
#include "Windows/EmuThread.h"
#include "Windows/W32Util/Misc.h"
2015-09-19 11:14:05 +00:00
#include "Windows/MainWindow.h"
#include "Windows/resource.h"
#include "Core/Reporting.h"
#include "Core/MemMap.h"
#include "Core/Core.h"
#include "Core/Host.h"
#include "Core/System.h"
#include "Core/Config.h"
#include "thread/threadutil.h"
2012-11-01 15:19:01 +00:00
#include <tchar.h>
#include <process.h>
#include <intrin.h>
#pragma intrinsic(_InterlockedExchange)
static recursive_mutex emuThreadLock;
2012-11-01 15:19:01 +00:00
static HANDLE emuThread;
static volatile long emuThreadReady;
extern std::vector<std::wstring> GetWideCmdLine();
enum EmuThreadStatus : long
{
THREAD_NONE = 0,
THREAD_INIT,
THREAD_CORE_LOOP,
THREAD_SHUTDOWN,
THREAD_END,
};
2012-11-01 15:19:01 +00:00
HANDLE EmuThread_GetThreadHandle()
{
lock_guard guard(emuThreadLock);
2012-11-01 15:19:01 +00:00
return emuThread;
}
unsigned int WINAPI TheThread(void *);
2012-11-01 15:19:01 +00:00
2013-03-29 17:50:08 +00:00
void EmuThread_Start()
2012-11-01 15:19:01 +00:00
{
lock_guard guard(emuThreadLock);
emuThread = (HANDLE)_beginthreadex(0, 0, &TheThread, 0, 0, 0);
2012-11-01 15:19:01 +00:00
}
void EmuThread_Stop()
{
2013-08-13 15:03:13 +00:00
// Already stopped?
{
lock_guard guard(emuThreadLock);
2014-02-10 01:35:43 +00:00
if (emuThread == NULL || emuThreadReady == THREAD_END)
return;
}
2013-08-13 15:03:13 +00:00
UpdateUIState(UISTATE_EXIT);
2012-11-01 15:19:01 +00:00
Core_Stop();
Core_WaitInactive(800);
if (WAIT_TIMEOUT == WaitForSingleObject(emuThread, 800))
{
_dbg_assert_msg_(COMMON, false, "Wait for EmuThread timed out.");
}
2012-11-01 15:19:01 +00:00
{
lock_guard guard(emuThreadLock);
CloseHandle(emuThread);
emuThread = 0;
2012-11-01 15:19:01 +00:00
}
host->UpdateUI();
}
bool EmuThread_Ready()
{
return emuThreadReady == THREAD_CORE_LOOP;
}
unsigned int WINAPI TheThread(void *)
{
_InterlockedExchange(&emuThreadReady, THREAD_INIT);
setCurrentThreadName("Emu"); // And graphics...
2012-11-01 15:19:01 +00:00
2013-03-29 17:50:08 +00:00
// Native overwrites host. Can't allow that.
Host *oldHost = host;
// Convert the command-line arguments to Unicode, then to proper UTF-8
// (the benefit being that we don't have to pollute the UI project with win32 ifdefs and lots of Convert<whatever>To<whatever>).
// This avoids issues with PPSSPP inadvertently destroying paths with Unicode glyphs
// (using the ANSI args resulted in Japanese/Chinese glyphs being turned into question marks, at least for me..).
// -TheDax
std::vector<std::wstring> wideArgs = GetWideCmdLine();
std::vector<std::string> argsUTF8;
for (auto& string : wideArgs) {
argsUTF8.push_back(ConvertWStringToUTF8(string));
}
std::vector<const char *> args;
for (auto& string : argsUTF8) {
args.push_back(string.c_str());
}
2014-10-15 22:38:22 +00:00
NativeInit(static_cast<int>(args.size()), &args[0], "1234", "1234", "1234");
2013-03-29 17:50:08 +00:00
Host *nativeHost = host;
host = oldHost;
2012-11-01 15:19:01 +00:00
host->UpdateUI();
std::string error_string;
if (!host->InitGraphics(&error_string)) {
I18NCategory *err = GetI18NCategory("Error");
2014-08-17 14:07:14 +00:00
Reporting::ReportMessage("Graphics init error: %s", error_string.c_str());
const char *defaultErrorOpenGL = "Failed initializing graphics. Try upgrading your graphics drivers.\n\nWould you like to try switching to DirectX 9?\n\nError message:";
const char *defaultErrorDirect3D9 = "Failed initializing graphics. Try upgrading your graphics drivers and directx 9 runtime.\n\nWould you like to try switching to OpenGL?\n\nError message:";
const char *genericError;
int nextBackend = GPU_BACKEND_DIRECT3D9;
switch (g_Config.iGPUBackend) {
case GPU_BACKEND_DIRECT3D9:
nextBackend = GPU_BACKEND_OPENGL;
genericError = err->T("GenericDirect3D9Error", defaultErrorDirect3D9);
break;
case GPU_BACKEND_OPENGL:
default:
nextBackend = GPU_BACKEND_DIRECT3D9;
genericError = err->T("GenericOpenGLError", defaultErrorOpenGL);
break;
}
std::string full_error = StringFromFormat("%s\n\n%s", genericError, error_string.c_str());
std::wstring title = ConvertUTF8ToWString(err->T("GenericGraphicsError", "Graphics Error"));
bool yes = IDYES == MessageBox(0, ConvertUTF8ToWString(full_error).c_str(), title.c_str(), MB_ICONERROR | MB_YESNO);
ERROR_LOG(BOOT, full_error.c_str());
if (yes) {
// Change the config to the alternative and restart.
g_Config.iGPUBackend = nextBackend;
g_Config.Save();
W32Util::ExitAndRestart();
}
// No safe way out without graphics.
ExitProcess(1);
}
2012-11-01 15:19:01 +00:00
2013-03-29 17:50:08 +00:00
NativeInitGraphics();
NativeResized();
2012-11-01 15:19:01 +00:00
INFO_LOG(BOOT, "Done.");
_dbg_update_();
if (coreState == CORE_POWERDOWN) {
INFO_LOG(BOOT, "Exit before core loop.");
goto shutdown;
}
_InterlockedExchange(&emuThreadReady, THREAD_CORE_LOOP);
if (g_Config.bBrowse)
PostMessage(MainWindow::GetHWND(), WM_COMMAND, ID_FILE_LOAD, 0);
2013-03-29 17:50:08 +00:00
Core_EnableStepping(FALSE);
while (GetUIState() != UISTATE_EXIT)
{
// We're here again, so the game quit. Restart Core_Run() which controls the UI.
// This way they can load a new game.
if (!Core_IsActive())
UpdateUIState(UISTATE_MENU);
Core_Run();
}
2012-11-01 15:19:01 +00:00
2012-11-05 12:42:33 +00:00
shutdown:
_InterlockedExchange(&emuThreadReady, THREAD_SHUTDOWN);
2013-03-29 17:50:08 +00:00
NativeShutdownGraphics();
host->ShutdownSound();
host = nativeHost;
2013-03-29 17:50:08 +00:00
NativeShutdown();
host = oldHost;
host->ShutdownGraphics();
2012-11-01 15:19:01 +00:00
_InterlockedExchange(&emuThreadReady, THREAD_END);
2012-11-01 15:19:01 +00:00
return 0;
}