Bug 443951

Summary: 5.23.0 Has Horrible Compositing Issues - Black Boxes and Flickering showing up
Product: [Plasma] kwin Reporter: Matt M (gardotd426) <gardotd426>
Component: compositingAssignee: KWin default assignee <kwin-bugs-null>
Status: RESOLVED FIXED    
Severity: normal CC: devguy.ca, dimanne, gardotd426, isaacair2006, jessica, kde, lucidsunlight, marc.schlegel, nate, nyanpasu64, pilotgi, techxgames, yfprojects
Priority: NOR Keywords: regression
Version: 5.23.0Flags: vlad.zahorodnii: NVIDIA+
Target Milestone: ---   
Platform: Other   
OS: Linux   
See Also: https://bugs.kde.org/show_bug.cgi?id=443341
Latest Commit: Version Fixed In: 5.23.2
Attachments: output of cat /proc/`pidof kwin_x11`/maps | grep gl
qdbus org.kde.KWin
qdbus org.kde.KWin /KWin supportInformation
Journalctl log section
Journalctl take 2

Description Matt M (gardotd426) 2021-10-18 08:49:53 UTC
Created attachment 142567 [details]
output of cat /proc/`pidof kwin_x11`/maps | grep gl

SUMMARY
Only hours after updating to 5.23.0, I've noticed horrible black boxes and flickering issues showing up. When fullscreening and un-fullscreening a video on YouTube in Brave Browser, black boxes show up everywhere when un-fullscreening and disappear when going back to fullscreen. Resizing windows causes really awful flickering. Popups like OBS's settings menu cause the whole screen behind the window to go black, and then you can make the black disappear by dragging the window around. It's really bad. 

Rebooting temporarily fixes it, but it always comes back. This issue was not present on 5.22. 

STEPS TO REPRODUCE
1. Upgrade to 5.23.0
2. Make sure compositing is enabled, OpenGL 3.1
3. After a period of time, compositing will go completely wonky.

OBSERVED RESULT
Compositing is essentially broken

EXPECTED RESULT
Compositing doesn't cause giant black boxes and flickering.

SOFTWARE/OS VERSIONS
Windows: 
macOS: 
Linux/KDE Plasma: Arch Linux
(available in About System)
KDE Plasma Version: 5.23.0
KDE Frameworks Version: 5.88.0
Qt Version: 5.15.2

ADDITIONAL INFORMATION

Two short video clips demonstrating the issue(s):

Black Boxes when fullscreen/unfullscreening: https://youtu.be/g5oObBP07VU

Black everything, flickering when resizing: https://youtu.be/P0NMTpt5pW0
Comment 1 Matt M (gardotd426) 2021-10-18 08:50:32 UTC
Created attachment 142568 [details]
qdbus org.kde.KWin
Comment 2 Vlad Zahorodnii 2021-10-18 08:58:16 UTC
Please post the output of

  qdbus org.kde.KWin /KWin supportInformation
Comment 3 Vlad Zahorodnii 2021-10-18 09:03:24 UTC
Based on the video, it seems like it's nvidia.

