Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qsgcontext_p.h
Go to the documentation of this file.
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#include <private/qfontengine_p.h>
28
29#include <QtQuick/qsgnode.h>
30#include <QtQuick/qsgrendererinterface.h>
31#include <QtQuick/qsgtextnode.h>
32
33#include <QtCore/qpointer.h>
34
36
37Q_DECLARE_LOGGING_CATEGORY(lcQsgLeak)
38
39class QSGContextPrivate;
40class QSGInternalRectangleNode;
41class QSGInternalImageNode;
42class QSGInternalTextNode;
43class QSGPainterNode;
44class QSGGlyphNode;
45class QSGRenderer;
46class QSGDistanceFieldGlyphCache;
47class QQuickWindow;
48class QSGTexture;
49class QSGMaterial;
50class QSGRenderLoop;
51class QSGLayer;
52class QQuickTextureFactory;
53class QSGCompressedTextureFactory;
54class QSGContext;
55class QQuickPaintedItem;
56class QSGRendererInterface;
57class QSGShaderEffectNode;
58class QSGGuiThreadShaderEffectManager;
59class QSGRectangleNode;
60class QSGTextNode;
61class QSGImageNode;
62class QSGNinePatchNode;
63class QSGSpriteNode;
64class QSGRenderContext;
65class QSGRenderTarget;
66class QRhi;
67class QRhiRenderTarget;
68class QRhiRenderPassDescriptor;
69class QRhiCommandBuffer;
70class QQuickGraphicsConfiguration;
71class QQuickItem;
72class QSGCurveGlyphAtlas;
73
74Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TIME_RENDERLOOP)
75Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TIME_COMPILATION)
76Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TIME_TEXTURE)
77Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TIME_GLYPH)
78Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_TIME_RENDERER)
79
80Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_INFO)
81Q_DECLARE_LOGGING_CATEGORY(QSG_LOG_RENDERLOOP)
82
83class Q_QUICK_EXPORT QSGContext : public QObject
84{
85 Q_OBJECT
86
87public:
88 enum AntialiasingMethod {
89 UndecidedAntialiasing,
90 VertexAntialiasing,
91 MsaaAntialiasing
92 };
93
94 explicit QSGContext(QObject *parent = nullptr);
95 ~QSGContext() override;
96
97 virtual void renderContextInitialized(QSGRenderContext *renderContext);
98 virtual void renderContextInvalidated(QSGRenderContext *renderContext);
99 virtual QSGRenderContext *createRenderContext() = 0;
100
101 QSGInternalRectangleNode *createInternalRectangleNode(const QRectF &rect, const QColor &c);
102 virtual QSGInternalRectangleNode *createInternalRectangleNode() = 0;
103 virtual QSGInternalImageNode *createInternalImageNode(QSGRenderContext *renderContext) = 0;
104 virtual QSGInternalTextNode *createInternalTextNode(QSGRenderContext *renderContext);
105 virtual QSGPainterNode *createPainterNode(QQuickPaintedItem *item) = 0;
106 virtual QSGGlyphNode *createGlyphNode(QSGRenderContext *rc, QSGTextNode::RenderType renderType, int renderTypeQuality) = 0;
107 virtual QSGLayer *createLayer(QSGRenderContext *renderContext) = 0;
108 virtual QSGGuiThreadShaderEffectManager *createGuiThreadShaderEffectManager();
109 virtual QSGShaderEffectNode *createShaderEffectNode(QSGRenderContext *renderContext);
110#if QT_CONFIG(quick_sprite)
111 virtual QSGSpriteNode *createSpriteNode() = 0;
112#endif
113 virtual QAnimationDriver *createAnimationDriver(QObject *parent);
114 virtual float vsyncIntervalForAnimationDriver(QAnimationDriver *driver);
115 virtual bool isVSyncDependent(QAnimationDriver *driver);
116
117 virtual QSize minimumFBOSize() const;
118 virtual QSurfaceFormat defaultSurfaceFormat() const = 0;
119
120 virtual QSGRendererInterface *rendererInterface(QSGRenderContext *renderContext);
121
122 virtual QSGTextNode *createTextNode(QSGRenderContext *renderContext);
123 virtual QSGRectangleNode *createRectangleNode() = 0;
124 virtual QSGImageNode *createImageNode() = 0;
125 virtual QSGNinePatchNode *createNinePatchNode() = 0;
126
127 static QSGContext *createDefaultContext();
128 static QQuickTextureFactory *createTextureFactoryFromImage(const QImage &image);
129 static QSGRenderLoop *createWindowManager();
130
131 static void setBackend(const QString &backend);
132 static QString backend();
133};
134
135class Q_QUICK_EXPORT QSGRenderContext : public QObject
136{
137 Q_OBJECT
138public:
139 enum CreateTextureFlags {
140 CreateTexture_Alpha = 0x1,
141 CreateTexture_Atlas = 0x2,
142 CreateTexture_Mipmap = 0x4
143 };
144
145 QSGRenderContext(QSGContext *context);
146 ~QSGRenderContext() override;
147
148 QSGContext *sceneGraphContext() const { return m_sg; }
149 virtual bool isValid() const { return true; }
150
151 struct InitParams { };
152 virtual void initialize(const InitParams *params);
153 virtual void invalidate();
154
155 using RenderPassCallback = void (*)(void *);
156
157 virtual void prepareSync(qreal devicePixelRatio,
158 QRhiCommandBuffer *cb,
159 const QQuickGraphicsConfiguration &config);
160
161 virtual void beginNextFrame(QSGRenderer *renderer, const QSGRenderTarget &renderTarget,
162 RenderPassCallback mainPassRecordingStart,
163 RenderPassCallback mainPassRecordingEnd,
164 void *callbackUserData);
165 virtual void renderNextFrame(QSGRenderer *renderer) = 0;
166 virtual void endNextFrame(QSGRenderer *renderer);
167
168 virtual void endSync();
169
170 virtual void preprocess();
171 virtual void invalidateGlyphCaches();
172 virtual void flushGlyphCaches();
173 virtual QSGDistanceFieldGlyphCache *distanceFieldGlyphCache(const QRawFont &font, int renderTypeQuality);
174 virtual QSGCurveGlyphAtlas *curveGlyphAtlas(const QRawFont &font);
175 QSGTexture *textureForFactory(QQuickTextureFactory *factory, QQuickWindow *window);
176
177 virtual QSGTexture *createTexture(const QImage &image, uint flags = CreateTexture_Alpha) const = 0;
178 virtual QSGRenderer *createRenderer(QSGRendererInterface::RenderMode renderMode = QSGRendererInterface::RenderMode2D) = 0;
179 virtual QSGTexture *compressedTextureForFactory(const QSGCompressedTextureFactory *) const;
180
181 virtual int maxTextureSize() const = 0;
182
183 void unregisterFontengineForCleanup(QFontEngine *engine);
184 void registerFontengineForCleanup(QFontEngine *engine);
185
186 virtual QRhi *rhi() const;
187
188Q_SIGNALS:
189 void initialized();
190 void invalidated();
191 void releaseCachedResourcesRequested();
192
193public Q_SLOTS:
194 void textureFactoryDestroyed(QObject *o);
195
196protected:
197 struct FontKey {
198 FontKey(const QRawFont &font, int renderTypeQuality);
199
200 QFontEngine::FaceId faceId;
201 QFont::Style style;
202 int weight;
203 int renderTypeQuality;
204 QString familyName;
205 QString styleName;
206 };
207 friend bool operator==(const QSGRenderContext::FontKey &f1, const QSGRenderContext::FontKey &f2);
208 friend size_t qHash(const QSGRenderContext::FontKey &f, size_t seed);
209
210 // Hold m_sg with QPointer in the rare case it gets deleted before us.
211 QPointer<QSGContext> m_sg;
212
213 QMutex m_mutex;
214 QHash<QObject *, QSGTexture *> m_textures;
215 QSet<QSGTexture *> m_texturesToDelete;
216 QHash<FontKey, QSGDistanceFieldGlyphCache *> m_glyphCaches;
217 QList<QSGDistanceFieldGlyphCache *> m_staleGlyphCaches;
218
219 // References to font engines that are currently in use by native rendering glyph nodes
220 // and which must be kept alive as long as they are used in the render thread.
221 QHash<QFontEngine *, int> m_fontEnginesToClean;
222};
223
224inline bool operator ==(const QSGRenderContext::FontKey &f1, const QSGRenderContext::FontKey &f2)
225{
226 return f1.faceId == f2.faceId
227 && f1.style == f2.style
228 && f1.weight == f2.weight
229 && f1.renderTypeQuality == f2.renderTypeQuality
230 && f1.familyName == f2.familyName
231 && f1.styleName == f2.styleName;
232}
233
234inline size_t qHash(const QSGRenderContext::FontKey &f, size_t seed = 0)
235{
236 return qHashMulti(seed, f.faceId, f.renderTypeQuality, f.familyName, f.styleName, f.style, f.weight);
237}
238
239
240QT_END_NAMESPACE
241
242#endif // QSGCONTEXT_H
friend bool operator==(const QByteArray::FromBase64Result &lhs, const QByteArray::FromBase64Result &rhs) noexcept
Returns true if lhs and rhs are equal, otherwise returns false.
Definition qbytearray.h:801
#define QSG_RHI_DISTANCEFIELD_GLYPH_CACHE_PADDING
#define FROM_FIXED_POINT(value)
constexpr size_t qHash(const QSize &s, size_t seed=0) noexcept
Definition qsize.h:191