Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / quick / scenegraph / coreapi / qsggeometry.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the Qt scene graph research project.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QSGGEOMETRY_H
43 #define QSGGEOMETRY_H
44
45 #include <QtQuick/qtquickglobal.h>
46 #include <QtGui/qopengl.h>
47 #include <QRectF>
48
49 QT_BEGIN_HEADER
50
51 QT_BEGIN_NAMESPACE
52
53 class QSGGeometryData;
54
55 class Q_QUICK_EXPORT QSGGeometry
56 {
57 public:
58
59     struct Attribute
60     {
61         int position;
62         int tupleSize;
63         int type;
64
65         uint isVertexCoordinate : 1;
66         uint migrateYourCodeToUseTheCreateFunction: 31; // ### Remove before release
67
68         static Attribute create(int pos, int tupleSize, int primitiveType, bool isPosition = false);
69     };
70
71     struct AttributeSet {
72         int count;
73         int stride;
74         const Attribute *attributes;
75     };
76
77     struct Point2D {
78         float x, y;
79         void set(float nx, float ny) {
80             x = nx; y = ny;
81         }
82     };
83     struct TexturedPoint2D {
84         float x, y;
85         float tx, ty;
86         void set(float nx, float ny, float ntx, float nty) {
87             x = nx; y = ny; tx = ntx; ty = nty;
88         }
89     };
90     struct ColoredPoint2D {
91         float x, y;
92         unsigned char r, g, b, a;
93         void set(float nx, float ny, uchar nr, uchar ng, uchar nb, uchar na) {
94             x = nx; y = ny;
95             r = nr; g = ng, b = nb; a = na;
96         }
97     };
98
99     static const AttributeSet &defaultAttributes_Point2D();
100     static const AttributeSet &defaultAttributes_TexturedPoint2D();
101     static const AttributeSet &defaultAttributes_ColoredPoint2D();
102
103     enum DataPattern {
104         AlwaysUploadPattern = 0,
105         StreamPattern       = 1,
106         DynamicPattern      = 2,
107         StaticPattern       = 3
108     };
109
110     QSGGeometry(const QSGGeometry::AttributeSet &attribs,
111                 int vertexCount,
112                 int indexCount = 0,
113                 int indexType = GL_UNSIGNED_SHORT);
114     virtual ~QSGGeometry();
115
116     void setDrawingMode(GLenum mode);
117     inline GLenum drawingMode() const { return m_drawing_mode; }
118
119     void allocate(int vertexCount, int indexCount = 0);
120
121     int vertexCount() const { return m_vertex_count; }
122
123     void *vertexData() { return m_data; }
124     inline Point2D *vertexDataAsPoint2D();
125     inline TexturedPoint2D *vertexDataAsTexturedPoint2D();
126     inline ColoredPoint2D *vertexDataAsColoredPoint2D();
127
128     inline const void *vertexData() const { return m_data; }
129     inline const Point2D *vertexDataAsPoint2D() const;
130     inline const TexturedPoint2D *vertexDataAsTexturedPoint2D() const;
131     inline const ColoredPoint2D *vertexDataAsColoredPoint2D() const;
132
133     inline int indexType() const { return m_index_type; }
134
135     int indexCount() const { return m_index_count; }
136
137     void *indexData();
138     inline uint *indexDataAsUInt();
139     inline quint16 *indexDataAsUShort();
140
141     inline int sizeOfIndex() const;
142
143     const void *indexData() const;
144     inline const uint *indexDataAsUInt() const;
145     inline const quint16 *indexDataAsUShort() const;
146
147     inline int attributeCount() const { return m_attributes.count; }
148     inline const Attribute *attributes() const { return m_attributes.attributes; }
149     inline int sizeOfVertex() const { return m_attributes.stride; }
150
151     static void updateRectGeometry(QSGGeometry *g, const QRectF &rect);
152     static void updateTexturedRectGeometry(QSGGeometry *g, const QRectF &rect, const QRectF &sourceRect);
153
154     void setIndexDataPattern(DataPattern p);
155     DataPattern indexDataPattern() const { return (DataPattern) m_index_usage_pattern; }
156
157     void setVertexDataPattern(DataPattern p);
158     DataPattern vertexDataPattern() const { return (DataPattern) m_vertex_usage_pattern; }
159
160     void markIndexDataDirty();
161     void markVertexDataDirty();
162
163     float lineWidth() const;
164     void setLineWidth(float w);
165
166 private:
167     friend class QSGGeometryData;
168
169     int m_drawing_mode;
170     int m_vertex_count;
171     int m_index_count;
172     int m_index_type;
173     const AttributeSet &m_attributes;
174     void *m_data;
175     int m_index_data_offset;
176
177     QSGGeometryData *m_server_data;
178
179     uint m_owns_data : 1;
180     uint m_index_usage_pattern : 2;
181     uint m_vertex_usage_pattern : 2;
182     uint m_dirty_index_data : 1;
183     uint m_dirty_vertex_data : 1;
184     uint m_reserved_bits : 27;
185
186     float m_prealloc[16];
187
188     float m_line_width;
189 };
190
191 inline uint *QSGGeometry::indexDataAsUInt()
192 {
193     Q_ASSERT(m_index_type == GL_UNSIGNED_INT);
194     return (uint *) indexData();
195 }
196
197 inline quint16 *QSGGeometry::indexDataAsUShort()
198 {
199     Q_ASSERT(m_index_type == GL_UNSIGNED_SHORT);
200     return (quint16 *) indexData();
201 }
202
203 inline const uint *QSGGeometry::indexDataAsUInt() const
204 {
205     Q_ASSERT(m_index_type == GL_UNSIGNED_INT);
206     return (uint *) indexData();
207 }
208
209 inline const quint16 *QSGGeometry::indexDataAsUShort() const
210 {
211     Q_ASSERT(m_index_type == GL_UNSIGNED_SHORT);
212     return (quint16 *) indexData();
213 }
214
215 inline QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D()
216 {
217     Q_ASSERT(m_attributes.count == 1);
218     Q_ASSERT(m_attributes.stride == 2 * sizeof(float));
219     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
220     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
221     Q_ASSERT(m_attributes.attributes[0].position == 0);
222     return (Point2D *) m_data;
223 }
224
225 inline QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D()
226 {
227     Q_ASSERT(m_attributes.count == 2);
228     Q_ASSERT(m_attributes.stride == 4 * sizeof(float));
229     Q_ASSERT(m_attributes.attributes[0].position == 0);
230     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
231     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
232     Q_ASSERT(m_attributes.attributes[1].position == 1);
233     Q_ASSERT(m_attributes.attributes[1].tupleSize == 2);
234     Q_ASSERT(m_attributes.attributes[1].type == GL_FLOAT);
235     return (TexturedPoint2D *) m_data;
236 }
237
238 inline QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D()
239 {
240     Q_ASSERT(m_attributes.count == 2);
241     Q_ASSERT(m_attributes.stride == 2 * sizeof(float) + 4 * sizeof(char));
242     Q_ASSERT(m_attributes.attributes[0].position == 0);
243     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
244     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
245     Q_ASSERT(m_attributes.attributes[1].position == 1);
246     Q_ASSERT(m_attributes.attributes[1].tupleSize == 4);
247     Q_ASSERT(m_attributes.attributes[1].type == GL_UNSIGNED_BYTE);
248     return (ColoredPoint2D *) m_data;
249 }
250
251 inline const QSGGeometry::Point2D *QSGGeometry::vertexDataAsPoint2D() const
252 {
253     Q_ASSERT(m_attributes.count == 1);
254     Q_ASSERT(m_attributes.stride == 2 * sizeof(float));
255     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
256     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
257     Q_ASSERT(m_attributes.attributes[0].position == 0);
258     return (const Point2D *) m_data;
259 }
260
261 inline const QSGGeometry::TexturedPoint2D *QSGGeometry::vertexDataAsTexturedPoint2D() const
262 {
263     Q_ASSERT(m_attributes.count == 2);
264     Q_ASSERT(m_attributes.stride == 4 * sizeof(float));
265     Q_ASSERT(m_attributes.attributes[0].position == 0);
266     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
267     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
268     Q_ASSERT(m_attributes.attributes[1].position == 1);
269     Q_ASSERT(m_attributes.attributes[1].tupleSize == 2);
270     Q_ASSERT(m_attributes.attributes[1].type == GL_FLOAT);
271     return (const TexturedPoint2D *) m_data;
272 }
273
274 inline const QSGGeometry::ColoredPoint2D *QSGGeometry::vertexDataAsColoredPoint2D() const
275 {
276     Q_ASSERT(m_attributes.count == 2);
277     Q_ASSERT(m_attributes.stride == 2 * sizeof(float) + 4 * sizeof(char));
278     Q_ASSERT(m_attributes.attributes[0].position == 0);
279     Q_ASSERT(m_attributes.attributes[0].tupleSize == 2);
280     Q_ASSERT(m_attributes.attributes[0].type == GL_FLOAT);
281     Q_ASSERT(m_attributes.attributes[1].position == 1);
282     Q_ASSERT(m_attributes.attributes[1].tupleSize == 4);
283     Q_ASSERT(m_attributes.attributes[1].type == GL_UNSIGNED_BYTE);
284     return (const ColoredPoint2D *) m_data;
285 }
286
287 int QSGGeometry::sizeOfIndex() const
288 {
289     if (m_index_type == GL_UNSIGNED_SHORT) return 2;
290     else if (m_index_type == GL_UNSIGNED_BYTE) return 1;
291     else if (m_index_type == GL_UNSIGNED_INT) return 4;
292     return 0;
293 }
294
295 QT_END_NAMESPACE
296
297 QT_END_HEADER
298
299 #endif // QSGGEOMETRY_H