Skip to content

armgit / krita

Public Imported from GitHub
0

Krita is a free and open source cross-platform application that offers an end-to-end solution for creating digital art files from scratch built on the KDE and Qt frameworks.

C++

Qt6: Fix OpenGL crash on macOS

Dmitry Kazakovbe5a55390977
diff --git a/libs/ui/CMakeLists.txt b/libs/ui/CMakeLists.txt
index 194512400f..46c1fbdb28 100644
--- a/libs/ui/CMakeLists.txt
+++ b/libs/ui/CMakeLists.txt
@@ -183,6 +183,7 @@ set(kritaui_LIB_SRCS
opengl/KisOpenGLSync.cpp
opengl/KisOpenGLBufferCreationGuard.cpp
opengl/KisOpenGLCanvasRenderer.cpp
+ opengl/KisOpenGLContextSwitchLock.cpp
kis_fps_decoration.cpp
tool/KisToolChangesTracker.cpp
diff --git a/libs/ui/opengl/KisOpenGLContextSwitchLock.cpp b/libs/ui/opengl/KisOpenGLContextSwitchLock.cpp
new file mode 100644
index 0000000000..f542da5876
--- /dev/null
+++ b/libs/ui/opengl/KisOpenGLContextSwitchLock.cpp
@@ -0,0 +1,53 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#include "KisOpenGLContextSwitchLock.h"
+
+#include <QOpenGLContext>
+#include <QOpenGLWidget>
+
+
+namespace {
+
+inline bool shouldUseLock()
+{
+#if defined(Q_OS_MACOS) || defined(Q_OS_ANDROID)
+ /**
+ * On OSX openGL different (shared) contexts have different execution queues.
+ * It means that the textures uploading and their painting can be easily reordered.
+ * To overcome the issue, we should ensure that the textures are uploaded in the
+ * same openGL context as the painting is done.
+ */
+ return true;
+#else
+ static const bool s_shouldUseLock = qEnvironmentVariableIsSet("KRITA_USE_STRICT_OPENGL_CONTEXT_SWITCH");
+ return s_shouldUseLock;
+#endif
+}
+
+}
+
+KisOpenGLContextSwitchLockAdapter::KisOpenGLContextSwitchLockAdapter(QOpenGLWidget *targetWidget)
+ : m_targetWidget(targetWidget)
+{
+}
+
+void KisOpenGLContextSwitchLockAdapter::lock() {
+ if (!shouldUseLock()) return;
+ m_oldContext = QOpenGLContext::currentContext();
+ m_oldSurface = m_oldContext ? m_oldContext->surface() : nullptr;
+ m_targetWidget->makeCurrent();
+}
+
+void KisOpenGLContextSwitchLockAdapter::unlock() {
+ if (!shouldUseLock()) return;
+ if (m_oldContext) {
+ m_oldContext->makeCurrent(m_oldSurface);
+ } else {
+ m_targetWidget->doneCurrent();
+ }
+}
+
diff --git a/libs/ui/opengl/KisOpenGLContextSwitchLock.h b/libs/ui/opengl/KisOpenGLContextSwitchLock.h
new file mode 100644
index 0000000000..feb722c3b2
--- /dev/null
+++ b/libs/ui/opengl/KisOpenGLContextSwitchLock.h
@@ -0,0 +1,33 @@
+/*
+ * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+
+#ifndef KISOPENGLCONTEXTSWITCHLOCK_H
+#define KISOPENGLCONTEXTSWITCHLOCK_H
+
+#include <kritaui_export.h>
+#include <KisAdaptedLock.h>
+
+class QOpenGLWidget;
+class QOpenGLContext;
+class QSurface;
+
+class KRITAUI_EXPORT KisOpenGLContextSwitchLockAdapter
+{
+public:
+ KisOpenGLContextSwitchLockAdapter(QOpenGLWidget *targetWidget);
+
+ void lock();
+ void unlock();
+
+private:
+ QOpenGLWidget *m_targetWidget {nullptr};
+ QOpenGLContext *m_oldContext {nullptr};
+ QSurface *m_oldSurface = {nullptr};
+};
+
+KIS_DECLARE_ADAPTED_LOCK(KisOpenGLContextSwitchLock, KisOpenGLContextSwitchLockAdapter)
+
+#endif // KISOPENGLCONTEXTSWITCHLOCK_H
diff --git a/libs/ui/opengl/kis_opengl_canvas2.cpp b/libs/ui/opengl/kis_opengl_canvas2.cpp
index 9744a39be6..4587004200 100644
--- a/libs/ui/opengl/kis_opengl_canvas2.cpp
+++ b/libs/ui/opengl/kis_opengl_canvas2.cpp
@@ -22,6 +22,7 @@
#include <QPointer>
#include "KisOpenGLModeProber.h"
+#include "KisOpenGLContextSwitchLock.h"
static bool OPENGL_SUCCESS = false;
@@ -151,11 +152,13 @@ KisOpenGLCanvas2::~KisOpenGLCanvas2()
void KisOpenGLCanvas2::setDisplayFilter(QSharedPointer<KisDisplayFilter> displayFilter)
{
+ KisOpenGLContextSwitchLock contextLock(this);
d->renderer->setDisplayFilter(displayFilter);
}
void KisOpenGLCanvas2::notifyImageColorSpaceChanged(const KoColorSpace *cs)
{
+ KisOpenGLContextSwitchLock contextLock(this);
d->renderer->notifyImageColorSpaceChanged(cs);
}
@@ -343,6 +346,7 @@ void KisOpenGLCanvas2::showEvent(QShowEvent *e)
void KisOpenGLCanvas2::setDisplayColorConverter(KisDisplayColorConverter *colorConverter)
{
+ KisOpenGLContextSwitchLock contextLock(this);
d->renderer->setDisplayColorConverter(colorConverter);
}
@@ -354,6 +358,7 @@ void KisOpenGLCanvas2::channelSelectionChanged(const QBitArray &channelFlags)
void KisOpenGLCanvas2::finishResizingImage(qint32 w, qint32 h)
{
+ KisOpenGLContextSwitchLock contextLock(this);
d->renderer->finishResizingImage(w, h);
}
@@ -370,31 +375,8 @@ QRect KisOpenGLCanvas2::updateCanvasProjection(KisUpdateInfoSP info)
QVector<QRect> KisOpenGLCanvas2::updateCanvasProjection(const QVector<KisUpdateInfoSP> &infoObjects)
{
-#if defined(Q_OS_MACOS) || defined(Q_OS_ANDROID)
- /**
- * On OSX openGL different (shared) contexts have different execution queues.
- * It means that the textures uploading and their painting can be easily reordered.
- * To overcome the issue, we should ensure that the textures are uploaded in the
- * same openGL context as the painting is done.
- */
-
- QOpenGLContext *oldContext = QOpenGLContext::currentContext();
- QSurface *oldSurface = oldContext ? oldContext->surface() : 0;
-
- this->makeCurrent();
-#endif
-
- QVector<QRect> result = KisCanvasWidgetBase::updateCanvasProjection(infoObjects);
-
-#if defined(Q_OS_MACOS) || defined(Q_OS_ANDROID)
- if (oldContext) {
- oldContext->makeCurrent(oldSurface);
- } else {
- this->doneCurrent();
- }
-#endif
-
- return result;
+ KisOpenGLContextSwitchLock contextLock(this);
+ return KisCanvasWidgetBase::updateCanvasProjection(infoObjects);
}
void KisOpenGLCanvas2::updateCanvasImage(const QRect &imageUpdateRect)