1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
49 #include <qopenglfunctions.h>
50 #include <qopenglshaderprogram.h>
53 #include "qsgmaterial.h"
54 #include <QtQuick/qsgtexture.h>
56 #include <QtQuick/private/qsgcontext_p.h>
62 class QSGMaterialShader;
63 struct QSGMaterialType;
64 class QOpenGLFramebufferObject;
65 class TextureReference;
69 class Q_QUICK_PRIVATE_EXPORT QSGRenderer : public QObject, public QOpenGLFunctions
79 Q_DECLARE_FLAGS(ClipType, ClipTypeBit)
83 ClearColorBuffer = 0x0001,
84 ClearDepthBuffer = 0x0002,
85 ClearStencilBuffer = 0x0004
87 Q_DECLARE_FLAGS(ClearMode, ClearModeBit)
89 QSGRenderer(QSGContext *context);
90 virtual ~QSGRenderer();
92 void setRootNode(QSGRootNode *node);
93 QSGRootNode *rootNode() const { return m_root_node; }
95 void setDeviceRect(const QRect &rect) { m_device_rect = rect; }
96 inline void setDeviceRect(const QSize &size) { setDeviceRect(QRect(QPoint(), size)); }
97 QRect deviceRect() const { return m_device_rect; }
99 void setViewportRect(const QRect &rect) { m_viewport_rect = rect; }
100 inline void setViewportRect(const QSize &size) { setViewportRect(QRect(QPoint(), size)); }
101 QRect viewportRect() const { return m_viewport_rect; }
103 // Accessed by QSGMaterialShader::RenderState.
104 QMatrix4x4 currentProjectionMatrix() const { return m_current_projection_matrix; }
105 QMatrix4x4 currentModelViewMatrix() const { return m_current_model_view_matrix; }
106 QMatrix4x4 currentCombinedMatrix() const { return m_current_projection_matrix * m_current_model_view_matrix; }
107 qreal currentOpacity() const { return m_current_opacity; }
108 qreal determinant() const { return m_current_determinant; }
110 void setProjectionMatrixToDeviceRect();
111 virtual void setProjectionMatrixToRect(const QRectF &rect);
112 void setProjectionMatrix(const QMatrix4x4 &matrix);
113 QMatrix4x4 projectionMatrix() const { return m_projection_matrix; }
114 bool isMirrored() const { return m_mirrored; }
116 void setClearColor(const QColor &color);
117 QColor clearColor() const { return m_clear_color; }
119 QOpenGLContext *glContext() const { Q_ASSERT(m_context); return m_context->glContext(); }
121 QSGContext *context();
124 void renderScene(const QSGBindable &bindable);
125 virtual void nodeChanged(QSGNode *node, QSGNode::DirtyState state);
126 virtual void materialChanged(QSGGeometryNode *node, QSGMaterial *from, QSGMaterial *to);
128 QSGNodeUpdater *nodeUpdater() const;
129 void setNodeUpdater(QSGNodeUpdater *updater);
131 inline QSGMaterialShader::RenderState state(QSGMaterialShader::RenderState::DirtyStates dirty) const;
133 void setClearMode(ClearMode mode) { m_clear_mode = mode; }
134 ClearMode clearMode() const { return m_clear_mode; }
137 void sceneGraphChanged(); // Add, remove, ChangeFlags changes...
140 void draw(const QSGMaterialShader *material, const QSGGeometry *g);
142 virtual void render() = 0;
143 QSGRenderer::ClipType updateStencilClip(const QSGClipNode *clip);
145 const QSGBindable *bindable() const { return m_bindable; }
147 virtual void preprocess();
149 void addNodesToPreprocess(QSGNode *node);
150 void removeNodesToPreprocess(QSGNode *node);
153 QColor m_clear_color;
154 ClearMode m_clear_mode;
155 QMatrix4x4 m_current_projection_matrix;
156 QMatrix4x4 m_current_model_view_matrix;
157 qreal m_current_opacity;
158 qreal m_current_determinant;
159 QRect m_current_scissor_rect;
160 int m_current_stencil_value;
162 QSGContext *m_context;
165 QSGRootNode *m_root_node;
166 QSGNodeUpdater *m_node_updater;
169 QRect m_viewport_rect;
171 QSet<QSGNode *> m_nodes_to_preprocess;
173 QMatrix4x4 m_projection_matrix;
174 QOpenGLShaderProgram m_clip_program;
175 int m_clip_matrix_id;
177 const QSGBindable *m_bindable;
179 uint m_changed_emitted : 1;
181 uint m_is_rendering : 1;
183 uint m_vertex_buffer_bound : 1;
184 uint m_index_buffer_bound : 1;
187 Q_DECLARE_OPERATORS_FOR_FLAGS(QSGRenderer::ClearMode)
189 class Q_QUICK_PRIVATE_EXPORT QSGBindable
192 virtual ~QSGBindable() { }
193 virtual void bind() const = 0;
194 virtual void clear(QSGRenderer::ClearMode mode) const;
195 virtual void reactivate() const;
198 class QSGBindableFbo : public QSGBindable
201 QSGBindableFbo(QOpenGLFramebufferObject *fbo);
202 virtual void bind() const;
204 QOpenGLFramebufferObject *m_fbo;
207 class QSGBindableFboId : public QSGBindable
210 QSGBindableFboId(GLuint);
211 virtual void bind() const;
218 QSGMaterialShader::RenderState QSGRenderer::state(QSGMaterialShader::RenderState::DirtyStates dirty) const
220 QSGMaterialShader::RenderState s;
227 class Q_QUICK_PRIVATE_EXPORT QSGNodeDumper : public QSGNodeVisitor {
230 static void dump(QSGNode *n);
232 QSGNodeDumper() : m_indent(0) {}
233 void visitNode(QSGNode *n);
234 void visitChildren(QSGNode *n);