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

source code of qtdeclarative/src/quick/items/context2d/qquickcanvasitem_p.h