|
13 | 13 | #include "Common/Log.h" |
14 | 14 | #include "Common/StringUtils.h" |
15 | 15 | #include "Common/GPU/GraphicsContext.h" |
| 16 | +#include "Common/System/Display.h" |
16 | 17 | #include "Common/Thread/ThreadUtil.h" |
| 18 | +#include "Common/TimeUtil.h" |
| 19 | +#include "Common/VR/PPSSPPVR.h" |
17 | 20 |
|
18 | 21 | #include "Core/EmuThread.h" |
19 | 22 | #include "Core/Core.h" |
@@ -125,34 +128,68 @@ bool RunMainLoop(GraphicsContext *graphicsContext, Application *application, std |
125 | 128 | return true; |
126 | 129 | } |
127 | 130 |
|
128 | | -// Call InitAPI and ShutdownAPI outside this! |
129 | | -bool MainThreadFunc(GraphicsContext *graphicsContext, Application *application, const WindowDesc &windowDesc, std::function<bool(GraphicsContext *)> frame, std::string *errorMessage) { |
130 | | - // This is now the render thread, and will spawn the emu thread below. |
131 | | - if (!graphicsContext->InitSurface(windowDesc.winsys, windowDesc.data1, windowDesc.data2, errorMessage)) { |
132 | | - ERROR_LOG(Log::G3D, "MainThreadFunc: InitSurface failed: %s", errorMessage->c_str()); |
133 | | - delete application; |
134 | | - return false; |
| 131 | +void RunGraphicsLoop(GraphicsContext *graphicsContext, Application *application, std::function<bool(GraphicsContext *)> frame, std::function<bool()> shouldExit) { |
| 132 | + if (!graphicsContext->NeedsSeparateEmuThread()) { |
| 133 | + // The backend spawns its own render thread, so this thread is simply where emulation runs. |
| 134 | + SetCurrentThreadName("EmuThread"); |
| 135 | + RunMainLoop(graphicsContext, application, frame); |
| 136 | + return; |
135 | 137 | } |
136 | | - if (graphicsContext->NeedsSeparateEmuThread()) { |
137 | | - SetCurrentThreadName("RenderThread"); |
138 | 138 |
|
139 | | - g_inLoop = true; |
140 | | - std::thread emuThread = EmuThread_Start(graphicsContext, application, frame); |
141 | | - graphicsContext->ThreadStart(); |
142 | | - // This thread becomes the render thread. EmuThread will tell it when to quit by sending a message. |
143 | | - while (graphicsContext->ThreadFrame()) {} |
144 | | - EmuThread_Join(graphicsContext, emuThread); |
145 | | - g_inLoop = false; |
| 139 | + // OpenGL wants its API calls on the thread its context is current on, which is this one. So |
| 140 | + // this thread becomes the render thread - all it does is execute the GPU commands the emu |
| 141 | + // thread queues up - and emulation moves to a thread we spawn here. |
| 142 | + SetCurrentThreadName("RenderThread"); |
146 | 143 |
|
147 | | - graphicsContext->ThreadEnd(); |
| 144 | + g_inLoop = true; |
148 | 145 |
|
149 | | - INFO_LOG(Log::System, "RenderThread - joined"); |
| 146 | + // EmuThread_Start calls ThreadStart() for us, and EmuThread_Join calls ThreadEnd(), so neither |
| 147 | + // is called directly here. |
| 148 | + std::thread emuThread = EmuThread_Start(graphicsContext, application, frame); |
150 | 149 |
|
151 | | - } else { |
152 | | - SetCurrentThreadName("MainThread"); |
| 150 | + if (IsVREnabled()) { |
| 151 | + static bool vrFirstStart = true; |
| 152 | + EnterVR(vrFirstStart); |
| 153 | + vrFirstStart = false; |
| 154 | + } |
153 | 155 |
|
154 | | - RunMainLoop(graphicsContext, application, frame); |
| 156 | + // Normally ThreadFrame() returning false is what gets us out of here - the frame callback |
| 157 | + // decides emulation is done, and the emu thread's NotifyEmuThreadExit then queues the exit. |
| 158 | + // That isn't enough in VR: while the session is idle we skip ThreadFrame() entirely, and going |
| 159 | + // to the background is precisely when the session goes idle, so the exit would never be seen |
| 160 | + // and whoever is waiting to join this thread would wait forever. Hence shouldExit. |
| 161 | + while (!(shouldExit && shouldExit())) { |
| 162 | + if (IsVREnabled() && !StartVRRender()) { |
| 163 | + // The session isn't active, so there's no frame to render into. Nothing to do until it |
| 164 | + // comes back - wait out a frame rather than spinning on it. |
| 165 | + sleep_ms(16, "vr-session-idle"); |
| 166 | + continue; |
| 167 | + } |
| 168 | + if (!graphicsContext->ThreadFrame()) { |
| 169 | + break; |
| 170 | + } |
| 171 | + if (IsVREnabled()) { |
| 172 | + UpdateVRInput(g_Config.bHapticFeedback, g_display.dpi_scale_x, g_display.dpi_scale_y); |
| 173 | + FinishVRRender(); |
| 174 | + } |
| 175 | + } |
| 176 | + |
| 177 | + // Also drains whatever the emu thread still had queued, so it can't get stuck waiting on us. |
| 178 | + EmuThread_Join(graphicsContext, emuThread); |
| 179 | + |
| 180 | + g_inLoop = false; |
| 181 | + |
| 182 | + INFO_LOG(Log::System, "RenderThread - joined"); |
| 183 | +} |
| 184 | + |
| 185 | +// Call InitAPI and ShutdownAPI outside this! |
| 186 | +bool MainThreadFunc(GraphicsContext *graphicsContext, Application *application, const WindowDesc &windowDesc, std::function<bool(GraphicsContext *)> frame, std::string *errorMessage) { |
| 187 | + if (!graphicsContext->InitSurface(windowDesc.winsys, windowDesc.data1, windowDesc.data2, errorMessage)) { |
| 188 | + ERROR_LOG(Log::G3D, "MainThreadFunc: InitSurface failed: %s", errorMessage->c_str()); |
| 189 | + delete application; |
| 190 | + return false; |
155 | 191 | } |
| 192 | + RunGraphicsLoop(graphicsContext, application, frame); |
156 | 193 | graphicsContext->ShutdownSurface(); |
157 | 194 | return true; |
158 | 195 | } |
0 commit comments