c4f4674a797e2f1949931c3b207b4b5fd65df82a
[profile/ivi/qtdeclarative.git] / src / declarative / scenegraph / coreapi / qsgnode.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
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 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 QT_MODULE(Declarative)
55
56 #define QML_RUNTIME_TESTING
57
58 class QSGRenderer;
59
60 class QSGNode;
61 class QSGRootNode;
62 class QSGGeometryNode;
63 class QSGTransformNode;
64 class QSGClipNode;
65
66 class Q_DECLARATIVE_EXPORT QSGNode
67 {
68 public:
69     enum NodeType {
70         BasicNodeType,
71         RootNodeType,
72         GeometryNodeType,
73         TransformNodeType,
74         ClipNodeType,
75         OpacityNodeType,
76         UserNodeType = 1024
77     };
78
79     enum DirtyFlag {
80         DirtyMatrix                 = 0x0001,
81         DirtyClipList               = 0x0002,
82         DirtyNodeAdded              = 0x0004,
83         DirtyNodeRemoved            = 0x0008,
84         DirtyGeometry               = 0x0010,
85         DirtyMaterial               = 0x0040,
86         DirtyOpacity                = 0x0080,
87         DirtyForceUpdate            = 0x0100,
88
89         DirtyPropagationMask        = DirtyMatrix
90                                       | DirtyClipList
91                                       | DirtyNodeAdded
92                                       | DirtyOpacity
93                                       | DirtyForceUpdate,
94
95     };
96     Q_DECLARE_FLAGS(DirtyFlags, DirtyFlag)
97
98     enum Flag {
99         // Lower 16 bites reserved for general node
100         OwnedByParent               = 0x0001,
101         UsePreprocess               = 0x0002,
102         ChildrenDoNotOverlap        = 0x0004,
103
104         // Upper 16 bits reserved for node subclasses
105
106         // QSGBasicGeometryNode
107         OwnsGeometry                = 0x00010000,
108         OwnsMaterial                = 0x00020000,
109         OwnsOpaqueMaterial          = 0x00040000
110     };
111     Q_DECLARE_FLAGS(Flags, Flag)
112
113     QSGNode();
114     virtual ~QSGNode();
115
116     QSGNode *parent() const { return m_parent; }
117
118     void removeChildNode(QSGNode *node);
119     void removeAllChildNodes();
120     void prependChildNode(QSGNode *node);
121     void appendChildNode(QSGNode *node);
122     void insertChildNodeBefore(QSGNode *node, QSGNode *before);
123     void insertChildNodeAfter(QSGNode *node, QSGNode *after);
124
125     int childCount() const;
126     QSGNode *childAtIndex(int i) const;
127     QSGNode *firstChild() const { return m_firstChild; }
128     QSGNode *lastChild() const { return m_lastChild; }
129     QSGNode *nextSibling() const { return m_nextSibling; }
130     QSGNode* previousSibling() const { return m_previousSibling; }
131
132     inline NodeType type() const { return m_type; }
133
134     void clearDirty() { m_flags = 0; }
135     void markDirty(DirtyFlags flags);
136     DirtyFlags dirtyFlags() const { return m_flags; }
137
138     virtual bool isSubtreeBlocked() const;
139
140     Flags flags() const { return m_nodeFlags; }
141     void setFlag(Flag, bool = true);
142     void setFlags(Flags, bool = true);
143
144     virtual void preprocess() { }
145
146 #ifdef QML_RUNTIME_TESTING
147     QString description;
148 #endif
149
150 protected:
151     QSGNode(NodeType type);
152
153 private:
154     friend class QSGRootNode;
155
156     void init();
157     void destroy();
158
159     QSGNode *m_parent;
160     NodeType m_type;
161     QSGNode *m_firstChild;
162     QSGNode *m_lastChild;
163     QSGNode *m_nextSibling;
164     QSGNode *m_previousSibling;
165     int m_subtreeGeometryCount;
166
167     Flags m_nodeFlags;
168     DirtyFlags m_flags;
169
170     void *m_reserved;
171 };
172
173 Q_DECLARE_OPERATORS_FOR_FLAGS(QSGNode::DirtyFlags);
174 Q_DECLARE_OPERATORS_FOR_FLAGS(QSGNode::Flags);
175
176 class Q_DECLARATIVE_EXPORT QSGBasicGeometryNode : public QSGNode
177 {
178 public:
179 //    enum  UsagePattern {
180 //        Static,
181 //        Dynamic,
182 //        Stream
183 //    };
184 //    void setUsagePattern(UsagePattern pattern);
185 //    UsagePattern usagePattern() const { return m_pattern; }
186
187     ~QSGBasicGeometryNode();
188
189     void setGeometry(QSGGeometry *geometry);
190     const QSGGeometry *geometry() const { return m_geometry; }
191     QSGGeometry *geometry() { return m_geometry; }
192
193     const QMatrix4x4 *matrix() const { return m_matrix; }
194     const QSGClipNode *clipList() const { return m_clip_list; }
195
196 protected:
197     QSGBasicGeometryNode(NodeType type);
198
199 private:
200     friend class QSGNodeUpdater;
201     QSGGeometry *m_geometry;
202
203     int m_reserved_start_index;
204     int m_reserved_end_index;
205
206     const QMatrix4x4 *m_matrix;
207     const QSGClipNode *m_clip_list;
208
209 //    UsagePattern m_pattern;
210 };
211
212 class QSGMaterial;
213
214 class Q_DECLARATIVE_EXPORT QSGGeometryNode : public QSGBasicGeometryNode
215 {
216 public:
217     QSGGeometryNode();
218     ~QSGGeometryNode();
219
220     void setMaterial(QSGMaterial *material);
221     QSGMaterial *material() const { return m_material; }
222
223     void setOpaqueMaterial(QSGMaterial *material);
224     QSGMaterial *opaqueMaterial() const { return m_opaque_material; }
225
226     QSGMaterial *activeMaterial() const;
227
228     void setRenderOrder(int order);
229     int renderOrder() const { return m_render_order; }
230
231     void setInheritedOpacity(qreal opacity);
232     qreal inheritedOpacity() const { return m_opacity; }
233
234 private:
235     friend class QSGNodeUpdater;
236
237     int m_render_order;
238     QSGMaterial *m_material;
239     QSGMaterial *m_opaque_material;
240
241     qreal m_opacity;
242 };
243
244 class Q_DECLARATIVE_EXPORT QSGClipNode : public QSGBasicGeometryNode
245 {
246 public:
247     QSGClipNode();
248     ~QSGClipNode();
249
250     void setIsRectangular(bool rectHint);
251     bool isRectangular() const { return m_is_rectangular; }
252
253     void setClipRect(const QRectF &);
254     QRectF clipRect() const { return m_clip_rect; }
255
256 private:
257     uint m_is_rectangular : 1;
258     uint m_reserved : 31;
259
260     QRectF m_clip_rect;
261 };
262
263
264 class Q_DECLARATIVE_EXPORT QSGTransformNode : public QSGNode
265 {
266 public:
267     QSGTransformNode();
268     ~QSGTransformNode();
269
270     void setMatrix(const QMatrix4x4 &matrix);
271     const QMatrix4x4 &matrix() const { return m_matrix; }
272
273     void setCombinedMatrix(const QMatrix4x4 &matrix);
274     const QMatrix4x4 &combinedMatrix() const { return m_combined_matrix; }
275
276 private:
277     QMatrix4x4 m_matrix;
278     QMatrix4x4 m_combined_matrix;
279 };
280
281
282 class Q_DECLARATIVE_EXPORT QSGRootNode : public QSGNode
283 {
284 public:
285     QSGRootNode();
286     ~QSGRootNode();
287
288 private:
289     void notifyNodeChange(QSGNode *node, DirtyFlags flags);
290
291     friend class QSGRenderer;
292     friend class QSGNode;
293     friend class QSGGeometryNode;
294
295     QList<QSGRenderer *> m_renderers;
296 };
297
298
299 class Q_DECLARATIVE_EXPORT QSGOpacityNode : public QSGNode
300 {
301 public:
302     QSGOpacityNode();
303     ~QSGOpacityNode();
304
305     void setOpacity(qreal opacity);
306     qreal opacity() const { return m_opacity; }
307
308     void setCombinedOpacity(qreal opacity);
309     qreal combinedOpacity() const { return m_combined_opacity; }
310
311     bool isSubtreeBlocked() const;
312
313 private:
314     qreal m_opacity;
315     qreal m_combined_opacity;
316 };
317
318 class Q_DECLARATIVE_EXPORT QSGNodeVisitor {
319 public:
320     virtual ~QSGNodeVisitor();
321
322 protected:
323     virtual void enterTransformNode(QSGTransformNode *) {}
324     virtual void leaveTransformNode(QSGTransformNode *) {}
325     virtual void enterClipNode(QSGClipNode *) {}
326     virtual void leaveClipNode(QSGClipNode *) {}
327     virtual void enterGeometryNode(QSGGeometryNode *) {}
328     virtual void leaveGeometryNode(QSGGeometryNode *) {}
329     virtual void enterOpacityNode(QSGOpacityNode *) {}
330     virtual void leaveOpacityNode(QSGOpacityNode *) {}
331     virtual void visitNode(QSGNode *n);
332     virtual void visitChildren(QSGNode *n);
333 };
334
335 #ifndef QT_NO_DEBUG_STREAM
336 Q_DECLARATIVE_EXPORT QDebug operator<<(QDebug, const QSGNode *n);
337 Q_DECLARATIVE_EXPORT QDebug operator<<(QDebug, const QSGGeometryNode *n);
338 Q_DECLARATIVE_EXPORT QDebug operator<<(QDebug, const QSGTransformNode *n);
339 Q_DECLARATIVE_EXPORT QDebug operator<<(QDebug, const QSGOpacityNode *n);
340 Q_DECLARATIVE_EXPORT QDebug operator<<(QDebug, const QSGRootNode *n);
341
342 #endif
343
344 QT_END_NAMESPACE
345
346 QT_END_HEADER
347
348 #endif // NODE_H