metal: copy screen data (see opengl commit for why)

This commit is contained in:
Mitchell Hashimoto
2022-11-13 17:29:23 -08:00
parent 3fcdd9a196
commit 019f1e34bb
3 changed files with 113 additions and 39 deletions

View File

@ -325,17 +325,16 @@ pub fn render(
bg: terminal.color.RGB, bg: terminal.color.RGB,
screen_size: ?renderer.ScreenSize, screen_size: ?renderer.ScreenSize,
devmode: bool, devmode: bool,
selection: ?terminal.Selection,
screen: terminal.Screen,
draw_cursor: bool,
}; };
// Update all our data as tightly as possible within the mutex. // Update all our data as tightly as possible within the mutex.
const critical: Critical = critical: { var critical: Critical = critical: {
state.mutex.lock(); state.mutex.lock();
defer state.mutex.unlock(); defer state.mutex.unlock();
// If we're resizing, then handle that now.
if (state.resize_screen) |size| try self.setScreenSize(size);
defer state.resize_screen = null;
// Setup our cursor state // Setup our cursor state
if (self.focused) { if (self.focused) {
self.cursor_visible = self.cursor_visible and state.cursor.visible; self.cursor_visible = self.cursor_visible and state.cursor.visible;
@ -357,15 +356,34 @@ pub fn render(
self.foreground = bg; self.foreground = bg;
} }
// Build our GPU cells // We used to share terminal state, but we've since learned through
try self.rebuildCells(state.terminal); // analysis that it is faster to copy the terminal state than to
// hold the lock wile rebuilding GPU cells.
const viewport_bottom = state.terminal.screen.viewportIsBottom();
var screen_copy = if (viewport_bottom) try state.terminal.screen.clone(
self.alloc,
.{ .active = 0 },
.{ .active = state.terminal.rows - 1 },
) else try state.terminal.screen.clone(
self.alloc,
.{ .viewport = 0 },
.{ .viewport = state.terminal.rows - 1 },
);
errdefer screen_copy.deinit();
// We set this in the state below
defer state.resize_screen = null;
break :critical .{ break :critical .{
.bg = self.background, .bg = self.background,
.screen_size = state.resize_screen, .screen_size = state.resize_screen,
.devmode = if (state.devmode) |dm| dm.visible else false, .devmode = if (state.devmode) |dm| dm.visible else false,
.selection = state.terminal.selection,
.screen = screen_copy,
.draw_cursor = self.cursor_visible and state.terminal.screen.viewportIsBottom(),
}; };
}; };
defer critical.screen.deinit();
// @autoreleasepool {} // @autoreleasepool {}
const pool = objc_autoreleasePoolPush(); const pool = objc_autoreleasePoolPush();
@ -373,6 +391,9 @@ pub fn render(
// If we're resizing, then we have to update a bunch of things... // If we're resizing, then we have to update a bunch of things...
if (critical.screen_size) |screen_size| { if (critical.screen_size) |screen_size| {
// Update our grid size
try self.setScreenSize(screen_size);
const bounds = self.swapchain.getProperty(macos.graphics.Rect, "bounds"); const bounds = self.swapchain.getProperty(macos.graphics.Rect, "bounds");
// Scale the bounds based on the layer content scale so that we // Scale the bounds based on the layer content scale so that we
@ -387,7 +408,6 @@ pub fn render(
// Set the size of the drawable surface to the scaled bounds // Set the size of the drawable surface to the scaled bounds
self.swapchain.setProperty("drawableSize", scaled); self.swapchain.setProperty("drawableSize", scaled);
_ = screen_size;
//log.warn("bounds={} screen={} scaled={}", .{ bounds, screen_size, scaled }); //log.warn("bounds={} screen={} scaled={}", .{ bounds, screen_size, scaled });
// Setup our uniforms // Setup our uniforms
@ -407,6 +427,13 @@ pub fn render(
}; };
} }
// Build our GPU cells
try self.rebuildCells(
critical.selection,
&critical.screen,
critical.draw_cursor,
);
// Get our surface (CAMetalDrawable) // Get our surface (CAMetalDrawable)
const surface = self.swapchain.msgSend(objc.Object, objc.sel("nextDrawable"), .{}); const surface = self.swapchain.msgSend(objc.Object, objc.sel("nextDrawable"), .{});
@ -562,7 +589,12 @@ fn setScreenSize(self: *Metal, dim: renderer.ScreenSize) !void {
/// Sync all the CPU cells with the GPU state (but still on the CPU here). /// Sync all the CPU cells with the GPU state (but still on the CPU here).
/// This builds all our "GPUCells" on this struct, but doesn't send them /// This builds all our "GPUCells" on this struct, but doesn't send them
/// down to the GPU yet. /// down to the GPU yet.
fn rebuildCells(self: *Metal, term: *Terminal) !void { fn rebuildCells(
self: *Metal,
term_selection: ?terminal.Selection,
screen: *terminal.Screen,
draw_cursor: bool,
) !void {
// Over-allocate just to ensure we don't allocate again during loops. // Over-allocate just to ensure we don't allocate again during loops.
self.cells.clearRetainingCapacity(); self.cells.clearRetainingCapacity();
try self.cells.ensureTotalCapacity( try self.cells.ensureTotalCapacity(
@ -570,7 +602,7 @@ fn rebuildCells(self: *Metal, term: *Terminal) !void {
// * 3 for background modes and cursor and underlines // * 3 for background modes and cursor and underlines
// + 1 for cursor // + 1 for cursor
(term.screen.rows * term.screen.cols * 3) + 1, (screen.rows * screen.cols * 3) + 1,
); );
// This is the cell that has [mode == .fg] and is underneath our cursor. // This is the cell that has [mode == .fg] and is underneath our cursor.
@ -579,7 +611,7 @@ fn rebuildCells(self: *Metal, term: *Terminal) !void {
var cursor_cell: ?GPUCell = null; var cursor_cell: ?GPUCell = null;
// Build each cell // Build each cell
var rowIter = term.screen.rowIterator(.viewport); var rowIter = screen.rowIterator(.viewport);
var y: usize = 0; var y: usize = 0;
while (rowIter.next()) |row| { while (rowIter.next()) |row| {
defer y += 1; defer y += 1;
@ -589,11 +621,11 @@ fn rebuildCells(self: *Metal, term: *Terminal) !void {
const start_i: usize = self.cells.items.len; const start_i: usize = self.cells.items.len;
defer if (self.cursor_visible and defer if (self.cursor_visible and
self.cursor_style == .box and self.cursor_style == .box and
term.screen.viewportIsBottom() and screen.viewportIsBottom() and
y == term.screen.cursor.y) y == screen.cursor.y)
{ {
for (self.cells.items[start_i..]) |cell| { for (self.cells.items[start_i..]) |cell| {
if (cell.grid_pos[0] == @intToFloat(f32, term.screen.cursor.x) and if (cell.grid_pos[0] == @intToFloat(f32, screen.cursor.x) and
cell.mode == .fg) cell.mode == .fg)
{ {
cursor_cell = cell; cursor_cell = cell;
@ -607,7 +639,8 @@ fn rebuildCells(self: *Metal, term: *Terminal) !void {
while (try iter.next(self.alloc)) |run| { while (try iter.next(self.alloc)) |run| {
for (try self.font_shaper.shape(run)) |shaper_cell| { for (try self.font_shaper.shape(run)) |shaper_cell| {
assert(try self.updateCell( assert(try self.updateCell(
term, term_selection,
screen,
row.getCell(shaper_cell.x), row.getCell(shaper_cell.x),
shaper_cell, shaper_cell,
run, run,
@ -624,7 +657,7 @@ fn rebuildCells(self: *Metal, term: *Terminal) !void {
// Add the cursor at the end so that it overlays everything. If we have // Add the cursor at the end so that it overlays everything. If we have
// a cursor cell then we invert the colors on that and add it in so // a cursor cell then we invert the colors on that and add it in so
// that we can always see it. // that we can always see it.
self.addCursor(term); if (draw_cursor) self.addCursor(screen);
if (cursor_cell) |*cell| { if (cursor_cell) |*cell| {
cell.color = .{ 0, 0, 0, 255 }; cell.color = .{ 0, 0, 0, 255 };
self.cells.appendAssumeCapacity(cell.*); self.cells.appendAssumeCapacity(cell.*);
@ -633,7 +666,8 @@ fn rebuildCells(self: *Metal, term: *Terminal) !void {
pub fn updateCell( pub fn updateCell(
self: *Metal, self: *Metal,
term: *Terminal, selection: ?terminal.Selection,
screen: *terminal.Screen,
cell: terminal.Screen.Cell, cell: terminal.Screen.Cell,
shaper_cell: font.Shaper.Cell, shaper_cell: font.Shaper.Cell,
shaper_run: font.Shaper.TextRun, shaper_run: font.Shaper.TextRun,
@ -658,11 +692,11 @@ pub fn updateCell(
// cell is selected. // cell is selected.
// TODO(perf): we can check in advance if selection is in // TODO(perf): we can check in advance if selection is in
// our viewport at all and not run this on every point. // our viewport at all and not run this on every point.
if (term.selection) |sel| { if (selection) |sel| {
const screen_point = (terminal.point.Viewport{ const screen_point = (terminal.point.Viewport{
.x = x, .x = x,
.y = y, .y = y,
}).toScreen(&term.screen); }).toScreen(screen);
// If we are selected, we our colors are just inverted fg/bg // If we are selected, we our colors are just inverted fg/bg
if (sel.contains(screen_point)) { if (sel.contains(screen_point)) {
@ -747,26 +781,24 @@ pub fn updateCell(
return true; return true;
} }
fn addCursor(self: *Metal, term: *Terminal) void { fn addCursor(self: *Metal, screen: *terminal.Screen) void {
// Add the cursor // Add the cursor
if (self.cursor_visible and term.screen.viewportIsBottom()) { const cell = screen.getCell(
const cell = term.screen.getCell(
.active, .active,
term.screen.cursor.y, screen.cursor.y,
term.screen.cursor.x, screen.cursor.x,
); );
self.cells.appendAssumeCapacity(.{ self.cells.appendAssumeCapacity(.{
.mode = GPUCellMode.fromCursor(self.cursor_style), .mode = GPUCellMode.fromCursor(self.cursor_style),
.grid_pos = .{ .grid_pos = .{
@intToFloat(f32, term.screen.cursor.x), @intToFloat(f32, screen.cursor.x),
@intToFloat(f32, term.screen.cursor.y), @intToFloat(f32, screen.cursor.y),
}, },
.cell_width = if (cell.attrs.wide) 2 else 1, .cell_width = if (cell.attrs.wide) 2 else 1,
.color = .{ 0xFF, 0xFF, 0xFF, 0xFF }, .color = .{ 0xFF, 0xFF, 0xFF, 0xFF },
}); });
} }
}
/// Sync the vertex buffer inputs to the GPU. This will attempt to reuse /// Sync the vertex buffer inputs to the GPU. This will attempt to reuse
/// the existing buffer (of course!) but will allocate a new buffer if /// the existing buffer (of course!) but will allocate a new buffer if

View File

@ -516,10 +516,15 @@ pub fn render(
// We used to share terminal state, but we've since learned through // We used to share terminal state, but we've since learned through
// analysis that it is faster to copy the terminal state than to // analysis that it is faster to copy the terminal state than to
// hold the lock wile rebuilding GPU cells. // hold the lock wile rebuilding GPU cells.
var screen_copy = try state.terminal.screen.clone( const viewport_bottom = state.terminal.screen.viewportIsBottom();
var screen_copy = if (viewport_bottom) try state.terminal.screen.clone(
self.alloc, self.alloc,
.{ .active = 0 }, .{ .active = 0 },
.{ .active = state.terminal.screen.rows - 1 }, .{ .active = state.terminal.rows - 1 },
) else try state.terminal.screen.clone(
self.alloc,
.{ .viewport = 0 },
.{ .viewport = state.terminal.rows - 1 },
); );
errdefer screen_copy.deinit(); errdefer screen_copy.deinit();

View File

@ -588,7 +588,7 @@ pub const RowIndexTag = enum {
// Viewport can be any of the written rows or the max size // Viewport can be any of the written rows or the max size
// of a viewport. // of a viewport.
.viewport => @min(screen.rows, screen.rowsWritten()), .viewport => @max(1, @min(screen.rows, screen.rowsWritten())),
// History is all the way up to the top of our active area. If // History is all the way up to the top of our active area. If
// we haven't filled our active area, there is no history. // we haven't filled our active area, there is no history.
@ -2323,6 +2323,43 @@ test "Screen: clone" {
} }
} }
test "Screen: clone empty viewport" {
const testing = std.testing;
const alloc = testing.allocator;
var s = try init(alloc, 3, 5, 0);
defer s.deinit();
{
var s2 = try s.clone(alloc, .{ .viewport = 0 }, .{ .viewport = 0 });
defer s2.deinit();
// Test our contents rotated
var contents = try s2.testString(alloc, .viewport);
defer alloc.free(contents);
try testing.expectEqualStrings("", contents);
}
}
test "Screen: clone one line viewport" {
const testing = std.testing;
const alloc = testing.allocator;
var s = try init(alloc, 3, 5, 0);
defer s.deinit();
try s.testWriteString("1ABC");
{
var s2 = try s.clone(alloc, .{ .viewport = 0 }, .{ .viewport = 0 });
defer s2.deinit();
// Test our contents rotated
var contents = try s2.testString(alloc, .viewport);
defer alloc.free(contents);
try testing.expectEqualStrings("1ABC", contents);
}
}
test "Screen: scrollRegionUp single" { test "Screen: scrollRegionUp single" {
const testing = std.testing; const testing = std.testing;
const alloc = testing.allocator; const alloc = testing.allocator;