https://bugs.kde.org/show_bug.cgi?id=447383

            Bug ID: 447383
           Summary: When running QPainter Wayland sessions
                    (KWIN_COMPOSE=Q) on discrete GPU, alt-tab/previews and
                    logout screen are extremely slow
           Product: kwin
           Version: 5.23.4
          Platform: Other
                OS: Linux
            Status: REPORTED
          Severity: normal
          Priority: NOR
         Component: compositing
          Assignee: kwin-bugs-n...@kde.org
          Reporter: nyanpas...@tuta.io
  Target Milestone: ---

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

-- 
You are receiving this mail because:
You are watching all bug changes.

Reply via email to