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

wayland: use correct rounding in logical->physical conversions #3955

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mahkoh
Copy link
Contributor

@mahkoh mahkoh commented Oct 18, 2024

/// A wp-fractional-scale scale.
///
/// This type implements the `physical_size = round_half_up(logical_size * scale)`
/// operation with infinite precision as required by the protocol.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Scale {
        scale: u64,
}
  • Tested on all platforms changed
  • Added an entry to the changelog module if knowledge of this change could be valuable to users
  • Updated documentation to reflect any user-facing changes, including notes of platform-specific behavior
  • Created or updated an example program if it would help users understand this functionality
  • Updated feature matrix, if new features were added or implemented

```rust
/// A wp-fractional-scale scale.
///
/// This type implements the `physical_size = round_half_up(logical_size * scale)`
/// operation with infinite precision as required by the protocol.
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
pub struct Scale {
        scale: u64,
}
```
@kchibisov
Copy link
Member

While I don't mind changing the algorithm is unspecified, which we brought before here https://gitlab.freedesktop.org/wayland/wayland-protocols/-/issues/132

What winit does is what majority of compositors do (probably not yours since you've sent the patch), so we error the same.

Unless upstream issue progresses, I don't see why this new rounding is correct when there's no specification of what is correct.

@mahkoh
Copy link
Contributor Author

mahkoh commented Oct 18, 2024

See https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/309/diffs

What winit does is what majority of compositors do (probably not yours since you've sent the patch)

Actually my compositor also uses f64 just like winit. A mistake for sure.

Copy link
Member

@kchibisov kchibisov left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be also nice to have a test where naive impl doesn't round correctly, so it won't regress.


impl Scale {
pub fn from_wp_fractional_scale(v: u32) -> Self {
assert!(v > 0);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should use NonZero rather than an assert.

self.scale as f64 / BASE_F64
}

pub fn round_up(self) -> u32 {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we really need a roundup? The old logic was trunc. Though, the path were it can happen is always having an integer, so it would never round in reality.


const BASE: u32 = 120;
const BASE_U64: u64 = BASE as u64;
const BASE_F64: f64 = BASE as f64;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's used exactly in one place, so you can inline and just call it DENOMINATOR: u64 = 120.

@kchibisov
Copy link
Member

See https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/309/diffs

You should've have linked it right away in the PR description.

Will wait until it merges and then I'll merge it here as well once review is done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants