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 QSGCONTEXT_H
41 #define QSGCONTEXT_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 <QtCore/QObject>
55 #include <QtCore/qabstractanimation.h>
56 #include <QtCore/QMutex>
57 
58 #include <QtGui/QImage>
59 #include <QtGui/QSurfaceFormat>
60 
61 #include <private/qtquickglobal_p.h>
62 #include <private/qrawfont_p.h>
63 
64 #include <QtQuick/qsgnode.h>
65 
66 QT_BEGIN_NAMESPACE
67 
68 class QSGContextPrivate;
69 class QSGInternalRectangleNode;
70 class QSGInternalImageNode;
71 class QSGPainterNode;
72 class QSGGlyphNode;
73 class QSGRenderer;
74 class QSGDistanceFieldGlyphCache;
75 class QQuickWindow;
76 class QSGTexture;
77 class QSGMaterial;
78 class QSGRenderLoop;
79 class QSGLayer;
80 class QQuickTextureFactory;
81 class QSGCompressedTextureFactory;
82 class QSGContext;
83 class QQuickPaintedItem;
84 class QSGRendererInterface;
85 class QSGShaderEffectNode;
86 class QSGGuiThreadShaderEffectManager;
87 class QSGRectangleNode;
88 class QSGImageNode;
89 class QSGNinePatchNode;
90 class QSGSpriteNode;
91 class QSGRenderContext;
92 class QRhi;
93 class QRhiRenderTarget;
94 class QRhiRenderPassDescriptor;
95 class QRhiCommandBuffer;
96 
97 Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TIME_RENDERLOOP)
Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TIME_COMPILATION)98 Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TIME_COMPILATION)
99 Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TIME_TEXTURE)
100 Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TIME_GLYPH)
101 Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TIME_RENDERER)
102 
103 Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_INFO)
104 Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_RENDERLOOP)
105 
106 class Q_QUICK_PRIVATE_EXPORT QSGContext : public QObject
107 {
108     Q_OBJECT
109 
110 public:
111     enum AntialiasingMethod {
112         UndecidedAntialiasing,
113         VertexAntialiasing,
114         MsaaAntialiasing
115     };
116 
117     explicit QSGContext(QObject *parent = nullptr);
118     ~QSGContext() override;
119 
120     virtual void renderContextInitialized(QSGRenderContext *renderContext);
121     virtual void renderContextInvalidated(QSGRenderContext *renderContext);
122     virtual QSGRenderContext *createRenderContext() = 0;
123 
124     QSGInternalRectangleNode *createInternalRectangleNode(const QRectF &rect, const QColor &c);
125     virtual QSGInternalRectangleNode *createInternalRectangleNode() = 0;
126     virtual QSGInternalImageNode *createInternalImageNode(QSGRenderContext *renderContext) = 0;
127     virtual QSGPainterNode *createPainterNode(QQuickPaintedItem *item) = 0;
128     virtual QSGGlyphNode *createGlyphNode(QSGRenderContext *rc, bool preferNativeGlyphNode) = 0;
129     virtual QSGLayer *createLayer(QSGRenderContext *renderContext) = 0;
130     virtual QSGGuiThreadShaderEffectManager *createGuiThreadShaderEffectManager();
131     virtual QSGShaderEffectNode *createShaderEffectNode(QSGRenderContext *renderContext,
132                                                         QSGGuiThreadShaderEffectManager *mgr);
133 #if QT_CONFIG(quick_sprite)
134     virtual QSGSpriteNode *createSpriteNode() = 0;
135 #endif
136     virtual QAnimationDriver *createAnimationDriver(QObject *parent);
137 
138     virtual QSize minimumFBOSize() const;
139     virtual QSurfaceFormat defaultSurfaceFormat() const = 0;
140 
141     virtual QSGRendererInterface *rendererInterface(QSGRenderContext *renderContext);
142 
143     virtual QSGRectangleNode *createRectangleNode() = 0;
144     virtual QSGImageNode *createImageNode() = 0;
145     virtual QSGNinePatchNode *createNinePatchNode() = 0;
146 
147     static QSGContext *createDefaultContext();
148     static QQuickTextureFactory *createTextureFactoryFromImage(const QImage &image);
149     static QSGRenderLoop *createWindowManager();
150 
151     static void setBackend(const QString &backend);
152     static QString backend();
153 };
154 
155 class Q_QUICK_PRIVATE_EXPORT QSGRenderContext : public QObject
156 {
157     Q_OBJECT
158 public:
159     enum CreateTextureFlags {
160         CreateTexture_Alpha       = 0x1,
161         CreateTexture_Atlas       = 0x2,
162         CreateTexture_Mipmap      = 0x4
163     };
164 
165     QSGRenderContext(QSGContext *context);
166     ~QSGRenderContext() override;
167 
sceneGraphContext()168     QSGContext *sceneGraphContext() const { return m_sg; }
isValid()169     virtual bool isValid() const { return true; }
170 
171     struct InitParams { };
172     virtual void initialize(const InitParams *params);
173     virtual void invalidate();
174 
175     using RenderPassCallback = void (*)(void *);
176 
177     virtual void prepareSync(qreal devicePixelRatio, QRhiCommandBuffer *cb);
178     virtual void beginNextFrame(QSGRenderer *renderer,
179                                 RenderPassCallback mainPassRecordingStart,
180                                 RenderPassCallback mainPassRecordingEnd,
181                                 void *callbackUserData);
182     virtual void renderNextFrame(QSGRenderer *renderer, uint fboId) = 0;
183     virtual void endNextFrame(QSGRenderer *renderer);
184 
185     virtual void beginNextRhiFrame(QSGRenderer *renderer,
186                                    QRhiRenderTarget *rt, QRhiRenderPassDescriptor *rp, QRhiCommandBuffer *cb,
187                                    RenderPassCallback mainPassRecordingStart,
188                                    RenderPassCallback mainPassRecordingEnd,
189                                    void *callbackUserData);
190     virtual void renderNextRhiFrame(QSGRenderer *renderer);
191     virtual void endNextRhiFrame(QSGRenderer *renderer);
192 
193     virtual void endSync();
194 
195     virtual void preprocess();
196     virtual QSGDistanceFieldGlyphCache *distanceFieldGlyphCache(const QRawFont &font);
197     QSGTexture *textureForFactory(QQuickTextureFactory *factory, QQuickWindow *window);
198 
199     virtual QSGTexture *createTexture(const QImage &image, uint flags = CreateTexture_Alpha) const = 0;
200     virtual QSGRenderer *createRenderer() = 0;
201     virtual QSGTexture *compressedTextureForFactory(const QSGCompressedTextureFactory *) const;
202 
setAttachToGraphicsContext(bool attach)203     virtual void setAttachToGraphicsContext(bool attach) { Q_UNUSED(attach); }
204 
205     virtual int maxTextureSize() const = 0;
206 
207     void registerFontengineForCleanup(QFontEngine *engine);
208 
209     virtual QRhi *rhi() const;
210 
211 Q_SIGNALS:
212     void initialized();
213     void invalidated();
214 
215 public Q_SLOTS:
216     void textureFactoryDestroyed(QObject *o);
217 
218 protected:
219     // Hold m_sg with QPointer in the rare case it gets deleted before us.
220     QPointer<QSGContext> m_sg;
221 
222     QMutex m_mutex;
223     QHash<QObject *, QSGTexture *> m_textures;
224     QSet<QSGTexture *> m_texturesToDelete;
225     QHash<QString, QSGDistanceFieldGlyphCache *> m_glyphCaches;
226 
227     QSet<QFontEngine *> m_fontEnginesToClean;
228 };
229 
230 QT_END_NAMESPACE
231 
232 #endif // QSGCONTEXT_H
233