Bug 447383

Summary: When running QPainter Wayland sessions (KWIN_COMPOSE=Q) on discrete GPU, alt-tab/previews and logout screen are extremely slow
Product: [Plasma] kwin Reporter: nyanpasu64 <nyanpasu64>
Component: platform-drmAssignee: KWin default assignee <kwin-bugs-null>
Status: RESOLVED UNMAINTAINED    
Severity: normal Keywords: wayland-only
Priority: LO    
Version First Reported In: 5.23.4   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed/Implemented In:
Sentry Crash Report:

Description nyanpasu64 2021-12-22 12:19:10 UTC
SUMMARY
***
NOTE: If you are reporting a crash, please try to attach a backtrace with debug symbols.
See https://community.kde.org/Guidelines_and_HOWTOs/Debugging/How_to_create_useful_crash_reports
***
When I start Plasma Wayland in software-rendered QPainter mode, pressing Alt+Tab, hovering taskbar items for window previews, or opening the logout screen causes KWin to drop in performance to "seconds per frame".

STEPS TO REPRODUCE
1. In a TTY session, run KWIN_COMPOSE=Q startplasma-wayland.
2. Once KWin Wayland starts up, open some app windows.
3. Press Alt+Tab, and hold Alt for long enough that the left sidebar appears and windows become translucent.
4. Hover a taskbar icon and then hover the window popup.
5. Press Ctrl+Alt+Del and watch the black logout screen.
6. (Less slow) Open any plasmashell panel (the start menu, clock, etc.).

OBSERVED RESULT

When pressing Alt+Tab, you may get drastic slowdowns before you release Alt. After you release Alt, you get slowdowns proportional to the total surface area of all windows other than the window you switched to.
When hovering a window popup to show a window preview, you get a drastic slowdown.
When pressing Ctrl+Alt+Del, painting every frame takes over 1 second.

I performed testing using a patched KWin (https://invent.kde.org/nyanpasu/kwin/-/tree/log-redraw-speed) which defaults to QPainter compositing and logs the duration of some QPainter::drawImage() calls to /tmp/kwin-log.txt. Then I watched for slow paints (100ms or more) using `tail +1f /tmp/kwin-log.txt | rg 'after (\\d){9}'`, and cleared the log (to save RAM) using `truncate --size 0 /tmp/kwin-log.txt`.

In my testing, the slowdown occurs in QPainter::drawImage() (when hovering a taskbar item, it's called with a destination image equal to my screen resolution, and a smaller source resolution). Depending on how you triggered the slowdown, this function is called by either KWin::SceneQPainter::Window::renderSurfaceItem or KWin::SceneQPainter::Window::performPaint. Then QPainter::drawImage() calls QRasterPaintEngine::drawImage calls QRasterPaintEnginePrivate::drawImage, which dispatches to a CPU-specific function (on my machine, qt_blend_argb32_on_argb32_avx2). According to my perf logs, this function spends a massive amount of time in BYTE_MUL_AVX2 -> _mm256_and_si256, and _mm256_shuffle_epi8 (in optimized Qt5 builds, these get inlined into consecutive vpand and vpshufb instructions).

On Matrix #kwin:kde.org, I was told that this is because the QPainter is reading from, and writing to, GPU memory mapped into the CPU address space. I think that on non-unified-memory configurations, this requires synchronizing on PCIe traffic (causing latency?).

EXPECTED RESULT
QPainter Wayland sessions should not be much slower than non-composited Xorg sessions on KDE or Xfce, and should not slow down drastically when performing certain operations.

I'm told that to fix this bug, you need to detect when kwin_wayland (possibly kwin_x11 too) is running on discrete GPUs, and draw to an intermediate target buffer later copied to the GPU (not sure if you need to read images from intermediate buffers too).

SOFTWARE/OS VERSIONS
Operating System: Arch Linux
KDE Plasma Version: 5.23.4
KDE Frameworks Version: 5.89.0
Qt Version: 5.15.2
Kernel Version: 5.15.10-zen1-1-zen (64-bit)
Graphics Platform: Wayland
Processors: 12 × AMD Ryzen 5 5600X 6-Core Processor
Memory: 15.6 GiB of RAM
Graphics Processor: llvmpipe on Wayland (NVIDIA GeForce GT 730/PCIe/SSE2 on X11)

ADDITIONAL INFORMATION
Comment 1 David Edmundson 2023-09-06 10:38:58 UTC
This bug was reported against an outdated version of KWin. We have made many changes since the. 
If the issue persists in newer versions can you reopen the bug report updating the version number.