10#include <config-hdr.h>
11#include <QApplication>
12#include <QOpenGLContext>
13#include <QOpenGLFunctions>
25#include <qpa/qplatformnativeinterface.h>
28#include <QGlobalStatic>
50 return isFormatHDR(QSurfaceFormat::defaultFormat());
56 QOpenGLContext *sharedContext = QOpenGLContext::globalShareContext();
57 QSurfaceFormat format = sharedContext ? sharedContext->format() : QSurfaceFormat::defaultFormat();
65 const auto surfaceColorSpace =
86struct AppAttributeSetter
88 AppAttributeSetter(Qt::ApplicationAttribute attribute,
bool useOpenGLES)
89 : m_attribute(attribute),
90 m_oldValue(QCoreApplication::testAttribute(attribute))
92 QCoreApplication::setAttribute(attribute, useOpenGLES);
95 ~AppAttributeSetter() {
96 QCoreApplication::setAttribute(m_attribute, m_oldValue);
100 Qt::ApplicationAttribute m_attribute;
101 bool m_oldValue =
false;
105struct DisableDesktopAwareSetter
107 DisableDesktopAwareSetter()
108 : m_oldValue(QGuiApplication::desktopSettingsAware())
110 QGuiApplication::setDesktopSettingsAware(
false);
113 ~DisableDesktopAwareSetter() {
114 QGuiApplication::setDesktopSettingsAware(m_oldValue);
118 bool m_oldValue =
false;
122struct SurfaceFormatSetter
124 SurfaceFormatSetter(
const QSurfaceFormat &format)
125 : m_oldFormat(QSurfaceFormat::defaultFormat())
127 QSurfaceFormat::setDefaultFormat(format);
130 ~SurfaceFormatSetter() {
131 QSurfaceFormat::setDefaultFormat(m_oldFormat);
135 QSurfaceFormat m_oldFormat;
139struct EnvironmentSetter
141 EnvironmentSetter(
const QLatin1String &env,
const QString &
value)
144 if (!qEnvironmentVariableIsEmpty(m_env.latin1())) {
145 m_oldValue = qgetenv(env.latin1());
147 if (!
value.isEmpty()) {
148 qputenv(env.latin1(),
value.toLatin1());
150 qunsetenv(env.latin1());
154 ~EnvironmentSetter() {
156 qputenv(m_env.latin1(), (*m_oldValue).toLatin1());
158 qunsetenv(m_env.latin1());
163 const QLatin1String m_env;
164 boost::optional<QString> m_oldValue;
169boost::optional<KisOpenGLModeProber::Result>
171 bool adjustGlobalState)
173 const QSurfaceFormat &format = rendererConfig.
format;
178 QScopedPointer<AppAttributeSetter> sharedContextSetter;
179 QScopedPointer<AppAttributeSetter> glSetter;
180 QScopedPointer<AppAttributeSetter> glesSetter;
181 QScopedPointer<SurfaceFormatSetter> formatSetter;
182 QScopedPointer<EnvironmentSetter> rendererSetter;
183 QScopedPointer<EnvironmentSetter> portalSetter;
185 QScopedPointer<DisableDesktopAwareSetter> desktopAwareSetter;
187 QScopedPointer<QGuiApplication> application;
190 QByteArray probeAppName(
"krita");
191 char *argv = probeAppName.data();
194 if (adjustGlobalState) {
195 sharedContextSetter.reset(
new AppAttributeSetter(Qt::AA_ShareOpenGLContexts,
false));
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));
202 if (!qEnvironmentVariableIsSet(
"QT_ANGLE_PLATFORM")) {
205 portalSetter.reset(
new EnvironmentSetter(QLatin1String(
"QT_NO_XDG_DESKTOP_PORTAL"), QLatin1String(
"1")));
206 formatSetter.reset(
new SurfaceFormatSetter(format));
209 if (!qEnvironmentVariableIsSet(
"KRITA_DISABLE_DESKTOP_UNAWARE_PROBING_WORKAROUND")) {
216 desktopAwareSetter.reset(
new DisableDesktopAwareSetter());
220 application.reset(
new QGuiApplication(argc, &argv));
224 if (!qEnvironmentVariableIsSet(
"KRITA_DISABLE_TEMPORARY_APP_EVENTS_PROCESSING_WORKAROUND")) {
229 QScopeGuard forceEventProcessingGuard([&]() {
233 QMetaObject::invokeMethod(
235 [app = application.data()]() {
238 Qt::QueuedConnection);
246 surface.setFormat(format);
247 surface.setSurfaceType(QSurface::OpenGLSurface);
249 QOpenGLContext context;
250 context.setFormat(format);
253 if (!context.create()) {
254 dbgOpenGL <<
"OpenGL context cannot be created";
257 if (!context.isValid()) {
258 dbgOpenGL <<
"OpenGL context is not valid while checking Qt's OpenGL status";
261 if (!context.makeCurrent(&surface)) {
262 dbgOpenGL <<
"OpenGL context cannot be made current";
270 dbgOpenGL <<
"Failed to create an OpenGL context with requested color space. Requested:" << format.colorSpace() <<
"Actual:" << context.format().colorSpace();
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())) {
279 dbgOpenGL <<
"Failed to create an OpenGL context with requested bit depth. Requested:" << format.redBufferSize()
280 <<
"Actual:" << context.format().redBufferSize();
301 QSurfaceFormat *format)
309 format->setRedBufferSize(10);
310 format->setGreenBufferSize(10);
311 format->setBlueBufferSize(10);
312 format->setAlphaBufferSize(2);
316 format->setRedBufferSize(16);
317 format->setGreenBufferSize(16);
318 format->setBlueBufferSize(16);
319 format->setAlphaBufferSize(16);
323 format->setRedBufferSize(8);
324 format->setGreenBufferSize(8);
325 format->setBlueBufferSize(8);
326 format->setAlphaBufferSize(8);
336 qWarning() <<
"WARNING: Bt.2020 PQ surface type is not supported by this build of Krita";
339 qWarning() <<
"WARNING: scRGB surface type is not supported by this build of Krita";
345 if (rootSurfaceFormat.second == 10) {
346 format->setRedBufferSize(10);
347 format->setGreenBufferSize(10);
348 format->setBlueBufferSize(10);
349 format->setAlphaBufferSize(2);
353 format->setRedBufferSize(8);
354 format->setGreenBufferSize(8);
355 format->setBlueBufferSize(8);
356 format->setAlphaBufferSize(8);
370 format.redBufferSize() == 10 &&
371 format.greenBufferSize() == 10 &&
372 format.blueBufferSize() == 10 &&
373 format.alphaBufferSize() == 2;
377 format.redBufferSize() == 16 &&
378 format.greenBufferSize() == 16 &&
379 format.blueBufferSize() == 16 &&
380 format.alphaBufferSize() == 16;
382 return isBt2020PQ || isBt709G10;
411 if (!context.isValid()) {
415 QOpenGLFunctions *funcs = context.functions();
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)));
426 m_supportsFBO = context.functions()->hasOpenGLFeature(QOpenGLFunctions::Framebuffers);
430 context.hasExtension(
"GL_OES_mapbuffer") ||
431 context.hasExtension(
"GL_EXT_map_buffer_range") ||
432 context.hasExtension(
"GL_ARB_map_buffer_range");
436 context.hasExtension(
"GL_ARB_invalidate_subdata"));
437 m_supportsLod = context.format().majorVersion() >= 3 || (
m_isOpenGLES && context.hasExtension(
"GL_EXT_shader_texture_lod"));
440 if (QApplication::platformName() ==
"xcb") {
441 QPlatformNativeInterface *native = qApp->platformNativeInterface();
442 if (native->nativeResourceFunctionForContext(
"eglcontext")) {
444 }
else if (native->nativeResourceFunctionForContext(
"glxcontext")) {
447 qWarning() <<
"WARNING: Failed to detect QXcbGlIntegration type!";
float value(const T *src, size_t ch)
Q_GLOBAL_STATIC(KisStoragePluginRegistry, s_instance)
@ PRIMARIES_ITU_R_BT_2020_2_AND_2100_0
std::optional< KisOpenGL::XcbGLProviderProtocol > m_xcbGlProviderProtocol
QString rendererString() const
bool m_supportsBufferInvalidation
bool m_supportsBufferMapping
Result(QOpenGLContext &context)
QSet< QByteArray > m_extensions
QString m_driverVersionString
QString driverVersionString() const
QString m_shadingLanguageString
bool m_supportsDeprecatedFunctions
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)
static constexpr KisSurfaceColorSpaceWrapper makeSCRGBColorSpace()
static KisSurfaceColorSpaceWrapper fromQtColorSpace(const QColorSpace &colorSpace)
static constexpr KisSurfaceColorSpaceWrapper makeBt2020PQColorSpace()
#define KIS_SAFE_ASSERT_RECOVER_NOOP(cond)
AngleRenderer angleRenderer
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