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

Improve rendering performance on the cirrus chip

Johannes Zellner 1 месяц назад
Родитель
Сommit
5b909756c2
3 измененных файлов с 67 добавлено и 49 удалено
  1. 12 48
      vga.c
  2. 3 0
      vga.h
  3. 52 1
      wm.c

+ 12 - 48
vga.c

@@ -393,7 +393,7 @@ void vga_shadow_init(void)
 	clear_dirty_rows();
 }
 
-/* Copy only dirty rows from shadow to VRAM. */
+/* Copy only dirty rows from shadow to VRAM, batching contiguous runs. */
 static void shadow_copy_dirty_rows(void)
 {
 	unsigned int row;
@@ -408,59 +408,20 @@ static void shadow_copy_dirty_rows(void)
 
 	for (row = 0; row < max_row; row++) {
 		if (vga_shadow_dirty_rows[row / 8] & (1 << (row % 8))) {
+			unsigned int run = row + 1;
+			while (run < max_row &&
+			       (vga_shadow_dirty_rows[run / 8] & (1 << (run % 8))))
+				run++;
 			vga_fb_copy_row(
 				(void *)(dst + (unsigned long)row * vga_fb_pitch_val),
 				(void *)(src + (unsigned long)row * vga_shadow_pitch),
-				dwords
+				dwords * (run - row)
 			);
+			row = run - 1;
 		}
 	}
 }
 
-/* Debug square: 32x32 at bottom-right, cycles color each flush. Drawn into shadow buffer
-   so it gets copied to VRAM along with everything else. Uses bright colors to be obvious. */
-static void draw_flush_debug_square(void)
-{
-	static unsigned int flush_color_counter;
-	unsigned int r, g, b;
-	unsigned int x, y;
-	unsigned int sx = vga_fb_w - 32;
-	unsigned int sy = vga_fb_h - 32;
-	unsigned int pitch = vga_shadow_pitch;
-	unsigned int bpp = vga_fb_bpp_val;
-	unsigned char *fb = vga_shadow_buf;
-
-	flush_color_counter++;
-	/* Cycle through bright primary/secondary colors */
-	switch (flush_color_counter % 6) {
-		case 0: r = 255; g = 0;   b = 0;   break; /* red */
-		case 1: r = 0;   g = 255; b = 0;   break; /* green */
-		case 2: r = 0;   g = 0;   b = 255; break; /* blue */
-		case 3: r = 255; g = 255; b = 0;   break; /* yellow */
-		case 4: r = 255; g = 0;   b = 255; break; /* magenta */
-		default: r = 0; g = 255; b = 255; break; /* cyan */
-	}
-
-	for (y = 0; y < 32; y++) {
-		unsigned char *row = fb + (sy + y) * pitch;
-		for (x = 0; x < 32; x++) {
-			if (bpp == 16) {
-				unsigned int rgb16 = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
-				unsigned short *p = (unsigned short *)(row + (sx + x) * 2);
-				*p = (unsigned short)rgb16;
-			} else if (bpp == 24) {
-				row[(sx + x) * 3]     = (unsigned char)b;
-				row[(sx + x) * 3 + 1] = (unsigned char)g;
-				row[(sx + x) * 3 + 2] = (unsigned char)r;
-			} else if (bpp == 32) {
-				unsigned int *p = (unsigned int *)(row + (sx + x) * 4);
-				*p = (unsigned int)b | ((unsigned int)g << 8) | ((unsigned int)r << 16);
-			}
-		}
-	}
-	mark_dirty_rect(sy, 32);
-}
-
 void vga_fb_flush_pending_check(void)
 {
 	if (!vga_shadow_flush_pending)
@@ -468,7 +429,6 @@ void vga_fb_flush_pending_check(void)
 	vga_shadow_flush_pending = 0;
 	if (!any_dirty_rows())
 		return;
-	draw_flush_debug_square();
 	shadow_copy_dirty_rows();
 	clear_dirty_rows();
 }
@@ -480,7 +440,6 @@ void vga_fb_flush(void)
 		return;
 	if (!any_dirty_rows())
 		return;
-	draw_flush_debug_square();
 	shadow_copy_dirty_rows();
 	clear_dirty_rows();
 }
