Closed Bug 2080307 Opened 1 month ago Closed 1 month ago

Wayland: toplevel opaque region set via gdk_window_set_opaque_region() is applied late, or not until the next GdkWindow repaint

Categories

(Core :: Widget: Gtk, defect)

Firefox 157
defect

Tracking

()

RESOLVED FIXED
160 Branch
Tracking Status
firefox160 --- fixed

People

(Reporter: Davis.pies1, Assigned: Davis.pies1)

References

(Blocks 1 open bug)

Details

Attachments

(1 external file)

User Agent: Mozilla/5.0 (X11; Linux x86_64; rv:157.0) Gecko/20100101 Firefox/157.0

Steps to reproduce

  1. Run Firefox in a Wayland session with WAYLAND_DEBUG=client.
  2. Click the URL bar, type a few characters, press Escape. Repeat a few times.
  3. Compare wl_surface.set_opaque_region requests on the toplevel (created by GTK) and on Gecko's subsurface.

Environment: Arch Linux, sway (wlroots), NVIDIA 610.57.04, GTK 3.24.52.

Actual results

The toplevel's opaque region lags the subsurface's by 0.4–1 s on each URL bar focus change. At startup, a 26x24 rect stayed on the toplevel for 12.5 s after the subsurface cleared it (32.61 -> 45.11 below).

Cause: nsWindow::UpdateOpaqueRegionInternal() sets the toplevel's opaque region with gdk_window_set_opaque_region(). On Wayland, GTK3 only marks the region dirty and sends wl_surface.set_opaque_region from the GdkWindow's paint path (gdkwindow-wayland.c, gdk_wayland_window_sync_opaque_region). Gecko draws into its own wl_subsurface, so the toplevel GdkWindow rarely repaints, and the toplevel's opaque region lags the subsurface's.

While the toplevel's region is stale, the compositor treats that area as opaque and stops drawing what's behind the window there. In stock Firefox this isn't visible, because the stale rects sit over opaque toolbar content. It is visible with transparent chrome: in Zen Browser (Firefox fork, 157.0.1) with its Linux transparency enabled, closing the opaque floating URL bar leaves a stale block that turns black wherever the compositor repaints (https://github.com/zen-browser/desktop/issues/15856). It would equally affect a transparent window set up through userChrome.css and widget.transparent-windows, which bug 2045222 comment 11 describes as the way forward for transparency.

