From de7af293b8438c54e6680a3d03ec768ffee34987 Mon Sep 17 00:00:00 2001 From: osy Date: Tue, 17 Nov 2026 08:55:50 -0800 Subject: [PATCH] x11-randr: create new modes with the pixel clock in Hz When no mode of the requested size exists, the agent creates one with set_reduced_cvt_mode(). That function comes from the X server's xf86cvt.c, where a mode's clock is in kHz, and it still computes dotClock in kHz. XRRModeInfo.dotClock is in Hz, so every mode the agent created ran 1000 times too slowly: 60 Hz became 0.06 Hz. For example, xrandr shows "1840x1010-0 0.124MHz ... clock 0.06Hz". A compositor that paces its repaints to the output's refresh rate then updates the screen about once a second. KWin 5.27 on X11 does this on Ubuntu 24.04 with virtio-gpu after a dynamic-resolution resize, while the X server and applications keep running normally. Keep the computation and its rounding to the CVT clock step in kHz, then convert the result to Hz: - Cap the clock at what the protocol's 32-bit field holds, so a huge mode on a 32-bit guest gets a lower refresh rate instead of wrapping around to a meaningless one. - An older agent may already have left such modes in the running X server, and find_mode_by_size() would hand them back. Replace a mode of the requested size whose clock is below 1 MHz, which no real mode uses, instead of using it again. Tested in an Ubuntu 24.04 aarch64 guest (KWin 5.27.11 X11, virgl) on UTM 5.0.9 with dynamic resolution. Each figure is the distinct frames per second the host window showed while a window moved: - Before, the agent created 3840x957-0 and 3840x961-0 at 0.06 Hz: 0.5 fps, with frames held for up to 3.4 s. - After, in the same X server, the agent replaced the stale 3840x961-0 (0.237 MHz) with 237.000 MHz (59.91 Hz). It created 1920x961-0 at 59.91 Hz and 2560x961-0 at 59.94 Hz: 47.2 fps on 3840x961-0. Resolves utmapp/UTM#8197 Assisted-by: Claude:claude-opus-5-5 --- src/vdagent/x11-randr.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/vdagent/x11-randr.c b/src/vdagent/x11-randr.c index 037aded..600aa03 100644 --- a/src/vdagent/x11-randr.c +++ b/src/vdagent/x11-randr.c @@ -249,6 +249,9 @@ static void delete_mode(struct vdagent_x11 *x11, int output_index, update_randr_res(x11, 0); } +/* Below any real mode's pixel clock (Hz): 320x200 at 60 Hz needs ~5 MHz */ +#define MIN_MODE_DOT_CLOCK 1000000 + static void set_reduced_cvt_mode(XRRModeInfo *mode, int width, int height) { /* Code taken from hw/xfree86/modes/xf86cvt.c @@ -321,6 +324,12 @@ static void set_reduced_cvt_mode(XRRModeInfo *mode, int width, int height) /* 15/13. Find pixel clock frequency (kHz for xf86) */ mode->dotClock = mode->hTotal * 1000.0 / HPeriod; mode->dotClock -= mode->dotClock % CVT_CLOCK_STEP; + /* XRRModeInfo.dotClock is in Hz, not in kHz like xf86 modes. The + * protocol carries it in 32 bits: cap huge modes there, at a lower + * refresh rate. */ + if (mode->dotClock > UINT_MAX / 1000) + mode->dotClock = UINT_MAX / 1000 / CVT_CLOCK_STEP * CVT_CLOCK_STEP; + mode->dotClock *= 1000; } @@ -377,6 +386,12 @@ static int xrandr_add_and_set(struct vdagent_x11 *x11, int output_index, int x, } xid = x11->randr.res->outputs[output_index]; mode = find_mode_by_size(x11, output_index, width, height); + if (mode && mode->dotClock < MIN_MODE_DOT_CLOCK) { + /* left by an older agent that set the clock in kHz (0.06 Hz): + * replace it instead of using it again */ + delete_mode(x11, output_index, width, height); + mode = NULL; + } if (!mode) { mode = create_new_mode(x11, output_index, width, height); }