Line | Source | Count |
1 | | - |
2 | | - |
3 | | - |
4 | | - |
5 | | - |
6 | | - |
7 | | - |
8 | | - |
9 | | - |
10 | | - |
11 | | - |
12 | | - |
13 | | - |
14 | | - |
15 | | - |
16 | | - |
17 | | - |
18 | | - |
19 | | - |
20 | | - |
21 | | - |
22 | | - |
23 | | - |
24 | | - |
25 | | - |
26 | | - |
27 | | - |
28 | | - |
29 | | - |
30 | | - |
31 | | - |
32 | | - |
33 | | - |
34 | | - |
35 | | - |
36 | | - |
37 | | - |
38 | | - |
39 | | - |
40 | #include "qeglplatformcontext_p.h" | - |
41 | #include "qeglconvenience_p.h" | - |
42 | #include "qeglpbuffer_p.h" | - |
43 | #include <qpa/qplatformwindow.h> | - |
44 | #include <QOpenGLContext> | - |
45 | #include <QtPlatformHeaders/QEGLNativeContext> | - |
46 | #include <QDebug> | - |
47 | | - |
48 | #ifdef Q_OS_ANDROID | - |
49 | #include <QtCore/private/qjnihelpers_p.h> | - |
50 | #endif | - |
51 | #ifndef Q_OS_WIN | - |
52 | #include <dlfcn.h> | - |
53 | #endif | - |
54 | | - |
55 | QT_BEGIN_NAMESPACE | - |
56 | | - |
57 | | - |
58 | | - |
59 | | - |
60 | | - |
61 | | - |
62 | | - |
63 | | - |
64 | | - |
65 | | - |
66 | | - |
67 | | - |
68 | | - |
69 | | - |
70 | | - |
71 | | - |
72 | | - |
73 | | - |
74 | | - |
75 | #ifndef EGL_CONTEXT_MINOR_VERSION_KHR | - |
76 | #define EGL_CONTEXT_MINOR_VERSION_KHR 0x30FB | - |
77 | #endif | - |
78 | #ifndef EGL_CONTEXT_FLAGS_KHR | - |
79 | #define EGL_CONTEXT_FLAGS_KHR 0x30FC | - |
80 | #endif | - |
81 | #ifndef EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR | - |
82 | #define EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR 0x30FD | - |
83 | #endif | - |
84 | #ifndef EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR | - |
85 | #define EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR 0x00000001 | - |
86 | #endif | - |
87 | #ifndef EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR | - |
88 | #define EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR 0x00000002 | - |
89 | #endif | - |
90 | #ifndef EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR | - |
91 | #define EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR 0x00000001 | - |
92 | #endif | - |
93 | #ifndef EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR | - |
94 | #define EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR 0x00000002 | - |
95 | #endif | - |
96 | | - |
97 | | - |
98 | #ifndef GL_CONTEXT_FLAGS | - |
99 | #define GL_CONTEXT_FLAGS 0x821E | - |
100 | #endif | - |
101 | #ifndef GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT | - |
102 | #define GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT 0x0001 | - |
103 | #endif | - |
104 | #ifndef GL_CONTEXT_FLAG_DEBUG_BIT | - |
105 | #define GL_CONTEXT_FLAG_DEBUG_BIT 0x00000002 | - |
106 | #endif | - |
107 | #ifndef GL_CONTEXT_PROFILE_MASK | - |
108 | #define GL_CONTEXT_PROFILE_MASK 0x9126 | - |
109 | #endif | - |
110 | #ifndef GL_CONTEXT_CORE_PROFILE_BIT | - |
111 | #define GL_CONTEXT_CORE_PROFILE_BIT 0x00000001 | - |
112 | #endif | - |
113 | #ifndef GL_CONTEXT_COMPATIBILITY_PROFILE_BIT | - |
114 | #define GL_CONTEXT_COMPATIBILITY_PROFILE_BIT 0x00000002 | - |
115 | #endif | - |
116 | | - |
117 | QEGLPlatformContext::QEGLPlatformContext(const QSurfaceFormat &format, QPlatformOpenGLContext *share, EGLDisplay display, | - |
118 | EGLConfig *config, const QVariant &nativeHandle, Flags flags) | - |
119 | : m_eglDisplay(display) | - |
120 | , m_swapInterval(-1) | - |
121 | , m_swapIntervalEnvChecked(false) | - |
122 | , m_swapIntervalFromEnv(-1) | - |
123 | , m_flags(flags) | - |
124 | { | - |
125 | if (nativeHandle.isNull()) { | - |
126 | m_eglConfig = config ? *config : q_configFromGLFormat(display, format); | - |
127 | m_ownsContext = true; | - |
128 | init(format, share); | - |
129 | } else { | - |
130 | m_ownsContext = false; | - |
131 | adopt(nativeHandle, share); | - |
132 | } | - |
133 | } | - |
134 | | - |
135 | void QEGLPlatformContext::init(const QSurfaceFormat &format, QPlatformOpenGLContext *share) | - |
136 | { | - |
137 | m_format = q_glFormatFromConfig(m_eglDisplay, m_eglConfig); | - |
138 | | - |
139 | | - |
140 | m_shareContext = share ? static_cast<QEGLPlatformContext *>(share)->m_eglContext : 0; | - |
141 | | - |
142 | QVector<EGLint> contextAttrs; | - |
143 | contextAttrs.append(EGL_CONTEXT_CLIENT_VERSION); | - |
144 | contextAttrs.append(format.majorVersion()); | - |
145 | const bool hasKHRCreateContext = q_hasEglExtension(m_eglDisplay, "EGL_KHR_create_context"); | - |
146 | if (hasKHRCreateContext) { | - |
147 | contextAttrs.append(EGL_CONTEXT_MINOR_VERSION_KHR); | - |
148 | contextAttrs.append(format.minorVersion()); | - |
149 | int flags = 0; | - |
150 | | - |
151 | if (format.testOption(QSurfaceFormat::DebugContext)) | - |
152 | flags |= EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR; | - |
153 | | - |
154 | if (m_format.renderableType() == QSurfaceFormat::OpenGL | - |
155 | && format.majorVersion() >= 3 | - |
156 | && !format.testOption(QSurfaceFormat::DeprecatedFunctions)) | - |
157 | flags |= EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR; | - |
158 | if (flags) { | - |
159 | contextAttrs.append(EGL_CONTEXT_FLAGS_KHR); | - |
160 | contextAttrs.append(flags); | - |
161 | } | - |
162 | | - |
163 | if (m_format.renderableType() == QSurfaceFormat::OpenGL) { | - |
164 | contextAttrs.append(EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR); | - |
165 | contextAttrs.append(format.profile() == QSurfaceFormat::CoreProfile | - |
166 | ? EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR | - |
167 | : EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR); | - |
168 | } | - |
169 | } | - |
170 | contextAttrs.append(EGL_NONE); | - |
171 | m_contextAttrs = contextAttrs; | - |
172 | | - |
173 | switch (m_format.renderableType()) { | - |
174 | case QSurfaceFormat::OpenVG: | - |
175 | m_api = EGL_OPENVG_API; | - |
176 | break; | - |
177 | #ifdef EGL_VERSION_1_4 | - |
178 | case QSurfaceFormat::OpenGL: | - |
179 | m_api = EGL_OPENGL_API; | - |
180 | break; | - |
181 | #endif // EGL_VERSION_1_4 | - |
182 | default: | - |
183 | m_api = EGL_OPENGL_ES_API; | - |
184 | break; | - |
185 | } | - |
186 | | - |
187 | eglBindAPI(m_api); | - |
188 | m_eglContext = eglCreateContext(m_eglDisplay, m_eglConfig, m_shareContext, contextAttrs.constData()); | - |
189 | if (m_eglContext == EGL_NO_CONTEXT && m_shareContext != EGL_NO_CONTEXT) { | - |
190 | m_shareContext = 0; | - |
191 | m_eglContext = eglCreateContext(m_eglDisplay, m_eglConfig, 0, contextAttrs.constData()); | - |
192 | } | - |
193 | | - |
194 | if (m_eglContext == EGL_NO_CONTEXT) { | - |
195 | qWarning("QEGLPlatformContext: Failed to create context: %x", eglGetError()); | - |
196 | return; | - |
197 | } | - |
198 | | - |
199 | static const bool printConfig = qEnvironmentVariableIntValue("QT_QPA_EGLFS_DEBUG"); | - |
200 | if (printConfig) { | - |
201 | qDebug() << "Created context for format" << format << "with config:"; | - |
202 | q_printEglConfig(m_eglDisplay, m_eglConfig); | - |
203 | } | - |
204 | | - |
205 | | - |
206 | } | - |
207 | | - |
208 | void QEGLPlatformContext::adopt(const QVariant &nativeHandle, QPlatformOpenGLContext *share) | - |
209 | { | - |
210 | if (!nativeHandle.canConvert<QEGLNativeContext>()) { | - |
211 | qWarning("QEGLPlatformContext: Requires a QEGLNativeContext"); | - |
212 | return; | - |
213 | } | - |
214 | QEGLNativeContext handle = nativeHandle.value<QEGLNativeContext>(); | - |
215 | EGLContext context = handle.context(); | - |
216 | if (!context) { | - |
217 | qWarning("QEGLPlatformContext: No EGLContext given"); | - |
218 | return; | - |
219 | } | - |
220 | | - |
221 | | - |
222 | if (handle.display() != m_eglDisplay) { | - |
223 | qWarning("QEGLPlatformContext: Cannot adopt context from different display"); | - |
224 | return; | - |
225 | } | - |
226 | | - |
227 | | - |
228 | EGLint value = 0; | - |
229 | eglQueryContext(m_eglDisplay, context, EGL_CONFIG_ID, &value); | - |
230 | EGLint n = 0; | - |
231 | EGLConfig cfg; | - |
232 | const EGLint attribs[] = { EGL_CONFIG_ID, value, EGL_NONE }; | - |
233 | if (eglChooseConfig(m_eglDisplay, attribs, &cfg, 1, &n) && n == 1) { | - |
234 | m_eglConfig = cfg; | - |
235 | m_format = q_glFormatFromConfig(m_eglDisplay, m_eglConfig); | - |
236 | } else { | - |
237 | qWarning("QEGLPlatformContext: Failed to get framebuffer configuration for context"); | - |
238 | } | - |
239 | | - |
240 | | - |
241 | value = 0; | - |
242 | eglQueryContext(m_eglDisplay, context, EGL_CONTEXT_CLIENT_TYPE, &value); | - |
243 | if (value == EGL_OPENGL_API || value == EGL_OPENGL_ES_API) { | - |
244 | m_api = value; | - |
245 | eglBindAPI(m_api); | - |
246 | } else { | - |
247 | qWarning("QEGLPlatformContext: Failed to get client API type"); | - |
248 | m_api = EGL_OPENGL_ES_API; | - |
249 | } | - |
250 | | - |
251 | m_eglContext = context; | - |
252 | m_shareContext = share ? static_cast<QEGLPlatformContext *>(share)->m_eglContext : 0; | - |
253 | updateFormatFromGL(); | - |
254 | } | - |
255 | | - |
256 | void QEGLPlatformContext::initialize() | - |
257 | { | - |
258 | if (m_eglContext != EGL_NO_CONTEXT) | - |
259 | updateFormatFromGL(); | - |
260 | } | - |
261 | | - |
262 | | - |
263 | | - |
264 | EGLSurface QEGLPlatformContext::createTemporaryOffscreenSurface() | - |
265 | { | - |
266 | | - |
267 | const EGLint pbufferAttributes[] = { | - |
268 | EGL_WIDTH, 1, | - |
269 | EGL_HEIGHT, 1, | - |
270 | EGL_LARGEST_PBUFFER, EGL_FALSE, | - |
271 | EGL_NONE | - |
272 | }; | - |
273 | | - |
274 | | - |
275 | | - |
276 | | - |
277 | EGLConfig config = q_configFromGLFormat(m_eglDisplay, m_format, false, EGL_PBUFFER_BIT); | - |
278 | | - |
279 | return eglCreatePbufferSurface(m_eglDisplay, config, pbufferAttributes); | - |
280 | } | - |
281 | | - |
282 | void QEGLPlatformContext::destroyTemporaryOffscreenSurface(EGLSurface surface) | - |
283 | { | - |
284 | eglDestroySurface(m_eglDisplay, surface); | - |
285 | } | - |
286 | | - |
287 | void QEGLPlatformContext::runGLChecks() | - |
288 | { | - |
289 | | - |
290 | | - |
291 | } | - |
292 | | - |
293 | void QEGLPlatformContext::updateFormatFromGL() | - |
294 | { | - |
295 | #ifndef QT_NO_OPENGL | - |
296 | | - |
297 | | - |
298 | EGLDisplay prevDisplay = eglGetCurrentDisplay(); | - |
299 | if (prevDisplay == EGL_NO_DISPLAY) | - |
300 | prevDisplay = m_eglDisplay; | - |
301 | EGLContext prevContext = eglGetCurrentContext(); | - |
302 | EGLSurface prevSurfaceDraw = eglGetCurrentSurface(EGL_DRAW); | - |
303 | EGLSurface prevSurfaceRead = eglGetCurrentSurface(EGL_READ); | - |
304 | | - |
305 | | - |
306 | | - |
307 | | - |
308 | EGLSurface tempSurface = EGL_NO_SURFACE; | - |
309 | EGLContext tempContext = EGL_NO_CONTEXT; | - |
310 | if (m_flags.testFlag(NoSurfaceless) || !q_hasEglExtension(m_eglDisplay, "EGL_KHR_surfaceless_context")) | - |
311 | tempSurface = createTemporaryOffscreenSurface(); | - |
312 | | - |
313 | EGLBoolean ok = eglMakeCurrent(m_eglDisplay, tempSurface, tempSurface, m_eglContext); | - |
314 | if (!ok) { | - |
315 | EGLConfig config = q_configFromGLFormat(m_eglDisplay, m_format, false, EGL_PBUFFER_BIT); | - |
316 | tempContext = eglCreateContext(m_eglDisplay, config, 0, m_contextAttrs.constData()); | - |
317 | if (tempContext != EGL_NO_CONTEXT) | - |
318 | ok = eglMakeCurrent(m_eglDisplay, tempSurface, tempSurface, tempContext); | - |
319 | } | - |
320 | if (ok) { | - |
321 | if (m_format.renderableType() == QSurfaceFormat::OpenGL | - |
322 | || m_format.renderableType() == QSurfaceFormat::OpenGLES) { | - |
323 | const GLubyte *s = glGetString(GL_VERSION); | - |
324 | if (s) { | - |
325 | QByteArray version = QByteArray(reinterpret_cast<const char *>(s)); | - |
326 | int major, minor; | - |
327 | if (QPlatformOpenGLContext::parseOpenGLVersion(version, major, minor)) { | - |
328 | #ifdef Q_OS_ANDROID | - |
329 | | - |
330 | static int apiLevel = QtAndroidPrivate::androidSdkVersion(); | - |
331 | if (apiLevel <= 17 && major >= 3) { | - |
332 | major = 2; | - |
333 | minor = 0; | - |
334 | } | - |
335 | #endif | - |
336 | m_format.setMajorVersion(major); | - |
337 | m_format.setMinorVersion(minor); | - |
338 | } | - |
339 | } | - |
340 | m_format.setProfile(QSurfaceFormat::NoProfile); | - |
341 | m_format.setOptions(QSurfaceFormat::FormatOptions()); | - |
342 | if (m_format.renderableType() == QSurfaceFormat::OpenGL) { | - |
343 | | - |
344 | if (m_format.majorVersion() < 3) { | - |
345 | m_format.setOption(QSurfaceFormat::DeprecatedFunctions); | - |
346 | } else { | - |
347 | GLint value = 0; | - |
348 | glGetIntegerv(GL_CONTEXT_FLAGS, &value); | - |
349 | if (!(value & GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT)) | - |
350 | m_format.setOption(QSurfaceFormat::DeprecatedFunctions); | - |
351 | if (value & GL_CONTEXT_FLAG_DEBUG_BIT) | - |
352 | m_format.setOption(QSurfaceFormat::DebugContext); | - |
353 | if (m_format.version() >= qMakePair(3, 2)) { | - |
354 | value = 0; | - |
355 | glGetIntegerv(GL_CONTEXT_PROFILE_MASK, &value); | - |
356 | if (value & GL_CONTEXT_CORE_PROFILE_BIT) | - |
357 | m_format.setProfile(QSurfaceFormat::CoreProfile); | - |
358 | else if (value & GL_CONTEXT_COMPATIBILITY_PROFILE_BIT) | - |
359 | m_format.setProfile(QSurfaceFormat::CompatibilityProfile); | - |
360 | } | - |
361 | } | - |
362 | } | - |
363 | } | - |
364 | runGLChecks(); | - |
365 | eglMakeCurrent(prevDisplay, prevSurfaceDraw, prevSurfaceRead, prevContext); | - |
366 | } else { | - |
367 | qWarning("QEGLPlatformContext: Failed to make temporary surface current, format not updated (%x)", eglGetError()); | - |
368 | } | - |
369 | if (tempSurface != EGL_NO_SURFACE) | - |
370 | destroyTemporaryOffscreenSurface(tempSurface); | - |
371 | if (tempContext != EGL_NO_CONTEXT) | - |
372 | eglDestroyContext(m_eglDisplay, tempContext); | - |
373 | #endif // QT_NO_OPENGL | - |
374 | } | - |
375 | | - |
376 | bool QEGLPlatformContext::makeCurrent(QPlatformSurface *surface) | - |
377 | { | - |
378 | Q_ASSERT(surface->surface()->supportsOpenGL()); | - |
379 | | - |
380 | eglBindAPI(m_api); | - |
381 | | - |
382 | EGLSurface eglSurface = eglSurfaceForPlatformSurface(surface); | - |
383 | | - |
384 | | - |
385 | if (eglGetCurrentContext() == m_eglContext && | - |
386 | eglGetCurrentDisplay() == m_eglDisplay && | - |
387 | eglGetCurrentSurface(EGL_READ) == eglSurface && | - |
388 | eglGetCurrentSurface(EGL_DRAW) == eglSurface) { | - |
389 | return true; | - |
390 | } | - |
391 | | - |
392 | const bool ok = eglMakeCurrent(m_eglDisplay, eglSurface, eglSurface, m_eglContext); | - |
393 | if (ok) { | - |
394 | if (!m_swapIntervalEnvChecked) { | - |
395 | m_swapIntervalEnvChecked = true; | - |
396 | if (qEnvironmentVariableIsSet("QT_QPA_EGLFS_SWAPINTERVAL")) { | - |
397 | QByteArray swapIntervalString = qgetenv("QT_QPA_EGLFS_SWAPINTERVAL"); | - |
398 | bool intervalOk; | - |
399 | const int swapInterval = swapIntervalString.toInt(&intervalOk); | - |
400 | if (intervalOk) | - |
401 | m_swapIntervalFromEnv = swapInterval; | - |
402 | } | - |
403 | } | - |
404 | const int requestedSwapInterval = m_swapIntervalFromEnv >= 0 | - |
405 | ? m_swapIntervalFromEnv | - |
406 | : surface->format().swapInterval(); | - |
407 | if (requestedSwapInterval >= 0 && m_swapInterval != requestedSwapInterval) { | - |
408 | m_swapInterval = requestedSwapInterval; | - |
409 | if (eglSurface != EGL_NO_SURFACE) | - |
410 | eglSwapInterval(eglDisplay(), m_swapInterval); | - |
411 | } | - |
412 | } else { | - |
413 | qWarning("QEGLPlatformContext: eglMakeCurrent failed: %x", eglGetError()); | - |
414 | } | - |
415 | | - |
416 | return ok; | - |
417 | } | - |
418 | | - |
419 | QEGLPlatformContext::~QEGLPlatformContext() | - |
420 | { | - |
421 | if (m_ownsContext && m_eglContext != EGL_NO_CONTEXT) | - |
422 | eglDestroyContext(m_eglDisplay, m_eglContext); | - |
423 | | - |
424 | m_eglContext = EGL_NO_CONTEXT; | - |
425 | } | - |
426 | | - |
427 | void QEGLPlatformContext::doneCurrent() | - |
428 | { | - |
429 | eglBindAPI(m_api); | - |
430 | bool ok = eglMakeCurrent(m_eglDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); | - |
431 | if (!ok) | - |
432 | qWarning("QEGLPlatformContext: eglMakeCurrent failed: %x", eglGetError()); | - |
433 | } | - |
434 | | - |
435 | void QEGLPlatformContext::swapBuffers(QPlatformSurface *surface) | - |
436 | { | - |
437 | eglBindAPI(m_api); | - |
438 | EGLSurface eglSurface = eglSurfaceForPlatformSurface(surface); | - |
439 | if (eglSurface != EGL_NO_SURFACE) { | - |
440 | bool ok = eglSwapBuffers(m_eglDisplay, eglSurface); | - |
441 | if (!ok) | - |
442 | qWarning("QEGLPlatformContext: eglSwapBuffers failed: %x", eglGetError()); | - |
443 | } | - |
444 | } | - |
445 | | - |
| void (*QFunctionPointer QEGLPlatformContext::getProcAddress(const QByteArray &procName)) ()char *procName) | |
447 | { | - |
448 | eglBindAPI(m_api); | - |
449 | returnQFunctionPointer proc = (QFunctionPointer) eglGetProcAddress(procName.constData());); | - |
450 | #if !defined(Q_OS_WIN) && !defined(Q_OS_INTEGRITY) | - |
451 | if (!proc)TRUE | never evaluated | FALSE | never evaluated |
| 0 |
452 | proc = (QFunctionPointer) dlsym(RTLD_DEFAULT, procName); never executed: proc = (QFunctionPointer) dlsym(((void *) 0), procName); | 0 |
453 | #endif | - |
454 | return proc; never executed: return proc; | 0 |
455 | } | - |
456 | | - |
457 | QSurfaceFormat QEGLPlatformContext::format() const | - |
458 | { | - |
459 | return m_format; | - |
460 | } | - |
461 | | - |
462 | EGLContext QEGLPlatformContext::eglContext() const | - |
463 | { | - |
464 | return m_eglContext; | - |
465 | } | - |
466 | | - |
467 | EGLDisplay QEGLPlatformContext::eglDisplay() const | - |
468 | { | - |
469 | return m_eglDisplay; | - |
470 | } | - |
471 | | - |
472 | EGLConfig QEGLPlatformContext::eglConfig() const | - |
473 | { | - |
474 | return m_eglConfig; | - |
475 | } | - |
476 | | - |
477 | QT_END_NAMESPACE | - |
| | |