Stock Firefox 157.0.1, condensed (one line per set_opaque_region, region resolved to rects; #75 = GTK toplevel, #69 = Gecko subsurface, #75 coords offset by (45, 45)):
  32.521957  #75 toplevel    (313, 97) 26x24
  32.568281  #69 subsurface  (268, 52) 26x24
  32.572959  #75 toplevel    (312, 97) 26x24
  32.575495  #69 subsurface  (267, 52) 26x24
  32.605910  #69 subsurface  empty
  33.088604  #69 subsurface  empty
  45.111230  #75 toplevel    nil
  45.384045  #75 toplevel    (302, 97) 26x24
  45.388859  #69 subsurface  (257, 52) 26x24
  45.405546  #69 subsurface  (257, 52) 26x24 + (258, 93) 724x27
  45.444798  #75 toplevel    (302, 97) 26x24 + (303, 138) 724x27
  46.522748  #75 toplevel    (302, 97) 26x24
  46.538838  #69 subsurface  (257, 52) 26x24
  46.963257  #69 subsurface  (257, 52) 26x24 + (258, 93) 724x27
  47.919585  #75 toplevel    (302, 97) 26x24 + (303, 138) 724x27
  48.638813  #69 subsurface  (257, 52) 26x24
  48.740596  #75 toplevel    (302, 97) 26x24
  49.008928  #69 subsurface  (257, 52) 26x24 + (258, 93) 724x27
  49.400161  #75 toplevel    (302, 97) 26x24 + (303, 138) 724x27
  50.655573  #69 subsurface  (257, 52) 26x24
  50.740573  #75 toplevel    (302, 97) 26x24
  51.022468  #69 subsurface  (257, 52) 26x24 + (258, 93) 724x27
  51.436667  #75 toplevel    (302, 97) 26x24 + (303, 138) 724x27
  52.303049  #75 toplevel    (302, 97) 26x24
  52.305769  #69 subsurface  (257, 52) 26x24
  53.383821  window closed

Expected results

The toplevel's opaque region is updated together with the subsurface's.

nsWindow::SetInputRegion() already handles the same GTK caching for the input region by calling gdk_window_invalidate_rect() right after gdk_window_input_shape_combine_region(). Doing the same after gdk_window_set_opaque_region() fixes it:

 #ifdef MOZ_WAYLAND
     if (GdkIsWaylandDisplay()) {
+      // On Wayland gdk_window_set_opaque_region() call is cached and applied
+      // to underlying wl_surface when GdkWindow is repainted. Force repaint of
+      // GdkWindow to apply the change immediately, otherwise the compositor
+      // keeps treating a no longer opaque area as opaque.
+      gdk_window_invalidate_rect(window, nullptr, false);
       mSurface->SetOpaqueRegion(mOpaqueRegion.ToUnknownRegion());
     }
 #endif

With this applied, the toplevel's opaque region follows the subsurface's within a few milliseconds. The code is unchanged on main. I can submit it through Phabricator.

I tracked this down and drafted the patch with help from Claude; I've verified the behavior with the logs above, and the fix by applying the same call to Zen 1.23.1b at runtime (LD_PRELOAD wrapper around gdk_window_set_opaque_region); it hasn't been through a full build yet.

Blocks: wayland
Severity: -- → S3
Flags: needinfo?(stransky)

On Wayland, gdk_window_set_opaque_region() is cached by GTK and only sent
to the wl_surface when the GdkWindow repaints. Gecko draws into its own
subsurface, so the toplevel rarely repaints and its opaque region lags the
subsurface's, sometimes by seconds. While it is stale the compositor skips
drawing what's behind that area, which shows up as stale or black blocks on
transparent windows.

Force a repaint after setting the region, as nsWindow::SetInputRegion()
already does for the input region.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

Assignee: nobody → Davis.pies1
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
Pushed by ealvarez@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/2dd0b125686c https://hg.mozilla.org/integration/autoland/rev/0b7f3d862d7e [Wayland] Apply toplevel opaque region changes immediately. r=gtk-reviewers,stransky,emilio
Flags: needinfo?(stransky)
Status: ASSIGNED → RESOLVED
Closed: 1 month ago
Resolution: --- → FIXED
Target Milestone: --- → 160 Branch
See Also: → 2081619
Regressions: 2081642
Pushed by amarc@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/861b4fc88a01 https://hg.mozilla.org/integration/autoland/rev/8410999b5d9b Revert "Bug 2080307 - [Wayland] Apply toplevel opaque region changes immediately. r=gtk-reviewers,stransky,emilio" for causing bug 2081642
Status: RESOLVED → REOPENED
Flags: needinfo?(Davis.pies1)
Resolution: FIXED → ---
Target Milestone: 160 Branch → ---

Ack, thanks for the backout. The cause is explained in bug 2081642 comment 5: the explicit toplevel commit can run before the xdg_toplevel exists when session restore delays showing the window (needs a gtk3 with the xdg-toplevel-realized patch, e.g. Fedora 44). I've updated D332610 to skip the direct set and commit until the window is mapped, and I'll ask for re-review once it's been tested on an affected setup.

Flags: needinfo?(Davis.pies1)
Pushed by stransky@redhat.com: https://github.com/mozilla-firefox/firefox/commit/a877dd2f4444 https://hg.mozilla.org/integration/autoland/rev/d1238f5a7228 [Wayland] Apply toplevel opaque region changes immediately. r=gtk-reviewers,stransky,emilio
Status: REOPENED → RESOLVED
Closed: 1 month ago → 1 month ago
Resolution: --- → FIXED
Target Milestone: --- → 160 Branch
QA Whiteboard: [qa-triage-done-c161/b160]
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: