| 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 QQUICKCONTEXT2DTEXTURE_P_H | 
| 41 | #define QQUICKCONTEXT2DTEXTURE_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/qsgtexture.h> | 
| 59 | #include "qquickcanvasitem_p.h" | 
| 60 | #include "qquickcontext2d_p.h" | 
| 61 | #if QT_CONFIG(opengl) | 
| 62 | # include <QOpenGLContext> | 
| 63 | # include <QOpenGLFramebufferObject> | 
| 64 | #endif | 
| 65 | #include <QtCore/QMutex> | 
| 66 | #include <QtCore/QWaitCondition> | 
| 67 | #include <QtCore/QThread> | 
| 68 |  | 
| 69 | QT_BEGIN_NAMESPACE | 
| 70 |  | 
| 71 | class QQuickContext2DTile; | 
| 72 | class QQuickContext2DCommandBuffer; | 
| 73 |  | 
| 74 | class QQuickContext2DTexture : public QObject | 
| 75 | { | 
| 76 |     Q_OBJECT | 
| 77 | public: | 
| 78 |     class PaintEvent : public QEvent { | 
| 79 |     public: | 
| 80 |         PaintEvent(QQuickContext2DCommandBuffer *b) : QEvent(QEvent::Type(QEvent::User + 1)), buffer(b) {} | 
| 81 |         QQuickContext2DCommandBuffer *buffer; | 
| 82 |     }; | 
| 83 |  | 
| 84 |     class CanvasChangeEvent : public QEvent { | 
| 85 |     public: | 
| 86 |         CanvasChangeEvent(const QSize &cSize, | 
| 87 |                           const QSize &tSize, | 
| 88 |                           const QRect &cWindow, | 
| 89 |                           const QRect &dRect, | 
| 90 |                           bool sm, | 
| 91 |                           bool aa) | 
| 92 |             : QEvent(QEvent::Type(QEvent::User + 2)) | 
| 93 |             , canvasSize(cSize) | 
| 94 |             , tileSize(tSize) | 
| 95 |             , canvasWindow(cWindow) | 
| 96 |             , dirtyRect(dRect) | 
| 97 |             , smooth(sm) | 
| 98 |             , antialiasing(aa) | 
| 99 |         { | 
| 100 |         } | 
| 101 |         QSize canvasSize; | 
| 102 |         QSize tileSize; | 
| 103 |         QRect canvasWindow; | 
| 104 |         QRect dirtyRect; | 
| 105 |         bool smooth; | 
| 106 |         bool antialiasing; | 
| 107 |     }; | 
| 108 |  | 
| 109 |     QQuickContext2DTexture(); | 
| 110 |     ~QQuickContext2DTexture(); | 
| 111 |  | 
| 112 |     virtual QQuickCanvasItem::RenderTarget renderTarget() const = 0; | 
| 113 |     static QRect tiledRect(const QRectF& window, const QSize& tileSize); | 
| 114 |  | 
| 115 |     bool setCanvasSize(const QSize &size); | 
| 116 |     bool setTileSize(const QSize &size); | 
| 117 |     bool setCanvasWindow(const QRect& canvasWindow); | 
| 118 |     void setSmooth(bool smooth); | 
| 119 |     void setAntialiasing(bool antialiasing); | 
| 120 |     bool setDirtyRect(const QRect &dirtyRect); | 
| 121 |     bool canvasDestroyed(); | 
| 122 |     void setOnCustomThread(bool is) { m_onCustomThread = is; } | 
| 123 |     bool isOnCustomThread() const { return m_onCustomThread; } | 
| 124 |  | 
| 125 |     // Called during sync() on the scene graph thread while GUI is blocked. | 
| 126 |     virtual QSGTexture *textureForNextFrame(QSGTexture *lastFrame, QQuickWindow *window) = 0; | 
| 127 |     bool event(QEvent *e) override; | 
| 128 | #if QT_CONFIG(opengl) | 
| 129 |     void initializeOpenGL(QOpenGLContext *gl, QOffscreenSurface *s) { | 
| 130 |         m_gl = gl; | 
| 131 |         m_surface = s; | 
| 132 |     } | 
| 133 | #endif | 
| 134 |  | 
| 135 | Q_SIGNALS: | 
| 136 |     void textureChanged(); | 
| 137 |  | 
| 138 | public Q_SLOTS: | 
| 139 |     void canvasChanged(const QSize& canvasSize, const QSize& tileSize, const QRect& canvasWindow, const QRect& dirtyRect, bool smooth, bool antialiasing); | 
| 140 |     void paint(QQuickContext2DCommandBuffer *ccb); | 
| 141 |     void markDirtyTexture(); | 
| 142 |     void setItem(QQuickCanvasItem* item); | 
| 143 |     virtual void grabImage(const QRectF& region = QRectF()) = 0; | 
| 144 |  | 
| 145 | protected: | 
| 146 |     virtual QVector2D scaleFactor() const { return QVector2D(1, 1); } | 
| 147 |  | 
| 148 |     void paintWithoutTiles(QQuickContext2DCommandBuffer *ccb); | 
| 149 |     virtual QPaintDevice* beginPainting() {m_painting = true; return nullptr; } | 
| 150 |     virtual void endPainting() {m_painting = false;} | 
| 151 |     virtual QQuickContext2DTile* createTile() const = 0; | 
| 152 |     virtual void compositeTile(QQuickContext2DTile* tile) = 0; | 
| 153 |  | 
| 154 |     void clearTiles(); | 
| 155 |     virtual QSize adjustedTileSize(const QSize &ts); | 
| 156 |     QRect createTiles(const QRect& window); | 
| 157 |  | 
| 158 |     QList<QQuickContext2DTile*> m_tiles; | 
| 159 |     QQuickContext2D *m_context; | 
| 160 | #if QT_CONFIG(opengl) | 
| 161 |     QOpenGLContext *m_gl; | 
| 162 | #endif | 
| 163 |     QSurface *m_surface; | 
| 164 |  | 
| 165 |     QQuickContext2D::State m_state; | 
| 166 |  | 
| 167 |     QQuickCanvasItem* m_item; | 
| 168 |     QSize m_canvasSize; | 
| 169 |     QSize m_tileSize; | 
| 170 |     QRect m_canvasWindow; | 
| 171 |     qreal m_canvasDevicePixelRatio; | 
| 172 |  | 
| 173 |     QMutex m_mutex; | 
| 174 |     QWaitCondition m_condition; | 
| 175 |  | 
| 176 |     uint m_canvasWindowChanged : 1; | 
| 177 |     uint m_dirtyTexture : 1; | 
| 178 |     uint m_smooth : 1; | 
| 179 |     uint m_antialiasing : 1; | 
| 180 |     uint m_tiledCanvas : 1; | 
| 181 |     uint m_painting : 1; | 
| 182 |     uint m_onCustomThread : 1; // Not GUI and not SGRender | 
| 183 | }; | 
| 184 | #if QT_CONFIG(opengl) | 
| 185 | class QQuickContext2DFBOTexture : public QQuickContext2DTexture | 
| 186 | { | 
| 187 |     Q_OBJECT | 
| 188 |  | 
| 189 | public: | 
| 190 |     QQuickContext2DFBOTexture(); | 
| 191 |     ~QQuickContext2DFBOTexture(); | 
| 192 |     QQuickContext2DTile* createTile() const override; | 
| 193 |     QPaintDevice* beginPainting() override; | 
| 194 |     void endPainting() override; | 
| 195 |     QRectF normalizedTextureSubRect() const; | 
| 196 |     QQuickCanvasItem::RenderTarget renderTarget() const override; | 
| 197 |     void compositeTile(QQuickContext2DTile* tile) override; | 
| 198 |     QSize adjustedTileSize(const QSize &ts) override; | 
| 199 |  | 
| 200 |     QSGTexture *textureForNextFrame(QSGTexture *, QQuickWindow *window) override; | 
| 201 |  | 
| 202 | protected: | 
| 203 |     QVector2D scaleFactor() const override; | 
| 204 |  | 
| 205 | public Q_SLOTS: | 
| 206 |     void grabImage(const QRectF& region = QRectF()) override; | 
| 207 |  | 
| 208 | private: | 
| 209 |     bool doMultisampling() const; | 
| 210 |     QOpenGLFramebufferObject *m_fbo; | 
| 211 |     QOpenGLFramebufferObject *m_multisampledFbo; | 
| 212 |     QSize m_fboSize; | 
| 213 |     QPaintDevice *m_paint_device; | 
| 214 |  | 
| 215 |  | 
| 216 |     GLuint m_displayTextures[2]; | 
| 217 |     int m_displayTexture; | 
| 218 | }; | 
| 219 | #endif | 
| 220 | class QSGPlainTexture; | 
| 221 | class QQuickContext2DImageTexture : public QQuickContext2DTexture | 
| 222 | { | 
| 223 |     Q_OBJECT | 
| 224 |  | 
| 225 | public: | 
| 226 |     QQuickContext2DImageTexture(); | 
| 227 |     ~QQuickContext2DImageTexture(); | 
| 228 |  | 
| 229 |     QQuickCanvasItem::RenderTarget renderTarget() const override; | 
| 230 |  | 
| 231 |     QQuickContext2DTile* createTile() const override; | 
| 232 |     QPaintDevice* beginPainting() override; | 
| 233 |     void endPainting() override; | 
| 234 |     void compositeTile(QQuickContext2DTile* tile) override; | 
| 235 |  | 
| 236 |     QSGTexture *textureForNextFrame(QSGTexture *lastFrame, QQuickWindow *window) override; | 
| 237 |  | 
| 238 | public Q_SLOTS: | 
| 239 |     void grabImage(const QRectF& region = QRectF()) override; | 
| 240 |  | 
| 241 | private: | 
| 242 |     QImage m_image; | 
| 243 |     QImage m_displayImage; | 
| 244 |     QPainter m_painter; | 
| 245 | }; | 
| 246 |  | 
| 247 | QT_END_NAMESPACE | 
| 248 |  | 
| 249 | #endif // QQUICKCONTEXT2DTEXTURE_P_H | 
| 250 |  |