setup app, run works but crashes in OpenGL

This commit is contained in:
Mitchell Hashimoto
2023-02-21 10:18:04 -08:00
parent f268f3955e
commit d368b8e727
3 changed files with 59 additions and 22 deletions

View File

@ -30,6 +30,16 @@ pub const App = struct {
)) orelse return error.GtkInitFailed;
errdefer c.g_object_unref(app);
// Setup our callbacks
_ = c.g_signal_connect_data(
app,
"activate",
c.G_CALLBACK(&activate),
null,
null,
c.G_CONNECT_DEFAULT,
);
// We don't use g_application_run, we want to manually control the
// loop so we have to do the same things the run function does:
// https://github.com/GNOME/glib/blob/a8e8b742e7926e33eb635a8edceac74cf239d6ed/gio/gapplication.c#L2533
@ -51,6 +61,9 @@ pub const App = struct {
return error.GtkApplicationRegisterFailed;
}
// This just calls the "activate" signal but its part of the normal
// startup routine so we just call it:
// https://gitlab.gnome.org/GNOME/glib/-/blob/bd2ccc2f69ecfd78ca3f34ab59e42e2b462bad65/gio/gapplication.c#L2302
c.g_application_activate(gapp);
return .{ .app = app, .ctx = ctx };
@ -71,6 +84,12 @@ pub const App = struct {
pub fn wait(self: App) !void {
_ = c.g_main_context_iteration(self.ctx, 1);
}
fn activate(app: *c.GtkApplication, ud: ?*anyopaque) callconv(.C) void {
_ = app;
_ = ud;
log.debug("application activated", .{});
}
};
pub const Window = struct {

View File

@ -101,9 +101,8 @@ pub fn main() !void {
// Run our app with a single initial window to start.
var app = try App.create(alloc, .{}, &config);
defer app.destroy();
try app.run();
if (build_config.app_runtime == .gtk) return;
_ = try app.newWindow(.{});
try app.run();
}
// Required by tracy/tracy.zig to enable/disable tracy support.

View File

@ -420,8 +420,18 @@ pub fn deinitDevMode(self: *const OpenGL) void {
/// Callback called by renderer.Thread when it begins.
pub fn threadEnter(self: *const OpenGL, win: apprt.runtime.Window) !void {
_ = self;
if (apprt.runtime == apprt.gtk) @panic("TODO");
switch (apprt.runtime) {
else => @compileError("unsupported app runtime for OpenGL"),
apprt.gtk => {
// GTK doesn't support threaded OpenGL operations as far as I can
// tell, so we use the renderer thread to setup all the state
// but then do the actual draws and texture syncs and all that
// on the main thread. As such, we don't do anything here.
},
apprt.glfw => {
// We need to make the OpenGL context current. OpenGL requires
// that a single thread own the a single OpenGL context (if any). This
// ensures that the context switches over to our thread. Important:
@ -439,15 +449,24 @@ pub fn threadEnter(self: *const OpenGL, win: apprt.runtime.Window) !void {
gl.glad.versionMajor(@intCast(c_uint, version)),
gl.glad.versionMinor(@intCast(c_uint, version)),
});
},
}
}
/// Callback called by renderer.Thread when it exits.
pub fn threadExit(self: *const OpenGL) void {
_ = self;
if (apprt.runtime == apprt.gtk) @panic("TODO");
switch (apprt.runtime) {
else => @compileError("unsupported app runtime for OpenGL"),
apprt.gtk => {},
apprt.glfw => {
gl.glad.unload();
glfw.makeContextCurrent(null);
},
}
}
/// Callback when the focus changes for the terminal this is rendering.