Merge pull request #2100 from qwerasd205/mac-opengl

macos/opengl: lock context while rendering to stop resize crashes
This commit is contained in:
Mitchell Hashimoto
2024-08-15 14:25:20 -07:00
committed by GitHub
2 changed files with 18 additions and 0 deletions

View File

@ -1117,6 +1117,10 @@ fn addDeps(
step.root_module.addImport("macos", macos_dep.module("macos"));
step.linkLibrary(macos_dep.artifact("macos"));
try static_libs.append(macos_dep.artifact("macos").getEmittedBin());
if (config.renderer == .opengl) {
step.linkFramework("OpenGL");
}
}
// cimgui

View File

@ -2030,6 +2030,20 @@ pub fn drawFrame(self: *OpenGL, surface: *apprt.Surface) !void {
}
}
// In the "OpenGL Programming Guide for Mac" it explains that: "When you
// use an NSOpenGLView object with OpenGL calls that are issued from a
// thread other than the main one, you must set up mutex locking."
// This locks the context and avoids crashes that can happen due to
// races with the underlying Metal layer that Apple is using to
// implement OpenGL.
const is_darwin = builtin.target.isDarwin();
const ogl = if (comptime is_darwin) @cImport({
@cInclude("OpenGL/OpenGL.h");
}) else {};
const cgl_ctx = if (comptime is_darwin) ogl.CGLGetCurrentContext();
if (comptime is_darwin) _ = ogl.CGLLockContext(cgl_ctx);
defer _ = if (comptime is_darwin) ogl.CGLUnlockContext(cgl_ctx);
// Draw our terminal cells
try self.drawCellProgram(gl_state);