| 1 | /**************************************************************************** |
| 2 | ** |
| 3 | ** Copyright (C) 2016 The Qt Company Ltd. |
| 4 | ** Contact: https://www.qt.io/licensing/ |
| 5 | ** |
| 6 | ** This file is part of the QtQuick module of the Qt Toolkit. |
| 7 | ** |
| 8 | ** $QT_BEGIN_LICENSE:LGPL$ |
| 9 | ** Commercial License Usage |
| 10 | ** Licensees holding valid commercial Qt licenses may use this file in |
| 11 | ** accordance with the commercial license agreement provided with the |
| 12 | ** Software or, alternatively, in accordance with the terms contained in |
| 13 | ** a written agreement between you and The Qt Company. For licensing terms |
| 14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
| 15 | ** information use the contact form at https://www.qt.io/contact-us. |
| 16 | ** |
| 17 | ** GNU Lesser General Public License Usage |
| 18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
| 19 | ** General Public License version 3 as published by the Free Software |
| 20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
| 21 | ** packaging of this file. Please review the following information to |
| 22 | ** ensure the GNU Lesser General Public License version 3 requirements |
| 23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
| 24 | ** |
| 25 | ** GNU General Public License Usage |
| 26 | ** Alternatively, this file may be used under the terms of the GNU |
| 27 | ** General Public License version 2.0 or (at your option) the GNU General |
| 28 | ** Public license version 3 or any later version approved by the KDE Free |
| 29 | ** Qt Foundation. The licenses are as published by the Free Software |
| 30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
| 31 | ** included in the packaging of this file. Please review the following |
| 32 | ** information to ensure the GNU General Public License requirements will |
| 33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
| 34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
| 35 | ** |
| 36 | ** $QT_END_LICENSE$ |
| 37 | ** |
| 38 | ****************************************************************************/ |
| 39 | |
| 40 | #ifndef QQUICKCANVASITEM_P_H |
| 41 | #define QQUICKCANVASITEM_P_H |
| 42 | |
| 43 | // |
| 44 | // W A R N I N G |
| 45 | // ------------- |
| 46 | // |
| 47 | // This file is not part of the Qt API. It exists purely as an |
| 48 | // implementation detail. This header file may change from version to |
| 49 | // version without notice, or even be removed. |
| 50 | // |
| 51 | // We mean it. |
| 52 | // |
| 53 | |
| 54 | #include <private/qtquickglobal_p.h> |
| 55 | |
| 56 | QT_REQUIRE_CONFIG(quick_canvas); |
| 57 | |
| 58 | #include <QtQuick/qquickitem.h> |
| 59 | #include <private/qqmlrefcount_p.h> |
| 60 | #include <QtCore/QThread> |
| 61 | #include <QtCore/qmutex.h> |
| 62 | #include <QtGui/QImage> |
| 63 | |
| 64 | QT_BEGIN_NAMESPACE |
| 65 | |
| 66 | class QQuickCanvasContext; |
| 67 | |
| 68 | class QQuickCanvasItemPrivate; |
| 69 | class QQuickPixmap; |
| 70 | |
| 71 | class QQuickCanvasPixmap : public QQmlRefCount |
| 72 | { |
| 73 | public: |
| 74 | QQuickCanvasPixmap(const QImage& image); |
| 75 | QQuickCanvasPixmap(QQuickPixmap *pixmap); |
| 76 | ~QQuickCanvasPixmap(); |
| 77 | |
| 78 | QImage image(); |
| 79 | |
| 80 | qreal width() const; |
| 81 | qreal height() const; |
| 82 | bool isValid() const; |
| 83 | QQuickPixmap *pixmap() const { return m_pixmap;} |
| 84 | |
| 85 | private: |
| 86 | QQuickPixmap *m_pixmap; |
| 87 | QImage m_image; |
| 88 | }; |
| 89 | |
| 90 | class QQuickCanvasItem : public QQuickItem |
| 91 | { |
| 92 | Q_OBJECT |
| 93 | |
| 94 | Q_PROPERTY(bool available READ isAvailable NOTIFY availableChanged) |
| 95 | Q_PROPERTY(QString contextType READ contextType WRITE setContextType NOTIFY contextTypeChanged) |
| 96 | Q_PROPERTY(QJSValue context READ context NOTIFY contextChanged) |
| 97 | Q_PROPERTY(QSizeF canvasSize READ canvasSize WRITE setCanvasSize NOTIFY canvasSizeChanged) |
| 98 | Q_PROPERTY(QSize tileSize READ tileSize WRITE setTileSize NOTIFY tileSizeChanged) |
| 99 | Q_PROPERTY(QRectF canvasWindow READ canvasWindow WRITE setCanvasWindow NOTIFY canvasWindowChanged) |
| 100 | Q_PROPERTY(RenderTarget renderTarget READ renderTarget WRITE setRenderTarget NOTIFY renderTargetChanged) |
| 101 | Q_PROPERTY(RenderStrategy renderStrategy READ renderStrategy WRITE setRenderStrategy NOTIFY renderStrategyChanged) |
| 102 | QML_NAMED_ELEMENT(Canvas) |
| 103 | |
| 104 | public: |
| 105 | enum RenderTarget { |
| 106 | Image, |
| 107 | FramebufferObject |
| 108 | }; |
| 109 | Q_ENUM(RenderTarget) |
| 110 | |
| 111 | enum RenderStrategy { |
| 112 | Immediate, |
| 113 | Threaded, |
| 114 | Cooperative |
| 115 | }; |
| 116 | Q_ENUM(RenderStrategy) |
| 117 | |
| 118 | QQuickCanvasItem(QQuickItem *parent = nullptr); |
| 119 | ~QQuickCanvasItem(); |
| 120 | |
| 121 | bool isAvailable() const; |
| 122 | |
| 123 | QString contextType() const; |
| 124 | void setContextType(const QString &contextType); |
| 125 | |
| 126 | QJSValue context() const; |
| 127 | |
| 128 | QSizeF canvasSize() const; |
| 129 | void setCanvasSize(const QSizeF &); |
| 130 | |
| 131 | QSize tileSize() const; |
| 132 | void setTileSize(const QSize &); |
| 133 | |
| 134 | QRectF canvasWindow() const; |
| 135 | void setCanvasWindow(const QRectF& rect); |
| 136 | |
| 137 | RenderTarget renderTarget() const; |
| 138 | void setRenderTarget(RenderTarget target); |
| 139 | |
| 140 | RenderStrategy renderStrategy() const; |
| 141 | void setRenderStrategy(RenderStrategy strategy); |
| 142 | |
| 143 | QQuickCanvasContext *rawContext() const; |
| 144 | |
| 145 | QImage toImage(const QRectF& rect = QRectF()) const; |
| 146 | |
| 147 | Q_INVOKABLE void getContext(QQmlV4Function *args); |
| 148 | |
| 149 | Q_INVOKABLE void requestAnimationFrame(QQmlV4Function *args); |
| 150 | Q_INVOKABLE void cancelRequestAnimationFrame(QQmlV4Function *args); |
| 151 | |
| 152 | Q_INVOKABLE void requestPaint(); |
| 153 | Q_INVOKABLE void markDirty(const QRectF& dirtyRect = QRectF()); |
| 154 | |
| 155 | Q_INVOKABLE bool save(const QString &filename) const; |
| 156 | Q_INVOKABLE QString toDataURL(const QString& type = QLatin1String("image/png" )) const; |
| 157 | QQmlRefPointer<QQuickCanvasPixmap> loadedPixmap(const QUrl& url); |
| 158 | |
| 159 | bool isTextureProvider() const override; |
| 160 | QSGTextureProvider *textureProvider() const override; |
| 161 | |
| 162 | Q_SIGNALS: |
| 163 | void paint(const QRect ®ion); |
| 164 | void painted(); |
| 165 | void availableChanged(); |
| 166 | void contextTypeChanged(); |
| 167 | void contextChanged(); |
| 168 | void canvasSizeChanged(); |
| 169 | void tileSizeChanged(); |
| 170 | void canvasWindowChanged(); |
| 171 | void renderTargetChanged(); |
| 172 | void renderStrategyChanged(); |
| 173 | void imageLoaded(); |
| 174 | |
| 175 | public Q_SLOTS: |
| 176 | void loadImage(const QUrl& url); |
| 177 | void unloadImage(const QUrl& url); |
| 178 | bool isImageLoaded(const QUrl& url) const; |
| 179 | bool isImageLoading(const QUrl& url) const; |
| 180 | bool isImageError(const QUrl& url) const; |
| 181 | |
| 182 | private Q_SLOTS: |
| 183 | void sceneGraphInitialized(); |
| 184 | void checkAnimationCallbacks(); |
| 185 | void invalidateSceneGraph(); |
| 186 | void schedulePolish(); |
| 187 | |
| 188 | protected: |
| 189 | void componentComplete() override; |
| 190 | void itemChange(QQuickItem::ItemChange, const QQuickItem::ItemChangeData &) override; |
| 191 | void updatePolish() override; |
| 192 | QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override; |
| 193 | void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override; |
| 194 | void releaseResources() override; |
| 195 | bool event(QEvent *event) override; |
| 196 | private: |
| 197 | Q_DECLARE_PRIVATE(QQuickCanvasItem) |
| 198 | Q_INVOKABLE void delayedCreate(); |
| 199 | bool createContext(const QString &contextType); |
| 200 | void initializeContext(QQuickCanvasContext *context, const QVariantMap &args = QVariantMap()); |
| 201 | static QRect tiledRect(const QRectF &window, const QSize &tileSize); |
| 202 | bool isPaintConnected(); |
| 203 | }; |
| 204 | |
| 205 | class QQuickContext2DRenderThread : public QThread |
| 206 | { |
| 207 | Q_OBJECT |
| 208 | public: |
| 209 | QQuickContext2DRenderThread(QQmlEngine *eng); |
| 210 | ~QQuickContext2DRenderThread(); |
| 211 | |
| 212 | static QQuickContext2DRenderThread *instance(QQmlEngine *engine); |
| 213 | |
| 214 | private: |
| 215 | QQmlEngine *m_engine; |
| 216 | QObject *m_eventLoopQuitHack; |
| 217 | static QHash<QQmlEngine *,QQuickContext2DRenderThread*> renderThreads; |
| 218 | static QMutex renderThreadsMutex; |
| 219 | }; |
| 220 | |
| 221 | QT_END_NAMESPACE |
| 222 | |
| 223 | QML_DECLARE_TYPE(QQuickCanvasItem) |
| 224 | |
| 225 | #endif //QQUICKCANVASITEM_P_H |
| 226 | |