diff --git a/Source/Core/Common/GL/GLContext.cpp b/Source/Core/Common/GL/GLContext.cpp index da22f45c3b..0b7f0238c1 100644 --- a/Source/Core/Common/GL/GLContext.cpp +++ b/Source/Core/Common/GL/GLContext.cpp @@ -27,7 +27,7 @@ std::unique_ptr g_main_gl_context; GLContext::~GLContext() = default; -bool GLContext::Initialize(void* window_handle, bool stereo, bool core) +bool GLContext::Initialize(void* display_handle, void* window_handle, bool stereo, bool core) { return false; } @@ -37,12 +37,6 @@ bool GLContext::Initialize(GLContext* main_context) return false; } -void GLContext::SetBackBufferDimensions(u32 w, u32 h) -{ - m_backbuffer_width = w; - m_backbuffer_height = h; -} - bool GLContext::IsHeadless() const { return true; @@ -88,7 +82,8 @@ void* GLContext::GetFuncAddress(const std::string& name) return nullptr; } -std::unique_ptr GLContext::Create(void* window_handle, bool stereo, bool core) +std::unique_ptr GLContext::Create(void* display_handle, void* window_handle, bool stereo, + bool core) { std::unique_ptr context; #if defined(__APPLE__) @@ -108,7 +103,7 @@ std::unique_ptr GLContext::Create(void* window_handle, bool stereo, b #else return nullptr; #endif - if (!context->Initialize(window_handle, stereo, core)) + if (!context->Initialize(display_handle, window_handle, stereo, core)) return nullptr; return context; diff --git a/Source/Core/Common/GL/GLContext.h b/Source/Core/Common/GL/GLContext.h index 990883ca90..46df03dba8 100644 --- a/Source/Core/Common/GL/GLContext.h +++ b/Source/Core/Common/GL/GLContext.h @@ -26,7 +26,6 @@ public: u32 GetBackBufferWidth() { return m_backbuffer_width; } u32 GetBackBufferHeight() { return m_backbuffer_height; } - void SetBackBufferDimensions(u32 w, u32 h); virtual bool IsHeadless() const; @@ -45,11 +44,11 @@ public: virtual void* GetFuncAddress(const std::string& name); // Creates an instance of GLInterface specific to the platform we are running on. - static std::unique_ptr Create(void* window_handle, bool stereo = false, - bool core = true); + static std::unique_ptr Create(void* display_handle, void* window_handle, + bool stereo = false, bool core = true); protected: - virtual bool Initialize(void* window_handle, bool stereo, bool core); + virtual bool Initialize(void* display_handle, void* window_handle, bool stereo, bool core); virtual bool Initialize(GLContext* main_context); Mode m_opengl_mode = Mode::Detect; diff --git a/Source/Core/Common/GL/GLInterface/AGL.h b/Source/Core/Common/GL/GLInterface/AGL.h index 3032fe573d..00978535f8 100644 --- a/Source/Core/Common/GL/GLInterface/AGL.h +++ b/Source/Core/Common/GL/GLInterface/AGL.h @@ -31,7 +31,7 @@ public: void SwapInterval(int interval) override; protected: - bool Initialize(void* window_handle, bool stereo, bool core) override; + bool Initialize(void* display_handle, void* window_handle, bool stereo, bool core) override; bool Initialize(GLContext* main_context) override; NSView* m_view = nullptr; diff --git a/Source/Core/Common/GL/GLInterface/AGL.mm b/Source/Core/Common/GL/GLInterface/AGL.mm index 80b82f917e..4998fab40e 100644 --- a/Source/Core/Common/GL/GLInterface/AGL.mm +++ b/Source/Core/Common/GL/GLInterface/AGL.mm @@ -55,7 +55,7 @@ void GLContextAGL::Swap() // Create rendering window. // Call browser: Core.cpp:EmuThread() > main.cpp:Video_Initialize() -bool GLContextAGL::Initialize(void* window_handle, bool stereo, bool core) +bool GLContextAGL::Initialize(void* display_handle, void* window_handle, bool stereo, bool core) { NSOpenGLPixelFormatAttribute attr[] = { NSOpenGLPFADoubleBuffer, diff --git a/Source/Core/Common/GL/GLInterface/EGL.cpp b/Source/Core/Common/GL/GLInterface/EGL.cpp index 46073fe074..eebe5e2e0e 100644 --- a/Source/Core/Common/GL/GLInterface/EGL.cpp +++ b/Source/Core/Common/GL/GLInterface/EGL.cpp @@ -152,15 +152,16 @@ EGLNativeWindowType GLContextEGL::GetEGLNativeWindow(EGLConfig config) // Create rendering window. // Call browser: Core.cpp:EmuThread() > main.cpp:Video_Initialize() -bool GLContextEGL::Initialize(void* window_handle, bool stereo, bool core) +bool GLContextEGL::Initialize(void* display_handle, void* window_handle, bool stereo, bool core) { const bool has_handle = !!window_handle; EGLint egl_major, egl_minor; bool supports_core_profile = false; - m_egl_display = OpenEGLDisplay(); + m_host_display = display_handle; m_host_window = window_handle; + m_egl_display = OpenEGLDisplay(); m_is_core_context = core; if (!m_egl_display) @@ -299,6 +300,7 @@ bool GLContextEGL::Initialize(GLContext* main_context) GLContextEGL* egl_context = static_cast(main_context); m_opengl_mode = egl_context->m_opengl_mode; + m_host_display = egl_context->m_host_display; m_egl_display = egl_context->m_egl_display; m_is_core_context = egl_context->m_is_core_context; m_config = egl_context->m_config; diff --git a/Source/Core/Common/GL/GLInterface/EGL.h b/Source/Core/Common/GL/GLInterface/EGL.h index 49abd63cc1..52c183e21c 100644 --- a/Source/Core/Common/GL/GLInterface/EGL.h +++ b/Source/Core/Common/GL/GLInterface/EGL.h @@ -35,13 +35,14 @@ protected: virtual EGLDisplay OpenEGLDisplay(); virtual EGLNativeWindowType GetEGLNativeWindow(EGLConfig config); - bool Initialize(void* window_handle, bool stereo, bool core) override; + bool Initialize(void* display_handle, void* window_handle, bool stereo, bool core) override; bool Initialize(GLContext* main_context) override; bool CreateWindowSurface(); void DestroyWindowSurface(); void DetectMode(bool has_handle); + void* m_host_display = nullptr; void* m_host_window = nullptr; EGLConfig m_config; diff --git a/Source/Core/Common/GL/GLInterface/EGLX11.cpp b/Source/Core/Common/GL/GLInterface/EGLX11.cpp index 58bf3133a8..bc87769440 100644 --- a/Source/Core/Common/GL/GLInterface/EGLX11.cpp +++ b/Source/Core/Common/GL/GLInterface/EGLX11.cpp @@ -3,20 +3,19 @@ // Refer to the license.txt file included. #include "Common/GL/GLInterface/EGLX11.h" -#include "Common/Logging/Log.h" -GLContextEGLX11::~GLContextEGLX11() +GLContextEGLX11::~GLContextEGLX11() = default; + +void GLContextEGLX11::Update() { - if (m_display) - XCloseDisplay(m_display); + m_render_window->UpdateDimensions(); + m_backbuffer_width = m_render_window->GetWidth(); + m_backbuffer_height = m_render_window->GetHeight(); } EGLDisplay GLContextEGLX11::OpenEGLDisplay() { - if (!m_display) - m_display = XOpenDisplay(nullptr); - - return eglGetDisplay(m_display); + return eglGetDisplay(static_cast(m_host_display)); } EGLNativeWindowType GLContextEGLX11::GetEGLNativeWindow(EGLConfig config) @@ -28,16 +27,18 @@ EGLNativeWindowType GLContextEGLX11::GetEGLNativeWindow(EGLConfig config) visTemplate.visualid = vid; int nVisuals; - XVisualInfo* vi = XGetVisualInfo(m_display, VisualIDMask, &visTemplate, &nVisuals); + XVisualInfo* vi = + XGetVisualInfo(static_cast(m_host_display), VisualIDMask, &visTemplate, &nVisuals); - if (m_x_window) - m_x_window.reset(); + if (m_render_window) + m_render_window.reset(); - m_x_window = GLX11Window::Create(m_display, reinterpret_cast(m_host_window), vi); - m_backbuffer_width = m_x_window->GetWidth(); - m_backbuffer_height = m_x_window->GetHeight(); + m_render_window = GLX11Window::Create(static_cast(m_host_display), + reinterpret_cast(m_host_window), vi); + m_backbuffer_width = m_render_window->GetWidth(); + m_backbuffer_height = m_render_window->GetHeight(); XFree(vi); - return reinterpret_cast(m_x_window->GetWindow()); + return reinterpret_cast(m_render_window->GetWindow()); } diff --git a/Source/Core/Common/GL/GLInterface/EGLX11.h b/Source/Core/Common/GL/GLInterface/EGLX11.h index ec0e933ef7..6254d7ca0a 100644 --- a/Source/Core/Common/GL/GLInterface/EGLX11.h +++ b/Source/Core/Common/GL/GLInterface/EGLX11.h @@ -14,10 +14,11 @@ class GLContextEGLX11 : public GLContextEGL public: ~GLContextEGLX11() override; + void Update() override; + protected: EGLDisplay OpenEGLDisplay() override; EGLNativeWindowType GetEGLNativeWindow(EGLConfig config) override; - Display* m_display = nullptr; - std::unique_ptr m_x_window; + std::unique_ptr m_render_window; }; diff --git a/Source/Core/Common/GL/GLInterface/GLX.cpp b/Source/Core/Common/GL/GLInterface/GLX.cpp index 173127fc5b..c2569bd153 100644 --- a/Source/Core/Common/GL/GLInterface/GLX.cpp +++ b/Source/Core/Common/GL/GLInterface/GLX.cpp @@ -56,9 +56,9 @@ void GLContextGLX::Swap() // Create rendering window. // Call browser: Core.cpp:EmuThread() > main.cpp:Video_Initialize() -bool GLContextGLX::Initialize(void* window_handle, bool stereo, bool core) +bool GLContextGLX::Initialize(void* display_handle, void* window_handle, bool stereo, bool core) { - m_display = XOpenDisplay(nullptr); + m_display = static_cast(display_handle); int screen = DefaultScreen(m_display); // checking glx version @@ -298,16 +298,12 @@ void GLContextGLX::Shutdown() { DestroyWindowSurface(); if (m_context) - { glXDestroyContext(m_display, m_context); - - // Don't close the display connection if we are a shared context. - // Saves doing reference counting on this object, and the main context will always - // be shut down last anyway. - if (m_render_window) - { - XCloseDisplay(m_display); - m_context = nullptr; - } - } +} + +void GLContextGLX::Update() +{ + m_render_window->UpdateDimensions(); + m_backbuffer_width = m_render_window->GetWidth(); + m_backbuffer_height = m_render_window->GetHeight(); } diff --git a/Source/Core/Common/GL/GLInterface/GLX.h b/Source/Core/Common/GL/GLInterface/GLX.h index 42e36f863f..ef90e0ba5e 100644 --- a/Source/Core/Common/GL/GLInterface/GLX.h +++ b/Source/Core/Common/GL/GLInterface/GLX.h @@ -24,13 +24,15 @@ public: bool MakeCurrent() override; bool ClearCurrent() override; + void Update() override; + void SwapInterval(int Interval) override; void Swap() override; void* GetFuncAddress(const std::string& name) override; protected: - bool Initialize(void* window_handle, bool stereo, bool core) override; + bool Initialize(void* display_handle, void* window_handle, bool stereo, bool core) override; bool Initialize(GLContext* main_context) override; Display* m_display = nullptr; diff --git a/Source/Core/Common/GL/GLInterface/WGL.cpp b/Source/Core/Common/GL/GLInterface/WGL.cpp index d7c8c87c4f..6d1f072aee 100644 --- a/Source/Core/Common/GL/GLInterface/WGL.cpp +++ b/Source/Core/Common/GL/GLInterface/WGL.cpp @@ -190,7 +190,7 @@ void* GLContextWGL::GetFuncAddress(const std::string& name) // Create rendering window. // Call browser: Core.cpp:EmuThread() > main.cpp:Video_Initialize() -bool GLContextWGL::Initialize(void* window_handle, bool stereo, bool core) +bool GLContextWGL::Initialize(void* display_handle, void* window_handle, bool stereo, bool core) { if (!window_handle) return false; diff --git a/Source/Core/Common/GL/GLInterface/WGL.h b/Source/Core/Common/GL/GLInterface/WGL.h index 7e74470866..e253a1accd 100644 --- a/Source/Core/Common/GL/GLInterface/WGL.h +++ b/Source/Core/Common/GL/GLInterface/WGL.h @@ -27,7 +27,7 @@ public: void* GetFuncAddress(const std::string& name) override; protected: - bool Initialize(void* window_handle, bool stereo, bool core) override; + bool Initialize(void* display_handle, void* window_handle, bool stereo, bool core) override; bool Initialize(GLContext* main_context) override; static HGLRC CreateCoreContext(HDC dc, HGLRC share_context); diff --git a/Source/Core/Common/GL/GLX11Window.cpp b/Source/Core/Common/GL/GLX11Window.cpp index 793939377d..681adaa099 100644 --- a/Source/Core/Common/GL/GLX11Window.cpp +++ b/Source/Core/Common/GL/GLX11Window.cpp @@ -4,27 +4,30 @@ #include "Common/GL/GLX11Window.h" #include "Common/GL/GLContext.h" -#include "Common/Thread.h" GLX11Window::GLX11Window(Display* display, Window parent_window, Colormap color_map, Window window, int width, int height) : m_display(display), m_parent_window(parent_window), m_color_map(color_map), m_window(window), - m_width(width), m_height(height), - m_event_thread(std::thread(&GLX11Window::XEventThread, this)) + m_width(width), m_height(height) { } GLX11Window::~GLX11Window() { - Window window = m_window; - m_window = None; - if (m_event_thread.joinable()) - m_event_thread.join(); - XUnmapWindow(m_display, window); - XDestroyWindow(m_display, window); + XUnmapWindow(m_display, m_window); + XDestroyWindow(m_display, m_window); XFreeColormap(m_display, m_color_map); } +void GLX11Window::UpdateDimensions() +{ + XWindowAttributes attribs; + XGetWindowAttributes(m_display, m_parent_window, &attribs); + XResizeWindow(m_display, m_window, attribs.width, attribs.height); + m_width = attribs.width; + m_height = attribs.height; +} + std::unique_ptr GLX11Window::Create(Display* display, Window parent_window, XVisualInfo* vi) { @@ -48,30 +51,3 @@ std::unique_ptr GLX11Window::Create(Display* display, Window parent return std::make_unique(display, parent_window, color_map, window, parent_attribs.width, parent_attribs.height); } - -void GLX11Window::XEventThread() -{ - // There's a potential race here on m_window. But this thread will disappear soon. - while (m_window) - { - XEvent event; - for (int num_events = XPending(m_display); num_events > 0; num_events--) - { - XNextEvent(m_display, &event); - switch (event.type) - { - case ConfigureNotify: - { - m_width = event.xconfigure.width; - m_height = event.xconfigure.height; - XResizeWindow(m_display, m_window, m_width, m_height); - g_main_gl_context->SetBackBufferDimensions(m_width, m_height); - } - break; - default: - break; - } - } - Common::SleepCurrentThread(20); - } -} diff --git a/Source/Core/Common/GL/GLX11Window.h b/Source/Core/Common/GL/GLX11Window.h index 15c183c7cf..fd3020d1d8 100644 --- a/Source/Core/Common/GL/GLX11Window.h +++ b/Source/Core/Common/GL/GLX11Window.h @@ -23,12 +23,12 @@ public: int GetWidth() const { return m_width; } int GetHeight() const { return m_height; } + void UpdateDimensions(); + static std::unique_ptr Create(Display* display, Window parent_window, XVisualInfo* vi); private: - void XEventThread(); - Display* m_display; Window m_parent_window; Colormap m_color_map; @@ -36,6 +36,4 @@ private: int m_width; int m_height; - - std::thread m_event_thread; }; diff --git a/Source/Core/VideoBackends/OGL/main.cpp b/Source/Core/VideoBackends/OGL/main.cpp index 27a0192b2c..edfb861eea 100644 --- a/Source/Core/VideoBackends/OGL/main.cpp +++ b/Source/Core/VideoBackends/OGL/main.cpp @@ -161,8 +161,8 @@ bool VideoBackend::Initialize(void* display_handle, void* window_handle) { InitializeShared(); - g_main_gl_context = - GLContext::Create(window_handle, g_ActiveConfig.stereo_mode == StereoMode::QuadBuffer); + g_main_gl_context = GLContext::Create(display_handle, window_handle, + g_ActiveConfig.stereo_mode == StereoMode::QuadBuffer); if (!g_main_gl_context) return false; diff --git a/Source/Core/VideoBackends/Software/SWOGLWindow.cpp b/Source/Core/VideoBackends/Software/SWOGLWindow.cpp index dd390bc6dc..fb6731b4b7 100644 --- a/Source/Core/VideoBackends/Software/SWOGLWindow.cpp +++ b/Source/Core/VideoBackends/Software/SWOGLWindow.cpp @@ -13,9 +13,9 @@ std::unique_ptr SWOGLWindow::s_instance; -void SWOGLWindow::Init(void* window_handle) +void SWOGLWindow::Init(void* display_handle, void* window_handle) { - g_main_gl_context = GLContext::Create(window_handle); + g_main_gl_context = GLContext::Create(display_handle, window_handle); if (!g_main_gl_context) { ERROR_LOG(VIDEO, "GLInterface::Create failed."); diff --git a/Source/Core/VideoBackends/Software/SWOGLWindow.h b/Source/Core/VideoBackends/Software/SWOGLWindow.h index eadf3171cb..a393194ed7 100644 --- a/Source/Core/VideoBackends/Software/SWOGLWindow.h +++ b/Source/Core/VideoBackends/Software/SWOGLWindow.h @@ -16,7 +16,7 @@ class AbstractTexture; class SWOGLWindow { public: - static void Init(void* window_handle); + static void Init(void* display_handle, void* window_handle); static void Shutdown(); void Prepare(); diff --git a/Source/Core/VideoBackends/Software/SWmain.cpp b/Source/Core/VideoBackends/Software/SWmain.cpp index f4c0aaa641..2aa099e9e9 100644 --- a/Source/Core/VideoBackends/Software/SWmain.cpp +++ b/Source/Core/VideoBackends/Software/SWmain.cpp @@ -82,7 +82,7 @@ bool VideoSoftware::Initialize(void* display_handle, void* window_handle) { InitializeShared(); - SWOGLWindow::Init(window_handle); + SWOGLWindow::Init(display_handle, window_handle); Clipper::Init(); Rasterizer::Init();