> After a period of time, compositing will go completely wonky.
Have you noticed anything that is more likely to trigger these visual artifacts?
Comment 4 David Edmundson 2021-10-18 09:29:43 UTC
Can you also confirm that restart kwin fixes it?
Comment 5 Matt M (gardotd426) 2021-10-18 10:42:00 UTC
Created attachment 142572 [details]
qdbus org.kde.KWin /KWin supportInformation
Comment 6 Matt M (gardotd426) 2021-10-18 10:44:12 UTC
(In reply to Vlad Zahorodnii from comment #3)
> Based on the video, it seems like it's nvidia.
> 
> > After a period of time, compositing will go completely wonky.
> Have you noticed anything that is more likely to trigger these visual
> artifacts?

Playing Games that trigger disabling the compositor (by using the "disable desktop effects" toggle in Lutris), so basically enabling and disabling the compositor, and watching videos in browsers. 

>Can you also confirm that restart kwin fixes it?

I'll try and trigger it and see if kwin_x11 --replace fixes it.
Comment 7 Matt M (gardotd426) 2021-10-19 18:02:08 UTC
Okay, so I can force it to happen every time by switching to a TTY and back. 

kwin_x11 --replace fixes it *most* times, once it caused everything to go wonky and I had to log out and back in, but yeah it generally fixes it. 

But before 5.23 I could change to a TTY and back and this wouldn't happen.
Comment 8 Matt M (gardotd426) 2021-10-20 03:39:14 UTC
Alt-Tabbing out of Control (the game) triggered it again. kwin_x11 --replace fixed it.
Comment 9 Matt M (gardotd426) 2021-10-20 03:44:59 UTC
(In reply to Matt McDonald from comment #8)
> Alt-Tabbing out of Control (the game) triggered it again. kwin_x11 --replace
> fixed it.

Actually just disabling and re-enabling compositing with Alt+Shift+F12 causes it. It wasn't the alt-tabbing out of the game, it was that I have desktop effects disabled and re-enabled upon startup and exit of the game.
Comment 10 Vlad Zahorodnii 2021-10-21 07:30:13 UTC
Can you please check a couple more things?

* does this glitch show up with KWIN_USE_BUFFER_AGE=0 envvar?
* does this glitch show up with fullscreen repaints? (in order to force fullscreen compositing, go to compositor settings and select "full screen repaints" tearing prevention)
* when the glitch occurs, can you check kwin's logs?
Comment 11 Matt M (gardotd426) 2021-10-21 08:01:34 UTC
(In reply to Vlad Zahorodnii from comment #10)
> Can you please check a couple more things?
> 
> * does this glitch show up with KWIN_USE_BUFFER_AGE=0 envvar?
> * does this glitch show up with fullscreen repaints? (in order to force
> fullscreen compositing, go to compositor settings and select "full screen
> repaints" tearing prevention)
> * when the glitch occurs, can you check kwin's logs?


 - I can't seem to force it with fullscreen repaints enabled. Obviously for performance reasons I hope to not have to keep that option enabled. 

 - I put `KWIN_USE_BUFFER_AGE=0 in my /etc/environment and will try to force it (without full screen repaints) next reboot. 

 - Actually I just got out of Doom Eternal and the bug appeared (obviously this was without full screen repaints) so I have the journalctl logs from that moment, but there doesn't seem to be much. I'll upload the relevant section as an attachment.
Comment 12 Matt M (gardotd426) 2021-10-21 08:01:55 UTC
Created attachment 142706 [details]
Journalctl log section
Comment 13 Matt M (gardotd426) 2021-10-21 08:04:56 UTC
Oh I'm also seeing a really horrible new bug where right-clicking tray icons on the system tray to bring up their menu will often cause the desktop to lose it's mind. Like, open windows will rapidly rotate in and out of focus, I'll get 20 "Configure Desktop and Wallpaper" windows opening (as if I right-clicked and selected "Configure Desktop and Wallpaper" a dozen or more times), I can't click on any window or do anything for like 3 minutes, I have to just wait for it to stop having a nervous breakdown. I'm not sure if this is related or not, but this also never once happened before 5.23 and I've seen it happen about twice a day since the upgrade (and it happens on both 470 and 495 Nvidia drivers). I'll try to get logs from that next time it happens too.
Comment 14 Vlad Zahorodnii 2021-10-21 08:11:49 UTC
(In reply to Matt McDonald from comment #13)
> Oh I'm also seeing a really horrible new bug where right-clicking tray icons
> on the system tray to bring up their menu will often cause the desktop to
> lose it's mind. Like, open windows will rapidly rotate in and out of focus,
> I'll get 20 "Configure Desktop and Wallpaper" windows opening (as if I
> right-clicked and selected "Configure Desktop and Wallpaper" a dozen or more
> times), I can't click on any window or do anything for like 3 minutes, I
> have to just wait for it to stop having a nervous breakdown. I'm not sure if
> this is related or not, but this also never once happened before 5.23 and
> I've seen it happen about twice a day since the upgrade (and it happens on
> both 470 and 495 Nvidia drivers). I'll try to get logs from that next time
> it happens too.

Maybe file another bug report about this one.
Comment 15 Matt M (gardotd426) 2021-10-21 16:30:23 UTC
Okay so full screen repaints isn't cutting it. After selecting the option earlier, I couldn't force the bug by switching to a TTY and back, but after I rebooted to test KWIN_USE_BUFFER_AGE=0 in my /etc/environment, and switching back and forth between "Automatic," "Only when cheap," and "Full screen repaints," I saw the issue on all three - which also means that KWIN_USE_BUFFER_AGE=0 doesn't help. 

kwin_x11 --replace still works to fix it. 

I'll attach the journalctl messages from this go-round.
Comment 16 Matt M (gardotd426) 2021-10-21 16:30:41 UTC
Created attachment 142734 [details]
Journalctl take 2
Comment 17 Jessica M 2021-10-22 21:30:13 UTC
I have this issue as well on my laptop with Intel HD 4000 graphics and two desktop machines with an RTX 2080 Super and GTX 970. Sometimes this bug can cause an entire monitor to start flashing rapidly. I cannot recreate it consistently but it started happening after the 5.23 update.

This is a rather serious issue for me as I am photosensitive and flashing can cause medical problems.
Comment 18 Vlad Zahorodnii 2021-10-25 06:58:51 UTC
Can somebody verify whether setting `NVreg_PreserveVideoMemoryAllocations=1` makes any difference? https://bugs.kde.org/show_bug.cgi?id=443341#c7
Comment 19 Matt M (gardotd426) 2021-10-25 08:18:51 UTC
(In reply to Vlad Zahorodnii from comment #18)
> Can somebody verify whether setting `NVreg_PreserveVideoMemoryAllocations=1`
> makes any difference? https://bugs.kde.org/show_bug.cgi?id=443341#c7

Maybe I should have been more clear but I don't suspend or hibernate my system, so that parameter doesn't apply.
Comment 20 Matt M (gardotd426) 2021-10-25 08:19:48 UTC
I mean I can try it just for the sake of it but that parameter is specifically for preserving VRAM for suspend/resume, and I've never suspended this PC once.
Comment 21 Vlad Zahorodnii 2021-10-25 08:34:04 UTC
(In reply to Matt McDonald from comment #9)
> Actually just disabling and re-enabling compositing with Alt+Shift+F12
> causes it. It wasn't the alt-tabbing out of the game, it was that I have
> desktop effects disabled and re-enabled upon startup and exit of the game.

I've installed an nvidia video card on my machine and can't reproduce the bug by toggling compositing. Some apps don't handle graphics reset properly, but that seems the case even with kwin 5.22.
Comment 22 Vlad Zahorodnii 2021-10-25 08:48:16 UTC
Okay, it just happened to me. In order to reproduce the issue I pressed alt-tab to show and hide the task switcher, switched to another vt, switched back to plasma and clicked "Edit Current Profile..." in konsole
Comment 23 Vlad Zahorodnii 2021-10-25 08:49:02 UTC
I only had two apps open - firefox and konsole.
Comment 24 Vlad Zahorodnii 2021-10-25 08:59:00 UTC
Yeah, it has something to do with window thumbnails. In 5.23.0, we changed how window thumbnails are rendered (for the new overview effect and to avoid having detached thumbnails in the task switcher).

With relevant code being commented out, I see no flickering...
Comment 25 Matt M (gardotd426) 2021-10-25 09:11:31 UTC
I just hit alt-tab, then opened Konsole -> right-click -> Edit current profile then toggled compositing on and off and caused the issue as well. Yeah after my initial comment saying I could force it just by toggling compositing I found that it doesn't do it every time, but yeah...
Comment 26 Vlad Zahorodnii 2021-10-25 09:32:57 UTC
Aha, I have a theory why it happens. The WindowThumbnail item will delay the destruction of the thumbnail texture to avoid destroying a texture which is still in use.

On the other hand, the GLTexture seems to have a ref-counted VBO (https://invent.kde.org/plasma/kwin/-/blob/bd3d275293821acd490a8d4e28e59860b8f0bae5/src/libkwineffects/kwingltexture.cpp#L295), which is destroyed when the last texture is gone.

Since the thumbnail texture can be alive even after compositing has been re-initialized. KWin can continue using old VBO, which is long time gone.

Just to confirm my theory, I made the WindowThumbnail destroy the thumbnail texture immediately and it seems like there are no glitches anymore..
Comment 27 Vlad Zahorodnii 2021-10-25 09:40:33 UTC
Correction: m_vbo is an instance property. The issue seems to affect s_fbo. With the following patch

diff --git a/src/libkwineffects/kwingltexture.cpp b/src/libkwineffects/kwingltexture.cpp
index de08572ef..3e3c0fb8d 100644
--- a/src/libkwineffects/kwingltexture.cpp
+++ b/src/libkwineffects/kwingltexture.cpp
@@ -334,6 +334,10 @@ void GLTexturePrivate::cleanup()
 {
     s_supportsFramebufferObjects = false;
     s_supportsARGB32 = false;
+    if (s_fbo) {
+        glDeleteFramebuffers(1, &s_fbo);
+        s_fbo = 0;
+    }
 }
 
 bool GLTexture::isNull() const

I can't reproduce the bug.
Comment 28 Matt M (gardotd426) 2021-10-25 09:49:19 UTC
(In reply to Vlad Zahorodnii from comment #27)
> Correction: m_vbo is an instance property. The issue seems to affect s_fbo.
> With the following patch
> 
> diff --git a/src/libkwineffects/kwingltexture.cpp
> b/src/libkwineffects/kwingltexture.cpp
> index de08572ef..3e3c0fb8d 100644
> --- a/src/libkwineffects/kwingltexture.cpp
> +++ b/src/libkwineffects/kwingltexture.cpp
> @@ -334,6 +334,10 @@ void GLTexturePrivate::cleanup()
>  {
>      s_supportsFramebufferObjects = false;
>      s_supportsARGB32 = false;
> +    if (s_fbo) {
> +        glDeleteFramebuffers(1, &s_fbo);
> +        s_fbo = 0;
> +    }
>  }
>  
>  bool GLTexture::isNull() const
> 
> I can't reproduce the bug.


I just built KWin with that patch and I can't get the bug to trigger as of right now. I've alt-tabbed and opened windows and disabled and enabled compositing like crazy, and I can't reproduce it anymore. I'll obviously keep trying and will report what I find.
Comment 29 Bug Janitor Service 2021-10-25 10:02:02 UTC
A possibly relevant merge request was started @ https://invent.kde.org/plasma/kwin/-/merge_requests/1564
Comment 30 Vlad Zahorodnii 2021-10-25 10:28:41 UTC
*** Bug 443277 has been marked as a duplicate of this bug. ***
Comment 31 Vlad Zahorodnii 2021-10-25 10:29:17 UTC
Git commit fda7e150df207d512b60ba4062be5edafc61d539 by Vlad Zahorodnii.
Committed on 25/10/2021 at 10:00.
Pushed by vladz into branch 'master'.

kwineffects: Fix destruction of s_fbo with shared GLTexture objects

The WindowThumbnail item uses the GLTexture class. In order to destroy
the thumbnail texture, the item will schedule a destroy job.

This means that the GLTexture can be alive during or after graphics
reset.

On the other hand, as an implementation detail, GLTexture::clear() may
allocate a framebuffer object, which is going to be destroyed together
with the last texture.

Given that window thumbnail textures can be still alive after a graphics
reset and the fact that GLTexturePrivate::s_fbo gets destroyed when the
last texture is destroyed, kwin can end up trying to clear a decoration
texture with now defunct s_fbo.

Since the old old s_fbo is inert, the glBindFramebuffer() function will
fail and the glClear() operation will affect the default framebuffer,
thus leading to black flickering visual artifacts.

In order to fix that issue, this change makes GLTexture destroy s_fbo
unconditionally in GLTexturePrivate::cleanup() which is called whenever
OpenGL stuff is about to tear down, e.g. due to graphics reset, etc.

M  +4    -7    src/libkwineffects/kwingltexture.cpp
M  +0    -1    src/libkwineffects/kwingltexture_p.h

https://invent.kde.org/plasma/kwin/commit/fda7e150df207d512b60ba4062be5edafc61d539
Comment 32 Vlad Zahorodnii 2021-10-25 10:29:48 UTC
Git commit 1b1aef83b7ebf388c1d9ce6b8925eb89347c4a00 by Vlad Zahorodnii.
Committed on 25/10/2021 at 10:29.
Pushed by vladz into branch 'Plasma/5.23'.

kwineffects: Fix destruction of s_fbo with shared GLTexture objects

The WindowThumbnail item uses the GLTexture class. In order to destroy
the thumbnail texture, the item will schedule a destroy job.

This means that the GLTexture can be alive during or after graphics
reset.

On the other hand, as an implementation detail, GLTexture::clear() may
allocate a framebuffer object, which is going to be destroyed together
with the last texture.

Given that window thumbnail textures can be still alive after a graphics
reset and the fact that GLTexturePrivate::s_fbo gets destroyed when the
last texture is destroyed, kwin can end up trying to clear a decoration
texture with now defunct s_fbo.

Since the old old s_fbo is inert, the glBindFramebuffer() function will
fail and the glClear() operation will affect the default framebuffer,
thus leading to black flickering visual artifacts.

In order to fix that issue, this change makes GLTexture destroy s_fbo
unconditionally in GLTexturePrivate::cleanup() which is called whenever
OpenGL stuff is about to tear down, e.g. due to graphics reset, etc.


(cherry picked from commit fda7e150df207d512b60ba4062be5edafc61d539)

M  +4    -7    src/libkwineffects/kwingltexture.cpp
M  +0    -1    src/libkwineffects/kwingltexture_p.h

https://invent.kde.org/plasma/kwin/commit/1b1aef83b7ebf388c1d9ce6b8925eb89347c4a00
Comment 33 Awakening 2021-10-25 13:37:24 UTC
I'm coming from bug 443906, which is presumably the same thing,
tried running Plasma/5.23 branch which contains the patch,
still managed to run into full screen flickering after a while.
(unless my previous `kwin_x11 --replace` experiments somehow also at fault)
Comment 34 Vlad Zahorodnii 2021-10-25 13:47:26 UTC
Please make sure that it's not a packaging issue and you actually run kwin with the fix.
Comment 35 Awakening 2021-10-25 14:16:59 UTC
I'm using PKGBUILD from Arch repos as a reference and doing essentially the following:
- `cmake -B build -S kwin -DBUILD_TESTING=OFF -DCMAKE_INSTALL_LIBEXECDIR=lib`
- `cmake --build build -j $(nproc)`
- `DESTDIR=install cmake --install build`
- `cd install/usr/bin`
- `./kwin_x11 --replace`

Did a clean clone and added the print in the `main_x11.cpp` as a sanity check when running the binary, will report back once I run into flickering again.
Comment 36 Vlad Zahorodnii 2021-10-25 14:20:45 UTC
If you see flickering again, please describe how to reproduce the issue. The hardest part with this sort of bugs is finding a reliable way to reproduce the bug.
Comment 37 Awakening 2021-10-25 14:43:51 UTC
Well that was fast, got the flickering again.

Yeah, I'm not exactly sure whats causing it, sometimes it takes hours, sometimes shows up in 10 or so minutes.

Just doing the usual window manipulation (opening, moving, resizing, minimizing, maximizing). 
Occasionally toggling mpv border, but mostly for checking if flickering happens. (toggling it on will trigger the flicker if kwin is flicker-happy)

Right now flickering seemingly started right after Thunderbird notification, but trying to send some with `notify-send` doesn't seem to break it.

I tried "bisecting" but since reproduction is flaky, only managed to confirm that it still seem to happen on d543c0dff6b5ad287615d6435a2a2dfd85e4085c (using v5.22.90 tag as a base)
Comment 38 Vlad Zahorodnii 2021-10-25 14:49:27 UTC
Can you reproduce the bug by following https://bugs.kde.org/show_bug.cgi?id=443951#c22 or https://bugs.kde.org/show_bug.cgi?id=443951#c25 ?
Comment 39 Awakening 2021-10-25 14:56:30 UTC
Tried following the second one, alt-tabbing and then toggling compositing off and on seems to be enough to trigger it. After that if I toggle mpv border or resize a window the flickering starts.
Comment 40 Vlad Zahorodnii 2021-10-25 15:00:52 UTC
(In reply to Awakening from comment #39)
> Tried following the second one, alt-tabbing and then toggling compositing
> off and on seems to be enough to trigger it. After that if I toggle mpv
> border or resize a window the flickering starts.

That's odd... I'm most certainly sure that this bug should be fixed. Does your Plasma/5.23 branch include 1b1aef83b7ebf388c1d9ce6b8925eb89347c4a00?
Comment 41 Awakening 2021-10-25 15:15:40 UTC
Pretty sure it does, not sure if stdout is redirected somewhere else in there, but I'm not hitting the print I put in `GLTexturePrivate::cleanup` or constructor.
Comment 42 Vlad Zahorodnii 2021-10-25 15:22:40 UTC
(In reply to Awakening from comment #41)
> Pretty sure it does, not sure if stdout is redirected somewhere else in
> there, but I'm not hitting the print I put in `GLTexturePrivate::cleanup` or
> constructor.

Oh, that's a good clue. It sounds like kwin uses wrong libkwinglutils for some reason. If you use qDebug(), it will be printed in stdout.

In either case, 5.23.2 will be released tomorrow (10/26). I suggest to wait until it's released. If the issue is still there, file a new bug report.
Comment 43 Awakening 2021-10-25 15:36:48 UTC
> It sounds like kwin uses wrong libkwinglutils for some reason.

Oops, it is indeed my "packaging" fault. 

`lld` shows kwin using my system libs, instead of local ones. 

Pointing it to local libs with `LD_LIBRARY_PATH=./../lib ./kwin_x11 --replace`, I can't reproduce the flickering after an alt-tab, and actually see the prints.
Comment 44 Awakening 2021-10-25 15:54:25 UTC
*** Bug 443906 has been marked as a duplicate of this bug. ***
Comment 45 Vlad Zahorodnii 2021-10-25 18:19:42 UTC
*** Bug 444379 has been marked as a duplicate of this bug. ***
Comment 46 Nate Graham 2021-10-25 20:25:37 UTC
*** Bug 444261 has been marked as a duplicate of this bug. ***
Comment 47 Nate Graham 2021-10-25 23:25:51 UTC
*** Bug 443341 has been marked as a duplicate of this bug. ***
Comment 48 Nate Graham 2021-10-26 13:53:46 UTC
*** Bug 444410 has been marked as a duplicate of this bug. ***
Comment 49 Nate Graham 2021-10-28 15:33:04 UTC
*** Bug 444463 has been marked as a duplicate of this bug. ***
Comment 50 Rajinder Yadav 2021-10-28 22:20:56 UTC
I am on openSUSE TW, how can I check if this fix has made it is? I am still seeing this problem today, which seems to have shown up last week.

I made a quick video, but unable to capture the flickering: https://www.youtube.com/watch?v=YMKQJ-kWfVg

Something all I need to do is open a new window and both my monitor will go into rapid flickering which is really jarring!

Operating System: openSUSE Tumbleweed 20211025
KDE Plasma Version: 5.23.1
KDE Frameworks Version: 5.87.0
Qt Version: 5.15.2
Kernel Version: 5.14.14-1-default (64-bit)
Graphics Platform: X11
Processors: 16 × AMD Ryzen 7 2700X Eight-Core Processor
Memory: 62.7 GiB of RAM
Graphics Processor: NVIDIA GeForce RTX 3080/PCIe/SSE2
Comment 51 Matt M (gardotd426) 2021-10-28 22:22:39 UTC
(In reply to Rajinder Yadav from comment #50)
> I am on openSUSE TW, how can I check if this fix has made it is? I am still
> seeing this problem today, which seems to have shown up last week.
> 
> I made a quick video, but unable to capture the flickering:
> https://www.youtube.com/watch?v=YMKQJ-kWfVg
> 
> Something all I need to do is open a new window and both my monitor will go
> into rapid flickering which is really jarring!
> 
> Operating System: openSUSE Tumbleweed 20211025
> KDE Plasma Version: 5.23.1
> KDE Frameworks Version: 5.87.0
> Qt Version: 5.15.2
> Kernel Version: 5.14.14-1-default (64-bit)
> Graphics Platform: X11
> Processors: 16 × AMD Ryzen 7 2700X Eight-Core Processor
> Memory: 62.7 GiB of RAM
> Graphics Processor: NVIDIA GeForce RTX 3080/PCIe/SSE2



>Version Fixed In: 5.23.2


...


> Operating System: openSUSE Tumbleweed 20211025
> KDE Plasma Version: 5.23.1
Comment 52 Nate Graham 2021-11-01 15:36:13 UTC
*** Bug 444632 has been marked as a duplicate of this bug. ***
Comment 53 Nate Graham 2022-01-12 01:10:04 UTC
*** Bug 447704 has been marked as a duplicate of this bug. ***