Sfoglia il codice sorgente

Fix cursor lag over large window canvases

The software cursor shared the shadow-buffer damage queue with the
window under it.  A browser repaint marks its whole 960x640 canvas
dirty, and the cursor's 16x16 damage lies inside that rectangle, so
damage_add_locked() treated it as covered and dropped it.  The cursor
then waited behind up to three 1 MB flush iterations.  The browser also
re-presented its canvas at up to 100 Hz because ngui_begin() asked for
a redraw on every pointer sample, not just when the hover state
changed.

Present the cursor rectangle straight from the shadow to VRAM instead:
vga_fb_present_rect() copies it out of band and vga_fb_put_pixel()
writes the shape without per-pixel damage marks.  Erase the old
position when the WM releases the frame, but not at frame start, so the
cursor does not blink on every window repaint.  ngui now caps
pointer-motion repaints at 33 ms with one final frame after motion
stops; held-button drags stay at full rate.

The QEMU OHCI cursor test passes repeatedly, and the browser still
updates its link status bar on hover.
Johannes Zellner 2 settimane fa
parent
commit
fb05f402d8
5 ha cambiato i file con 163 aggiunte e 2 eliminazioni
  1. 31 2
      mouse.c
  2. 26 0
      ngui.c
  3. 7 0
      ngui.h
  4. 88 0
      vga.c
  5. 11 0
      vga.h

+ 31 - 2
mouse.c

@@ -127,6 +127,12 @@ static int mouse_pixel_at(int r, int c)
 
 
 static void cursor_save_under(void)
 static void cursor_save_under(void)
 {
 {
+	if (!vga_shadow_is_active()) {
+		/* Boot: the shadow buffer is not up yet, so there is no
+		 * background to save (and the saved buffer is uninitialized). */
+		saved_w = saved_h = 0;
+		return;
+	}
 	saved_x = cur_x;
 	saved_x = cur_x;
 	saved_y = cur_y;
 	saved_y = cur_y;
 	saved_w = (cur_x + MOUSE_CUR_W <= fbw) ? MOUSE_CUR_W : fbw - cur_x;
 	saved_w = (cur_x + MOUSE_CUR_W <= fbw) ? MOUSE_CUR_W : fbw - cur_x;
@@ -141,6 +147,15 @@ static void cursor_restore_under(void)
 		vga_fb_write_rect(saved_x, saved_y, saved_w, saved_h, cur_saved);
 		vga_fb_write_rect(saved_x, saved_y, saved_w, saved_h, cur_saved);
 }
 }
 
 
+/* Push the rectangle the cursor last touched to VRAM right away.  The cursor
+ * must not wait for the damage queue: over a large canvas (the browser) a
+ * whole-window repaint can sit ahead of it for several flush iterations. */
+static void cursor_present(void)
+{
+	if (saved_w && saved_h)
+		vga_fb_present_rect(saved_x, saved_y, saved_w, saved_h);
+}
+
 static void mouse_draw_cursor(unsigned int x0, unsigned int y0)
 static void mouse_draw_cursor(unsigned int x0, unsigned int y0)
 {
 {
 	unsigned int r, c;
 	unsigned int r, c;
@@ -150,9 +165,9 @@ static void mouse_draw_cursor(unsigned int x0, unsigned int y0)
 		for (c = 0; c < MOUSE_CUR_W; c++) {
 		for (c = 0; c < MOUSE_CUR_W; c++) {
 			int px = mouse_pixel_at((int)r, (int)c);
 			int px = mouse_pixel_at((int)r, (int)c);
 			if (px == MOUSE_PIX_FILL)
 			if (px == MOUSE_PIX_FILL)
-				vga_put_pixel_rgb(x0 + c, y0 + r, 255, 255, 255);
+				vga_fb_put_pixel(x0 + c, y0 + r, 255, 255, 255);
 			else if (px == MOUSE_PIX_OUTLINE)
 			else if (px == MOUSE_PIX_OUTLINE)
-				vga_put_pixel_rgb(x0 + c, y0 + r, 0, 0, 0);
+				vga_fb_put_pixel(x0 + c, y0 + r, 0, 0, 0);
 		}
 		}
 	}
 	}
 }
 }
