Merge pull request #9870 from OatmealDome/ui-thread

macOS: Move UI API calls to the main thread
This commit is contained in:
Léo Lam 2021-07-13 03:29:49 +02:00 committed by GitHub
commit 162af2a7bb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 40 additions and 14 deletions

View File

@ -3,6 +3,9 @@
#pragma once #pragma once
// This define will silence all "OpenGL is deprecated, use Metal" warnings.
#define GL_SILENCE_DEPRECATION 1
#if defined(__APPLE__) && defined(__OBJC__) #if defined(__APPLE__) && defined(__OBJC__)
#import <AppKit/AppKit.h> #import <AppKit/AppKit.h>
#else #else

View File

@ -4,6 +4,9 @@
#include "Common/GL/GLInterface/AGL.h" #include "Common/GL/GLInterface/AGL.h"
#include "Common/Logging/Log.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) static bool UpdateCachedDimensions(NSView* view, u32* width, u32* height)
{ {
NSWindow* window = [view window]; NSWindow* window = [view window];
@ -35,12 +38,10 @@ static bool AttachContextToView(NSOpenGLContext* context, NSView* view, u32* wid
(void)UpdateCachedDimensions(view, width, height); (void)UpdateCachedDimensions(view, width, height);
// the following calls can crash if not called from the main thread on macOS 10.15 [window makeFirstResponder:view];
dispatch_sync(dispatch_get_main_queue(), ^{ [context setView:view];
[window makeFirstResponder:view]; [window makeKeyAndOrderFront:nil];
[context setView:view];
[window makeKeyAndOrderFront:nil];
});
return true; return true;
} }
@ -98,8 +99,16 @@ bool GLContextAGL::Initialize(const WindowSystemInfo& wsi, bool stereo, bool cor
m_view = static_cast<NSView*>(wsi.render_surface); m_view = static_cast<NSView*>(wsi.render_surface);
m_opengl_mode = Mode::OpenGL; 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; return false;
}
[m_context makeCurrentContext]; [m_context makeCurrentContext];
return true; return true;
@ -140,11 +149,12 @@ void GLContextAGL::Update()
if (!m_view) if (!m_view)
return; return;
if (UpdateCachedDimensions(m_view, &m_backbuffer_width, &m_backbuffer_height)) dispatch_sync(dispatch_get_main_queue(), ^{
// the following calls can crash if not called from the main thread on macOS 10.15 if (UpdateCachedDimensions(m_view, &m_backbuffer_width, &m_backbuffer_height))
dispatch_sync(dispatch_get_main_queue(), ^{ {
[m_context update]; [m_context update];
}); }
});
} }
void GLContextAGL::SwapInterval(int interval) void GLContextAGL::SwapInterval(int interval)

View File

@ -58,7 +58,7 @@ private:
public: public:
void UpdateInput() override; void UpdateInput() override;
explicit KeyboardAndMouse(void* window); explicit KeyboardAndMouse(void* view);
std::string GetName() const override; std::string GetName() const override;
std::string GetSource() const override; std::string GetSource() const override;

View File

@ -135,7 +135,7 @@ std::string KeyboardAndMouse::Key::GetName() const
return m_name; return m_name;
} }
KeyboardAndMouse::KeyboardAndMouse(void* window) KeyboardAndMouse::KeyboardAndMouse(void* view)
{ {
// All keycodes in <HIToolbox/Events.h> are 0x7e or lower. If you notice // All keycodes in <HIToolbox/Events.h> are 0x7e or lower. If you notice
// keys that aren't being recognized, bump this number up! // 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("Shift", {"Left Shift", "Right Shift"});
AddCombinedInput("Ctrl", {"Left Control", "Right Control"}); AddCombinedInput("Ctrl", {"Left Control", "Right Control"});
m_windowid = [[reinterpret_cast<NSView*>(window) window] windowNumber]; NSView* cocoa_view = reinterpret_cast<NSView*>(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 // cursor, with a hax for-loop
for (unsigned int i = 0; i < 4; ++i) for (unsigned int i = 0; i < 4; ++i)