[client] wayland: improve fractional scaling

avoids the 24.8 quantisation without introducing floating-point
equality/rounding behaviour throughout the Wayland state.

Closes #1257
This commit is contained in:
Geoffrey McRae
2026-05-31 13:15:42 +10:00
parent dba2d23eb2
commit 01b8724cfe
6 changed files with 103 additions and 21 deletions

View File

@@ -183,7 +183,7 @@ void waylandCursorFree(void)
void waylandCursorScaleChange(void)
{
int newScale = ceil(wl_fixed_to_double(wlWm.scale));
int newScale = waylandScaleCeil(wlWm.scale);
if (newScale == wlWm.cursorScale)
return;

View File

@@ -92,8 +92,8 @@ void waylandEGLSwapBuffers(EGLDisplay display, EGLSurface surface, const struct
int width, height;
wlWm.desktop->getSize(&width, &height);
wl_egl_window_resize(wlWm.eglWindow, wl_fixed_to_int(width * wlWm.scale),
wl_fixed_to_int(height * wlWm.scale), 0, 0);
wl_egl_window_resize(wlWm.eglWindow, waylandScaleMulInt(wlWm.scale, width),
waylandScaleMulInt(wlWm.scale, height), 0, 0);
if (width == 0 || height == 0)
skipResize = true;
@@ -123,7 +123,7 @@ void waylandEGLSwapBuffers(EGLDisplay display, EGLSurface surface, const struct
wp_viewport_destroy(wlWm.viewport);
wlWm.viewport = NULL;
}
wl_surface_set_buffer_scale(wlWm.surface, wl_fixed_to_int(wlWm.scale));
wl_surface_set_buffer_scale(wlWm.surface, waylandScaleFloor(wlWm.scale));
}
struct wl_region * region = wl_compositor_create_region(wlWm.compositor);
@@ -131,7 +131,7 @@ void waylandEGLSwapBuffers(EGLDisplay display, EGLSurface surface, const struct
wl_surface_set_opaque_region(wlWm.surface, region);
wl_region_destroy(region);
app_handleResizeEvent(width, height, wl_fixed_to_double(wlWm.scale),
app_handleResizeEvent(width, height, waylandScaleToDouble(wlWm.scale),
(struct Border) {0, 0, 0, 0});
app_invalidateWindow(true);
waylandStopWaitFrame();

View File

@@ -29,17 +29,17 @@
static void outputUpdateScale(struct WaylandOutput * node)
{
wl_fixed_t original = node->scale;
struct WaylandScale original = node->scale;
if (!wlWm.useFractionalScale || !wlWm.viewporter || !node->logicalWidth)
node->scale = wl_fixed_from_int(node->scaleInt);
node->scale = waylandScaleFromInt(node->scaleInt);
else
{
int32_t modeWidth = node->modeRotate ? node->modeHeight : node->modeWidth;
node->scale = wl_fixed_from_double(1.0 * modeWidth / node->logicalWidth);
node->scale = waylandScaleFromRatio(modeWidth, node->logicalWidth);
}
if (original != node->scale)
if (!waylandScaleEqual(original, node->scale))
waylandWindowUpdateScale();
}
@@ -167,7 +167,7 @@ void waylandOutputBind(uint32_t name, uint32_t version)
}
node->name = name;
node->scale = 0;
node->scale = waylandScaleFromInt(0);
node->version = version;
node->output = wl_registry_bind(wlWm.registry, name,
&wl_output_interface, version >= 3 ? 3 : 2);
@@ -209,12 +209,12 @@ void waylandOutputTryUnbind(uint32_t name)
}
}
wl_fixed_t waylandOutputGetScale(struct wl_output * output)
struct WaylandScale waylandOutputGetScale(struct wl_output * output)
{
struct WaylandOutput * node;
wl_list_for_each(node, &wlWm.outputs, link)
if (node->output == output)
return node->scale;
return 0;
return waylandScaleFromInt(0);
}

View File

