Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move irrelevant code out of Core.cpp #19691

Merged
merged 1 commit into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Common/System/NativeApp.h
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,10 @@ inline bool NativeClearSecret(std::string_view nameOfSecret) {
}
// On failure, returns an empty string. Good enough since any real secret is non-empty.
std::string NativeLoadSecret(std::string_view nameOfSecret);

// Don't run the core when minimized etc.
void Native_NotifyWindowHidden(bool hidden);
bool Native_IsWindowHidden();

// TODO: Feels like this belongs elsewhere.
bool Native_UpdateScreenScale(int width, int height);
117 changes: 1 addition & 116 deletions Core/Core.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ static int steppingCounter = 0;
static std::set<CoreLifecycleFunc> lifecycleFuncs;
static std::set<CoreStopRequestFunc> stopFuncs;

static bool windowHidden = false;
static bool powerSaving = false;

static MIPSExceptionInfo g_exceptionInfo;
Expand All @@ -88,15 +87,6 @@ void Core_SetGraphicsContext(GraphicsContext *ctx) {
PSP_CoreParameter().graphicsContext = ctx;
}

void Core_NotifyWindowHidden(bool hidden) {
windowHidden = hidden;
// TODO: Wait until we can react?
}

bool Core_IsWindowHidden() {
return windowHidden;
}

void Core_ListenLifecycle(CoreLifecycleFunc func) {
lifecycleFuncs.insert(func);
}
Expand Down Expand Up @@ -144,7 +134,7 @@ bool Core_IsInactive() {
return coreState != CORE_RUNNING_CPU && coreState != CORE_NEXTFRAME && !coreStatePending;
}

