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 "qeglfsemulatorintegration.h"
41#include "qeglfsemulatorscreen.h"
42#include "private/qeglfsintegration_p.h"
43
44#include <private/qguiapplication_p.h>
45#include <QtGui/private/qeglconvenience_p.h>
46#include <QtGui/private/qeglplatformcontext_p.h>
47
48#include <qpa/qwindowsysteminterface.h>
49
50#include <QtCore/QJsonDocument>
51#include <QtCore/QJsonArray>
52#include <QtCore/QJsonParseError>
53
54QT_BEGIN_NAMESPACE
55
56QEglFSEmulatorIntegration::QEglFSEmulatorIntegration()
57{
58 // The Qt Emulator provides the ability to render to multiple displays
59 // In addition to the usual EGL and OpenGLESv2 API's, there are also a
60 // few additional API's that enable the client (this plugin) to query
61 // the available screens and their properties, as well as the ability
62 // to select which screen is the active render target (as this is
63 // usually handled in a platform specific way and not by EGL itself).
64
65 getDisplays = reinterpret_cast<PFNQGSGETDISPLAYSPROC>(eglGetProcAddress("qgsGetDisplays"));
66 setDisplay = reinterpret_cast<PFNQGSSETDISPLAYPROC>(eglGetProcAddress("qgsSetDisplay"));
67}
68
69void QEglFSEmulatorIntegration::platformInit()
70{
71}
72
73void QEglFSEmulatorIntegration::platformDestroy()
74{
75}
76
77bool QEglFSEmulatorIntegration::usesDefaultScreen()
78{
79 // This makes it possible to remotely query and then register our own set of screens
80 return false;
81}
82
83void QEglFSEmulatorIntegration::screenInit()
84{
85 // Use qgsGetDisplays() call to retrieve the available screens from the Emulator
86 if (getDisplays) {
87 QByteArray displaysInfo = getDisplays();
88 QJsonParseError error;
89 QJsonDocument displaysDocument = QJsonDocument::fromJson(displaysInfo, &error);
90 if (error.error == QJsonParseError::NoError) {
91 // Document should be an array of screen objects
92 if (displaysDocument.isArray()){
93 QJsonArray screenArray = displaysDocument.array();
94 for (auto screenValue : screenArray) {
95 if (screenValue.isObject())
96 QWindowSystemInterface::handleScreenAdded(new QEglFSEmulatorScreen(screenValue.toObject()));
97 }
98 }
99 } else {
100 qWarning() << "eglfs_emu: Failed to parse display info JSON with error: " << error.errorString()
101 << " at offset " << error.offset << " : " << displaysInfo;
102
103 }
104 } else {
105 qFatal("EGL library doesn't support Emulator extensions");
106 }
107}
108
109bool QEglFSEmulatorIntegration::hasCapability(QPlatformIntegration::Capability cap) const
110{
111 switch (cap) {
112 case QPlatformIntegration::ThreadedPixmaps:
113 case QPlatformIntegration::OpenGL:
114 case QPlatformIntegration::ThreadedOpenGL:
115 return true;
116 default:
117 return false;
118 }
119}
120
121EGLNativeWindowType QEglFSEmulatorIntegration::createNativeWindow(QPlatformWindow *platformWindow,
122 const QSize &size,
123 const QSurfaceFormat &format)
124{
125 Q_UNUSED(size);
126 Q_UNUSED(format);
127 QEglFSEmulatorScreen *screen = static_cast<QEglFSEmulatorScreen *>(platformWindow->screen());
128 if (screen && setDisplay) {
129 // Let the emulator know which screen the window surface is attached to
130 setDisplay(screen->id());
131 }
132 static QBasicAtomicInt uniqueWindowId = Q_BASIC_ATOMIC_INITIALIZER(0);
133 return EGLNativeWindowType(qintptr(1 + uniqueWindowId.fetchAndAddRelaxed(1)));
134}
135
136QT_END_NAMESPACE
137