ddf5ba14e4cdad67a5a3fc8cb66c1cc4637cd56f
[profile/ivi/qtdeclarative.git] / src / quick / items / context2d / qquickcontext2dcommandbuffer_p.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 QtQml module of the Qt Toolkit.
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 QQUICKCONTEXT2DCOMMANDBUFFER_P_H
43 #define QQUICKCONTEXT2DCOMMANDBUFFER_P_H
44
45 #include <QtCore/qmutex.h>
46 #include "qquickcontext2d_p.h"
47
48 QT_BEGIN_HEADER
49
50 QT_BEGIN_NAMESPACE
51
52 class QQuickCanvasItem;
53 class QMutex;
54
55 class QQuickContext2DCommandBuffer
56 {
57 public:
58     QQuickContext2DCommandBuffer();
59     ~QQuickContext2DCommandBuffer();
60     void reset();
61     void clear();
62
63     void lockQueue() { queueLock.lock(); }
64     void unlockQueue() { queueLock.unlock(); }
65     inline int size() {return commands.size();}
66     inline bool isEmpty() const {return commands.isEmpty(); }
67     inline bool hasNext() const {return cmdIdx < commands.size(); }
68     inline QQuickContext2D::PaintCommand takeNextCommand() { return commands[cmdIdx++]; }
69
70     inline qreal takeGlobalAlpha() { return takeReal(); }
71     inline QPainter::CompositionMode takeGlobalCompositeOperation(){ return static_cast<QPainter::CompositionMode>(takeInt()); }
72     inline QBrush takeStrokeStyle() { return takeBrush(); }
73     inline QBrush takeFillStyle() { return takeBrush(); }
74
75     inline qreal takeLineWidth() { return takeReal(); }
76     inline Qt::PenCapStyle takeLineCap() { return static_cast<Qt::PenCapStyle>(takeInt());}
77     inline Qt::PenJoinStyle takeLineJoin(){ return static_cast<Qt::PenJoinStyle>(takeInt());}
78     inline qreal takeMiterLimit() { return takeReal(); }
79
80     inline void setGlobalAlpha( qreal alpha)
81     {
82         commands << QQuickContext2D::GlobalAlpha;
83         reals << alpha;
84     }
85
86     inline void setGlobalCompositeOperation(QPainter::CompositionMode cm)
87     {
88         commands << QQuickContext2D::GlobalCompositeOperation;
89         ints << cm;
90     }
91
92     inline void setStrokeStyle(const QBrush &style, bool repeatX = false, bool repeatY = false)
93     {
94         commands << QQuickContext2D::StrokeStyle;
95         brushes << style;
96         bools << repeatX << repeatY;
97     }
98
99     inline void drawImage(const QImage& image,  const QRectF& sr, const QRectF& dr)
100     {
101         commands << QQuickContext2D::DrawImage;
102         images << image;
103         rects << sr << dr;
104     }
105
106     inline void drawPixmap(QQmlRefPointer<QQuickCanvasPixmap> pixmap, const QRectF& sr, const QRectF& dr)
107     {
108         commands << QQuickContext2D::DrawPixmap;
109         pixmaps << pixmap;
110         rects << sr << dr;
111     }
112
113     inline qreal takeShadowOffsetX() { return takeReal(); }
114     inline qreal takeShadowOffsetY() { return takeReal(); }
115     inline qreal takeShadowBlur() { return takeReal(); }
116     inline QColor takeShadowColor() { return takeColor(); }
117
118
119     inline void updateMatrix(const QTransform& matrix)
120     {
121         commands << QQuickContext2D::UpdateMatrix;
122         matrixes << matrix;
123     }
124
125     inline void clearRect(const QRectF& r)
126     {
127         commands << QQuickContext2D::ClearRect;
128         rects << r;
129     }
130
131     inline void fillRect(const QRectF& r)
132     {
133         commands << QQuickContext2D::FillRect;
134         rects << r;
135     }
136
137     inline void strokeRect(const QRectF& r)
138     {
139         QPainterPath p;
140         p.addRect(r);
141
142         commands << QQuickContext2D::Stroke;
143         pathes << p;
144     }
145
146
147     inline void fill(const QPainterPath& path)
148     {
149         commands << QQuickContext2D::Fill;
150         pathes << path;
151
152     }
153
154     inline void stroke(const QPainterPath& path)
155     {
156         commands << QQuickContext2D::Stroke;
157         pathes << path;
158     }
159
160     inline void clip(const QPainterPath& path)
161     {
162         commands << QQuickContext2D::Clip;
163         pathes << path;
164     }
165
166
167
168     inline void setFillStyle(const QBrush &style, bool repeatX = false, bool repeatY = false)
169     {
170         commands << QQuickContext2D::FillStyle;
171         brushes << style;
172         bools << repeatX << repeatY;
173     }
174
175
176     inline void setLineWidth( qreal w)
177     {
178         commands << QQuickContext2D::LineWidth;
179         reals << w;
180     }
181
182     inline void setLineCap(Qt::PenCapStyle  cap)
183     {
184         commands << QQuickContext2D::LineCap;
185         ints << cap;
186     }
187
188     inline void setLineJoin(Qt::PenJoinStyle join)
189     {
190         commands << QQuickContext2D::LineJoin;
191         ints << join;
192     }
193
194     inline void setMiterLimit( qreal limit)
195     {
196         commands << QQuickContext2D::MiterLimit;
197         reals << limit;
198     }
199
200     inline void setShadowOffsetX( qreal x)
201     {
202         commands << QQuickContext2D::ShadowOffsetX;
203         reals << x;
204     }
205
206     inline void setShadowOffsetY( qreal y)
207     {
208         commands << QQuickContext2D::ShadowOffsetY;
209         reals << y;
210     }
211
212     inline void setShadowBlur( qreal b)
213     {
214         commands << QQuickContext2D::ShadowBlur;
215         reals << b;
216     }
217
218     inline void setShadowColor(const QColor &color)
219     {
220         commands << QQuickContext2D::ShadowColor;
221         colors << color;
222     }
223
224     inline QTransform takeMatrix() { return matrixes[matrixIdx++]; }
225
226     inline QRectF takeRect() { return rects[rectIdx++]; }
227
228     inline QPainterPath takePath() { return pathes[pathIdx++]; }
229
230     inline const QImage& takeImage() { return images[imageIdx++]; }
231     inline QQmlRefPointer<QQuickCanvasPixmap> takePixmap() { return pixmaps[pixmapIdx++]; }
232
233     inline int takeInt() { return ints[intIdx++]; }
234     inline bool takeBool() {return bools[boolIdx++]; }
235     inline qreal takeReal() { return reals[realIdx++]; }
236     inline QColor takeColor() { return colors[colorIdx++]; }
237     inline QBrush takeBrush() { return brushes[brushIdx++]; }
238
239     void replay(QPainter* painter, QQuickContext2D::State& state);
240 private:
241     QPen makePen(const QQuickContext2D::State& state);
242     void setPainterState(QPainter* painter, const QQuickContext2D::State& state, const QPen& pen);
243     int cmdIdx;
244     int intIdx;
245     int boolIdx;
246     int realIdx;
247     int rectIdx;
248     int colorIdx;
249     int matrixIdx;
250     int brushIdx;
251     int pathIdx;
252     int imageIdx;
253     int pixmapIdx;
254     QVector<QQuickContext2D::PaintCommand> commands;
255
256     QVector<int> ints;
257     QVector<bool> bools;
258     QVector<qreal> reals;
259     QVector<QRectF> rects;
260     QVector<QColor> colors;
261     QVector<QTransform> matrixes;
262     QVector<QBrush> brushes;
263     QVector<QPainterPath> pathes;
264     QVector<QImage> images;
265     QVector<QQmlRefPointer<QQuickCanvasPixmap> > pixmaps;
266     QMutex queueLock;
267 };
268
269 QT_END_HEADER
270
271 QT_END_NAMESPACE
272
273 #endif // QQUICKCONTEXT2DCOMMANDBUFFER_P_H