Closed Bug 2087615 Opened 17 days ago Closed 7 days ago

wl_shm pool memfd size isn't page aligned, which breaks udmabuf import in compositors

Categories

(Core :: Widget: Gtk, enhancement)

Firefox 157
enhancement

Tracking

()

RESOLVED FIXED
162 Branch
Tracking Status
firefox162 --- fixed

People

(Reporter: netflate, Assigned: stransky)

References

(Blocks 1 open bug)

Details

Attachments

(1 external file, 1 obsolete file)

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

Steps to reproduce

Used Firefox with gfx.webrender.software=true on Wayland compositor that imports wl_shm buffers via udmabuf (Smithay, kwin)

Actual results

SHMBufSurfaceRGBA::CreateImpl() (widget/gtk/SHMBufSurface.cpp) creates the memfd with exactly widthheight4 bytes (3901640, not a multiple of 4096) and seals it against growing.

while UDMABUF_CREATE needs whole pages, so it fails with EINVAL, making the compositor fallback to simlpe copying every frame.

The code is unchanged on main.

Expected results

memfd size should be rounded up to the page size (less than 4 KiB extra per buffer), so the compositor will be able to import it without copying (+-670 us vs +-22 us per 1080p frame).
37% less compositor cpu on video.

obviously only a small chunk of people run firefox with software rendering, but it's a nice performance gain for such a small harmless change

We use SW rendering for small popups so it's used along HW rendering.

Blocks: wayland
Assignee: nobody → stransky
Status: UNCONFIRMED → ASSIGNED
Ever confirmed: true
Status: ASSIGNED → RESOLVED
Closed: 14 days ago
Resolution: --- → FIXED
Target Milestone: --- → 161 Branch
Regressions: 2088923
See Also: → 2088176

Let's revert it due to Bug 2088923 regression which can't be simply fixed.

Pushed by asilaghi@mozilla.com: https://github.com/mozilla-firefox/firefox/commit/f5652fc5dd26 https://hg.mozilla.org/integration/autoland/rev/0c094062a7ed Revert "Bug 2087615 [Wayland] Align allocated wl_shm pool to page size r=emilio,gtk-reviewers" at dev request for causing Bug 2088923
Status: RESOLVED → REOPENED
Resolution: FIXED → ---
Target Milestone: 161 Branch → ---
Attachment #9671867 - Attachment is obsolete: true

Copy comment from https://bugzilla.mozilla.org/show_bug.cgi?id=2088923#c9

Unfortunately this is a KDE problem: kwin relies on the driver to reject strides it can't handle, and old AMD (r600) silently renders garbage instead, while newer AMD drivers reject them properly.

fix on the Firefox side would be to align the stride to 256 bytes, like Qt does. The stride is hardcoded as width * bpp in several places. Here's what I found, there might be more, but I think that's all:

SHMBufSurface.cpp: CreateImpl, UploadTexture, Lock, CreateWlBuffer, GetAsSourceSurface
WindowSurfaceWaylandMultiBuffer.cpp and NativeLayerWayland.cpp, where the front buffer is copied

I guess gfx::GetAlignedStride<256>() can compute it, and BufferSurface has mStrides[] to store it.

Really sorry about that. I'm not affiliated with KDE. I took their approach as the reference and almost made the same mistake.

I don't know how to address it - we just added the memory piece to the surface end, we don't use any stride at all. Is that bug on the gfx driver side where whole buffer is rendered regardless of passed buffer size? Because Firefox promotes correct width/height while stride=width so the memory at the end is juts unused.

nothing reads or renders the extra memory at the end. Problem is what the page-aligned size enables:

  • Before the patch, KWin could create a udmabuf from Firefox's buffers only by chance, when width * height happened to give a page aligned size. So it almost always copied them and the stride didn't matter
  • With the patch, udmabuf creation succeeds, so KWin lets gpu read Firefox's memory directly
    • Now gpu has to handle the stride itself (width * 4). Newer drivers (Intel/AMD) reject strides they can't handle and KWin falls back to copying, but old r600 silently accepts them and renders garbage.

Aligning the stride to 256 bytes like Qt does would make it work on any gpu There's no point waiting for a kwin fix: what they'd do is stop taking this path for unaligned strides, which would just send Firefox back to copying. So to get the udmabuf benefit, firefox needs the aligned stride anyway

We don't use stride at all - we just align the memory to page size and allocate the whole image at beginning. The stride is zero.

By stride I mean the row length in bytes, and Firefox always passes it: it's the width * 4 in wl_shm_pool_create_buffer() in CreateWlBuffer(). I think by stride you mean padding - you're right that there's no padding and that's the fix: pad each row so the row length is a multiple of 256 bytes, like Qt does

I can surely review a patch for it but I have no idea how to fix the alignment. So patches welcome here.

Duplicate of this bug: 2088923
Severity: -- → S3

Compositors that support importing wl_shm pools to GPU memory via
udmabuf (which avoids a copy) need the pool to be a whole number of
pages. For udmabuf to work and show a correct image on all drivers,
the stride is also aligned to 256 bytes and stored in
BufferSurface::mStrides[0], so all code accessing SHM buffer memory
uses it instead of width * bpp.

Some drivers don't reject a udmabuf import with an unaligned stride
but silently show a broken image instead (Bug 2088923).

Pushed by stransky@redhat.com: https://github.com/mozilla-firefox/firefox/commit/066db198eb2e https://hg.mozilla.org/integration/autoland/rev/450792dbae17 [Wayland] Align wl_shm pool size to page size and stride to 256 bytes r=stransky,gtk-reviewers
Status: REOPENED → RESOLVED
Closed: 14 days ago → 7 days ago
Resolution: --- → FIXED
Target Milestone: --- → 162 Branch
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: