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 QtCanvas3D module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
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** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40//
41// W A R N I N G
42// -------------
43//
44// This file is not part of the QtCanvas3D API. It exists purely as an
45// implementation detail. This header file may change from version to
46// version without notice, or even be removed.
47//
48// We mean it.
49
50#ifndef CANVAS3D_P_H
51#define CANVAS3D_P_H
52
53#include "canvas3dcommon_p.h"
54#include "context3d_p.h"
55
56#include <QtQuick/QQuickItem>
57#include <QtQuick/QQuickWindow>
58#include <QtGui/QOpenGLFramebufferObject>
59#include <QtCore/QElapsedTimer>
60#include <QtCore/QPointer>
61
62QT_BEGIN_NAMESPACE
63
64class QOffscreenSurface;
65
66QT_CANVAS3D_BEGIN_NAMESPACE
67
68class CanvasGlCommandQueue;
69class CanvasRenderer;
70
71// Debug: Logs on high level information about the OpenGL driver and context.
72Q_DECLARE_LOGGING_CATEGORY(canvas3dinfo)
73
74// Debug: Logs all the calls made in to Canvas3D and Context3D.
75// Warning: Only logs warnings on failures in verifications.
76Q_DECLARE_LOGGING_CATEGORY(canvas3drendering)
77
78// Debug: Aggressive error checking. This means calling glGetError() after each OpenGL call.
79// Since checking for errors is a synchronous call, this will cause a negative performance hit.
80// Warning: All detected GL errors are logged as warnings on this category.
81Q_DECLARE_LOGGING_CATEGORY(canvas3dglerrors)
82
83class QT_CANVAS3D_EXPORT Canvas : public QQuickItem
84{
85 Q_OBJECT
86 Q_DISABLE_COPY(Canvas)
87
88 Q_ENUMS(RenderTarget)
89
90 Q_INTERFACES(QQmlParserStatus)
91 Q_PROPERTY(CanvasContext *context READ context NOTIFY contextChanged)
92 Q_PROPERTY(float devicePixelRatio READ devicePixelRatio NOTIFY devicePixelRatioChanged)
93 Q_PROPERTY(uint fps READ fps NOTIFY fpsChanged)
94 Q_PROPERTY(QSize pixelSize READ pixelSize WRITE setPixelSize NOTIFY pixelSizeChanged)
95 Q_PROPERTY(RenderTarget renderTarget READ renderTarget WRITE setRenderTarget NOTIFY renderTargetChanged REVISION 1)
96 Q_PROPERTY(bool renderOnDemand READ renderOnDemand WRITE setRenderOnDemand NOTIFY renderOnDemandChanged REVISION 1)
97
98public:
99 enum RenderTarget {
100 RenderTargetOffscreenBuffer,
101 RenderTargetBackground,
102 RenderTargetForeground
103 };
104
105 // internal
106 enum ContextState {
107 ContextNone,
108 ContextLost,
109 ContextRestoring,
110 ContextAlive
111 };
112
113 Canvas(QQuickItem *parent = 0);
114 ~Canvas();
115
116 void handleWindowChanged(QQuickWindow *win);
117 float devicePixelRatio();
118 QSize pixelSize();
119 void setPixelSize(QSize pixelSize);
120 void setRenderTarget(RenderTarget target);
121 RenderTarget renderTarget() const;
122 void setRenderOnDemand(bool enable);
123 bool renderOnDemand() const;
124
125 uint fps();
126 Q_INVOKABLE int frameTimeMs();
127 Q_REVISION(1) Q_INVOKABLE int frameSetupTimeMs();
128
129 Q_INVOKABLE QJSValue getContext(const QString &name);
130 Q_INVOKABLE QJSValue getContext(const QString &name, const QVariantMap &options);
131 CanvasContext *context();
132 CanvasRenderer *renderer();
133
134public slots:
135 void requestRender();
136
137private slots:
138 void queueNextRender();
139 void queueResizeGL();
140 void emitNeedRender();
141 void handleBeforeSynchronizing();
142 void handleRendererFpsChange(uint fps);
143 void handleContextLost();
144
145signals:
146 void needRender();
147 void devicePixelRatioChanged(float ratio);
148 void contextChanged(CanvasContext *context);
149 void fpsChanged(uint fps);
150 void pixelSizeChanged(QSize pixelSize);
151 void renderTargetChanged();
152 void renderOnDemandChanged();
153 void contextLost();
154 void contextRestored();
155
156 void initializeGL();
157 void paintGL();
158 void resizeGL(int width, int height, float devicePixelRatio);
159
160 void textureReady(int id, const QSize &size, float devicePixelRatio);
161
162protected:
163 virtual void geometryChanged(const QRectF & newGeometry, const QRectF & oldGeometry);
164 virtual void itemChange(ItemChange change, const ItemChangeData &value);
165 virtual QSGNode *updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *);
166
167private:
168 void setupAntialiasing();
169 void updateWindowParameters();
170 void sync();
171 bool firstSync();
172
173 bool m_isNeedRenderQueued;
174 bool m_rendererReady;
175 QPointer<CanvasContext> m_context3D;
176 QSize m_fboSize;
177 QSize m_maxSize;
178
179 uint m_frameTimeMs;
180 uint m_frameSetupTimeMs;
181 QElapsedTimer m_frameTimer;
182 int m_maxSamples;
183 float m_devicePixelRatio;
184
185 bool m_isOpenGLES2;
186 bool m_isCombinedDepthStencilSupported;
187 bool m_isSoftwareRendered;
188 bool m_runningInDesigner;
189 CanvasContextAttributes m_contextAttribs;
190 bool m_isContextAttribsSet;
191 bool m_alphaChanged;
192 bool m_resizeGLQueued;
193 bool m_allowRenderTargetChange;
194 bool m_renderTargetSyncConnected;
195 RenderTarget m_renderTarget;
196 bool m_renderOnDemand;
197
198 CanvasRenderer *m_renderer;
199
200 GLint m_maxVertexAttribs;
201 int m_contextVersion;
202 QSet<QByteArray> m_extensions;
203
204 uint m_fps;
205
206 ContextState m_contextState;
207 QPointer<QQuickWindow> m_contextWindow; // Not owned
208};
209
210QT_CANVAS3D_END_NAMESPACE
211QT_END_NAMESPACE
212
213#endif // CANVAS3D_P_H
214
215

source code of qtcanvas3d/src/imports/qtcanvas3d/canvas3d_p.h