Преглед изворни кода

wm: preserve the frame and throttle live resize steps

A live resize replaced the front canvas and both buffers with freshly
zeroed allocations before the client acknowledged, so the content went
black on every drag step until the owner reflowed and repainted.

Add the window-origin delta (new minus old) to struct wm_size dx/dy and
have wm_resize_window() copy the overlapping part of the old front
canvas into the new one before freeing it.  Pixels therefore stay put on
screen: a left/top edge drag keeps the content in place and only the
newly exposed strip is black until SYS_WIN_ACK_RESIZE.  Popover
self-resizes pass a zero delta.

The ring-3 WM now commits an edge-drag step only after the edge moved
WM_RESIZE_MIN_STEP (8 px) or WM_RESIZE_COMMIT_MS (30 ms), and forces the
final geometry on button release, so a pointer-rate drag no longer
reallocates the canvases and wakes the client to reflow on every
motion.

Tested in QEMU with QMP input: pre-ack screenshots show the previous
terminal frame preserved and cropped (right edge) and anchored in place
(40 px left-edge drag, rightmost text and scrollbar unmoved), followed
by a reflowed frame; a smoke run of right/bottom/left resizes, maximize
and restore leaves the window at the expected geometry with no kernel
fault.
Johannes Zellner пре 1 дан
родитељ
комит
851ae2823d
8 измењених фајлова са 110 додато и 14 уклоњено
  1. 13 2
      AGENTS.md
  2. 10 1
      docs/graphics.html
  3. 5 0
      include/wm_abi.h
  4. 1 1
      kernel/sys_wm.c
  5. 48 4
      kernel/wm.c
  6. 2 1
      kernel/wm.h
  7. 28 4
      user/apps/wm/ucmd_wm.c
  8. 3 1
      user/ucmd_lib.h

+ 13 - 2
AGENTS.md

@@ -581,7 +581,11 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   put, clamped to the work area.  The topmost window's band wins over a
   window behind it.  While dragged or resized the WM owns the pointer
   (`SYS_WM_MOUSE_CAPTURE`), so canvas clients see no held button and cannot
-  start a scrollbar drag.  The kernel hit-test uses the same order.
+  start a scrollbar drag.  The kernel hit-test uses the same order.  A live
+  resize commits at most every `WM_RESIZE_COMMIT_MS` (30 ms) or once the edge
+  moved `WM_RESIZE_MIN_STEP` (8 px), and the release forces the final
+  geometry; the kernel carries the previous front canvas over so the content
+  does not blink black while the client reflows (see the canvas API bullet).
   The desktop background is `COL_BG` until a wallpaper is installed.  The
   image viewer pre-renders the current image to a screen-aspect RGB565
   buffer (at most 1440x810, cropped to cover) and writes one
