From 4c283e4a3c95e09659bf747c7624d075f3110f33 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Thu, 8 Feb 2024 18:55:52 +0100 Subject: [PATCH] Wayland: Be more specific about scales We are about to introduce another scaling method (fractional-scale-v1), so it will become more important to be specific about what scales are used where and what their units are. This removes 'content scale' from window and monitor structs. A monitor (output) now has just a 'scale', which becomes a 'buffer scale' when applied to a window. A window now has a list of 'output scales' to select its buffer scale from. Content scales are calculated from the respective monitor or window when queried, even if the calculation right now is to just return the same value as before. --- src/wl_monitor.c | 16 ++++++------- src/wl_platform.h | 12 +++++----- src/wl_window.c | 57 ++++++++++++++++++++++++----------------------- 3 files changed, 43 insertions(+), 42 deletions(-) diff --git a/src/wl_monitor.c b/src/wl_monitor.c index 96527a48..56ea56d7 100644 --- a/src/wl_monitor.c +++ b/src/wl_monitor.c @@ -112,16 +112,16 @@ static void outputHandleScale(void* userData, { struct _GLFWmonitor* monitor = userData; - monitor->wl.contentScale = factor; + monitor->wl.scale = factor; for (_GLFWwindow* window = _glfw.windowListHead; window; window = window->next) { - for (int i = 0; i < window->wl.scaleCount; i++) + for (int i = 0; i < window->wl.outputScaleCount; i++) { - if (window->wl.scales[i].output == monitor->wl.output) + if (window->wl.outputScales[i].output == monitor->wl.output) { - window->wl.scales[i].factor = monitor->wl.contentScale; - _glfwUpdateContentScaleWayland(window); + window->wl.outputScales[i].factor = monitor->wl.scale; + _glfwUpdateBufferScaleFromOutputsWayland(window); break; } } @@ -176,7 +176,7 @@ void _glfwAddOutputWayland(uint32_t name, uint32_t version) // The actual name of this output will be set in the geometry handler _GLFWmonitor* monitor = _glfwAllocMonitor("", 0, 0); - monitor->wl.contentScale = 1; + monitor->wl.scale = 1; monitor->wl.output = output; monitor->wl.name = name; @@ -207,9 +207,9 @@ void _glfwGetMonitorContentScaleWayland(_GLFWmonitor* monitor, float* xscale, float* yscale) { if (xscale) - *xscale = (float) monitor->wl.contentScale; + *xscale = (float) monitor->wl.scale; if (yscale) - *yscale = (float) monitor->wl.contentScale; + *yscale = (float) monitor->wl.scale; } void _glfwGetMonitorWorkareaWayland(_GLFWmonitor* monitor, diff --git a/src/wl_platform.h b/src/wl_platform.h index 37adcfd1..23fd8c14 100644 --- a/src/wl_platform.h +++ b/src/wl_platform.h @@ -389,10 +389,10 @@ typedef struct _GLFWwindowWayland // We need to track the monitors the window spans on to calculate the // optimal scaling factor. - int contentScale; - _GLFWscaleWayland* scales; - int scaleCount; - int scaleSize; + int bufferScale; + _GLFWscaleWayland* outputScales; + int outputScaleCount; + int outputScaleSize; struct zwp_relative_pointer_v1* relativePointer; struct zwp_locked_pointer_v1* lockedPointer; @@ -586,7 +586,7 @@ typedef struct _GLFWmonitorWayland int x; int y; - int contentScale; + int scale; } _GLFWmonitorWayland; // Wayland-specific per-cursor data @@ -677,7 +677,7 @@ GLFWbool _glfwGetGammaRampWayland(_GLFWmonitor* monitor, GLFWgammaramp* ramp); void _glfwSetGammaRampWayland(_GLFWmonitor* monitor, const GLFWgammaramp* ramp); void _glfwAddOutputWayland(uint32_t name, uint32_t version); -void _glfwUpdateContentScaleWayland(_GLFWwindow* window); +void _glfwUpdateBufferScaleFromOutputsWayland(_GLFWwindow* window); void _glfwAddSeatListenerWayland(struct wl_seat* seat); void _glfwAddDataDeviceListenerWayland(struct wl_data_device* device); diff --git a/src/wl_window.c b/src/wl_window.c index 995c17a6..e5699ab3 100644 --- a/src/wl_window.c +++ b/src/wl_window.c @@ -312,7 +312,7 @@ static void setContentAreaOpaque(_GLFWwindow* window) static void resizeFramebuffer(_GLFWwindow* window) { - int scale = window->wl.contentScale; + int scale = window->wl.bufferScale; int scaledWidth = window->wl.width * scale; int scaledHeight = window->wl.height * scale; @@ -357,7 +357,7 @@ static void resizeWindow(_GLFWwindow* window) } } -void _glfwUpdateContentScaleWayland(_GLFWwindow* window) +void _glfwUpdateBufferScaleFromOutputsWayland(_GLFWwindow* window) { if (wl_compositor_get_version(_glfw.wl.compositor) < WL_SURFACE_SET_BUFFER_SCALE_SINCE_VERSION) @@ -368,13 +368,13 @@ void _glfwUpdateContentScaleWayland(_GLFWwindow* window) // Get the scale factor from the highest scale monitor. int maxScale = 1; - for (int i = 0; i < window->wl.scaleCount; i++) - maxScale = _glfw_max(window->wl.scales[i].factor, maxScale); + for (int i = 0; i < window->wl.outputScaleCount; i++) + maxScale = _glfw_max(window->wl.outputScales[i].factor, maxScale); // Only change the framebuffer size if the scale changed. - if (window->wl.contentScale != maxScale) + if (window->wl.bufferScale != maxScale) { - window->wl.contentScale = maxScale; + window->wl.bufferScale = maxScale; wl_surface_set_buffer_scale(window->wl.surface, maxScale); _glfwInputWindowContentScale(window, maxScale, maxScale); resizeFramebuffer(window); @@ -396,19 +396,19 @@ static void surfaceHandleEnter(void* userData, if (!window || !monitor) return; - if (window->wl.scaleCount + 1 > window->wl.scaleSize) + if (window->wl.outputScaleCount + 1 > window->wl.outputScaleSize) { - window->wl.scaleSize++; - window->wl.scales = - _glfw_realloc(window->wl.scales, - window->wl.scaleSize * sizeof(_GLFWscaleWayland)); + window->wl.outputScaleSize++; + window->wl.outputScales = + _glfw_realloc(window->wl.outputScales, + window->wl.outputScaleSize * sizeof(_GLFWscaleWayland)); } - window->wl.scaleCount++; - window->wl.scales[window->wl.scaleCount - 1].factor = monitor->wl.contentScale; - window->wl.scales[window->wl.scaleCount - 1].output = output; + window->wl.outputScaleCount++; + window->wl.outputScales[window->wl.outputScaleCount - 1] = + (_GLFWscaleWayland) { output, monitor->wl.scale }; - _glfwUpdateContentScaleWayland(window); + _glfwUpdateBufferScaleFromOutputsWayland(window); } static void surfaceHandleLeave(void* userData, @@ -420,17 +420,18 @@ static void surfaceHandleLeave(void* userData, _GLFWwindow* window = userData; - for (int i = 0; i < window->wl.scaleCount; i++) + for (int i = 0; i < window->wl.outputScaleCount; i++) { - if (window->wl.scales[i].output == output) + if (window->wl.outputScales[i].output == output) { - window->wl.scales[i] = window->wl.scales[window->wl.scaleCount - 1]; - window->wl.scaleCount--; + window->wl.outputScales[i] = + window->wl.outputScales[window->wl.outputScaleCount - 1]; + window->wl.outputScaleCount--; break; } } - _glfwUpdateContentScaleWayland(window); + _glfwUpdateBufferScaleFromOutputsWayland(window); } static const struct wl_surface_listener surfaceListener = @@ -970,7 +971,7 @@ static GLFWbool createNativeSurface(_GLFWwindow* window, window->wl.width = wndconfig->width; window->wl.height = wndconfig->height; - window->wl.contentScale = 1; + window->wl.bufferScale = 1; window->wl.title = _glfw_strdup(wndconfig->title); window->wl.appId = _glfw_strdup(wndconfig->wl.appId); @@ -997,7 +998,7 @@ static void setCursorImage(_GLFWwindow* window, buffer = cursorWayland->buffer; else { - if (window->wl.contentScale > 1 && cursorWayland->cursorHiDPI) + if (window->wl.bufferScale > 1 && cursorWayland->cursorHiDPI) { wlCursor = cursorWayland->cursorHiDPI; scale = 2; @@ -1398,7 +1399,7 @@ static void pointerHandleMotion(void* userData, struct wl_cursor_theme* theme = _glfw.wl.cursorTheme; int scale = 1; - if (window->wl.contentScale > 1 && _glfw.wl.cursorThemeHiDPI) + if (window->wl.bufferScale > 1 && _glfw.wl.cursorThemeHiDPI) { // We only support up to scale=2 for now, since libwayland-cursor // requires us to load a different theme for each size. @@ -2120,7 +2121,7 @@ void _glfwDestroyWindowWayland(_GLFWwindow* window) _glfw_free(window->wl.title); _glfw_free(window->wl.appId); - _glfw_free(window->wl.scales); + _glfw_free(window->wl.outputScales); } void _glfwSetWindowTitleWayland(_GLFWwindow* window, const char* title) @@ -2279,9 +2280,9 @@ void _glfwGetFramebufferSizeWayland(_GLFWwindow* window, int* width, int* height { _glfwGetWindowSizeWayland(window, width, height); if (width) - *width *= window->wl.contentScale; + *width *= window->wl.bufferScale; if (height) - *height *= window->wl.contentScale; + *height *= window->wl.bufferScale; } void _glfwGetWindowFrameSizeWayland(_GLFWwindow* window, @@ -2305,9 +2306,9 @@ void _glfwGetWindowContentScaleWayland(_GLFWwindow* window, float* xscale, float* yscale) { if (xscale) - *xscale = (float) window->wl.contentScale; + *xscale = (float) window->wl.bufferScale; if (yscale) - *yscale = (float) window->wl.contentScale; + *yscale = (float) window->wl.bufferScale; } void _glfwIconifyWindowWayland(_GLFWwindow* window)