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 QQUICKCONTEXT2DTEXTURE_P_H |
5 | #define QQUICKCONTEXT2DTEXTURE_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/qsgtexture.h> |
23 | #include "qquickcanvasitem_p.h" |
24 | #include "qquickcontext2d_p.h" |
25 | #include <QtCore/QMutex> |
26 | #include <QtCore/QWaitCondition> |
27 | #include <QtCore/QThread> |
28 | |
29 | QT_BEGIN_NAMESPACE |
30 | |
31 | class QQuickContext2DTile; |
32 | class QQuickContext2DCommandBuffer; |
33 | |
34 | class QQuickContext2DTexture : public QObject |
35 | { |
36 | Q_OBJECT |
37 | public: |
38 | class PaintEvent : public QEvent { |
39 | public: |
40 | PaintEvent(QQuickContext2DCommandBuffer *b) : QEvent(QEvent::Type(QEvent::User + 1)), buffer(b) {} |
41 | QQuickContext2DCommandBuffer *buffer; |
42 | }; |
43 | |
44 | class CanvasChangeEvent : public QEvent { |
45 | public: |
46 | CanvasChangeEvent(const QSize &cSize, |
47 | const QSize &tSize, |
48 | const QRect &cWindow, |
49 | const QRect &dRect, |
50 | bool sm, |
51 | bool aa) |
52 | : QEvent(QEvent::Type(QEvent::User + 2)) |
53 | , canvasSize(cSize) |
54 | , tileSize(tSize) |
55 | , canvasWindow(cWindow) |
56 | , dirtyRect(dRect) |
57 | , smooth(sm) |
58 | , antialiasing(aa) |
59 | { |
60 | } |
61 | QSize canvasSize; |
62 | QSize tileSize; |
63 | QRect canvasWindow; |
64 | QRect dirtyRect; |
65 | bool smooth; |
66 | bool antialiasing; |
67 | }; |
68 | |
69 | QQuickContext2DTexture(); |
70 | ~QQuickContext2DTexture(); |
71 | |
72 | virtual QQuickCanvasItem::RenderTarget renderTarget() const = 0; |
73 | static QRect tiledRect(const QRectF& window, const QSize& tileSize); |
74 | |
75 | bool setCanvasSize(const QSize &size); |
76 | bool setTileSize(const QSize &size); |
77 | bool setCanvasWindow(const QRect& canvasWindow); |
78 | void setSmooth(bool smooth); |
79 | void setAntialiasing(bool antialiasing); |
80 | bool setDirtyRect(const QRect &dirtyRect); |
81 | bool canvasDestroyed(); |
82 | void setOnCustomThread(bool is) { m_onCustomThread = is; } |
83 | bool isOnCustomThread() const { return m_onCustomThread; } |
84 | |
85 | // Called during sync() on the scene graph thread while GUI is blocked. |
86 | virtual QSGTexture *textureForNextFrame(QSGTexture *lastFrame, QQuickWindow *window) = 0; |
87 | bool event(QEvent *e) override; |
88 | |
89 | Q_SIGNALS: |
90 | void textureChanged(); |
91 | |
92 | public Q_SLOTS: |
93 | void canvasChanged(const QSize& canvasSize, const QSize& tileSize, const QRect& canvasWindow, const QRect& dirtyRect, bool smooth, bool antialiasing); |
94 | void paint(QQuickContext2DCommandBuffer *ccb); |
95 | void markDirtyTexture(); |
96 | void setItem(QQuickCanvasItem* item); |
97 | virtual void grabImage(const QRectF& region = QRectF()) = 0; |
98 | |
99 | protected: |
100 | virtual QVector2D scaleFactor() const { return QVector2D(1, 1); } |
101 | |
102 | void paintWithoutTiles(QQuickContext2DCommandBuffer *ccb); |
103 | virtual QPaintDevice* beginPainting() {m_painting = true; return nullptr; } |
104 | virtual void endPainting() {m_painting = false;} |
105 | virtual QQuickContext2DTile* createTile() const = 0; |
106 | virtual void compositeTile(QQuickContext2DTile* tile) = 0; |
107 | |
108 | void clearTiles(); |
109 | virtual QSize adjustedTileSize(const QSize &ts); |
110 | QRect createTiles(const QRect& window); |
111 | |
112 | QList<QQuickContext2DTile*> m_tiles; |
113 | QQuickContext2D *m_context; |
114 | QSurface *m_surface; |
115 | |
116 | QQuickContext2D::State m_state; |
117 | |
118 | QQuickCanvasItem* m_item; |
119 | QSize m_canvasSize; |
120 | QSize m_tileSize; |
121 | QRect m_canvasWindow; |
122 | qreal m_canvasDevicePixelRatio; |
123 | |
124 | QMutex m_mutex; |
125 | QWaitCondition m_condition; |
126 | |
127 | uint m_canvasWindowChanged : 1; |
128 | uint m_dirtyTexture : 1; |
129 | uint m_smooth : 1; |
130 | uint m_antialiasing : 1; |
131 | uint m_tiledCanvas : 1; |
132 | uint m_painting : 1; |
133 | uint m_onCustomThread : 1; // Not GUI and not SGRender |
134 | }; |
135 | |
136 | class QSGPlainTexture; |
137 | class QQuickContext2DImageTexture : public QQuickContext2DTexture |
138 | { |
139 | Q_OBJECT |
140 | |
141 | public: |
142 | QQuickContext2DImageTexture(); |
143 | ~QQuickContext2DImageTexture(); |
144 | |
145 | QQuickCanvasItem::RenderTarget renderTarget() const override; |
146 | |
147 | QQuickContext2DTile* createTile() const override; |
148 | QPaintDevice* beginPainting() override; |
149 | void endPainting() override; |
150 | void compositeTile(QQuickContext2DTile* tile) override; |
151 | |
152 | QSGTexture *textureForNextFrame(QSGTexture *lastFrame, QQuickWindow *window) override; |
153 | |
154 | public Q_SLOTS: |
155 | void grabImage(const QRectF& region = QRectF()) override; |
156 | |
157 | private: |
158 | QImage m_image; |
159 | QImage m_displayImage; |
160 | QPainter m_painter; |
161 | }; |
162 | |
163 | QT_END_NAMESPACE |
164 | |
165 | #endif // QQUICKCONTEXT2DTEXTURE_P_H |
166 | |