From e6f0d77aa00f490875430c95ab30c4157ace5c05 Mon Sep 17 00:00:00 2001
From: Arisotura <thetotalworm@gmail.com>
Date: Sun, 27 Oct 2024 10:17:59 +0100
Subject: [PATCH] fix freeze when starting new emu instance while using OpenGL

---
 src/frontend/qt_sdl/EmuInstance.cpp |  2 +-
 src/frontend/qt_sdl/Screen.cpp      |  4 +++-
 src/frontend/qt_sdl/Window.cpp      | 24 ++++++++++++++++--------
 src/frontend/qt_sdl/Window.h        |  2 +-
 4 files changed, 21 insertions(+), 11 deletions(-)

diff --git a/src/frontend/qt_sdl/EmuInstance.cpp b/src/frontend/qt_sdl/EmuInstance.cpp
index 7fc73670..3906b186 100644
--- a/src/frontend/qt_sdl/EmuInstance.cpp
+++ b/src/frontend/qt_sdl/EmuInstance.cpp
@@ -201,7 +201,7 @@ void EmuInstance::createWindow()
     emuThread->attachWindow(win);
 
     // if creating a secondary window, we may need to initialize its OpenGL context here
-    if (win->hasOpenGL() && (win != topWindow))
+    if (win->hasOpenGL() && (id != 0))
         emuThread->initContext(id);
 }
 
diff --git a/src/frontend/qt_sdl/Screen.cpp b/src/frontend/qt_sdl/Screen.cpp
index ff049120..37c35a65 100644
--- a/src/frontend/qt_sdl/Screen.cpp
+++ b/src/frontend/qt_sdl/Screen.cpp
@@ -749,8 +749,10 @@ bool ScreenPanelGL::createContext()
 
     // if our parent window is parented to another window, we will
     // share our OpenGL context with that window
+    MainWindow* ourwin = (MainWindow*)parentWidget();
     MainWindow* parentwin = (MainWindow*)parentWidget()->parentWidget();
-    if (parentwin)
+    //if (parentwin)
+    if (ourwin->getWindowID() != 0)
     {
         if (windowinfo.has_value())
             if (glContext = parentwin->getOGLContext()->CreateSharedContext(*windowinfo))
diff --git a/src/frontend/qt_sdl/Window.cpp b/src/frontend/qt_sdl/Window.cpp
index b44fed7a..a231a6f8 100644
--- a/src/frontend/qt_sdl/Window.cpp
+++ b/src/frontend/qt_sdl/Window.cpp
@@ -2147,9 +2147,14 @@ void MainWindow::onEmuReset()
 
 void MainWindow::onUpdateVideoSettings(bool glchange)
 {
-    MainWindow* parentwin = (MainWindow*)parentWidget();
-    if (parentwin)
-        return parentwin->onUpdateVideoSettings(glchange);
+    if (windowID != 0)
+    {
+        MainWindow* parentwin = (MainWindow*)parentWidget();
+        if (parentwin)
+            parentwin->onUpdateVideoSettings(glchange);
+
+        return;
+    }
 
     bool hadOGL = hasOGL;
     if (glchange)
@@ -2166,12 +2171,15 @@ void MainWindow::onUpdateVideoSettings(bool glchange)
     {
         if (hasOGL) emuThread->initContext(windowID);
 
-        auto childwins = findChildren<MainWindow*>(nullptr, Qt::FindDirectChildrenOnly);
-        for (auto child : childwins)
+        if (windowID == 0)
         {
-            if (hadOGL) emuThread->deinitContext(child->windowID);
-            child->createScreenPanel();
-            if (hasOGL) emuThread->initContext(child->windowID);
+            auto childwins = findChildren<MainWindow *>(nullptr, Qt::FindDirectChildrenOnly);
+            for (auto child: childwins)
+            {
+                if (hadOGL) emuThread->deinitContext(child->windowID);
+                child->createScreenPanel();
+                if (hasOGL) emuThread->initContext(child->windowID);
+            }
         }
 
         emuThread->emuUnpause();
diff --git a/src/frontend/qt_sdl/Window.h b/src/frontend/qt_sdl/Window.h
index a344c1e3..73bb84bb 100644
--- a/src/frontend/qt_sdl/Window.h
+++ b/src/frontend/qt_sdl/Window.h
@@ -110,7 +110,7 @@ public:
 
     EmuInstance* getEmuInstance() { return emuInstance; }
     Config::Table& getWindowConfig() { return windowCfg; }
-    int getID() { return windowID; }
+    int getWindowID() { return windowID; }
 
     bool winHasMenu() { return hasMenu; }