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>
59 //#define QQUICKCONTEXT2D_DEBUG //enable this for just DEBUG purpose!
61 #ifdef QQUICKCONTEXT2D_DEBUG
62 #include <QElapsedTimer>
69 class QQuickContext2DCommandBuffer;
70 class QQuickContext2DTexture;
75 class QQuickContext2D : public QQuickCanvasContext
78 Q_DISABLE_COPY(QQuickContext2D)
80 enum TextBaseLineType { Alphabetic=0, Top, Middle, Bottom, Hanging};
81 enum TextAlignType { Start=0, End, Left, Right, Center};
93 GlobalCompositeOperation,
115 : strokeStyle(QColor("#000000"))
116 , fillStyle(QColor("#000000"))
117 , fillPatternRepeatX(false)
118 , fillPatternRepeatY(false)
119 , strokePatternRepeatX(false)
120 , strokePatternRepeatY(false)
121 , invertibleCTM(true)
122 , fillRule(Qt::WindingFill)
125 , lineCap(Qt::FlatCap)
126 , lineJoin(Qt::MiterJoin)
131 , shadowColor(qRgba(0, 0, 0, 0))
132 , globalCompositeOperation(QPainter::CompositionMode_SourceOver)
133 , font(QFont(QLatin1String("sans-serif"), 10))
134 , textAlign(QQuickContext2D::Start)
135 , textBaseline(QQuickContext2D::Alphabetic)
140 QPainterPath clipPath;
143 bool fillPatternRepeatX:1;
144 bool fillPatternRepeatY:1;
145 bool strokePatternRepeatX:1;
146 bool strokePatternRepeatY:1;
147 bool invertibleCTM:1;
148 Qt::FillRule fillRule;
151 Qt::PenCapStyle lineCap;
152 Qt::PenJoinStyle lineJoin;
158 QPainter::CompositionMode globalCompositeOperation;
160 QQuickContext2D::TextAlignType textAlign;
161 QQuickContext2D::TextBaseLineType textBaseline;
164 QQuickContext2D(QObject *parent = 0);
167 QStringList contextNames() const;
168 void init(QQuickCanvasItem *canvasItem, const QVariantMap &args);
169 void prepare(const QSize& canvasSize, const QSize& tileSize, const QRect& canvasWindow, const QRect& dirtyRect, bool smooth);
172 QSGDynamicTexture *texture() const;
173 QImage toImage(const QRectF& bounds);
175 v8::Handle<v8::Object> v8value() const;
176 void setV8Engine(QV8Engine *eng);
178 QQuickCanvasItem* canvas() const { return m_canvas; }
179 QQuickContext2DCommandBuffer* buffer() const { return m_buffer; }
180 QQuickContext2DCommandBuffer* nextBuffer();
182 bool bufferValid() const { return m_buffer != 0; }
190 void fillRect(qreal x, qreal y, qreal w, qreal h);
191 void strokeRect(qreal x, qreal y, qreal w, qreal h);
192 void clearRect(qreal x, qreal y, qreal w, qreal h);
193 void drawText(const QString& text, qreal x, qreal y, bool fill);
196 void scale(qreal x, qreal y);
197 void rotate(qreal angle);
198 void shear(qreal h, qreal v);
199 void translate(qreal x, qreal y);
200 void transform(qreal a, qreal b, qreal c, qreal d, qreal e, qreal f);
201 void setTransform(qreal a, qreal b, qreal c, qreal d, qreal e, qreal f);
206 void moveTo(qreal x, qreal y);
207 void lineTo(qreal x, qreal y);
208 void quadraticCurveTo(qreal cpx, qreal cpy, qreal x, qreal y);
209 void bezierCurveTo(qreal cp1x, qreal cp1y,
210 qreal cp2x, qreal cp2y, qreal x, qreal y);
211 void arcTo(qreal x1, qreal y1, qreal x2, qreal y2, qreal radius);
212 void rect(qreal x, qreal y, qreal w, qreal h);
213 void roundedRect(qreal x, qreal y,qreal w, qreal h, qreal xr, qreal yr);
214 void ellipse(qreal x, qreal y,qreal w, qreal h);
215 void text(const QString& str, qreal x, qreal y);
216 void arc(qreal x, qreal y, qreal radius,
217 qreal startAngle, qreal endAngle,
219 void addArcTo(const QPointF& p1, const QPointF& p2, float radius);
221 bool isPointInPath(qreal x, qreal y) const;
223 QPainterPath createTextGlyphs(qreal x, qreal y, const QString& text);
224 QImage createImage(const QUrl& url);
227 QStack<QQuickContext2D::State> m_stateStack;
228 QQuickCanvasItem* m_canvas;
229 QQuickContext2DCommandBuffer* m_buffer;
231 v8::Local<v8::Value> m_fillStyle;
232 v8::Local<v8::Value> m_strokeStyle;
233 v8::Handle<v8::Value> m_v8path;
234 QV8Engine *m_v8engine;
235 v8::Persistent<v8::Object> m_v8value;
236 QQuickContext2DTexture *m_texture;
237 QQuickCanvasItem::RenderTarget m_renderTarget;
238 QQuickCanvasItem::RenderStrategy m_renderStrategy;
239 QQueue<QQuickContext2DCommandBuffer*> m_bufferQueue;
240 QMutex m_bufferMutex;
245 QML_DECLARE_TYPE(QQuickContext2D)
249 #endif // QQUICKCONTEXT2D_P_H