1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the plugins of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#include "qwaylandeglclientbufferintegration.h"
41
42#include "qwaylandeglwindow.h"
43#include "qwaylandglcontext.h"
44
45#include <wayland-client-core.h>
46
47#include <QtCore/QDebug>
48#include <private/qeglconvenience_p.h>
49
50#ifndef EGL_EXT_platform_base
51typedef EGLDisplay (*PFNEGLGETPLATFORMDISPLAYEXTPROC) (EGLenum platform, void *native_display, const EGLint *attrib_list);
52#endif
53
54#ifndef EGL_PLATFORM_WAYLAND_KHR
55#define EGL_PLATFORM_WAYLAND_KHR 0x31D8
56#endif
57
58QT_BEGIN_NAMESPACE
59
60namespace QtWaylandClient {
61
62static const char *qwaylandegl_threadedgl_blacklist_vendor[] = {
63 0
64};
65
66QWaylandEglClientBufferIntegration::QWaylandEglClientBufferIntegration()
67{
68 qCDebug(lcQpaWayland) << "Using Wayland-EGL";
69}
70
71
72QWaylandEglClientBufferIntegration::~QWaylandEglClientBufferIntegration()
73{
74 eglTerminate(dpy: m_eglDisplay);
75}
76
77void QWaylandEglClientBufferIntegration::initialize(QWaylandDisplay *display)
78{
79#if QT_CONFIG(egl_extension_platform_wayland)
80 m_eglDisplay = eglGetPlatformDisplay(EGL_PLATFORM_WAYLAND_EXT, native_display: display->wl_display(), attrib_list: nullptr);
81#else
82 if (q_hasEglExtension(EGL_NO_DISPLAY, "EGL_EXT_platform_base")) {
83 if (q_hasEglExtension(EGL_NO_DISPLAY, "EGL_KHR_platform_wayland") ||
84 q_hasEglExtension(EGL_NO_DISPLAY, "EGL_EXT_platform_wayland") ||
85 q_hasEglExtension(EGL_NO_DISPLAY, "EGL_MESA_platform_wayland")) {
86
87 static PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplay = nullptr;
88 if (!eglGetPlatformDisplay)
89 eglGetPlatformDisplay = (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDisplayEXT");
90
91 m_eglDisplay = eglGetPlatformDisplay(EGL_PLATFORM_WAYLAND_KHR, display->wl_display(), nullptr);
92 } else {
93 qCWarning(lcQpaWayland) << "The EGL implementation does not support the Wayland platform";
94 return;
95 }
96 } else {
97 QByteArray eglPlatform = qgetenv("EGL_PLATFORM");
98 if (eglPlatform.isEmpty()) {
99 setenv("EGL_PLATFORM","wayland",true);
100 }
101
102 m_eglDisplay = eglGetDisplay((EGLNativeDisplayType) display->wl_display());
103 }
104#endif
105
106 m_display = display;
107
108 if (m_eglDisplay == EGL_NO_DISPLAY) {
109 qCWarning(lcQpaWayland) << "EGL not available";
110 return;
111 }
112
113 EGLint major,minor;
114 if (!eglInitialize(dpy: m_eglDisplay, major: &major, minor: &minor)) {
115 qCWarning(lcQpaWayland) << "Failed to initialize EGL display" << Qt::hex << eglGetError();
116 m_eglDisplay = EGL_NO_DISPLAY;
117 return;
118 }
119
120 m_supportsThreading = true;
121 if (qEnvironmentVariableIsSet(varName: "QT_OPENGL_NO_SANITY_CHECK"))
122 return;
123
124 const char *vendor = eglQueryString(dpy: m_eglDisplay, EGL_VENDOR);
125 for (int i = 0; qwaylandegl_threadedgl_blacklist_vendor[i]; ++i) {
126 if (strstr(haystack: vendor, needle: qwaylandegl_threadedgl_blacklist_vendor[i]) != 0) {
127 m_supportsThreading = false;
128 break;
129 }
130 }
131}
132
133bool QWaylandEglClientBufferIntegration::isValid() const
134{
135 return m_eglDisplay != EGL_NO_DISPLAY;
136}
137
138bool QWaylandEglClientBufferIntegration::supportsThreadedOpenGL() const
139{
140 return m_supportsThreading;
141}
142
143bool QWaylandEglClientBufferIntegration::supportsWindowDecoration() const
144{
145 return true;
146}
147
148QWaylandWindow *QWaylandEglClientBufferIntegration::createEglWindow(QWindow *window)
149{
150 return new QWaylandEglWindow(window, m_display);
151}
152
153QPlatformOpenGLContext *QWaylandEglClientBufferIntegration::createPlatformOpenGLContext(const QSurfaceFormat &glFormat, QPlatformOpenGLContext *share) const
154{
155 return new QWaylandGLContext(m_eglDisplay, m_display, glFormat, share);
156}
157
158void *QWaylandEglClientBufferIntegration::nativeResource(NativeResource resource)
159{
160 switch (resource) {
161 case EglDisplay:
162 return m_eglDisplay;
163 default:
164 break;
165 }
166 return nullptr;
167}
168
169void *QWaylandEglClientBufferIntegration::nativeResourceForContext(NativeResource resource, QPlatformOpenGLContext *context)
170{
171 Q_ASSERT(context);
172 switch (resource) {
173 case EglConfig:
174 return static_cast<QWaylandGLContext *>(context)->eglConfig();
175 case EglContext:
176 return static_cast<QWaylandGLContext *>(context)->eglContext();
177 case EglDisplay:
178 return m_eglDisplay;
179 default:
180 break;
181 }
182 return nullptr;
183}
184
185EGLDisplay QWaylandEglClientBufferIntegration::eglDisplay() const
186{
187 return m_eglDisplay;
188}
189
190}
191
192QT_END_NAMESPACE
193

source code of qtwayland/src/hardwareintegration/client/wayland-egl/qwaylandeglclientbufferintegration.cpp