From 204c031fefbec75c75711f7574f9d5b4f0ee051f Mon Sep 17 00:00:00 2001 From: merry Date: Fri, 2 Dec 2022 13:37:22 +0000 Subject: [PATCH] SDL2Driver: Invoke dispatcher on main thread (#3818) --- Ryujinx.Ava/Program.cs | 4 ++++ Ryujinx.Headless.SDL2/Program.cs | 11 +---------- Ryujinx.Headless.SDL2/WindowBase.cs | 16 ++++++++-------- Ryujinx.SDL2.Common/SDL2Driver.cs | 11 ++++++++--- Ryujinx/Program.cs | 10 ++++++++++ 5 files changed, 31 insertions(+), 21 deletions(-) diff --git a/Ryujinx.Ava/Program.cs b/Ryujinx.Ava/Program.cs index 91040397..9ffbf9e3 100644 --- a/Ryujinx.Ava/Program.cs +++ b/Ryujinx.Ava/Program.cs @@ -8,6 +8,7 @@ using Ryujinx.Common.Logging; using Ryujinx.Common.System; using Ryujinx.Common.SystemInfo; using Ryujinx.Modules; +using Ryujinx.SDL2.Common; using Ryujinx.Ui.Common; using Ryujinx.Ui.Common.Configuration; using Ryujinx.Ui.Common.Helper; @@ -94,6 +95,9 @@ namespace Ryujinx.Ava // Initialize Discord integration. DiscordIntegrationModule.Initialize(); + // Initialize SDL2 driver + SDL2Driver.MainThreadDispatcher = action => Dispatcher.UIThread.InvokeAsync(action, DispatcherPriority.Input); + ReloadConfig(); ForceDpiAware.Windows(); diff --git a/Ryujinx.Headless.SDL2/Program.cs b/Ryujinx.Headless.SDL2/Program.cs index 168e826f..bfc33edc 100644 --- a/Ryujinx.Headless.SDL2/Program.cs +++ b/Ryujinx.Headless.SDL2/Program.cs @@ -638,16 +638,7 @@ namespace Ryujinx.Headless.SDL2 Translator.IsReadyForTranslation.Reset(); - Thread windowThread = new Thread(() => - { - ExecutionEntrypoint(); - }) - { - Name = "GUI.WindowThread" - }; - - windowThread.Start(); - windowThread.Join(); + ExecutionEntrypoint(); return true; } diff --git a/Ryujinx.Headless.SDL2/WindowBase.cs b/Ryujinx.Headless.SDL2/WindowBase.cs index 2d6963dd..9aa17936 100644 --- a/Ryujinx.Headless.SDL2/WindowBase.cs +++ b/Ryujinx.Headless.SDL2/WindowBase.cs @@ -168,14 +168,6 @@ namespace Ryujinx.Headless.SDL2 public void Render() { - InitializeWindowRenderer(); - - Device.Gpu.Renderer.Initialize(_glLogLevel); - - InitializeRenderer(); - - _gpuVendorName = GetGpuVendorName(); - Device.Gpu.Renderer.RunLoop(() => { Device.Gpu.SetGpuThread(); @@ -323,6 +315,14 @@ namespace Ryujinx.Headless.SDL2 InitializeWindow(); + InitializeWindowRenderer(); + + Device.Gpu.Renderer.Initialize(_glLogLevel); + + InitializeRenderer(); + + _gpuVendorName = GetGpuVendorName(); + Thread renderLoopThread = new Thread(Render) { Name = "GUI.RenderLoop" diff --git a/Ryujinx.SDL2.Common/SDL2Driver.cs b/Ryujinx.SDL2.Common/SDL2Driver.cs index 9d8dacf8..7aa2d584 100644 --- a/Ryujinx.SDL2.Common/SDL2Driver.cs +++ b/Ryujinx.SDL2.Common/SDL2Driver.cs @@ -28,6 +28,8 @@ namespace Ryujinx.SDL2.Common } } + public static Action MainThreadDispatcher { get; set; } + private const uint SdlInitFlags = SDL_INIT_EVENTS | SDL_INIT_GAMECONTROLLER | SDL_INIT_JOYSTICK | SDL_INIT_AUDIO | SDL_INIT_VIDEO; private bool _isRunning; @@ -154,10 +156,13 @@ namespace Ryujinx.SDL2.Common while (_isRunning) { - while (SDL_PollEvent(out SDL_Event evnt) != 0) + MainThreadDispatcher?.Invoke(() => { - HandleSDLEvent(ref evnt); - } + while (SDL_PollEvent(out SDL_Event evnt) != 0) + { + HandleSDLEvent(ref evnt); + } + }); waitHandle.Wait(WaitTimeMs); } diff --git a/Ryujinx/Program.cs b/Ryujinx/Program.cs index be790a48..162bd89d 100644 --- a/Ryujinx/Program.cs +++ b/Ryujinx/Program.cs @@ -7,6 +7,7 @@ using Ryujinx.Common.Logging; using Ryujinx.Common.System; using Ryujinx.Common.SystemInfo; using Ryujinx.Modules; +using Ryujinx.SDL2.Common; using Ryujinx.Ui; using Ryujinx.Ui.Common; using Ryujinx.Ui.Common.Configuration; @@ -111,6 +112,15 @@ namespace Ryujinx // Initialize Discord integration. DiscordIntegrationModule.Initialize(); + // Initialize SDL2 driver + SDL2Driver.MainThreadDispatcher = action => + { + Gtk.Application.Invoke(delegate + { + action(); + }); + }; + // Sets ImageSharp Jpeg Encoder Quality. SixLabors.ImageSharp.Configuration.Default.ImageFormatsManager.SetEncoder(JpegFormat.Instance, new JpegEncoder() {