Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/app/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,7 @@ int main(int argc, char **argv) {
fullscreen_thread.request_stop();
overlay_thread.request_stop();
hook->stop();
overlay_thread.join();
overlay.shutdown();
engine.shutdown();
lizard::platform::shutdown_tray();
Expand Down
29 changes: 7 additions & 22 deletions src/overlay/overlay.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,6 @@ void stbi_image_free(void *);
#include <CoreGraphics/CoreGraphics.h>
#endif

#ifdef __APPLE__
#include <objc/message.h>
#endif

#ifndef LIZARD_TEST
#include "embedded.h"
#endif
Expand Down Expand Up @@ -464,6 +460,8 @@ bool Overlay::init(const app::Config &cfg, std::optional<std::filesystem::path>
glDeleteShader(vsId);
glDeleteShader(fsId);

platform::clear_current_context(m_window);

#endif
m_running = true;
return true;
Expand Down Expand Up @@ -716,29 +714,13 @@ void Overlay::apply_pending_config() {
void Overlay::shutdown() {
#ifndef LIZARD_TEST
stop();
// Release GL resources after the render loop has stopped
#ifdef _WIN32
if (m_window.device && m_window.glContext) {
wglMakeCurrent((HDC)m_window.device, (HGLRC)m_window.glContext);
}
#elif defined(__linux__)
if (m_window.native && m_window.glContext) {
Display *dpy = glXGetCurrentDisplay();
if (dpy) {
glXMakeCurrent(dpy, (GLXDrawable)m_window.native, (GLXContext)m_window.glContext);
}
}
#elif defined(__APPLE__)
if (m_window.glContext) {
auto makeCurrent = reinterpret_cast<void (*)(id, SEL)>(objc_msgSend);
makeCurrent((id)m_window.glContext, sel_getUid("makeCurrentContext"));
}
#endif
platform::make_context_current(m_window);
m_texture.reset();
m_vbo.reset();
m_instance.reset();
m_vao.reset();
m_program.reset();
platform::clear_current_context(m_window);
platform::destroy_window(m_window);
Comment on lines 714 to 724

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Coordinate GL context teardown with render thread

The new helper in shutdown() calls platform::make_context_current(m_window) and immediately deletes GL objects even though the render loop runs on a separate thread. In main() the exit path requests the render thread to stop and then invokes overlay.shutdown() without waiting for the thread to finish, so the context is still current on the render thread when shutdown re‑binds it or destroys it. This can leave the running thread executing render()/swap_buffers() with no current context or with a destroyed device, which on GLX/WGL typically produces GLXBadDrawable/wglDeleteContext failures or outright crashes. The shutdown path needs to join the render thread (or otherwise wait for run() to clear the context) before stealing or deleting the GL context.

Useful? React with 👍 / 👎.

#endif
}
Expand Down Expand Up @@ -888,12 +870,14 @@ void Overlay::render() {
glBindVertexArray(m_vao.id);
glBindTexture(GL_TEXTURE_2D, m_texture.id);
glDrawArraysInstanced(GL_TRIANGLE_FAN, 0, 4, static_cast<GLsizei>(m_badges.size()));
platform::swap_buffers(m_window);
platform::poll_events(m_window);
#endif
}

void Overlay::run(std::stop_token st) {
#ifndef LIZARD_TEST
platform::make_context_current(m_window);
using clock = std::chrono::steady_clock;
auto last = clock::now();
while (m_running && !st.stop_requested()) {
Expand All @@ -918,6 +902,7 @@ void Overlay::run(std::stop_token st) {
std::this_thread::sleep_for(frame - spend);
}
}
platform::clear_current_context(m_window);
stop();
#else
(void)st;
Expand Down
22 changes: 22 additions & 0 deletions src/platform/linux/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -250,4 +250,26 @@ bool fullscreen_window_present() {
return full;
}

void make_context_current(Window &window) {
std::lock_guard<std::mutex> lock(g_display_mutex);
if (g_display && window.native && window.glContext) {
glXMakeCurrent(g_display, static_cast<GLXDrawable>(reinterpret_cast<::Window>(window.native)),
window.glContext);
}
}

void clear_current_context(Window &) {
std::lock_guard<std::mutex> lock(g_display_mutex);
if (g_display) {
glXMakeCurrent(g_display, None, nullptr);
}
}

void swap_buffers(Window &window) {
std::lock_guard<std::mutex> lock(g_display_mutex);
if (g_display && window.native) {
glXSwapBuffers(g_display, static_cast<GLXDrawable>(reinterpret_cast<::Window>(window.native)));
}
}

} // namespace lizard::platform
20 changes: 20 additions & 0 deletions src/platform/mac/window.mm
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,26 @@ bool fullscreen_window_present() {
}
}

void make_context_current(Window &window) {
@autoreleasepool {
if (window.glContext) {
[(NSOpenGLContext *)window.glContext makeCurrentContext];
}
}
}

void clear_current_context(Window &) {
@autoreleasepool { [NSOpenGLContext clearCurrentContext]; }
}

void swap_buffers(Window &window) {
@autoreleasepool {
if (window.glContext) {
[(NSOpenGLContext *)window.glContext flushBuffer];
}
}
}

} // namespace lizard::platform

#endif
14 changes: 14 additions & 0 deletions src/platform/win/window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,20 @@ bool fullscreen_window_present() {
return data.full;
}

void make_context_current(Window &window) {
if (window.device && window.glContext) {
wglMakeCurrent(static_cast<HDC>(window.device), static_cast<HGLRC>(window.glContext));
}
}

void clear_current_context(Window &) { wglMakeCurrent(nullptr, nullptr); }

void swap_buffers(Window &window) {
if (window.device) {
SwapBuffers(static_cast<HDC>(window.device));
}
}

} // namespace lizard::platform

#endif
3 changes: 3 additions & 0 deletions src/platform/window.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ void destroy_window(Window &window);
void poll_events(Window &window);
bool fullscreen_window_present();
std::pair<float, float> cursor_pos();
void make_context_current(Window &window);
void clear_current_context(Window &window);
void swap_buffers(Window &window);
#if defined(__linux__)
void init_xlib_threads();
#endif
Expand Down
Loading