Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#138 always round scales to nearest 1/8 to support fractional scaling and other recent compositor changes; use --log-threshold debug to see details #145

Merged
merged 14 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions inc/displ.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#define DISPL_H

#include <stdint.h>
#include <stdbool.h>

enum ConfigState {
IDLE = 0,
Expand All @@ -23,6 +24,8 @@ struct Displ {
char *interface;
uint32_t output_manager_version;

bool have_fractional_scale_v1;

enum ConfigState config_state;
};

Expand Down
13 changes: 13 additions & 0 deletions inc/head.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,13 @@
#include "mode.h"
#include "wlr-output-management-unstable-v1.h"

// wl_fixed_t, used by the wlr-output-management protocol, uses scales in multiples of 1/256.
// Meanwhile, the fractional-scale-v1 protocol deals with scales in multiples of 1/120,
// and there are observed differences in behavior between compositors, see !138.
// We force scales to be multiples of 1/8, because gcd(256, 120) = 8.
#define HEAD_DEFAULT_SCALING_BASE 8
#define HEAD_WLFIXED_SCALING_BASE 256

extern struct SList *heads;
extern struct SList *heads_arrived;
extern struct SList *heads_departed;
Expand Down Expand Up @@ -51,6 +58,8 @@ struct Head {
int32_t height;
} scaled;

int32_t scaling_base;

bool warned_no_preferred;
bool warned_no_mode;
};
Expand All @@ -67,6 +76,10 @@ bool head_matches_name_desc(const void *head, const void *name_desc);

bool head_name_desc_matches_head(const void *name_desc, const void *head);

wl_fixed_t head_get_fixed_scale(const struct Head *head, double scale, int32_t base);

int32_t head_get_scaled_length(int32_t length, wl_fixed_t fixed_scale, int32_t base);

wl_fixed_t head_auto_scale(struct Head *head, double min, double max);

void head_scaled_dimensions(struct Head *head);
Expand Down
102 changes: 102 additions & 0 deletions pro/fractional-scale-v1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<protocol name="fractional_scale_v1">
<copyright>
Copyright © 2022 Kenny Levinsen

Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice (including the next
paragraph) shall be included in all copies or substantial portions of the
Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
</copyright>

<description summary="Protocol for requesting fractional surface scales">
This protocol allows a compositor to suggest for surfaces to render at
fractional scales.

A client can submit scaled content by utilizing wp_viewport. This is done by
creating a wp_viewport object for the surface and setting the destination
rectangle to the surface size before the scale factor is applied.

The buffer size is calculated by multiplying the surface size by the
intended scale.

The wl_surface buffer scale should remain set to 1.

If a surface has a surface-local size of 100 px by 50 px and wishes to
submit buffers with a scale of 1.5, then a buffer of 150px by 75 px should
be used and the wp_viewport destination rectangle should be 100 px by 50 px.

For toplevel surfaces, the size is rounded halfway away from zero. The
rounding algorithm for subsurface position and size is not defined.
</description>

<interface name="wp_fractional_scale_manager_v1" version="1">
<description summary="fractional surface scale information">
A global interface for requesting surfaces to use fractional scales.
</description>

<request name="destroy" type="destructor">
<description summary="unbind the fractional surface scale interface">
Informs the server that the client will not be using this protocol
object anymore. This does not affect any other objects,
wp_fractional_scale_v1 objects included.
</description>
</request>

<enum name="error">
<entry name="fractional_scale_exists" value="0"
summary="the surface already has a fractional_scale object associated"/>
</enum>

<request name="get_fractional_scale">
<description summary="extend surface interface for scale information">
Create an add-on object for the the wl_surface to let the compositor
request fractional scales. If the given wl_surface already has a
wp_fractional_scale_v1 object associated, the fractional_scale_exists
protocol error is raised.
</description>
<arg name="id" type="new_id" interface="wp_fractional_scale_v1"
summary="the new surface scale info interface id"/>
<arg name="surface" type="object" interface="wl_surface"
summary="the surface"/>
</request>
</interface>

<interface name="wp_fractional_scale_v1" version="1">
<description summary="fractional scale interface to a wl_surface">
An additional interface to a wl_surface object which allows the compositor
to inform the client of the preferred scale.
</description>

<request name="destroy" type="destructor">
<description summary="remove surface scale information for surface">
Destroy the fractional scale object. When this object is destroyed,
preferred_scale events will no longer be sent.
</description>
</request>

<event name="preferred_scale">
<description summary="notify of new preferred scale">
Notification of a new preferred scale for this surface that the
compositor suggests that the client should use.

