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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
|
// Copyright (C) 2024 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
// Qt-Security score:significant reason:default
#include "egl_helper.h"
#include "compositor/compositor.h"
#include "ozone_util_qt.h"
#include "web_engine_context.h"
#include <QtCore/qthread.h>
#include <QtGui/qguiapplication.h>
#include <QtGui/qoffscreensurface.h>
#include <QtGui/qopenglcontext.h>
#include <QtGui/qopenglfunctions.h>
#include <qpa/qplatformnativeinterface.h>
#include <cstdint>
#include <unistd.h>
#include <vector>
namespace {
static const char *getEGLErrorString(uint32_t error)
{
switch (error) {
case EGL_SUCCESS:
return "EGL_SUCCESS";
case EGL_NOT_INITIALIZED:
return "EGL_NOT_INITIALIZED";
case EGL_BAD_ACCESS:
return "EGL_BAD_ACCESS";
case EGL_BAD_ALLOC:
return "EGL_BAD_ALLOC";
case EGL_BAD_ATTRIBUTE:
return "EGL_BAD_ATTRIBUTE";
case EGL_BAD_CONFIG:
return "EGL_BAD_CONFIG";
case EGL_BAD_CONTEXT:
return "EGL_BAD_CONTEXT";
case EGL_BAD_CURRENT_SURFACE:
return "EGL_BAD_CURRENT_SURFACE";
case EGL_BAD_DISPLAY:
return "EGL_BAD_DISPLAY";
case EGL_BAD_MATCH:
return "EGL_BAD_MATCH";
case EGL_BAD_NATIVE_PIXMAP:
return "EGL_BAD_NATIVE_PIXMAP";
case EGL_BAD_NATIVE_WINDOW:
return "EGL_BAD_NATIVE_WINDOW";
case EGL_BAD_PARAMETER:
return "EGL_BAD_PARAMETER";
case EGL_BAD_SURFACE:
return "EGL_BAD_SURFACE";
case EGL_CONTEXT_LOST:
return "EGL_CONTEXT_LOST";
default:
return "UNKNOWN";
}
}
} // namespace
QT_BEGIN_NAMESPACE
class ScopedGLContext
{
public:
ScopedGLContext(QOffscreenSurface *surface, EGLHelper::EGLFunctions *eglFun)
: m_context(new QOpenGLContext()), m_eglFun(eglFun)
{
if ((m_previousEGLContext = m_eglFun->eglGetCurrentContext())) {
m_previousEGLDrawSurface = m_eglFun->eglGetCurrentSurface(EGL_DRAW);
m_previousEGLReadSurface = m_eglFun->eglGetCurrentSurface(EGL_READ);
m_previousEGLDisplay = m_eglFun->eglGetCurrentDisplay();
}
if (!m_context->create()) {
qWarning("Failed to create OpenGL context.");
return;
}
Q_ASSERT(surface->isValid());
if (!m_context->makeCurrent(surface)) {
qWarning("Failed to make OpenGL context current.");
return;
}
}
~ScopedGLContext()
{
if (!m_textures.empty()) {
auto *glFun = m_context->functions();
glFun->glDeleteTextures(m_textures.size(), m_textures.data());
}
if (m_previousEGLContext) {
// Make sure the scoped context is not current when restoring the previous
// EGL context otherwise the QOpenGLContext destructor resets the restored
// current context.
m_context->doneCurrent();
m_eglFun->eglMakeCurrent(m_previousEGLDisplay, m_previousEGLDrawSurface,
m_previousEGLReadSurface, m_previousEGLContext);
if (m_eglFun->eglGetError() != EGL_SUCCESS)
qWarning("Failed to restore EGL context.");
}
}
bool isValid() const { return m_context->isValid() && (m_context->surface() != nullptr); }
EGLContext eglContext() const
{
QNativeInterface::QEGLContext *nativeInterface =
m_context->nativeInterface<QNativeInterface::QEGLContext>();
return nativeInterface->nativeContext();
}
uint createTexture(int width, int height)
{
auto *glFun = m_context->functions();
uint glTexture;
glFun->glGenTextures(1, &glTexture);
glFun->glBindTexture(GL_TEXTURE_2D, glTexture);
glFun->glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE,
NULL);
glFun->glBindTexture(GL_TEXTURE_2D, 0);
m_textures.push_back(glTexture);
return glTexture;
}
private:
QScopedPointer<QOpenGLContext> m_context;
EGLHelper::EGLFunctions *m_eglFun;
EGLContext m_previousEGLContext = nullptr;
EGLSurface m_previousEGLDrawSurface = nullptr;
EGLSurface m_previousEGLReadSurface = nullptr;
EGLDisplay m_previousEGLDisplay = nullptr;
std::vector<uint> m_textures;
};
EGLHelper::EGLFunctions::EGLFunctions()
{
QOpenGLContext *context = OzoneUtilQt::getQOpenGLContext();
eglCreateImage =
reinterpret_cast<PFNEGLCREATEIMAGEPROC>(context->getProcAddress("eglCreateImage"));
eglCreateDRMImageMESA = reinterpret_cast<PFNEGLCREATEDRMIMAGEMESAPROC>(
context->getProcAddress("eglCreateDRMImageMESA"));
eglDestroyImage =
reinterpret_cast<PFNEGLDESTROYIMAGEPROC>(context->getProcAddress("eglDestroyImage"));
eglExportDMABUFImageMESA = reinterpret_cast<PFNEGLEXPORTDMABUFIMAGEMESAPROC>(
context->getProcAddress("eglExportDMABUFImageMESA"));
eglExportDMABUFImageQueryMESA = reinterpret_cast<PFNEGLEXPORTDMABUFIMAGEQUERYMESAPROC>(
context->getProcAddress("eglExportDMABUFImageQueryMESA"));
eglGetCurrentContext = reinterpret_cast<PFNEGLGETCURRENTCONTEXTPROC>(
context->getProcAddress("eglGetCurrentContext"));
eglGetCurrentDisplay = reinterpret_cast<PFNEGLGETCURRENTDISPLAYPROC>(
context->getProcAddress("eglGetCurrentDisplay"));
eglGetCurrentSurface = reinterpret_cast<PFNEGLGETCURRENTSURFACEPROC>(
context->getProcAddress("eglGetCurrentSurface"));
eglGetError = reinterpret_cast<PFNEGLGETERRORPROC>(context->getProcAddress("eglGetError"));
eglMakeCurrent =
reinterpret_cast<PFNEGLMAKECURRENTPROC>(context->getProcAddress("eglMakeCurrent"));
eglQueryString =
reinterpret_cast<PFNEGLQUERYSTRINGPROC>(context->getProcAddress("eglQueryString"));
}
EGLHelper *EGLHelper::instance()
{
static EGLHelper eglHelper;
return &eglHelper;
}
EGLHelper::EGLHelper()
: m_eglDisplay(qApp->platformNativeInterface()->nativeResourceForIntegration("egldisplay"))
, m_functions(new EGLHelper::EGLFunctions())
{
const char *extensions = m_functions->eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
if (!extensions) {
qWarning("EGL: Failed to query EGL extensions.");
return;
}
if (strstr(extensions, "EGL_KHR_base_image")) {
qWarning("EGL: EGL_KHR_base_image extension is not supported.");
return;
}
if (m_eglDisplay == EGL_NO_DISPLAY) {
qWarning("EGL: No EGL display.");
return;
}
m_isDmaBufSupported = QtWebEngineCore::WebEngineContext::isGbmSupported();
// Check extensions.
if (m_isDmaBufSupported) {
const char *displayExtensions = m_functions->eglQueryString(m_eglDisplay, EGL_EXTENSIONS);
m_isDmaBufSupported = strstr(displayExtensions, "EGL_EXT_image_dma_buf_import")
&& strstr(displayExtensions, "EGL_EXT_image_dma_buf_import_modifiers")
&& strstr(displayExtensions, "EGL_MESA_image_dma_buf_export");
m_isCreateDRMImageMesaSupported = strstr(displayExtensions, "EGL_MESA_drm_image");
if (!m_isDmaBufSupported) {
qCDebug(QtWebEngineCore::lcWebEngineCompositor,
"EGL: MESA extensions not found, will not use dma-buf");
} else if (!m_isCreateDRMImageMesaSupported) {
qCDebug(QtWebEngineCore::lcWebEngineCompositor,
"EGL: MESA extensions found but missing EGL_MESA_drm_image, will use dma-buf, "
"some older graphics cards may not be supported");
m_offscreenSurface.reset(new QOffscreenSurface());
Q_ASSERT(QThread::currentThread() == qApp->thread());
m_offscreenSurface->create();
} else {
qCDebug(QtWebEngineCore::lcWebEngineCompositor,
"EGL: MESA extensions and EGL_MESA_drm_image found, will use dma-buf with GEM "
"buffer allocation");
}
}
// Try to create dma-buf.
if (m_isDmaBufSupported) {
int fd = -1;
queryDmaBuf(2, 2, &fd, nullptr, nullptr, nullptr);
if (fd == -1)
m_isDmaBufSupported = false;
else
close(fd);
}
}
void EGLHelper::queryDmaBuf(const int width, const int height, int *fd, int *stride, int *offset,
uint64_t *modifiers)
{
if (!m_isDmaBufSupported)
return;
EGLImage eglImage = EGL_NO_IMAGE;
// Probably doesn't need to live to the end of the function, but just in case.
std::unique_ptr<ScopedGLContext> glContext;
if (m_isCreateDRMImageMesaSupported) {
// This approach is slightly worse for security and no longer supported in mesa 25.2,
// but it allows us to keep support for the Panthor driver prior to that mesa version.
// clang-format off
EGLint attribs[] = {
EGL_WIDTH, width,
EGL_HEIGHT, height,
EGL_DRM_BUFFER_FORMAT_MESA, EGL_DRM_BUFFER_FORMAT_ARGB32_MESA,
EGL_DRM_BUFFER_USE_MESA, EGL_DRM_BUFFER_USE_SHARE_MESA,
EGL_NONE
};
// clang-format on
eglImage = m_functions->eglCreateDRMImageMESA(m_eglDisplay, attribs);
} else {
glContext = std::make_unique<ScopedGLContext>(m_offscreenSurface.get(), m_functions.get());
if (!glContext->isValid())
return;
EGLContext eglContext = glContext->eglContext();
if (!eglContext) {
qWarning("EGL: No EGLContext.");
return;
}
uint64_t textureId = glContext->createTexture(width, height);
eglImage = m_functions->eglCreateImage(m_eglDisplay, eglContext, EGL_GL_TEXTURE_2D,
(EGLClientBuffer)textureId, NULL);
}
if (eglImage == EGL_NO_IMAGE) {
qWarning("EGL: Failed to create EGLImage: %s", getLastEGLErrorString());
return;
}
int numPlanes = 0;
if (!m_functions->eglExportDMABUFImageQueryMESA(m_eglDisplay, eglImage, nullptr, &numPlanes,
modifiers)) {
qWarning("EGL: Failed to retrieve the pixel format of the buffer: %s",
getLastEGLErrorString());
}
Q_ASSERT(numPlanes == 1);
if (!m_functions->eglExportDMABUFImageMESA(m_eglDisplay, eglImage, fd, stride, offset)) {
qWarning("EGL: Failed to retrieve the dma_buf file descriptor: %s",
getLastEGLErrorString());
}
m_functions->eglDestroyImage(m_eglDisplay, eglImage);
}
const char *EGLHelper::getLastEGLErrorString() const
{
return getEGLErrorString(m_functions->eglGetError());
}
QT_END_NAMESPACE
|