Krita Source Code Documentation
Loading...
Searching...
No Matches
KisOpenGLModeProber.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2017 Alvin Wong <alvinhochun@gmail.com>
3 * SPDX-FileCopyrightText: 2019 Dmitry Kazakov <dimula73@gmail.com>
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 */
7
9
10#include <config-hdr.h>
11#include <QApplication>
12#include <QOpenGLContext>
13#include <QOpenGLFunctions>
14#include <QWindow>
15#include <QColorSpace>
16#include <QScopeGuard>
17#include <KoConfig.h>
18
19#ifdef HAVE_HDR
20// for fetching the legacy 80-nits PQ space
21#include <KoColorProfileQuery.h>
22#endif
23
24#ifdef HAVE_X11
25#include <qpa/qplatformnativeinterface.h>
26#endif
27
28#include <QGlobalStatic>
30
32
33
37
42
44{
45 return s_instance;
46}
47
49{
50 return isFormatHDR(QSurfaceFormat::defaultFormat());
51}
52
54{
55 // TODO: use information provided by KisOpenGL instead
56 QOpenGLContext *sharedContext = QOpenGLContext::globalShareContext();
57 QSurfaceFormat format = sharedContext ? sharedContext->format() : QSurfaceFormat::defaultFormat();
58 return format;
59}
60
62{
64
65 const auto surfaceColorSpace =
67
68 if (surfaceColorSpace == KisSurfaceColorSpaceWrapper::sRGBColorSpace) {
69 // use the default one!
70#ifdef HAVE_HDR
71 } else if (surfaceColorSpace == KisSurfaceColorSpaceWrapper::scRGBColorSpace) {
73 } else if (surfaceColorSpace == KisSurfaceColorSpaceWrapper::bt2020PQColorSpace) {
74 // On Windows we use the legacy PQ profile, explicitly pinned to
75 // 80 nits. It is baked in the Qt's source code
77 query.hdrReferenceWhite = 80.0;
79#endif
80 }
81
82 return profile;
83}
84
85namespace {
86struct AppAttributeSetter
87{
88 AppAttributeSetter(Qt::ApplicationAttribute attribute, bool useOpenGLES)
89 : m_attribute(attribute),
90 m_oldValue(QCoreApplication::testAttribute(attribute))
91 {
92 QCoreApplication::setAttribute(attribute, useOpenGLES);
93 }
94
95 ~AppAttributeSetter() {
96 QCoreApplication::setAttribute(m_attribute, m_oldValue);
97 }
98
99private:
100 Qt::ApplicationAttribute m_attribute;
101 bool m_oldValue = false;
102};
103
104#ifdef HAVE_DBUS
105struct DisableDesktopAwareSetter
106{
107 DisableDesktopAwareSetter()
108 : m_oldValue(QGuiApplication::desktopSettingsAware())
109 {
110 QGuiApplication::setDesktopSettingsAware(false);
111 }
112
113 ~DisableDesktopAwareSetter() {
114 QGuiApplication::setDesktopSettingsAware(m_oldValue);
115 }
116
117private:
118 bool m_oldValue = false;
119};
120#endif
121
122struct SurfaceFormatSetter
123{
124 SurfaceFormatSetter(const QSurfaceFormat &format)
125 : m_oldFormat(QSurfaceFormat::defaultFormat())
126 {
127 QSurfaceFormat::setDefaultFormat(format);
128 }
129
130 ~SurfaceFormatSetter() {
131 QSurfaceFormat::setDefaultFormat(m_oldFormat);
132 }
133
134private:
135 QSurfaceFormat m_oldFormat;
136};
137
138
139struct EnvironmentSetter
140{
141 EnvironmentSetter(const QLatin1String &env, const QString &value)
142 : m_env(env)
143 {
144 if (!qEnvironmentVariableIsEmpty(m_env.latin1())) {
145 m_oldValue = qgetenv(env.latin1());
146 }
147 if (!value.isEmpty()) {
148 qputenv(env.latin1(), value.toLatin1());
149 } else {
150 qunsetenv(env.latin1());
151 }
152 }
153
154 ~EnvironmentSetter() {
155 if (m_oldValue) {
156 qputenv(m_env.latin1(), (*m_oldValue).toLatin1());
157 } else {
158 qunsetenv(m_env.latin1());
159 }
160 }
161
162private:
163 const QLatin1String m_env;
164 boost::optional<QString> m_oldValue;
165};
166
167}
168
169boost::optional<KisOpenGLModeProber::Result>
171 bool adjustGlobalState)
172{
173 const QSurfaceFormat &format = rendererConfig.format;
174
175 dbgOpenGL << "Probing format" << rendererConfig.rendererId() << rendererConfig.angleRenderer
176 << rendererConfig.format;
177
178 QScopedPointer<AppAttributeSetter> sharedContextSetter;
179 QScopedPointer<AppAttributeSetter> glSetter;
180 QScopedPointer<AppAttributeSetter> glesSetter;
181 QScopedPointer<SurfaceFormatSetter> formatSetter;
182 QScopedPointer<EnvironmentSetter> rendererSetter;
183 QScopedPointer<EnvironmentSetter> portalSetter;
184#ifdef HAVE_DBUS
185 QScopedPointer<DisableDesktopAwareSetter> desktopAwareSetter;
186#endif
187 QScopedPointer<QGuiApplication> application;
188
189 int argc = 1;
190 QByteArray probeAppName("krita");
191 char *argv = probeAppName.data();
192
193
194 if (adjustGlobalState) {
195 sharedContextSetter.reset(new AppAttributeSetter(Qt::AA_ShareOpenGLContexts, false));
196
197 if (format.renderableType() != QSurfaceFormat::DefaultRenderableType) {
198 glSetter.reset(new AppAttributeSetter(Qt::AA_UseDesktopOpenGL, format.renderableType() != QSurfaceFormat::OpenGLES));
199 glesSetter.reset(new AppAttributeSetter(Qt::AA_UseOpenGLES, format.renderableType() == QSurfaceFormat::OpenGLES));
200 }
201
202 if (!qEnvironmentVariableIsSet("QT_ANGLE_PLATFORM")) {
203 rendererSetter.reset(new EnvironmentSetter(QLatin1String("QT_ANGLE_PLATFORM"), angleRendererToString(rendererConfig.angleRenderer)));
204 }
205 portalSetter.reset(new EnvironmentSetter(QLatin1String("QT_NO_XDG_DESKTOP_PORTAL"), QLatin1String("1")));
206 formatSetter.reset(new SurfaceFormatSetter(format));
207
208#ifdef HAVE_DBUS
209 if (!qEnvironmentVariableIsSet("KRITA_DISABLE_DESKTOP_UNAWARE_PROBING_WORKAROUND")) {
210 // When QGuiApplication is created, it also creates platform-specific GUI
211 // elements, like QPlatformMenu and QPlatformSystemTrayIcon. These classes
212 // can utilize DBus for communicating the platform. If we create a DBus
213 // connection, but do not start an event loop, then the connection will
214 // stuck in a "suspended delivery" state and will not be able to process
215 // anything. See bug 408015 and bug 515889
216 desktopAwareSetter.reset(new DisableDesktopAwareSetter());
217 }
218#endif /* HAVE_DBUS */
219
220 application.reset(new QGuiApplication(argc, &argv));
221 }
222
223#ifdef HAVE_DBUS
224 if (!qEnvironmentVariableIsSet("KRITA_DISABLE_TEMPORARY_APP_EVENTS_PROCESSING_WORKAROUND")) {
225 // If an application has been created, force it to process
226 // all the queued events and exit. Some internal
227 // Qt systems' initialization process rely on the queued
228 // events to be processed. See bug 515889.
229 QScopeGuard forceEventProcessingGuard([&]() {
230 if (!application)
231 return;
232
233 QMetaObject::invokeMethod(
234 application.data(),
235 [app = application.data()]() {
236 app->quit();
237 },
238 Qt::QueuedConnection);
239
240 application->exec();
241 });
242 }
243#endif /* HAVE_DBUS */
244
245 QWindow surface;
246 surface.setFormat(format);
247 surface.setSurfaceType(QSurface::OpenGLSurface);
248 surface.create();
249 QOpenGLContext context;
250 context.setFormat(format);
251
252
253 if (!context.create()) {
254 dbgOpenGL << "OpenGL context cannot be created";
255 return boost::none;
256 }
257 if (!context.isValid()) {
258 dbgOpenGL << "OpenGL context is not valid while checking Qt's OpenGL status";
259 return boost::none;
260 }
261 if (!context.makeCurrent(&surface)) {
262 dbgOpenGL << "OpenGL context cannot be made current";
263 return boost::none;
264 }
265
267 KisSurfaceColorSpaceWrapper::fromQtColorSpace(context.format().colorSpace()),
268 KisSurfaceColorSpaceWrapper::fromQtColorSpace(format.colorSpace()))) {
269
270 dbgOpenGL << "Failed to create an OpenGL context with requested color space. Requested:" << format.colorSpace() << "Actual:" << context.format().colorSpace();
271 return boost::none;
272 }
273
274 if (format.redBufferSize() > 0 && format.greenBufferSize() > 0 && format.blueBufferSize() > 0
275 && (context.format().redBufferSize() != format.redBufferSize()
276 || context.format().greenBufferSize() != format.greenBufferSize()
277 || context.format().blueBufferSize() != format.blueBufferSize())) {
278
279 dbgOpenGL << "Failed to create an OpenGL context with requested bit depth. Requested:" << format.redBufferSize()
280 << "Actual:" << context.format().redBufferSize();
281 return boost::none;
282 }
283
284 Result result(context);
285
286 dbgOpenGL << "Probe returned" << result.rendererString() << result.driverVersionString() << result.isOpenGLES();
287
288 return result;
289}
290
299
300void KisOpenGLModeProber::initSurfaceFormatFromConfig(std::pair<KisSurfaceColorSpaceWrapper, int> rootSurfaceFormat,
301 QSurfaceFormat *format)
302{
303#ifdef HAVE_HDR
307 if (rootSurfaceFormat.first == KisSurfaceColorSpaceWrapper::bt2020PQColorSpace) {
308 KIS_SAFE_ASSERT_RECOVER_NOOP(rootSurfaceFormat.second == 10);
309 format->setRedBufferSize(10);
310 format->setGreenBufferSize(10);
311 format->setBlueBufferSize(10);
312 format->setAlphaBufferSize(2);
314 } else if (rootSurfaceFormat.first == KisSurfaceColorSpaceWrapper::scRGBColorSpace) {
315 KIS_SAFE_ASSERT_RECOVER_NOOP(rootSurfaceFormat.second == 16);
316 format->setRedBufferSize(16);
317 format->setGreenBufferSize(16);
318 format->setBlueBufferSize(16);
319 format->setAlphaBufferSize(16);
321 } else {
322 KIS_SAFE_ASSERT_RECOVER_NOOP(rootSurfaceFormat.second == 8);
323 format->setRedBufferSize(8);
324 format->setGreenBufferSize(8);
325 format->setBlueBufferSize(8);
326 format->setAlphaBufferSize(8);
327 // TODO: check if we can use real sRGB space here
328 format->setColorSpace(KisSurfaceColorSpaceWrapper());
329 }
330#else
335 if (rootSurfaceFormat.first == KisSurfaceColorSpaceWrapper::bt2020PQColorSpace) {
336 qWarning() << "WARNING: Bt.2020 PQ surface type is not supported by this build of Krita";
337 rootSurfaceFormat.first = KisSurfaceColorSpaceWrapper::DefaultColorSpace;
338 } else if (rootSurfaceFormat.first == KisSurfaceColorSpaceWrapper::scRGBColorSpace) {
339 qWarning() << "WARNING: scRGB surface type is not supported by this build of Krita";
340 rootSurfaceFormat.first = KisSurfaceColorSpaceWrapper::DefaultColorSpace;
341 } else {
343 KIS_SAFE_ASSERT_RECOVER_NOOP(rootSurfaceFormat.second == 8 || rootSurfaceFormat.second == 10);
344
345 if (rootSurfaceFormat.second == 10) {
346 format->setRedBufferSize(10);
347 format->setGreenBufferSize(10);
348 format->setBlueBufferSize(10);
349 format->setAlphaBufferSize(2);
350 // TODO: check if we can use real sRGB space here
351 format->setColorSpace(KisSurfaceColorSpaceWrapper());
352 } else {
353 format->setRedBufferSize(8);
354 format->setGreenBufferSize(8);
355 format->setBlueBufferSize(8);
356 format->setAlphaBufferSize(8);
357 // TODO: check if we can use real sRGB space here
358 format->setColorSpace(KisSurfaceColorSpaceWrapper());
359 }
360 }
361#endif
362}
363
364bool KisOpenGLModeProber::isFormatHDR(const QSurfaceFormat &format)
365{
366#ifdef HAVE_HDR
367
368 bool isBt2020PQ =
370 format.redBufferSize() == 10 &&
371 format.greenBufferSize() == 10 &&
372 format.blueBufferSize() == 10 &&
373 format.alphaBufferSize() == 2;
374
375 bool isBt709G10 =
376 format.colorSpace() == KisSurfaceColorSpaceWrapper::makeSCRGBColorSpace() &&
377 format.redBufferSize() == 16 &&
378 format.greenBufferSize() == 16 &&
379 format.blueBufferSize() == 16 &&
380 format.alphaBufferSize() == 16;
381
382 return isBt2020PQ || isBt709G10;
383#else
384 Q_UNUSED(format);
385 return false;
386#endif
387}
388
390{
391 QString value;
392
393 switch (renderer) {
395 break;
397 value = "d3d9";
398 break;
400 value = "d3d11";
401 break;
403 value = "warp";
404 break;
405 };
406
407 return value;
408}
409
410KisOpenGLModeProber::Result::Result(QOpenGLContext &context) {
411 if (!context.isValid()) {
412 return;
413 }
414
415 QOpenGLFunctions *funcs = context.functions(); // funcs is ready to be used
416
417 m_rendererString = QString(reinterpret_cast<const char *>(funcs->glGetString(GL_RENDERER)));
418 m_driverVersionString = QString(reinterpret_cast<const char *>(funcs->glGetString(GL_VERSION)));
419 m_vendorString = QString(reinterpret_cast<const char *>(funcs->glGetString(GL_VENDOR)));
420 m_shadingLanguageString = QString(reinterpret_cast<const char *>(funcs->glGetString(GL_SHADING_LANGUAGE_VERSION)));
421 m_glMajorVersion = context.format().majorVersion();
422 m_glMinorVersion = context.format().minorVersion();
423 m_supportsDeprecatedFunctions = (context.format().options() & QSurfaceFormat::DeprecatedFunctions);
424 m_isOpenGLES = context.isOpenGLES();
425 m_format = context.format();
426 m_supportsFBO = context.functions()->hasOpenGLFeature(QOpenGLFunctions::Framebuffers);
427
429 m_glMajorVersion >= 3 ||
430 context.hasExtension("GL_OES_mapbuffer") ||
431 context.hasExtension("GL_EXT_map_buffer_range") ||
432 context.hasExtension("GL_ARB_map_buffer_range");
433
435 ((m_glMajorVersion >= 4 && m_glMinorVersion >= 3) ||
436 context.hasExtension("GL_ARB_invalidate_subdata"));
437 m_supportsLod = context.format().majorVersion() >= 3 || (m_isOpenGLES && context.hasExtension("GL_EXT_shader_texture_lod"));
438
439#ifdef HAVE_X11
440 if (QApplication::platformName() == "xcb") {
441 QPlatformNativeInterface *native = qApp->platformNativeInterface();
442 if (native->nativeResourceFunctionForContext("eglcontext")) {
444 } else if (native->nativeResourceFunctionForContext("glxcontext")) {
446 } else {
447 qWarning() << "WARNING: Failed to detect QXcbGlIntegration type!";
448 }
449 }
450#endif /* HAVE_X11 */
451
452 m_extensions = context.extensions();
453 // Remove empty name extension that sometimes appears on NVIDIA output
454 m_extensions.remove("");
455}
float value(const T *src, size_t ch)
Q_GLOBAL_STATIC(KisStoragePluginRegistry, s_instance)
@ PRIMARIES_ITU_R_BT_2020_2_AND_2100_0
@ TRC_ITU_R_BT_2100_0_PQ
std::optional< KisOpenGL::XcbGLProviderProtocol > m_xcbGlProviderProtocol
Result(QOpenGLContext &context)
static KisOpenGLModeProber * instance()
boost::optional< Result > probeFormat(const KisOpenGL::RendererConfig &rendererConfig, bool adjustGlobalState=true)
const KoColorProfile * rootSurfaceColorProfile() const
QSurfaceFormat surfaceformatInUse() const
static bool isFormatHDR(const QSurfaceFormat &format)
static void initSurfaceFormatFromConfig(std::pair< KisSurfaceColorSpaceWrapper, int > rootSurfaceFormat, QSurfaceFormat *format)
static QString angleRendererToString(KisOpenGL::AngleRenderer renderer)
static bool fuzzyCompareColorSpaces(const KisSurfaceColorSpaceWrapper &lhs, const KisSurfaceColorSpaceWrapper &rhs)
@ AngleRendererD3d11Warp
Definition kis_opengl.h:53
@ AngleRendererD3d11
Definition kis_opengl.h:51
@ AngleRendererD3d9
Definition kis_opengl.h:52
@ AngleRendererDefault
Definition kis_opengl.h:50
static constexpr KisSurfaceColorSpaceWrapper makeSCRGBColorSpace()
static KisSurfaceColorSpaceWrapper fromQtColorSpace(const QColorSpace &colorSpace)
static constexpr KisSurfaceColorSpaceWrapper makeBt2020PQColorSpace()
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
Definition kis_assert.h:130
#define dbgOpenGL
Definition kis_debug.h:63
#define GL_RENDERER
AngleRenderer angleRenderer
Definition kis_opengl.h:58
QSurfaceFormat format
Definition kis_opengl.h:57
OpenGLRenderer rendererId() const
The KoColorProfileQuery struct.
std::optional< double > hdrReferenceWhite
CICP-compatible enum value representing the transfer function.
static KoColorSpaceRegistry * instance()
const KoColorProfile * profileFor(const KoColorProfileQuery &query, const bool generate=true) const
profileFor tries to find the profile that matches these characteristics, if no such profile is found,...
const KoColorProfile * p709G10Profile() const
const KoColorProfile * p709SRGBProfile() const