The sent scale is the numerator of a fraction with a denominator of 120.
</description>
<arg name="scale" type="uint" summary="the new preferred scale"/>
</event>
</interface>
</protocol>
45 changes: 39 additions & 6 deletions src/head.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,48 @@ bool head_matches_name_desc_exact(const void *h, const void *n) {
(head->description && strcmp(head->description, name_desc) == 0);
}

wl_fixed_t head_get_fixed_scale(const struct Head *head, double scale, int32_t base) {
// computes a scale value that is appropriate for putting into `zwlr_output_configuration_head_v1_set_scale`

wl_fixed_t fixed_scale = wl_fixed_from_double(scale);
wl_fixed_t fixed_scale_before = fixed_scale;

// See !138
base = base ? base : HEAD_DEFAULT_SCALING_BASE;
fixed_scale = round((double)fixed_scale / HEAD_WLFIXED_SCALING_BASE * base) \
* ((double)HEAD_WLFIXED_SCALING_BASE / base);
if (fixed_scale != fixed_scale_before) {
log_debug("\n%s: Rounded scale %g to nearest multiple of 1/%d: %.03f", head && head->name ? head->name : "???", scale, base, wl_fixed_to_double(fixed_scale));
}

return fixed_scale;
}

int32_t head_get_scaled_length(int32_t length, wl_fixed_t fixed_scale, int32_t base) {
// scales a (pixel) length by fixed_scale

// in case `base` comes from a not fully initialized Head (like in tests)
base = base ? base : HEAD_DEFAULT_SCALING_BASE;

fixed_scale = (double)fixed_scale / HEAD_WLFIXED_SCALING_BASE * base + 0.5;

// wayland truncates when calculating size
return floor((double)length * base / fixed_scale);
}

