1/****************************************************************************
2**
3** Copyright (C) 2017 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#ifndef WINDOW_MULTITHREADED_H
52#define WINDOW_MULTITHREADED_H
53
54#include <QWindow>
55#include <QMatrix4x4>
56#include <QThread>
57#include <QWaitCondition>
58#include <QMutex>
59
60QT_FORWARD_DECLARE_CLASS(QOpenGLContext)
61QT_FORWARD_DECLARE_CLASS(QOpenGLFramebufferObject)
62QT_FORWARD_DECLARE_CLASS(QOffscreenSurface)
63QT_FORWARD_DECLARE_CLASS(QQuickRenderControl)
64QT_FORWARD_DECLARE_CLASS(QQuickWindow)
65QT_FORWARD_DECLARE_CLASS(QQmlEngine)
66QT_FORWARD_DECLARE_CLASS(QQmlComponent)
67QT_FORWARD_DECLARE_CLASS(QQuickItem)
68
69class CubeRenderer;
70
71class QuickRenderer : public QObject
72{
73 Q_OBJECT
74
75public:
76 QuickRenderer();
77
78 void requestInit();
79 void requestRender();
80 void requestResize();
81 void requestStop();
82
83 QWaitCondition *cond() { return &m_cond; }
84 QMutex *mutex() { return &m_mutex; }
85
86 void setContext(QOpenGLContext *ctx) { m_context = ctx; }
87 void setSurface(QOffscreenSurface *s) { m_surface = s; }
88 void setWindow(QWindow *w) { m_window = w; }
89 void setQuickWindow(QQuickWindow *w) { m_quickWindow = w; }
90 void setRenderControl(QQuickRenderControl *r) { m_renderControl = r; }
91
92 void aboutToQuit();
93
94private:
95 bool event(QEvent *e) override;
96 void init();
97 void cleanup();
98 void ensureFbo();
99 void render(QMutexLocker *lock);
100
101 QWaitCondition m_cond;
102 QMutex m_mutex;
103 QOpenGLContext *m_context;
104 QOffscreenSurface *m_surface;
105 QOpenGLFramebufferObject *m_fbo;
106 QWindow *m_window;
107 QQuickWindow *m_quickWindow;
108 QQuickRenderControl *m_renderControl;
109 CubeRenderer *m_cubeRenderer;
110 QMutex m_quitMutex;
111 bool m_quit;
112};
113
114class WindowMultiThreaded : public QWindow
115{
116 Q_OBJECT
117
118public:
119 WindowMultiThreaded();
120 ~WindowMultiThreaded();
121
122protected:
123 void exposeEvent(QExposeEvent *e) override;
124 void resizeEvent(QResizeEvent *e) override;
125 void mousePressEvent(QMouseEvent *e) override;
126 void mouseReleaseEvent(QMouseEvent *e) override;
127 bool event(QEvent *e) override;
128
129private slots:
130 void run();
131 void requestUpdate();
132 void polishSyncAndRender();
133
134private:
135 void startQuick(const QString &filename);
136 void updateSizes();
137
138 QuickRenderer *m_quickRenderer;
139 QThread *m_quickRendererThread;
140
141 QOpenGLContext *m_context;
142 QOffscreenSurface *m_offscreenSurface;
143 QQuickRenderControl *m_renderControl;
144 QQuickWindow *m_quickWindow;
145 QQmlEngine *m_qmlEngine;
146 QQmlComponent *m_qmlComponent;
147 QQuickItem *m_rootItem;
148 bool m_quickInitialized;
149 bool m_psrRequested;
150};
151
152#endif
153

source code of qtdeclarative/examples/quick/rendercontrol/window_multithreaded.h