From 9a1b74799d350f9b4ba365bf8b118bddf517711f Mon Sep 17 00:00:00 2001
From: TSRBerry <20988865+TSRBerry@users.noreply.github.com>
Date: Sun, 11 Jun 2023 18:31:22 +0200
Subject: Ava: Fix OpenGL on Linux again (#5216)

* ava: Fix OpenGL on Linux again

This shouldn't be working like that, but for some reason it does.

* Apply the correct fix

* gtk: Add warning messages for caught exceptions

* ava: Handle disposing the same way as GTK does

* Address review feedback
---
 src/Ryujinx.Ava/AppHost.cs                         | 18 +++++++++++++----
 src/Ryujinx.Ava/UI/Renderer/EmbeddedWindow.cs      |  2 +-
 .../UI/Renderer/EmbeddedWindowOpenGL.cs            | 23 ++++++++++++++--------
 src/Ryujinx/Ui/GLRenderer.cs                       | 18 ++++++++++++-----
 4 files changed, 43 insertions(+), 18 deletions(-)

diff --git a/src/Ryujinx.Ava/AppHost.cs b/src/Ryujinx.Ava/AppHost.cs
index 0c816538..d3e0ea39 100644
--- a/src/Ryujinx.Ava/AppHost.cs
+++ b/src/Ryujinx.Ava/AppHost.cs
@@ -40,6 +40,7 @@ using SixLabors.ImageSharp;
 using SixLabors.ImageSharp.Formats.Png;
 using SixLabors.ImageSharp.PixelFormats;
 using SixLabors.ImageSharp.Processing;
+using SPB.Graphics.Exceptions;
 using SPB.Graphics.Vulkan;
 using System;
 using System.Collections.Generic;
@@ -475,11 +476,20 @@ namespace Ryujinx.Ava
                 _windowsMultimediaTimerResolution = null;
             }
 
-            (_rendererHost.EmbeddedWindow as EmbeddedWindowOpenGL)?.MakeCurrent();
+            if (_rendererHost.EmbeddedWindow is EmbeddedWindowOpenGL openGlWindow)
+            {
+                // Try to bind the OpenGL context before calling the shutdown event.
+                openGlWindow.MakeCurrent(false, false);
 
-            Device.DisposeGpu();
+                Device.DisposeGpu();
 
-            (_rendererHost.EmbeddedWindow as EmbeddedWindowOpenGL)?.MakeCurrent(null);
+                // Unbind context and destroy everything.
+                openGlWindow.MakeCurrent(true, false);
+            }
+            else
+            {
+                Device.DisposeGpu();
+            }
         }
 
         private void HideCursorState_Changed(object sender, ReactiveEventArgs<HideCursorMode> state)
@@ -930,7 +940,7 @@ namespace Ryujinx.Ava
                 _gpuDoneEvent.Set();
             });
 
-            (_rendererHost.EmbeddedWindow as EmbeddedWindowOpenGL)?.MakeCurrent(null);
+            (_rendererHost.EmbeddedWindow as EmbeddedWindowOpenGL)?.MakeCurrent(true);
         }
 
         public void UpdateStatus()
diff --git a/src/Ryujinx.Ava/UI/Renderer/EmbeddedWindow.cs b/src/Ryujinx.Ava/UI/Renderer/EmbeddedWindow.cs
index a5c8b003..3b2c32e3 100644
--- a/src/Ryujinx.Ava/UI/Renderer/EmbeddedWindow.cs
+++ b/src/Ryujinx.Ava/UI/Renderer/EmbeddedWindow.cs
@@ -123,7 +123,7 @@ namespace Ryujinx.Ava.UI.Renderer
             }
             else
             {
-                X11Window = PlatformHelper.CreateOpenGLWindow(FramebufferFormat.Default, 0, 0, 100, 100) as GLXWindow;
+                X11Window = PlatformHelper.CreateOpenGLWindow(new FramebufferFormat(new ColorFormat(8, 8, 8, 0), 16, 0, ColorFormat.Zero, 0, 2, false), 0, 0, 100, 100) as GLXWindow;
             }
 
             WindowHandle = X11Window.WindowHandle.RawHandle;
