1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** 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_EXPORT QSGRenderer : public QObject, public QOpenGLFunctions
82 ClearColorBuffer = 0x0001,
83 ClearDepthBuffer = 0x0002,
84 ClearStencilBuffer = 0x0004
86 Q_DECLARE_FLAGS(ClearMode, ClearModeBit)
88 QSGRenderer(QSGContext *context);
89 virtual ~QSGRenderer();
91 void setRootNode(QSGRootNode *node);
92 QSGRootNode *rootNode() const { return m_root_node; }
94 void setDeviceRect(const QRect &rect) { m_device_rect = rect; }
95 inline void setDeviceRect(const QSize &size) { setDeviceRect(QRect(QPoint(), size)); }
96 QRect deviceRect() const { return m_device_rect; }
98 void setViewportRect(const QRect &rect) { m_viewport_rect = rect; }
99 inline void setViewportRect(const QSize &size) { setViewportRect(QRect(QPoint(), size)); }
100 QRect viewportRect() const { return m_viewport_rect; }
102 // Accessed by QSGMaterialShader::RenderState.
103 QMatrix4x4 currentProjectionMatrix() const { return m_current_projection_matrix; }
104 QMatrix4x4 currentModelViewMatrix() const { return m_current_model_view_matrix; }
105 QMatrix4x4 currentCombinedMatrix() const { return m_current_projection_matrix * m_current_model_view_matrix; }
106 qreal currentOpacity() const { return m_current_opacity; }
108 void setProjectionMatrixToDeviceRect();
109 void setProjectionMatrixToRect(const QRectF &rect);
110 void setProjectionMatrix(const QMatrix4x4 &matrix);
111 QMatrix4x4 projectionMatrix() const { return m_projection_matrix; }
112 bool isMirrored() const { return m_mirrored; }
114 void setClearColor(const QColor &color);
115 QColor clearColor() const { return m_clear_color; }
117 QOpenGLContext *glContext() const { Q_ASSERT(m_context); return m_context->glContext(); }
119 QSGContext *context();
122 void renderScene(const QSGBindable &bindable);
123 virtual void nodeChanged(QSGNode *node, QSGNode::DirtyState state);
124 virtual void materialChanged(QSGGeometryNode *node, QSGMaterial *from, QSGMaterial *to);
126 QSGNodeUpdater *nodeUpdater() const;
127 void setNodeUpdater(QSGNodeUpdater *updater);
129 inline QSGMaterialShader::RenderState state(QSGMaterialShader::RenderState::DirtyStates dirty) const;
131 void setClearMode(ClearMode mode) { m_clear_mode = mode; }
132 ClearMode clearMode() const { return m_clear_mode; }
135 void sceneGraphChanged(); // Add, remove, ChangeFlags changes...
138 void draw(const QSGMaterialShader *material, const QSGGeometry *g);
140 virtual void render() = 0;
141 QSGRenderer::ClipType updateStencilClip(const QSGClipNode *clip);
143 const QSGBindable *bindable() const { return m_bindable; }
145 virtual void preprocess();
147 void addNodesToPreprocess(QSGNode *node);
148 void removeNodesToPreprocess(QSGNode *node);
151 QColor m_clear_color;
152 ClearMode m_clear_mode;
153 QMatrix4x4 m_current_projection_matrix;
154 QMatrix4x4 m_current_model_view_matrix;
155 qreal m_current_opacity;
157 QSGContext *m_context;
160 QSGRootNode *m_root_node;
161 QSGNodeUpdater *m_node_updater;
164 QRect m_viewport_rect;
166 QSet<QSGNode *> m_nodes_to_preprocess;
168 QMatrix4x4 m_projection_matrix;
169 QOpenGLShaderProgram m_clip_program;
170 int m_clip_matrix_id;
172 const QSGBindable *m_bindable;
174 uint m_changed_emitted : 1;
176 uint m_is_rendering : 1;
178 uint m_vertex_buffer_bound : 1;
179 uint m_index_buffer_bound : 1;
182 Q_DECLARE_OPERATORS_FOR_FLAGS(QSGRenderer::ClearMode)
184 class Q_QUICK_EXPORT QSGBindable
187 virtual ~QSGBindable() { }
188 virtual void bind() const = 0;
189 virtual void clear(QSGRenderer::ClearMode mode) const;
190 virtual void reactivate() const;
193 class QSGBindableFbo : public QSGBindable
196 QSGBindableFbo(QOpenGLFramebufferObject *fbo);
197 virtual void bind() const;
199 QOpenGLFramebufferObject *m_fbo;
204 QSGMaterialShader::RenderState QSGRenderer::state(QSGMaterialShader::RenderState::DirtyStates dirty) const
206 QSGMaterialShader::RenderState s;
213 class Q_QUICK_EXPORT QSGNodeDumper : public QSGNodeVisitor {
216 static void dump(QSGNode *n);
218 QSGNodeDumper() : m_indent(0) {}
219 void visitNode(QSGNode *n);
220 void visitChildren(QSGNode *n);