@@ -714,7 +718,14 @@ it to `0x7E00`.  The kernel region itself only has one wall (`_end` below
   them to collapse to the pills or grow over an expanded section.  The kernel
   refuses a self-resize while the WM owns a frame, and the caller retries.  The WM
   composites the front canvas; presented frames carry monotonic seq
-  counters, so the WM can never lose one.  `content_gen` comes from a global
+  counters, so the WM can never lose one.  `SYS_WM_RESIZE` carries the
+  window-origin delta (new minus old) in `struct wm_size.dx/dy`;
+  `wm_resize_window()` copies the old front canvas into the new one anchored
+  by that delta before freeing it, so a left/top edge drag keeps the content
+  in place and the newly exposed strip is the only black area until the owner
+  acknowledges (`SYS_WIN_ACK_RESIZE`) and presents its reflowed frame.  The
+  client's back buffer stays zeroed on purpose: it paints and presents a full
+  frame after the ack.  `content_gen` comes from a global
   counter and is unique per window creation, so a close followed by a new
   window in the same slot forces a remap instead of reusing a stale front
   canvas mapping.  The ring-3 WM refreshes `SYS_WM_INFO`/`SYS_WM_WIN_MAP`

+ 10 - 1
docs/graphics.html

@@ -402,7 +402,16 @@
 			<code>SYS_WM_RESIZE</code> (WM side) and
 			<code>SYS_WIN_ACK_RESIZE</code> (client side) swap in a resized
 			canvas; the content size is bounded by the framebuffer, so a
-			window can grow to the edge of the screen.
+			window can grow to the edge of the screen. <code>struct
+			wm_size</code> carries the window-origin delta (new minus old) in
+			<code>dx</code>/<code>dy</code>: the kernel copies the old front
+			canvas into the new one anchored by that delta before freeing it,
+			so a left/top edge drag keeps the content in place and only the
+			newly exposed strip starts black until the owner acknowledges and
+			repaints. A live resize commits at most every 30 ms
+			(<code>WM_RESIZE_COMMIT_MS</code>) or after the edge moved 8 px
+			(<code>WM_RESIZE_MIN_STEP</code>), and the button release forces
+			the final geometry.
 		</p>
 		<p>
 			Double-clicking a title bar maximizes the window to the work area

+ 5 - 0
include/wm_abi.h

@@ -138,6 +138,11 @@ struct wm_geom {
 
 struct wm_size {
 	unsigned int w, h;
+	/* New window origin minus the old one, in screen pixels.  The kernel
+	 * carries the previous front canvas over anchored by this delta, so a
+	 * left/top edge drag keeps the content in place instead of sliding it
+	 * or blanking it until the client repaints. */
+	int dx, dy;
 };
 
 /* Generic canvas-window client API (gfx commands own their own window). */

+ 1 - 1
kernel/sys_wm.c

@@ -282,7 +282,7 @@ int sys_wm_resize(unsigned int index, const struct wm_size *in)
 	if (!user_ok(in, sizeof(s), 0))
 		return -1;
 	copy_from_user(&s, in, sizeof(s));
-	return wm_server_resize(index, s.w, s.h);
+	return wm_server_resize(index, s.w, s.h, s.dx, s.dy);
 }
 
 int sys_win_key_wait(unsigned int timeout_ms)

+ 48 - 4
kernel/wm.c

@@ -535,7 +535,49 @@ int wm_server_win_scroll(struct win_scroll *out)
 	return (out->dx || out->dy) ? 1 : 0;
 }
 