@@ -0,0 +1,80 @@
/**
* Looking Glass
* Copyright © 2017-2026 The Looking Glass Authors
* https://looking-glass.io
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the Free
* Software Foundation; either version 2 of the License, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc., 59
* Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <stdint.h>
struct WaylandScale
{
int32_t num;
int32_t den;
};
static inline struct WaylandScale waylandScaleFromInt(int32_t scale)
{
return (struct WaylandScale) { scale, 1 };
}
static inline struct WaylandScale waylandScaleFromRatio(int32_t num, int32_t den)
{
return (struct WaylandScale) { num, den };
}
static inline bool waylandScaleValid(struct WaylandScale scale)
{
return scale.num > 0 && scale.den > 0;
}
static inline bool waylandScaleEqual(struct WaylandScale a, struct WaylandScale b)
{
return (int64_t)a.num * b.den == (int64_t)b.num * a.den;
}
static inline int waylandScaleCmp(struct WaylandScale a, struct WaylandScale b)
{
int64_t lhs = (int64_t)a.num * b.den;
int64_t rhs = (int64_t)b.num * a.den;
return (lhs > rhs) - (lhs < rhs);
}
static inline bool waylandScaleIsFractional(struct WaylandScale scale)
{
return waylandScaleValid(scale) && scale.num % scale.den != 0;
}
static inline int waylandScaleFloor(struct WaylandScale scale)
{
return scale.num / scale.den;
}
static inline int waylandScaleCeil(struct WaylandScale scale)
{
return (scale.num + scale.den - 1) / scale.den;
}
static inline int waylandScaleMulInt(struct WaylandScale scale, int value)
{
return (int)(((int64_t)value * scale.num) / scale.den);
}
static inline double waylandScaleToDouble(struct WaylandScale scale)
{
return (double)scale.num / (double)scale.den;
}

View File

@@ -48,6 +48,8 @@
#include "wayland-xdg-output-unstable-v1-client-protocol.h"
#include "wayland-xdg-activation-v1-client-protocol.h"
#include "scale.h"
typedef void (*WaylandPollCallback)(uint32_t events, void * opaque);
struct WaylandPoll
@@ -62,7 +64,7 @@ struct WaylandPoll
struct WaylandOutput
{
uint32_t name;
wl_fixed_t scale;
struct WaylandScale scale;
int32_t scaleInt;
int32_t logicalWidth;
int32_t logicalHeight;
@@ -108,7 +110,7 @@ struct WaylandDSState
struct wl_shm * shm;
struct wl_compositor * compositor;
wl_fixed_t scale;
struct WaylandScale scale;
bool fractionalScale;
bool needsResize;
bool configured;
@@ -285,7 +287,7 @@ bool waylandOutputInit(void);
void waylandOutputFree(void);
void waylandOutputBind(uint32_t name, uint32_t version);
void waylandOutputTryUnbind(uint32_t name);
wl_fixed_t waylandOutputGetScale(struct wl_output * output);
struct WaylandScale waylandOutputGetScale(struct wl_output * output);
// poll module
bool waylandPollInit(void);

View File

@@ -33,20 +33,20 @@
void waylandWindowUpdateScale(void)
{
wl_fixed_t maxScale = 0;
struct WaylandScale maxScale = waylandScaleFromInt(0);
struct SurfaceOutput * node;
wl_list_for_each(node, &wlWm.surfaceOutputs, link)
{
wl_fixed_t scale = waylandOutputGetScale(node->output);
if (scale > maxScale)
struct WaylandScale scale = waylandOutputGetScale(node->output);
if (waylandScaleCmp(scale, maxScale) > 0)
maxScale = scale;
}
if (maxScale)
if (waylandScaleValid(maxScale))
{
wlWm.scale = maxScale;
wlWm.fractionalScale = wl_fixed_from_int(wl_fixed_to_int(maxScale)) != maxScale;
wlWm.fractionalScale = waylandScaleIsFractional(maxScale);
wlWm.needsResize = true;
waylandCursorScaleChange();
app_invalidateWindow(true);
@@ -87,7 +87,7 @@ static const struct wl_surface_listener wlSurfaceListener = {
bool waylandWindowInit(const char * title, const char * appId, bool fullscreen, bool maximize, bool borderless, bool resizable)
{
wlWm.scale = wl_fixed_from_int(1);
wlWm.scale = waylandScaleFromInt(1);
wlWm.frameEvent = lgCreateEvent(true, 0);
if (!wlWm.frameEvent)