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 QQUICKCANVASITEM_P_H
43 #define QQUICKCANVASITEM_P_H
45 #include <QtQuick/qquickitem.h>
46 #include <private/qv8engine_p.h>
47 #include <QtCore/QThread>
48 #include <QtGui/QImage>
54 class QQuickCanvasContext;
56 class QQuickCanvasItemPrivate;
60 class QQuickCanvasPixmap : public QQmlRefCount
63 QQuickCanvasPixmap(const QImage& image, QQuickWindow *window);
64 QQuickCanvasPixmap(QQuickPixmap *pixmap, QQuickWindow *window);
65 ~QQuickCanvasPixmap();
67 QSGTexture *texture();
73 QQuickPixmap *pixmap() const { return m_pixmap;}
76 QQuickPixmap *m_pixmap;
78 QSGTexture *m_texture;
79 QQuickWindow *m_window;
82 class QQuickCanvasItem : public QQuickItem
86 Q_ENUMS(RenderStrategy)
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)
103 enum RenderStrategy {
109 QQuickCanvasItem(QQuickItem *parent = 0);
112 bool isAvailable() const;
114 QString contextType() const;
115 void setContextType(const QString &contextType);
117 QQmlV8Handle context() const;
119 QSizeF canvasSize() const;
120 void setCanvasSize(const QSizeF &);
122 QSize tileSize() const;
123 void setTileSize(const QSize &);
125 QRectF canvasWindow() const;
126 void setCanvasWindow(const QRectF& rect);
128 RenderTarget renderTarget() const;
129 void setRenderTarget(RenderTarget target);
131 RenderStrategy renderStrategy() const;
132 void setRenderStrategy(RenderStrategy strategy);
134 QQuickCanvasContext *rawContext() const;
136 QImage toImage(const QRectF& rect = QRectF()) const;
138 Q_INVOKABLE void getContext(QQmlV8Function *args);
140 Q_INVOKABLE void requestAnimationFrame(QQmlV8Function *args);
141 Q_INVOKABLE void cancelRequestAnimationFrame(QQmlV8Function *args);
143 Q_INVOKABLE void requestPaint();
144 Q_INVOKABLE void markDirty(const QRectF& dirtyRect = QRectF());
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);
151 void paint(const QRect ®ion);
153 void availableChanged();
154 void contextTypeChanged();
155 void contextChanged();
156 void canvasSizeChanged();
157 void tileSizeChanged();
158 void canvasWindowChanged();
159 void renderTargetChanged();
160 void renderStrategyChanged();
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;
171 void sceneGraphInitialized();
172 void checkAnimationCallbacks();
175 void componentComplete();
176 void itemChange(QQuickItem::ItemChange, const QQuickItem::ItemChangeData &);
178 QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *);
179 void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry);
180 void releaseResources();
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();
190 class QQuickContext2DRenderThread : public QThread
194 QQuickContext2DRenderThread(QQmlEngine *eng);
195 ~QQuickContext2DRenderThread();
197 static QQuickContext2DRenderThread *instance(QQmlEngine *engine);
200 QQmlEngine *m_engine;
201 QObject *m_eventLoopQuitHack;
202 static QHash<QQmlEngine *,QQuickContext2DRenderThread*> renderThreads;
203 static QMutex renderThreadsMutex;
208 QML_DECLARE_TYPE(QQuickCanvasItem)
212 #endif //QQUICKCANVASITEM_P_H