From 84648518dc901afb3bbebb193ff933d45660e658 Mon Sep 17 00:00:00 2001 From: keith Date: Wed, 18 Dec 2019 11:55:02 -0800 Subject: [PATCH] progress toward progressive rendering --- src/editor/mod.rs | 24 ++++++++++++++++++------ src/renderer/mod.rs | 6 ++---- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/src/editor/mod.rs b/src/editor/mod.rs index 46768d9..7c84af3 100644 --- a/src/editor/mod.rs +++ b/src/editor/mod.rs @@ -19,6 +19,7 @@ pub struct DrawCommand { pub struct Editor { pub grid: Vec>, + pub dirty: Vec>, pub title: String, pub size: (u64, u64), pub cursor: Cursor, @@ -31,6 +32,7 @@ impl Editor { pub fn new(width: u64, height: u64) -> Editor { let mut editor = Editor { grid: Vec::new(), + dirty: Vec::new(), title: "Neovide".to_string(), cursor: Cursor::new(), size: (width, height), @@ -60,8 +62,8 @@ impl Editor { } } - pub fn build_draw_commands(&self) -> Vec { - self.grid.iter().enumerate().map(|(row_index, row)| { + pub fn build_draw_commands(&mut self) -> Vec { + let commands = self.grid.iter().enumerate().map(|(row_index, row)| { let mut draw_commands = Vec::new(); let mut command = None; @@ -78,11 +80,13 @@ impl Editor { } } - fn add_character(command: &mut Option, character: &char, row_index: u64, col_index: u64, style: Style) { + fn add_character(command: &mut Option, character: &char, dirty: bool, row_index: u64, col_index: u64, style: Style) { match command { Some(command) => command.text.push(character.clone()), None => { - command.replace(DrawCommand::new(character.to_string(), (col_index, row_index), style)); + if dirty { + command.replace(DrawCommand::new(character.to_string(), (col_index, row_index), style)); + } } } } @@ -93,7 +97,7 @@ impl Editor { add_command(&mut draw_commands, command); command = None; } - add_character(&mut command, &character, row_index as u64, col_index as u64, new_style.clone()); + add_character(&mut command, &character, self.dirty[row_index][col_index], row_index as u64, col_index as u64, new_style.clone()); } else { add_command(&mut draw_commands, command); command = None; @@ -102,7 +106,10 @@ impl Editor { add_command(&mut draw_commands, command); draw_commands - }).flatten().collect() + }).flatten().collect(); + let (width, height) = self.size; + self.dirty = vec![vec![false; width as usize]; height as usize]; + commands } fn draw_grid_line_cell(&mut self, row_index: u64, column_pos: &mut u64, cell: GridLineCell) { @@ -119,10 +126,12 @@ impl Editor { } let row = self.grid.get_mut(row_index as usize).expect("Grid must have size greater than row_index"); + let dirty_row = &mut self.dirty[row_index as usize]; for (i, character) in text.chars().enumerate() { let pointer_index = i + *column_pos as usize; if pointer_index < row.len() { row[pointer_index] = Some((character, style.clone())); + dirty_row[pointer_index] = true; } } @@ -149,6 +158,7 @@ impl Editor { fn clear(&mut self) { let (width, height) = self.size; self.grid = vec![vec![None; width as usize]; height as usize]; + self.dirty = vec![vec![true; width as usize]; height as usize]; } fn scroll_region(&mut self, top: u64, bot: u64, left: u64, right: u64, rows: i64, cols: i64) { @@ -186,9 +196,11 @@ impl Editor { let y = new_top + y as i64; if y >= 0 && y < self.grid.len() as i64 { let row = &mut self.grid[y as usize]; + let dirty_row = &mut self.dirty[y as usize]; let x = new_left + x as i64; if x >= 0 && x < row.len() as i64 { row[x as usize] = cell; + dirty_row[x as usize] = true; } } } diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 9e427ea..4b4fc39 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -94,9 +94,9 @@ impl Renderer { pub fn draw(&mut self, gpu_canvas: &mut Canvas, coordinate_system_helper: &CoordinateSystemHelper) { let (draw_commands, default_colors, (width, height), cursor) = { - let editor = self.editor.lock().unwrap(); + let mut editor = self.editor.lock().unwrap(); ( - editor.build_draw_commands().clone(), + editor.build_draw_commands(), editor.default_colors.clone(), editor.size.clone(), editor.cursor.clone() @@ -132,11 +132,9 @@ impl Renderer { let target_cursor_y = cursor_grid_y as f32 * self.font_height; let (previous_cursor_x, previous_cursor_y) = self.cursor_pos; - self.image = Some(surface.image_snapshot()); coordinate_system_helper.use_physical_coordinates(gpu_canvas); gpu_canvas.draw_image(self.image.as_ref().unwrap(), (0, 0), Some(&self.paint)); - coordinate_system_helper.use_logical_coordinates(gpu_canvas); let cursor_x = (target_cursor_x - previous_cursor_x) * 0.5 + previous_cursor_x; let cursor_y = (target_cursor_y - previous_cursor_y) * 0.5 + previous_cursor_y;