Explorar o código

Keep a drag step in one frame and make the shadow copy dword-wide

The 256 KB flush budget is smaller than a window, and the flush always
started at the topmost dirty row, so a drag could only ship the top rows
of the swept band per iteration.  The next mouse move re-dirtied the whole
band and the flush started at the top again: the top of the window moved
while the rest visibly dragged behind.

- wm_drag_shadow() flushes its swept band immediately, so every drag step
  reaches VRAM as one consistent frame (and the cursor sits on top of it).
- VGA_FLUSH_BUDGET_BYTES grows to 1 MB: a window-sized repaint (~0.7 MB)
  is flushed as one frame, only full-screen repaints are chunked.
- wm_memmove() copies dwords (byte fallback for unaligned 24 bpp rows),
  making the drag's shadow copy about four times cheaper.
- wm_drag_fast() drops its now redundant flush.
Johannes Zellner hai 1 mes
pai
achega
b4cdea9312
Modificáronse 3 ficheiros con 39 adicións e 10 borrados
  1. 3 2
      AGENTS.md
  2. 4 2
      vga.h
  3. 32 6
      wm.c

+ 3 - 2
AGENTS.md

@@ -634,8 +634,9 @@ Caveats:
 - **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`.
+  screen within a loop instead of waiting for a fixed tick. A window drag
+  flushes its swept band immediately (`wm_drag_shadow`) so each step is one
+  consistent frame. Full-screen programs call `vga_clear_rgb`.
 - **FAT is read/write** — FAT12/16/32 read plus write/delete/mkdir/rmdir.
   FAT32 allocation uses an FSInfo-seeded free-cluster hint; the FSInfo
   free-count is not updated and a completely full FAT32 directory is not

+ 4 - 2
vga.h

@@ -56,8 +56,10 @@ void vga_fb_flush(void);
  * within one loop, large ones are chunked so they cannot stall input. */
 void vga_fb_flush_budget(unsigned int max_bytes);
 
-/* Per-iteration flush budget: roughly a millisecond of VRAM writes. */
-#define VGA_FLUSH_BUDGET_BYTES (256u * 1024u)
+/* Per-iteration flush budget: large enough that a window-sized repaint
+ * (~0.7 MB) is flushed as one frame, small enough that a full-screen repaint
+ * is chunked instead of stalling input behind it. */
+#define VGA_FLUSH_BUDGET_BYTES (1024u * 1024u)
 
 /* Returns 1 if shadow buffer is active, 0 if rendering directly to VRAM. */
 int vga_shadow_is_active(void);

+ 32 - 6
wm.c

@@ -230,11 +230,35 @@ static void wm_clamp_window(struct window *w, int nx, int ny)
 	w->y = (unsigned int)ny;
 }
 
-/* Byte copy that handles overlapping source/destination. */
+/* Overlap-safe copy.  Moving a window copies most of its area, so take four
+   bytes at a time when both rows are dword aligned (32 bpp rows always are)
+   and fall back to bytes otherwise (24 bpp at an odd x). */
 static void wm_memmove(unsigned char *dst, const unsigned char *src, unsigned int n)
 {
 	if (dst == src || n == 0)
 		return;
+
+	if ((((unsigned int)dst | (unsigned int)src) & 3) == 0) {
+		unsigned int words = n >> 2;
+		unsigned int i;
+		unsigned int *dw = (unsigned int *)dst;
+		const unsigned int *sw = (const unsigned int *)src;
+
+		if (dst < src) {
+			for (i = 0; i < words; i++)
+				dw[i] = sw[i];
+			for (i = words << 2; i < n; i++)
+				dst[i] = src[i];
+		} else {
+			/* Overlapping move down: back to front. */
+			for (i = n; i > (words << 2); i--)
+				dst[i - 1] = src[i - 1];
+			for (i = words; i-- > 0; )
+				dw[i] = sw[i];
+		}
+		return;
+	}
+
 	if (dst < src) {
 		unsigned int i;
 		for (i = 0; i < n; i++)
@@ -317,8 +341,10 @@ static void wm_redraw_rect(unsigned int x, unsigned int y,
 }
 
 /* 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
-   touch VRAM; the main loop's budgeted flush copies the affected rows. */
+   sliver, keep the cursor on top, and flush the swept band immediately so a
+   drag step is always one consistent frame (a budgeted flush would copy only
+   the top rows of the band per iteration and the window would appear to
+   stretch while the mouse keeps moving). */
 static void wm_drag_shadow(struct window *w, unsigned int ox, unsigned int oy,
 			   unsigned int nx, unsigned int ny)
 {
@@ -348,16 +374,16 @@ static void wm_drag_shadow(struct window *w, unsigned int ox, unsigned int oy,
 		vga_fb_mark_dirty_rect_xy(y0, y1 - y0, x0, x1);
 	}
 	mouse_redraw_cursor();
+	vga_fb_flush();
 }
 
-/* Cirrus fast drag: also move the window in VRAM with the BitBLT engine, then
-   mirror in the shadow buffer and flush immediately. */
+/* Cirrus fast drag: also move the window in VRAM with the BitBLT engine
+   (wm_drag_shadow flushes the mirrored copy in the shadow buffer). */
 static void wm_drag_fast(struct window *w, unsigned int ox, unsigned int oy,
 			 unsigned int nx, unsigned int ny)
 {
 	cirrus_blit(ox, oy, nx, ny, w->pw, w->ph);
 	wm_drag_shadow(w, ox, oy, nx, ny);
-	vga_fb_flush();
 }
 
 static void wm_handle_mouse(void)