1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the examples of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
20 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
39 ****************************************************************************/
41 #include <QGuiApplication>
42 #include <qpa/qplatformintegration.h>
43 #include <private/qguiapplication_p.h>
47 #include "hellowindow.h"
49 int main(int argc, char **argv)
51 QGuiApplication app(argc, argv);
53 const bool multipleWindows =
54 QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedOpenGL)
55 && !QGuiApplication::arguments().contains(QLatin1String("--single"));
57 QScreen *screen = QGuiApplication::primaryScreen();
59 QRect screenGeometry = screen->availableGeometry();
61 QSurfaceFormat format;
62 format.setDepthBufferSize(16);
65 QPoint center = QPoint(screenGeometry.center().x(), screenGeometry.top() + 80);
66 QSize windowSize(400, 320);
69 QList<QWindow *> windows;
70 QSharedPointer<Renderer> rendererA(new Renderer(format));
72 HelloWindow *windowA = new HelloWindow(rendererA);
73 windowA->setGeometry(QRect(center, windowSize).translated(-windowSize.width() - delta / 2, 0));
74 windowA->setWindowTitle(QLatin1String("Thread A - Context A"));
75 windowA->setVisible(true);
76 windows.prepend(windowA);
78 QList<QThread *> renderThreads;
79 if (multipleWindows) {
80 QSharedPointer<Renderer> rendererB(new Renderer(format, rendererA.data()));
82 QThread *renderThread = new QThread;
83 rendererB->moveToThread(renderThread);
84 renderThreads << renderThread;
86 HelloWindow *windowB = new HelloWindow(rendererA);
87 windowB->setGeometry(QRect(center, windowSize).translated(delta / 2, 0));
88 windowB->setWindowTitle(QLatin1String("Thread A - Context A"));
89 windowB->setVisible(true);
90 windows.prepend(windowB);
92 HelloWindow *windowC = new HelloWindow(rendererB);
93 windowC->setGeometry(QRect(center, windowSize).translated(-windowSize.width() / 2, windowSize.height() + delta));
94 windowC->setWindowTitle(QLatin1String("Thread B - Context B"));
95 windowC->setVisible(true);
96 windows.prepend(windowC);
98 for (int i = 1; i < QGuiApplication::screens().size(); ++i) {
99 QScreen *screen = QGuiApplication::screens().at(i);
100 QSharedPointer<Renderer> renderer(new Renderer(format, rendererA.data(), screen));
102 renderThread = new QThread;
103 renderer->moveToThread(renderThread);
104 renderThreads.prepend(renderThread);
106 QRect screenGeometry = screen->availableGeometry();
107 QPoint center = screenGeometry.center();
109 QSize windowSize = screenGeometry.size() * 0.8;
111 HelloWindow *window = new HelloWindow(renderer);
112 window->setScreen(screen);
113 window->setGeometry(QRect(center, windowSize).translated(-windowSize.width() / 2, -windowSize.height() / 2));
115 QChar id = QChar('B' + i);
116 window->setWindowTitle(QLatin1String("Thread ") + id + QLatin1String(" - Context ") + id);
117 window->setVisible(true);
118 windows.prepend(window);
122 for (int i = 0; i < renderThreads.size(); ++i) {
123 QObject::connect(qGuiApp, SIGNAL(lastWindowClosed()), renderThreads.at(i), SLOT(quit()));
124 renderThreads.at(i)->start();
127 const int exitValue = app.exec();
129 for (int i = 0; i < renderThreads.size(); ++i)
130 renderThreads.at(i)->wait();
132 qDeleteAll(renderThreads);