Closed Bug 2000769 Opened 1 year ago Closed 11 months ago

[Wayland] Follow "stable rounding" algorithm for fractionaly scaled subsurfaces

Categories

(Core :: Widget: Gtk, enhancement, P3)

enhancement

Tracking

()

RESOLVED FIXED
147 Branch
Tracking Status
relnote-firefox --- 147+
firefox147 --- fixed

People

(Reporter: stransky, Assigned: stransky)

References

(Blocks 1 open bug, )

Details

Attachments

(2 files, 1 external file)

I've been pointed out at https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/143#note_1343171
which suggests subsurface rounding as:

Parent position independent subsurface position rounding: Scale and round subsurface positions and add the scaled and rounded parent position afterwards (pos = round(subsurface_pos * scale) + (parent_pos * scale), making subsurface position rounding independent of the position of its parent.

Subsurface position dependent subsurface dimension rounding: Scale and round subsurface dimensions after adding the position, then substracting the position in both compositor and client (size = round((subsurface_size + subsurface_pos) * scale) - round(subsurface_pos * scale), called "scale_length" in wlroots/sway). This is only made possible due to the parent position independent rounding, as the client would otherwise not know what position to use and so the compositor and client would never be able to agree (hence why I previously referred to this as a bug).

It's claimed to work for all cases. Let's investigate it. It may save us new v2 fractional scale protocol extension.

Would be great to get info how Mutter/KWin actually rounds the subsurfaces now. But with latest Firefix nightly, both Kwin and Mutter produces better results than before Firefox scale fixes. There may be very slight blur on Mutter for some window sizes, will check on different monitor when it's available.

Flags: needinfo?(stransky)

stale -> stable

Flags: needinfo?(stransky)
Summary: [Wayland] Investigate "stale rounding" algorithm for fractionaly scaled subsurfaces → [Wayland] Investigate "stable rounding" algorithm for fractionaly scaled subsurfaces

Sure :)

Flags: needinfo?(stransky)

So let's look at the rounding here, parent_pos is always zero as we use toplevel, so:

pos = round(subsurface_pos * scale)
size = round((subsurface_size + subsurface_pos) * scale) - round(subsurface_pos * scale)

I resized toplevel Firefox window until it's very blurry on mutter / Fedora 43 / 125% scale.
Actual Firefox rounding / subsurface sizing:

[Parent 148068: Main Thread]: D/Widget [7f1f380c2e00]: RecomputeBoundsWayland(1) GetBounds(mGdkWindow) [26,23] -> [971 x 851]
[Parent 148068: Main Thread]: V/WidgetWayland [7f1f380c2e00]: WaylandSurface::GetScale() fractional scale 1.250000
[Parent 148068: Main Thread]: D/Widget [7f1f380c2e00]: nsWindow::DispatchResized() client size [1213, 1064]
[Parent 148068: Main Thread]: D/Widget [7f1f380c2e00]: GtkCompositorWidget::NotifyClientSizeChanged() to 1213 x 1064

recalc according to stable algorithm:

pos = [26,23] -> [32.5, 28.75]
size = round([1246.25, 1092.5]) - round ([32.5, 28.75]) = [1246,1093] - [33,29] = [1213, 1064]

so Firefox produces the same values (for one subsurface placed at [26,23]) but mutter fails to follow it and produces extra blurry output.

Unfortunately I can't catch the difference on screenshot - mutter blurs the fonts when trying to get one.

Flags: needinfo?(stransky)
Type: enhancement → defect
Priority: -- → P3
Summary: [Wayland] Investigate "stable rounding" algorithm for fractionaly scaled subsurfaces → [Wayland] Mutter doesn't follow "stable rounding" algorithm for fractionaly scaled subsurfaces
No longer blocks: wayland-kde

Hold a moment here - I found another rounding bug in Firefox EGL window size. Will investigate first (related to Bug 1988717).

