Skip to content

Commit d3de344

Browse files
authored
gtk: add config option to disable GTK OpenGL debug logging (#4662)
The only change from default should be that when running a ReleaseFast binary you won't get OpenGL debugging.
2 parents 8e52c6d + 96e427c commit d3de344

File tree

2 files changed

+116
-36
lines changed

2 files changed

+116
-36
lines changed

src/apprt/gtk/App.zig

Lines changed: 98 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -104,42 +104,6 @@ pub fn init(core_app: *CoreApp, opts: Options) !App {
104104
c.gtk_get_micro_version(),
105105
});
106106

107-
// Disabling Vulkan can improve startup times by hundreds of
108-
// milliseconds on some systems. We don't use Vulkan so we can just
109-
// disable it.
110-
if (version.runtimeAtLeast(4, 16, 0)) {
111-
// From gtk 4.16, GDK_DEBUG is split into GDK_DEBUG and GDK_DISABLE.
112-
// For the remainder of "why" see the 4.14 comment below.
113-
_ = internal_os.setenv("GDK_DISABLE", "gles-api,vulkan");
114-
_ = internal_os.setenv("GDK_DEBUG", "opengl,gl-no-fractional");
115-
} else if (version.runtimeAtLeast(4, 14, 0)) {
116-
// We need to export GDK_DEBUG to run on Wayland after GTK 4.14.
117-
// Older versions of GTK do not support these values so it is safe
118-
// to always set this. Forwards versions are uncertain so we'll have to
119-
// reassess...
120-
//
121-
// Upstream issue: https://gitlab.gnome.org/GNOME/gtk/-/issues/6589
122-
//
123-
// Specific details about values:
124-
// - "opengl" - output OpenGL debug information
125-
// - "gl-disable-gles" - disable GLES, Ghostty can't use GLES
126-
// - "vulkan-disable" - disable Vulkan, Ghostty can't use Vulkan
127-
// and initializing a Vulkan context was causing a longer delay
128-
// on some systems.
129-
_ = internal_os.setenv("GDK_DEBUG", "opengl,gl-disable-gles,vulkan-disable,gl-no-fractional");
130-
} else {
131-
// Versions prior to 4.14 are a bit of an unknown for Ghostty. It
132-
// is an environment that isn't tested well and we don't have a
133-
// good understanding of what we may need to do.
134-
_ = internal_os.setenv("GDK_DEBUG", "vulkan-disable");
135-
}
136-
137-
if (version.runtimeAtLeast(4, 14, 0)) {
138-
// We need to export GSK_RENDERER to opengl because GTK uses ngl by
139-
// default after 4.14
140-
_ = internal_os.setenv("GSK_RENDERER", "opengl");
141-
}
142-
143107
// Load our configuration
144108
var config = try Config.load(core_app.alloc);
145109
errdefer config.deinit();
@@ -161,6 +125,104 @@ pub fn init(core_app: *CoreApp, opts: Options) !App {
161125
}
162126
}
163127