wl_fixed_t head_auto_scale(struct Head *head, double min, double max) {
if (!head || !head->desired.mode) {
return wl_fixed_from_int(1);
if (!head) {
return head_get_fixed_scale(head, 1.0, HEAD_DEFAULT_SCALING_BASE);
}

if (!head->desired.mode) {
return head_get_fixed_scale(head, 1.0, head->scaling_base);
}

// average dpi
double dpi = mode_dpi(head->desired.mode);
if (dpi == 0) {
return wl_fixed_from_int(1);
return head_get_fixed_scale(head, 1.0, head->scaling_base);
}

// round the dpi to the nearest 12, so that we get a nice even wl_fixed_t
Expand All @@ -170,7 +203,7 @@ wl_fixed_t head_auto_scale(struct Head *head, double min, double max) {
}

// 96dpi approximately correct for older monitors and became the convention for 1:1 scaling
return wl_fixed_from_double((double) dpi_quantized / 96);
return head_get_fixed_scale(head, (double) dpi_quantized / 96, head->scaling_base);
}

void head_scaled_dimensions(struct Head *head) {
Expand All @@ -186,8 +219,8 @@ void head_scaled_dimensions(struct Head *head) {
head->scaled.height = head->desired.mode->width;
}

head->scaled.height = (int32_t)((double)head->scaled.height * 256 / head->desired.scale + 0.5);
head->scaled.width = (int32_t)((double)head->scaled.width * 256 / head->desired.scale + 0.5);
head->scaled.height = head_get_scaled_length(head->scaled.height, head->desired.scale, head->scaling_base);
head->scaled.width = head_get_scaled_length(head->scaled.width, head->desired.scale, head->scaling_base);
}

struct Mode *head_find_mode(struct Head *head) {
Expand Down
7 changes: 3 additions & 4 deletions src/layout.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <stdlib.h>
#include <string.h>
#include <wayland-client-protocol.h>
#include <wayland-util.h>

#include "layout.h"

Expand Down Expand Up @@ -182,7 +181,7 @@ void desire_scale(struct Head *head) {

// all scaling disabled
if (cfg->scaling == OFF) {
head->desired.scale = wl_fixed_from_int(1);
head->desired.scale = head_get_fixed_scale(head, 1.0, head->scaling_base);
return;
}

Expand All @@ -191,7 +190,7 @@ void desire_scale(struct Head *head) {
for (struct SList *i = cfg->user_scales; i; i = i->nex) {
user_scale = (struct UserScale*)i->val;
if (head_matches_name_desc(head, user_scale->name_desc)) {
head->desired.scale = wl_fixed_from_double(user_scale->scale);
head->desired.scale = head_get_fixed_scale(head, user_scale->scale, head->scaling_base);
return;
}
}
Expand All @@ -201,7 +200,7 @@ void desire_scale(struct Head *head) {
head->desired.scale =
head_auto_scale(head, cfg->auto_scale_min, cfg->auto_scale_max);
} else {
head->desired.scale = wl_fixed_from_int(1);
head->desired.scale = head_get_fixed_scale(head, 1.0, head->scaling_base);
}
}

Expand Down
1 change: 1 addition & 0 deletions src/listener_output_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ static void head(void *data,

struct Head *head = calloc(1, sizeof(struct Head));
head->zwlr_head = zwlr_output_head_v1;
head->scaling_base = HEAD_DEFAULT_SCALING_BASE;

slist_append(&heads, head);
slist_append(&heads_arrived, head);
Expand Down
39 changes: 21 additions & 18 deletions src/listener_registry.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
Expand All @@ -9,6 +10,7 @@
#include "displ.h"
#include "log.h"
#include "process.h"
#include "fractional-scale-v1.h"
#include "wlr-output-management-unstable-v1.h"

// Displ data
Expand All @@ -18,28 +20,29 @@ static void global(void *data,
uint32_t name,
const char *interface,
uint32_t version) {

// only register for WLR output manager events
if (strcmp(interface, zwlr_output_manager_v1_interface.name) != 0)
return;

struct Displ *displ = data;
displ->name = name;
displ->interface = strdup(interface);

if (version < ZWLR_OUTPUT_MANAGER_V1_VERSION_MIN) {
log_error("\nwlr-output-management version %d found, minimum %d required, exiting. Consider upgrading your compositor.", version, ZWLR_OUTPUT_MANAGER_V1_VERSION_MIN);
wd_exit(EXIT_FAILURE);
} else if (version < ZWLR_OUTPUT_MANAGER_V1_VERSION) {
log_warn("\nwlr-output-management version %d found; %d required for full functionality. Consider upgrading your compositor.", version, ZWLR_OUTPUT_MANAGER_V1_VERSION);
displ->output_manager_version = ZWLR_OUTPUT_MANAGER_V1_VERSION_MIN;
} else {
displ->output_manager_version = ZWLR_OUTPUT_MANAGER_V1_VERSION;
}
if (strcmp(interface, zwlr_output_manager_v1_interface.name) == 0) {
displ->name = name;
displ->interface = strdup(interface);

if (version < ZWLR_OUTPUT_MANAGER_V1_VERSION_MIN) {
log_error("\nwlr-output-management version %d found, minimum %d required, exiting. Consider upgrading your compositor.", version, ZWLR_OUTPUT_MANAGER_V1_VERSION_MIN);
wd_exit(EXIT_FAILURE);
} else if (version < ZWLR_OUTPUT_MANAGER_V1_VERSION) {
log_warn("\nwlr-output-management version %d found; %d required for full functionality. Consider upgrading your compositor.", version, ZWLR_OUTPUT_MANAGER_V1_VERSION);
displ->output_manager_version = ZWLR_OUTPUT_MANAGER_V1_VERSION_MIN;
} else {
displ->output_manager_version = ZWLR_OUTPUT_MANAGER_V1_VERSION;
}

displ->output_manager = wl_registry_bind(wl_registry, name, &zwlr_output_manager_v1_interface, displ->output_manager_version);
displ->output_manager = wl_registry_bind(wl_registry, name, &zwlr_output_manager_v1_interface, displ->output_manager_version);

zwlr_output_manager_v1_add_listener(displ->output_manager, output_manager_listener(), displ);
zwlr_output_manager_v1_add_listener(displ->output_manager, output_manager_listener(), displ);
} else if (strcmp(interface, wp_fractional_scale_manager_v1_interface.name) == 0) {
displ->have_fractional_scale_v1 = true;
log_debug("\nCompositor supports %s version %d", interface, version);
}
}

static void global_remove(void *data,
Expand Down
2 changes: 1 addition & 1 deletion tst/GNUmakefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ OBJS = tst/util.o \

WRAPS_COMMON = -Wl,$\
--wrap=log_set_threshold,$\
--wrap=log_,--wrap=log_error,--wrap=log_warn,--wrap=log_info,--wrap=log_error_errno,$\
--wrap=log_,--wrap=log_error,--wrap=log_warn,--wrap=log_info,--wrap=log_debug,--wrap=log_error_errno,$\
--wrap=print_head,--wrap=print_mode,$\
--wrap=wd_exit,--wrap=wd_exit_message

Expand Down
Loading
Loading