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

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