| 1 | // Copyright (C) 2016 The Qt Company Ltd. | 
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only | 
| 3 |  | 
| 4 | #ifndef QQUICKCANVASITEM_P_H | 
| 5 | #define QQUICKCANVASITEM_P_H | 
| 6 |  | 
| 7 | // | 
| 8 | //  W A R N I N G | 
| 9 | //  ------------- | 
| 10 | // | 
| 11 | // This file is not part of the Qt API.  It exists purely as an | 
| 12 | // implementation detail.  This header file may change from version to | 
| 13 | // version without notice, or even be removed. | 
| 14 | // | 
| 15 | // We mean it. | 
| 16 | // | 
| 17 |  | 
| 18 | #include <private/qtquickglobal_p.h> | 
| 19 |  | 
| 20 | QT_REQUIRE_CONFIG(quick_canvas); | 
| 21 |  | 
| 22 | #include <QtQuick/qquickitem.h> | 
| 23 | #include <private/qqmlrefcount_p.h> | 
| 24 | #include <QtCore/QThread> | 
| 25 | #include <QtCore/qmutex.h> | 
| 26 | #include <QtGui/QImage> | 
| 27 |  | 
| 28 | QT_BEGIN_NAMESPACE | 
| 29 |  | 
| 30 | class QQuickCanvasContext; | 
| 31 |  | 
| 32 | class QQuickCanvasItemPrivate; | 
| 33 | class QQuickPixmap; | 
| 34 |  | 
| 35 | class QQuickCanvasPixmap final : public QQmlRefCounted<QQuickCanvasPixmap> | 
| 36 | { | 
| 37 | public: | 
| 38 |     QQuickCanvasPixmap(const QImage& image); | 
| 39 |     QQuickCanvasPixmap(QQuickPixmap *pixmap); | 
| 40 |     ~QQuickCanvasPixmap(); | 
| 41 |  | 
| 42 |     QImage image(); | 
| 43 |  | 
| 44 |     qreal width() const; | 
| 45 |     qreal height() const; | 
| 46 |     bool isValid() const; | 
| 47 |     QQuickPixmap *pixmap() const { return m_pixmap;} | 
| 48 |  | 
| 49 | private: | 
| 50 |     QQuickPixmap *m_pixmap; | 
| 51 |     QImage m_image; | 
| 52 | }; | 
| 53 |  | 
| 54 | class QQuickCanvasItem : public QQuickItem | 
| 55 | { | 
| 56 |     Q_OBJECT | 
| 57 |  | 
| 58 |     Q_PROPERTY(bool available READ isAvailable NOTIFY availableChanged) | 
| 59 |     Q_PROPERTY(QString contextType READ contextType WRITE setContextType NOTIFY contextTypeChanged) | 
| 60 |     Q_PROPERTY(QJSValue context READ context NOTIFY contextChanged) | 
| 61 |     Q_PROPERTY(QSizeF canvasSize READ canvasSize WRITE setCanvasSize NOTIFY canvasSizeChanged) | 
| 62 |     Q_PROPERTY(QSize tileSize READ tileSize WRITE setTileSize NOTIFY tileSizeChanged) | 
| 63 |     Q_PROPERTY(QRectF canvasWindow READ canvasWindow WRITE setCanvasWindow NOTIFY canvasWindowChanged) | 
| 64 |     Q_PROPERTY(RenderTarget renderTarget READ renderTarget WRITE setRenderTarget NOTIFY renderTargetChanged) | 
| 65 |     Q_PROPERTY(RenderStrategy renderStrategy READ renderStrategy WRITE setRenderStrategy NOTIFY renderStrategyChanged) | 
| 66 |     QML_NAMED_ELEMENT(Canvas) | 
| 67 |     QML_ADDED_IN_VERSION(2, 0) | 
| 68 |  | 
| 69 | public: | 
| 70 |     enum RenderTarget { | 
| 71 |         Image, | 
| 72 |         FramebufferObject | 
| 73 |     }; | 
| 74 |     Q_ENUM(RenderTarget) | 
| 75 |  | 
| 76 |     enum RenderStrategy { | 
| 77 |         Immediate, | 
| 78 |         Threaded, | 
| 79 |         Cooperative | 
| 80 |     }; | 
| 81 |     Q_ENUM(RenderStrategy) | 
| 82 |  | 
| 83 |     QQuickCanvasItem(QQuickItem *parent = nullptr); | 
| 84 |     ~QQuickCanvasItem(); | 
| 85 |  | 
| 86 |     bool isAvailable() const; | 
| 87 |  | 
| 88 |     QString contextType() const; | 
| 89 |     void setContextType(const QString &contextType); | 
| 90 |  | 
| 91 |     QJSValue context() const; | 
| 92 |  | 
| 93 |     QSizeF canvasSize() const; | 
| 94 |     void setCanvasSize(const QSizeF &); | 
| 95 |  | 
| 96 |     QSize tileSize() const; | 
| 97 |     void setTileSize(const QSize &); | 
| 98 |  | 
| 99 |     QRectF canvasWindow() const; | 
| 100 |     void setCanvasWindow(const QRectF& rect); | 
| 101 |  | 
| 102 |     RenderTarget renderTarget() const; | 
| 103 |     void setRenderTarget(RenderTarget target); | 
| 104 |  | 
| 105 |     RenderStrategy renderStrategy() const; | 
| 106 |     void setRenderStrategy(RenderStrategy strategy); | 
| 107 |  | 
| 108 |     QQuickCanvasContext *rawContext() const; | 
| 109 |  | 
| 110 |     QImage toImage(const QRectF& rect = QRectF()) const; | 
| 111 |  | 
| 112 |     Q_INVOKABLE void getContext(QQmlV4FunctionPtr args); | 
| 113 |  | 
| 114 |     Q_INVOKABLE void requestAnimationFrame(QQmlV4FunctionPtr args); | 
| 115 |     Q_INVOKABLE void cancelRequestAnimationFrame(QQmlV4FunctionPtr args); | 
| 116 |  | 
| 117 |     Q_INVOKABLE void requestPaint(); | 
| 118 |     Q_INVOKABLE void markDirty(const QRectF& dirtyRect = QRectF()); | 
| 119 |  | 
| 120 |     Q_INVOKABLE bool save(const QString &filename, const QSizeF &imageSize = QSizeF()) const; | 
| 121 |     Q_INVOKABLE QString toDataURL(const QString& type = QLatin1String("image/png" )) const; | 
| 122 |     QQmlRefPointer<QQuickCanvasPixmap> loadedPixmap(const QUrl& url, QSizeF sourceSize = QSizeF()); | 
| 123 |  | 
| 124 |     bool isTextureProvider() const override; | 
| 125 |     QSGTextureProvider *textureProvider() const override; | 
| 126 |  | 
| 127 | Q_SIGNALS: | 
| 128 |     void paint(const QRect ®ion); | 
| 129 |     void painted(); | 
| 130 |     void availableChanged(); | 
| 131 |     void contextTypeChanged(); | 
| 132 |     void contextChanged(); | 
| 133 |     void canvasSizeChanged(); | 
| 134 |     void tileSizeChanged(); | 
| 135 |     void canvasWindowChanged(); | 
| 136 |     void renderTargetChanged(); | 
| 137 |     void renderStrategyChanged(); | 
| 138 |     void imageLoaded(); | 
| 139 |  | 
| 140 | public Q_SLOTS: | 
| 141 |     void loadImage(const QUrl& url, QSizeF sourceSize = QSizeF()); | 
| 142 |     void unloadImage(const QUrl& url); | 
| 143 |     bool isImageLoaded(const QUrl& url) const; | 
| 144 |     bool isImageLoading(const QUrl& url) const; | 
| 145 |     bool isImageError(const QUrl& url) const; | 
| 146 |  | 
| 147 | private Q_SLOTS: | 
| 148 |     void sceneGraphInitialized(); | 
| 149 |     void checkAnimationCallbacks(); | 
| 150 |     void invalidateSceneGraph(); | 
| 151 |     void schedulePolish(); | 
| 152 |  | 
| 153 | protected: | 
| 154 |     void componentComplete() override; | 
| 155 |     void itemChange(QQuickItem::ItemChange, const QQuickItem::ItemChangeData &) override; | 
| 156 |     void updatePolish() override; | 
| 157 |     QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override; | 
| 158 |     void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; | 
| 159 |     void releaseResources() override; | 
| 160 |     bool event(QEvent *event) override; | 
| 161 | private: | 
| 162 |     Q_DECLARE_PRIVATE(QQuickCanvasItem) | 
| 163 |     Q_INVOKABLE void delayedCreate(); | 
| 164 |     bool createContext(const QString &contextType); | 
| 165 |     void initializeContext(QQuickCanvasContext *context, const QVariantMap &args = QVariantMap()); | 
| 166 |     static QRect tiledRect(const QRectF &window, const QSize &tileSize); | 
| 167 |     bool isPaintConnected(); | 
| 168 | }; | 
| 169 |  | 
| 170 | class QQuickContext2DRenderThread : public QThread | 
| 171 | { | 
| 172 |     Q_OBJECT | 
| 173 | public: | 
| 174 |     QQuickContext2DRenderThread(QQmlEngine *eng); | 
| 175 |     ~QQuickContext2DRenderThread(); | 
| 176 |  | 
| 177 |     static QQuickContext2DRenderThread *instance(QQmlEngine *engine); | 
| 178 |  | 
| 179 | private: | 
| 180 |     QQmlEngine *m_engine; | 
| 181 |     QObject *m_eventLoopQuitHack; | 
| 182 |     static QHash<QQmlEngine *,QQuickContext2DRenderThread*> renderThreads; | 
| 183 |     static QMutex renderThreadsMutex; | 
| 184 | }; | 
| 185 |  | 
| 186 | QT_END_NAMESPACE | 
| 187 |  | 
| 188 | #endif //QQUICKCANVASITEM_P_H | 
| 189 |  |