4fde0e26c8a1b274104b6763de5439f3aa375af4
[profile/ivi/qtdeclarative.git] / src / quick / scenegraph / coreapi / qsgrenderer_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef RENDERER_H
43 #define RENDERER_H
44
45 #include <qset.h>
46 #include <qhash.h>
47
48 #include <qcolor.h>
49 #include <qopenglfunctions.h>
50 #include <qopenglshaderprogram.h>
51
52 #include "qsgnode.h"
53 #include "qsgmaterial.h"
54 #include <QtQuick/qsgtexture.h>
55
56 #include <QtQuick/private/qsgcontext_p.h>
57
58 QT_BEGIN_HEADER
59
60 QT_BEGIN_NAMESPACE
61
62 class QSGMaterialShader;
63 struct QSGMaterialType;
64 class QOpenGLFramebufferObject;
65 class TextureReference;
66 class QSGBindable;
67 class QSGNodeUpdater;
68
69 class Q_QUICK_EXPORT QSGRenderer : public QObject, public QOpenGLFunctions
70 {
71     Q_OBJECT
72 public:
73     enum ClipType
74     {
75         NoClip,
76         ScissorClip,
77         StencilClip
78     };
79
80     enum ClearModeBit
81     {
82         ClearColorBuffer    = 0x0001,
83         ClearDepthBuffer    = 0x0002,
84         ClearStencilBuffer  = 0x0004
85     };
86     Q_DECLARE_FLAGS(ClearMode, ClearModeBit)
87
88     QSGRenderer(QSGContext *context);
89     virtual ~QSGRenderer();
90
91     void setRootNode(QSGRootNode *node);
92     QSGRootNode *rootNode() const { return m_root_node; }
93
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; }
97
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; }
101
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; }
107
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; }
113
114     void setClearColor(const QColor &color);
115     QColor clearColor() const { return m_clear_color; }
116
117     QOpenGLContext *glContext() const { Q_ASSERT(m_context); return m_context->glContext(); }
118
119     QSGContext *context();
120
121     void renderScene();
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);
125
126     QSGNodeUpdater *nodeUpdater() const;
127     void setNodeUpdater(QSGNodeUpdater *updater);
128
129     inline QSGMaterialShader::RenderState state(QSGMaterialShader::RenderState::DirtyStates dirty) const;
130
131     void setClearMode(ClearMode mode) { m_clear_mode = mode; }
132     ClearMode clearMode() const { return m_clear_mode; }
133
134 signals:
135     void sceneGraphChanged(); // Add, remove, ChangeFlags changes...
136
137 protected:
138     void draw(const QSGMaterialShader *material, const QSGGeometry *g);
139
140     virtual void render() = 0;
141     QSGRenderer::ClipType updateStencilClip(const QSGClipNode *clip);
142
143     const QSGBindable *bindable() const { return m_bindable; }
144
145     virtual void preprocess();
146
147     void addNodesToPreprocess(QSGNode *node);
148     void removeNodesToPreprocess(QSGNode *node);
149
150
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;
156
157     QSGContext *m_context;
158
159 private:
160     QSGRootNode *m_root_node;
161     QSGNodeUpdater *m_node_updater;
162
163     QRect m_device_rect;
164     QRect m_viewport_rect;
165
166     QSet<QSGNode *> m_nodes_to_preprocess;
167
168     QMatrix4x4 m_projection_matrix;
169     QOpenGLShaderProgram m_clip_program;
170     int m_clip_matrix_id;
171
172     const QSGBindable *m_bindable;
173
174     uint m_changed_emitted : 1;
175     uint m_mirrored : 1;
176     uint m_is_rendering : 1;
177
178     uint m_vertex_buffer_bound : 1;
179     uint m_index_buffer_bound : 1;
180 };
181
182 Q_DECLARE_OPERATORS_FOR_FLAGS(QSGRenderer::ClearMode)
183
184 class Q_QUICK_EXPORT QSGBindable
185 {
186 public:
187     virtual ~QSGBindable() { }
188     virtual void bind() const = 0;
189     virtual void clear(QSGRenderer::ClearMode mode) const;
190     virtual void reactivate() const;
191 };
192
193 class QSGBindableFbo : public QSGBindable
194 {
195 public:
196     QSGBindableFbo(QOpenGLFramebufferObject *fbo);
197     virtual void bind() const;
198 private:
199     QOpenGLFramebufferObject *m_fbo;
200 };
201
202
203
204 QSGMaterialShader::RenderState QSGRenderer::state(QSGMaterialShader::RenderState::DirtyStates dirty) const
205 {
206     QSGMaterialShader::RenderState s;
207     s.m_dirty = dirty;
208     s.m_data = this;
209     return s;
210 }
211
212
213 class Q_QUICK_EXPORT QSGNodeDumper : public QSGNodeVisitor {
214
215 public:
216     static void dump(QSGNode *n);
217
218     QSGNodeDumper() : m_indent(0) {}
219     void visitNode(QSGNode *n);
220     void visitChildren(QSGNode *n);
221
222 private:
223     int m_indent;
224 };
225
226
227
228 QT_END_NAMESPACE
229
230 QT_END_HEADER
231
232 #endif // RENDERER_H