Refactor context2d thread logic
[profile/ivi/qtdeclarative.git] / src / quick / items / context2d / qquickcontext2d_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
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.
16 **
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.
20 **
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.
28 **
29 ** Other Usage
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.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QQUICKCONTEXT2D_P_H
43 #define QQUICKCONTEXT2D_P_H
44
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>
57
58
59 //#define QQUICKCONTEXT2D_DEBUG //enable this for just DEBUG purpose!
60
61 #ifdef QQUICKCONTEXT2D_DEBUG
62 #include <QElapsedTimer>
63 #endif
64
65 QT_BEGIN_HEADER
66
67 QT_BEGIN_NAMESPACE
68
69 class QQuickContext2DCommandBuffer;
70 class QQuickContext2DTexture;
71 class QQuickPixmap;
72 class QSGTexture;
73 class QQuickWindowManager;
74 class QSurface;
75 class QOpenGLContext;
76
77 class QQuickContext2D : public QQuickCanvasContext
78 {
79 public:
80     Q_DISABLE_COPY(QQuickContext2D)
81
82     enum TextBaseLineType { Alphabetic=0, Top, Middle, Bottom, Hanging};
83     enum TextAlignType { Start=0, End, Left, Right, Center};
84     enum PaintCommand {
85         Invalid = 0,
86         UpdateMatrix,
87         ClearRect,
88         FillRect,
89         StrokeRect,
90         Fill,
91         Stroke,
92         Clip,
93         UpdateBrush,
94         GlobalAlpha,
95         GlobalCompositeOperation,
96         StrokeStyle,
97         FillStyle,
98         LineWidth,
99         LineCap,
100         LineJoin,
101         MiterLimit,
102         ShadowOffsetX,
103         ShadowOffsetY,
104         ShadowBlur,
105         ShadowColor,
106         Font,
107         TextBaseline,
108         TextAlign,
109         FillText,
110         StrokeText,
111         DrawImage,
112         DrawPixmap,
113         GetImageData
114     };
115
116     struct State {
117         State()
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)
126             , globalAlpha(1.0)
127             , lineWidth(1)
128             , lineCap(Qt::FlatCap)
129             , lineJoin(Qt::MiterJoin)
130             , miterLimit(10)
131             , shadowOffsetX(0)
132             , shadowOffsetY(0)
133             , shadowBlur(0)
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)
139         {
140         }
141
142         QTransform matrix;
143         QPainterPath clipPath;
144         QBrush strokeStyle;
145         QBrush fillStyle;
146         bool fillPatternRepeatX:1;
147         bool fillPatternRepeatY:1;
148         bool strokePatternRepeatX:1;
149         bool strokePatternRepeatY:1;
150         bool invertibleCTM:1;
151         Qt::FillRule fillRule;
152         qreal globalAlpha;
153         qreal lineWidth;
154         Qt::PenCapStyle lineCap;
155         Qt::PenJoinStyle lineJoin;
156         qreal miterLimit;
157         qreal shadowOffsetX;
158         qreal shadowOffsetY;
159         qreal shadowBlur;
160         QColor shadowColor;
161         QPainter::CompositionMode globalCompositeOperation;
162         QFont font;
163         QQuickContext2D::TextAlignType textAlign;
164         QQuickContext2D::TextBaseLineType textBaseline;
165     };
166
167     QQuickContext2D(QObject *parent = 0);
168     ~QQuickContext2D();
169
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);
173     void flush();
174     void sync();
175     QThread *thread() const {return m_thread;}
176     QSGDynamicTexture *texture() const;
177     QImage toImage(const QRectF& bounds);
178
179     v8::Handle<v8::Object> v8value() const;
180     void setV8Engine(QV8Engine *eng);
181
182     QQuickCanvasItem* canvas() const { return m_canvas; }
183     QQuickContext2DCommandBuffer* buffer() const { return m_buffer; }
184     QQuickContext2DCommandBuffer* nextBuffer();
185
186     bool bufferValid() const { return m_buffer != 0; }
187     void popState();
188     void pushState();
189     void reset();
190
191     void fill();
192     void clip();
193     void stroke();
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);
198
199     //Transform APIs
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);
206
207     // Path APIs
208     void beginPath();
209     void closePath();
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,
222              bool anticlockwise);
223     void addArcTo(const QPointF& p1, const QPointF& p2, float radius);
224
225     bool isPointInPath(qreal x, qreal y) const;
226
227     QPainterPath createTextGlyphs(qreal x, qreal y, const QString& text);
228     QQmlRefPointer<QQuickCanvasPixmap> createPixmap(const QUrl& url);
229
230     QOpenGLContext *glContext() { return m_glContext; }
231     QSurface *surface() { return m_surface; }
232     void setGrabbedImage(const QImage& grab);
233
234     State state;
235     QStack<QQuickContext2D::State> m_stateStack;
236     QQuickCanvasItem* m_canvas;
237     QQuickContext2DCommandBuffer* m_buffer;
238     QPainterPath m_path;
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;
244     QSurface *m_surface;
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;
251     QThread *m_thread;
252     QImage m_grabbedImage;
253     bool m_grabbed:1;
254
255     QMutex m_mutex;
256 };
257
258
259 QT_END_NAMESPACE
260 QML_DECLARE_TYPE(QQuickContext2D)
261
262 QT_END_HEADER
263
264 #endif // QQUICKCONTEXT2D_P_H