1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtQuick 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 The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/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 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QQUICKCONTEXT2DCOMMANDBUFFER_P_H
41#define QQUICKCONTEXT2DCOMMANDBUFFER_P_H
42
43//
44// W A R N I N G
45// -------------
46//
47// This file is not part of the Qt API. It exists purely as an
48// implementation detail. This header file may change from version to
49// version without notice, or even be removed.
50//
51// We mean it.
52//
53
54#include <private/qtquickglobal_p.h>
55
56QT_REQUIRE_CONFIG(quick_canvas);
57
58#include <QtCore/qmutex.h>
59#include "qquickcontext2d_p.h"
60
61QT_BEGIN_NAMESPACE
62
63class QQuickCanvasItem;
64class QMutex;
65
66class QQuickContext2DCommandBuffer
67{
68public:
69 QQuickContext2DCommandBuffer();
70 ~QQuickContext2DCommandBuffer();
71 void reset();
72 void clear();
73
74 inline int size() const { return commands.size(); }
75 inline bool isEmpty() const {return commands.isEmpty(); }
76 inline bool hasNext() const {return cmdIdx < commands.size(); }
77 inline QQuickContext2D::PaintCommand takeNextCommand() { return commands.at(i: cmdIdx++); }
78
79 inline qreal takeGlobalAlpha() { return takeReal(); }
80 inline QPainter::CompositionMode takeGlobalCompositeOperation(){ return static_cast<QPainter::CompositionMode>(takeInt()); }
81 inline QBrush takeStrokeStyle() { return takeBrush(); }
82 inline QBrush takeFillStyle() { return takeBrush(); }
83
84 inline qreal takeLineWidth() { return takeReal(); }
85 inline Qt::PenCapStyle takeLineCap() { return static_cast<Qt::PenCapStyle>(takeInt());}
86 inline Qt::PenJoinStyle takeLineJoin(){ return static_cast<Qt::PenJoinStyle>(takeInt());}
87 inline qreal takeMiterLimit() { return takeReal(); }
88
89 inline void setGlobalAlpha( qreal alpha)
90 {
91 commands << QQuickContext2D::GlobalAlpha;
92 reals << alpha;
93 }
94
95 inline void setGlobalCompositeOperation(QPainter::CompositionMode cm)
96 {
97 commands << QQuickContext2D::GlobalCompositeOperation;
98 ints << cm;
99 }
100
101 inline void setStrokeStyle(const QBrush &style, bool repeatX = false, bool repeatY = false)
102 {
103 commands << QQuickContext2D::StrokeStyle;
104 brushes << style;
105 bools << repeatX << repeatY;
106 }
107
108 inline void drawImage(const QImage& image, const QRectF& sr, const QRectF& dr)
109 {
110 commands << QQuickContext2D::DrawImage;
111 images << image;
112 rects << sr << dr;
113 }
114
115 inline void drawPixmap(QQmlRefPointer<QQuickCanvasPixmap> pixmap, const QRectF& sr, const QRectF& dr)
116 {
117 commands << QQuickContext2D::DrawPixmap;
118 pixmaps << pixmap;
119 rects << sr << dr;
120 }
121
122 inline qreal takeShadowOffsetX() { return takeReal(); }
123 inline qreal takeShadowOffsetY() { return takeReal(); }
124 inline qreal takeShadowBlur() { return takeReal(); }
125 inline QColor takeShadowColor() { return takeColor(); }
126
127
128 inline void updateMatrix(const QTransform& matrix)
129 {
130 commands << QQuickContext2D::UpdateMatrix;
131 matrixes << matrix;
132 }
133
134 inline void clearRect(const QRectF& r)
135 {
136 commands << QQuickContext2D::ClearRect;
137 rects << r;
138 }
139
140 inline void fillRect(const QRectF& r)
141 {
142 commands << QQuickContext2D::FillRect;
143 rects << r;
144 }
145
146 inline void strokeRect(const QRectF& r)
147 {
148 QPainterPath p;
149 p.addRect(rect: r);
150
151 commands << QQuickContext2D::Stroke;
152 pathes << p;
153 }
154
155
156 inline void fill(const QPainterPath& path)
157 {
158 commands << QQuickContext2D::Fill;
159 pathes << path;
160
161 }
162
163 inline void stroke(const QPainterPath& path)
164 {
165 commands << QQuickContext2D::Stroke;
166 pathes << path;
167 }
168
169 inline void clip(bool enabled, const QPainterPath& path)
170 {
171 commands << QQuickContext2D::Clip;
172 bools << enabled;
173 pathes << path;
174 }
175
176
177
178 inline void setFillStyle(const QBrush &style, bool repeatX = false, bool repeatY = false)
179 {
180 commands << QQuickContext2D::FillStyle;
181 brushes << style;
182 bools << repeatX << repeatY;
183 }
184
185
186 inline void setLineWidth( qreal w)
187 {
188 commands << QQuickContext2D::LineWidth;
189 reals << w;
190 }
191
192 inline void setLineCap(Qt::PenCapStyle cap)
193 {
194 commands << QQuickContext2D::LineCap;
195 ints << cap;
196 }
197
198 inline void setLineJoin(Qt::PenJoinStyle join)
199 {
200 commands << QQuickContext2D::LineJoin;
201 ints << join;
202 }
203
204 inline void setLineDash(const QVector<qreal> &pattern)
205 {
206 commands << QQuickContext2D::LineDash;
207 reals << pattern.length();
208 for (qreal r : pattern)
209 reals << r;
210 }
211
212 inline void setLineDashOffset( qreal offset)
213 {
214 commands << QQuickContext2D::LineDashOffset;
215 reals << offset;
216 }
217
218 inline void setMiterLimit( qreal limit)
219 {
220 commands << QQuickContext2D::MiterLimit;
221 reals << limit;
222 }
223
224 inline void setShadowOffsetX( qreal x)
225 {
226 commands << QQuickContext2D::ShadowOffsetX;
227 reals << x;
228 }
229
230 inline void setShadowOffsetY( qreal y)
231 {
232 commands << QQuickContext2D::ShadowOffsetY;
233 reals << y;
234 }
235
236 inline void setShadowBlur( qreal b)
237 {
238 commands << QQuickContext2D::ShadowBlur;
239 reals << b;
240 }
241
242 inline void setShadowColor(const QColor &color)
243 {
244 commands << QQuickContext2D::ShadowColor;
245 colors << color;
246 }
247
248 inline QTransform takeMatrix() { return matrixes.at(i: matrixIdx++); }
249
250 inline QRectF takeRect() { return rects.at(i: rectIdx++); }
251
252 inline QPainterPath takePath() { return pathes.at(i: pathIdx++); }
253
254 inline const QImage& takeImage() { return images.at(i: imageIdx++); }
255 inline QQmlRefPointer<QQuickCanvasPixmap> takePixmap() { return pixmaps.at(i: pixmapIdx++); }
256
257 inline int takeInt() { return ints.at(i: intIdx++); }
258 inline bool takeBool() {return bools.at(i: boolIdx++); }
259 inline qreal takeReal() { return reals.at(i: realIdx++); }
260 inline QColor takeColor() { return colors.at(i: colorIdx++); }
261 inline QBrush takeBrush() { return brushes.at(i: brushIdx++); }
262
263 void replay(QPainter* painter, QQuickContext2D::State& state, const QVector2D &scaleFactor);
264
265private:
266 static QPen makePen(const QQuickContext2D::State& state);
267 void setPainterState(QPainter* painter, const QQuickContext2D::State& state, const QPen& pen);
268 int cmdIdx;
269 int intIdx;
270 int boolIdx;
271 int realIdx;
272 int rectIdx;
273 int colorIdx;
274 int matrixIdx;
275 int brushIdx;
276 int pathIdx;
277 int imageIdx;
278 int pixmapIdx;
279 QVector<QQuickContext2D::PaintCommand> commands;
280
281 QVector<int> ints;
282 QVector<bool> bools;
283 QVector<qreal> reals;
284 QVector<QRectF> rects;
285 QVector<QColor> colors;
286 QVector<QTransform> matrixes;
287 QVector<QBrush> brushes;
288 QVector<QPainterPath> pathes;
289 QVector<QImage> images;
290 QVector<QQmlRefPointer<QQuickCanvasPixmap> > pixmaps;
291 QMutex queueLock;
292};
293
294QT_END_NAMESPACE
295
296#endif // QQUICKCONTEXT2DCOMMANDBUFFER_P_H
297

source code of qtdeclarative/src/quick/items/context2d/qquickcontext2dcommandbuffer_p.h