@@ -225,6 +240,7 @@ static void mouse_cursor_setup(void)
 	}
 	}
 	cursor_save_under();
 	cursor_save_under();
 	mouse_draw_cursor(cur_x, cur_y);
 	mouse_draw_cursor(cur_x, cur_y);
+	cursor_present();
 	mouse_cursor_flag = 1;
 	mouse_cursor_flag = 1;
 }
 }
 
 
@@ -307,10 +323,12 @@ static void mouse_move(int dx, int dy)
 		cur_y = (unsigned int)ny;
 		cur_y = (unsigned int)ny;
 	} else {
 	} else {
 		cursor_restore_under();
 		cursor_restore_under();
+		cursor_present();          /* erase the old position in VRAM */
 		cur_x = (unsigned int)nx;
 		cur_x = (unsigned int)nx;
 		cur_y = (unsigned int)ny;
 		cur_y = (unsigned int)ny;
 		cursor_save_under();
 		cursor_save_under();
 		mouse_draw_cursor(cur_x, cur_y);
 		mouse_draw_cursor(cur_x, cur_y);
+		cursor_present();          /* draw the new position in VRAM */
 	}
 	}
 }
 }
 
 
@@ -433,8 +451,15 @@ void mouse_redraw_cursor(void)
 {
 {
 	if (!mouse_cursor_flag || mouse_hw_cursor)
 	if (!mouse_cursor_flag || mouse_hw_cursor)
 		return;
 		return;
+	/* If the pointer moved while a WM frame owned the shadow, the saved
+	 * rectangle is still the old position and has been restored to the
+	 * background there.  Erase it in VRAM before drawing the new one, so
+	 * a moving cursor does not leave a trail behind a slow frame. */
+	if ((saved_x != cur_x || saved_y != cur_y) && saved_w && saved_h)
+		vga_fb_present_rect(saved_x, saved_y, saved_w, saved_h);
 	cursor_save_under();
 	cursor_save_under();
 	mouse_draw_cursor(cur_x, cur_y);
 	mouse_draw_cursor(cur_x, cur_y);
+	cursor_present();
 }
 }
 
 
 void mouse_cursor_erase(void)
 void mouse_cursor_erase(void)
@@ -442,6 +467,10 @@ void mouse_cursor_erase(void)
 	if (!mouse_cursor_flag || mouse_hw_cursor)
 	if (!mouse_cursor_flag || mouse_hw_cursor)
 		return;
 		return;
 	cursor_restore_under();
 	cursor_restore_under();
+	/* No direct present here: the caller is about to redraw the scene.
+	 * Keeping the old cursor on screen until that flush avoids a visible
+	 * blink at the start of every window frame.  The restore already
+	 * marked the rectangle dirty, so the flush erases it. */
 }
 }
 
 
 void mouse_set_auto_draw(int on)
 void mouse_set_auto_draw(int on)

+ 26 - 0
ngui.c

@@ -94,6 +94,7 @@ void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h)
 	ui->dirty = 1;
 	ui->dirty = 1;
 	ui->caret_on = 1;
 	ui->caret_on = 1;
 	ui->last_blink = u_ticks_ms();
 	ui->last_blink = u_ticks_ms();
+	ui->motion_ms = ui->last_blink;
 	ui->last_mouse_x = -1;
 	ui->last_mouse_x = -1;
 	ui->last_mouse_y = -1;
 	ui->last_mouse_y = -1;
 }
 }
