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 | class QQmlV4Function; |
35 | |
36 | class QQuickCanvasPixmap : public QQmlRefCounted<QQuickCanvasPixmap> |
37 | { |
38 | public: |
39 | QQuickCanvasPixmap(const QImage& image); |
40 | QQuickCanvasPixmap(QQuickPixmap *pixmap); |
41 | ~QQuickCanvasPixmap(); |
42 | |
43 | QImage image(); |
44 | |
45 | qreal width() const; |
46 | qreal height() const; |
47 | bool isValid() const; |
48 | QQuickPixmap *pixmap() const { return m_pixmap;} |
49 | |
50 | private: |
51 | QQuickPixmap *m_pixmap; |
52 | QImage m_image; |
53 | }; |
54 | |
55 | class QQuickCanvasItem : public QQuickItem |
56 | { |
57 | Q_OBJECT |
58 | |
59 | Q_PROPERTY(bool available READ isAvailable NOTIFY availableChanged FINAL) |
60 | Q_PROPERTY(QString contextType READ contextType WRITE setContextType NOTIFY contextTypeChanged FINAL) |
61 | Q_PROPERTY(QJSValue context READ context NOTIFY contextChanged FINAL) |
62 | Q_PROPERTY(QSizeF canvasSize READ canvasSize WRITE setCanvasSize NOTIFY canvasSizeChanged FINAL) |
63 | Q_PROPERTY(QSize tileSize READ tileSize WRITE setTileSize NOTIFY tileSizeChanged FINAL) |
64 | Q_PROPERTY(QRectF canvasWindow READ canvasWindow WRITE setCanvasWindow NOTIFY canvasWindowChanged FINAL) |
65 | Q_PROPERTY(RenderTarget renderTarget READ renderTarget WRITE setRenderTarget NOTIFY renderTargetChanged FINAL) |
66 | Q_PROPERTY(RenderStrategy renderStrategy READ renderStrategy WRITE setRenderStrategy NOTIFY renderStrategyChanged FINAL) |
67 | QML_NAMED_ELEMENT(Canvas) |
68 | QML_ADDED_IN_VERSION(2, 0) |
69 | |
70 | public: |
71 | enum RenderTarget { |
72 | Image, |
73 | FramebufferObject |
74 | }; |
75 | Q_ENUM(RenderTarget) |
76 | |
77 | enum RenderStrategy { |
78 | Immediate, |
79 | Threaded, |
80 | Cooperative |
81 | }; |
82 | Q_ENUM(RenderStrategy) |
83 | |
84 | QQuickCanvasItem(QQuickItem *parent = nullptr); |
85 | ~QQuickCanvasItem(); |
86 | |
87 | bool isAvailable() const; |
88 | |
89 | QString contextType() const; |
90 | void setContextType(const QString &contextType); |
91 | |
92 | QJSValue context() const; |
93 | |
94 | QSizeF canvasSize() const; |
95 | void setCanvasSize(const QSizeF &); |
96 | |
97 | QSize tileSize() const; |
98 | void setTileSize(const QSize &); |
99 | |
100 | QRectF canvasWindow() const; |
101 | void setCanvasWindow(const QRectF& rect); |
102 | |
103 | RenderTarget renderTarget() const; |
104 | void setRenderTarget(RenderTarget target); |
105 | |
106 | RenderStrategy renderStrategy() const; |
107 | void setRenderStrategy(RenderStrategy strategy); |
108 | |
109 | QQuickCanvasContext *rawContext() const; |
110 | |
111 | QImage toImage(const QRectF& rect = QRectF()) const; |
112 | |
113 | Q_INVOKABLE void getContext(QQmlV4Function *args); |
114 | |
115 | Q_INVOKABLE void requestAnimationFrame(QQmlV4Function *args); |
116 | Q_INVOKABLE void cancelRequestAnimationFrame(QQmlV4Function *args); |
117 | |
118 | Q_INVOKABLE void requestPaint(); |
119 | Q_INVOKABLE void markDirty(const QRectF& dirtyRect = QRectF()); |
120 | |
121 | Q_INVOKABLE bool save(const QString &filename, const QSizeF &imageSize = QSizeF()) const; |
122 | Q_INVOKABLE QString toDataURL(const QString& type = QLatin1String("image/png" )) const; |
123 | QQmlRefPointer<QQuickCanvasPixmap> loadedPixmap(const QUrl& url); |
124 | |
125 | bool isTextureProvider() const override; |
126 | QSGTextureProvider *textureProvider() const override; |
127 | |
128 | Q_SIGNALS: |
129 | void paint(const QRect ®ion); |
130 | void painted(); |
131 | void availableChanged(); |
132 | void contextTypeChanged(); |
133 | void contextChanged(); |
134 | void canvasSizeChanged(); |
135 | void tileSizeChanged(); |
136 | void canvasWindowChanged(); |
137 | void renderTargetChanged(); |
138 | void renderStrategyChanged(); |
139 | void imageLoaded(); |
140 | |
141 | public Q_SLOTS: |
142 | void loadImage(const QUrl& url); |
143 | void unloadImage(const QUrl& url); |
144 | bool isImageLoaded(const QUrl& url) const; |
145 | bool isImageLoading(const QUrl& url) const; |
146 | bool isImageError(const QUrl& url) const; |
147 | |
148 | private Q_SLOTS: |
149 | void sceneGraphInitialized(); |
150 | void checkAnimationCallbacks(); |
151 | void invalidateSceneGraph(); |
152 | void schedulePolish(); |
153 | |
154 | protected: |
155 | void componentComplete() override; |
156 | void itemChange(QQuickItem::ItemChange, const QQuickItem::ItemChangeData &) override; |
157 | void updatePolish() override; |
158 | QSGNode *updatePaintNode(QSGNode *, UpdatePaintNodeData *) override; |
159 | void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override; |
160 | void releaseResources() override; |
161 | bool event(QEvent *event) override; |
162 | private: |
163 | Q_DECLARE_PRIVATE(QQuickCanvasItem) |
164 | Q_INVOKABLE void delayedCreate(); |
165 | bool createContext(const QString &contextType); |
166 | void initializeContext(QQuickCanvasContext *context, const QVariantMap &args = QVariantMap()); |
167 | static QRect tiledRect(const QRectF &window, const QSize &tileSize); |
168 | bool isPaintConnected(); |
169 | }; |
170 | |
171 | class QQuickContext2DRenderThread : public QThread |
172 | { |
173 | Q_OBJECT |
174 | public: |
175 | QQuickContext2DRenderThread(QQmlEngine *eng); |
176 | ~QQuickContext2DRenderThread(); |
177 | |
178 | static QQuickContext2DRenderThread *instance(QQmlEngine *engine); |
179 | |
180 | private: |
181 | QQmlEngine *m_engine; |
182 | QObject *m_eventLoopQuitHack; |
183 | static QHash<QQmlEngine *,QQuickContext2DRenderThread*> renderThreads; |
184 | static QMutex renderThreadsMutex; |
185 | }; |
186 | |
187 | QT_END_NAMESPACE |
188 | |
189 | QML_DECLARE_TYPE(QQuickCanvasItem) |
190 | |
191 | #endif //QQUICKCANVASITEM_P_H |
192 | |