Эх сурвалжийг харах

Repaint only the damaged cell block of a text window

Any change in a terminal set a single dirty flag and the WM repainted the
whole content rectangle (~0.65 MB at 1280x1024x32), so a keystroke cost
milliseconds on the ION and the blinking cursor repainted a whole window
twice a second.

struct text_screen now tracks the inclusive cell block that changed:
screen_damage_cell() grows it around a cell, screen_damage_all() covers the
grid (scroll/clear), screen_damage_cursor() marks the cell the cursor sits
in, screen_damage_block() reports col0/row0/cols/rows and
screen_clear_damage() drops it after rendering.  Every mutation marks what
it touches -- a printed character the cell written *and* the cursor's new
cell, \n and \b both cursor cells (the cursor is an overlay, so leaving a
cell has to repaint it), screen_set_cursor()/screen_put_at() their cells --
and io.c's cursor show/hide hooks mark the cursor's cell.

wm_redraw_if_needed() repaints just that block through the damage-rect
compositor; a canvas window still repaints its whole content, since a
presented frame has no cell granularity.  wm_blink_cursor() marks only the
cursor's cell.
Johannes Zellner 1 сар өмнө
parent
commit
b591544916
5 өөрчлөгдсөн 118 нэмэгдсэн , 20 устгасан
  1. 8 2
      AGENTS.md
  2. 2 2
      io.c
  3. 74 7
      screen.c
  4. 16 0
      screen.h
  5. 18 9
      wm.c

+ 8 - 2
AGENTS.md

@@ -86,7 +86,9 @@ below the run area), so adding code no longer moves any address.
   clock + rainbow). Click-to-focus, drag-by-title-bar, close (×). The "+ New"
   button and first boot spawn terminals; there is no `term` command.  Content is
   repainted per damage rect (`wm_redraw_rect`: desktop fill + every intersecting
-  window back-to-front, clipped by `wm_draw_window_clip`), not by full redraws.
+  window back-to-front, clipped by `wm_draw_window_clip`), not by full redraws —
+  a text window only over its damaged cell block (`screen_damage_block`), a
+  canvas window over its whole content.
 - **Window graphics service**: `gfx.c/h` (RGB565 window canvases + bpp-aware
   blits), `sys_gfx.c/h` — a ring-3 command gets its window's canvas mapped RW at
   `0x50000000` (`SYS_GFX_BEGIN`; a mapped back buffer) and publishes frames with
@@ -116,7 +118,11 @@ below the run area), so adding code no longer moves any address.
   that has run cannot be restarted — see the header of `nv50.c`.
 - **Text console**: `fbtext.c`, `font_8x8.c/h`, `io.c/h`, `screen.c/h` — 8×8 font,
   framebuffer glyph blitting (`fbtext_draw_cells`) and per-terminal cell buffers
-  (`struct text_screen`).
+  (`struct text_screen`).  A screen tracks the changed cell block
+  (`screen_damage_block()`: `dmg_col0..dmg_col1`, `dmg_row0..dmg_row1`, grown by
+  `screen_damage_cell()`/`screen_damage_cursor()`, reset by
+  `screen_clear_damage()`), so the WM repaints a keystroke's cell instead of the
+  whole window content and the blink only marks the cursor's own cell.
 - **On-screen debug**: `kdbg.c/h` — framebuffer debug overlay (live status rows,
   heartbeat, boot info rows, exception banner) for machines without serial; see
   the kdbg section below.

+ 2 - 2
io.c

@@ -201,14 +201,14 @@ void io_cursor_show_block(void)
 {
 	struct text_screen *s = io_current_screen();
 	s->cursor_visible = 1;
-	s->dirty = 1;
+	screen_damage_cursor(s);
 }
 
 void io_cursor_hide_block(void)
 {
 	struct text_screen *s = io_current_screen();
 	s->cursor_visible = 0;
-	s->dirty = 1;
+	screen_damage_cursor(s);
 }
 
 void io_vga_write(unsigned int pos, unsigned short value)

+ 74 - 7
screen.c

@@ -5,6 +5,69 @@
 
 #define SCREEN_ATTR 0x0A  /* light green on black */
 
