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

From 572b7b40205f3cf31b43250ad31497ee90133590 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Martin=20Gr=C3=A4=C3=9Flin?= <mgraesslin@kde.org>
Date: Fri, 21 Oct 2016 14:21:29 +0200
Subject: [PATCH 1/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.

Reviewers: #kwin, davidedmundson, #plasma

Reviewed By: davidedmundson, #plasma

Subscribers: davidedmundson, plasma-devel, kwin

Tags: #plasma

Differential Revision: https://phabricator.kde.org/D3127
---
 composite.cpp                                     | 11 ++++++++++-
 composite.h                                       |  1 +
 platform.h                                        |  4 +++-
 plugins/platforms/x11/standalone/x11_platform.cpp | 22 ++++++++++++++++++++++
 plugins/platforms/x11/standalone/x11_platform.h   |  2 ++
 5 files changed, 38 insertions(+), 2 deletions(-)

diff --git a/composite.cpp b/composite.cpp
index 7290440..c3ae50e 100644
--- a/composite.cpp
+++ b/composite.cpp
@@ -207,7 +207,6 @@ void Compositor::slotCompositingOptionsInitialized()
 
             m_scene = SceneOpenGL::createScene(this);
 
-            // TODO: Add 30 second delay to protect against screen freezes as well
             kwinApp()->platform()->createOpenGLSafePoint(Platform::OpenGLSafePoint::PostInit);
 
             if (m_scene && !m_scene->initFailed()) {
@@ -294,6 +293,7 @@ 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,7 +734,16 @@ 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);
+    }
     m_timeSinceLastVBlank = m_scene->paint(repaints, windows);
+    if (m_firstFrame) {
+        if (m_scene->compositingType() & OpenGLCompositing) {
+            kwinApp()->platform()->createOpenGLSafePoint(Platform::OpenGLSafePoint::PostFirstFrame);
+        }
+        m_firstFrame = false;
+    }
     m_timeSinceStart += m_timeSinceLastVBlank;
 
     if (waylandServer()) {
diff --git a/composite.h b/composite.h
index a1690e2..5412628 100644
--- a/composite.h
+++ b/composite.h
@@ -239,6 +239,7 @@ private:
     Scene *m_scene;
     bool m_bufferSwapPending;
     bool m_composeAtSwapCompletion;
+    bool m_firstFrame = true;
 
     KWIN_SINGLETON_VARIABLE(Compositor, s_compositor)
 };
diff --git a/platform.h b/platform.h
index 3991f5f..f1b8688 100644
--- a/platform.h
+++ b/platform.h
@@ -142,7 +142,9 @@ public:
     virtual bool openGLCompositingIsBroken() const;
     enum class OpenGLSafePoint {
         PreInit,
-        PostInit
+        PostInit,
+        PreFirstFrame,
+        PostFirstFrame
     };
     /**
      * 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 8212d1e..1cd73a5 100644
--- a/plugins/platforms/x11/standalone/x11_platform.cpp
+++ b/plugins/platforms/x11/standalone/x11_platform.cpp
@@ -37,6 +37,7 @@ along with this program.  If not, see <http://www.gnu.org/licenses/>.
 #include <KConfigGroup>
 #include <KLocalizedString>
 
+#include <QThread>
 #include <QOpenGLContext>
 #include <QX11Info>
 
@@ -199,9 +200,30 @@ void X11StandalonePlatform::createOpenGLSafePoint(OpenGLSafePoint safePoint)
     auto group = KConfigGroup(kwinApp()->config(), "Compositing");
     switch (safePoint) {
     case OpenGLSafePoint::PreInit:
+    case OpenGLSafePoint::PreFirstFrame:
+        Q_ASSERT(m_openGLFreezeProtectionThread == nullptr);
+        m_openGLFreezeProtectionThread = new QThread(this);
+        m_openGLFreezeProtectionThread->start();
+        m_openGLFreezeProtection = new QTimer;
+        m_openGLFreezeProtection->setInterval(15000);
+        m_openGLFreezeProtection->setSingleShot(true);
+        m_openGLFreezeProtection->start();
+        m_openGLFreezeProtection->moveToThread(m_openGLFreezeProtectionThread);
+        connect(m_openGLFreezeProtection, &QTimer::timeout, m_openGLFreezeProtection,
+            [] {
+                qFatal("Freeze in OpenGL initialization detected");
+            }, Qt::DirectConnection);
+
         group.writeEntry(unsafeKey, true);
         break;
     case OpenGLSafePoint::PostInit:
+    case OpenGLSafePoint::PostFirstFrame:
+        m_openGLFreezeProtection->deleteLater();
+        m_openGLFreezeProtectionThread->quit();
+        m_openGLFreezeProtectionThread->wait();
+        delete m_openGLFreezeProtectionThread;
+        m_openGLFreezeProtectionThread = nullptr;
+        m_openGLFreezeProtection = nullptr;
         group.writeEntry(unsafeKey, false);
         break;
     }
diff --git a/plugins/platforms/x11/standalone/x11_platform.h b/plugins/platforms/x11/standalone/x11_platform.h
index e3a670e..b90b759 100644
--- a/plugins/platforms/x11/standalone/x11_platform.h
+++ b/plugins/platforms/x11/standalone/x11_platform.h
@@ -62,6 +62,8 @@ private:
     static bool hasGlx();
 
     XInputIntegration *m_xinputIntegration = nullptr;
+    QThread *m_openGLFreezeProtectionThread = nullptr;
+    QTimer *m_openGLFreezeProtection = nullptr;
 
 };
 
-- 
2.10.1

openSUSE Build Service is sponsored by