forked from hrydgard/ppsspp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCore.cpp
364 lines (307 loc) · 9.23 KB
/
Core.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// Copyright (c) 2012- PPSSPP Project.
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, version 2.0 or later versions.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License 2.0 for more details.
// A copy of the GPL 2.0 should have been included with the program.
// If not, see http://www.gnu.org/licenses/
// Official git repository and contact information can be found at
// https://github.com/hrydgard/ppsspp and http://www.ppsspp.org/.
#include "ppsspp_config.h"
#include <set>
#include <chrono>
#include <mutex>
#include <condition_variable>
#include "base/NativeApp.h"
#include "base/display.h"
#include "base/timeutil.h"
#include "thread/threadutil.h"
#include "profiler/profiler.h"
#include "Common/GraphicsContext.h"
#include "Core/Core.h"
#include "Core/Config.h"
#include "Core/Host.h"
#include "Core/MemMap.h"
#include "Core/SaveState.h"
#include "Core/System.h"
#include "Core/Debugger/Breakpoints.h"
#include "Core/MIPS/MIPS.h"
#include "GPU/Debugger/Stepping.h"
#ifdef _WIN32
#include "Common/CommonWindows.h"
#include "Windows/InputDevice.h"
#endif
// Time until we stop considering the core active without user input.
// Should this be configurable? 2 hours currently.
static const double ACTIVITY_IDLE_TIMEOUT = 2.0 * 3600.0;
static std::condition_variable m_StepCond;
static std::mutex m_hStepMutex;
static std::condition_variable m_InactiveCond;
static std::mutex m_hInactiveMutex;
static bool singleStepPending = false;
static int steppingCounter = 0;
static std::set<CoreLifecycleFunc> lifecycleFuncs;
static std::set<CoreStopRequestFunc> stopFuncs;
static bool windowHidden = false;
static double lastActivity = 0.0;
static double lastKeepAwake = 0.0;
static GraphicsContext *graphicsContext;
static bool powerSaving = false;
void Core_SetGraphicsContext(GraphicsContext *ctx) {
graphicsContext = ctx;
PSP_CoreParameter().graphicsContext = graphicsContext;
}
void Core_NotifyWindowHidden(bool hidden) {
windowHidden = hidden;
// TODO: Wait until we can react?
}
void Core_NotifyActivity() {
lastActivity = time_now_d();
}
void Core_ListenLifecycle(CoreLifecycleFunc func) {
lifecycleFuncs.insert(func);
}
void Core_NotifyLifecycle(CoreLifecycle stage) {
for (auto func : lifecycleFuncs) {
func(stage);
}
}
void Core_ListenStopRequest(CoreStopRequestFunc func) {
stopFuncs.insert(func);
}
void Core_Stop() {
Core_UpdateState(CORE_POWERDOWN);
for (auto func : stopFuncs) {
func();
}
}
bool Core_IsStepping() {
return coreState == CORE_STEPPING || coreState == CORE_POWERDOWN;
}
bool Core_IsActive() {
return coreState == CORE_RUNNING || coreState == CORE_NEXTFRAME || coreStatePending;
}
bool Core_IsInactive() {
return coreState != CORE_RUNNING && coreState != CORE_NEXTFRAME && !coreStatePending;
}
static inline void Core_StateProcessed() {
if (coreStatePending) {
std::lock_guard<std::mutex> guard(m_hInactiveMutex);
coreStatePending = false;
m_InactiveCond.notify_all();
}
}
void Core_WaitInactive() {
while (Core_IsActive()) {
std::unique_lock<std::mutex> guard(m_hInactiveMutex);
m_InactiveCond.wait(guard);
}
}
void Core_WaitInactive(int milliseconds) {
if (Core_IsActive()) {
std::unique_lock<std::mutex> guard(m_hInactiveMutex);
m_InactiveCond.wait_for(guard, std::chrono::milliseconds(milliseconds));
}
}
void Core_SetPowerSaving(bool mode) {
powerSaving = mode;
}
bool Core_GetPowerSaving() {
return powerSaving;
}
static bool IsWindowSmall(int pixelWidth, int pixelHeight) {
// Can't take this from config as it will not be set if windows is maximized.
int w = (int)(pixelWidth * g_dpi_scale_x);
int h = (int)(pixelHeight * g_dpi_scale_y);
return g_Config.IsPortrait() ? (h < 480 + 80) : (w < 480 + 80);
}
// TODO: Feels like this belongs elsewhere.
bool UpdateScreenScale(int width, int height) {
bool smallWindow;
#ifdef _WIN32
g_dpi = (float)System_GetPropertyInt(SYSPROP_DISPLAY_DPI);
g_dpi_scale_x = 96.0f / g_dpi;
g_dpi_scale_y = 96.0f / g_dpi;
#else
g_dpi = 96.0f;
g_dpi_scale_x = 1.0f;
g_dpi_scale_y = 1.0f;
#endif
g_dpi_scale_real_x = g_dpi_scale_x;
g_dpi_scale_real_y = g_dpi_scale_y;
smallWindow = IsWindowSmall(width, height);
if (smallWindow) {
g_dpi /= 2.0f;
g_dpi_scale_x *= 2.0f;
g_dpi_scale_y *= 2.0f;
}
pixel_in_dps_x = 1.0f / g_dpi_scale_x;
pixel_in_dps_y = 1.0f / g_dpi_scale_y;
int new_dp_xres = width * g_dpi_scale_x;
int new_dp_yres = height * g_dpi_scale_y;
bool dp_changed = new_dp_xres != dp_xres || new_dp_yres != dp_yres;
bool px_changed = pixel_xres != width || pixel_yres != height;
if (dp_changed || px_changed) {
dp_xres = new_dp_xres;
dp_yres = new_dp_yres;
pixel_xres = width;
pixel_yres = height;
DEBUG_LOG(SYSTEM, "pixel_res: %dx%d. Calling NativeResized()", pixel_xres, pixel_yres);
NativeResized();
return true;
}
return false;
}
// Note: not used on Android.
void UpdateRunLoop() {
if (windowHidden && g_Config.bPauseWhenMinimized) {
sleep_ms(16);
return;
}
NativeUpdate();
NativeRender(graphicsContext);
}
void KeepScreenAwake() {
#if defined(_WIN32) && !PPSSPP_PLATFORM(UWP)
SetThreadExecutionState(ES_SYSTEM_REQUIRED | ES_DISPLAY_REQUIRED);
#endif
}
void Core_RunLoop(GraphicsContext *ctx) {
graphicsContext = ctx;
while ((GetUIState() != UISTATE_INGAME || !PSP_IsInited()) && GetUIState() != UISTATE_EXIT) {
// In case it was pending, we're not in game anymore. We won't get to Core_Run().
Core_StateProcessed();
time_update();
double startTime = time_now_d();
UpdateRunLoop();
// Simple throttling to not burn the GPU in the menu.
time_update();
double diffTime = time_now_d() - startTime;
int sleepTime = (int)(1000.0 / 60.0) - (int)(diffTime * 1000.0);
if (sleepTime > 0)
sleep_ms(sleepTime);
if (!windowHidden) {
ctx->SwapBuffers();
}
}
while ((coreState == CORE_RUNNING || coreState == CORE_STEPPING) && GetUIState() == UISTATE_INGAME) {
time_update();
UpdateRunLoop();
if (!windowHidden && !Core_IsStepping()) {
ctx->SwapBuffers();
// Keep the system awake for longer than normal for cutscenes and the like.
const double now = time_now_d();
if (now < lastActivity + ACTIVITY_IDLE_TIMEOUT) {
// Only resetting it ever prime number seconds in case the call is expensive.
// Using a prime number to ensure there's no interaction with other periodic events.
if (now - lastKeepAwake > 89.0 || now < lastKeepAwake) {
KeepScreenAwake();
lastKeepAwake = now;
}
}
}
}
}
void Core_DoSingleStep() {
std::lock_guard<std::mutex> guard(m_hStepMutex);
singleStepPending = true;
m_StepCond.notify_all();
}
void Core_UpdateSingleStep() {
std::lock_guard<std::mutex> guard(m_hStepMutex);
m_StepCond.notify_all();
}
void Core_SingleStep() {
currentMIPS->SingleStep();
if (coreState == CORE_STEPPING)
steppingCounter++;
}
static inline bool Core_WaitStepping() {
std::unique_lock<std::mutex> guard(m_hStepMutex);
// We only wait 16ms so that we can still draw UI or react to events.
if (!singleStepPending && coreState == CORE_STEPPING)
m_StepCond.wait_for(guard, std::chrono::milliseconds(16));
bool result = singleStepPending;
singleStepPending = false;
return result;
}
void Core_ProcessStepping() {
Core_StateProcessed();
// Check if there's any pending save state actions.
SaveState::Process();
if (coreState != CORE_STEPPING) {
return;
}
// Or any GPU actions.
GPUStepping::SingleStep();
// We're not inside jit now, so it's safe to clear the breakpoints.
static int lastSteppingCounter = -1;
if (lastSteppingCounter != steppingCounter) {
CBreakPoints::ClearTemporaryBreakPoints();
host->UpdateDisassembly();
host->UpdateMemView();
lastSteppingCounter = steppingCounter;
}
// Need to check inside the lock to avoid races.
bool doStep = Core_WaitStepping();
// We may still be stepping without singleStepPending to process a save state.
if (doStep && coreState == CORE_STEPPING) {
Core_SingleStep();
// Update disasm dialog.
host->UpdateDisassembly();
host->UpdateMemView();
}
}
// Many platforms, like Android, do not call this function but handle things on their own.
// Instead they simply call NativeRender and NativeUpdate directly.
void Core_Run(GraphicsContext *ctx) {
host->UpdateDisassembly();
while (true) {
if (GetUIState() != UISTATE_INGAME) {
Core_StateProcessed();
if (GetUIState() == UISTATE_EXIT) {
UpdateRunLoop();
return;
}
Core_RunLoop(ctx);
continue;
}
switch (coreState) {
case CORE_RUNNING:
case CORE_STEPPING:
// enter a fast runloop
Core_RunLoop(ctx);
if (coreState == CORE_POWERDOWN) {
Core_StateProcessed();
return;
}
break;
case CORE_POWERUP:
case CORE_POWERDOWN:
case CORE_ERROR:
// Exit loop!!
Core_StateProcessed();
return;
case CORE_NEXTFRAME:
return;
}
}
}
void Core_EnableStepping(bool step) {
if (step) {
host->SetDebugMode(true);
Core_UpdateState(CORE_STEPPING);
steppingCounter++;
} else {
host->SetDebugMode(false);
coreState = CORE_RUNNING;
coreStatePending = false;
m_StepCond.notify_all();
}
}
int Core_GetSteppingCounter() {
return steppingCounter;
}