mirror of
https://github.com/ghostty-org/ghostty.git
synced 2026-09-20 12:38:08 +00:00
GtkGLArea had numerous downsides that forced us to invent unsightly hacks in our renderer to work around them, most chiefly the fact that it holds its own GdkGLContext on the main thread (GL contexts are not at all thread-safe), forcing us to keep our GL calls on the main thread. It also does not interact well with triple-buffering and initialization is forced to be this sort of deferred song-and-dance since we need to wait for the GLArea to initialize its GL context before we can initialize the renderer, the core surface, and then most things in the GTK surface. We instead invent our own custom widget named RenderSurface that takes simple DMABUFs and displays them. The task of obtaining a GL context falls to manual EGL bindings, since we also need EGL to export OpenGL textures into DMABUFs. We keep the EGL context solely on the render thread meaning that the main thread never concerns itself with rendering except when being notified that the renderer has pushed a new frame. What makes this extra significant is that now the entire GTK apprt no longer depends on OpenGL in any way, shape or form. As long as it is being fed DMABUFs, it can render from whichever graphics API you want. This means we can add more backends based on OpenGL ES or more likely Vulkan rather painlessly in the future. **AI disclosure**: I came up with the idea and let Pi implement most of the nitty-gritty details around EGL, as well as replumbing the renderer and cleaning up all the GTK-specific workarounds there. I then carefully vetted every line of code and spent roughly as much time reviewing as coding. Most of the documentation and all commit messages are in my own words.
76 lines
2.3 KiB
Zig
76 lines
2.3 KiB
Zig
//! A DMABUF frame produced by exporting a GPU texture.
|
|
//! This may be used on apprts like GTK that require us to manually
|
|
//! export each frame and import them as textures in their UI scene graphs.
|
|
//!
|
|
//! Note that DMABUFs are independent of the graphics API used:
|
|
//! the OpenGL renderer allocates them with GBM, and Vulkan can
|
|
//! export them with a KHR external memory implementation. Therefore
|
|
//! this struct has to be placed parallel to the renderer
|
|
//! implementations.
|
|
pub const Dmabuf = @This();
|
|
pub const std = @import("std");
|
|
|
|
/// The maximum number of planes in a DMABUF that we support.
|
|
/// This matches the maximum found in apprts such as GTK.
|
|
pub const max_planes = 4;
|
|
|
|
/// Width of the texture in device pixels.
|
|
width: u32,
|
|
|
|
/// Height of the texture in device pixels.
|
|
height: u32,
|
|
|
|
/// DRM fourcc of the pixel format.
|
|
/// Ghostty renders RGBA/BGRA8 premultiplied.
|
|
fourcc: u32,
|
|
|
|
/// DRM modifier of the format.
|
|
modifier: u64,
|
|
|
|
/// Whether the data is premultiplied.
|
|
/// Ghostty's GL renderers output premultiplied alpha.
|
|
premultiplied: bool,
|
|
|
|
/// The DMABUF planes for a presented frame. The DMABUF owns the fds
|
|
/// and must either call `deinit` manually, or pass them to an apprt
|
|
/// that consumes them.
|
|
planes: Planes,
|
|
|
|
pub const Planes = struct {
|
|
/// Number of planes. Valid planes are `planes[0..count]`.
|
|
count: u8,
|
|
|
|
/// File descriptor for each plane.
|
|
fds: [max_planes]std.posix.fd_t = @splat(-1),
|
|
|
|
/// Offset into the DMABUF where each plane starts, in bytes.
|
|
offsets: [max_planes]c_int = @splat(0),
|
|
|
|
/// Strides of each plane, in bytes.
|
|
strides: [max_planes]c_int = @splat(0),
|
|
|
|
/// Close all valid fds.
|
|
pub fn deinit(self: Planes) void {
|
|
for (self.fds[0..self.count]) |fd| {
|
|
if (fd >= 0) _ = std.posix.system.close(fd);
|
|
}
|
|
}
|
|
|
|
/// Validate the current planes. If any plane failed to export
|
|
/// and has an invalid FD, we close all the known valid FDs
|
|
/// and bail.
|
|
pub fn validate(self: Planes) error{BadDmabuf}!void {
|
|
var n_valid: usize = 0;
|
|
while (n_valid < self.count) : (n_valid += 1) {
|
|
if (self.fds[n_valid] < 0) {
|
|
for (self.fds[0..n_valid]) |bad| _ = std.posix.system.close(bad);
|
|
return error.BadDmabuf;
|
|
}
|
|
}
|
|
}
|
|
};
|
|
|
|
pub fn deinit(self: Dmabuf) void {
|
|
self.planes.deinit();
|
|
}
|