wayland: Unify integer and fractional output scaling Previously, scale values used by the displays and surfaces were always integers, with fractional scale values only being calculated when the backbuffer and viewport sizes were being determined. Now, if xdg-output is available, the fractional scale of output displays is calculated when the displays are enumerated and the true scale values of the output devices are used whenever possible. This unifies the integer and fractional scaling systems, allows for the use of more accurate scale values that minimize overdraw when windows straddle multiple outputs, and lays the groundwork for the pending Wayland scaling protocols that will report the preferred scale values per-surface instead of per-output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
diff --git a/src/video/wayland/SDL_waylandvideo.c b/src/video/wayland/SDL_waylandvideo.c
index 2992d52..6688f13 100644
--- a/src/video/wayland/SDL_waylandvideo.c
+++ b/src/video/wayland/SDL_waylandvideo.c
@@ -578,8 +578,9 @@ display_handle_done(void *data,
SDL_zero(desktop_mode);
desktop_mode.format = SDL_PIXELFORMAT_RGB888;
- /* Scale the desktop coordinates, if xdg-output isn't present */
- if (!driverdata->has_logical_size) {
+ if (driverdata->has_logical_size) { /* If xdg-output is present, calculate the true scale of the desktop */
+ driverdata->scale_factor = (float)native_mode.w / (float)driverdata->width;
+ } else { /* Scale the desktop coordinates, if xdg-output isn't present */
driverdata->width /= driverdata->scale_factor;
driverdata->height /= driverdata->scale_factor;
}
diff --git a/src/video/wayland/SDL_waylandwindow.c b/src/video/wayland/SDL_waylandwindow.c
index ef70f28..027834f 100644
--- a/src/video/wayland/SDL_waylandwindow.c
+++ b/src/video/wayland/SDL_waylandwindow.c
@@ -44,6 +44,15 @@
#include <libdecor.h>
#endif
+SDL_FORCE_INLINE SDL_bool
+FloatEqual(float a, float b)
+{
+ const float diff = SDL_fabsf(a - b);
+ const float largest = SDL_max(SDL_fabsf(a), SDL_fabsf(b));
+
+ return diff <= largest * SDL_FLT_EPSILON;
+}
+
static void
GetFullScreenDimensions(SDL_Window *window, int *width, int *height, int *drawable_width, int *drawable_height)
{
@@ -104,17 +113,10 @@ GetFullScreenDimensions(SDL_Window *window, int *width, int *height, int *drawab
}
SDL_FORCE_INLINE SDL_bool
-DesktopIsFractionalScaled(SDL_Window *window)
+SurfaceScaleIsFractional(SDL_Window *window)
{
- SDL_WindowData *data = window->driverdata;
- SDL_WaylandOutputData *output = (SDL_WaylandOutputData *)SDL_GetDisplayForWindow(window)->driverdata;
-
- if ((output->native_width != (int)(output->width * data->scale_factor) ||
- output->native_height != (int)(output->height * data->scale_factor))) {
- return SDL_TRUE;
- }
-
- return SDL_FALSE;
+ SDL_WindowData *data = window->driverdata;
+ return !FloatEqual(SDL_roundf(data->scale_factor), data->scale_factor);
}
SDL_FORCE_INLINE SDL_bool
@@ -143,7 +145,7 @@ NeedViewport(SDL_Window *window)
if (fs_width != output->width || fs_height != output->height) {
return SDL_TRUE;
}
- } else if (DesktopIsFractionalScaled(window) && (window->flags & SDL_WINDOW_ALLOW_HIGHDPI)) {
+ } else if (SurfaceScaleIsFractional(window) && (window->flags & SDL_WINDOW_ALLOW_HIGHDPI)) {
return SDL_TRUE;
}
}
@@ -169,22 +171,26 @@ GetWindowHeight(SDL_Window *window)
static void
GetBufferSize(SDL_Window *window, int *width, int *height)
{
- SDL_WindowData *data = window->driverdata;
- SDL_WaylandOutputData *output = (SDL_WaylandOutputData *)SDL_GetDisplayForWindow(window)->driverdata;
- int buf_width;
- int buf_height;
-
+ SDL_WindowData *data = window->driverdata;
+ int buf_width;
+ int buf_height;
if (FullscreenModeEmulation(window)) {
GetFullScreenDimensions(window, NULL, NULL, &buf_width, &buf_height);
} else if (NeedViewport(window)) {
- const double frac_scale = (double)output->native_width / (double)output->width;
+ buf_width = (int)SDL_ceil(window->w * data->scale_factor);
+ buf_height = (int)SDL_ceil(window->h * data->scale_factor);
+ } else {
+ /*
+ * Integer scaled windowed or fullscreen with no viewport
+ *
+ * Round the scale factor up in the unlikely scenario of a compositor
+ * that supports fractional scaling, but not viewports.
+ */
+ int scale_factor = (int)SDL_ceilf(data->scale_factor);
- buf_width = (int)SDL_ceil(window->w * frac_scale);
- buf_height = (int)SDL_ceil(window->h * frac_scale);
- } else { /* Windowed or fullscreen with no viewport */
- buf_width = window->w * data->scale_factor;
- buf_height = window->h * data->scale_factor;
+ buf_width = window->w * scale_factor;
+ buf_height = window->h * scale_factor;
}
if (width) {
@@ -230,7 +236,7 @@ ConfigureWindowGeometry(SDL_Window *window)
SDL_WaylandOutputData *output = (SDL_WaylandOutputData *)SDL_GetDisplayForWindow(window)->driverdata;
struct wl_region *region;
- /* Set the drawable backbuffer size */
+ /* Set the drawable backbuffer size. */
GetBufferSize(window, &data->drawable_width, &data->drawable_height);
if (data->egl_window) {
@@ -269,7 +275,9 @@ ConfigureWindowGeometry(SDL_Window *window)
SetDrawSurfaceViewport(window, data->drawable_width, data->drawable_height, window->w, window->h);
} else {
UnsetDrawSurfaceViewport(window);
- wl_surface_set_buffer_scale(data->surface, (int32_t)data->scale_factor);
+
+ /* Round to the next integer in case of a fractional value. */
+ wl_surface_set_buffer_scale(data->surface, (int32_t)SDL_ceilf(data->scale_factor));
}
SDL_zero(data->viewport_rect);
@@ -507,7 +515,7 @@ handle_configure_xdg_toplevel(void *data,
}
/* This part is good though. */
- if ((window->flags & SDL_WINDOW_ALLOW_HIGHDPI) && wind->scale_factor != driverdata->scale_factor) {
+ if ((window->flags & SDL_WINDOW_ALLOW_HIGHDPI) && !FloatEqual(wind->scale_factor, driverdata->scale_factor)) {
wind->scale_factor = driverdata->scale_factor;
wind->needs_resize_event = SDL_TRUE;
}
@@ -586,7 +594,7 @@ handle_configure_xdg_toplevel(void *data,
}
/* This part is good though. */
- if ((window->flags & SDL_WINDOW_ALLOW_HIGHDPI) && wind->scale_factor != driverdata->scale_factor) {
+ if ((window->flags & SDL_WINDOW_ALLOW_HIGHDPI) && !FloatEqual(wind->scale_factor, driverdata->scale_factor)) {
wind->scale_factor = driverdata->scale_factor;
wind->needs_resize_event = SDL_TRUE;
}
@@ -896,13 +904,11 @@ update_scale_factor(SDL_WindowData *window)
new_factor = 0.0f;
for (i = 0; i < window->num_outputs; i++) {
SDL_WaylandOutputData* driverdata = window->outputs[i];
- if (driverdata->scale_factor > new_factor) {
- new_factor = driverdata->scale_factor;
- }
+ new_factor = SDL_max(new_factor, driverdata->scale_factor);
}
}
- if (new_factor != old_factor) {
+ if (!FloatEqual(new_factor, old_factor)) {
Wayland_HandleResize(window->sdlwindow, window->sdlwindow->w, window->sdlwindow->h, new_factor);
}
}
@@ -1800,9 +1806,7 @@ int Wayland_CreateWindow(_THIS, SDL_Window *window)
int i;
for (i=0; i < SDL_GetVideoDevice()->num_displays; i++) {
float scale = ((SDL_WaylandOutputData*)SDL_GetVideoDevice()->displays[i].driverdata)->scale_factor;
- if (scale > data->scale_factor) {
- data->scale_factor = scale;
- }
+ data->scale_factor = SDL_max(data->scale_factor, scale);
}
}
@@ -1841,8 +1845,8 @@ int Wayland_CreateWindow(_THIS, SDL_Window *window)
}
#endif /* SDL_VIDEO_DRIVER_WAYLAND_QT_TOUCH */
- data->drawable_width = window->w * data->scale_factor;
- data->drawable_height = window->h * data->scale_factor;
+ data->drawable_width = SDL_ceilf(window->w * data->scale_factor);
+ data->drawable_height = SDL_ceilf(window->h * data->scale_factor);
if (window->flags & SDL_WINDOW_OPENGL) {
data->egl_window = WAYLAND_wl_egl_window_create(data->surface, data->drawable_width, data->drawable_height);
@@ -1906,7 +1910,7 @@ Wayland_HandleResize(SDL_Window *window, int width, int height, float scale)
SDL_WindowData *data = (SDL_WindowData *) window->driverdata;
SDL_VideoData *viddata = data->waylandData;
- if (data->needs_resize_event || window->w != width || window->h != height || data->scale_factor != scale) {
+ if (data->needs_resize_event || window->w != width || window->h != height || !FloatEqual(data->scale_factor, scale)) {
/* We may have already updated window w/h (or only adjusted scale factor),
* so we must override the deduplication logic in the video core */
window->w = 0;
@@ -1914,6 +1918,7 @@ Wayland_HandleResize(SDL_Window *window, int width, int height, float scale)
SDL_SendWindowEvent(window, SDL_WINDOWEVENT_RESIZED, width, height);
window->w = width;
window->h = height;
+ data->scale_factor = scale;
data->needs_resize_event = SDL_FALSE;
}