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 QQUICKCONTEXT2D_P_H |
5 | #define QQUICKCONTEXT2D_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 | |
20 | QT_REQUIRE_CONFIG(quick_canvas); |
21 | |
22 | #include <QtQml/qqml.h> |
23 | #include <QtQml/qqmlcomponent.h> |
24 | #include <private/qquickcanvascontext_p.h> |
25 | #include <private/qquickcanvasitem_p.h> |
26 | #include <QtGui/qpainter.h> |
27 | #include <QtGui/qpainterpath.h> |
28 | #include <QtGui/qoffscreensurface.h> |
29 | #include <QtCore/qstring.h> |
30 | #include <QtCore/qstack.h> |
31 | #include <QtCore/qqueue.h> |
32 | #include <QtCore/QWaitCondition> |
33 | |
34 | #include <private/qv4persistent_p.h> |
35 | |
36 | //#define QQUICKCONTEXT2D_DEBUG //enable this for just DEBUG purpose! |
37 | |
38 | #ifdef QQUICKCONTEXT2D_DEBUG |
39 | #include <QElapsedTimer> |
40 | #endif |
41 | |
42 | QT_BEGIN_NAMESPACE |
43 | |
44 | namespace QV4 { |
45 | struct ExecutionEngine; |
46 | } |
47 | |
48 | class QQuickContext2DCommandBuffer; |
49 | class QQuickContext2DTexture; |
50 | class QQuickPixmap; |
51 | class QSGTexture; |
52 | class QSurface; |
53 | |
54 | class QQuickContext2D : public QQuickCanvasContext |
55 | { |
56 | Q_OBJECT |
57 | |
58 | public: |
59 | Q_DISABLE_COPY(QQuickContext2D) |
60 | |
61 | enum TextBaseLineType { Alphabetic=0, Top, Middle, Bottom, Hanging}; |
62 | enum TextAlignType { Start=0, End, Left, Right, Center}; |
63 | enum PaintCommand { |
64 | Invalid = 0, |
65 | UpdateMatrix, |
66 | ClearRect, |
67 | FillRect, |
68 | StrokeRect, |
69 | Fill, |
70 | Stroke, |
71 | Clip, |
72 | UpdateBrush, |
73 | GlobalAlpha, |
74 | GlobalCompositeOperation, |
75 | StrokeStyle, |
76 | FillStyle, |
77 | LineWidth, |
78 | LineCap, |
79 | LineJoin, |
80 | LineDash, |
81 | LineDashOffset, |
82 | MiterLimit, |
83 | ShadowOffsetX, |
84 | ShadowOffsetY, |
85 | ShadowBlur, |
86 | ShadowColor, |
87 | Font, |
88 | TextBaseline, |
89 | TextAlign, |
90 | FillText, |
91 | StrokeText, |
92 | DrawImage, |
93 | DrawPixmap, |
94 | GetImageData |
95 | }; |
96 | |
97 | struct State { |
98 | State() |
99 | : strokeStyle(QColor(Qt::black)) |
100 | , fillStyle(QColor(Qt::black)) |
101 | , fillPatternRepeatX(false) |
102 | , fillPatternRepeatY(false) |
103 | , strokePatternRepeatX(false) |
104 | , strokePatternRepeatY(false) |
105 | , invertibleCTM(true) |
106 | , clip(false) |
107 | , fillRule(Qt::WindingFill) |
108 | , globalAlpha(1.0) |
109 | , lineWidth(1) |
110 | , lineCap(Qt::FlatCap) |
111 | , lineJoin(Qt::MiterJoin) |
112 | , lineDashOffset(0) |
113 | , miterLimit(10) |
114 | , shadowOffsetX(0) |
115 | , shadowOffsetY(0) |
116 | , shadowBlur(0) |
117 | , shadowColor(qRgba(r: 0, g: 0, b: 0, a: 0)) |
118 | , globalCompositeOperation(QPainter::CompositionMode_SourceOver) |
119 | , font(QFont(QLatin1String("sans-serif" ))) |
120 | , textAlign(QQuickContext2D::Start) |
121 | , textBaseline(QQuickContext2D::Alphabetic) |
122 | { |
123 | font.setPixelSize(10); |
124 | } |
125 | |
126 | QTransform matrix; |
127 | QPainterPath clipPath; |
128 | QBrush strokeStyle; |
129 | QBrush fillStyle; |
130 | bool fillPatternRepeatX:1; |
131 | bool fillPatternRepeatY:1; |
132 | bool strokePatternRepeatX:1; |
133 | bool strokePatternRepeatY:1; |
134 | bool invertibleCTM:1; |
135 | bool clip:1; |
136 | Qt::FillRule fillRule; |
137 | qreal globalAlpha; |
138 | qreal lineWidth; |
139 | Qt::PenCapStyle lineCap; |
140 | Qt::PenJoinStyle lineJoin; |
141 | QVector<qreal> lineDash; |
142 | qreal lineDashOffset; |
143 | qreal miterLimit; |
144 | qreal shadowOffsetX; |
145 | qreal shadowOffsetY; |
146 | qreal shadowBlur; |
147 | QColor shadowColor; |
148 | QPainter::CompositionMode globalCompositeOperation; |
149 | QFont font; |
150 | QQuickContext2D::TextAlignType textAlign; |
151 | QQuickContext2D::TextBaseLineType textBaseline; |
152 | }; |
153 | |
154 | QQuickContext2D(QObject *parent = nullptr); |
155 | ~QQuickContext2D(); |
156 | |
157 | QStringList contextNames() const override; |
158 | void init(QQuickCanvasItem *canvasItem, const QVariantMap &args) override; |
159 | void prepare(const QSize& canvasSize, const QSize& tileSize, const QRect& canvasWindow, const QRect& dirtyRect, bool smooth, bool antialiasing) override; |
160 | void flush() override; |
161 | void sync(); |
162 | QThread *thread() const { return m_thread; } |
163 | QQuickContext2DTexture *texture() const; |
164 | QImage toImage(const QRectF& bounds) override; |
165 | |
166 | QV4::ReturnedValue v4value() const override; |
167 | QV4::ExecutionEngine *v4Engine() const override; |
168 | void setV4Engine(QV4::ExecutionEngine *eng) override; |
169 | |
170 | QQuickCanvasItem* canvas() const { return m_canvas; } |
171 | QQuickContext2DCommandBuffer* buffer() const { return m_buffer; } |
172 | |
173 | bool bufferValid() const { return m_buffer != nullptr; } |
174 | void popState(); |
175 | void pushState(); |
176 | void reset(); |
177 | |
178 | void fill(); |
179 | void clip(); |
180 | void stroke(); |
181 | void fillRect(qreal x, qreal y, qreal w, qreal h); |
182 | void strokeRect(qreal x, qreal y, qreal w, qreal h); |
183 | void clearRect(qreal x, qreal y, qreal w, qreal h); |
184 | void drawText(const QString& text, qreal x, qreal y, bool fill); |
185 | |
186 | //Transform APIs |
187 | void scale(qreal x, qreal y); |
188 | void rotate(qreal angle); |
189 | void shear(qreal h, qreal v); |
190 | void translate(qreal x, qreal y); |
191 | void transform(qreal a, qreal b, qreal c, qreal d, qreal e, qreal f); |
192 | void setTransform(qreal a, qreal b, qreal c, qreal d, qreal e, qreal f); |
193 | |
194 | // Path APIs |
195 | void beginPath(); |
196 | void closePath(); |
197 | void moveTo(qreal x, qreal y); |
198 | void lineTo(qreal x, qreal y); |
199 | void quadraticCurveTo(qreal cpx, qreal cpy, qreal x, qreal y); |
200 | void bezierCurveTo(qreal cp1x, qreal cp1y, |
201 | qreal cp2x, qreal cp2y, qreal x, qreal y); |
202 | void arcTo(qreal x1, qreal y1, qreal x2, qreal y2, qreal radius); |
203 | void rect(qreal x, qreal y, qreal w, qreal h); |
204 | void roundedRect(qreal x, qreal y,qreal w, qreal h, qreal xr, qreal yr); |
205 | void ellipse(qreal x, qreal y,qreal w, qreal h); |
206 | void text(const QString& str, qreal x, qreal y); |
207 | void arc(qreal x, qreal y, qreal radius, |
208 | qreal startAngle, qreal endAngle, |
209 | bool anticlockwise); |
210 | void addArcTo(const QPointF& p1, const QPointF& p2, qreal radius); |
211 | |
212 | bool isPointInPath(qreal x, qreal y) const; |
213 | |
214 | QPainterPath createTextGlyphs(qreal x, qreal y, const QString& text); |
215 | QQmlRefPointer<QQuickCanvasPixmap> createPixmap(const QUrl& url); |
216 | |
217 | QSurface *surface() const { return m_surface.data(); } |
218 | void setGrabbedImage(const QImage& grab); |
219 | |
220 | State state; |
221 | QStack<QQuickContext2D::State> m_stateStack; |
222 | QQuickCanvasItem* m_canvas; |
223 | QQuickContext2DCommandBuffer* m_buffer; |
224 | QPainterPath m_path; |
225 | QV4::PersistentValue m_fillStyle; |
226 | QV4::PersistentValue m_strokeStyle; |
227 | QV4::PersistentValue m_v4path; |
228 | QV4::ExecutionEngine *m_v4engine; |
229 | QScopedPointer<QOffscreenSurface> m_surface; |
230 | QV4::PersistentValue m_v4value; |
231 | QQuickContext2DTexture *m_texture; |
232 | QQuickCanvasItem::RenderTarget m_renderTarget; |
233 | QQuickCanvasItem::RenderStrategy m_renderStrategy; |
234 | QQueue<QQuickContext2DCommandBuffer*> m_bufferQueue; |
235 | QThread *m_thread; |
236 | QImage m_grabbedImage; |
237 | bool m_grabbed:1; |
238 | |
239 | static QMutex mutex; |
240 | }; |
241 | |
242 | |
243 | QT_END_NAMESPACE |
244 | QML_DECLARE_TYPE(QQuickContext2D) |
245 | |
246 | #endif // QQUICKCONTEXT2D_P_H |
247 | |