QtBase  v6.3.1
qeglfskmsvsp2integration.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2015 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com>
4 ** Copyright (C) 2017 The Qt Company Ltd.
5 ** Copyright (C) 2016 Pelagicore AG
6 ** Contact: https://www.qt.io/licensing/
7 **
8 ** This file is part of the plugins of the Qt Toolkit.
9 **
10 ** $QT_BEGIN_LICENSE:LGPL$
11 ** Commercial License Usage
12 ** Licensees holding valid commercial Qt licenses may use this file in
13 ** accordance with the commercial license agreement provided with the
14 ** Software or, alternatively, in accordance with the terms contained in
15 ** a written agreement between you and The Qt Company. For licensing terms
16 ** and conditions see https://www.qt.io/terms-conditions. For further
17 ** information use the contact form at https://www.qt.io/contact-us.
18 **
19 ** GNU Lesser General Public License Usage
20 ** Alternatively, this file may be used under the terms of the GNU Lesser
21 ** General Public License version 3 as published by the Free Software
22 ** Foundation and appearing in the file LICENSE.LGPL3 included in the
23 ** packaging of this file. Please review the following information to
24 ** ensure the GNU Lesser General Public License version 3 requirements
25 ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
26 **
27 ** GNU General Public License Usage
28 ** Alternatively, this file may be used under the terms of the GNU
29 ** General Public License version 2.0 or (at your option) the GNU General
30 ** Public license version 3 or any later version approved by the KDE Free
31 ** Qt Foundation. The licenses are as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
33 ** included in the packaging of this file. Please review the following
34 ** information to ensure the GNU General Public License requirements will
35 ** be met: https://www.gnu.org/licenses/gpl-2.0.html and
36 ** https://www.gnu.org/licenses/gpl-3.0.html.
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
43 #include "qeglfskmsvsp2device.h"
44 #include "qeglfskmsvsp2screen.h"
45 #include "private/qeglfswindow_p.h"
46 
47 #include <QtDeviceDiscoverySupport/private/qdevicediscovery_p.h>
48 #include <QtGui/private/qeglconvenience_p.h>
49 #include <QtCore/QLoggingCategory>
50 #include <QtCore/QJsonDocument>
51 #include <QtCore/QJsonObject>
52 #include <QtCore/QJsonArray>
53 #include <QtGui/qpa/qplatformwindow.h>
54 #include <QtGui/QScreen>
55 
56 #include <xf86drm.h>
57 #include <xf86drmMode.h>
58 #include <gbm.h>
59 
61 
63 {
64  qCDebug(qLcEglfsKmsDebug, "New DRM/KMS via Vsp2 integration created");
65 }
66 
67 #ifndef EGL_EXT_platform_base
68 typedef EGLDisplay (EGLAPIENTRYP PFNEGLGETPLATFORMDISPLAYEXTPROC) (EGLenum platform, void *native_display, const EGLint *attrib_list);
69 typedef EGLSurface (EGLAPIENTRYP PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC) (EGLDisplay dpy, EGLConfig config, void *native_window, const EGLint *attrib_list);
70 #endif
71 
72 #ifndef EGL_PLATFORM_GBM_KHR
73 #define EGL_PLATFORM_GBM_KHR 0x31D7
74 #endif
75 
76 EGLDisplay QEglFSKmsVsp2Integration::createDisplay(EGLNativeDisplayType nativeDisplay)
77 {
78  qCDebug(qLcEglfsKmsDebug, "Querying EGLDisplay");
79  EGLDisplay display;
80 
81  PFNEGLGETPLATFORMDISPLAYEXTPROC getPlatformDisplay = nullptr;
82  const char *extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
83  if (extensions && (strstr(extensions, "EGL_KHR_platform_gbm") || strstr(extensions, "EGL_MESA_platform_gbm"))) {
84  getPlatformDisplay = reinterpret_cast<PFNEGLGETPLATFORMDISPLAYEXTPROC>(
85  eglGetProcAddress("eglGetPlatformDisplayEXT"));
86  }
87 
88  if (getPlatformDisplay) {
89  display = getPlatformDisplay(EGL_PLATFORM_GBM_KHR, nativeDisplay, nullptr);
90  } else {
91  qCDebug(qLcEglfsKmsDebug, "No eglGetPlatformDisplay for GBM, falling back to eglGetDisplay");
92  display = eglGetDisplay(nativeDisplay);
93  }
94 
95  return display;
96 }
97 
99 {
100  Q_UNUSED(format);
101  Q_ASSERT(device());
102 
103  gbm_surface *surface = gbm_surface_create(static_cast<QEglFSKmsVsp2Device *>(device())->gbmDevice(),
104  1, 1,
105  GBM_FORMAT_XRGB8888,
106  GBM_BO_USE_RENDERING);
107 
108  return reinterpret_cast<EGLNativeWindowType>(surface);
109 }
110 
112 {
113  gbm_surface *surface = reinterpret_cast<gbm_surface *>(window);
114  //TODO call screen destroysurface instead
115  gbm_surface_destroy(surface);
116 }
117 
119 {
120  QWindow *window = static_cast<QWindow *>(surface->surface());
121  auto *screen = static_cast<QEglFSKmsVsp2Screen *>(window->screen()->handle());
122  screen->flip();
123 }
124 
126 {
127  QString path = screenConfig()->devicePath();
128  if (!path.isEmpty()) {
129  qCDebug(qLcEglfsKmsDebug) << "VSP2: Using DRM device" << path << "specified in config file";
130  } else {
132  const QStringList devices = d->scanConnectedDevices();
133  qCDebug(qLcEglfsKmsDebug) << "Found the following video devices:" << devices;
134  d->deleteLater();
135 
136  if (Q_UNLIKELY(devices.isEmpty()))
137  qFatal("Could not find DRM device!");
138 
139  path = devices.first();
140  qCDebug(qLcEglfsKmsDebug) << "Using" << path;
141  }
142 
143  return new QEglFSKmsVsp2Device(screenConfig(), path);
144 }
145 
147 {
148 public:
150  : QEglFSWindow(w)
151  , m_integration(integration)
152  {}
153 
155 
156  void resetSurface() override;
157  void invalidateSurface() override;
159 };
160 
162 {
163  auto *vsp2Screen = static_cast<QEglFSKmsVsp2Screen *>(screen());
164  EGLDisplay display = vsp2Screen->display();
165  QSurfaceFormat platformFormat = m_integration->surfaceFormatFor(window()->requestedFormat());
166  m_config = QEglFSDeviceIntegration::chooseConfig(display, platformFormat);
167  m_format = q_glFormatFromConfig(display, m_config, platformFormat);
168  // One fullscreen window per screen -> the native window is simply the gbm_surface the screen created.
169  m_window = reinterpret_cast<EGLNativeWindowType>(vsp2Screen->createSurface());
170 
171  PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC createPlatformWindowSurface = nullptr;
172  const char *extensions = eglQueryString(EGL_NO_DISPLAY, EGL_EXTENSIONS);
173  if (extensions && (strstr(extensions, "EGL_KHR_platform_gbm") || strstr(extensions, "EGL_MESA_platform_gbm"))) {
174  createPlatformWindowSurface = reinterpret_cast<PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC>(
175  eglGetProcAddress("eglCreatePlatformWindowSurfaceEXT"));
176  }
177 
178  if (createPlatformWindowSurface) {
179  m_surface = createPlatformWindowSurface(display, m_config, reinterpret_cast<void *>(m_window), nullptr);
180  } else {
181  qCDebug(qLcEglfsKmsDebug, "No eglCreatePlatformWindowSurface for GBM, falling back to eglCreateWindowSurface");
182  m_surface = eglCreateWindowSurface(display, m_config, m_window, nullptr);
183  }
184 }
185 
187 {
188  auto *vsp2Screen = static_cast<QEglFSKmsVsp2Screen *>(screen());
190  vsp2Screen->resetSurface();
191 }
192 
194 {
195  return new QEglFSKmsVsp2Window(window, this);
196 }
197 
static QDeviceDiscovery * create(QDeviceTypes type, QObject *parent=nullptr)
static EGLConfig chooseConfig(EGLDisplay display, const QSurfaceFormat &format)
QEglFSWindow * createWindow(QWindow *window) const override
QKmsDevice * createDevice() override
void presentBuffer(QPlatformSurface *surface) override
void destroyNativeWindow(EGLNativeWindowType window) override
EGLNativeWindowType createNativeOffscreenWindow(const QSurfaceFormat &format) override
EGLDisplay createDisplay(EGLNativeDisplayType nativeDisplay) override
const QEglFSKmsVsp2Integration * m_integration
QEglFSKmsVsp2Window(QWindow *w, const QEglFSKmsVsp2Integration *integration)
EGLSurface m_surface
EGLConfig m_config
void invalidateSurface() override
QSurfaceFormat m_format
EGLNativeWindowType m_window
QEglFSScreen * screen() const override
QSurface * surface() const
QWindow * window() const
The QString class provides a Unicode character string.
Definition: qstring.h:388
The QStringList class provides a list of strings.
The QSurfaceFormat class represents the format of a QSurface. \inmodule QtGui.
The QWindow class represents a window in the underlying windowing system.
Definition: qwindow.h:99
#define Q_UNLIKELY(x)
QSurfaceFormat q_glFormatFromConfig(EGLDisplay display, const EGLConfig config, const QSurfaceFormat &referenceFormat)
EGLConfig config
EGLConfig void * native_window
typedef EGLDisplay(EGLAPIENTRYP PFNEGLGETPLATFORMDISPLAYEXTPROC)(EGLenum platform
typedef EGLSurface(EGLAPIENTRYP PFNEGLCREATEPLATFORMWINDOWSURFACEEXTPROC)(EGLDisplay dpy
void * native_display
void const EGLint * attrib_list
#define EGL_PLATFORM_GBM_KHR
EGLDeviceEXT * devices
#define qFatal
Definition: qlogging.h:181
#define qCDebug(category,...)
GLfloat GLfloat GLfloat w
[0]
GLint GLsizei GLsizei GLenum format
GLsizei const GLchar *const * path
Definition: qopenglext.h:4283
#define Q_ASSERT(cond)
Definition: qrandom.cpp:84
Q_UNUSED(salary)
[21]
QScreen * screen
[1]
Definition: main.cpp:76
aWidget window() -> setWindowTitle("New Window Title")
[2]