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 <QtPlatformCompositorSupport/private/qopenglcompositorbackingstore_p.h>
49#endif
50#include <QtEglSupport/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
59QT_BEGIN_NAMESPACE
60
61QEglFSWindow::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
73QEglFSWindow::~QEglFSWindow()
74{
75 destroy();
76}
77
78static WId newWId()
79{
80 static WId id = 0;
81
82 if (id == std::numeric_limits<WId>::max())
83 qWarning(msg: "QEGLPlatformWindow: Out of window IDs");
84
85 return ++id;
86}
87
88void QEglFSWindow::create()
89{
90 if (m_flags.testFlag(flag: 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: window(), newRect: 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(msg: "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(dpy: screen->display());
134 qFatal(msg: "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(msg: "EGLFS: Failed to create compositing context");
147 compositor->setTarget(context: m_rasterCompositingContext, window: window(), nativeTargetGeometry: screen->rawGeometry());
148 compositor->setRotation(qEnvironmentVariableIntValue(varName: "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(context: m_rasterCompositingContext);
153 }
154#endif // QT_NO_OPENGL
155}
156
157void QEglFSWindow::destroy()
158{
159 if (!m_flags.testFlag(flag: Created))
160 return; // already destroyed
161
162#ifndef QT_NO_OPENGL
163 QOpenGLCompositor::instance()->removeWindow(window: this);
164#endif
165
166 QEglFSScreen *screen = this->screen();
167 if (m_flags.testFlag(flag: HasNativeWindow)) {
168#ifndef QT_NO_OPENGL
169 QEglFSCursor *cursor = qobject_cast<QEglFSCursor *>(object: 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
187void QEglFSWindow::invalidateSurface()
188{
189 if (m_surface != EGL_NO_SURFACE) {
190 eglDestroySurface(dpy: screen()->display(), surface: m_surface);
191 m_surface = EGL_NO_SURFACE;
192 }
193 qt_egl_device_integration()->destroyNativeWindow(window: m_window);
194 m_window = 0;
195}
196
197void QEglFSWindow::resetSurface()
198{
199 EGLDisplay display = screen()->display();
200 QSurfaceFormat platformFormat = qt_egl_device_integration()->surfaceFormatFor(inputFormat: window()->requestedFormat());
201
202 m_config = QEglFSDeviceIntegration::chooseConfig(display, format: platformFormat);
203 m_format = q_glFormatFromConfig(display, config: m_config, referenceFormat: platformFormat);
204 const QSize surfaceSize = screen()->rawGeometry().size();
205 m_window = qt_egl_device_integration()->createNativeWindow(platformWindow: this, size: surfaceSize, format: m_format);
206 m_surface = eglCreateWindowSurface(dpy: display, config: m_config, win: m_window, attrib_list: nullptr);
207}
208
209void 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(window: this);
219 } else {
220 compositor->removeWindow(window: 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(window: wnd, region: QRect(QPoint(0, 0), wnd->geometry().size()));
230
231 if (visible)
232 QWindowSystemInterface::flushWindowSystemEvents(flags: QEventLoop::ExcludeUserInputEvents);
233}
234
235void QEglFSWindow::setGeometry(const QRect &r)
236{
237 QRect rect = r;
238 if (m_flags.testFlag(flag: HasNativeWindow))
239 rect = screen()->availableGeometry();
240
241 QPlatformWindow::setGeometry(rect);
242
243 QWindowSystemInterface::handleGeometryChange(window: window(), newRect: rect);
244
245 const QRect lastReportedGeometry = qt_window_private(window: window())->geometry;
246 if (rect != lastReportedGeometry)
247 QWindowSystemInterface::handleExposeEvent(window: window(), region: QRect(QPoint(0, 0), rect.size()));
248}
249
250QRect 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(flag: Created) && screen()->primarySurface() == EGL_NO_SURFACE)
256 return screen()->availableGeometry();
257
258 return QPlatformWindow::geometry();
259}
260
261void QEglFSWindow::requestActivateWindow()
262{
263#ifndef QT_NO_OPENGL
264 if (window()->type() != Qt::Desktop)
265 QOpenGLCompositor::instance()->moveToTop(window: this);
266#endif
267 QWindow *wnd = window();
268 QWindowSystemInterface::handleWindowActivated(window: wnd);
269 QWindowSystemInterface::handleExposeEvent(window: wnd, region: QRect(QPoint(0, 0), wnd->geometry().size()));
270}
271
272void QEglFSWindow::raise()
273{
274 QWindow *wnd = window();
275 if (wnd->type() != Qt::Desktop) {
276#ifndef QT_NO_OPENGL
277 QOpenGLCompositor::instance()->moveToTop(window: this);
278#endif
279 QWindowSystemInterface::handleExposeEvent(window: wnd, region: QRect(QPoint(0, 0), wnd->geometry().size()));
280 }
281}
282
283void 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(t: this);
290 if (idx > 0) {
291 compositor->changeWindowIndex(window: this, newIdx: idx - 1);
292 QWindowSystemInterface::handleExposeEvent(window: windows.last()->sourceWindow(),
293 region: QRect(QPoint(0, 0), windows.last()->sourceWindow()->geometry().size()));
294 }
295 }
296#endif
297}
298
299EGLSurface QEglFSWindow::surface() const
300{
301 return m_surface != EGL_NO_SURFACE ? m_surface : screen()->primarySurface();
302}
303
304QSurfaceFormat QEglFSWindow::format() const
305{
306 return m_format;
307}
308
309EGLNativeWindowType QEglFSWindow::eglWindow() const
310{
311 return m_window;
312}
313
314QEglFSScreen *QEglFSWindow::screen() const
315{
316 return static_cast<QEglFSScreen *>(QPlatformWindow::screen());
317}
318
319bool 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
326QWindow *QEglFSWindow::sourceWindow() const
327{
328 return window();
329}
330
331const QPlatformTextureList *QEglFSWindow::textures() const
332{
333 if (m_backingStore)
334 return m_backingStore->textures();
335
336 return nullptr;
337}
338
339void QEglFSWindow::endCompositing()
340{
341 if (m_backingStore)
342 m_backingStore->notifyComposited();
343}
344#endif
345
346WId QEglFSWindow::winId() const
347{
348 return m_winId;
349}
350
351void QEglFSWindow::setOpacity(qreal)
352{
353 if (!isRaster())
354 qWarning(msg: "QEglFSWindow: Cannot set opacity for non-raster windows");
355
356 // Nothing to do here. The opacity is stored in the QWindow.
357}
358
359QT_END_NAMESPACE
360

source code of qtbase/src/plugins/platforms/eglfs/api/qeglfswindow.cpp