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