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(m_eglDisplay);
75}
76
77void QWaylandEglClientBufferIntegration::initialize(QWaylandDisplay *display)
78{
79 if (q_hasEglExtension(EGL_NO_DISPLAY, "EGL_EXT_platform_base")) {
80 if (q_hasEglExtension(EGL_NO_DISPLAY, "EGL_KHR_platform_wayland") ||
81 q_hasEglExtension(EGL_NO_DISPLAY, "EGL_EXT_platform_wayland") ||
82 q_hasEglExtension(EGL_NO_DISPLAY, "EGL_MESA_platform_wayland")) {
83
84 static PFNEGLGETPLATFORMDISPLAYEXTPROC eglGetPlatformDisplay = nullptr;
85 if (!eglGetPlatformDisplay)
86 eglGetPlatformDisplay = (PFNEGLGETPLATFORMDISPLAYEXTPROC)eglGetProcAddress("eglGetPlatformDisplayEXT");
87
88 m_eglDisplay = eglGetPlatformDisplay(EGL_PLATFORM_WAYLAND_KHR, display->wl_display(), nullptr);
89 } else {
90 qCWarning(lcQpaWayland) << "The EGL implementation does not support the Wayland platform";
91 return;
92 }
93 } else {
94 QByteArray eglPlatform = qgetenv("EGL_PLATFORM");
95 if (eglPlatform.isEmpty()) {
96 setenv("EGL_PLATFORM","wayland",true);
97 }
98
99 m_eglDisplay = eglGetDisplay((EGLNativeDisplayType) display->wl_display());
100 }
101
102 m_display = display;
103
104 if (m_eglDisplay == EGL_NO_DISPLAY) {
105 qCWarning(lcQpaWayland) << "EGL not available";
106 return;
107 }
108
109 EGLint major,minor;
110 if (!eglInitialize(m_eglDisplay, &major, &minor)) {
111 qCWarning(lcQpaWayland) << "Failed to initialize EGL display" << hex << eglGetError();
112 m_eglDisplay = EGL_NO_DISPLAY;
113 return;
114 }
115
116 m_supportsThreading = true;
117 if (qEnvironmentVariableIsSet("QT_OPENGL_NO_SANITY_CHECK"))
118 return;
119
120 const char *vendor = eglQueryString(m_eglDisplay, EGL_VENDOR);
121 for (int i = 0; qwaylandegl_threadedgl_blacklist_vendor[i]; ++i) {
122 if (strstr(vendor, qwaylandegl_threadedgl_blacklist_vendor[i]) != 0) {
123 m_supportsThreading = false;
124 break;
125 }
126 }
127}
128
129bool QWaylandEglClientBufferIntegration::isValid() const
130{
131 return m_eglDisplay != EGL_NO_DISPLAY;
132}
133
134bool QWaylandEglClientBufferIntegration::supportsThreadedOpenGL() const
135{
136 return m_supportsThreading;
137}
138
139bool QWaylandEglClientBufferIntegration::supportsWindowDecoration() const
140{
141 return true;
142}
143
144QWaylandWindow *QWaylandEglClientBufferIntegration::createEglWindow(QWindow *window)
145{
146 return new QWaylandEglWindow(window);
147}
148
149QPlatformOpenGLContext *QWaylandEglClientBufferIntegration::createPlatformOpenGLContext(const QSurfaceFormat &glFormat, QPlatformOpenGLContext *share) const
150{
151 return new QWaylandGLContext(m_eglDisplay, m_display, glFormat, share);
152}
153
154void *QWaylandEglClientBufferIntegration::nativeResource(NativeResource resource)
155{
156 switch (resource) {
157 case EglDisplay:
158 return m_eglDisplay;
159 default:
160 break;
161 }
162 return nullptr;
163}
164
165void *QWaylandEglClientBufferIntegration::nativeResourceForContext(NativeResource resource, QPlatformOpenGLContext *context)
166{
167 Q_ASSERT(context);
168 switch (resource) {
169 case EglConfig:
170 return static_cast<QWaylandGLContext *>(context)->eglConfig();
171 case EglContext:
172 return static_cast<QWaylandGLContext *>(context)->eglContext();
173 case EglDisplay:
174 return m_eglDisplay;
175 default:
176 break;
177 }
178 return nullptr;
179}
180
181EGLDisplay QWaylandEglClientBufferIntegration::eglDisplay() const
182{
183 return m_eglDisplay;
184}
185
186}
187
188QT_END_NAMESPACE
189