+/* ---- Damage tracking ----
+ *
+ * The WM repaints only the cell block that changed, so every mutation of the
+ * cell buffer -- or of the cursor, which is drawn as an overlay -- has to mark
+ * it: screen_damage_cell() grows the block around a cell, screen_damage_all()
+ * covers the whole grid (a scroll or a clear) and screen_damage_cursor() marks
+ * the cell the cursor sits in.
+ */
+static void screen_damage_cell(struct text_screen *s, unsigned int col, unsigned int row)
+{
+	if (!s->dirty) {
+		s->dirty = 1;
+		s->dmg_col0 = s->dmg_col1 = col;
+		s->dmg_row0 = s->dmg_row1 = row;
+		return;
+	}
+	if (col < s->dmg_col0)
+		s->dmg_col0 = col;
+	if (col > s->dmg_col1)
+		s->dmg_col1 = col;
+	if (row < s->dmg_row0)
+		s->dmg_row0 = row;
+	if (row > s->dmg_row1)
+		s->dmg_row1 = row;
+}
+
+static void screen_damage_all(struct text_screen *s)
+{
+	s->dirty = 1;
+	s->dmg_col0 = 0;
+	s->dmg_col1 = s->cols - 1;
+	s->dmg_row0 = 0;
+	s->dmg_row1 = s->rows - 1;
+}
+
+void screen_damage_cursor(struct text_screen *s)
+{
+	unsigned int pos = s->cursor;
+
+	if (pos >= s->cols * s->rows) {
+		screen_damage_all(s);
+		return;
+	}
+	screen_damage_cell(s, pos % s->cols, pos / s->cols);
+}
+
+int screen_damage_block(const struct text_screen *s, unsigned int *col0,
+		unsigned int *row0, unsigned int *cols, unsigned int *rows)
+{
+	if (!s->dirty)
+		return 0;
+	*col0 = s->dmg_col0;
+	*row0 = s->dmg_row0;
+	*cols = s->dmg_col1 - s->dmg_col0 + 1;
+	*rows = s->dmg_row1 - s->dmg_row0 + 1;
+	return 1;
+}
+
+void screen_clear_damage(struct text_screen *s)
+{
+	s->dirty = 0;
+}
+
 void screen_init(struct text_screen *s)
 {
 	s->cols = SCREEN_COLS;
@@ -12,7 +75,6 @@ void screen_init(struct text_screen *s)
 	s->cursor = 0;
 	s->cursor_attr = (SCREEN_ATTR << 8);
 	s->cursor_visible = 0;
-	s->dirty = 1;
 	s->scrolled = 0;
 	screen_clear(s);
 }
@@ -23,7 +85,7 @@ void screen_clear(struct text_screen *s)
 	for (i = 0; i < n; i++)
 		s->cells[i] = (unsigned short)((SCREEN_ATTR << 8) | ' ');
 	s->cursor = 0;
-	s->dirty = 1;
+	screen_damage_all(s);
 	s->scrolled = 0;
 }
 
@@ -36,7 +98,7 @@ static void screen_scroll_up(struct text_screen *s)
 		s->cells[i] = s->cells[i + ncols];
 	for (; i < total; i++)
 		s->cells[i] = (unsigned short)((SCREEN_ATTR << 8) | ' ');
-	s->dirty = 1;
+	screen_damage_all(s);
 }
 
 void screen_putchar(struct text_screen *s, char c)
@@ -47,6 +109,7 @@ void screen_putchar(struct text_screen *s, char c)
 	netlog_char(c);   /* terminal text also goes to the netlog mirror */
 
 	if (c == '\n') {
+		screen_damage_cursor(s);      /* the cursor leaves this cell */
 		s->cursor = (s->cursor / ncols + 1) * ncols;
 		if (s->cursor >= total) {
 			screen_scroll_up(s);
@@ -54,22 +117,24 @@ void screen_putchar(struct text_screen *s, char c)
 			s->scrolled = 1;
 		}
 		s->cursor_attr = (SCREEN_ATTR << 8);
-		s->dirty = 1;
+		screen_damage_cursor(s);
 		return;
 	}
 	if (c == '\b') {
 		if (s->cursor > 0) {
+			screen_damage_cursor(s);
 			s->cursor--;
 			s->cells[s->cursor] = (unsigned short)((SCREEN_ATTR << 8) | ' ');
-			s->dirty = 1;
+			screen_damage_cursor(s);
 		}
 		return;
 	}
 	if (s->cursor < total) {
+		screen_damage_cursor(s);      /* the written cell + the cursor */
 		s->cells[s->cursor] = (unsigned short)(s->cursor_attr | (unsigned char)c);
 		s->cursor++;
 		s->cursor_attr = (SCREEN_ATTR << 8);
-		s->dirty = 1;
+		screen_damage_cursor(s);      /* the cursor moved on */
 	}
 }
 
@@ -101,15 +166,17 @@ void screen_set_cursor(struct text_screen *s, unsigned int pos)
 {
 	if (pos >= s->cols * s->rows)
 		return;
+	screen_damage_cursor(s);      /* erase the old cursor cell */
 	s->cursor = pos;
 	s->cursor_attr = (SCREEN_ATTR << 8);
+	screen_damage_cursor(s);
 }
 
 void screen_put_at(struct text_screen *s, unsigned int pos, unsigned short value)
 {
 	if (pos < s->cols * s->rows) {
+		screen_damage_cell(s, pos % s->cols, pos / s->cols);
 		s->cells[pos] = value;
-		s->dirty = 1;
 	}
 }
 