128+
var gdk_debug: struct {
129+
/// output OpenGL debug information
130+
opengl: bool = false,
131+
/// disable GLES, Ghostty can't use GLES
132+
@"gl-disable-gles": bool = false,
133+
@"gl-no-fractional": bool = false,
134+
/// Disabling Vulkan can improve startup times by hundreds of
135+
/// milliseconds on some systems. We don't use Vulkan so we can just
136+
/// disable it.
137+
@"vulkan-disable": bool = false,
138+
} = .{
139+
.opengl = config.@"gtk-opengl-debug",
140+
};
141+
142+
var gdk_disable: struct {
143+
@"gles-api": bool = false,
144+
/// Disabling Vulkan can improve startup times by hundreds of
145+
/// milliseconds on some systems. We don't use Vulkan so we can just
146+
/// disable it.
147+
vulkan: bool = false,
148+
} = .{};
149+
150+
environment: {
151+
if (version.runtimeAtLeast(4, 16, 0)) {
152+
// From gtk 4.16, GDK_DEBUG is split into GDK_DEBUG and GDK_DISABLE.
153+
// For the remainder of "why" see the 4.14 comment below.
154+
gdk_disable.@"gles-api" = true;
155+
gdk_disable.vulkan = true;
156+
gdk_debug.@"gl-no-fractional" = true;
157+
break :environment;
158+
}
159+
if (version.runtimeAtLeast(4, 14, 0)) {
160+
// We need to export GDK_DEBUG to run on Wayland after GTK 4.14.
161+
// Older versions of GTK do not support these values so it is safe
162+
// to always set this. Forwards versions are uncertain so we'll have
163+
// to reassess...
164+
//
165+
// Upstream issue: https://gitlab.gnome.org/GNOME/gtk/-/issues/6589
166+
gdk_debug.@"gl-disable-gles" = true;
167+
gdk_debug.@"gl-no-fractional" = true;
168+
gdk_debug.@"vulkan-disable" = true;
169+
break :environment;
170+
}
171+
// Versions prior to 4.14 are a bit of an unknown for Ghostty. It
172+
// is an environment that isn't tested well and we don't have a
173+
// good understanding of what we may need to do.
174+
gdk_debug.@"vulkan-disable" = true;
175+
}
176+
177+
{
178+
var buf: [128]u8 = undefined;
179+
var fmt = std.io.fixedBufferStream(&buf);
180+
const writer = fmt.writer();
181+
var first: bool = true;
182+
inline for (@typeInfo(@TypeOf(gdk_debug)).Struct.fields) |field| {
183+
if (@field(gdk_debug, field.name)) {
184+
if (!first) try writer.writeAll(",");
185+
try writer.writeAll(field.name);
186+
first = false;
187+
}
188+
}
189+
try writer.writeByte(0);
190+
const value = fmt.getWritten();
191+
log.warn("setting GDK_DEBUG={s}", .{value[0 .. value.len - 1]});
192+
_ = internal_os.setenv("GDK_DEBUG", value[0 .. value.len - 1 :0]);
193+
}
194+
195+
{
196+
var buf: [128]u8 = undefined;
197+
var fmt = std.io.fixedBufferStream(&buf);
198+
const writer = fmt.writer();
199+
var first: bool = true;
200+
inline for (@typeInfo(@TypeOf(gdk_disable)).Struct.fields) |field| {
201+
if (@field(gdk_disable, field.name)) {
202+
if (!first) try writer.writeAll(",");
203+
try writer.writeAll(field.name);
204+
first = false;
205+
}
206+
}
207+
try writer.writeByte(0);
208+
const value = fmt.getWritten();
209+
log.warn("setting GDK_DISABLE={s}", .{value[0 .. value.len - 1]});
210+
_ = internal_os.setenv("GDK_DISABLE", value[0 .. value.len - 1 :0]);
211+
}
212+
213+
if (version.runtimeAtLeast(4, 14, 0)) {
214+
switch (config.@"gtk-gsk-renderer") {
215+
.default => {},
216+
else => |renderer| {
217+
// Force the GSK renderer to a specific value. After GTK 4.14 the
218+
// `ngl` renderer is used by default which causes artifacts when
219+
// used with Ghostty so it should be avoided.
220+
log.warn("setting GSK_RENDERER={s}", .{@tagName(renderer)});
221+
_ = internal_os.setenv("GSK_RENDERER", @tagName(renderer));
222+
},
223+
}
224+
}
225+
164226
c.gtk_init();
165227
const display: *c.GdkDisplay = c.gdk_display_get_default() orelse {
166228
// I'm unsure of any scenario where this happens. Because we don't

src/config/Config.zig

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1975,6 +1975,18 @@ keybind: Keybinds = .{},
19751975
/// must always be able to move themselves into an isolated cgroup.
19761976
@"linux-cgroup-hard-fail": bool = false,
19771977

1978+
/// Enable or disable GTK's OpenGL debugging logs. The default is `true` for
1979+
/// debug builds, `false` for all others.
1980+
@"gtk-opengl-debug": bool = builtin.mode == .Debug,
1981+
1982+
/// After GTK 4.14.0, we need to force the GSK renderer to OpenGL as the default
1983+
/// GSK renderer is broken on some systems. If you would like to override
1984+
/// that bekavior, set `gtk-gsk-renderer=default` and either use your system's
1985+
/// default GSK renderer, or set the GSK_RENDERER environment variable to your
1986+
/// renderer of choice before launching Ghostty. This setting has no effect when
1987+
/// using versions of GTK earlier than 4.14.0.
1988+
@"gtk-gsk-renderer": GtkGskRenderer = .opengl,
1989+
19781990
/// If `true`, the Ghostty GTK application will run in single-instance mode:
19791991
/// each new `ghostty` process launched will result in a new window if there is
19801992
/// already a running process.
@@ -6158,6 +6170,12 @@ pub const WindowPadding = struct {
61586170
}
61596171
};
61606172

6173+
/// See the `gtk-gsk-renderer` config.
6174+
pub const GtkGskRenderer = enum {
6175+
default,
6176+
opengl,
6177+
};
6178+
61616179
test "parse duration" {
61626180
inline for (Duration.units) |unit| {
61636181
var buf: [16]u8 = undefined;

0 commit comments

Comments
 (0)