diff --git a/src/Ryujinx.Ava/UI/Renderer/EmbeddedWindowOpenGL.cs b/src/Ryujinx.Ava/UI/Renderer/EmbeddedWindowOpenGL.cs
index 305e891a..d427ab88 100644
--- a/src/Ryujinx.Ava/UI/Renderer/EmbeddedWindowOpenGL.cs
+++ b/src/Ryujinx.Ava/UI/Renderer/EmbeddedWindowOpenGL.cs
@@ -1,9 +1,11 @@
 using OpenTK.Graphics.OpenGL;
 using Ryujinx.Common.Configuration;
+using Ryujinx.Common.Logging;
 using Ryujinx.Graphics.GAL;
 using Ryujinx.Graphics.OpenGL;
 using Ryujinx.Ui.Common.Configuration;
 using SPB.Graphics;
+using SPB.Graphics.Exceptions;
 using SPB.Graphics.OpenGL;
 using SPB.Platform;
 using SPB.Platform.WGL;
@@ -18,8 +20,6 @@ namespace Ryujinx.Ava.UI.Renderer
 
         public OpenGLContextBase Context { get; set; }
 
-        public EmbeddedWindowOpenGL() { }
-
         protected override void OnWindowDestroying()
         {
             Context.Dispose();
@@ -62,14 +62,21 @@ namespace Ryujinx.Ava.UI.Renderer
             Context.MakeCurrent(null);
         }
 
-        public void MakeCurrent()
+        public void MakeCurrent(bool unbind = false, bool shouldThrow = true)
         {
-            Context?.MakeCurrent(_window);
-        }
+            try
+            {
+                Context?.MakeCurrent(!unbind ? _window : null);
+            }
+            catch (ContextException e)
+            {
+                if (shouldThrow)
+                {
+                    throw;
+                }
 
-        public void MakeCurrent(NativeWindowBase window)
-        {
-            Context?.MakeCurrent(window);
+                Logger.Warning?.Print(LogClass.Ui, $"Failed to {(!unbind ? "bind" : "unbind")} OpenGL context: {e}");
+            }
         }
 
         public void SwapBuffers()
diff --git a/src/Ryujinx/Ui/GLRenderer.cs b/src/Ryujinx/Ui/GLRenderer.cs
index e3a9804e..c5699691 100644
--- a/src/Ryujinx/Ui/GLRenderer.cs
+++ b/src/Ryujinx/Ui/GLRenderer.cs
@@ -1,8 +1,10 @@
 using OpenTK.Graphics.OpenGL;
 using Ryujinx.Common.Configuration;
+using Ryujinx.Common.Logging;
 using Ryujinx.Graphics.OpenGL;
 using Ryujinx.Input.HLE;
 using SPB.Graphics;
+using SPB.Graphics.Exceptions;
 using SPB.Graphics.OpenGL;
 using SPB.Platform;
 using SPB.Platform.GLX;
@@ -112,24 +114,30 @@ namespace Ryujinx.Ui
 
         protected override void Dispose(bool disposing)
         {
-            // Try to bind the OpenGL context before calling the shutdown event
+            // Try to bind the OpenGL context before calling the shutdown event.
             try
             {
                 _openGLContext?.MakeCurrent(_nativeWindow);
             }
-            catch (Exception) { }
+            catch (ContextException e)
+            {
+                Logger.Warning?.Print(LogClass.Ui, $"Failed to bind OpenGL context: {e}");
+            }
 
             Device?.DisposeGpu();
             NpadManager.Dispose();
 
-            // Unbind context and destroy everything
+            // Unbind context and destroy everything.
             try
             {
                 _openGLContext?.MakeCurrent(null);
             }
-            catch (Exception) { }
+            catch (ContextException e)
+            {
+                Logger.Warning?.Print(LogClass.Ui, $"Failed to unbind OpenGL context: {e}");
+            }
 
             _openGLContext?.Dispose();
         }
     }
-}
+}
\ No newline at end of file
-- 
cgit v1.2.3-70-g09d2