From 1bb72f00b5216869c7346a4fda61047d4abb244c Mon Sep 17 00:00:00 2001 From: OatmealDome Date: Tue, 6 Jul 2021 04:46:27 -0400 Subject: [PATCH 1/3] QuartzKeyboardAndMouse: Ensure windowNumber is fetched on the main thread --- .../Quartz/QuartzKeyboardAndMouse.h | 2 +- .../Quartz/QuartzKeyboardAndMouse.mm | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.h b/Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.h index aee721f9d9..dfcf216e45 100644 --- a/Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.h +++ b/Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.h @@ -58,7 +58,7 @@ private: public: void UpdateInput() override; - explicit KeyboardAndMouse(void* window); + explicit KeyboardAndMouse(void* view); std::string GetName() const override; std::string GetSource() const override; diff --git a/Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.mm b/Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.mm index 1d55b07795..7aae59bf95 100644 --- a/Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.mm +++ b/Source/Core/InputCommon/ControllerInterface/Quartz/QuartzKeyboardAndMouse.mm @@ -135,7 +135,7 @@ std::string KeyboardAndMouse::Key::GetName() const return m_name; } -KeyboardAndMouse::KeyboardAndMouse(void* window) +KeyboardAndMouse::KeyboardAndMouse(void* view) { // All keycodes in are 0x7e or lower. If you notice // keys that aren't being recognized, bump this number up! @@ -147,7 +147,20 @@ KeyboardAndMouse::KeyboardAndMouse(void* window) AddCombinedInput("Shift", {"Left Shift", "Right Shift"}); AddCombinedInput("Ctrl", {"Left Control", "Right Control"}); - m_windowid = [[reinterpret_cast(window) window] windowNumber]; + NSView* cocoa_view = reinterpret_cast(view); + + // PopulateDevices may be called on the Emuthread, so we need to ensure that + // these UI APIs are only ever called on the main thread. + if ([NSThread isMainThread]) + { + m_windowid = [[cocoa_view window] windowNumber]; + } + else + { + dispatch_sync(dispatch_get_main_queue(), ^{ + m_windowid = [[cocoa_view window] windowNumber]; + }); + } // cursor, with a hax for-loop for (unsigned int i = 0; i < 4; ++i) From 4d944342fcf7ef9bfcf3d0dd75c0d8032850eb62 Mon Sep 17 00:00:00 2001 From: OatmealDome Date: Tue, 6 Jul 2021 14:30:15 -0400 Subject: [PATCH 2/3] AGL: Move more UI API calls to the main thread --- Source/Core/Common/GL/GLInterface/AGL.mm | 32 ++++++++++++++++-------- 1 file changed, 21 insertions(+), 11 deletions(-) diff --git a/Source/Core/Common/GL/GLInterface/AGL.mm b/Source/Core/Common/GL/GLInterface/AGL.mm index 3649cd0a76..a6fc7f0c0e 100644 --- a/Source/Core/Common/GL/GLInterface/AGL.mm +++ b/Source/Core/Common/GL/GLInterface/AGL.mm @@ -4,6 +4,9 @@ #include "Common/GL/GLInterface/AGL.h" #include "Common/Logging/Log.h" +// UpdateCachedDimensions and AttachContextToView contain calls to UI APIs, so they must only be +// called from the main thread or they risk crashing! + static bool UpdateCachedDimensions(NSView* view, u32* width, u32* height) { NSWindow* window = [view window]; @@ -35,12 +38,10 @@ static bool AttachContextToView(NSOpenGLContext* context, NSView* view, u32* wid (void)UpdateCachedDimensions(view, width, height); - // the following calls can crash if not called from the main thread on macOS 10.15 - dispatch_sync(dispatch_get_main_queue(), ^{ - [window makeFirstResponder:view]; - [context setView:view]; - [window makeKeyAndOrderFront:nil]; - }); + [window makeFirstResponder:view]; + [context setView:view]; + [window makeKeyAndOrderFront:nil]; + return true; } @@ -98,8 +99,16 @@ bool GLContextAGL::Initialize(const WindowSystemInfo& wsi, bool stereo, bool cor m_view = static_cast(wsi.render_surface); m_opengl_mode = Mode::OpenGL; - if (!AttachContextToView(m_context, m_view, &m_backbuffer_width, &m_backbuffer_height)) + + __block bool success; + dispatch_sync(dispatch_get_main_queue(), ^{ + success = AttachContextToView(m_context, m_view, &m_backbuffer_width, &m_backbuffer_height); + }); + + if (!success) + { return false; + } [m_context makeCurrentContext]; return true; @@ -140,11 +149,12 @@ void GLContextAGL::Update() if (!m_view) return; - if (UpdateCachedDimensions(m_view, &m_backbuffer_width, &m_backbuffer_height)) - // the following calls can crash if not called from the main thread on macOS 10.15 - dispatch_sync(dispatch_get_main_queue(), ^{ + dispatch_sync(dispatch_get_main_queue(), ^{ + if (UpdateCachedDimensions(m_view, &m_backbuffer_width, &m_backbuffer_height)) + { [m_context update]; - }); + } + }); } void GLContextAGL::SwapInterval(int interval) From 8c728945fba74caa62d7ee81b523074c16300719 Mon Sep 17 00:00:00 2001 From: OatmealDome Date: Tue, 6 Jul 2021 14:31:01 -0400 Subject: [PATCH 3/3] AGL: Silence OpenGL deprecation warnings --- Source/Core/Common/GL/GLInterface/AGL.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Source/Core/Common/GL/GLInterface/AGL.h b/Source/Core/Common/GL/GLInterface/AGL.h index cb3b03974e..dda9fee0e3 100644 --- a/Source/Core/Common/GL/GLInterface/AGL.h +++ b/Source/Core/Common/GL/GLInterface/AGL.h @@ -3,6 +3,9 @@ #pragma once +// This define will silence all "OpenGL is deprecated, use Metal" warnings. +#define GL_SILENCE_DEPRECATION 1 + #if defined(__APPLE__) && defined(__OBJC__) #import #else