wl_shm pool memfd size isn't page aligned, which breaks udmabuf import in compositors
Categories
(Core :: Widget: Gtk, enhancement)
Tracking
()
| 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
| Assignee | ||
Comment 1•17 days ago
|
||
We use SW rendering for small popups so it's used along HW rendering.
| Assignee | ||
Comment 2•17 days ago
|
||
Updated•17 days ago
|
| Assignee | ||
Comment 5•11 days ago
|
||
Let's revert it due to Bug 2088923 regression which can't be simply fixed.
Comment 7•11 days ago
|
||
Backed out as requested in Matrix https://hg.mozilla.org/integration/autoland/rev/0c094062a7edb73d80f46516d91778a3856d9413
Updated•11 days ago
|
| Assignee | ||
Comment 8•11 days ago
|
||
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.
| Assignee | ||
Comment 9•11 days ago
|
||
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.
| Reporter | ||
Comment 10•11 days ago
|
||
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
| Reporter | ||
Comment 11•11 days ago
|
||
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
| Assignee | ||
Comment 12•10 days ago
|
||
I can surely review a patch for it but I have no idea how to fix the alignment. So patches welcome here.
Updated•9 days ago
|
| Reporter | ||
Comment 14•8 days ago
|
||
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).
Comment 15•7 days ago
|
||
Comment 16•7 days ago
|
||
| bugherder | ||
Description
•