Просмотр исходного кода

Flush the shadow buffer every main-loop iteration with a byte budget

The 30 Hz PIT-gated flush put up to 33 ms between drawing and pixels
reaching VRAM: a cursor move (~1 KB) was drawn in microseconds but only
shown on the next tick, and a large repaint could stall input behind its
own copy.

- switch.S: drop the flush counter and vga_shadow_flush_pending -- the
  PIT no longer paces rendering.
- vga.c/h: shadow_copy_dirty_rows() takes a byte budget and clears each
  row as it is copied, so a flush resumes with the remainder on the next
  call; vga_fb_flush_budget() is the new entry point (vga_fb_flush() stays
  as the unbounded form for boot and blocking redraws) and
  VGA_FLUSH_BUDGET_BYTES (256 KB, roughly a millisecond of VRAM writes) is
  the suggested per-iteration budget.
- wm.c: the main loop flushes after drawing and before idling, so small
  updates (cursor, one keystroke's row) reach the screen within a loop
  instead of waiting for a fixed tick, while a full-screen repaint is
  chunked so it cannot starve input.
Johannes Zellner 1 месяц назад
Родитель
Сommit
cdd218dece
6 измененных файлов с 50 добавлено и 45 удалено
  1. 7 3
      AGENTS.md
  2. 1 1
      mtrr.c
  3. 1 12
      switch.S
  4. 27 20
      vga.c
  5. 12 7
      vga.h
  6. 2 2
      wm.c

+ 7 - 3
AGENTS.md

@@ -99,7 +99,8 @@ below the run area), so adding code no longer moves any address.
   `sys_gfx_reap`/`sys_gfx_collect` free the canvas after exit/crash.  `clock`
   `sys_gfx_reap`/`sys_gfx_collect` free the canvas after exit/crash.  `clock`
   and `colors` are its users.
   and `colors` are its users.
 - **Graphics**: `vga.c/h`, `vga_copy.S`, `gpu.c`, `ati.c`, `nvidia.c`, `nv50.c`,
 - **Graphics**: `vga.c/h`, `vga_copy.S`, `gpu.c`, `ati.c`, `nvidia.c`, `nv50.c`,
-  `pci.h` — VESA LFB + shadow buffer, 30 Hz dirty-row flush. ATI Mach64 CRTC
+  `pci.h` — VESA LFB + shadow buffer, damage-driven dirty-row flush (byte
+  budget per main-loop iteration, no fixed frame rate). ATI Mach64 CRTC
   fixup; NVIDIA is VBE-only (never touch the legacy VGA CRTC). `nvidia.c`
   fixup; NVIDIA is VBE-only (never touch the legacy VGA CRTC). `nvidia.c`
   identifies the card, maps BAR0/BAR1/BAR3 and reads the MCP7x stolen-VRAM
   identifies the card, maps BAR0/BAR1/BAR3 and reads the MCP7x stolen-VRAM
   geometry; `nv50.c` probes the nv50-family display engine at boot (read-only,
   geometry; `nv50.c` probes the nv50-family display engine at boot (read-only,
@@ -630,8 +631,11 @@ Caveats:
   `procs[i].cr3` made the boot-time MMIO mapping (`paging_map_mmio_cr3`) fault
   `procs[i].cr3` made the boot-time MMIO mapping (`paging_map_mmio_cr3`) fault
   on the board while QEMU was fine.  Anything above `MM_MAX_PHYS` (128 MB) is
   on the board while QEMU was fine.  Anything above `MM_MAX_PHYS` (128 MB) is
   also *unmapped*, so a wild pointer there is an instant #PF.
   also *unmapped*, so a wild pointer there is an instant #PF.
-- **All drawing goes into the shadow buffer** (`vga_put_pixel_rgb` etc.); a 30 Hz
-  flush copies dirty rows to VRAM. Full-screen programs call `vga_clear_rgb`.
+- **All drawing goes into the shadow buffer** (`vga_put_pixel_rgb` etc.); the
+  main loop flushes dirty rows to VRAM on every iteration (byte budget, see
+  `VGA_FLUSH_BUDGET_BYTES`) so small updates (cursor, a keystroke) reach the
+  screen within a loop instead of waiting for a fixed tick. Full-screen programs
+  call `vga_clear_rgb`.
 - **FAT is read/write** — FAT12/16/32 read plus write/delete/mkdir/rmdir.
 - **FAT is read/write** — FAT12/16/32 read plus write/delete/mkdir/rmdir.
   FAT32 allocation uses an FSInfo-seeded free-cluster hint; the FSInfo
   FAT32 allocation uses an FSInfo-seeded free-cluster hint; the FSInfo
   free-count is not updated and a completely full FAT32 directory is not
   free-count is not updated and a completely full FAT32 directory is not

+ 1 - 1
mtrr.c

@@ -2,7 +2,7 @@
  * MTRR write-combining.
  * MTRR write-combining.
  *
  *
  * The VESA LFB is mapped uncacheable (UC) by paging.c, so every dword the
  * The VESA LFB is mapped uncacheable (UC) by paging.c, so every dword the
- * 30 Hz shadow flush writes to VRAM is a separate uncached PCI transaction.
+ * shadow flush writes to VRAM is a separate uncached PCI transaction.
  * Marking the LFB range write-combining (WC) via an MTRR lets the CPU batch
  * Marking the LFB range write-combining (WC) via an MTRR lets the CPU batch
  * those writes into burst transfers, which is the single biggest rendering
  * those writes into burst transfers, which is the single biggest rendering
  * win on real hardware.
  * win on real hardware.

+ 1 - 12
switch.S

@@ -6,7 +6,7 @@
  * and -- when the timeslice expires -- call schedule() and swap ESP
  * and -- when the timeslice expires -- call schedule() and swap ESP
  * to the next process.
  * to the next process.
  *
  *
- * Shadow buffer flush: every ~33 ticks (30Hz), set pending flag.
+ * Shadow buffer flush: damage-driven, done by the main loop (byte budget).
  * Main loop checks dirty rows and copies only what changed.
  * Main loop checks dirty rows and copies only what changed.
  *
  *
  * Serial poll: every tick, check UART for received data and push
  * Serial poll: every tick, check UART for received data and push
@@ -19,7 +19,6 @@
 .extern current_pid
 .extern current_pid
 .extern procs
 .extern procs
 .extern schedule
 .extern schedule
-.extern vga_shadow_flush_pending
 .extern serial_poll
 .extern serial_poll
 .extern kdbg_pit_tick
 .extern kdbg_pit_tick
 .extern tss
 .extern tss
@@ -34,7 +33,6 @@
 .equ PCB_SIZE,  116     /* sizeof(struct process): 44 + cwd[64] + 2*4 */
 .equ PCB_SIZE,  116     /* sizeof(struct process): 44 + cwd[64] + 2*4 */
 
 
 .equ TIMESLICE, 20
 .equ TIMESLICE, 20
-.equ FLUSH_INTERVAL, 33 /* 1000Hz PIT / 30Hz flush ≈ 33 ticks */
 
 
 .globl pit_irq_stub
 .globl pit_irq_stub
 pit_irq_stub:
 pit_irq_stub:
@@ -68,13 +66,6 @@ pit_irq_stub:
 	 * some targets; this keeps a live indicator on the framebuffer). */
 	 * some targets; this keeps a live indicator on the framebuffer). */
 	call  kdbg_pit_tick
 	call  kdbg_pit_tick
 
 
-	/* Shadow buffer flush at 30Hz: set pending flag, main loop does the copy. */
-	decl  flush_counter
-	jnz   .no_flush
-	movl  $FLUSH_INTERVAL, flush_counter
-	movl  $1, vga_shadow_flush_pending
-.no_flush:
-
 	/* Decrement timeslice counter; switch only when it reaches 0. */
 	/* Decrement timeslice counter; switch only when it reaches 0. */
 	decl  slice_counter
 	decl  slice_counter
 	jnz   .no_switch
 	jnz   .no_switch
@@ -114,5 +105,3 @@ pit_irq_stub:
 .align 4
 .align 4
 slice_counter:
 slice_counter:
 	.long TIMESLICE
 	.long TIMESLICE
-flush_counter:
-	.long FLUSH_INTERVAL

+ 27 - 20
vga.c

@@ -1,4 +1,4 @@
-/* VGA: VESA LFB (from boot) with shadow buffer and 30Hz USWC flush. */
+/* VGA: VESA LFB (from boot) with a shadow buffer and a damage-driven flush. */
 
 
 #include "vga.h"
 #include "vga.h"
 #include "pci.h"
 #include "pci.h"
@@ -415,8 +415,6 @@ void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
 /* Assembly row copy: rep movsl optimized for P5 USWC write-combining to UC memory. */
 /* Assembly row copy: rep movsl optimized for P5 USWC write-combining to UC memory. */
 extern void vga_fb_copy_row(void *dst, void *src, unsigned int dwords);
 extern void vga_fb_copy_row(void *dst, void *src, unsigned int dwords);
 
 
