| 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 examples of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:BSD$ |
| 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 | ** BSD License Usage |
| 18 | ** Alternatively, you may use this file under the terms of the BSD license |
| 19 | ** as follows: |
| 20 | ** |
| 21 | ** "Redistribution and use in source and binary forms, with or without |
| 22 | ** modification, are permitted provided that the following conditions are |
| 23 | ** met: |
| 24 | ** * Redistributions of source code must retain the above copyright |
| 25 | ** notice, this list of conditions and the following disclaimer. |
| 26 | ** * Redistributions in binary form must reproduce the above copyright |
| 27 | ** notice, this list of conditions and the following disclaimer in |
| 28 | ** the documentation and/or other materials provided with the |
| 29 | ** distribution. |
| 30 | ** * Neither the name of The Qt Company Ltd nor the names of its |
| 31 | ** contributors may be used to endorse or promote products derived |
| 32 | ** from this software without specific prior written permission. |
| 33 | ** |
| 34 | ** |
| 35 | ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 36 | ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 37 | ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 38 | ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 39 | ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 41 | ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 42 | ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 43 | ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 44 | ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 45 | ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE." |
| 46 | ** |
| 47 | ** $QT_END_LICENSE$ |
| 48 | ** |
| 49 | ****************************************************************************/ |
| 50 | |
| 51 | #include "hellowindow.h" |
| 52 | |
| 53 | #include <qpa/qplatformintegration.h> |
| 54 | |
| 55 | #include <QCommandLineParser> |
| 56 | #include <QCommandLineOption> |
| 57 | #include <QGuiApplication> |
| 58 | #include <QScreen> |
| 59 | #include <QThread> |
| 60 | |
| 61 | int main(int argc, char *argv[]) |
| 62 | { |
| 63 | QGuiApplication app(argc, argv); |
| 64 | |
| 65 | QCoreApplication::setApplicationName("Qt HelloWindow GL Example" ); |
| 66 | QCoreApplication::setOrganizationName("QtProject" ); |
| 67 | QCoreApplication::setApplicationVersion(QT_VERSION_STR); |
| 68 | QCommandLineParser parser; |
| 69 | parser.setApplicationDescription(QCoreApplication::applicationName()); |
| 70 | parser.addHelpOption(); |
| 71 | parser.addVersionOption(); |
| 72 | QCommandLineOption multipleOption("multiple" , "Create multiple windows" ); |
| 73 | parser.addOption(multipleOption); |
| 74 | QCommandLineOption multipleSampleOption("multisample" , "Multisampling" ); |
| 75 | parser.addOption(multipleSampleOption); |
| 76 | QCommandLineOption multipleScreenOption("multiscreen" , "Run on multiple screens" ); |
| 77 | parser.addOption(multipleScreenOption); |
| 78 | QCommandLineOption timeoutOption("timeout" , "Close after 10s" ); |
| 79 | parser.addOption(timeoutOption); |
| 80 | parser.process(app); |
| 81 | |
| 82 | // Some platforms can only have one window per screen. Therefore we need to differentiate. |
| 83 | const bool multipleWindows = parser.isSet(multipleOption); |
| 84 | const bool multipleScreens = parser.isSet(multipleScreenOption); |
| 85 | |
| 86 | QScreen *screen = QGuiApplication::primaryScreen(); |
| 87 | |
| 88 | QRect screenGeometry = screen->availableGeometry(); |
| 89 | |
| 90 | QSurfaceFormat format; |
| 91 | format.setDepthBufferSize(16); |
| 92 | if (parser.isSet(multipleSampleOption)) |
| 93 | format.setSamples(4); |
| 94 | |
| 95 | QPoint center = QPoint(screenGeometry.center().x(), screenGeometry.top() + 80); |
| 96 | QSize windowSize(400, 320); |
| 97 | int delta = 40; |
| 98 | |
| 99 | QList<QWindow *> windows; |
| 100 | QSharedPointer<Renderer> rendererA(new Renderer(format)); |
| 101 | |
| 102 | HelloWindow *windowA = new HelloWindow(rendererA); |
| 103 | windowA->setGeometry(QRect(center, windowSize).translated(-windowSize.width() - delta / 2, 0)); |
| 104 | windowA->setTitle(QStringLiteral("Thread A - Context A" )); |
| 105 | windowA->setVisible(true); |
| 106 | windows.prepend(windowA); |
| 107 | |
| 108 | QList<QThread *> renderThreads; |
| 109 | if (multipleWindows) { |
| 110 | QSharedPointer<Renderer> rendererB(new Renderer(format, rendererA.data())); |
| 111 | |
| 112 | QThread *renderThread = new QThread; |
| 113 | rendererB->moveToThread(renderThread); |
| 114 | renderThreads << renderThread; |
| 115 | |
| 116 | HelloWindow *windowB = new HelloWindow(rendererA); |
| 117 | windowB->setGeometry(QRect(center, windowSize).translated(delta / 2, 0)); |
| 118 | windowB->setTitle(QStringLiteral("Thread A - Context A" )); |
| 119 | windowB->setVisible(true); |
| 120 | windows.prepend(windowB); |
| 121 | |
| 122 | HelloWindow *windowC = new HelloWindow(rendererB); |
| 123 | windowC->setGeometry(QRect(center, windowSize).translated(-windowSize.width() / 2, windowSize.height() + delta)); |
| 124 | windowC->setTitle(QStringLiteral("Thread B - Context B" )); |
| 125 | windowC->setVisible(true); |
| 126 | windows.prepend(windowC); |
| 127 | } |
| 128 | if (multipleScreens) { |
| 129 | for (int i = 1; i < QGuiApplication::screens().size(); ++i) { |
| 130 | QScreen *screen = QGuiApplication::screens().at(i); |
| 131 | QSharedPointer<Renderer> renderer(new Renderer(format, rendererA.data(), screen)); |
| 132 | |
| 133 | QThread *renderThread = new QThread; |
| 134 | renderer->moveToThread(renderThread); |
| 135 | renderThreads.prepend(renderThread); |
| 136 | |
| 137 | QRect screenGeometry = screen->availableGeometry(); |
| 138 | QPoint center = screenGeometry.center(); |
| 139 | |
| 140 | QSize windowSize = screenGeometry.size() * 0.8; |
| 141 | |
| 142 | HelloWindow *window = new HelloWindow(renderer, screen); |
| 143 | window->setGeometry(QRect(center, windowSize).translated(-windowSize.width() / 2, -windowSize.height() / 2)); |
| 144 | |
| 145 | QChar id = QChar('B' + i); |
| 146 | window->setTitle(QStringLiteral("Thread " ) + id + QStringLiteral(" - Context " ) + id); |
| 147 | window->setVisible(true); |
| 148 | windows.prepend(window); |
| 149 | } |
| 150 | } |
| 151 | |
| 152 | for (int i = 0; i < renderThreads.size(); ++i) { |
| 153 | QObject::connect(qGuiApp, &QGuiApplication::lastWindowClosed, renderThreads.at(i), &QThread::quit); |
| 154 | renderThreads.at(i)->start(); |
| 155 | } |
| 156 | |
| 157 | // Quit after 10 seconds. For platforms that do not have windows that are closeable. |
| 158 | if (parser.isSet(timeoutOption)) |
| 159 | QTimer::singleShot(10000, qGuiApp, &QCoreApplication::quit); |
| 160 | |
| 161 | const int exitValue = app.exec(); |
| 162 | |
| 163 | for (int i = 0; i < renderThreads.size(); ++i) { |
| 164 | renderThreads.at(i)->quit(); // some platforms may not have windows to close so ensure quit() |
| 165 | renderThreads.at(i)->wait(); |
| 166 | } |
| 167 | |
| 168 | qDeleteAll(windows); |
| 169 | qDeleteAll(renderThreads); |
| 170 | |
| 171 | return exitValue; |
| 172 | } |
| 173 | |