@@ -225,9 +226,32 @@ int ngui_begin(struct ngui *ui)
 
 
 	if (ui->inside != ui->last_inside) {
 	if (ui->inside != ui->last_inside) {
 		want = 1;
 		want = 1;
+		ui->motion_pending = 0;
+		ui->motion_ms = now;
 	} else if (ui->inside &&
 	} else if (ui->inside &&
 		   (ui->mouse_x != ui->last_mouse_x ||
 		   (ui->mouse_x != ui->last_mouse_x ||
 		    ui->mouse_y != ui->last_mouse_y)) {
 		    ui->mouse_y != ui->last_mouse_y)) {
+		/* Hover feedback needs a repaint, but not one per mouse
+		 * report: while the pointer keeps moving, redraw at most every
+		 * 33 ms and remember that a final frame is still due.  A held
+		 * button is a drag, which stays at full rate. */
+		if (ui->buttons) {
+			ui->motion_ms = now;
+			ui->motion_pending = 0;
+			want = 1;
+		} else {
+			ui->motion_pending = 1;
+			if ((unsigned int)(now - ui->motion_ms) >=
+			    NGUI_MOTION_REDRAW_MS) {
+				ui->motion_ms = now;
+				ui->motion_pending = 0;
+				want = 1;
+			}
+		}
+	} else if (ui->motion_pending &&
+		   (unsigned int)(now - ui->motion_ms) >= NGUI_MOTION_REDRAW_MS) {
+		ui->motion_ms = now;
+		ui->motion_pending = 0;
 		want = 1;
 		want = 1;
 	}
 	}
 	if (ui->focused != ui->last_focused)
 	if (ui->focused != ui->last_focused)
@@ -239,6 +263,8 @@ int ngui_begin(struct ngui *ui)
 
 
 	if (ui->pressed || ui->released)
 	if (ui->pressed || ui->released)
 		want = 1;
 		want = 1;
+	if (want)
+		ui->motion_pending = 0;
 	return want;
 	return want;
 }
 }
 
 

+ 7 - 0
ngui.h

@@ -10,6 +10,7 @@
 #include "wm_abi.h"
 #include "wm_abi.h"
 
 
 #define NGUI_INPUT_MAX 128
 #define NGUI_INPUT_MAX 128
+#define NGUI_MOTION_REDRAW_MS 33
 
 
 #define NGUI_INPUT_CHANGED 1
 #define NGUI_INPUT_CHANGED 1
 #define NGUI_INPUT_SUBMIT  2
 #define NGUI_INPUT_SUBMIT  2
@@ -127,6 +128,12 @@ struct ngui {
 
 
 	int last_mouse_x, last_mouse_y;
 	int last_mouse_x, last_mouse_y;
 	int last_inside, last_focused;
 	int last_inside, last_focused;
+
+	/* Pointer motion is throttled: a redraw is needed for hover feedback,
+	 * but not for every 200 Hz mouse report.  A pending motion is flushed
+	 * once 33 ms have passed, so the final hover state still appears. */
+	unsigned int motion_ms;
+	int motion_pending;
 };
 };
 
 
 void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h);
 void ngui_init(struct ngui *ui, unsigned short *fb, int w, int h);

+ 88 - 0
vga.c

@@ -395,6 +395,8 @@ void vga_target_damage(unsigned int x, unsigned int y, unsigned int w,
 	mark_dirty_rect_xy(y, h, x, x + w);
 	mark_dirty_rect_xy(y, h, x, x + w);
 }
 }
 
 
+static unsigned int vga_pixel_bytes(void);
+
 /* 16bpp: RGB 5:6:5.  32bpp: BGRX byte order (standard VBE / QEMU). */
 /* 16bpp: RGB 5:6:5.  32bpp: BGRX byte order (standard VBE / QEMU). */
 static void target_put_pixel(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
 static void target_put_pixel(unsigned int x, unsigned int y, unsigned int r, unsigned int g, unsigned int b)
 {
 {
@@ -433,6 +435,38 @@ void vga_put_pixel_rgb(unsigned int x, unsigned int y, unsigned int r, unsigned
 	target_put_pixel(x, y, r, g, b);
 	target_put_pixel(x, y, r, g, b);
 }
 }
 
 
+/* Cursor pixels go into the screen shadow without a per-pixel damage mark:
+ * the cursor presents its whole 16x16 rectangle directly to VRAM. */
+void vga_fb_put_pixel(unsigned int x, unsigned int y,
+		      unsigned int r, unsigned int g, unsigned int b)
+{
+	unsigned char *p;
+	unsigned int pb, pitch;
+	unsigned char *fb;
+
+	vga_cache_boot_fb();
+	if (x >= vga_fb_w || y >= vga_fb_h)
+		return;
+	fb = vga_shadow_buf ? vga_shadow_buf : (unsigned char *)vga_fb_lfb;
+	if (!fb)
+		return;
+	pb = vga_pixel_bytes();
+	pitch = vga_shadow_buf ? vga_shadow_pitch : vga_fb_pitch_val;
+	p = fb + (unsigned long)y * pitch + x * pb;
+	if (pb == 2) {
+		unsigned int v = ((r >> 3) << 11) | ((g >> 2) << 5) | (b >> 3);
+
+		p[0] = (unsigned char)(v & 0xFF);
+		p[1] = (unsigned char)(v >> 8);
+	} else {
+		p[0] = (unsigned char)b;
+		p[1] = (unsigned char)g;
+		p[2] = (unsigned char)r;
+		if (pb == 4)
+			p[3] = 0xFF;
+	}
+}
+
 void vga_fill_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
 void vga_fill_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
 		unsigned int r, unsigned int g, unsigned int b)
 		unsigned int r, unsigned int g, unsigned int b)
 {
 {
@@ -520,6 +554,60 @@ mark:
 	}
 	}
 }
 }
 
 