-int vga_shadow_flush_pending;
-
 void vga_shadow_init(void)
 void vga_shadow_init(void)
 {
 {
 	vga_cache_boot_fb();
 	vga_cache_boot_fb();
@@ -426,14 +424,16 @@ void vga_shadow_init(void)
 	vga_shadow_buf = (unsigned char *)VGA_SHADOW_BASE;
 	vga_shadow_buf = (unsigned char *)VGA_SHADOW_BASE;
 	vga_shadow_pitch = vga_fb_pitch_val;
 	vga_shadow_pitch = vga_fb_pitch_val;
 	vga_shadow_size = vga_fb_h * vga_shadow_pitch;
 	vga_shadow_size = vga_fb_h * vga_shadow_pitch;
-	vga_shadow_flush_pending = 0;
 	clear_dirty_rows();
 	clear_dirty_rows();
 }
 }
 
 
-/* Copy only dirty rows from shadow to VRAM, batching contiguous runs. */
-static void shadow_copy_dirty_rows(void)
+/* Copy dirty rows from shadow to VRAM, at most about `max_bytes` this call
+ * (0 = no limit).  A row is cleared as it is copied, so the next call resumes
+ * with whatever is left: a large repaint is spread over several main-loop
+ * iterations instead of stalling input behind it. */
+static unsigned int shadow_copy_dirty_rows(unsigned int max_bytes)
 {
 {
-	unsigned int row;
+	unsigned int row, bytes = 0;
 	unsigned int pb = vga_pixel_bytes();
 	unsigned int pb = vga_pixel_bytes();
 	unsigned int align = (pb == 2) ? 2 : (pb == 3) ? 4 : 1;
 	unsigned int align = (pb == 2) ? 2 : (pb == 3) ? 4 : 1;
 	unsigned int x0 = vga_dirty_x0;
 	unsigned int x0 = vga_dirty_x0;
@@ -454,39 +454,46 @@ static void shadow_copy_dirty_rows(void)
 	if (x1 > vga_fb_w)
 	if (x1 > vga_fb_w)
 		x1 = vga_fb_w;
 		x1 = vga_fb_w;
 	if (x0 >= x1)
 	if (x0 >= x1)
-		return;
+		return 0;
 	dwords = ((x1 - x0) * pb) / 4;
 	dwords = ((x1 - x0) * pb) / 4;
 
 
 	for (row = 0; row < max_row; row++) {
 	for (row = 0; row < max_row; row++) {
 		if (vga_shadow_dirty_rows[row / 8] & (1 << (row % 8))) {
 		if (vga_shadow_dirty_rows[row / 8] & (1 << (row % 8))) {
+			vga_shadow_dirty_rows[row / 8] &= ~(1 << (row % 8));
 			vga_fb_copy_row(
 			vga_fb_copy_row(
 				(void *)(dst + (unsigned long)row * vga_fb_pitch_val + x0 * pb),
 				(void *)(dst + (unsigned long)row * vga_fb_pitch_val + x0 * pb),
 				(void *)(src + (unsigned long)row * vga_shadow_pitch + x0 * pb),
 				(void *)(src + (unsigned long)row * vga_shadow_pitch + x0 * pb),
 				dwords);
 				dwords);
+			bytes += dwords * 4;
+			if (max_bytes && bytes >= max_bytes)
+				break;
 		}
 		}
 	}
 	}
+	return bytes;
 }
 }
 
 
-void vga_fb_flush_pending_check(void)
+/* Copy up to `max_bytes` of the dirty shadow rows to VRAM.  Called from the
+ * main loop on every iteration: small updates (a cursor move is ~1 KB) reach
+ * the screen within one loop instead of waiting for a fixed 30 Hz tick, while
+ * a full-screen repaint is chunked so it cannot stall input. */
+void vga_fb_flush_budget(unsigned int max_bytes)
 {
 {
-	if (!vga_shadow_flush_pending)
+	vga_cache_boot_fb();
+	if (!vga_shadow_buf || !vga_fb_lfb)
 		return;
 		return;
-	vga_shadow_flush_pending = 0;
 	if (!any_dirty_rows())
 	if (!any_dirty_rows())
 		return;
 		return;
-	shadow_copy_dirty_rows();
-	clear_dirty_rows();
+	if (shadow_copy_dirty_rows(max_bytes) == 0) {
+		clear_dirty_rows();          /* degenerate span: drop the marks */
+		return;
+	}
+	if (!any_dirty_rows())
+		clear_dirty_rows();          /* also resets the dirty x-span */
 }
 }
 
 
 void vga_fb_flush(void)
 void vga_fb_flush(void)
 {
 {
-	vga_cache_boot_fb();
-	if (!vga_shadow_buf || !vga_fb_lfb)
-		return;
-	if (!any_dirty_rows())
-		return;
-	shadow_copy_dirty_rows();
-	clear_dirty_rows();
+	vga_fb_flush_budget(0);
 }
 }
 
 
 int vga_shadow_is_active(void)
 int vga_shadow_is_active(void)

