-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
137 lines (107 loc) · 4.28 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/*
This file is part of qt-flutter-embedder.
SPDX-FileCopyrightText: 2024 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
Author: Sérgio Martins <sergio.martins@kdab.com>
SPDX-License-Identifier: GPL-3.0-only
Contact KDAB at <info@kdab.com> for commercial licensing options.
*/
#include "src/Embedder.h"
#include "src/FlutterWindow.h"
#include "flutter/encodable_value.h"
#include <QApplication>
#include <QThread>
#include <QDebug>
#include <QCommandLineParser>
#include <string>
using namespace KDAB;
/// Before we have QApplication we need to decide if we want GLES or not
/// as the shared context is created when QGuiApplication is created
static Embedder::Features defaultFeatures(int argc, char **argv)
{
Embedder::Features features = {};
bool gl = false;
bool gles = false;
for (int i = 1; i < argc; ++i) {
if (strcmp(argv[i], "-gl") == 0)
gl = true;
if (strcmp(argv[i], "-gles") == 0)
gles = true;
}
if (gl && gles) {
qFatal("Don't specify both -gl and -gles");
} else if (gles) {
features |= Embedder::Feature::GLES;
} else if (gl) {
features |= Embedder::Feature::GL;
}
return features;
}
#ifdef DEVELOPER_BUILD
// for testing
#include <flutter/event_channel.h>
#include <flutter/event_sink.h>
#include <flutter/event_stream_handler_functions.h>
#include <flutter/method_channel.h>
#include <flutter/standard_method_codec.h>
void registerDummyMethodChannel(Embedder &embedder)
{
flutter::MethodChannel<> channel(
embedder.binaryMessenger(), "qtembedder.kdab.com/testPlatformChannel",
&flutter::StandardMethodCodec::GetInstance());
channel.SetMethodCallHandler(
[](const flutter::MethodCall<> &call,
std::unique_ptr<flutter::MethodResult<>> result) {
qWarning() << "method call handler!";
result->Success(flutter::EncodableValue(42));
// result->Error("UNAVAILABLE", "Not available.");
});
}
#endif
int main(int argc, char **argv)
{
qputenv("QT_XCB_GL_INTEGRATION", "xcb_egl");
QApplication::setAttribute(Qt::AA_ShareOpenGLContexts);
QApplication::setAttribute(Qt::AA_DontCheckOpenGLContextThreadAffinity); // TODO: Needed ?
// default format for shared context
QSurfaceFormat::setDefaultFormat(Embedder::surfaceFormat(defaultFeatures(argc, argv)));
QApplication app(argc, argv);
QCommandLineParser parser;
parser.setApplicationDescription("Qt Embedder example");
QCommandLineOption enableMultiWindowOpt = { { "m", "multiwindow" }, "Enable multi-window mode" };
QCommandLineOption enableTextureGLContextOpt = { { "t", "textureGLContext" }, "Enable GL context for texture uploads (broken still)" };
QCommandLineOption useGLESopt = { { "e", "gles" }, "Explicitly request GLES instead of the default" };
QCommandLineOption useGLopt = { { "g", "gl" }, "Explicitly request GL instead of the default" };
parser.addOption(enableMultiWindowOpt);
parser.addOption(enableTextureGLContextOpt);
parser.addOption(useGLESopt);
parser.addOption(useGLopt);
parser.addPositionalArgument("project", "the root of the flutter project directory");
parser.addHelpOption();
parser.process(app);
if (parser.positionalArguments().isEmpty()) {
qInfo().noquote() << parser.helpText() << "\n";
return 1;
}
qDebug() << "Qt thread is" << QThread::currentThreadId();
const QString projectPath = parser.positionalArguments().constFirst();
Embedder::Features features = {};
if (parser.isSet(enableMultiWindowOpt))
features |= Embedder::Feature::MultiWindow;
if (parser.isSet(enableTextureGLContextOpt))
features |= Embedder::Feature::TextureGLContext;
if (parser.isSet(useGLESopt))
features |= Embedder::Feature::GLES;
if (parser.isSet(useGLopt))
features |= Embedder::Feature::GL;
Embedder embedder(features);
registerDummyMethodChannel(embedder);
const auto icuPath = std::string(FLUTTER_ICUDTL_DIR) + std::string("/icudtl.dat");
if (!embedder.runFlutter(argc, argv, projectPath.toStdString(), icuPath)) {
return -1;
}
if (embedder.isMultiWindowMode()) {
qDebug() << "Adding Window";
embedder.addWindow();
}
return QApplication::exec();
}