rename QSGGeometry::stride() to sizeOfVertex for symetry with index
[profile/ivi/qtdeclarative.git] / src / declarative / scenegraph / coreapi / qsggeometry.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 Qt scene graph research project.
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 QSGGEOMETRY_H
43 #define QSGGEOMETRY_H
44
45 #include <QtGui/qopengl.h>
46 #include <QRectF>
47
48 QT_BEGIN_HEADER
49
50 QT_BEGIN_NAMESPACE
51
52 QT_MODULE(Declarative)
53
54 class QSGGeometryData;
55
56 class Q_DECLARATIVE_EXPORT QSGGeometry
57 {
58 public:
59
60     struct Attribute
61     {
62         int position;
63         int tupleSize;
64         int type;
65
66         uint isVertexCoordinate : 1;
67         uint migrateYourCodeToUseTheCreateFunction: 31; // ### Remove before release
68
69         static Attribute create(int pos, int tupleSize, int primitiveType, bool isPosition = false);
70     };
71
72     struct AttributeSet {
73         int count;
74         int stride;
75         const Attribute *attributes;
76     };
77
78     struct Point2D {
79         float x, y;
80         void set(float nx, float ny) {
81             x = nx; y = ny;
82         }
83     };
84     struct TexturedPoint2D {
85         float x, y;
86         float tx, ty;
87         void set(float nx, float ny, float ntx, float nty) {
88             x = nx; y = ny; tx = ntx; ty = nty;
89         }
90     };
91     struct ColoredPoint2D {
92         float x, y;
93         unsigned char r, g, b, a;
94         void set(float nx, float ny, uchar nr, uchar ng, uchar nb, uchar na) {
95             x = nx; y = ny;
96             r = nr; g = ng, b = nb; a = na;
97         }
98     };
99
100     static const AttributeSet &defaultAttributes_Point2D();
101     static const AttributeSet &defaultAttributes_TexturedPoint2D();
102     static const AttributeSet &defaultAttributes_ColoredPoint2D();
103
104     enum DataPattern {
105         AlwaysUploadPattern = 0,
106         StreamPattern       = 1,
107         DynamicPattern      = 2,
108         StaticPattern       = 3
109     };
110
111     QSGGeometry(const QSGGeometry::AttributeSet &attribs,
112                 int vertexCount,
113                 int indexCount = 0,
114                 int indexType = GL_UNSIGNED_SHORT);
115     virtual ~QSGGeometry();
116
117     void setDrawingMode(GLenum mode);
118     inline GLenum drawingMode() const { return m_drawing_mode; }
119
120     void allocate(int vertexCount, int indexCount = 0);
121
122     int vertexCount() const { return m_vertex_count; }
123
124     void *vertexData() { return m_data; }
125     inline Point2D *vertexDataAsPoint2D();
126     inline TexturedPoint2D *vertexDataAsTexturedPoint2D();
127     inline ColoredPoint2D *vertexDataAsColoredPoint2D();
128
129     inline const void *vertexData() const { return m_data; }
130     inline const Point2D *vertexDataAsPoint2D() const;
131     inline const TexturedPoint2D *vertexDataAsTexturedPoint2D() const;
132     inline const ColoredPoint2D *vertexDataAsColoredPoint2D() const;
133
134     inline int indexType() const { return m_index_type; }
135
136     int indexCount() const { return m_index_count; }
137
138     void *indexData();
139     inline uint *indexDataAsUInt();
140     inline quint16 *indexDataAsUShort();
141
142     inline int sizeOfIndex() const;
143
144     const void *indexData() const;
145     inline const uint *indexDataAsUInt() const;
146     inline const quint16 *indexDataAsUShort() const;
147
148     inline int attributeCount() const { return m_attributes.count; }
149     inline const Attribute *attributes() const { return m_attributes.attributes; }
150     inline int sizeOfVertex() const { return m_attributes.stride; }
151
152     static void updateRectGeometry(QSGGeometry *g, const QRectF &rect);
153     static void updateTexturedRectGeometry(QSGGeometry *g, const QRectF &rect, const QRectF &sourceRect);
154
155     void setIndexDataPattern(DataPattern p);
156     DataPattern indexDataPattern() const { return (DataPattern) m_index_usage_pattern; }
157
158     void setVertexDataPattern(DataPattern p);
159     DataPattern vertexDataPattern() const { return (DataPattern) m_vertex_usage_pattern; }
160
161     void markIndexDataDirty();
162     void markVertexDataDirty();
163
164 private:
165     friend class QSGGeometryData;
166
167     int m_drawing_mode;
168     int m_vertex_count;
169     int m_index_count;
170     int m_index_type;
171     const AttributeSet &m_attributes;
172     void *m_data;
173     int m_index_data_offset;
174
175     QSGGeometryData *m_server_data;
176
177     uint m_owns_data : 1;
178     uint m_index_usage_pattern : 2;
179     uint m_vertex_usage_pattern : 2;
180     uint m_dirty_index_data : 1;
181     uint m_dirty_vertex_data : 1;
182     uint m_reserved_bits : 27;
183
184     float m_prealloc[16];
185 };
186
187 inline uint *QSGGeometry::indexDataAsUInt()
188 {
189     Q_ASSERT(m_index_type == GL_UNSIGNED_INT);
190     return (uint *) indexData();
191 }
192
193 inline quint16 *QSGGeometry::indexDataAsUShort()
194 {
195     Q_ASSERT(m_index_type == GL_UNSIGNED_SHORT);
196     return (quint16 *) indexData();
197 }
198
199 inline const uint *QSGGeometry::indexDataAsUInt() const
200 {
201     Q_ASSERT(m_index_type == GL_UNSIGNED_INT);
202     return (uint *) indexData();
203 }
204
205 inline const quint16 *QSGGeometry::indexDataAsUShort() const
206 {
207     Q_ASSERT(m_index_type == GL_UNSIGNED_SHORT);
208     return (quint16 *) indexData();
209 }
210
211 inline QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D()
212 {
213     Q_ASSERT(m_attributes.count == 1);
214     Q_ASSERT(m_attributes.stride == 2 * sizeof(float));
215     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
216     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
217     Q_ASSERT(m_attributes.attributes[0].position == 0);
218     return (Point2D *) m_data;
219 }
220
221 inline QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D()
222 {
223     Q_ASSERT(m_attributes.count == 2);
224     Q_ASSERT(m_attributes.stride == 4 * sizeof(float));
225     Q_ASSERT(m_attributes.attributes[0].position == 0);
226     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
227     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
228     Q_ASSERT(m_attributes.attributes[1].position == 1);
229     Q_ASSERT(m_attributes.attributes[1].tupleSize == 2);
230     Q_ASSERT(m_attributes.attributes[1].type == GL_FLOAT);
231     return (TexturedPoint2D *) m_data;
232 }
233
234 inline QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D()
235 {
236     Q_ASSERT(m_attributes.count == 2);
237     Q_ASSERT(m_attributes.stride == 2 * sizeof(float) + 4 * sizeof(char));
238     Q_ASSERT(m_attributes.attributes[0].position == 0);
239     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
240     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
241     Q_ASSERT(m_attributes.attributes[1].position == 1);
242     Q_ASSERT(m_attributes.attributes[1].tupleSize == 4);
243     Q_ASSERT(m_attributes.attributes[1].type == GL_UNSIGNED_BYTE);
244     return (ColoredPoint2D *) m_data;
245 }
246
247 inline const QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D() const
248 {
249     Q_ASSERT(m_attributes.count == 1);
250     Q_ASSERT(m_attributes.stride == 2 * sizeof(float));
251     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
252     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
253     Q_ASSERT(m_attributes.attributes[0].position == 0);
254     return (const Point2D *) m_data;
255 }
256
257 inline const QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D() const
258 {
259     Q_ASSERT(m_attributes.count == 2);
260     Q_ASSERT(m_attributes.stride == 4 * sizeof(float));
261     Q_ASSERT(m_attributes.attributes[0].position == 0);
262     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
263     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
264     Q_ASSERT(m_attributes.attributes[1].position == 1);
265     Q_ASSERT(m_attributes.attributes[1].tupleSize == 2);
266     Q_ASSERT(m_attributes.attributes[1].type == GL_FLOAT);
267     return (const TexturedPoint2D *) m_data;
268 }
269
270 inline const QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D() const
271 {
272     Q_ASSERT(m_attributes.count == 2);
273     Q_ASSERT(m_attributes.stride == 2 * sizeof(float) + 4 * sizeof(char));
274     Q_ASSERT(m_attributes.attributes[0].position == 0);
275     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
276     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
277     Q_ASSERT(m_attributes.attributes[1].position == 1);
278     Q_ASSERT(m_attributes.attributes[1].tupleSize == 4);
279     Q_ASSERT(m_attributes.attributes[1].type == GL_UNSIGNED_BYTE);
280     return (const ColoredPoint2D *) m_data;
281 }
282
283 int QSGGeometry::sizeOfIndex() const
284 {
285     if (m_index_type == GL_UNSIGNED_SHORT) return 2;
286     else if (m_index_type == GL_UNSIGNED_BYTE) return 1;
287     else if (m_index_type == GL_UNSIGNED_INT) return 4;
288     return 0;
289 }
290
291 QT_END_NAMESPACE
292
293 QT_END_HEADER
294
295 #endif // QSGGEOMETRY_H