618a499286a9b0bb984dad483280bd95048dc6cd
[profile/ivi/qtbase.git] / examples / opengl / hellowindow / main.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the examples of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
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
19 **     distribution.
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.
23 **
24 **
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."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40
41 #include <QGuiApplication>
42 #include <qpa/qplatformintegration.h>
43 #include <private/qguiapplication_p.h>
44 #include <QScreen>
45 #include <QThread>
46
47 #include "hellowindow.h"
48
49 int main(int argc, char **argv)
50 {
51     QGuiApplication app(argc, argv);
52
53     const bool multipleWindows =
54         QGuiApplicationPrivate::platformIntegration()->hasCapability(QPlatformIntegration::ThreadedOpenGL)
55         && !QGuiApplication::arguments().contains(QLatin1String("--single"));
56
57     QScreen *screen = QGuiApplication::primaryScreen();
58
59     QRect screenGeometry = screen->availableGeometry();
60
61     QSurfaceFormat format;
62     format.setDepthBufferSize(16);
63     format.setSamples(4);
64
65     QPoint center = QPoint(screenGeometry.center().x(), screenGeometry.top() + 80);
66     QSize windowSize(400, 320);
67     int delta = 40;
68
69     QList<QWindow *> windows;
70     QSharedPointer<Renderer> rendererA(new Renderer(format));
71
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);
77
78     QList<QThread *> renderThreads;
79     if (multipleWindows) {
80         QSharedPointer<Renderer> rendererB(new Renderer(format, rendererA.data()));
81
82         QThread *renderThread = new QThread;
83         rendererB->moveToThread(renderThread);
84         renderThreads << renderThread;
85
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);
91
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);
97
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));
101
102             renderThread = new QThread;
103             renderer->moveToThread(renderThread);
104             renderThreads.prepend(renderThread);
105
106             QRect screenGeometry = screen->availableGeometry();
107             QPoint center = screenGeometry.center();
108
109             QSize windowSize = screenGeometry.size() * 0.8;
110
111             HelloWindow *window = new HelloWindow(renderer);
112             window->setScreen(screen);
113             window->setGeometry(QRect(center, windowSize).translated(-windowSize.width() / 2, -windowSize.height() / 2));
114
115             QChar id = QChar('B' + i);
116             window->setWindowTitle(QLatin1String("Thread ") + id + QLatin1String(" - Context ") + id);
117             window->setVisible(true);
118             windows.prepend(window);
119         }
120     }
121
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();
125     }
126
127     const int exitValue = app.exec();
128
129     for (int i = 0; i < renderThreads.size(); ++i)
130         renderThreads.at(i)->wait();
131     qDeleteAll(windows);
132     qDeleteAll(renderThreads);
133
134     return exitValue;
135 }