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++
Implement automatic sRGB assignment to native windows
Dmitry Kazakov469ffd7866ed
diff --git a/libs/ui/CMakeLists.txt b/libs/ui/CMakeLists.txtindex 3c1791718c..d578bbe051 100644--- a/libs/ui/CMakeLists.txt+++ b/libs/ui/CMakeLists.txt@@ -413,6 +413,7 @@ set(kritaui_LIB_SRCSkcanvaspreview.cckis_abstract_perspective_grid.cpp+ KisPlatformPluginInterfaceFactory.cppKisApplication.cppKisAutoSaveRecoveryDialog.cppKisDetailsPane.cppdiff --git a/libs/ui/KisApplication.cpp b/libs/ui/KisApplication.cppindex c11701b558..d6e5f6d518 100644--- a/libs/ui/KisApplication.cpp+++ b/libs/ui/KisApplication.cpp@@ -117,9 +117,17 @@#include <config-seexpr.h>#include <config-safe-asserts.h>-#include <kpluginfactory.h>#include <input/KisExtendedModifiersMapperPluginInterface.h>+#include <KisPlatformPluginInterfaceFactory.h>+#include <config-use-surface-color-management-api.h>++#if KRITA_USE_SURFACE_COLOR_MANAGEMENT_API++#include <QPlatformSurfaceEvent>+#include <KisSRGBSurfaceColorSpaceManager.h>++#endif /* KRITA_USE_SURFACE_COLOR_MANAGEMENT_API */namespace {const QTime appStartTime(QTime::currentTime());@@ -260,18 +268,49 @@ KisApplication::KisApplication(const QString &key, int &argc, char **argv)* Load platform plugin for modifiers fetching*/{- KPluginFactory *factory = KoPluginLoader::instance()->loadSinglePlugin(- std::make_pair("X-Krita-PlatformId", QGuiApplication::platformName()),- "Krita/PlatformPlugin");-- if (factory) {- d->extendedModifiersPluginInterface.reset(factory->create<KisExtendedModifiersMapperPluginInterface>());- }+ d->extendedModifiersPluginInterface.reset(KisPlatformPluginInterfaceFactory::createExtendedModifiersMapper());}// store the style nameqApp->setProperty(currentUnderlyingStyleNameProperty, style()->objectName());KisSynchronizedConnectionBase::registerSynchronizedEventBarrier(std::bind(&KisApplication::processPostponedSynchronizationEvents, this));+++#if KRITA_USE_SURFACE_COLOR_MANAGEMENT_API++ /**+ * Automatically assign sRGB color space to all Krita windows,+ * which are not marked with a special tag.+ */+ struct PlatformWindowCreationFilter : QObject+ {+ using QObject::QObject;++ bool eventFilter(QObject *watched, QEvent *event) override {+ if (event->type() == QEvent::PlatformSurface) {+ QWidget *widget = qobject_cast<QWidget*>(watched);+ if (!widget) return false;++ /**+ * Check for the special tag that is set for the windows that handle+ * their color space themselves+ */+ if (watched->property("krita_skip_srgb_surface_manager_assignment").toBool()) {+ return false;+ }++ QPlatformSurfaceEvent *surfaceEvent = static_cast<QPlatformSurfaceEvent*>(event);+ if (surfaceEvent->surfaceEventType() == QPlatformSurfaceEvent::SurfaceCreated) {+ KisSRGBSurfaceColorSpaceManager::tryCreateForCurrentPlatform(widget);+ }+ }++ return false;+ }+ };++ this->installEventFilter(new PlatformWindowCreationFilter(this));+#endif /* KRITA_USE_SURFACE_COLOR_MANAGEMENT_API */}#if defined(Q_OS_WIN) && defined(ENV32BIT)diff --git a/libs/ui/KisMainWindow.cpp b/libs/ui/KisMainWindow.cppindex c3b507c52f..a62b1ec542 100644--- a/libs/ui/KisMainWindow.cpp+++ b/libs/ui/KisMainWindow.cpp@@ -160,9 +160,6 @@#if KRITA_USE_SURFACE_COLOR_MANAGEMENT_API-// surface color management-#include <kpluginfactory.h>-#include <surfacecolormanagement/KisSurfaceColorManagerInterface.h>#include <KisSRGBSurfaceColorSpaceManager.h>#endif /* KRITA_USE_SURFACE_COLOR_MANAGEMENT_API */@@ -362,6 +359,10 @@ KisMainWindow::KisMainWindow(QUuid uuid): KXmlGuiWindow(), d(new Private(this, uuid)){+ // Krita's main window handles sRGB space itself, exclude that+ // from auto-srgb-assignment+ setProperty("krita_skip_srgb_surface_manager_assignment", true);+KAcceleratorManager::setNoAccel(this);d->workspacemodel = new KisResourceModel(ResourceType::Workspaces, this);@@ -677,25 +678,8 @@ KisMainWindow::KisMainWindow(QUuid uuid)* Load platform plugin for surface color management and set* the surface color space to sRGB exactly*/- {- KPluginFactory *factory = KoPluginLoader::instance()->loadSinglePlugin(- std::make_pair("X-Krita-PlatformId", QGuiApplication::platformName()),- "Krita/PlatformPlugin");-- if (factory) {- QWindow *nativeWindow = this->window()->windowHandle();- KIS_SAFE_ASSERT_RECOVER_RETURN(nativeWindow);-- QVariantList args = {QVariant::fromValue(nativeWindow)};- std::unique_ptr<KisSurfaceColorManagerInterface> iface(- factory->create<KisSurfaceColorManagerInterface>(nullptr, args));-- if (iface) {- d->surfaceColorSpaceManager = new KisSRGBSurfaceColorSpaceManager(iface.release(), this);- }- }- }+ d->surfaceColorSpaceManager = KisSRGBSurfaceColorSpaceManager::tryCreateForCurrentPlatform(this);#endif /* KRITA_USE_SURFACE_COLOR_MANAGEMENT_API */}diff --git a/libs/ui/KisPlatformPluginInterfaceFactory.cpp b/libs/ui/KisPlatformPluginInterfaceFactory.cppnew file mode 100644index 0000000000..53f42d2269--- /dev/null+++ b/libs/ui/KisPlatformPluginInterfaceFactory.cpp@@ -0,0 +1,52 @@+/*+ * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#include "KisPlatformPluginInterfaceFactory.h"++#include <KoPluginLoader.h>+#include <kpluginfactory.h>+#include <QApplication>+#include <QWindow>++#include <kis_assert.h>++#include <input/KisExtendedModifiersMapperPluginInterface.h>++#if KRITA_USE_SURFACE_COLOR_MANAGEMENT_API++#include <surfacecolormanagement/KisSurfaceColorManagerInterface.h>++KisSurfaceColorManagerInterface* KisPlatformPluginInterfaceFactory::createSurfaceColorManager(QWindow *nativeWindow)+{+ KPluginFactory *factory = KoPluginLoader::instance()->loadSinglePlugin(+ std::make_pair("X-Krita-PlatformId", QApplication::platformName()),+ "Krita/PlatformPlugin");++ if (factory) {+ KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(nativeWindow, nullptr);++ QVariantList args = {QVariant::fromValue(nativeWindow)};++ return factory->create<KisSurfaceColorManagerInterface>(nullptr, args);+ }++ return nullptr;+}++#endif++KisExtendedModifiersMapperPluginInterface* KisPlatformPluginInterfaceFactory::createExtendedModifiersMapper()+{+ KPluginFactory *factory = KoPluginLoader::instance()->loadSinglePlugin(+ std::make_pair("X-Krita-PlatformId", QApplication::platformName()),+ "Krita/PlatformPlugin");++ if (factory) {+ return factory->create<KisExtendedModifiersMapperPluginInterface>();+ }++ return nullptr;+}diff --git a/libs/ui/KisPlatformPluginInterfaceFactory.h b/libs/ui/KisPlatformPluginInterfaceFactory.hnew file mode 100644index 0000000000..83afc4141a--- /dev/null+++ b/libs/ui/KisPlatformPluginInterfaceFactory.h@@ -0,0 +1,37 @@+/*+ * SPDX-FileCopyrightText: 2025 Dmitry Kazakov <[email protected]>+ *+ * SPDX-License-Identifier: GPL-2.0-or-later+ */++#ifndef KISPLATFORMPLUGININTERFACEFACTORY_H+#define KISPLATFORMPLUGININTERFACEFACTORY_H++#include <kritaui_export.h>++#include <config-use-surface-color-management-api.h>++class QWindow;+class KisSurfaceColorManagerInterface;+class KisExtendedModifiersMapperPluginInterface;++class KRITAUI_EXPORT KisPlatformPluginInterfaceFactory {+public:+#if KRITA_USE_SURFACE_COLOR_MANAGEMENT_API+ /**+ * Creates an instance of the color manager interface using the platform plugin+ *+ * If the current platform plugin doesn't profide this interface, returns nullptr+ */+ static KisSurfaceColorManagerInterface* createSurfaceColorManager(QWindow *nativeWindow);+#endif++ /**+ * Creates an instance of the extended modifiers interface using the platform plugin+ *+ * If the current platform plugin doesn't profide this interface, returns nullptr+ */+ static KisExtendedModifiersMapperPluginInterface* createExtendedModifiersMapper();+};++#endif // KISPLATFORMPLUGININTERFACEFACTORY_Hdiff --git a/libs/ui/KisSRGBSurfaceColorSpaceManager.cpp b/libs/ui/KisSRGBSurfaceColorSpaceManager.cppindex b0b806cc20..5b7471f861 100644--- a/libs/ui/KisSRGBSurfaceColorSpaceManager.cpp+++ b/libs/ui/KisSRGBSurfaceColorSpaceManager.cpp@@ -6,10 +6,14 @@#include "KisSRGBSurfaceColorSpaceManager.h"+#include <QWidget>+#include <QWindow>+#include <kis_assert.h>#include <kis_config.h>#include <kis_config_notifier.h>+#include <KisPlatformPluginInterfaceFactory.h>#include <surfacecolormanagement/KisSurfaceColorManagerInterface.h>@@ -174,4 +178,19 @@ void KisSRGBSurfaceColorSpaceManager::reinitializeSurfaceDescription() {}}+KisSRGBSurfaceColorSpaceManager* KisSRGBSurfaceColorSpaceManager::tryCreateForCurrentPlatform(QWidget *widget)+{+ QWindow *nativeWindow = widget->windowHandle();+ KIS_SAFE_ASSERT_RECOVER_RETURN_VALUE(widget->windowHandle(), nullptr);++ std::unique_ptr<KisSurfaceColorManagerInterface> iface(+ KisPlatformPluginInterfaceFactory::createSurfaceColorManager(widget->windowHandle()));++ if (iface) {+ return new KisSRGBSurfaceColorSpaceManager(iface.release(), nativeWindow);+ }++ return nullptr;+}+#include <moc_KisSRGBSurfaceColorSpaceManager.cpp>diff --git a/libs/ui/KisSRGBSurfaceColorSpaceManager.h b/libs/ui/KisSRGBSurfaceColorSpaceManager.hindex eff49eae4a..84739f26b0 100644--- a/libs/ui/KisSRGBSurfaceColorSpaceManager.h+++ b/libs/ui/KisSRGBSurfaceColorSpaceManager.h@@ -23,6 +23,8 @@ public:QString colorManagementReport() const;QString osPreferredColorSpaceReport() const;+ static KisSRGBSurfaceColorSpaceManager* tryCreateForCurrentPlatform(QWidget *widget);+protected:static KisSurfaceColorimetry::RenderIntent calculateConfigIntent();diff --git a/libs/ui/opengl/kis_opengl_canvas2.cpp b/libs/ui/opengl/kis_opengl_canvas2.cppindex e5d875192d..6c1a80af9b 100644--- a/libs/ui/opengl/kis_opengl_canvas2.cpp+++ b/libs/ui/opengl/kis_opengl_canvas2.cpp@@ -82,6 +82,8 @@ KisOpenGLCanvas2::KisOpenGLCanvas2(KisCanvas2 *canvas,, KisCanvasWidgetBase(canvas, coordinatesConverter), d(new Private()){+ setProperty("krita_skip_srgb_surface_manager_assignment", true);+KisConfig cfg(false);cfg.setCanvasState("OPENGL_STARTED");