Skip to contentSkip to conversation

ui/gtk: Add keep-aspect-ratio and scale option

PATCH In review Weifeng Liu 2 participants 4 messages Jun 1, 2025 – Jun 3, 2025

lore
Share & tools
lore
#0 lore raw

cover ui/gtk: Add keep-aspect-ratio and scale option

Add these options to give users more control over behaviors in gtk display backend:

- keep-aspect-ratio: when set to true, if the aspect ratio of host window differs from that of guest frame-buffer, padding will be added to the host window to preserve the aspect ratio of guest frame-buffer.

- scale: allow user to set a preferred scale factor, which would be helpful for users running on a hi-dpi desktop to achieve pixel to pixel display.

Cc BALATON Zoltan <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc "Kim, Dongwon" <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Dmitry Osipenko <[email protected]>

Best regards,
Weifeng

Weifeng Liu (2):
ui/gtk: Add keep-aspect-ratio option
ui/gtk: Add scale option

 include/ui/gtk.h |  2 ++
 qapi/ui.json     | 15 +++++++++----
 ui/gtk.c         | 58 ++++++++++++++++++++++++++++++++----------------
 3 files changed, 52 insertions(+), 23 deletions(-)
Signature · 1 line
2.49.0
#1 lore raw

PATCH 1/2 ui/gtk: Add keep-aspect-ratio option

When aspect ratio of host window and that of guest display are not aligned, we can either zoom the guest content to fill the whole host window or add padding to respect aspect ratio of the guest. Add an option keep-aspect-ratio to allow users to select their preferred behavior in this case.

---
 include/ui/gtk.h |  1 +
 qapi/ui.json     | 12 ++++++++----
 ui/gtk.c         | 12 ++++++++++--
 3 files changed, 19 insertions(+), 6 deletions(-)
include/ui/gtk.h +1 -0
index d3944046db..b7cfbf218e 100644
@@ -140,6 +140,7 @@ struct GtkDisplayState {
     GdkCursor *null_cursor;
     Notifier mouse_mode_notifier;
     gboolean free_scale;
+    gboolean keep_aspect_ratio;
 
     bool external_pause_update;
 
qapi/ui.json +8 -4
index 3d0c853c9a..4f7d994e26 100644
@@ -1335,13 +1335,17 @@
 # @show-menubar: Display the main window menubar.  Defaults to "on".
 #     (Since 8.0)
 #
+# @keep-aspect-ratio: Keep width/height aspect ratio of guest content when
+#     resizing host window.  Defaults to "on". (Since 10.1)
+#
 # Since: 2.12
 ##
 { 'struct'  : 'DisplayGTK',
-  'data'    : { '*grab-on-hover' : 'bool',
-                '*zoom-to-fit'   : 'bool',
-                '*show-tabs'     : 'bool',
-                '*show-menubar'  : 'bool'  } }
+  'data'    : { '*grab-on-hover'     : 'bool',
+                '*zoom-to-fit'       : 'bool',
+                '*show-tabs'         : 'bool',
+                '*show-menubar'      : 'bool',
+                '*keep-aspect-ratio' : 'bool'  } }
 
 ##
 # @DisplayEGLHeadless:
ui/gtk.c +10 -2
index 8c4a94c8f6..9104509ee1 100644
@@ -828,8 +828,12 @@ void gd_update_scale(VirtualConsole *vc, int ww, int wh, int fbw, int fbh)
 
         sx = (double)ww / fbw;
         sy = (double)wh / fbh;