(In reply to Martin Stránský [:stransky] (ni? me) from comment #1)

Would be great to get info how Mutter/KWin actually rounds the subsurfaces now. But with latest Firefix nightly, both Kwin and Mutter produces better results than before Firefox scale fixes. There may be very slight blur on Mutter for some window sizes, will check on different monitor when it's available.

regarding kwin, it's

subsurface_top_left_x = round(subsurface.x * scale)
subsurface_top_left_y = round(subsurface.y * scale)

subsurface_bottom_right_x = subsurface_top_left_x + round(subsurface.width * scale)
    = round(subsurface.x * scale) + round(subsurface.width * scale)

subsurface_bottom_right_y = subsurface_top_left_y + round(subsurface.height * scale)
    = round(subsurface.y * scale) + round(subsurface.height * scale)

if it's a grand child surface (grandparent <- parent <- subsurface)

subsurface_top_left_x = round(grandparent.x * scale) + round(parent.x * scale) + round(subsurface.x * scale)
subsurface_top_left_y = round(grandparent.y * scale) + round(parent.y * scale) + round(subsurface.y * scale)

subsurface_bottom_right_x = subsurface_top_left_x + round(subsurface.width * scale)
    = round(grandparent.x * scale) + round(parent.x * scale) + round(subsurface.x * scale) + round(subsurface.width * scale)

subsurface_bottom_right_y = subsurface_top_left_y + round(subsurface.height * scale)
    = round(grandparent.y * scale) + round(parent.y * scale) + round(subsurface.y * scale) + round(subsurface.height * scale)

kwin rounds the position at every level in the scene graph up until the root node

if firefox makes x, y, w, and h of each surface integers (no fractions) after scaling, is it still matter how or when the compositor rounds? Usually, scale factor are 1.25, 1.5, 2.0, ... rational numbers.

Severity: -- → S3

(In reply to Thinker Li [:sinker] from comment #10)

if firefox makes x, y, w, and h of each surface integers (no fractions) after scaling, is it still matter how or when the compositor rounds? Usually, scale factor are 1.25, 1.5, 2.0, ... rational numbers.

The problem here is subsurface rounding while toplevel window has different size than the subsurface (child) and we need to match both together with the fractional scale.

Unfortunately I can't catch the difference on screenshot - mutter blurs the fonts when trying to get one.

Screenshots with fractional scaling enabled are kinda tricky on GNOME. You should:

  • Ensure all monitors are using the same scale factor, if you have multiple monitors
  • Use the "Selection" or "Screen" selection method, not "Window"

That was you get a screenshot which is a 1:1 mapping to physical pixels on the display.

If you use the "window" selection method, Mutter will take an "unscaled" screenshot of the window by using the buffer size of the toplevel surface as the screenshot image size. With Firefox, the toplevel is at an integer scale because that's what Gtk 3 supports, so the fractionally scaled subsurface will get upscaled to match.

Hold a moment here - I found another rounding bug in Firefox EGL window size. Will investigate first (related to Bug 1988717).

I just got an update on Nightly to build 20251125095327, and I found that it seems to make the rounding behaviour worse on GNOME, specifically in the vertical direction. It looks like (at 150% scale) at some vertical window sizes the top of the Firefox subsurface is placed 1 logical pixel too low, and is downscaled vertically to be 1 logical pixel smaller.

Screenshot of Firefox nightly 20251125095327 with blurred content on GNOME 49 (Fedora 43) at 150% fractional scaling.

There is a 2 physical pixel gap between the window shadow and the top of the firefox subsurface, and a 1 physical pixel gap between the window shadow and the bottom of the firefox subsurface. The window contents are blurred vertically due to buffer size mismatch (I think the buffer is 1px taller than Mutter thinks it should be, so it's downscaled by 1px)

Same window, but resized vertically slightly. At this window size there is a 1 physical pixel gap visible between the shadow and the top of the firefox subsurface, and no gap at the bottom of the window. The contents of the subsurface are sharp (Mutter and Firefox agree on the buffer size)

Thanks. If you run it on terminal with MOZ_LOG="Widget:5,WidgetWayland:5" env variables you can check how the subsurface is placed by firefox.

Looks like we compute the WaylandSurface size wrongly - without subsurface origin. Should be 1603 but we pass 1604.

[Parent 132826: Main Thread]: D/Widget [7fdc1c96db00]: RecomputeBoundsWayland(1) GetBounds(mGdkWindow) [26,23] -> [1283 x 1040] 
[Parent 132826: Main Thread]: D/Widget [7fdc1c96db00]: nsWindow::DispatchResized() client scaled size [1603, 1300]
[Parent 132826: Main Thread]: V/WidgetWayland [7fdc1c96db00]: WaylandSurface::SetSize() size [1283 x 1040] scale 1.250000 scaled [1604 x 1300]

Looks like I need to rework the WaylandSurface sizing a bit to consider subsurface position for sizing/rounding.

Vlad, do you think Kwin may also use the 'stable' [1] rounding if Mutter/Sway(WLRoots) use that? It looks promising and would be great to have all three major compositors on the same boat here.

I'd like to fix all issues for Firefox 147+ to have sharp fractional rendering there with it implemented. (but it's not finished yet - I'd like to just keep you up to date).

[1] https://gitlab.freedesktop.org/wayland/wayland-protocols/-/merge_requests/143#note_1343171

Flags: needinfo?(vlad.zahorodnii)
Type: defect → enhancement
Summary: [Wayland] Mutter doesn't follow "stable rounding" algorithm for fractionaly scaled subsurfaces → [Wayland] Follow "stable rounding" algorithm for fractionaly scaled subsurfaces

KWin claims to use stable already.

Flags: needinfo?(vlad.zahorodnii)
Flags: needinfo?(stransky)

Got build failure with this type conversion error trying to build with the attachment:

30:29.82 /var/tmp/portage/www-client/firefox-147.0_beta0/work/firefox-147.0/widget/gtk/WaylandSurface.cpp:1098:41: error: cannot convert 'mozilla::DesktopIntRect' {aka 'mozilla::gfx::IntRectTyped<mozilla::DesktopPixel>'} to 'mozilla::DesktopIntSize' {aka 'mozilla::gfx::IntSizeTyped<mozilla::DesktopPixel>'}
30:29.82 1098 | SetViewPortDestLocked(aSurfaceLock, viewportSize);
30:29.82 | ^~~~~~~~~~~~
30:29.82 | |
30:29.82 | mozilla::DesktopIntRect {aka mozilla::gfx::IntRectTyped<mozilla::DesktopPixel>}

Assignee: nobody → stransky
Attachment #9529512 - Attachment description: WIP: Bug 2000769 [Wayland] Follow 'stable rounding' algorithm for fractionaly scaled subsurfaces → Bug 2000769 [Wayland] Follow 'stable rounding' algorithm for fractionaly scaled subsurfaces r?emilio
Status: NEW → ASSIGNED
Flags: needinfo?(stransky)
Pushed by stransky@redhat.com: https://github.com/mozilla-firefox/firefox/commit/e8e17d408317 https://hg.mozilla.org/integration/autoland/rev/027d73824fe4 [Wayland] Follow 'stable rounding' algorithm for fractionaly scaled subsurfaces r=emilio
Status: ASSIGNED → RESOLVED
Closed: 11 months ago
Resolution: --- → FIXED
Target Milestone: --- → 147 Branch

Not sure what's going on but tested on my office monitor and it's broken/blurred on both KDE and Mutter now...so not a great improvement.

Flags: needinfo?(stransky)

Resize artifacts affects EGLWindow only - filed Bug 2004002 for it.

Flags: needinfo?(stransky)
Regressions: 2004350
Regressions: 2005160

Release Note Request (optional, but appreciated)
[Why is this notable]: Updates window and rendering surface sizes to match actual pixel grid so Firefox delivers sharp rendering on fractionally scaled displays regardless of actual window size.
[Affects Firefox for Android]:
[Suggested wording]:
[Links (documentation, blog post, etc)]:

relnote-firefox: --- → ?

Added to the Fx147 relnotes.

For Linux GNOME Mutter users, window and rendering surface sizes were updated to match the actual pixel grid so Firefox delivers sharp rendering on fractionally scaled displays regardless of actual window size.

(In reply to Ryan VanderMeulen [:RyanVM] from comment #28)

Added to the Fx147 relnotes.

For Linux GNOME Mutter users, window and rendering surface sizes were updated to match the actual pixel grid so Firefox delivers sharp rendering on fractionally scaled displays regardless of actual window size.

When Bug 2005160 is fixed it will work for KDE too.

Duplicate of this bug: 2005160
No longer duplicate of this bug: 2005160
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: