renderer/metal: free resources when occluded

This commit is contained in:
Mitchell Hashimoto
2024-02-13 20:07:41 -08:00
parent bef83446d1
commit b5d543705d
2 changed files with 21 additions and 0 deletions

View File

@ -509,6 +509,19 @@ pub fn setFocus(self: *Metal, focus: bool) !void {
self.focused = focus;
}
/// Callback when the view visibility changes, i.e. if a window is on
/// another workspace, behind a window, etc.
pub fn setVisible(self: *Metal, visible: bool) !void {
//if (true) return;
if (!visible) {
if (self.visible_resources) |*v| {
log.debug("view occluded, deallocating GPU resources", .{});
v.deinit(self.alloc);
self.visible_resources = null;
}
}
}
/// Set the new font size.
///
/// Must be called on the render thread.
@ -712,6 +725,7 @@ pub fn drawFrame(self: *Metal, surface: *apprt.Surface) !void {
// Get our cached resources. If we don't have them, then we need to
// create them. If we fail to create them, mark the renderer as unhealthy.
const resources: *VisibleResources = if (self.visible_resources) |*v| v else resources: {
log.debug("view is visible, allocating GPU resources", .{});
const resources = VisibleResources.init(self) catch |err| {
self.setHealth(.unhealthy);
return err;
@ -2226,6 +2240,10 @@ const VisibleResources = struct {
const texture_color = try initAtlasTexture(m.device, &m.font_group.atlas_color);
errdefer deinitMTLResource(texture_color);
// Mark our atlas as modified so the textures are synced
m.font_group.atlas_greyscale.modified = true;
m.font_group.atlas_color.modified = true;
return .{
.shaders = shaders,
.buf_cells = buf_cells,

View File

@ -250,6 +250,9 @@ fn drainMailbox(self: *Thread) !void {
// Set our visible state
self.flags.visible = v;
// Set it on the renderer
try self.renderer.setVisible(v);
// If we became visible then we immediately trigger a draw.
// We don't need to update frame data because that should
// still be happening.