static inline void Core_StateProcessed() {
void Core_StateProcessed() {
if (coreStatePending) {
std::lock_guard<std::mutex> guard(m_hInactiveMutex);
coreStatePending = false;
Expand Down Expand Up @@ -174,67 +164,6 @@ 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_display.dpi_scale_x);
int h = (int)(pixelHeight * g_display.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;

float g_logical_dpi = System_GetPropertyFloat(SYSPROP_DISPLAY_LOGICAL_DPI);
g_display.dpi = System_GetPropertyFloat(SYSPROP_DISPLAY_DPI);

if (g_display.dpi < 0.0f) {
g_display.dpi = 96.0f;
}
if (g_logical_dpi < 0.0f) {
g_logical_dpi = 96.0f;
}

g_display.dpi_scale_x = g_logical_dpi / g_display.dpi;
g_display.dpi_scale_y = g_logical_dpi / g_display.dpi;
g_display.dpi_scale_real_x = g_display.dpi_scale_x;
g_display.dpi_scale_real_y = g_display.dpi_scale_y;

smallWindow = IsWindowSmall(width, height);
if (smallWindow) {
g_display.dpi /= 2.0f;
g_display.dpi_scale_x *= 2.0f;
g_display.dpi_scale_y *= 2.0f;
}
g_display.pixel_in_dps_x = 1.0f / g_display.dpi_scale_x;
g_display.pixel_in_dps_y = 1.0f / g_display.dpi_scale_y;

int new_dp_xres = (int)(width * g_display.dpi_scale_x);
int new_dp_yres = (int)(height * g_display.dpi_scale_y);

bool dp_changed = new_dp_xres != g_display.dp_xres || new_dp_yres != g_display.dp_yres;
bool px_changed = g_display.pixel_xres != width || g_display.pixel_yres != height;

if (dp_changed || px_changed) {
g_display.dp_xres = new_dp_xres;
g_display.dp_yres = new_dp_yres;
g_display.pixel_xres = width;
g_display.pixel_yres = height;
NativeResized();
return true;
}
return false;
}

// Used by Windows, SDL, Qt. Doesn't belong in this file.
void Core_RunLoop(GraphicsContext *ctx) {
if (windowHidden && g_Config.bPauseWhenMinimized) {
sleep_ms(16, "window-hidden");
return;
}
NativeFrame(ctx);
}

bool Core_RequestSingleStep(CPUStepType type, int stepSize) {
std::lock_guard<std::mutex> guard(g_stepMutex);
if (g_stepCommand.type != CPUStepType::None) {
Expand Down Expand Up @@ -389,50 +318,6 @@ void Core_ProcessStepping(MIPSDebugInterface *cpu) {
System_Notify(SystemNotification::MEM_VIEW);
}

// Many platforms, like Android, do not call this function but handle things on their own.
// Instead they simply call NativeFrame directly.
bool Core_Run(GraphicsContext *ctx) {
System_Notify(SystemNotification::DISASSEMBLY);
while (true) {
if (GetUIState() != UISTATE_INGAME) {
Core_StateProcessed();
if (GetUIState() == UISTATE_EXIT) {
// Not sure why we do a final frame here?
NativeFrame(ctx);
return false;
}
Core_RunLoop(ctx);
continue;
}

switch (coreState) {
case CORE_RUNNING_CPU:
case CORE_STEPPING_CPU:
case CORE_RUNNING_GE:
case CORE_STEPPING_GE:
// enter a fast runloop
Core_RunLoop(ctx);
if (coreState == CORE_POWERDOWN) {
return true;
}
break;
case CORE_POWERDOWN:
// Need to step the loop.
Core_RunLoop(ctx);
return true;

case CORE_POWERUP:
case CORE_BOOT_ERROR:
case CORE_RUNTIME_ERROR:
// Exit loop!!
return true;

case CORE_NEXTFRAME:
return true;
}
}
}

// Free-threaded (hm, possibly except tracing).
void Core_Break(const char *reason, u32 relatedAddress) {
// Stop the tracer
Expand Down
13 changes: 3 additions & 10 deletions Core/Core.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,10 @@

class GraphicsContext;

void Core_RunLoop(GraphicsContext *ctx);

// For platforms that don't call Core_Run
void Core_SetGraphicsContext(GraphicsContext *ctx);

// Returns false when an UI exit state is detected.
bool Core_Run(GraphicsContext *ctx);
void Core_Stop();

// X11, sigh.
Expand Down Expand Up @@ -95,16 +92,12 @@ bool Core_IsStepping();

bool Core_IsActive();
bool Core_IsInactive();
// Warning: these currently work only on Windows.

// Warning: these three are only used on Windows - debugger integration.
void Core_StateProcessed();
void Core_WaitInactive();
void Core_WaitInactive(int milliseconds);

bool UpdateScreenScale(int width, int height);

// Don't run the core when minimized etc.
void Core_NotifyWindowHidden(bool hidden);
bool Core_IsWindowHidden();

void Core_SetPowerSaving(bool mode);
bool Core_GetPowerSaving();

Expand Down
8 changes: 4 additions & 4 deletions Qt/QtMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ void MainUI::EmuThreadFunc() {
emuThreadState = (int)EmuThreadState::RUNNING;
while (emuThreadState != (int)EmuThreadState::QUIT_REQUESTED) {
updateAccelerometer();
Core_RunLoop(graphicsContext);
NativeFrame(graphicsContext);
}
emuThreadState = (int)EmuThreadState::STOPPED;

Expand Down Expand Up @@ -548,7 +548,7 @@ QString MainUI::InputBoxGetQString(QString title, QString defaultValue) {
}

void MainUI::resizeGL(int w, int h) {
if (UpdateScreenScale(w, h)) {
if (Native_UpdateScreenScale(w, h)) {
System_PostUIMessage(UIMessage::GPU_RENDER_RESIZED);
}
xscale = w / this->width();
Expand All @@ -566,7 +566,7 @@ void MainUI::timerEvent(QTimerEvent *) {
void MainUI::changeEvent(QEvent *e) {
QGLWidget::changeEvent(e);
if (e->type() == QEvent::WindowStateChange)
Core_NotifyWindowHidden(isMinimized());
Native_NotifyWindowHidden(isMinimized());
}

bool MainUI::event(QEvent *e) {
Expand Down Expand Up @@ -726,7 +726,7 @@ void MainUI::paintGL() {
#endif
updateAccelerometer();
if (emuThreadState == (int)EmuThreadState::DISABLED) {
Core_RunLoop(graphicsContext);
NativeFrame(graphicsContext);
} else {
graphicsContext->ThreadFrame();
// Do the rest in EmuThreadFunc
Expand Down
2 changes: 1 addition & 1 deletion Qt/mainwindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class MainWindow : public QMainWindow
QMainWindow::changeEvent(e);
// Does not work on Linux for Qt5.2 or Qt5.3 (Qt bug)
if(e->type() == QEvent::WindowStateChange)
Core_NotifyWindowHidden(isMinimized());
Native_NotifyWindowHidden(isMinimized());
}

void closeEvent(QCloseEvent *) { exitAct(); }
Expand Down
20 changes: 10 additions & 10 deletions SDL/SDLMain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ static void EmuThreadFunc(GraphicsContext *graphicsContext) {
NativeInitGraphics(graphicsContext);

while (emuThreadState != (int)EmuThreadState::QUIT_REQUESTED) {
Core_RunLoop(graphicsContext);
NativeFrame(graphicsContext);
}
emuThreadState = (int)EmuThreadState::STOPPED;
graphicsContext->StopThread();
Expand Down Expand Up @@ -737,7 +737,7 @@ static void ProcessSDLEvent(SDL_Window *window, const SDL_Event &event, InputSta
// We have to juggle around 3 kinds of "DPI spaces" if a logical DPI is
// provided (through --dpi, it is equal to system DPI if unspecified):
// - SDL gives us motion events in "system DPI" points
// - UpdateScreenScale expects pixels, so in a way "96 DPI" points
// - Native_UpdateScreenScale expects pixels, so in a way "96 DPI" points
// - The UI code expects motion events in "logical DPI" points
float mx = event.motion.x * g_DesktopDPI * g_display.dpi_scale_x;
float my = event.motion.y * g_DesktopDPI * g_display.dpi_scale_x;
Expand All @@ -756,17 +756,17 @@ static void ProcessSDLEvent(SDL_Window *window, const SDL_Event &event, InputSta
int new_height = event.window.data2;

// The size given by SDL is in point-units, convert these to
// pixels before passing to UpdateScreenScale()
// pixels before passing to Native_UpdateScreenScale()
int new_width_px = new_width * g_DesktopDPI;
int new_height_px = new_height * g_DesktopDPI;

Core_NotifyWindowHidden(false);
Native_NotifyWindowHidden(false);

Uint32 window_flags = SDL_GetWindowFlags(window);
bool fullscreen = (window_flags & SDL_WINDOW_FULLSCREEN);

// This one calls NativeResized if the size changed.
UpdateScreenScale(new_width_px, new_height_px);
Native_UpdateScreenScale(new_width_px, new_height_px);

// Set variable here in case fullscreen was toggled by hotkey
if (g_Config.UseFullScreen() != fullscreen) {
Expand Down Expand Up @@ -804,11 +804,11 @@ static void ProcessSDLEvent(SDL_Window *window, const SDL_Event &event, InputSta

case SDL_WINDOWEVENT_MINIMIZED:
case SDL_WINDOWEVENT_HIDDEN:
Core_NotifyWindowHidden(true);
Native_NotifyWindowHidden(true);
break;
case SDL_WINDOWEVENT_EXPOSED:
case SDL_WINDOWEVENT_SHOWN:
Core_NotifyWindowHidden(false);
Native_NotifyWindowHidden(false);
break;
default:
break;
Expand Down Expand Up @@ -1426,7 +1426,7 @@ int main(int argc, char *argv[]) {

float dpi_scale = 1.0f / (g_ForcedDPI == 0.0f ? g_DesktopDPI : g_ForcedDPI);

UpdateScreenScale(w * g_DesktopDPI, h * g_DesktopDPI);
Native_UpdateScreenScale(w * g_DesktopDPI, h * g_DesktopDPI);

bool mainThreadIsRender = g_Config.iGPUBackend == (int)GPUBackend::OPENGL;

Expand Down Expand Up @@ -1535,7 +1535,7 @@ int main(int argc, char *argv[]) {
if (g_QuitRequested || g_RestartRequested)
break;
if (emuThreadState == (int)EmuThreadState::DISABLED) {
Core_RunLoop(graphicsContext);
NativeFrame(graphicsContext);
}
if (g_QuitRequested || g_RestartRequested)
break;
Expand All @@ -1545,7 +1545,7 @@ int main(int argc, char *argv[]) {

inputTracker.MouseCaptureControl();

bool renderThreadPaused = Core_IsWindowHidden() && g_Config.bPauseWhenMinimized && emuThreadState != (int)EmuThreadState::DISABLED;
bool renderThreadPaused = Native_IsWindowHidden() && g_Config.bPauseWhenMinimized && emuThreadState != (int)EmuThreadState::DISABLED;
if (emuThreadState != (int)EmuThreadState::DISABLED && !renderThreadPaused) {
if (!graphicsContext->ThreadFrame())
break;
Expand Down
Loading
Loading