090c763e465f690b0986a9214da4c1a96d4af262
[profile/ivi/qtdeclarative.git] / src / quick / items / context2d / qquickcanvasitem_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 QQUICKCANVASITEM_P_H
43 #define QQUICKCANVASITEM_P_H
44
45 #include <QtQuick/qquickitem.h>
46 #include <private/qv8engine_p.h>
47 #include <QtCore/QThread>
48 #include <QtGui/QImage>
49
50 QT_BEGIN_HEADER
51
52 QT_BEGIN_NAMESPACE
53
54 class QQuickCanvasContext;
55
56 class QQuickCanvasItemPrivate;
57 class QSGTexture;
58 class QQuickPixmap;
59
60 class QQuickCanvasPixmap : public QQmlRefCount
61 {
62 public:
63     QQuickCanvasPixmap(const QImage& image, QQuickCanvas *canvas);
64     QQuickCanvasPixmap(QQuickPixmap *pixmap, QQuickCanvas *canvas);
65     ~QQuickCanvasPixmap();
66
67     QSGTexture *texture();
68     QImage image();
69
70     qreal width() const;
71     qreal height() const;
72     bool isValid() const;
73     QQuickPixmap *pixmap() const { return m_pixmap;}
74
75 private:
76     QQuickPixmap *m_pixmap;
77     QImage m_image;
78     QSGTexture *m_texture;
79     QQuickCanvas *m_canvas;
80 };
81
82 class QQuickCanvasItem : public QQuickItem
83 {
84     Q_OBJECT
85     Q_ENUMS(RenderTarget)
86     Q_ENUMS(RenderStrategy)
87
88     Q_PROPERTY(bool available READ isAvailable NOTIFY availableChanged)
89     Q_PROPERTY(QString contextType READ contextType WRITE setContextType NOTIFY contextTypeChanged)
90     Q_PROPERTY(QQmlV8Handle context READ context NOTIFY contextChanged)
91     Q_PROPERTY(QSizeF canvasSize READ canvasSize WRITE setCanvasSize NOTIFY canvasSizeChanged)
92     Q_PROPERTY(QSize tileSize READ tileSize WRITE setTileSize NOTIFY tileSizeChanged)
93     Q_PROPERTY(QRectF canvasWindow READ canvasWindow WRITE setCanvasWindow NOTIFY canvasWindowChanged)
94     Q_PROPERTY(RenderTarget renderTarget READ renderTarget WRITE setRenderTarget NOTIFY renderTargetChanged)
95     Q_PROPERTY(RenderStrategy renderStrategy READ renderStrategy WRITE setRenderStrategy NOTIFY renderStrategyChanged)
96
97 public:
98     enum RenderTarget {
99         Image,
100         FramebufferObject
101     };
102
103     enum RenderStrategy {
104         Immediate,
105         Threaded,
106         Cooperative
107     };
108
109     QQuickCanvasItem(QQuickItem *parent = 0);
110     ~QQuickCanvasItem();
111
112     bool isAvailable() const;
113
114     QString contextType() const;
115     void setContextType(const QString &contextType);
116
117     QQmlV8Handle context() const;
118
119     QSizeF canvasSize() const;
120     void setCanvasSize(const QSizeF &);
121
122     QSize tileSize() const;
123     void setTileSize(const QSize &);
124
125     QRectF canvasWindow() const;
126     void setCanvasWindow(const QRectF& rect);
127
128     RenderTarget renderTarget() const;
129     void setRenderTarget(RenderTarget target);
130
131     RenderStrategy renderStrategy() const;
132     void setRenderStrategy(RenderStrategy strategy);
133
134     QQuickCanvasContext *rawContext() const;
135
136     QImage toImage(const QRectF& rect = QRectF()) const;
137
138     Q_INVOKABLE void getContext(QQmlV8Function *args);
139
140     Q_INVOKABLE void requestAnimationFrame(QQmlV8Function *args);
141     Q_INVOKABLE void cancelRequestAnimationFrame(QQmlV8Function *args);
142
143     Q_INVOKABLE void requestPaint();
144     Q_INVOKABLE void markDirty(const QRectF& dirtyRect = QRectF());
145
146     Q_INVOKABLE bool save(const QString &filename) const;
147     Q_INVOKABLE QString toDataURL(const QString& type = QLatin1String("image/png")) const;
148     QQmlRefPointer<QQuickCanvasPixmap> loadedPixmap(const QUrl& url);
149
150 Q_SIGNALS:
151     void paint(const QRect &region);
152     void painted();
153     void availableChanged();
154     void contextTypeChanged();
155     void contextChanged();
156     void canvasSizeChanged();
157     void tileSizeChanged();
158     void canvasWindowChanged();
159     void renderTargetChanged();
160     void renderStrategyChanged();
161     void imageLoaded();
162
163 public Q_SLOTS:
164     void loadImage(const QUrl& url);
165     void unloadImage(const QUrl& url);
166     bool isImageLoaded(const QUrl& url) const;
167     bool isImageLoading(const QUrl& url) const;
168     bool isImageError(const QUrl& url) const;
169
170 private Q_SLOTS:
171     void sceneGraphInitialized();
172     void checkAnimationCallbacks();
173
174 protected:
175     void componentComplete();
176     void itemChange(QQuickItem::ItemChange, const QQuickItem::ItemChangeData &);
177     void updatePolish();
178     QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *);
179     void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
180
181 private:
182     Q_DECLARE_PRIVATE(QQuickCanvasItem)
183     Q_INVOKABLE void delayedCreate();
184     bool createContext(const QString &contextType);
185     void initializeContext(QQuickCanvasContext *context, const QVariantMap &args = QVariantMap());
186     QRect tiledRect(const QRectF &window, const QSize &tileSize);
187     bool isPaintConnected();
188 };
189
190 class QQuickContext2DRenderThread : public QThread
191 {
192     Q_OBJECT
193 public:
194     QQuickContext2DRenderThread(QQmlEngine *eng);
195     ~QQuickContext2DRenderThread();
196
197     static QQuickContext2DRenderThread *instance(QQmlEngine *engine);
198
199 private:
200     QQmlEngine *m_engine;
201     QObject *m_eventLoopQuitHack;
202     static QHash<QQmlEngine *,QQuickContext2DRenderThread*> renderThreads;
203     static QMutex renderThreadsMutex;
204 };
205
206 QT_END_NAMESPACE
207
208 QML_DECLARE_TYPE(QQuickCanvasItem)
209
210 QT_END_HEADER
211
212 #endif //QQUICKCANVASITEM_P_H