ソースを参照

Much improve framerate

Johannes Zellner 5 ヶ月 前
親
コミット
3fbd2e8f69
4 ファイル変更、86 行追加、25 行削除
  1. 3 1
      fbtext.c
  2. 3 6
      switch.S
  3. 78 16
      vga.c
  4. 2 2
      vga.h

+ 3 - 1
fbtext.c

@@ -126,7 +126,7 @@ void fbtext_scroll_up_one_line(void)
 {
 	unsigned int y, i;
 	unsigned int pitch = vga_fb_pitch();
-	volatile unsigned char *fb = (volatile unsigned char *)vga_fb_ptr();
+	unsigned char *fb = (unsigned char *)vga_fb_ptr();
 	unsigned int height = vga_fb_height();
 
 	if (!fb)
@@ -145,6 +145,8 @@ void fbtext_scroll_up_one_line(void)
 		for (i = 0; i < pitch; i++)
 			fb[off + i] = 0;
 	}
+	/* Mark scrolled rows dirty so the flush copies them. */
+	vga_fb_mark_dirty_rect(CONTENT_TOP, height - CONTENT_TOP);
 }
 
 void fbtext_set_cursor(unsigned int col, unsigned int row)

+ 3 - 6
switch.S

@@ -6,8 +6,8 @@
  * and -- when the timeslice expires -- call schedule() and swap ESP
  * to the next process.
  *
- * Shadow buffer flush: every ~33 ticks (30Hz), copy the shadow buffer
- * to the video framebuffer using USWC write-combining.
+ * Shadow buffer flush: every ~33 ticks (30Hz), set pending flag.
+ * Main loop checks dirty rows and copies only what changed.
  */
 .code32
 .text
@@ -16,7 +16,6 @@
 .extern current_pid
 .extern procs
 .extern schedule
-.extern vga_shadow_dirty
 .extern vga_shadow_flush_pending
 
 /* struct process offsets (must match process.h / process.c) */
@@ -35,12 +34,10 @@ pit_irq_stub:
 	movb  $0x20, %al
 	outb  %al, $0x20
 
-	/* Shadow buffer flush at 30Hz: just set flag, main loop does the copy. */
+	/* Shadow buffer flush at 30Hz: set pending flag, main loop does the copy. */
 	decl  flush_counter
 	jnz   .no_flush
 	movl  $FLUSH_INTERVAL, flush_counter
-	cmpl  $0, vga_shadow_dirty
-	jz    .no_flush
 	movl  $1, vga_shadow_flush_pending
 .no_flush:
 

+ 78 - 16
vga.c

@@ -16,7 +16,45 @@ static int vga_fb_cached;
 static unsigned char *vga_shadow_buf;
 static unsigned int   vga_shadow_pitch;
 static unsigned int   vga_shadow_size;
-int vga_shadow_dirty;
+
+/* Dirty row bitmap: one bit per row. Max 2048 rows = 256 bytes. */
+#define MAX_DIRTY_ROWS 2048
+static unsigned char vga_shadow_dirty_rows[MAX_DIRTY_ROWS / 8];
+
+static void mark_dirty_row(unsigned int y)
+{
+	if (y < MAX_DIRTY_ROWS)
+		vga_shadow_dirty_rows[y / 8] |= (1 << (y % 8));
+}
+
+static void mark_dirty_rect(unsigned int y, unsigned int h)
+{
+	unsigned int i;
+	for (i = 0; i < h; i++)
+		mark_dirty_row(y + i);
+}
+
+static int any_dirty_rows(void)
+{
+	unsigned int i;
+	unsigned int max_row = vga_fb_h;
+	if (max_row > MAX_DIRTY_ROWS)
+		max_row = MAX_DIRTY_ROWS;
+	for (i = 0; i < (max_row + 7) / 8; i++)
+		if (vga_shadow_dirty_rows[i])
+			return 1;
+	return 0;
+}
+
+static void clear_dirty_rows(void)
+{
+	unsigned int i;
+	unsigned int max_row = vga_fb_h;
+	if (max_row > MAX_DIRTY_ROWS)
+		max_row = MAX_DIRTY_ROWS;
+	for (i = 0; i < (max_row + 7) / 8; i++)
+		vga_shadow_dirty_rows[i] = 0;
+}
 
 /*
  * Force CRTC Start Address to 0 and Line Compare to max (0x3FF).
@@ -184,6 +222,7 @@ static void shadow_put_pixel(unsigned int x, unsigned int y, unsigned int r, uns
 		p[off + 2] = (unsigned char)r;
 		p[off + 3] = 0;
 	}
+	mark_dirty_row(y);
 }
 
 void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
@@ -192,7 +231,6 @@ void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned
 	if (!vga_shadow_buf)
 		return;
 	shadow_put_pixel(x, y, r, g, b);
-	vga_shadow_dirty = 1;
 }
 
 static unsigned int vga_pixel_bytes(void)
@@ -240,7 +278,7 @@ void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned
 		for (col = 0; col < w * pb; col++)
 			dst[col] = buf[row * w * pb + col];
 	}
-	vga_shadow_dirty = 1;
+	mark_dirty_rect(y, h);
 }
 
 void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
@@ -280,7 +318,7 @@ void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
 			for (x = 0; x < vga_fb_w; x++)
 				fb[row * stride + x] = pixel;
 	}
-	vga_shadow_dirty = 1;
+	mark_dirty_rect(0, vga_fb_h);
 }
 
 /* ---- Shadow buffer init and flush ---- */
