core: begin converting to SharedGridSet, renderers still broken

This commit is contained in:
Mitchell Hashimoto
2024-04-05 15:24:24 -07:00
parent 4a29da3525
commit 04e0cd29e5
5 changed files with 24 additions and 33 deletions

View File

@ -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);
}

View File

@ -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);

View File

@ -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,

View File

@ -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,

View File

@ -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