+ 16 - 0
screen.h

@@ -14,7 +14,14 @@ struct text_screen {
 	unsigned int cursor;          /* linear index 0..cols*rows-1 */
 	unsigned int cursor_attr;
 	unsigned int cursor_visible;
+	/* Pending damage: the inclusive cell block [dmg_col0..dmg_col1] x
+	   [dmg_row0..dmg_row1] that changed since the last render, so the WM
+	   repaints a keystroke's cell instead of the whole window content.
+	   Only the helpers below update it -- screen_damage_block() reports it
+	   and screen_clear_damage() drops it after rendering. */
 	int dirty;                    /* content changed since last WM render */
+	unsigned int dmg_col0, dmg_col1;
+	unsigned int dmg_row0, dmg_row1;
 	int scrolled;                 /* one-shot, replaces io_did_scroll */
 };
 
@@ -28,4 +35,13 @@ void screen_set_cursor(struct text_screen *s, unsigned int pos);
 void screen_put_at(struct text_screen *s, unsigned int pos, unsigned short value);
 int screen_did_scroll(struct text_screen *s);
 
+/* Pending damage as a cell block: returns 1 and fills col0, row0, cols and
+   rows when something changed, 0 when the screen is clean. */
+int screen_damage_block(const struct text_screen *s, unsigned int *col0,
+		unsigned int *row0, unsigned int *cols, unsigned int *rows);
+void screen_clear_damage(struct text_screen *s);
+/* Mark the cell the cursor sits in (a blink or a cursor move only changes
+   that cell, but both the old and the new position must be repainted). */
+void screen_damage_cursor(struct text_screen *s);
+
 #endif

+ 18 - 9
wm.c

@@ -627,7 +627,7 @@ static void wm_redraw_if_needed(void)
 			if (windows[i].used) {
 				struct gfx_canvas *g = sys_gfx_canvas_for(&windows[i].term->screen);
 
-				windows[i].term->screen.dirty = 0;
+				screen_clear_damage(&windows[i].term->screen);
 				if (g)
 					g->drawn = seq_before[i];   /* keep a frame
 						presented during the render */
@@ -643,22 +643,31 @@ static void wm_redraw_if_needed(void)
 	}
 
 	/* Any window whose content changed (focused or not): repaint just its
-	   content rect through the damage-rect compositor. */
+	   damaged cell block (a canvas window repaints its whole content)
+	   through the damage-rect compositor. */
 	for (i = 0; i < MAX_WINDOWS; i++) {
+		struct text_screen *s = &windows[i].term->screen;
 		struct gfx_canvas *g;
-		unsigned int seq;
+		unsigned int seq, col0, row0, ncols, nrows;
 
 		if (!windows[i].used)
 			continue;
-		g = sys_gfx_canvas_for(&windows[i].term->screen);
-		if (!windows[i].term->screen.dirty && !(g && g->seq != g->drawn))
+		g = sys_gfx_canvas_for(s);
+		if (!screen_damage_block(s, &col0, &row0, &ncols, &nrows) &&
+		    !(g && g->seq != g->drawn))
 			continue;
 		seq = g ? g->seq : 0;   /* capture before compositing */
 		mouse_cursor_erase();
-		wm_redraw_rect(windows[i].x + 1, windows[i].y + 1 + WM_TITLE_H,
-			windows[i].pw - 2, SCREEN_ROWS * 10);
+		if (g)
+			wm_redraw_rect(windows[i].x + 1,
+				windows[i].y + 1 + WM_TITLE_H,
+				windows[i].pw - 2, SCREEN_ROWS * 10);
+		else
+			wm_redraw_rect(windows[i].x + 1 + col0 * 8,
+				windows[i].y + 1 + WM_TITLE_H + row0 * 10,
+				ncols * 8, nrows * 10);
 		mouse_redraw_cursor();
-		windows[i].term->screen.dirty = 0;
+		screen_clear_damage(s);
 		if (g)
 			g->drawn = seq;
 	}
@@ -691,7 +700,7 @@ static void wm_blink_cursor(void)
 	if (now - last_cursor_tick >= CURSOR_MS) {
 		last_cursor_tick = now;
 		fw->term->screen.cursor_visible = !fw->term->screen.cursor_visible;
-		fw->term->screen.dirty = 1;
+		screen_damage_cursor(&fw->term->screen);
 	}
 }