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 "qwaylandeglwindow.h" |
41 | |
42 | #include <QtWaylandClient/private/qwaylandscreen_p.h> |
43 | #include "qwaylandglcontext.h" |
44 | |
45 | #include <QtEglSupport/private/qeglconvenience_p.h> |
46 | |
47 | #include <QDebug> |
48 | #include <QtGui/QWindow> |
49 | #include <qpa/qwindowsysteminterface.h> |
50 | #include <QOpenGLFramebufferObject> |
51 | #include <QOpenGLContext> |
52 | |
53 | QT_BEGIN_NAMESPACE |
54 | |
55 | namespace QtWaylandClient { |
56 | |
57 | QWaylandEglWindow::QWaylandEglWindow(QWindow *window, QWaylandDisplay *display) |
58 | : QWaylandWindow(window, display) |
59 | , m_clientBufferIntegration(static_cast<QWaylandEglClientBufferIntegration *>(mDisplay->clientBufferIntegration())) |
60 | , m_format(window->requestedFormat()) |
61 | { |
62 | } |
63 | |
64 | QWaylandEglWindow::~QWaylandEglWindow() |
65 | { |
66 | if (m_eglSurface) { |
67 | eglDestroySurface(dpy: m_clientBufferIntegration->eglDisplay(), surface: m_eglSurface); |
68 | m_eglSurface = 0; |
69 | } |
70 | |
71 | if (m_waylandEglWindow) |
72 | wl_egl_window_destroy(egl_window: m_waylandEglWindow); |
73 | |
74 | delete m_contentFBO; |
75 | } |
76 | |
77 | QWaylandWindow::WindowType QWaylandEglWindow::windowType() const |
78 | { |
79 | return QWaylandWindow::Egl; |
80 | } |
81 | |
82 | void QWaylandEglWindow::ensureSize() |
83 | { |
84 | updateSurface(create: false); |
85 | } |
86 | |
87 | void QWaylandEglWindow::setGeometry(const QRect &rect) |
88 | { |
89 | QWaylandWindow::setGeometry(rect); |
90 | // If the surface was invalidated through invalidateSurface() and |
91 | // we're now getting a resize we don't want to create it again. |
92 | // Just resize the wl_egl_window, the EGLSurface will be created |
93 | // the next time makeCurrent is called. |
94 | updateSurface(create: false); |
95 | } |
96 | |
97 | void QWaylandEglWindow::updateSurface(bool create) |
98 | { |
99 | QMargins margins = frameMargins(); |
100 | QRect rect = geometry(); |
101 | QSize sizeWithMargins = (rect.size() + QSize(margins.left() + margins.right(), margins.top() + margins.bottom())) * scale(); |
102 | |
103 | // wl_egl_windows must have both width and height > 0 |
104 | // mesa's egl returns NULL if we try to create a, invalid wl_egl_window, however not all EGL |
105 | // implementations may do that, so check the size ourself. Besides, we must deal with resizing |
106 | // a valid window to 0x0, which would make it invalid. Hence, destroy it. |
107 | if (sizeWithMargins.isEmpty()) { |
108 | if (m_eglSurface) { |
109 | eglDestroySurface(dpy: m_clientBufferIntegration->eglDisplay(), surface: m_eglSurface); |
110 | m_eglSurface = 0; |
111 | } |
112 | if (m_waylandEglWindow) { |
113 | wl_egl_window_destroy(egl_window: m_waylandEglWindow); |
114 | m_waylandEglWindow = 0; |
115 | } |
116 | mOffset = QPoint(); |
117 | } else { |
118 | if (m_waylandEglWindow) { |
119 | int current_width, current_height; |
120 | static bool disableResizeCheck = qgetenv(varName: "QT_WAYLAND_DISABLE_RESIZECHECK" ).toInt(); |
121 | |
122 | if (!disableResizeCheck) { |
123 | wl_egl_window_get_attached_size(egl_window: m_waylandEglWindow, width: ¤t_width, height: ¤t_height); |
124 | } |
125 | if (disableResizeCheck || (current_width != sizeWithMargins.width() || current_height != sizeWithMargins.height())) { |
126 | wl_egl_window_resize(egl_window: m_waylandEglWindow, width: sizeWithMargins.width(), height: sizeWithMargins.height(), dx: mOffset.x(), dy: mOffset.y()); |
127 | mOffset = QPoint(); |
128 | |
129 | m_resize = true; |
130 | } |
131 | } else if (create && wlSurface()) { |
132 | m_waylandEglWindow = wl_egl_window_create(wlSurface(), sizeWithMargins.width(), sizeWithMargins.height()); |
133 | } |
134 | |
135 | if (!m_eglSurface && m_waylandEglWindow && create) { |
136 | EGLNativeWindowType eglw = (EGLNativeWindowType) m_waylandEglWindow; |
137 | QSurfaceFormat fmt = window()->requestedFormat(); |
138 | |
139 | if (mDisplay->supportsWindowDecoration()) |
140 | fmt.setAlphaBufferSize(8); |
141 | EGLConfig eglConfig = q_configFromGLFormat(display: m_clientBufferIntegration->eglDisplay(), format: fmt); |
142 | m_format = q_glFormatFromConfig(display: m_clientBufferIntegration->eglDisplay(), config: eglConfig); |
143 | m_eglSurface = eglCreateWindowSurface(dpy: m_clientBufferIntegration->eglDisplay(), config: eglConfig, win: eglw, attrib_list: 0); |
144 | if (Q_UNLIKELY(m_eglSurface == EGL_NO_SURFACE)) |
145 | qCWarning(lcQpaWayland, "Could not create EGL surface (EGL error 0x%x)\n" , eglGetError()); |
146 | } |
147 | } |
148 | } |
149 | |
150 | QRect QWaylandEglWindow::contentsRect() const |
151 | { |
152 | QRect r = geometry(); |
153 | QMargins m = frameMargins(); |
154 | return QRect(m.left(), m.bottom(), r.width(), r.height()); |
155 | } |
156 | |
157 | QSurfaceFormat QWaylandEglWindow::format() const |
158 | { |
159 | return m_format; |
160 | } |
161 | |
162 | void QWaylandEglWindow::invalidateSurface() |
163 | { |
164 | if (m_eglSurface) { |
165 | eglDestroySurface(dpy: m_clientBufferIntegration->eglDisplay(), surface: m_eglSurface); |
166 | m_eglSurface = 0; |
167 | } |
168 | if (m_waylandEglWindow) { |
169 | wl_egl_window_destroy(egl_window: m_waylandEglWindow); |
170 | m_waylandEglWindow = nullptr; |
171 | } |
172 | } |
173 | |
174 | EGLSurface QWaylandEglWindow::eglSurface() const |
175 | { |
176 | return m_eglSurface; |
177 | } |
178 | |
179 | GLuint QWaylandEglWindow::contentFBO() const |
180 | { |
181 | if (!decoration()) |
182 | return 0; |
183 | |
184 | if (m_resize || !m_contentFBO) { |
185 | QOpenGLFramebufferObject *old = m_contentFBO; |
186 | QSize fboSize = geometry().size() * scale(); |
187 | m_contentFBO = new QOpenGLFramebufferObject(fboSize.width(), fboSize.height(), QOpenGLFramebufferObject::CombinedDepthStencil); |
188 | |
189 | delete old; |
190 | m_resize = false; |
191 | } |
192 | |
193 | return m_contentFBO->handle(); |
194 | } |
195 | |
196 | GLuint QWaylandEglWindow::contentTexture() const |
197 | { |
198 | return m_contentFBO->texture(); |
199 | } |
200 | |
201 | void QWaylandEglWindow::bindContentFBO() |
202 | { |
203 | if (decoration()) { |
204 | contentFBO(); |
205 | m_contentFBO->bind(); |
206 | } |
207 | } |
208 | |
209 | } |
210 | |
211 | QT_END_NAMESPACE |
212 | |