Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
Loading...
Searching...
No Matches
qwaylandglcontext.cpp
Go to the documentation of this file.
1
// Copyright (C) 2016 The Qt Company Ltd.
2
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
// Qt-Security score:significant reason:default
4
5
#
include
"qwaylandglcontext_p.h"
6
7
#
include
<
QtWaylandClient
/
private
/
qwaylanddisplay_p
.
h
>
8
#
include
<
QtWaylandClient
/
private
/
qwaylandwindow_p
.
h
>
9
#
include
<
QtWaylandClient
/
private
/
qwaylandsubsurface_p
.
h
>
10
#
include
<
QtWaylandClient
/
private
/
qwaylandabstractdecoration_p
.
h
>
11
#
include
<
QtWaylandClient
/
private
/
qwaylandintegration_p
.
h
>
12
#
include
"qwaylandeglwindow_p.h"
13
14
#
include
<
QDebug
>
15
#
include
<
QtGui
/
private
/
qeglconvenience_p
.
h
>
16
#
include
<
QtGui
/
private
/
qopenglcontext_p
.
h
>
17
#
include
<
QtOpenGL
/
private
/
qopengltexturecache_p
.
h
>
18
#
include
<
QtGui
/
private
/
qguiapplication_p
.
h
>
19
#
include
<
QtGui
/
private
/
qeglpbuffer_p
.
h
>
20
21
22
#
include
<
qpa
/
qplatformopenglcontext
.
h
>
23
#
include
<
QtGui
/
QSurfaceFormat
>
24
#
include
<
QtOpenGL
/
QOpenGLShaderProgram
>
25
#
include
<
QtGui
/
QOpenGLFunctions
>
26
#
include
<
QOpenGLBuffer
>
27
28
#
include
<
QtCore
/
qmutex
.
h
>
29
30
#
include
<
dlfcn
.
h
>
31
32
// Constants from EGL_KHR_create_context
33
#
ifndef
EGL_CONTEXT_MINOR_VERSION_KHR
34
#
define
EGL_CONTEXT_MINOR_VERSION_KHR
0x30FB
35
#
endif
36
#
ifndef
EGL_CONTEXT_FLAGS_KHR
37
#
define
EGL_CONTEXT_FLAGS_KHR
0x30FC
38
#
endif
39
#
ifndef
EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR
40
#
define
EGL_CONTEXT_OPENGL_PROFILE_MASK_KHR
0x30FD
41
#
endif
42
#
ifndef
EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR
43
#
define
EGL_CONTEXT_OPENGL_DEBUG_BIT_KHR
0x00000001
44
#
endif
45
#
ifndef
EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR
46
#
define
EGL_CONTEXT_OPENGL_FORWARD_COMPATIBLE_BIT_KHR
0x00000002
47
#
endif
48
#
ifndef
EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
49
#
define
EGL_CONTEXT_OPENGL_CORE_PROFILE_BIT_KHR
0x00000001
50
#
endif
51
#
ifndef
EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR
52
#
define
EGL_CONTEXT_OPENGL_COMPATIBILITY_PROFILE_BIT_KHR
0x00000002
53
#
endif
54
#
ifndef
EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR
55
#
define
EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_KHR
0x31BD
56
#
endif
57
#
ifndef
EGL_LOSE_CONTEXT_ON_RESET_KHR
58
#
define
EGL_LOSE_CONTEXT_ON_RESET_KHR
0x31BF
59
#
endif
60
#
ifndef
EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
61
#
define
EGL_CONTEXT_OPENGL_ROBUST_ACCESS_BIT_KHR
0x00000004
62
#
endif
63
64
// Constants from EGL_EXT_create_context_robustness
65
#
ifndef
EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT
66
#
define
EGL_CONTEXT_OPENGL_ROBUST_ACCESS_EXT
0x30BF
67
#
endif
68
#
ifndef
EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT
69
#
define
EGL_CONTEXT_OPENGL_RESET_NOTIFICATION_STRATEGY_EXT
0x3138
70
#
endif
71
#
ifndef
EGL_LOSE_CONTEXT_ON_RESET_EXT
72
#
define
EGL_LOSE_CONTEXT_ON_RESET_EXT
0x31BF
73
#
endif
74
75
// Constants for OpenGL which are not available in the ES headers.
76
#
ifndef
GL_CONTEXT_FLAGS
77
#
define
GL_CONTEXT_FLAGS
0x821E
78
#
endif
79
#
ifndef
GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT
80
#
define
GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT
0x0001
81
#
endif
82
#
ifndef
GL_CONTEXT_FLAG_DEBUG_BIT
83
#
define
GL_CONTEXT_FLAG_DEBUG_BIT
0x00000002
84
#
endif
85
#
ifndef
GL_CONTEXT_PROFILE_MASK
86
#
define
GL_CONTEXT_PROFILE_MASK
0x9126
87
#
endif
88
#
ifndef
GL_CONTEXT_CORE_PROFILE_BIT
89
#
define
GL_CONTEXT_CORE_PROFILE_BIT
0x00000001
90
#
endif
91
#
ifndef
GL_CONTEXT_COMPATIBILITY_PROFILE_BIT
92
#
define
GL_CONTEXT_COMPATIBILITY_PROFILE_BIT
0x00000002
93
#
endif
94
95
// Constants from EGL_NV_robustness_video_memory_purge
96
#
ifndef
EGL_GENERATE_RESET_ON_VIDEO_MEMORY_PURGE_NV
97
#
define
EGL_GENERATE_RESET_ON_VIDEO_MEMORY_PURGE_NV
0x334C
98
#
endif
99
100
QT_BEGIN_NAMESPACE
101
102
namespace
QtWaylandClient
{
103
104
// Used by QEGLPlatformContext::createFrom() to adopt a native EGL context
105
// (QNativeInterface::QEGLContext::fromNative()). Both constructors take the
106
// Wayland display from the integration, as createFrom() cannot pass it:
107
// the temporary surface and the reconnection use it.
108
QWaylandGLContext
::
QWaylandGLContext
()
109
:
QEGLPlatformContext
(),
110
m_display
(
QWaylandIntegration
::
instance
()->
display
()),
111
m_api
(
EGL_OPENGL_ES_API
)
112
{
113
watchReconnection
();
114
}
115
116
QWaylandGLContext
::
QWaylandGLContext
(
EGLDisplay
eglDisplay
,
const
QSurfaceFormat
&
fmt
,
117
QPlatformOpenGLContext
*
share
)
118
:
QEGLPlatformContext
(
fmt
,
share
,
eglDisplay
)
119
,
m_display
(
QWaylandIntegration
::
instance
()->
display
())
120
{
121
watchReconnection
();
122
123
switch
(
format
().
renderableType
()) {
124
case
QSurfaceFormat
::
OpenVG
:
125
m_api
=
EGL_OPENVG_API
;
126
break
;
127
#
ifdef
EGL_VERSION_1_4
128
case
QSurfaceFormat
::
OpenGL
:
129
m_api
=
EGL_OPENGL_API
;
130
break
;
131
#
endif
// EGL_VERSION_1_4
132
default
:
133
m_api
=
EGL_OPENGL_ES_API
;
134
break
;
135
}
136
137
EGLint
a
=
EGL_MIN_SWAP_INTERVAL
;
138
EGLint
b
=
EGL_MAX_SWAP_INTERVAL
;
139
if
(!
eglGetConfigAttrib
(
eglDisplay
,
eglConfig
(),
a
, &
a
)
140
|| !
eglGetConfigAttrib
(
eglDisplay
,
eglConfig
(),
b
, &
b
) ||
a
> 0) {
141
m_supportNonBlockingSwap
=
false
;
142
}
143
{
144
bool
ok
;
145
int
supportNonBlockingSwap
=
qEnvironmentVariableIntValue
(
"QT_WAYLAND_FORCE_NONBLOCKING_SWAP_SUPPORT"
, &
ok
);
146
if
(
ok
)
147
m_supportNonBlockingSwap
=
supportNonBlockingSwap
!= 0;
148
}
149
if
(!
m_supportNonBlockingSwap
) {
150
qCWarning
(
lcQpaWayland
) <<
"Non-blocking swap buffers not supported."
151
<<
"Subsurface rendering can be affected."
152
<<
"It may also cause the event loop to freeze in some situations"
;
153
}
154
}
155
156
void
QWaylandGLContext
::
watchReconnection
()
157
{
158
m_reconnectionWatcher
=
QObject
::
connect
(
m_display
, &
QWaylandDisplay
::
connected
,
159
m_display
, [
this
] {
invalidateContext
(); });
160
}
161
162
EGLSurface
QWaylandGLContext
::
createTemporaryOffscreenSurface
()
163
{
164
m_wlSurface
=
m_display
->
createSurface
(
nullptr
);
165
m_eglWindow
=
wl_egl_window_create
(
m_wlSurface
, 1, 1);
166
#
if
QT_CONFIG
(
egl_extension_platform_wayland
)
167
EGLSurface
eglSurface
=
168
eglCreatePlatformWindowSurface
(
eglDisplay
(),
eglConfig
(),
m_eglWindow
,
nullptr
);
169
#
else
170
EGLSurface
eglSurface
=
eglCreateWindowSurface
(
eglDisplay
(),
eglConfig
(),
m_eglWindow
,
nullptr
);
171
#
endif
172
return
eglSurface
;
173
}
174
175
void
QWaylandGLContext
::
destroyTemporaryOffscreenSurface
(
EGLSurface
eglSurface
)
176
{
177
eglDestroySurface
(
eglDisplay
(),
eglSurface
);
178
wl_egl_window_destroy
(
m_eglWindow
);
179
m_eglWindow
=
nullptr
;
180
wl_surface_destroy
(
m_wlSurface
);
181
m_wlSurface
=
nullptr
;
182
}
183
184
void
QWaylandGLContext
::
runGLChecks
()
185
{
186
bool
ok
;
187
const
int
doneCurrentWorkAround
=
qEnvironmentVariableIntValue
(
"QT_WAYLAND_ENABLE_DONECURRENT_WORKAROUND"
, &
ok
);
188
if
(
ok
) {
189
m_doneCurrentWorkAround
=
doneCurrentWorkAround
!= 0;
190
if
(
m_doneCurrentWorkAround
)
191
qCDebug
(
lcQpaWayland
) <<
"Enabling doneCurrent() workaround on request."
;
192
else
193
qCDebug
(
lcQpaWayland
) <<
"Disabling doneCurrent() workaround on request."
;
194
195
}
else
{
196
// Note that even though there is an EGL context current here,
197
// QOpenGLContext and QOpenGLFunctions are not yet usable at this stage.
198
const
char
*
renderer
=
reinterpret_cast
<
const
char
*>(
glGetString
(
GL_RENDERER
));
199
if
(
renderer
&&
strstr
(
renderer
,
"Mali"
)) {
200
qCDebug
(
lcQpaWayland
) <<
"Enabling doneCurrent() workaround for Mali GPU."
201
<<
"Set QT_WAYLAND_ENABLE_DONECURRENT_WORKAROUND=0 to disable."
;
202
m_doneCurrentWorkAround
=
true
;
203
}
204
}
205
206
QEGLPlatformContext
::
runGLChecks
();
207
}
208
209
QWaylandGLContext
::~
QWaylandGLContext
()
210
{
211
QObject
::
disconnect
(
m_reconnectionWatcher
);
212
}
213
214
void
QWaylandGLContext
::
beginFrame
()
215
{
216
if
(!
m_currentWindow
)
217
return
;
218
219
if
(
m_supportNonBlockingSwap
)
220
m_currentWindow
->
beginFrame
();
221
}
222
223
void
QWaylandGLContext
::
endFrame
()
224
{
225
if
(!
m_currentWindow
)
226
return
;
227
228
QWaylandEglWindow
*
currentWindow
=
m_currentWindow
;
229
230
if
(
m_doneCurrentWorkAround
) {
231
doneCurrent
();
232
QOpenGLContextPrivate
::
setCurrentContext
(
nullptr
);
233
}
234
235
if
(
m_supportNonBlockingSwap
)
236
currentWindow
->
endFrame
();
237
}
238
239
bool
QWaylandGLContext
::
makeCurrent
(
QPlatformSurface
*
surface
)
240
{
241
if
(!
isValid
()) {
242
return
false
;
243
}
244
245
if
(
surface
->
surface
()->
surfaceClass
() ==
QSurface
::
Offscreen
) {
246
m_currentWindow
=
nullptr
;
247
return
QEGLPlatformContext
::
makeCurrent
(
surface
);
248
}
249
250
// in QWaylandGLContext() we called eglBindAPI with the correct value. However,
251
// eglBindAPI's documentation says:
252
// "eglBindAPI defines the current rendering API for EGL in the thread it is called from"
253
// Since makeCurrent() can be called from a different thread than the one we created the
254
// context in make sure to call eglBindAPI in the correct thread.
255
if
(
eglQueryAPI
() !=
m_api
) {
256
eglBindAPI
(
m_api
);
257
}
258
259
m_currentWindow
=
static_cast
<
QWaylandEglWindow
*>(
surface
);
260
261
QMutexLocker
lock
(
m_currentWindow
->
eglSurfaceLock
());
262
263
EGLSurface
eglSurface
=
m_currentWindow
->
eglSurface
();
264
QSize
size
=
m_currentWindow
->
bufferSize
();
265
266
if
(
eglSurface
==
EGL_NO_SURFACE
) {
267
m_currentWindow
->
createSurface
(
size
);
268
eglSurface
=
m_currentWindow
->
eglSurface
();
269
}
270
271
if
(!
eglMakeCurrent
(
eglDisplay
(),
eglSurface
,
eglSurface
,
eglContext
())) {
272
qWarning
(
"QWaylandGLContext::makeCurrent: eglError: %#x, this: %p \n"
,
eglGetError
(),
this
);
273
return
false
;
274
}
275
if
(!
checkGraphicsReset
())
276
return
false
;
277
278
// Nvidia requires that the surface is current and on the render thread at the time of doing the actual resizing
279
m_currentWindow
->
updateSurfaceSize
(
size
);
280
return
true
;
281
}
282
283
void
QWaylandGLContext
::
doneCurrent
()
284
{
285
m_currentWindow
=
nullptr
;
286
eglMakeCurrent
(
eglDisplay
(),
EGL_NO_SURFACE
,
EGL_NO_SURFACE
,
EGL_NO_CONTEXT
);
287
}
288
289
void
QWaylandGLContext
::
swapBuffers
(
QPlatformSurface
*
surface
)
290
{
291
if
(
surface
->
surface
()->
surfaceClass
() ==
QSurface
::
Offscreen
) {
292
return
QEGLPlatformContext
::
swapBuffers
(
surface
);
293
}
294
295
QWaylandEglWindow
*
window
=
static_cast
<
QWaylandEglWindow
*>(
surface
);
296
297
EGLSurface
eglSurface
=
window
->
eglSurface
();
298
299
int
swapInterval
=
m_supportNonBlockingSwap
? 0 :
format
().
swapInterval
();
300
eglSwapInterval
(
eglDisplay
(),
swapInterval
);
301
if
(
swapInterval
== 0 &&
format
().
swapInterval
() > 0) {
302
// Emulating a blocking swap
303
glFlush
();
// Flush before waiting so we can swap more quickly when the frame event arrives
304
window
->
waitForFrameSync
(100);
305
}
306
window
->
handleUpdate
();
307
if
(!
eglSwapBuffers
(
eglDisplay
(),
eglSurface
))
308
qCWarning
(
lcQpaWayland
,
"eglSwapBuffers failed with %#x, surface: %p"
,
eglGetError
(),
eglSurface
);
309
}
310
311
EGLSurface
QWaylandGLContext
::
eglSurfaceForPlatformSurface
(
QPlatformSurface
*
surface
)
312
{
313
if
(
surface
->
surface
()->
surfaceClass
() ==
QSurface
::
Window
)
314
return
static_cast
<
QWaylandEglWindow
*>(
surface
)->
eglSurface
();
315
else
316
return
static_cast
<
QEGLPbuffer
*>(
surface
)->
pbuffer
();
317
}
318
319
}
320
321
QT_END_NAMESPACE
QtWaylandClient
Definition
qwaylandclientextension.h:16
qtbase
src
plugins
platforms
wayland
plugins
hardwareintegration
wayland-egl
qwaylandglcontext.cpp
Generated on
for Qt by
1.18.0