@@ -301,8 +339,8 @@ void vga_shadow_init(void)
 	vga_shadow_buf = (unsigned char *)VGA_SHADOW_BASE;
 	vga_shadow_pitch = vga_fb_pitch_val;
 	vga_shadow_size = vga_fb_h * vga_shadow_pitch;
-	vga_shadow_dirty = 0;
 	vga_shadow_flush_pending = 0;
+	clear_dirty_rows();
 	serial_puts("vga: shadow buffer ");
 	serial_puthex((unsigned int)VGA_SHADOW_BASE);
 	serial_puts(" size=");
@@ -310,11 +348,28 @@ void vga_shadow_init(void)
 	serial_puts("KB\n");
 }
 
-/* Single bulk copy: shadow pitch == VRAM pitch, so entire buffer is contiguous. */
-static void shadow_bulk_copy(void)
+/* Copy only dirty rows from shadow to VRAM. */
+static void shadow_copy_dirty_rows(void)
 {
-	unsigned int dwords = vga_shadow_size / 4;
-	vga_fb_copy_row((void *)vga_fb_lfb, (void *)vga_shadow_buf, dwords);
+	unsigned int row;
+	unsigned int pb = vga_pixel_bytes();
+	unsigned int row_bytes = vga_fb_w * pb;
+	unsigned int dwords = row_bytes / 4;
+	unsigned char *src = vga_shadow_buf;
+	unsigned char *dst = (unsigned char *)vga_fb_lfb;
+	unsigned int max_row = vga_fb_h;
+	if (max_row > MAX_DIRTY_ROWS)
+		max_row = MAX_DIRTY_ROWS;
+
+	for (row = 0; row < max_row; row++) {
+		if (vga_shadow_dirty_rows[row / 8] & (1 << (row % 8))) {
+			vga_fb_copy_row(
+				(void *)(dst + (unsigned long)row * vga_fb_pitch_val),
+				(void *)(src + (unsigned long)row * vga_shadow_pitch),
+				dwords
+			);
+		}
+	}
 }
 
 /* Debug square: 32x32 at bottom-right, cycles color each flush. Drawn into shadow buffer
@@ -358,6 +413,7 @@ static void draw_flush_debug_square(void)
 			}
 		}
 	}
+	mark_dirty_rect(sy, 32);
 }
 
 void vga_fb_flush_pending_check(void)
@@ -365,25 +421,31 @@ void vga_fb_flush_pending_check(void)
 	if (!vga_shadow_flush_pending)
 		return;
 	vga_shadow_flush_pending = 0;
-	if (!vga_shadow_dirty)
+	if (!any_dirty_rows())
 		return;
-	serial_puts("vga: flush\n");
 	draw_flush_debug_square();
-	shadow_bulk_copy();
-	vga_shadow_dirty = 0;
+	shadow_copy_dirty_rows();
+	clear_dirty_rows();
 }
 
 void vga_fb_flush(void)
 {
 	vga_cache_boot_fb();
-	if (!vga_shadow_buf || !vga_fb_lfb || !vga_shadow_dirty)
+	if (!vga_shadow_buf || !vga_fb_lfb)
+		return;
+	if (!any_dirty_rows())
 		return;
 	draw_flush_debug_square();
-	shadow_bulk_copy();
-	vga_shadow_dirty = 0;
+	shadow_copy_dirty_rows();
+	clear_dirty_rows();
 }
 
 int vga_shadow_is_active(void)
 {
 	return vga_shadow_buf != 0;
 }
+
+void vga_fb_mark_dirty_rect(unsigned int y, unsigned int h)
+{
+	mark_dirty_rect(y, h);
+}

+ 2 - 2
vga.h

@@ -53,7 +53,7 @@ void vga_fb_flush_pending_check(void);
 /* Returns 1 if shadow buffer is active, 0 if rendering directly to VRAM. */
 int vga_shadow_is_active(void);
 
-/* Dirty flag: set by any draw operation, checked by timer flush. */
-extern int vga_shadow_dirty;
+/* Mark rows dirty for direct buffer manipulators (e.g. fbtext_scroll). */
+void vga_fb_mark_dirty_rect(unsigned int y, unsigned int h);
 
 #endif