Changeset View
Changeset View
Standalone View
Standalone View
libs/ui/opengl/KisOpenGLModeProber.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2017 Alvin Wong <alvinhochun@gmail.com> | ||||
3 | * Copyright (c) 2019 Dmitry Kazakov <dimula73@gmail.com> | ||||
4 | * | ||||
5 | * This program is free software; you can redistribute it and/or modify | ||||
6 | * it under the terms of the GNU General Public License as published by | ||||
7 | * the Free Software Foundation; either version 2 of the License, or | ||||
8 | * (at your option) any later version. | ||||
9 | * | ||||
10 | * This program is distributed in the hope that it will be useful, | ||||
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
13 | * GNU General Public License for more details. | ||||
14 | * | ||||
15 | * You should have received a copy of the GNU General Public License | ||||
16 | * along with this program; if not, write to the Free Software | ||||
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||||
18 | */ | ||||
19 | | ||||
20 | #include "KisOpenGLModeProber.h" | ||||
21 | | ||||
22 | #include <config-hdr.h> | ||||
23 | #include <QApplication> | ||||
24 | #include <QOpenGLContext> | ||||
25 | #include <QOpenGLFunctions> | ||||
26 | #include <QWindow> | ||||
27 | | ||||
28 | #include <QGlobalStatic> | ||||
29 | Q_GLOBAL_STATIC(KisOpenGLModeProber, s_instance) | ||||
30 | | ||||
31 | | ||||
32 | KisOpenGLModeProber::KisOpenGLModeProber() | ||||
33 | { | ||||
34 | } | ||||
35 | | ||||
36 | KisOpenGLModeProber::~KisOpenGLModeProber() | ||||
37 | { | ||||
38 | | ||||
39 | } | ||||
40 | | ||||
41 | KisOpenGLModeProber *KisOpenGLModeProber::instance() | ||||
42 | { | ||||
43 | return s_instance; | ||||
44 | } | ||||
45 | | ||||
46 | bool KisOpenGLModeProber::useHDRMode() const | ||||
47 | { | ||||
48 | return isFormatHDR(QSurfaceFormat::defaultFormat()); | ||||
49 | } | ||||
50 | | ||||
51 | QSurfaceFormat KisOpenGLModeProber::surfaceformatInUse() const | ||||
52 | { | ||||
53 | // TODO: use information provided by KisOpenGL instead | ||||
54 | QOpenGLContext *sharedContext = QOpenGLContext::globalShareContext(); | ||||
55 | QSurfaceFormat format = sharedContext ? sharedContext->format() : QSurfaceFormat::defaultFormat(); | ||||
56 | return format; | ||||
57 | } | ||||
58 | | ||||
59 | const KoColorProfile *KisOpenGLModeProber::rootSurfaceColorProfile() const | ||||
60 | { | ||||
61 | const QSurfaceFormat::ColorSpace surfaceColorSpace = surfaceformatInUse().colorSpace(); | ||||
62 | const KoColorProfile *profile = KoColorSpaceRegistry::instance()->p709SRGBProfile(); | ||||
63 | | ||||
64 | if (surfaceColorSpace == QSurfaceFormat::sRGBColorSpace) { | ||||
65 | // use the default one! | ||||
66 | #ifdef HAVE_HDR | ||||
67 | } else if (surfaceColorSpace == QSurfaceFormat::scRGBColorSpace) { | ||||
68 | profile = KoColorSpaceRegistry::instance()->p709G10Profile(); | ||||
69 | } else if (surfaceColorSpace == QSurfaceFormat::bt2020PQColorSpace) { | ||||
70 | profile = KoColorSpaceRegistry::instance()->p2020PQProfile(); | ||||
71 | #endif | ||||
72 | } | ||||
73 | | ||||
74 | return profile; | ||||
75 | } | ||||
76 | | ||||
77 | namespace { | ||||
78 | struct AppAttributeSetter | ||||
79 | { | ||||
80 | AppAttributeSetter(Qt::ApplicationAttribute attribute, bool useOpenGLES) | ||||
81 | : m_attribute(attribute), | ||||
82 | m_oldValue(QCoreApplication::testAttribute(attribute)) | ||||
83 | { | ||||
84 | QCoreApplication::setAttribute(attribute, useOpenGLES); | ||||
85 | } | ||||
86 | | ||||
87 | ~AppAttributeSetter() { | ||||
88 | QCoreApplication::setAttribute(m_attribute, m_oldValue); | ||||
89 | } | ||||
90 | | ||||
91 | private: | ||||
92 | Qt::ApplicationAttribute m_attribute; | ||||
93 | bool m_oldValue = false; | ||||
94 | }; | ||||
95 | | ||||
96 | struct SurfaceFormatSetter | ||||
97 | { | ||||
98 | SurfaceFormatSetter(const QSurfaceFormat &format) | ||||
99 | : m_oldFormat(QSurfaceFormat::defaultFormat()) | ||||
100 | { | ||||
101 | QSurfaceFormat::setDefaultFormat(format); | ||||
102 | } | ||||
103 | | ||||
104 | ~SurfaceFormatSetter() { | ||||
105 | QSurfaceFormat::setDefaultFormat(m_oldFormat); | ||||
106 | } | ||||
107 | | ||||
108 | private: | ||||
109 | QSurfaceFormat m_oldFormat; | ||||
110 | }; | ||||
111 | | ||||
112 | } | ||||
113 | | ||||
114 | boost::optional<KisOpenGLModeProber::Result> | ||||
115 | KisOpenGLModeProber::probeFormat(const QSurfaceFormat &format, bool adjustGlobalState) | ||||
116 | { | ||||
117 | QScopedPointer<AppAttributeSetter> sharedContextSetter; | ||||
118 | QScopedPointer<AppAttributeSetter> glSetter; | ||||
119 | QScopedPointer<AppAttributeSetter> glesSetter; | ||||
120 | QScopedPointer<SurfaceFormatSetter> formatSetter; | ||||
121 | QScopedPointer<QApplication> application; | ||||
122 | | ||||
123 | if (adjustGlobalState) { | ||||
124 | sharedContextSetter.reset(new AppAttributeSetter(Qt::AA_ShareOpenGLContexts, false)); | ||||
125 | | ||||
126 | if (format.renderableType() != QSurfaceFormat::DefaultRenderableType) { | ||||
127 | glSetter.reset(new AppAttributeSetter(Qt::AA_UseDesktopOpenGL, format.renderableType() != QSurfaceFormat::OpenGLES)); | ||||
128 | glesSetter.reset(new AppAttributeSetter(Qt::AA_UseOpenGLES, format.renderableType() == QSurfaceFormat::OpenGLES)); | ||||
129 | } | ||||
130 | | ||||
131 | formatSetter.reset(new SurfaceFormatSetter(format)); | ||||
132 | | ||||
133 | int argc = 1; | ||||
134 | QByteArray data("krita"); | ||||
135 | char *argv = data.data(); | ||||
136 | application.reset(new QApplication(argc, &argv)); | ||||
137 | } | ||||
138 | | ||||
139 | QWindow surface; | ||||
140 | surface.setFormat(format); | ||||
141 | surface.setSurfaceType(QSurface::OpenGLSurface); | ||||
142 | surface.create(); | ||||
143 | QOpenGLContext context; | ||||
144 | context.setFormat(format); | ||||
145 | | ||||
146 | | ||||
147 | if (!context.create()) { | ||||
148 | dbgOpenGL << "OpenGL context cannot be created"; | ||||
149 | return boost::none; | ||||
150 | } | ||||
151 | if (!context.isValid()) { | ||||
152 | dbgOpenGL << "OpenGL context is not valid while checking Qt's OpenGL status"; | ||||
153 | return boost::none; | ||||
154 | } | ||||
155 | if (!context.makeCurrent(&surface)) { | ||||
156 | dbgOpenGL << "OpenGL context cannot be made current"; | ||||
157 | return boost::none; | ||||
158 | } | ||||
159 | | ||||
160 | if (!fuzzyCompareColorSpaces(context.format().colorSpace(), format.colorSpace())) { | ||||
161 | dbgOpenGL << "Failed to create an OpenGL context with requested color space. Requested:" << format.colorSpace() << "Actual:" << context.format().colorSpace(); | ||||
162 | return boost::none; | ||||
163 | } | ||||
164 | | ||||
165 | return Result(context); | ||||
166 | } | ||||
167 | | ||||
168 | bool KisOpenGLModeProber::fuzzyCompareColorSpaces(const QSurfaceFormat::ColorSpace &lhs, const QSurfaceFormat::ColorSpace &rhs) | ||||
169 | { | ||||
170 | return lhs == rhs || | ||||
171 | ((lhs == QSurfaceFormat::DefaultColorSpace || | ||||
172 | lhs == QSurfaceFormat::sRGBColorSpace) && | ||||
173 | (rhs == QSurfaceFormat::DefaultColorSpace || | ||||
174 | rhs == QSurfaceFormat::sRGBColorSpace)); | ||||
175 | } | ||||
176 | | ||||
177 | void KisOpenGLModeProber::initSurfaceFormatFromConfig(KisConfig::RootSurfaceFormat config, | ||||
178 | QSurfaceFormat *format) | ||||
179 | { | ||||
180 | #ifdef HAVE_HDR | ||||
181 | if (config == KisConfig::BT2020_PQ) { | ||||
182 | | ||||
183 | format->setRedBufferSize(10); | ||||
184 | format->setGreenBufferSize(10); | ||||
185 | format->setBlueBufferSize(10); | ||||
186 | format->setAlphaBufferSize(2); | ||||
187 | format->setColorSpace(QSurfaceFormat::bt2020PQColorSpace); | ||||
188 | } else if (config == KisConfig::BT709_G10) { | ||||
189 | format->setRedBufferSize(16); | ||||
190 | format->setGreenBufferSize(16); | ||||
191 | format->setBlueBufferSize(16); | ||||
192 | format->setAlphaBufferSize(16); | ||||
193 | format->setColorSpace(QSurfaceFormat::scRGBColorSpace); | ||||
194 | } else | ||||
195 | #else | ||||
196 | if (config == KisConfig::BT2020_PQ) { | ||||
197 | qWarning() << "WARNING: Bt.2020 PQ surface type is not supoprted by this build of Krita"; | ||||
198 | } else if (config == KisConfig::BT709_G10) { | ||||
199 | qWarning() << "WARNING: scRGB surface type is not supoprted by this build of Krita"; | ||||
200 | } | ||||
201 | #endif | ||||
202 | | ||||
203 | { | ||||
204 | format->setRedBufferSize(8); | ||||
205 | format->setGreenBufferSize(8); | ||||
206 | format->setBlueBufferSize(8); | ||||
207 | format->setAlphaBufferSize(8); | ||||
208 | // TODO: check if we can use real sRGB space here | ||||
209 | format->setColorSpace(QSurfaceFormat::DefaultColorSpace); | ||||
210 | } | ||||
211 | } | ||||
212 | | ||||
213 | bool KisOpenGLModeProber::isFormatHDR(const QSurfaceFormat &format) | ||||
214 | { | ||||
215 | #ifdef HAVE_HDR | ||||
216 | | ||||
217 | bool isBt2020PQ = | ||||
218 | format.colorSpace() == QSurfaceFormat::bt2020PQColorSpace && | ||||
219 | format.redBufferSize() == 10 && | ||||
220 | format.greenBufferSize() == 10 && | ||||
221 | format.blueBufferSize() == 10 && | ||||
222 | format.alphaBufferSize() == 2; | ||||
223 | | ||||
224 | bool isBt709G10 = | ||||
225 | format.colorSpace() == QSurfaceFormat::scRGBColorSpace && | ||||
226 | format.redBufferSize() == 16 && | ||||
227 | format.greenBufferSize() == 16 && | ||||
228 | format.blueBufferSize() == 16 && | ||||
229 | format.alphaBufferSize() == 16; | ||||
230 | | ||||
231 | return isBt2020PQ || isBt709G10; | ||||
232 | #else | ||||
233 | return false; | ||||
234 | #endif | ||||
235 | } | ||||
236 | | ||||
237 | KisOpenGLModeProber::Result::Result(QOpenGLContext &context) { | ||||
238 | if (!context.isValid()) { | ||||
239 | return; | ||||
240 | } | ||||
241 | | ||||
242 | QOpenGLFunctions *funcs = context.functions(); // funcs is ready to be used | ||||
243 | | ||||
244 | m_rendererString = QString(reinterpret_cast<const char *>(funcs->glGetString(GL_RENDERER))); | ||||
245 | m_driverVersionString = QString(reinterpret_cast<const char *>(funcs->glGetString(GL_VERSION))); | ||||
246 | m_vendorString = QString(reinterpret_cast<const char *>(funcs->glGetString(GL_VENDOR))); | ||||
247 | m_shadingLanguageString = QString(reinterpret_cast<const char *>(funcs->glGetString(GL_SHADING_LANGUAGE_VERSION))); | ||||
248 | m_glMajorVersion = context.format().majorVersion(); | ||||
249 | m_glMinorVersion = context.format().minorVersion(); | ||||
250 | m_supportsDeprecatedFunctions = (context.format().options() & QSurfaceFormat::DeprecatedFunctions); | ||||
251 | m_isOpenGLES = context.isOpenGLES(); | ||||
252 | m_format = context.format(); | ||||
253 | } |