-static int wm_resize_window(unsigned int index, unsigned int cw, unsigned int ch)
+/* Keep the previous frame visible across a resize: copy the overlap of the old
+ * front canvas into the new one, shifted by the window's origin delta
+ * (dx,dy = new origin - old origin), so the pixels stay put on screen.  The
+ * newly exposed area starts black, and the client's back buffer stays blank on
+ * purpose: it presents a full frame right after SYS_WIN_ACK_RESIZE.  Without
+ * this the WM composited a zeroed canvas and the content blinked black on
+ * every resize step until the owner repainted. */
+static void wm_carry_canvas(const struct gfx_canvas *old,
+			    const struct gfx_canvas *fresh, int dx, int dy)
+{
+	int x0 = 0, y0 = 0, x1, y1, y;
+
+	if (!old->px || !fresh->px)
+		return;
+	/* New pixel (x,y) shows old pixel (x+dx, y+dy). */
+	x1 = (int)fresh->w;
+	if (x0 < -dx)
+		x0 = -dx;
+	if (x1 > (int)old->w - dx)
+		x1 = (int)old->w - dx;
+	y1 = (int)fresh->h;
+	if (y0 < -dy)
+		y0 = -dy;
+	if (y1 > (int)old->h - dy)
+		y1 = (int)old->h - dy;
+	if (x0 >= x1 || y0 >= y1)
+		return;
+	for (y = y0; y < y1; y++) {
+		const unsigned short *s = old->px +
+			(unsigned long)(y + dy) * old->w + (x0 + dx);
+		unsigned short *d = fresh->px +
+			(unsigned long)y * fresh->w + x0;
+		unsigned int n = (unsigned int)(x1 - x0);
+
+		__asm__ volatile ("cld; rep movsw"
+				  : "+D"(d), "+c"(n), "+S"(s)
+				  :
+				  : "memory");
+	}
+}
+
+static int wm_resize_window(unsigned int index, unsigned int cw,
+			    unsigned int ch, int dx, int dy)
 {
 	struct window *w;
 	struct gfx_canvas nf;
@@ -566,6 +608,7 @@ static int wm_resize_window(unsigned int index, unsigned int cw, unsigned int ch
 	for (i = 0; i < cw * ch; i++)
 		nb[i] = 0;
 
+	wm_carry_canvas(&w->canvas, &nf, dx, dy);
 	wm_server_unmap_index(index);
 	gfx_free(&w->canvas);
 	if (w->pending_back) {
@@ -589,11 +632,12 @@ static int wm_resize_window(unsigned int index, unsigned int cw, unsigned int ch
 	return 0;
 }
 
-int wm_server_resize(unsigned int index, unsigned int cw, unsigned int ch)
+int wm_server_resize(unsigned int index, unsigned int cw, unsigned int ch,
+		     int dx, int dy)
 {
 	if (!wm_server_is_wm())
 		return -1;
-	return wm_resize_window(index, cw, ch);
+	return wm_resize_window(index, cw, ch, dx, dy);
 }
 
 /* A canvas app resizes its own popover when it expands or collapses a
@@ -605,7 +649,7 @@ int wm_server_win_resize_self(unsigned int cw, unsigned int ch)
 
 	if (idx < 0 || wm_frame_active)
 		return -1;
-	return wm_resize_window((unsigned int)idx, cw, ch);
+	return wm_resize_window((unsigned int)idx, cw, ch, 0, 0);
 }
 
 int wm_server_win_set_cursor(int shape)

+ 2 - 1
kernel/wm.h

@@ -82,7 +82,8 @@ int  wm_server_win_present(const struct wm_win_present *in);
 int  wm_server_win_close(void);
 int  wm_server_win_info(struct win_info *out);
 int  wm_server_win_ack_resize(void);
-int  wm_server_resize(unsigned int index, unsigned int cw, unsigned int ch);
+int  wm_server_resize(unsigned int index, unsigned int cw, unsigned int ch,
+		      int dx, int dy);
 int  wm_server_key_poll(void);
 int  wm_server_key_wait(unsigned int timeout_ms);
 int  wm_server_win_pointer(struct win_pointer *out);

+ 28 - 4
user/apps/wm/ucmd_wm.c

@@ -80,6 +80,13 @@ static int resize_id = -1;
 static int resize_edges;           /* WM_EDGE_* bits captured at the press */
 static int resize_ox, resize_oy;
 static unsigned int resize_mx, resize_my, resize_ow, resize_oh;
+/* A live resize step reallocates both canvases and wakes the client to
+ * reflow, so pointer-rate commits starve it (and make the content blink).
+ * Commit when the edge moved at least WM_RESIZE_MIN_STEP pixels, else at
+ * most every WM_RESIZE_COMMIT_MS; the button release forces the final one. */
+#define WM_RESIZE_COMMIT_MS 30
+#define WM_RESIZE_MIN_STEP  8
+static unsigned int resize_last_ms;
 static unsigned int last_title_ms;
 static int last_title_id = -1, last_title_x, last_title_y;
 static unsigned int prev_buttons;
@@ -1518,7 +1525,8 @@ static int set_window_geometry(struct uwin *u, unsigned int idx,
 
 	if ((int)nw != (int)ow || (int)nh != (int)oh) {
 		if (u_wm_resize(idx, nw - 2 * WM_BORDER_W,
-				nh - WM_TITLE_H - 2 * WM_BORDER_W) != 0)
+				nh - WM_TITLE_H - 2 * WM_BORDER_W,
+				nx - ox, ny - oy) != 0)
 			return -1;
 	}
 	if (nx != ox || ny != oy)
@@ -2845,9 +2853,11 @@ static void dock_capture_update(unsigned int mx, unsigned int my,
 }
 
 /* Apply one edge-drag step.  The grabbed edges follow the pointer while the
- * opposite edges stay put; the work area clamps the active edges. */
+ * opposite edges stay put; the work area clamps the active edges.  `force`
+ * commits the geometry even when the throttle would still defer it (button
+ * release). */
 static void resize_window_drag(struct uwin *u, unsigned int idx,
-			       unsigned int mx, unsigned int my)
+			       unsigned int mx, unsigned int my, int force)
 {
 	int dx = (int)mx - (int)resize_mx;
 	int dy = (int)my - (int)resize_my;
@@ -2856,6 +2866,7 @@ static void resize_window_drag(struct uwin *u, unsigned int idx,
 	int top = resize_oy;
 	int bottom = resize_oy + (int)resize_oh;
 	int nx, ny, nw, nh;
+	unsigned int now;
 
 	if (resize_edges & WM_EDGE_LEFT) {
 		left = resize_ox + dx;
@@ -2904,6 +2915,13 @@ static void resize_window_drag(struct uwin *u, unsigned int idx,
 	if (nx == u->info.x && ny == u->info.y &&
 	    nw == (int)u->info.w && nh == (int)u->info.h)
 		return;
+	now = u_ticks_ms();
+	if (!force &&
+	    (unsigned int)(now - resize_last_ms) < WM_RESIZE_COMMIT_MS &&
+	    iabs(nw - (int)u->info.w) < WM_RESIZE_MIN_STEP &&
+	    iabs(nh - (int)u->info.h) < WM_RESIZE_MIN_STEP)
+		return;
+	resize_last_ms = now;
 	if (set_window_geometry(u, idx, nx, ny, (unsigned int)nw,
 				(unsigned int)nh) == 0)
 		u->state = WIN_NORMAL;
@@ -2923,6 +2941,7 @@ static void begin_resize(unsigned int idx, int edges, unsigned int mx,
 	resize_oy = u->info.y;
 	resize_ow = u->info.w;
 	resize_oh = u->info.h;
+	resize_last_ms = u_ticks_ms() - WM_RESIZE_COMMIT_MS;
 	u_wm_mouse_capture(1);
 }
 
@@ -3080,7 +3099,7 @@ static void handle_mouse(struct wm_status *st)
 		struct uwin *u = &wins[resize_id];
 
 		if (u->info.used)
-			resize_window_drag(u, (unsigned int)resize_id, mx, my);
+			resize_window_drag(u, (unsigned int)resize_id, mx, my, 0);
 		else {
 			resize_id = -1;
 			u_wm_mouse_capture(0);
@@ -3159,6 +3178,11 @@ static void handle_mouse(struct wm_status *st)
 			u_wm_mouse_capture(0);
 		}
 	} else if (!down && was) {
+		/* A throttled resize may still be behind the last pointer
+		 * position; commit the final geometry before dropping it. */
+		if (resize_id >= 0 && wins[resize_id].info.used)
+			resize_window_drag(&wins[resize_id],
+					   (unsigned int)resize_id, mx, my, 1);
 		/* Dropping a window with the pointer on the left/right screen
 		 * edge snaps it to that half; on the top edge it maximizes. */
 		if (drag_id >= 0 && drag_moved && wins[drag_id].info.used &&

+ 3 - 1
user/ucmd_lib.h

@@ -411,12 +411,14 @@ static inline int u_win_scroll(struct win_scroll *out)
 }
 
 static inline int u_wm_resize(unsigned int index, unsigned int cw,
-			      unsigned int ch)
+			      unsigned int ch, int dx, int dy)
 {
 	struct wm_size s;
 
 	s.w = cw;
 	s.h = ch;
+	s.dx = dx;
+	s.dy = dy;
 	return u_syscall2(SYS_WM_RESIZE, (int)index, (int)&s);
 }