Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qsgdefaultrendercontext.cpp
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
5
6#include <QtGui/QGuiApplication>
7
8#include <QtQuick/private/qsgbatchrenderer_p.h>
9#include <QtQuick/private/qsgrenderer_p.h>
10#include <QtQuick/private/qsgrhiatlastexture_p.h>
11#include <QtQuick/private/qsgrhidistancefieldglyphcache_p.h>
12#include <QtQuick/private/qsgmaterialshader_p.h>
13
14#include <QtQuick/private/qsgcompressedtexture_p.h>
15
16#include <QtQuick/qsgrendererinterface.h>
17#include <QtQuick/qquickgraphicsconfiguration.h>
18
20
23 , m_rhi(nullptr)
24 , m_maxTextureSize(0)
25 , m_rhiAtlasManager(nullptr)
26 , m_currentFrameCommandBuffer(nullptr)
27 , m_currentFrameRenderPass(nullptr)
28 , m_useDepthBufferFor2D(true)
29 , m_glyphCacheResourceUpdates(nullptr)
30{
31}
32
38{
39 if (!m_sg)
40 return;
41
42 const InitParams *initParams = static_cast<const InitParams *>(params);
43 if (initParams->sType != INIT_PARAMS_MAGIC)
44 qFatal("QSGDefaultRenderContext: Invalid parameters passed to initialize()");
45
46 m_initParams = *initParams;
47
52
54
56
58}
59
61{
62 {
63 auto it = m_glyphCaches.begin();
64 while (it != m_glyphCaches.end()) {
65 if (!(*it)->isActive()) {
66 delete *it;
68 } else {
69 ++it;
70 }
71 }
72 }
73
74 {
76 while (it != m_fontEnginesToClean.end()) {
77 if (it.value() == 0) {
78 it.key()->clearGlyphCache(this);
79 if (!it.key()->ref.deref())
80 delete it.key();
82 } else {
83 ++it;
84 }
85 }
86 }
87}
88
90{
91 if (!m_rhi)
92 return;
93
96
99
100 /* The cleanup of the atlas textures is a bit intriguing.
101 As part of the cleanup in the threaded render loop, we
102 do:
103 1. call this function
104 2. call QCoreApp::sendPostedEvents() to immediately process
105 any pending deferred deletes.
106 3. delete the GL context.
107
108 As textures need the atlas manager while cleaning up, the
109 manager needs to be cleaned up after the textures, so
110 we post a deleteLater here at the very bottom so it gets
111 deferred deleted last.
112
113 Another alternative would be to use a QPointer in
114 QSGOpenGLAtlasTexture::Texture, but this seemed simpler.
115 */
116 if (m_rhiAtlasManager) {
119 m_rhiAtlasManager = nullptr;
120 }
121
122 // The following piece of code will read/write to the font engine's caches,
123 // potentially from different threads. However, this is safe because this
124 // code is only called from QQuickWindow's shutdown which is called
125 // only when the GUI is blocked, and multiple threads will call it in
126 // sequence. (see qsgdefaultglyphnode_p.cpp's init())
128 it.key()->clearGlyphCache(this);
129 if (!it.key()->ref.deref())
130 delete it.key();
131 }
133
136
138
139 m_rhi = nullptr;
140
141 if (m_sg)
143
145}
146
150{
151 m_currentDevicePixelRatio = devicePixelRatio;
152 m_useDepthBufferFor2D = config.isDepthBufferEnabledFor2D();
153
154 // we store the command buffer already here, in case there is something in
155 // an updatePaintNode() implementation that leads to needing it (for
156 // example, an updateTexture() call on a QSGRhiLayer)
158}
159
161 RenderPassCallback mainPassRecordingStart,
162 RenderPassCallback mainPassRecordingEnd,
163 void *callbackUserData)
164{
165 renderer->setRenderTarget(renderTarget);
166 renderer->setRenderPassRecordingCallbacks(mainPassRecordingStart, mainPassRecordingEnd, callbackUserData);
167
168 m_currentFrameCommandBuffer = renderTarget.cb; // usually the same as what was passed to prepareSync() but cannot count on that having been called
169 m_currentFrameRenderPass = renderTarget.rpDesc;
170}
171
173{
174 renderer->renderScene();
175}
176
178{
181 m_currentFrameRenderPass = nullptr;
182}
183
185{
186 bool atlas = flags & CreateTexture_Atlas;
187 bool mipmap = flags & CreateTexture_Mipmap;
189
190 // The atlas implementation is only supported from the render thread and
191 // does not support mipmaps.
192 if (m_rhi) {
193 if (!mipmap && atlas && QThread::currentThread() == m_rhi->thread()) {
195 if (t)
196 return t;
197 }
198 }
199
202 if (texture->hasAlphaChannel() && !alpha)
203 texture->setHasAlphaChannel(false);
204
205 return texture;
206}
207
209{
210 return new QSGBatchRenderer::Renderer(this, renderMode);
211}
212
214{
215 // This is only used for atlasing compressed textures. Returning null implies no atlas.
216
219
220 return nullptr;
221}
222
224{
226 if (!fe->faceId().filename.isEmpty()) {
228 fe->faceId().filename + ' ' + QByteArray::number(fe->faceId().index)
231 + ' ' + QByteArray::number(renderTypeQuality)
232 + QByteArray(" DF");
234 } else {
235 return QString::fromLatin1("%1_%2_%3_%4_%5")
236 .arg(font.familyName())
237 .arg(font.styleName())
238 .arg(font.weight())
239 .arg(font.style())
240 .arg(renderTypeQuality);
241 }
242}
243
245{
247}
248
250{
251 for (auto it = m_glyphCaches.begin(); it != m_glyphCaches.end(); ++it) {
252 it.value()->processPendingGlyphs();
253 it.value()->update();
254 }
255}
256
258{
259 QString key = fontKey(font, renderTypeQuality);
261 if (!cache) {
262 cache = new QSGRhiDistanceFieldGlyphCache(this, font, renderTypeQuality);
264 }
265
266 return cache;
267}
268
270{
272}
273
275{
278
280}
281
283{
284 if (texture)
286}
287
289{
293 }
294
295 for (QRhiTexture *t : std::as_const(m_pendingGlyphCacheTextures))
296 t->deleteLater(); // the QRhiTexture object stays valid for the current frame
297
299}
300
302
303#include "moc_qsgdefaultrendercontext_p.cpp"
\inmodule QtCore
Definition qbytearray.h:57
bool isEmpty() const noexcept
Returns true if the byte array has size 0; otherwise returns false.
Definition qbytearray.h:106
static QByteArray number(int, int base=10)
Returns a byte-array representing the whole number n as text.
virtual FaceId faceId() const
QString styleName() const
Definition qfont.cpp:828
Weight weight() const
Returns the weight of the font, using the same scale as the \l{QFont::Weight} enumeration.
Definition qfont.cpp:1118
Style style() const
Returns the style of the font.
Definition qfont.cpp:1090
@ Normal
Definition qfont.h:64
@ StyleNormal
Definition qfont.h:74
iterator begin()
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1202
const_iterator constEnd() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the ...
Definition qhash.h:1209
const_iterator constBegin() const noexcept
Returns a const \l{STL-style iterators}{STL-style iterator} pointing to the first item in the hash.
Definition qhash.h:1205
iterator erase(const_iterator it)
Definition qhash.h:1223
T value(const Key &key) const noexcept
Definition qhash.h:1044
iterator end() noexcept
Returns an \l{STL-style iterators}{STL-style iterator} pointing to the imaginary item after the last ...
Definition qhash.h:1206
void clear() noexcept(std::is_nothrow_destructible< Node >::value)
Removes all items from the hash and frees up all memory used by it.
Definition qhash.h:949
iterator insert(const Key &key, const T &value)
Inserts a new item with the key and a value of value.
Definition qhash.h:1283
\inmodule QtGui
Definition qimage.h:37
void deleteLater()
\threadsafe
Definition qobject.cpp:2352
QQuickGraphicsConfiguration controls lower level graphics settings for the QQuickWindow.
static QRawFontPrivate * get(const QRawFont &font)
Definition qrawfont_p.h:104
QFontEngine * fontEngine
Definition qrawfont_p.h:106
The QRawFont class provides access to a single physical instance of a font.
Definition qrawfont.h:24
\inmodule QtGui
Definition qrhi.h:1614
\inmodule QtGui
Definition qrhi.h:1694
\inmodule QtGui
Definition qrhi.h:883
int resourceLimit(ResourceLimit limit) const
Definition qrhi.cpp:9692
QThread * thread() const
Definition qrhi.cpp:8428
@ TextureSizeMax
Definition qrhi.h:1848
QRhiResourceUpdateBatch * nextResourceUpdateBatch()
Definition qrhi.cpp:8854
The QSGContext holds the scene graph entry points for one QML engine.
virtual void renderContextInitialized(QSGRenderContext *renderContext)
virtual void renderContextInvalidated(QSGRenderContext *renderContext)
static QString fontKey(const QRawFont &font, int renderTypeQuality)
void deferredReleaseGlyphCacheTexture(QRhiTexture *texture)
QRhiResourceUpdateBatch * maybeGlyphCacheResourceUpdates()
void prepareSync(qreal devicePixelRatio, QRhiCommandBuffer *cb, const QQuickGraphicsConfiguration &config) override
void beginNextFrame(QSGRenderer *renderer, const QSGRenderTarget &renderTarget, RenderPassCallback mainPassRecordingStart, RenderPassCallback mainPassRecordingEnd, void *callbackUserData) override
virtual void initializeRhiShader(QSGMaterialShader *shader, QShader::Variant shaderVariant)
void initialize(const QSGRenderContext::InitParams *params) override
Initializes the scene graph render context with the GL context context.
QSGRenderer * createRenderer(QSGRendererInterface::RenderMode renderMode=QSGRendererInterface::RenderMode2D) override
QSGTexture * compressedTextureForFactory(const QSGCompressedTextureFactory *factory) const override
Return the texture corresponding to a texture factory.
QSGTexture * createTexture(const QImage &image, uint flags) const override
QSGDefaultRenderContext(QSGContext *context)
QRhiResourceUpdateBatch * m_glyphCacheResourceUpdates
QSGDistanceFieldGlyphCache * distanceFieldGlyphCache(const QRawFont &font, int renderTypeQuality) override
Factory function for scene graph backends of the distance-field glyph cache.
QSet< QRhiTexture * > m_pendingGlyphCacheTextures
QRhiRenderPassDescriptor * m_currentFrameRenderPass
void preprocess() override
Do necessary preprocessing before the frame.
QSGRhiAtlasTexture::Manager * m_rhiAtlasManager
void endNextFrame(QSGRenderer *renderer) override
void renderNextFrame(QSGRenderer *renderer) override
QRhiResourceUpdateBatch * glyphCacheResourceUpdates()
QRhiCommandBuffer * m_currentFrameCommandBuffer
static QSGMaterialShaderPrivate * get(QSGMaterialShader *s)
void prepare(QShader::Variant vertexShaderVariant)
The QSGMaterialShader class represents a graphics API independent shader program.
void setImage(const QImage &image)
QHash< QString, QSGDistanceFieldGlyphCache * > m_glyphCaches
QHash< QObject *, QSGTexture * > m_textures
QHash< QFontEngine *, int > m_fontEnginesToClean
QPointer< QSGContext > m_sg
void(*)(void *) RenderPassCallback
QSet< QSGTexture * > m_texturesToDelete
QRhiCommandBuffer * cb
QRhiRenderPassDescriptor * rpDesc
RenderMode
\value RenderMode2D Normal 2D rendering \value RenderMode2DNoDepthBuffer Normal 2D rendering with dep...
The renderer class is the abstract baseclass used for rendering the QML scene graph.
QSGTexture * create(const QImage &image, bool hasAlphaChannel)
\inmodule QtQuick
Definition qsgtexture.h:20
void clear()
Definition qset.h:61
iterator insert(const T &value)
Definition qset.h:155
Variant
Describes what kind of shader code an entry contains.
Definition qshader.h:103
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
static QString fromLatin1(QByteArrayView ba)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5710
static QString fromUtf8(QByteArrayView utf8)
This is an overloaded member function, provided for convenience. It differs from the above function o...
Definition qstring.cpp:5857
QString arg(qlonglong a, int fieldwidth=0, int base=10, QChar fillChar=u' ') const
Definition qstring.cpp:8606
static QThread * currentThread()
Definition qthread.cpp:966
qDeleteAll(list.begin(), list.end())
QCache< int, Employee > cache
[0]
QSet< QString >::iterator it
Combined button and popup list for selecting options.
Definition image.cpp:4
static void * context
EGLConfig config
#define qFatal
Definition qlogging.h:164
GLuint64 key
GLbitfield flags
GLenum GLuint texture
void ** params
GLuint shader
Definition qopenglext.h:665
GLdouble GLdouble t
Definition qopenglext.h:243
GLfloat GLfloat GLfloat alpha
Definition qopenglext.h:418
static QString keyName(const QString &rKey)
SSL_CTX int(* cb)(SSL *ssl, unsigned char **out, unsigned char *outlen, const unsigned char *in, unsigned int inlen, void *arg)
#define emit
#define Q_UNUSED(x)
unsigned int uint
Definition qtypes.h:29
double qreal
Definition qtypes.h:92
QObject::connect nullptr
QItemEditorFactory * factory
QSvgRenderer * renderer
[0]