-
-        vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
+        if (vc->s->keep_aspect_ratio) {
+            vc->gfx.scale_x = vc->gfx.scale_y = MIN(sx, sy);
+        } else {
+            vc->gfx.scale_x = sx;
+            vc->gfx.scale_y = sy;
+        }
     }
 }
 /**
@@ -2328,6 +2332,10 @@ static GSList *gd_vc_gfx_init(GtkDisplayState *s, VirtualConsole *vc,
         s->free_scale = true;
     }
 
+    s->keep_aspect_ratio = true;
+    if (s->opts->u.gtk.has_keep_aspect_ratio)
+        s->keep_aspect_ratio = s->opts->u.gtk.keep_aspect_ratio;
+
     for (i = 0; i < INPUT_EVENT_SLOTS_MAX; i++) {
         struct touch_slot *slot = &touch_slots[i];
         slot->tracking_id = -1;
Signature · 1 line
2.49.0
#2 lore raw

PATCH 2/2 ui/gtk: Add scale option

In reply to Weifeng Liu

Allow user to set a preferred scale (defaulting to 1) of the virtual display. Along with zoom-to-fix=false, this would be helpful for users running QEMU on hi-dpi host desktop to achieve pixel to pixel display -- e.g., if the scale factor of a user's host desktop is set to 200%, then they can set a 0.5 scale for the virtual display to avoid magnification that might cause blurriness.

---
 include/ui/gtk.h |  1 +
 qapi/ui.json     |  5 ++++-
 ui/gtk.c         | 46 +++++++++++++++++++++++++++++-----------------
 3 files changed, 34 insertions(+), 18 deletions(-)
include/ui/gtk.h +1 -0
index b7cfbf218e..3e6ce3cb48 100644
@@ -41,6 +41,7 @@ typedef struct VirtualGfxConsole {
     DisplaySurface *ds;
     pixman_image_t *convert;
     cairo_surface_t *surface;
+    double preferred_scale;
     double scale_x;
     double scale_y;
 #if defined(CONFIG_OPENGL)
qapi/ui.json +4 -1
index 4f7d994e26..023d055ef1 100644
@@ -1338,6 +1338,8 @@
 # @keep-aspect-ratio: Keep width/height aspect ratio of guest content when
 #     resizing host window.  Defaults to "on". (Since 10.1)
 #
+# @scale: Set preferred scale of the display.  Defaults to 1.0.  (Since 10.1)
+#
 # Since: 2.12
 ##
 { 'struct'  : 'DisplayGTK',
@@ -1345,7 +1347,8 @@
                 '*zoom-to-fit'       : 'bool',
                 '*show-tabs'         : 'bool',
                 '*show-menubar'      : 'bool',
-                '*keep-aspect-ratio' : 'bool'  } }
+                '*keep-aspect-ratio' : 'bool',
+                '*scale'             : 'number'  } }
 
 ##
 # @DisplayEGLHeadless:
ui/gtk.c +29 -17

88 lines not on this page · raw message

Signature · 1 line
2.49.0
Reviewed-by Tested-by on the v1 series
#3 lore raw
In reply to Weifeng Liu
Quoted 11 lines · Weifeng Liu

Add these options to give users more control over behaviors in gtk display backend:

- keep-aspect-ratio: when set to true, if the aspect ratio of host window differs from that of guest frame-buffer, padding will be added to the host window to preserve the aspect ratio of guest frame-buffer.

- scale: allow user to set a preferred scale factor, which would be helpful for users running on a hi-dpi desktop to achieve pixel to pixel display.

Quoted 8 lines
Cc BALATON Zoltan <[email protected]>
Cc Gerd Hoffmann <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc Marc-André Lureau <[email protected]>
Cc "Kim, Dongwon" <[email protected]>
Cc Alex Bennée <[email protected]>
Cc Dmitry Osipenko <[email protected]>
Quoted 16 lines

Best regards,
Weifeng

Weifeng Liu (2):
ui/gtk: Add keep-aspect-ratio option
ui/gtk: Add scale option

 include/ui/gtk.h |  2 ++
 qapi/ui.json     | 15 +++++++++----
 ui/gtk.c         | 58 ++++++++++++++++++++++++++++++++----------------
 3 files changed, 52 insertions(+), 23 deletions(-)

--
2.49.0