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 QSGCONTEXT_H
5#define QSGCONTEXT_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 <QtCore/QObject>
19#include <QtCore/qabstractanimation.h>
20#include <QtCore/QMutex>
21
22#include <QtGui/QImage>
23#include <QtGui/QSurfaceFormat>
24
25#include <private/qtquickglobal_p.h>
26#include <private/qrawfont_p.h>
27
28#include <QtQuick/qsgnode.h>
29#include <QtQuick/qsgrendererinterface.h>
30
31QT_BEGIN_NAMESPACE
32
33class QSGContextPrivate;
34class QSGInternalRectangleNode;
35class QSGInternalImageNode;
36class QSGPainterNode;
37class QSGGlyphNode;
38class QSGRenderer;
39class QSGDistanceFieldGlyphCache;
40class QQuickWindow;
41class QSGTexture;
42class QSGMaterial;
43class QSGRenderLoop;
44class QSGLayer;
45class QQuickTextureFactory;
46class QSGCompressedTextureFactory;
47class QSGContext;
48class QQuickPaintedItem;
49class QSGRendererInterface;
50class QSGShaderEffectNode;
51class QSGGuiThreadShaderEffectManager;
52class QSGRectangleNode;
53class QSGImageNode;
54class QSGNinePatchNode;
55class QSGSpriteNode;
56class QSGRenderContext;
57class QSGRenderTarget;
58class QRhi;
59class QRhiRenderTarget;
60class QRhiRenderPassDescriptor;
61class QRhiCommandBuffer;
62class QQuickGraphicsConfiguration;
63
64Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TIME_RENDERLOOP)
65Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TIME_COMPILATION)
66Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TIME_TEXTURE)
67Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TIME_GLYPH)
68Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TIME_RENDERER)
69
70Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_INFO)
71Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_RENDERLOOP)
72
73class Q_QUICK_PRIVATE_EXPORT QSGContext : public QObject
74{
75 Q_OBJECT
76
77public:
78 enum AntialiasingMethod {
79 UndecidedAntialiasing,
80 VertexAntialiasing,
81 MsaaAntialiasing
82 };
83
84 explicit QSGContext(QObject *parent = nullptr);
85 ~QSGContext() override;
86
87 virtual void renderContextInitialized(QSGRenderContext *renderContext);
88 virtual void renderContextInvalidated(QSGRenderContext *renderContext);
89 virtual QSGRenderContext *createRenderContext() = 0;
90
91 QSGInternalRectangleNode *createInternalRectangleNode(const QRectF &rect, const QColor &c);
92 virtual QSGInternalRectangleNode *createInternalRectangleNode() = 0;
93 virtual QSGInternalImageNode *createInternalImageNode(QSGRenderContext *renderContext) = 0;
94 virtual QSGPainterNode *createPainterNode(QQuickPaintedItem *item) = 0;
95 virtual QSGGlyphNode *createGlyphNode(QSGRenderContext *rc, bool preferNativeGlyphNode, int renderTypeQuality) = 0;
96 virtual QSGLayer *createLayer(QSGRenderContext *renderContext) = 0;
97 virtual QSGGuiThreadShaderEffectManager *createGuiThreadShaderEffectManager();
98 virtual QSGShaderEffectNode *createShaderEffectNode(QSGRenderContext *renderContext);
99#if QT_CONFIG(quick_sprite)
100 virtual QSGSpriteNode *createSpriteNode() = 0;
101#endif
102 virtual QAnimationDriver *createAnimationDriver(QObject *parent);
103 virtual float vsyncIntervalForAnimationDriver(QAnimationDriver *driver);
104 virtual bool isVSyncDependent(QAnimationDriver *driver);
105
106 virtual QSize minimumFBOSize() const;
107 virtual QSurfaceFormat defaultSurfaceFormat() const = 0;
108
109 virtual QSGRendererInterface *rendererInterface(QSGRenderContext *renderContext);
110
111 virtual QSGRectangleNode *createRectangleNode() = 0;
112 virtual QSGImageNode *createImageNode() = 0;
113 virtual QSGNinePatchNode *createNinePatchNode() = 0;
114
115 static QSGContext *createDefaultContext();
116 static QQuickTextureFactory *createTextureFactoryFromImage(const QImage &image);
117 static QSGRenderLoop *createWindowManager();
118
119 static void setBackend(const QString &backend);
120 static QString backend();
121};
122
123class Q_QUICK_PRIVATE_EXPORT QSGRenderContext : public QObject
124{
125 Q_OBJECT
126public:
127 enum CreateTextureFlags {
128 CreateTexture_Alpha = 0x1,
129 CreateTexture_Atlas = 0x2,
130 CreateTexture_Mipmap = 0x4
131 };
132
133 QSGRenderContext(QSGContext *context);
134 ~QSGRenderContext() override;
135
136 QSGContext *sceneGraphContext() const { return m_sg; }
137 virtual bool isValid() const { return true; }
138
139 struct InitParams { };
140 virtual void initialize(const InitParams *params);
141 virtual void invalidate();
142
143 using RenderPassCallback = void (*)(void *);
144
145 virtual void prepareSync(qreal devicePixelRatio,
146 QRhiCommandBuffer *cb,
147 const QQuickGraphicsConfiguration &config);
148
149 virtual void beginNextFrame(QSGRenderer *renderer, const QSGRenderTarget &renderTarget,
150 RenderPassCallback mainPassRecordingStart,
151 RenderPassCallback mainPassRecordingEnd,
152 void *callbackUserData);
153 virtual void renderNextFrame(QSGRenderer *renderer) = 0;
154 virtual void endNextFrame(QSGRenderer *renderer);
155
156 virtual void endSync();
157
158 virtual void preprocess();
159 virtual void invalidateGlyphCaches();
160 virtual QSGDistanceFieldGlyphCache *distanceFieldGlyphCache(const QRawFont &font, int renderTypeQuality);
161 QSGTexture *textureForFactory(QQuickTextureFactory *factory, QQuickWindow *window);
162
163 virtual QSGTexture *createTexture(const QImage &image, uint flags = CreateTexture_Alpha) const = 0;
164 virtual QSGRenderer *createRenderer(QSGRendererInterface::RenderMode renderMode = QSGRendererInterface::RenderMode2D) = 0;
165 virtual QSGTexture *compressedTextureForFactory(const QSGCompressedTextureFactory *) const;
166
167 virtual int maxTextureSize() const = 0;
168
169 void unregisterFontengineForCleanup(QFontEngine *engine);
170 void registerFontengineForCleanup(QFontEngine *engine);
171
172 virtual QRhi *rhi() const;
173
174Q_SIGNALS:
175 void initialized();
176 void invalidated();
177 void releaseCachedResourcesRequested();
178
179public Q_SLOTS:
180 void textureFactoryDestroyed(QObject *o);
181
182protected:
183 // Hold m_sg with QPointer in the rare case it gets deleted before us.
184 QPointer<QSGContext> m_sg;
185
186 QMutex m_mutex;
187 QHash<QObject *, QSGTexture *> m_textures;
188 QSet<QSGTexture *> m_texturesToDelete;
189 QHash<QString, QSGDistanceFieldGlyphCache *> m_glyphCaches;
190
191 // References to font engines that are currently in use by native rendering glyph nodes
192 // and which must be kept alive as long as they are used in the render thread.
193 QHash<QFontEngine *, int> m_fontEnginesToClean;
194};
195
196QT_END_NAMESPACE
197
198#endif // QSGCONTEXT_H
199

source code of qtdeclarative/src/quick/scenegraph/qsgcontext_p.h