File 0002-platformx-x11-Add-a-freeze-protection-against-OpenGL.patch of Package kwin5

From ee42322059cd772c38f9cbd33be69ae6316ccbce Mon Sep 17 00:00:00 2001
From: Antonio Larrosa <larrosa@kde.org>
Date: Fri, 21 Oct 2016 21:06:47 +0200
Subject: [PATCH 2/4] [platformx/x11] Add a freeze protection against OpenGL

Summary:
With nouveau driver it can happen that KWin gets frozen when first trying
to render with OpenGL. This results in a freeze of the complete desktop
as the compositor is non functional.

Our OpenGL breakage detection is only able to detect crashes, but not
freezes. This change improves it by also added a freeze protection.

In the PreInit stage a thread is started with a QTimer of 15 sec. If the
timer fires, qFatal is triggered to terminate KWin. This can only happen
if the creation of the OpenGL compositor takes longer than said 15 sec.

In the PostInit stage the timer gets deleted and the thread stopeed
again.

Thus if a freeze is detected the OpenGL unsafe protection is written into
the config. KWin aborts and gets restarted by DrKonqui. The new KWin
instance will no longer try to activate the freezing OpenGL as the
protection is set.

If KWin doesn't freeze the protection is removed from the config as
we are used to.

Check for freezes for the first n frames, not just the first

This patch changes the freeze detection code to detect freezes in the
first 30 frames (by default, users can change that with the
KWIN_MAX_FRAMES_TESTED environment variable). This detects
successfully the freezes associated to nouveau drivers
in https://bugzilla.suse.com/show_bug.cgi?id=1005323

Reviewers: #kwin, #plasma, davidedmundson

Subscribers: kwin, plasma-devel, davidedmundson

Tags: #kwin

Differential Revision: https://phabricator.kde.org/D3132
---
 composite.cpp                                     | 14 ++++++++------
 composite.h                                       |  3 ++-
 platform.h                                        |  4 ++--
 plugins/platforms/x11/standalone/x11_platform.cpp |  4 ++--
 4 files changed, 14 insertions(+), 11 deletions(-)

diff --git a/composite.cpp b/composite.cpp
index c3ae50e..a425a5f 100644
--- a/composite.cpp
+++ b/composite.cpp
@@ -135,6 +135,9 @@ Compositor::Compositor(QObject* workspace)
         }
     );
 
+    if (qEnvironmentVariableIsSet("KWIN_MAX_FRAMES_TESTED"))
+       m_maxFramesTestedForSafety = qEnvironmentVariableIntValue("KWIN_MAX_FRAMES_TESTED");
+
     // register DBus
     new CompositorDBusInterface(this);
 }
@@ -293,7 +296,6 @@ void Compositor::startupWithWorkspace()
         return;
     }
     Q_ASSERT(m_scene);
-    m_firstFrame = true;
     connect(workspace(), &Workspace::destroyed, this, [this] { compositeTimer.stop(); });
     claimCompositorSelection();
     m_xrrRefreshRate = KWin::currentRefreshRate();
@@ -734,15 +736,15 @@ void Compositor::performCompositing()
     // clear all repaints, so that post-pass can add repaints for the next repaint
     repaints_region = QRegion();
 
-    if (m_firstFrame && (m_scene->compositingType() & OpenGLCompositing)) {
-        kwinApp()->platform()->createOpenGLSafePoint(Platform::OpenGLSafePoint::PreFirstFrame);
+    if (m_frameCount < m_maxFramesTestedForSafety && (m_scene->compositingType() & OpenGLCompositing)) {
+        kwinApp()->platform()->createOpenGLSafePoint(Platform::OpenGLSafePoint::PreFrame);
     }
     m_timeSinceLastVBlank = m_scene->paint(repaints, windows);
-    if (m_firstFrame) {
+    if (m_frameCount < m_maxFramesTestedForSafety) {
         if (m_scene->compositingType() & OpenGLCompositing) {
-            kwinApp()->platform()->createOpenGLSafePoint(Platform::OpenGLSafePoint::PostFirstFrame);
+            kwinApp()->platform()->createOpenGLSafePoint(Platform::OpenGLSafePoint::PostFrame);
         }
-        m_firstFrame = false;
+        m_frameCount++;
     }
     m_timeSinceStart += m_timeSinceLastVBlank;
 
diff --git a/composite.h b/composite.h
index 5412628..c6effe0 100644
--- a/composite.h
+++ b/composite.h
@@ -239,7 +239,8 @@ private:
     Scene *m_scene;
     bool m_bufferSwapPending;
     bool m_composeAtSwapCompletion;
-    bool m_firstFrame = true;
+    int m_frameCount = 0;
+    int m_maxFramesTestedForSafety = 30;
 
     KWIN_SINGLETON_VARIABLE(Compositor, s_compositor)
 };
diff --git a/platform.h b/platform.h
index f1b8688..727ef70 100644
--- a/platform.h
+++ b/platform.h
@@ -143,8 +143,8 @@ public:
     enum class OpenGLSafePoint {
         PreInit,
         PostInit,
-        PreFirstFrame,
-        PostFirstFrame
+        PreFrame,
+        PostFrame
     };
     /**
      * This method is invoked before and after creating the OpenGL rendering Scene.
diff --git a/plugins/platforms/x11/standalone/x11_platform.cpp b/plugins/platforms/x11/standalone/x11_platform.cpp
index 1cd73a5..12c0395 100644
--- a/plugins/platforms/x11/standalone/x11_platform.cpp
+++ b/plugins/platforms/x11/standalone/x11_platform.cpp
@@ -200,7 +200,7 @@ void X11StandalonePlatform::createOpenGLSafePoint(OpenGLSafePoint safePoint)
     auto group = KConfigGroup(kwinApp()->config(), "Compositing");
     switch (safePoint) {
     case OpenGLSafePoint::PreInit:
-    case OpenGLSafePoint::PreFirstFrame:
+    case OpenGLSafePoint::PreFrame:
         Q_ASSERT(m_openGLFreezeProtectionThread == nullptr);
         m_openGLFreezeProtectionThread = new QThread(this);
         m_openGLFreezeProtectionThread->start();
@@ -217,7 +217,7 @@ void X11StandalonePlatform::createOpenGLSafePoint(OpenGLSafePoint safePoint)
         group.writeEntry(unsafeKey, true);
         break;
     case OpenGLSafePoint::PostInit:
-    case OpenGLSafePoint::PostFirstFrame:
+    case OpenGLSafePoint::PostFrame:
         m_openGLFreezeProtection->deleteLater();
         m_openGLFreezeProtectionThread->quit();
         m_openGLFreezeProtectionThread->wait();
-- 
2.10.1

openSUSE Build Service is sponsored by