+ 12 - 7
vga.h

@@ -1,4 +1,5 @@
-/* VGA: VESA LFB (from boot). Shadow buffer with 30Hz USWC flush. */
+/* VGA: VESA LFB (from boot). Shadow buffer, damage-driven flush with a
+ * per-iteration byte budget (no fixed frame rate). */
 #ifndef VGA_H
 #ifndef VGA_H
 #define VGA_H
 #define VGA_H
 
 
@@ -19,7 +20,8 @@ struct vga_boot_fb_info {
 /* Shadow buffer lives at 1MB — safely above kernel, stacks, DMA, and program load area. */
 /* Shadow buffer lives at 1MB — safely above kernel, stacks, DMA, and program load area. */
 #define VGA_SHADOW_BASE 0x100000
 #define VGA_SHADOW_BASE 0x100000
 
 
-/* LFB API. RGB 0..255. All drawing goes into the shadow buffer; flush copies to VRAM at 30Hz. */
+/* LFB API. RGB 0..255. All drawing goes into the shadow buffer; the main loop
+ * flushes dirty areas to VRAM on every iteration (byte-budgeted). */
 unsigned int vga_fb_width(void);
 unsigned int vga_fb_width(void);
 unsigned int vga_fb_height(void);
 unsigned int vga_fb_height(void);
 unsigned int vga_fb_pitch(void);
 unsigned int vga_fb_pitch(void);
@@ -45,14 +47,17 @@ void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned
 /* Initialize shadow buffer (called once from kernel_main after vga_cache_boot_fb). */
 /* Initialize shadow buffer (called once from kernel_main after vga_cache_boot_fb). */
 void vga_shadow_init(void);
 void vga_shadow_init(void);
 
 
-/* Copy shadow buffer to VRAM. Called by 30Hz timer or manually after init. */
+/* Copy shadow buffer to VRAM (everything dirty, no budget). Called after
+ * blocking redraws and at boot. */
 void vga_fb_flush(void);
 void vga_fb_flush(void);
 
 
-/* Flag set by PIT IRQ when 30Hz flush is due. Main loop checks and clears. */
-extern int vga_shadow_flush_pending;
+/* Copy up to max_bytes of the dirty shadow rows to VRAM (0 = no limit).
+ * Called by the main loop every iteration: small updates reach the screen
+ * within one loop, large ones are chunked so they cannot stall input. */
+void vga_fb_flush_budget(unsigned int max_bytes);
 
 
-/* Called from main loop: if pending, copies shadow to VRAM and draws debug square. */
-void vga_fb_flush_pending_check(void);
+/* Per-iteration flush budget: roughly a millisecond of VRAM writes. */
+#define VGA_FLUSH_BUDGET_BYTES (256u * 1024u)
 
 
 /* Returns 1 if shadow buffer is active, 0 if rendering directly to VRAM. */
 /* Returns 1 if shadow buffer is active, 0 if rendering directly to VRAM. */
 int vga_shadow_is_active(void);
 int vga_shadow_is_active(void);

+ 2 - 2
wm.c

@@ -318,7 +318,7 @@ static void wm_redraw_rect(unsigned int x, unsigned int y,
 
 
 /* Move a window in the shadow buffer: copy its rect, redraw the exposed
 /* Move a window in the shadow buffer: copy its rect, redraw the exposed
    sliver, keep the cursor on top, and mark the swept rows dirty.  Does not
    sliver, keep the cursor on top, and mark the swept rows dirty.  Does not
-   touch VRAM; the 30 Hz flush copies the affected rows. */
+   touch VRAM; the main loop's budgeted flush copies the affected rows. */
 static void wm_drag_shadow(struct window *w, unsigned int ox, unsigned int oy,
 static void wm_drag_shadow(struct window *w, unsigned int ox, unsigned int oy,
 			   unsigned int nx, unsigned int ny)
 			   unsigned int nx, unsigned int ny)
 {
 {
@@ -712,7 +712,7 @@ void wm_run(void)
 		wm_handle_kexec();
 		wm_handle_kexec();
 		sys_gfx_collect();
 		sys_gfx_collect();
 		wm_redraw_if_needed();
 		wm_redraw_if_needed();
-		vga_fb_flush_pending_check();
+		vga_fb_flush_budget(VGA_FLUSH_BUDGET_BYTES);
 		cpu_idle();
 		cpu_idle();
 	}
 	}
 }
 }