1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QQUICKCONTEXT2D_P_H
43 #define QQUICKCONTEXT2D_P_H
45 #include <QtQuick/qtquickglobal.h>
46 #include <QtQml/qqml.h>
47 #include <QtQml/qqmlcomponent.h>
48 #include <private/qquickcanvascontext_p.h>
49 #include <private/qquickcanvasitem_p.h>
50 #include <QtGui/qpainter.h>
51 #include <QtGui/qpainterpath.h>
52 #include <QtCore/qstring.h>
53 #include <QtCore/qstack.h>
54 #include <QtCore/qqueue.h>
55 #include <private/qv8engine_p.h>
56 #include <QtCore/QWaitCondition>
59 //#define QQUICKCONTEXT2D_DEBUG //enable this for just DEBUG purpose!
61 #ifdef QQUICKCONTEXT2D_DEBUG
62 #include <QElapsedTimer>
69 class QQuickContext2DCommandBuffer;
70 class QQuickContext2DTexture;
73 class QQuickWindowManager;
77 class QQuickContext2D : public QQuickCanvasContext
80 Q_DISABLE_COPY(QQuickContext2D)
82 enum TextBaseLineType { Alphabetic=0, Top, Middle, Bottom, Hanging};
83 enum TextAlignType { Start=0, End, Left, Right, Center};
95 GlobalCompositeOperation,
118 : strokeStyle(QColor("#000000"))
119 , fillStyle(QColor("#000000"))
120 , fillPatternRepeatX(false)
121 , fillPatternRepeatY(false)
122 , strokePatternRepeatX(false)
123 , strokePatternRepeatY(false)
124 , invertibleCTM(true)
125 , fillRule(Qt::WindingFill)
128 , lineCap(Qt::FlatCap)
129 , lineJoin(Qt::MiterJoin)
134 , shadowColor(qRgba(0, 0, 0, 0))
135 , globalCompositeOperation(QPainter::CompositionMode_SourceOver)
136 , font(QFont(QLatin1String("sans-serif"), 10))
137 , textAlign(QQuickContext2D::Start)
138 , textBaseline(QQuickContext2D::Alphabetic)
143 QPainterPath clipPath;
146 bool fillPatternRepeatX:1;
147 bool fillPatternRepeatY:1;
148 bool strokePatternRepeatX:1;
149 bool strokePatternRepeatY:1;
150 bool invertibleCTM:1;
151 Qt::FillRule fillRule;
154 Qt::PenCapStyle lineCap;
155 Qt::PenJoinStyle lineJoin;
161 QPainter::CompositionMode globalCompositeOperation;
163 QQuickContext2D::TextAlignType textAlign;
164 QQuickContext2D::TextBaseLineType textBaseline;
167 QQuickContext2D(QObject *parent = 0);
170 QStringList contextNames() const;
171 void init(QQuickCanvasItem *canvasItem, const QVariantMap &args);
172 void prepare(const QSize& canvasSize, const QSize& tileSize, const QRect& canvasWindow, const QRect& dirtyRect, bool smooth);
175 QThread *thread() const {return m_thread;}
176 QSGDynamicTexture *texture() const;
177 QImage toImage(const QRectF& bounds);
179 v8::Handle<v8::Object> v8value() const;
180 void setV8Engine(QV8Engine *eng);
182 QQuickCanvasItem* canvas() const { return m_canvas; }
183 QQuickContext2DCommandBuffer* buffer() const { return m_buffer; }
184 QQuickContext2DCommandBuffer* nextBuffer();
186 bool bufferValid() const { return m_buffer != 0; }
194 void fillRect(qreal x, qreal y, qreal w, qreal h);
195 void strokeRect(qreal x, qreal y, qreal w, qreal h);
196 void clearRect(qreal x, qreal y, qreal w, qreal h);
197 void drawText(const QString& text, qreal x, qreal y, bool fill);
200 void scale(qreal x, qreal y);
201 void rotate(qreal angle);
202 void shear(qreal h, qreal v);
203 void translate(qreal x, qreal y);
204 void transform(qreal a, qreal b, qreal c, qreal d, qreal e, qreal f);
205 void setTransform(qreal a, qreal b, qreal c, qreal d, qreal e, qreal f);
210 void moveTo(qreal x, qreal y);
211 void lineTo(qreal x, qreal y);
212 void quadraticCurveTo(qreal cpx, qreal cpy, qreal x, qreal y);
213 void bezierCurveTo(qreal cp1x, qreal cp1y,
214 qreal cp2x, qreal cp2y, qreal x, qreal y);
215 void arcTo(qreal x1, qreal y1, qreal x2, qreal y2, qreal radius);
216 void rect(qreal x, qreal y, qreal w, qreal h);
217 void roundedRect(qreal x, qreal y,qreal w, qreal h, qreal xr, qreal yr);
218 void ellipse(qreal x, qreal y,qreal w, qreal h);
219 void text(const QString& str, qreal x, qreal y);
220 void arc(qreal x, qreal y, qreal radius,
221 qreal startAngle, qreal endAngle,
223 void addArcTo(const QPointF& p1, const QPointF& p2, float radius);
225 bool isPointInPath(qreal x, qreal y) const;
227 QPainterPath createTextGlyphs(qreal x, qreal y, const QString& text);
228 QQmlRefPointer<QQuickCanvasPixmap> createPixmap(const QUrl& url);
230 QOpenGLContext *glContext() { return m_glContext; }
231 QSurface *surface() { return m_surface; }
232 void setGrabbedImage(const QImage& grab);
235 QStack<QQuickContext2D::State> m_stateStack;
236 QQuickCanvasItem* m_canvas;
237 QQuickContext2DCommandBuffer* m_buffer;
239 v8::Local<v8::Value> m_fillStyle;
240 v8::Local<v8::Value> m_strokeStyle;
241 v8::Handle<v8::Value> m_v8path;
242 QV8Engine *m_v8engine;
243 QQuickWindowManager *m_windowManager;
245 QOpenGLContext *m_glContext;
246 v8::Persistent<v8::Object> m_v8value;
247 QQuickContext2DTexture *m_texture;
248 QQuickCanvasItem::RenderTarget m_renderTarget;
249 QQuickCanvasItem::RenderStrategy m_renderStrategy;
250 QQueue<QQuickContext2DCommandBuffer*> m_bufferQueue;
252 QImage m_grabbedImage;
260 QML_DECLARE_TYPE(QQuickContext2D)
264 #endif // QQUICKCONTEXT2D_P_H