9271bc4d871710e3e9fec46641d3535ac39c345f
[profile/ivi/qtdeclarative.git] / src / quick / scenegraph / coreapi / qsgnode.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtQml 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 Digia.  For licensing terms and
14 ** conditions see http://qt.digia.com/licensing.  For further information
15 ** use the contact form at http://qt.digia.com/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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights.  These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file.  Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef NODE_H
43 #define NODE_H
44
45 #include "qsggeometry.h"
46 #include <QtGui/QMatrix4x4>
47
48 #include <float.h>
49
50 QT_BEGIN_HEADER
51
52 QT_BEGIN_NAMESPACE
53
54 //#define QML_RUNTIME_TESTING
55
56 class QSGRenderer;
57
58 class QSGNode;
59 class QSGRootNode;
60 class QSGGeometryNode;
61 class QSGTransformNode;
62 class QSGClipNode;
63
64 class Q_QUICK_EXPORT QSGNode
65 {
66 public:
67     enum NodeType {
68         BasicNodeType,
69         GeometryNodeType,
70         TransformNodeType,
71         ClipNodeType,
72         OpacityNodeType,
73 #ifndef qdoc
74         RootNodeType,
75         RenderNodeType
76 #endif
77     };
78
79     enum Flag {
80         // Lower 16 bites reserved for general node
81         OwnedByParent               = 0x0001,
82         UsePreprocess               = 0x0002,
83
84         // Upper 16 bits reserved for node subclasses
85
86         // QSGBasicGeometryNode
87         OwnsGeometry                = 0x00010000,
88         OwnsMaterial                = 0x00020000,
89         OwnsOpaqueMaterial          = 0x00040000
90     };
91     Q_DECLARE_FLAGS(Flags, Flag)
92
93     enum DirtyStateBit {
94         DirtyMatrix                 = 0x0100,
95         DirtyNodeAdded              = 0x0400,
96         DirtyNodeRemoved            = 0x0800,
97         DirtyGeometry               = 0x1000,
98         DirtyMaterial               = 0x2000,
99         DirtyOpacity                = 0x4000,
100
101 #ifndef qdoc
102         DirtyForceUpdate            = 0x8000,
103
104         DirtyUsePreprocess          = UsePreprocess,
105
106         DirtyPropagationMask        = DirtyMatrix
107                                       | DirtyNodeAdded
108                                       | DirtyOpacity
109                                       | DirtyForceUpdate
110 #endif
111
112     };
113     Q_DECLARE_FLAGS(DirtyState, DirtyStateBit)
114
115     QSGNode();
116     virtual ~QSGNode();
117
118     QSGNode *parent() const { return m_parent; }
119
120     void removeChildNode(QSGNode *node);
121     void removeAllChildNodes();
122     void prependChildNode(QSGNode *node);
123     void appendChildNode(QSGNode *node);
124     void insertChildNodeBefore(QSGNode *node, QSGNode *before);
125     void insertChildNodeAfter(QSGNode *node, QSGNode *after);
126
127     int childCount() const;
128     QSGNode *childAtIndex(int i) const;
129     QSGNode *firstChild() const { return m_firstChild; }
130     QSGNode *lastChild() const { return m_lastChild; }
131     QSGNode *nextSibling() const { return m_nextSibling; }
132     QSGNode* previousSibling() const { return m_previousSibling; }
133
134     inline NodeType type() const { return m_type; }
135
136     void clearDirty() { m_dirtyState = 0; }
137     void markDirty(DirtyState bits);
138     DirtyState dirtyState() const { return m_dirtyState; }
139
140     virtual bool isSubtreeBlocked() const;
141
142     Flags flags() const { return m_nodeFlags; }
143     void setFlag(Flag, bool = true);
144     void setFlags(Flags, bool = true);
145
146     virtual void preprocess() { }
147
148 #ifdef QML_RUNTIME_TESTING
149     QString description;
150 #endif
151
152 protected:
153     QSGNode(NodeType type);
154
155 private:
156     friend class QSGRootNode;
157
158     void init();
159     void destroy();
160
161     QSGNode *m_parent;
162     NodeType m_type;
163     QSGNode *m_firstChild;
164     QSGNode *m_lastChild;
165     QSGNode *m_nextSibling;
166     QSGNode *m_previousSibling;
167     int m_subtreeRenderableCount;
168
169     Flags m_nodeFlags;
170     DirtyState m_dirtyState;
171
172     void *m_reserved;
173 };
174
175 class Q_QUICK_EXPORT QSGBasicGeometryNode : public QSGNode
176 {
177 public:
178     ~QSGBasicGeometryNode();
179
180     void setGeometry(QSGGeometry *geometry);
181     const QSGGeometry *geometry() const { return m_geometry; }
182     QSGGeometry *geometry() { return m_geometry; }
183
184     const QMatrix4x4 *matrix() const { return m_matrix; }
185     const QSGClipNode *clipList() const { return m_clip_list; }
186
187 protected:
188     QSGBasicGeometryNode(NodeType type);
189
190 private:
191     friend class QSGNodeUpdater;
192     QSGGeometry *m_geometry;
193
194     int m_reserved_start_index;
195     int m_reserved_end_index;
196
197     const QMatrix4x4 *m_matrix;
198     const QSGClipNode *m_clip_list;
199 };
200
201 class QSGMaterial;
202
203 class Q_QUICK_EXPORT QSGGeometryNode : public QSGBasicGeometryNode
204 {
205 public:
206     QSGGeometryNode();
207     ~QSGGeometryNode();
208
209     void setMaterial(QSGMaterial *material);
210     QSGMaterial *material() const { return m_material; }
211
212     void setOpaqueMaterial(QSGMaterial *material);
213     QSGMaterial *opaqueMaterial() const { return m_opaque_material; }
214
215     QSGMaterial *activeMaterial() const;
216
217     void setRenderOrder(int order);
218     int renderOrder() const { return m_render_order; }
219
220     void setInheritedOpacity(qreal opacity);
221     qreal inheritedOpacity() const { return m_opacity; }
222
223 private:
224     friend class QSGNodeUpdater;
225
226     int m_render_order;
227     QSGMaterial *m_material;
228     QSGMaterial *m_opaque_material;
229
230     qreal m_opacity;
231 };
232
233 class Q_QUICK_EXPORT QSGClipNode : public QSGBasicGeometryNode
234 {
235 public:
236     QSGClipNode();
237     ~QSGClipNode();
238
239     void setIsRectangular(bool rectHint);
240     bool isRectangular() const { return m_is_rectangular; }
241
242     void setClipRect(const QRectF &);
243     QRectF clipRect() const { return m_clip_rect; }
244
245 private:
246     uint m_is_rectangular : 1;
247     uint m_reserved : 31;
248
249     QRectF m_clip_rect;
250 };
251
252
253 class Q_QUICK_EXPORT QSGTransformNode : public QSGNode
254 {
255 public:
256     QSGTransformNode();
257     ~QSGTransformNode();
258
259     void setMatrix(const QMatrix4x4 &matrix);
260     const QMatrix4x4 &matrix() const { return m_matrix; }
261
262     void setCombinedMatrix(const QMatrix4x4 &matrix);
263     const QMatrix4x4 &combinedMatrix() const { return m_combined_matrix; }
264
265 private:
266     QMatrix4x4 m_matrix;
267     QMatrix4x4 m_combined_matrix;
268 };
269
270
271 class Q_QUICK_EXPORT QSGRootNode : public QSGNode
272 {
273 public:
274     QSGRootNode();
275     ~QSGRootNode();
276
277 private:
278     void notifyNodeChange(QSGNode *node, DirtyState state);
279
280     friend class QSGRenderer;
281     friend class QSGNode;
282     friend class QSGGeometryNode;
283
284     QList<QSGRenderer *> m_renderers;
285 };
286
287
288 class Q_QUICK_EXPORT QSGOpacityNode : public QSGNode
289 {
290 public:
291     QSGOpacityNode();
292     ~QSGOpacityNode();
293
294     void setOpacity(qreal opacity);
295     qreal opacity() const { return m_opacity; }
296
297     void setCombinedOpacity(qreal opacity);
298     qreal combinedOpacity() const { return m_combined_opacity; }
299
300     bool isSubtreeBlocked() const;
301
302 private:
303     qreal m_opacity;
304     qreal m_combined_opacity;
305 };
306
307 class Q_QUICK_EXPORT QSGNodeVisitor {
308 public:
309     virtual ~QSGNodeVisitor();
310
311 protected:
312     virtual void enterTransformNode(QSGTransformNode *) {}
313     virtual void leaveTransformNode(QSGTransformNode *) {}
314     virtual void enterClipNode(QSGClipNode *) {}
315     virtual void leaveClipNode(QSGClipNode *) {}
316     virtual void enterGeometryNode(QSGGeometryNode *) {}
317     virtual void leaveGeometryNode(QSGGeometryNode *) {}
318     virtual void enterOpacityNode(QSGOpacityNode *) {}
319     virtual void leaveOpacityNode(QSGOpacityNode *) {}
320     virtual void visitNode(QSGNode *n);
321     virtual void visitChildren(QSGNode *n);
322 };
323
324 #ifndef QT_NO_DEBUG_STREAM
325 Q_QUICK_EXPORT QDebug operator<<(QDebug, const QSGNode *n);
326 Q_QUICK_EXPORT QDebug operator<<(QDebug, const QSGGeometryNode *n);
327 Q_QUICK_EXPORT QDebug operator<<(QDebug, const QSGTransformNode *n);
328 Q_QUICK_EXPORT QDebug operator<<(QDebug, const QSGOpacityNode *n);
329 Q_QUICK_EXPORT QDebug operator<<(QDebug, const QSGRootNode *n);
330
331 #endif
332
333 Q_DECLARE_OPERATORS_FOR_FLAGS(QSGNode::DirtyState)
334 Q_DECLARE_OPERATORS_FOR_FLAGS(QSGNode::Flags)
335
336 QT_END_NAMESPACE
337
338 QT_END_HEADER
339
340 #endif // NODE_H