+/* Present a rectangle out of band: the software cursor calls this on every
+ * move, so its pixels reach VRAM immediately instead of waiting in the damage
+ * list behind a full-window repaint.  The cursor path runs under the BKL, the
+ * same lock the damage flush is called under, so it cannot race a flush. */
+void vga_fb_present_rect(unsigned int x, unsigned int y,
+			 unsigned int w, unsigned int h)
+{
+	unsigned int pb, row_bytes, dwords, tail, row;
+	unsigned char *src, *dst;
+	int use_sse2;
+
+	vga_cache_boot_fb();
+	if (!vga_shadow_buf || !vga_fb_lfb || w == 0 || h == 0)
+		return;
+	if (x >= vga_fb_w || y >= vga_fb_h)
+		return;
+	if (x + w > vga_fb_w)
+		w = vga_fb_w - x;
+	if (y + h > vga_fb_h)
+		h = vga_fb_h - y;
+	pb = vga_pixel_bytes();
+	row_bytes = w * pb;
+	dwords = row_bytes / 4;
+	tail = row_bytes % 4;
+	use_sse2 = cpu_has_sse2();
+
+	for (row = 0; row < h; row++) {
+		src = vga_shadow_buf + (unsigned long)(y + row) * vga_shadow_pitch +
+		      x * pb;
+		dst = (unsigned char *)vga_fb_lfb +
+		      (unsigned long)(y + row) * vga_fb_pitch_val + x * pb;
+		if (lfb_swap_rb && pb == 4) {
+			const unsigned int *s32 = (const unsigned int *)src;
+			unsigned int *d32 = (unsigned int *)dst;
+			unsigned int k;
+
+			for (k = 0; k < dwords; k++)
+				d32[k] = swap_rb32(s32[k]);
+		} else if (use_sse2 && dwords) {
+			vga_fb_copy_row_nt(dst, src, dwords);
+		} else if (dwords) {
+			vga_fb_copy_row(dst, src, dwords);
+		}
+		if (tail) {
+			unsigned int k;
+
+			for (k = dwords * 4; k < row_bytes; k++)
+				dst[k] = src[k];
+		}
+	}
+	if (use_sse2)
+		__asm__ volatile ("sfence" ::: "memory");
+}
+
 void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
 void vga_clear_rgb(unsigned int r, unsigned int g, unsigned int b)
 {
 {
 	struct surface *t;
 	struct surface *t;

+ 11 - 0
vga.h

@@ -88,6 +88,17 @@ void vga_fb_read_rect(unsigned int x, unsigned int y, unsigned int w, unsigned i
 void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
 void vga_fb_write_rect(unsigned int x, unsigned int y, unsigned int w, unsigned int h,
 		const unsigned char *buf);
 		const unsigned char *buf);
 
 
+/* Copy one rectangle straight from the shadow to VRAM, outside the damage
+ * queue.  The software cursor uses this so it never waits behind a pending
+ * window repaint.  Callers run under the BKL, like the damage flush. */
+void vga_fb_present_rect(unsigned int x, unsigned int y,
+			 unsigned int w, unsigned int h);
+
+/* Write one pixel into the screen shadow without marking damage (the cursor
+ * presents its whole rectangle explicitly). */
+void vga_fb_put_pixel(unsigned int x, unsigned int y,
+		      unsigned int r, unsigned int g, unsigned int b);
+
 /* 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);