@@ -494,3 +453,8 @@ void vga_fb_mark_dirty_rect(unsigned int y, unsigned int h)
 {
 	mark_dirty_rect(y, h);
 }
+
+void vga_fb_clear_dirty(void)
+{
+	clear_dirty_rows();
+}

+ 3 - 0
vga.h

@@ -58,4 +58,7 @@ int vga_shadow_is_active(void);
 /* Mark rows dirty for direct buffer manipulators (e.g. fbtext_scroll). */
 void vga_fb_mark_dirty_rect(unsigned int y, unsigned int h);
 
+/* Clear the dirty-row bitmap (so only explicitly re-marked rows get flushed). */
+void vga_fb_clear_dirty(void);
+
 #endif

+ 52 - 1
wm.c

@@ -24,6 +24,12 @@ static int drag_window;      /* index into windows[] */
 static int drag_dx, drag_dy; /* mouse offset from window origin */
 static unsigned int prev_buttons;
 
+/* Drag damage: accumulated y-band of the moving window, flushed as a region. */
+static int drag_render_needed;
+static int drag_band_valid;
+static unsigned int drag_band_y0, drag_band_y1;
+static unsigned int last_drag_render;
+
 void wm_request_redraw(void)
 {
 	wm_full_redraw = 1;
@@ -195,6 +201,24 @@ static void wm_clamp_window(struct window *w, int nx, int ny)
 	w->y = (unsigned int)ny;
 }
 
+/* Grow the drag's accumulated dirty band to cover the window's old+new rects.
+   Only the y-range matters: the flush copies whole rows. */
+static void wm_drag_accumulate(unsigned int oy, unsigned int ny, unsigned int ph)
+{
+	unsigned int y0 = (oy < ny) ? oy : ny;
+	unsigned int y1 = ((oy > ny) ? oy : ny) + ph;
+	if (!drag_band_valid) {
+		drag_band_y0 = y0;
+		drag_band_y1 = y1;
+		drag_band_valid = 1;
+	} else {
+		if (y0 < drag_band_y0)
+			drag_band_y0 = y0;
+		if (y1 > drag_band_y1)
+			drag_band_y1 = y1;
+	}
+}
+
 static void wm_handle_mouse(void)
 {
 	unsigned int buttons = mouse_buttons_state();
@@ -223,14 +247,20 @@ static void wm_handle_mouse(void)
 					drag_window = idx;
 					drag_dx = (int)mx - (int)w->x;
 					drag_dy = (int)my - (int)w->y;
+					drag_band_valid = 0;
+					drag_render_needed = 0;
 				}
 			}
 		}
 	} else if (left_down && left_was_down && drag_active) {
 		struct window *w = &windows[drag_window];
 		if (w->used) {
+			unsigned int ox = w->x, oy = w->y;
 			wm_clamp_window(w, (int)mx - drag_dx, (int)my - drag_dy);
-			wm_request_redraw();
+			if (ox != w->x || oy != w->y) {
+				wm_drag_accumulate(oy, w->y, w->ph);
+				drag_render_needed = 1;
+			}
 		} else {
 			drag_active = 0;
 		}
@@ -388,6 +418,27 @@ static void wm_redraw_if_needed(void)
 			if (windows[i].used)
 				windows[i].term->screen.dirty = 0;
 		wm_full_redraw = 0;
+		drag_render_needed = 0;
+		drag_band_valid = 0;
+		last_sec = now_sec;
+		return;
+	}
+
+	/* Drag: redraw the full scene into the shadow buffer, but flush only the
+	   accumulated y-band (the window's swept region) to VRAM. Throttled to the
+	   flush rate so rapid mouse moves coalesce. */
+	if (drag_render_needed && (now_ms - last_drag_render >= 33)) {
+		last_drag_render = now_ms;
+		wm_render_full();
+		vga_fb_clear_dirty();
+		if (drag_band_valid)
+			vga_fb_mark_dirty_rect(drag_band_y0, drag_band_y1 - drag_band_y0);
+		if (clock_changed)
+			vga_fb_mark_dirty_rect(0, WM_BAR_H);
+		for (i = 0; i < MAX_WINDOWS; i++)
+			if (windows[i].used)
+				windows[i].term->screen.dirty = 0;
+		drag_render_needed = 0;
 		last_sec = now_sec;
 		return;
 	}