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 <QtCore/qtextstream.h> |
41 | #include <qpa/qwindowsysteminterface.h> |
42 | #include <qpa/qplatformintegration.h> |
43 | #include <private/qguiapplication_p.h> |
44 | #include <private/qwindow_p.h> |
45 | #ifndef QT_NO_OPENGL |
46 | # include <QtGui/private/qopenglcontext_p.h> |
47 | # include <QtGui/QOpenGLContext> |
48 | # include <QtOpenGL/private/qopenglcompositorbackingstore_p.h> |
49 | #endif |
50 | #include <QtGui/private/qeglconvenience_p.h> |
51 | |
52 | #include "qeglfswindow_p.h" |
53 | #ifndef QT_NO_OPENGL |
54 | # include "qeglfscursor_p.h" |
55 | #endif |
56 | #include "qeglfshooks_p.h" |
57 | #include "qeglfsdeviceintegration_p.h" |
58 | |
59 | QT_BEGIN_NAMESPACE |
60 | |
61 | QEglFSWindow::QEglFSWindow(QWindow *w) |
62 | : QPlatformWindow(w), |
63 | #ifndef QT_NO_OPENGL |
64 | m_backingStore(nullptr), |
65 | m_rasterCompositingContext(nullptr), |
66 | #endif |
67 | m_winId(0), |
68 | m_surface(EGL_NO_SURFACE), |
69 | m_window(0) |
70 | { |
71 | } |
72 | |
73 | QEglFSWindow::~QEglFSWindow() |
74 | { |
75 | destroy(); |
76 | } |
77 | |
78 | static WId newWId() |
79 | { |
80 | static WId id = 0; |
81 | |
82 | if (id == std::numeric_limits<WId>::max()) |
83 | qWarning("QEGLPlatformWindow: Out of window IDs" ); |
84 | |
85 | return ++id; |
86 | } |
87 | |
88 | void QEglFSWindow::create() |
89 | { |
90 | if (m_flags.testFlag(Created)) |
91 | return; |
92 | |
93 | m_winId = newWId(); |
94 | |
95 | if (window()->type() == Qt::Desktop) { |
96 | QRect fullscreenRect(QPoint(), screen()->availableGeometry().size()); |
97 | QWindowSystemInterface::handleGeometryChange(window(), fullscreenRect); |
98 | return; |
99 | } |
100 | |
101 | m_flags = Created; |
102 | |
103 | if (window()->type() == Qt::Desktop) |
104 | return; |
105 | |
106 | // Stop if there is already a window backed by a native window and surface. Additional |
107 | // raster windows will not have their own native window, surface and context. Instead, |
108 | // they will be composited onto the root window's surface. |
109 | QEglFSScreen *screen = this->screen(); |
110 | #ifndef QT_NO_OPENGL |
111 | QOpenGLCompositor *compositor = QOpenGLCompositor::instance(); |
112 | if (screen->primarySurface() != EGL_NO_SURFACE) { |
113 | if (Q_UNLIKELY(!isRaster() || !compositor->targetWindow())) { |
114 | #if !defined(Q_OS_ANDROID) || defined(Q_OS_ANDROID_EMBEDDED) |
115 | // We can have either a single OpenGL window or multiple raster windows. |
116 | // Other combinations cannot work. |
117 | qFatal("EGLFS: OpenGL windows cannot be mixed with others." ); |
118 | #endif |
119 | return; |
120 | } |
121 | m_format = compositor->targetWindow()->format(); |
122 | return; |
123 | } |
124 | #endif // QT_NO_OPENGL |
125 | |
126 | m_flags |= HasNativeWindow; |
127 | setGeometry(QRect()); // will become fullscreen |
128 | |
129 | resetSurface(); |
130 | |
131 | if (Q_UNLIKELY(m_surface == EGL_NO_SURFACE)) { |
132 | EGLint error = eglGetError(); |
133 | eglTerminate(screen->display()); |
134 | qFatal("EGL Error : Could not create the egl surface: error = 0x%x\n" , error); |
135 | } |
136 | |
137 | screen->setPrimarySurface(m_surface); |
138 | |
139 | #ifndef QT_NO_OPENGL |
140 | if (isRaster()) { |
141 | m_rasterCompositingContext = new QOpenGLContext; |
142 | m_rasterCompositingContext->setShareContext(qt_gl_global_share_context()); |
143 | m_rasterCompositingContext->setFormat(m_format); |
144 | m_rasterCompositingContext->setScreen(window()->screen()); |
145 | if (Q_UNLIKELY(!m_rasterCompositingContext->create())) |
146 | qFatal("EGLFS: Failed to create compositing context" ); |
147 | compositor->setTarget(m_rasterCompositingContext, window(), screen->rawGeometry()); |
148 | compositor->setRotation(qEnvironmentVariableIntValue("QT_QPA_EGLFS_ROTATION" )); |
149 | // If there is a "root" window into which raster and QOpenGLWidget content is |
150 | // composited, all other contexts must share with its context. |
151 | if (!qt_gl_global_share_context()) |
152 | qt_gl_set_global_share_context(m_rasterCompositingContext); |
153 | } |
154 | #endif // QT_NO_OPENGL |
155 | } |
156 | |
157 | void QEglFSWindow::destroy() |
158 | { |
159 | if (!m_flags.testFlag(Created)) |
160 | return; // already destroyed |
161 | |
162 | #ifndef QT_NO_OPENGL |
163 | QOpenGLCompositor::instance()->removeWindow(this); |
164 | #endif |
165 | |
166 | QEglFSScreen *screen = this->screen(); |
167 | if (m_flags.testFlag(HasNativeWindow)) { |
168 | #ifndef QT_NO_OPENGL |
169 | QEglFSCursor *cursor = qobject_cast<QEglFSCursor *>(screen->cursor()); |
170 | if (cursor) |
171 | cursor->resetResources(); |
172 | #endif |
173 | if (screen->primarySurface() == m_surface) |
174 | screen->setPrimarySurface(EGL_NO_SURFACE); |
175 | |
176 | invalidateSurface(); |
177 | |
178 | #ifndef QT_NO_OPENGL |
179 | QOpenGLCompositor::destroy(); |
180 | delete m_rasterCompositingContext; |
181 | #endif |
182 | } |
183 | |
184 | m_flags = { }; |
185 | } |
186 | |
187 | void QEglFSWindow::invalidateSurface() |
188 | { |
189 | if (m_surface != EGL_NO_SURFACE) { |
190 | eglDestroySurface(screen()->display(), m_surface); |
191 | m_surface = EGL_NO_SURFACE; |
192 | } |
193 | qt_egl_device_integration()->destroyNativeWindow(m_window); |
194 | m_window = 0; |
195 | } |
196 | |
197 | void QEglFSWindow::resetSurface() |
198 | { |
199 | EGLDisplay display = screen()->display(); |
200 | QSurfaceFormat platformFormat = qt_egl_device_integration()->surfaceFormatFor(window()->requestedFormat()); |
201 | |
202 | m_config = QEglFSDeviceIntegration::chooseConfig(display, platformFormat); |
203 | m_format = q_glFormatFromConfig(display, m_config, platformFormat); |
204 | const QSize surfaceSize = screen()->rawGeometry().size(); |
205 | m_window = qt_egl_device_integration()->createNativeWindow(this, surfaceSize, m_format); |
206 | m_surface = eglCreateWindowSurface(display, m_config, m_window, nullptr); |
207 | } |
208 | |
209 | void QEglFSWindow::setVisible(bool visible) |
210 | { |
211 | #ifndef QT_NO_OPENGL |
212 | QOpenGLCompositor *compositor = QOpenGLCompositor::instance(); |
213 | QList<QOpenGLCompositorWindow *> windows = compositor->windows(); |
214 | QWindow *wnd = window(); |
215 | |
216 | if (wnd->type() != Qt::Desktop) { |
217 | if (visible) { |
218 | compositor->addWindow(this); |
219 | } else { |
220 | compositor->removeWindow(this); |
221 | windows = compositor->windows(); |
222 | if (windows.size()) |
223 | windows.last()->sourceWindow()->requestActivate(); |
224 | } |
225 | } |
226 | #else |
227 | QWindow *wnd = window(); |
228 | #endif |
229 | QWindowSystemInterface::handleExposeEvent(wnd, QRect(QPoint(0, 0), wnd->geometry().size())); |
230 | |
231 | if (visible) |
232 | QWindowSystemInterface::flushWindowSystemEvents(QEventLoop::ExcludeUserInputEvents); |
233 | } |
234 | |
235 | void QEglFSWindow::setGeometry(const QRect &r) |
236 | { |
237 | QRect rect = r; |
238 | if (m_flags.testFlag(HasNativeWindow)) |
239 | rect = screen()->availableGeometry(); |
240 | |
241 | QPlatformWindow::setGeometry(rect); |
242 | |
243 | QWindowSystemInterface::handleGeometryChange(window(), rect); |
244 | |
245 | const QRect lastReportedGeometry = qt_window_private(window())->geometry; |
246 | if (rect != lastReportedGeometry) |
247 | QWindowSystemInterface::handleExposeEvent(window(), QRect(QPoint(0, 0), rect.size())); |
248 | } |
249 | |
250 | QRect QEglFSWindow::geometry() const |
251 | { |
252 | // For yet-to-become-fullscreen windows report the geometry covering the entire |
253 | // screen. This is particularly important for Quick where the root object may get |
254 | // sized to some geometry queried before calling create(). |
255 | if (!m_flags.testFlag(Created) && screen()->primarySurface() == EGL_NO_SURFACE) |
256 | return screen()->availableGeometry(); |
257 | |
258 | return QPlatformWindow::geometry(); |
259 | } |
260 | |
261 | void QEglFSWindow::requestActivateWindow() |
262 | { |
263 | #ifndef QT_NO_OPENGL |
264 | if (window()->type() != Qt::Desktop) |
265 | QOpenGLCompositor::instance()->moveToTop(this); |
266 | #endif |
267 | QWindow *wnd = window(); |
268 | QWindowSystemInterface::handleWindowActivated(wnd); |
269 | QWindowSystemInterface::handleExposeEvent(wnd, QRect(QPoint(0, 0), wnd->geometry().size())); |
270 | } |
271 | |
272 | void QEglFSWindow::raise() |
273 | { |
274 | QWindow *wnd = window(); |
275 | if (wnd->type() != Qt::Desktop) { |
276 | #ifndef QT_NO_OPENGL |
277 | QOpenGLCompositor::instance()->moveToTop(this); |
278 | #endif |
279 | QWindowSystemInterface::handleExposeEvent(wnd, QRect(QPoint(0, 0), wnd->geometry().size())); |
280 | } |
281 | } |
282 | |
283 | void QEglFSWindow::lower() |
284 | { |
285 | #ifndef QT_NO_OPENGL |
286 | QOpenGLCompositor *compositor = QOpenGLCompositor::instance(); |
287 | QList<QOpenGLCompositorWindow *> windows = compositor->windows(); |
288 | if (window()->type() != Qt::Desktop && windows.count() > 1) { |
289 | int idx = windows.indexOf(this); |
290 | if (idx > 0) { |
291 | compositor->changeWindowIndex(this, idx - 1); |
292 | QWindowSystemInterface::handleExposeEvent(windows.last()->sourceWindow(), |
293 | QRect(QPoint(0, 0), windows.last()->sourceWindow()->geometry().size())); |
294 | } |
295 | } |
296 | #endif |
297 | } |
298 | |
299 | EGLSurface QEglFSWindow::surface() const |
300 | { |
301 | return m_surface != EGL_NO_SURFACE ? m_surface : screen()->primarySurface(); |
302 | } |
303 | |
304 | QSurfaceFormat QEglFSWindow::format() const |
305 | { |
306 | return m_format; |
307 | } |
308 | |
309 | EGLNativeWindowType QEglFSWindow::eglWindow() const |
310 | { |
311 | return m_window; |
312 | } |
313 | |
314 | QEglFSScreen *QEglFSWindow::screen() const |
315 | { |
316 | return static_cast<QEglFSScreen *>(QPlatformWindow::screen()); |
317 | } |
318 | |
319 | bool QEglFSWindow::isRaster() const |
320 | { |
321 | const QWindow::SurfaceType type = window()->surfaceType(); |
322 | return type == QSurface::RasterSurface || type == QSurface::RasterGLSurface; |
323 | } |
324 | |
325 | #ifndef QT_NO_OPENGL |
326 | QWindow *QEglFSWindow::sourceWindow() const |
327 | { |
328 | return window(); |
329 | } |
330 | |
331 | const QPlatformTextureList *QEglFSWindow::textures() const |
332 | { |
333 | if (m_backingStore) |
334 | return m_backingStore->textures(); |
335 | |
336 | return nullptr; |
337 | } |
338 | |
339 | void QEglFSWindow::endCompositing() |
340 | { |
341 | if (m_backingStore) |
342 | m_backingStore->notifyComposited(); |
343 | } |
344 | #endif |
345 | |
346 | WId QEglFSWindow::winId() const |
347 | { |
348 | return m_winId; |
349 | } |
350 | |
351 | void QEglFSWindow::setOpacity(qreal) |
352 | { |
353 | if (!isRaster()) |
354 | qWarning("QEglFSWindow: Cannot set opacity for non-raster windows" ); |
355 | |
356 | // Nothing to do here. The opacity is stored in the QWindow. |
357 | } |
358 | |
359 | QT_END_NAMESPACE |
360 | |