GHOST/Wayland: store fractional scaling in each window instead of DPI

This makes it possible to access the fractional scaling for a window,
there is no need to store the DPI which can be returned by getDPIHint().
This commit is contained in:
Campbell Barton 2022-10-25 15:25:26 +11:00
parent dd08b490af
commit 059d963192
2 changed files with 26 additions and 31 deletions

View File

@ -90,13 +90,8 @@ struct GWL_Window {
/** The scale value written to #wl_surface_set_buffer_scale. */
int scale = 0;
/**
* The DPI, either:
* - `scale * base_dpi`
* - `wl_fixed_to_int(scale_fractional * base_dpi)`
* When fractional scaling is available.
*/
uint32_t dpi = 0;
/** The fractional scale used to calculate the DPI. */
wl_fixed_t scale_fractional = 0;
#ifdef WITH_GHOST_WAYLAND_LIBDECOR
WGL_LibDecor_Window *libdecor = nullptr;
@ -147,7 +142,7 @@ static int output_scale_cmp(const GWL_Output *output_a, const GWL_Output *output
static int outputs_max_scale_or_default(const std::vector<GWL_Output *> &outputs,
const int32_t scale_default,
uint32_t *r_dpi)
wl_fixed_t *r_scale_fractional)
{
const GWL_Output *output_max = nullptr;
for (const GWL_Output *reg_output : outputs) {
@ -157,18 +152,16 @@ static int outputs_max_scale_or_default(const std::vector<GWL_Output *> &outputs
}
if (output_max) {
if (r_dpi) {
*r_dpi = output_max->has_scale_fractional ?
/* Fractional DPI. */
wl_fixed_to_int(output_max->scale_fractional * base_dpi) :
/* Simple non-fractional DPI. */
(output_max->scale * base_dpi);
if (r_scale_fractional) {
*r_scale_fractional = output_max->has_scale_fractional ?
output_max->scale_fractional :
wl_fixed_from_int(output_max->scale);
}
return output_max->scale;
}
if (r_dpi) {
*r_dpi = scale_default * base_dpi;
if (r_scale_fractional) {
*r_scale_fractional = wl_fixed_from_int(scale_default);
}
return scale_default;
}
@ -479,7 +472,7 @@ GHOST_WindowWayland::GHOST_WindowWayland(GHOST_SystemWayland *system,
*
* Using the maximum scale is best as it results in the window first being smaller,
* avoiding a large window flashing before it's made smaller. */
window_->scale = outputs_max_scale_or_default(system_->outputs(), 1, &window_->dpi);
window_->scale = outputs_max_scale_or_default(system_->outputs(), 1, &window_->scale_fractional);
/* Window surfaces. */
window_->wl_surface = wl_compositor_create_surface(system_->wl_compositor());
@ -751,7 +744,9 @@ GHOST_WindowWayland::~GHOST_WindowWayland()
uint16_t GHOST_WindowWayland::getDPIHint()
{
return window_->dpi;
/* Using the physical DPI will cause wrong scaling of the UI
* use a multiplier for the default DPI as a workaround. */
return wl_fixed_to_int(window_->scale_fractional * base_dpi);
}
GHOST_TSuccess GHOST_WindowWayland::setWindowCursorVisibility(bool visible)
@ -962,16 +957,16 @@ GHOST_Context *GHOST_WindowWayland::newDrawingContext(GHOST_TDrawingContextType
* Expose some members via methods.
* \{ */
uint16_t GHOST_WindowWayland::dpi() const
{
return window_->dpi;
}
int GHOST_WindowWayland::scale() const
{
return window_->scale;
}
wl_fixed_t GHOST_WindowWayland::scale_fractional() const
{
return window_->scale_fractional;
}
wl_surface *GHOST_WindowWayland::wl_surface() const
{
return window_->wl_surface;
@ -1035,13 +1030,13 @@ GHOST_TSuccess GHOST_WindowWayland::notify_size()
*/
bool GHOST_WindowWayland::outputs_changed_update_scale()
{
uint32_t dpi_next;
const int scale_next = outputs_max_scale_or_default(outputs(), 0, &dpi_next);
wl_fixed_t scale_fractional_next = 0;
const int scale_next = outputs_max_scale_or_default(outputs(), 0, &scale_fractional_next);
if (UNLIKELY(scale_next == 0)) {
return false;
}
const uint32_t dpi_curr = window_->dpi;
const wl_fixed_t scale_fractional_curr = window_->scale_fractional;
const int scale_curr = window_->scale;
bool changed = false;
@ -1055,10 +1050,8 @@ bool GHOST_WindowWayland::outputs_changed_update_scale()
changed = true;
}
if (dpi_next != dpi_curr) {
/* Using the real DPI will cause wrong scaling of the UI
* use a multiplier for the default DPI as workaround. */
window_->dpi = dpi_next;
if (scale_fractional_next != scale_fractional_curr) {
window_->scale_fractional = scale_fractional_next;
changed = true;
/* As this is a low-level function, we might want adding this event to be optional,

View File

@ -12,6 +12,8 @@
#include <vector>
#include <wayland-util.h> /* For #wl_fixed_t */
class GHOST_SystemWayland;
struct GWL_Output;
@ -95,8 +97,8 @@ class GHOST_WindowWayland : public GHOST_Window {
/* WAYLAND direct-data access. */
uint16_t dpi() const;
int scale() const;
wl_fixed_t scale_fractional() const;
struct wl_surface *wl_surface() const;
const std::vector<GWL_Output *> &outputs();