From 04e0cd29e59ac9b99e0a7f98df398c6c082ba694 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Fri, 5 Apr 2024 15:24:24 -0700 Subject: [PATCH] core: begin converting to SharedGridSet, renderers still broken --- src/App.zig | 10 +++++----- src/Surface.zig | 14 +++++++------- src/font/SharedGrid.zig | 11 +++++++++++ src/renderer/Options.zig | 2 +- src/renderer/size.zig | 20 -------------------- 5 files changed, 24 insertions(+), 33 deletions(-) diff --git a/src/App.zig b/src/App.zig index 53ca77c6d..d9b5e67f2 100644 --- a/src/App.zig +++ b/src/App.zig @@ -43,7 +43,7 @@ quit: bool, /// The set of font GroupCache instances shared by surfaces with the /// same font configuration. -font_group_set: font.GroupCacheSet, +font_grid_set: font.SharedGridSet, /// Initialize the main app instance. This creates the main window, sets /// up the renderer state, compiles the shaders, etc. This is the primary @@ -54,15 +54,15 @@ pub fn create( var app = try alloc.create(App); errdefer alloc.destroy(app); - var font_group_set = try font.GroupCacheSet.init(alloc); - errdefer font_group_set.deinit(); + var font_grid_set = try font.SharedGridSet.init(alloc); + errdefer font_grid_set.deinit(); app.* = .{ .alloc = alloc, .surfaces = .{}, .mailbox = .{}, .quit = false, - .font_group_set = font_group_set, + .font_grid_set = font_grid_set, }; errdefer app.surfaces.deinit(alloc); @@ -76,7 +76,7 @@ pub fn destroy(self: *App) void { // Clean up our font group cache // TODO(fontmem): assert all ref counts are zero - self.font_group_set.deinit(); + self.font_grid_set.deinit(); self.alloc.destroy(self); } diff --git a/src/Surface.zig b/src/Surface.zig index 00258e112..25a8b800d 100644 --- a/src/Surface.zig +++ b/src/Surface.zig @@ -54,7 +54,7 @@ rt_app: *apprt.runtime.App, rt_surface: *apprt.runtime.Surface, /// The font structures -font_group_key: font.GroupCacheSet.Key, +font_grid_key: font.SharedGridSet.Key, font_size: font.face.DesiredSize, /// The renderer for this surface. @@ -321,13 +321,13 @@ pub fn init( // Setup our font group. This will reuse an existing font group if // it was already loaded. - const font_group_key, const font_group = try app.font_group_set.groupRef( + const font_grid_key, const font_grid = try app.font_grid_set.ref( config, font_size, ); // Pre-calculate our initial cell size ourselves. - const cell_size = try renderer.CellSize.init(alloc, font_group); + const cell_size = font_grid.cellSize(); // Convert our padding from points to pixels const padding_x: u32 = padding_x: { @@ -349,7 +349,7 @@ pub fn init( const app_mailbox: App.Mailbox = .{ .rt_app = rt_app, .mailbox = &app.mailbox }; var renderer_impl = try Renderer.init(alloc, .{ .config = try Renderer.DerivedConfig.init(alloc, config), - .font_group = font_group, + .font_grid = font_grid, .padding = .{ .explicit = padding, .balance = config.@"window-padding-balance", @@ -410,7 +410,7 @@ pub fn init( .app = app, .rt_app = rt_app, .rt_surface = rt_surface, - .font_group_key = font_group_key, + .font_grid_key = font_grid_key, .font_size = font_size, .renderer = renderer_impl, .renderer_thread = render_thread, @@ -530,8 +530,8 @@ pub fn deinit(self: *Surface) void { self.alloc.destroy(v); } - // Clean up our font group - self.app.font_group_set.groupDeref(self.font_group_key); + // Clean up our font grid + self.app.font_grid_set.deref(self.font_grid_key); // Clean up our render state if (self.renderer_state.preedit) |p| self.alloc.free(p.codepoints); diff --git a/src/font/SharedGrid.zig b/src/font/SharedGrid.zig index 994d84936..1333f2849 100644 --- a/src/font/SharedGrid.zig +++ b/src/font/SharedGrid.zig @@ -24,6 +24,7 @@ const SharedGrid = @This(); const std = @import("std"); const assert = std.debug.assert; const Allocator = std.mem.Allocator; +const renderer = @import("../renderer.zig"); const font = @import("main.zig"); const Atlas = font.Atlas; const CodepointResolver = font.CodepointResolver; @@ -132,6 +133,16 @@ fn reloadMetrics(self: *SharedGrid, thicken: bool) !void { }; } +/// Returns the grid cell size. +/// +/// This is not thread safe. +pub fn cellSize(self: *SharedGrid) renderer.CellSize { + return .{ + .width = self.metrics.cell_width, + .height = self.metrics.cell_height, + }; +} + const CodepointKey = struct { style: Style, codepoint: u32, diff --git a/src/renderer/Options.zig b/src/renderer/Options.zig index c951eacd1..18e98c73c 100644 --- a/src/renderer/Options.zig +++ b/src/renderer/Options.zig @@ -9,7 +9,7 @@ const Config = @import("../config.zig").Config; config: renderer.Renderer.DerivedConfig, /// The font group that should be used. -font_group: *font.GroupCache, +font_grid: *font.SharedGrid, /// Padding options for the viewport. padding: Padding, diff --git a/src/renderer/size.zig b/src/renderer/size.zig index 4f6b5fc5b..7b458b57e 100644 --- a/src/renderer/size.zig +++ b/src/renderer/size.zig @@ -17,26 +17,6 @@ const log = std.log.scoped(.renderer_size); pub const CellSize = struct { width: u32, height: u32, - - /// Initialize the cell size information from a font group. This ensures - /// that all renderers use the same cell sizing information for the same - /// fonts. - pub fn init(alloc: Allocator, group: *font.GroupCache) !CellSize { - // Get our cell metrics based on a regular font ascii 'M'. Why 'M'? - // Doesn't matter, any normal ASCII will do we're just trying to make - // sure we use the regular font. - const metrics = metrics: { - const index = (try group.indexForCodepoint(alloc, 'M', .regular, .text)).?; - const face = try group.group.faceFromIndex(index); - break :metrics face.metrics; - }; - log.debug("cell dimensions={}", .{metrics}); - - return CellSize{ - .width = metrics.cell_width, - .height = metrics.cell_height, - }; - } }; /// The dimensions of the screen that the grid is rendered to. This is the